Files
mtusi2026lab3/scripts/explain_history.py
T

77 lines
3.9 KiB
Python

"""Seed an isolated synthetic chat, measure the actual keyset SQL, keep raw plans."""
from datetime import datetime, timedelta, timezone
import json
from pathlib import Path
import sys
from urllib.parse import quote
from uuid import UUID
import psycopg
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
sys.path.insert(0, str(ROOT / "tests"))
from app.config import secret
from app.security import Cursors, stamp
from client import Client, check
def walk(node):
yield node
for child in node.get("Plans", []):
yield from walk(child)
def main():
c = Client()
actor = c.user("explain")
chat = c.call("POST", "/api/v1/chats", 201, actor=actor,
body={"title": "EXPLAIN 10000", "member_ids": [actor["id"]]}, schema="Chat")["json"]
base = datetime.now(timezone.utc)
with psycopg.connect(secret("DATABASE_URL"), connect_timeout=2) as conn:
conn.execute("""INSERT INTO messages(id, chat_id, sender_id, text, created_at)
SELECT gen_random_uuid(), %s, %s, 'synthetic-' || i,
%s + i * interval '1 microsecond'
FROM generate_series(1, 10000) AS i""", (UUID(chat["id"]), UUID(actor["id"]), base))
conn.execute("UPDATE chats SET last_message_at=%s WHERE id=%s",
(base + timedelta(microseconds=10000), UUID(chat["id"])))
conn.execute("ANALYZE messages")
report = {"messages_in_chat": 10000, "page_limit": 50,
"index": "messages_chat_created_id_idx", "cases": {}}
text_plans = []
cursors = Cursors(secret("CURSOR_KEY").encode())
with psycopg.connect(secret("DATABASE_URL"), connect_timeout=2) as conn:
for label, offset in (("first", 0), ("middle", 5000), ("late", 9900)):
sql = "SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id=%s"
params = [UUID(chat["id"])]
path = "/api/v1/chats/" + chat["id"] + "/messages?limit=50"
if offset:
boundary = conn.execute("SELECT created_at, id FROM messages WHERE chat_id=%s AND text=%s",
(UUID(chat["id"]), "synthetic-" + str(offset))).fetchone()
sql += " AND (created_at, id) > (%s, %s)"
params.extend(boundary)
token = cursors.encode("messages:" + chat["id"],
{"created_at": stamp(boundary[0]), "id": str(boundary[1])})
path += "&cursor=" + quote(token, safe="")
sql += " ORDER BY created_at, id LIMIT %s"
params.append(51)
data = conn.execute("EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) " + sql, params).fetchone()[0][0]
nodes = list(walk(data["Plan"]))
check(not any(x["Node Type"] == "Seq Scan" for x in nodes), "Unexpected full scan in keyset query")
check(any(x.get("Index Name") == report["index"] for x in nodes), "History index not used")
check(data["Plan"]["Actual Rows"] == 51, "LIMIT was not respected")
page = c.call("GET", path, actor=actor, schema="MessagePage")["json"]
check(len(page["items"]) == 50 and page["next_cursor"], "HTTP page differs from measured SQL")
expected = ["synthetic-" + str(i) for i in range(offset + 1, offset + 51)]
check([x["text"] for x in page["items"]] == expected, "HTTP keyset boundary differs")
report["cases"][label] = {"after_message": offset, "sql": sql, "plan": data}
text_plans.append(label.upper() + "\n" + sql + "\n" + "\n".join(
row[0] for row in conn.execute("EXPLAIN (ANALYZE, BUFFERS) " + sql, params).fetchall()))
(ROOT / "evidence" / "explain.json").write_text(json.dumps(report, indent=2) + "\n")
(ROOT / "evidence" / "explain.txt").write_text("\n\n".join(text_plans) + "\n")
print("PASS: 10000 messages, first/middle/late pages use history index; 51 SQL rows for 50 HTTP items")
if __name__ == "__main__":
main()