75 lines
2.5 KiB
Python
75 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Seed >=10k messages and print EXPLAIN (ANALYZE, BUFFERS) for keyset page."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import uuid
|
|
from datetime import datetime, timezone
|
|
|
|
import psycopg
|
|
from psycopg.rows import dict_row
|
|
|
|
|
|
def utc_now():
|
|
return datetime.now(timezone.utc).replace(microsecond=0)
|
|
|
|
|
|
def main() -> None:
|
|
url = open("/run/secrets/database_url", encoding="utf-8").read().strip()
|
|
with psycopg.connect(url, row_factory=dict_row) as conn:
|
|
with conn.transaction():
|
|
u1, u2 = uuid.uuid4(), uuid.uuid4()
|
|
now = utc_now()
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO users (id, username, display_name, password_hash, created_at)
|
|
VALUES (%s, %s, %s, %s, %s)
|
|
""",
|
|
(u1, f"explain_a_{u1.hex[:8]}", "A", "x", now),
|
|
)
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO users (id, username, display_name, password_hash, created_at)
|
|
VALUES (%s, %s, %s, %s, %s)
|
|
""",
|
|
(u2, f"explain_b_{u2.hex[:8]}", "B", "x", now),
|
|
)
|
|
chat = uuid.uuid4()
|
|
conn.execute(
|
|
"INSERT INTO chats (id, title, created_at) VALUES (%s, %s, %s)",
|
|
(chat, "explain", now),
|
|
)
|
|
conn.execute(
|
|
"INSERT INTO chat_members (chat_id, user_id) VALUES (%s, %s), (%s, %s)",
|
|
(chat, u1, chat, u2),
|
|
)
|
|
rows = [(uuid.uuid4(), chat, u1, f"m{i}", now) for i in range(10000)]
|
|
with conn.cursor() as cur:
|
|
cur.executemany(
|
|
"""
|
|
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
|
VALUES (%s, %s, %s, %s, %s)
|
|
""",
|
|
rows,
|
|
)
|
|
mid = rows[5000][0]
|
|
plan = conn.execute(
|
|
"""
|
|
EXPLAIN (ANALYZE, BUFFERS)
|
|
SELECT id, chat_id, sender_id, text, created_at
|
|
FROM messages
|
|
WHERE chat_id = %s AND (created_at, id) > (%s, %s)
|
|
ORDER BY created_at ASC, id ASC
|
|
LIMIT 51
|
|
""",
|
|
(chat, now, mid),
|
|
).fetchall()
|
|
for row in plan:
|
|
print(next(iter(row.values())))
|
|
print("CHAT_ID", chat)
|
|
print("SEEDED", 10000)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|