Files
mtusi2026lab2/scripts/explain_history.py
T
2026-10-21 20:10:27 +03:00

30 lines
1.1 KiB
Python

#!/usr/bin/env python3
"""Run the 10k-row message history plan probe inside a rolled-back transaction."""
from pathlib import Path
import subprocess
root = Path(__file__).resolve().parents[1]
sql = (root / 'scripts' / 'explain_message_history.sql').read_text(encoding='utf-8')
result = subprocess.run(
[
'docker', 'compose', '--env-file', '.env', 'exec', '-T', 'postgres',
'sh', '-lc', 'psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "$POSTGRES_DB"'
],
cwd=root,
input=sql,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=False,
)
print(result.stdout, end='')
if result.returncode != 0:
raise SystemExit(result.returncode)
plan = result.stdout[result.stdout.find('QUERY PLAN'):]
if 'Index Scan' not in plan or 'messages_chat_created_id_idx' not in plan:
raise SystemExit('FAIL: expected messages_chat_created_id_idx index scan')
if 'Seq Scan on messages' in plan:
raise SystemExit('FAIL: message history used a sequential scan')
print('PASS: 10,000-row keyset page used messages_chat_created_id_idx without a full scan.')