forked from mtusi2026/mtusi2026lab2
82 lines
3.9 KiB
Bash
Executable File
82 lines
3.9 KiB
Bash
Executable File
#!/bin/sh
|
|
# Seed synthetic history (20 000 messages in one chat + 50 000 in 50 other chats) and
|
|
# record EXPLAIN (ANALYZE, BUFFERS) of the exact keyset queries used by the API,
|
|
# with and without messages_chat_history_idx. Output: evidence/explain_history.txt
|
|
# Seeding is idempotent (fixed IDs, ON CONFLICT DO NOTHING); data is synthetic.
|
|
set -eu
|
|
|
|
cd "$(dirname "$0")/.."
|
|
OUT="${OUT:-evidence/explain_history.txt}"
|
|
PSQL="docker compose exec -T postgres psql -U messenger -d messenger -X -v ON_ERROR_STOP=1"
|
|
|
|
$PSQL -q <<'SQL'
|
|
INSERT INTO users (id, username, display_name, password_hash) VALUES
|
|
('00000000-0000-4000-8000-000000000001', 'seed_explain_a', 'Seed A', 'not-a-login'),
|
|
('00000000-0000-4000-8000-000000000002', 'seed_explain_b', 'Seed B', 'not-a-login')
|
|
ON CONFLICT DO NOTHING;
|
|
|
|
-- chat ...0100 is the measured one; ...0101-0150 are background noise.
|
|
INSERT INTO chats (id, title, created_at)
|
|
SELECT ('00000000-0000-4000-8000-' || lpad((100 + g)::text, 12, '0'))::uuid, 'explain ' || g, now()
|
|
FROM generate_series(0, 50) g
|
|
ON CONFLICT DO NOTHING;
|
|
|
|
INSERT INTO chat_members (chat_id, user_id, position)
|
|
SELECT c.id, u.id, u.pos
|
|
FROM chats c
|
|
CROSS JOIN (VALUES ('00000000-0000-4000-8000-000000000001'::uuid, 0),
|
|
('00000000-0000-4000-8000-000000000002'::uuid, 1)) AS u(id, pos)
|
|
WHERE c.title LIKE 'explain %'
|
|
ON CONFLICT DO NOTHING;
|
|
|
|
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
|
SELECT md5('explain-' || c.n || '-' || g)::uuid,
|
|
('00000000-0000-4000-8000-' || lpad((100 + c.n)::text, 12, '0'))::uuid,
|
|
CASE WHEN g % 2 = 0 THEN '00000000-0000-4000-8000-000000000001'::uuid
|
|
ELSE '00000000-0000-4000-8000-000000000002'::uuid END,
|
|
'synthetic message ' || g,
|
|
timestamptz '2026-01-01' + (g * interval '1 second') + (c.n * interval '1 millisecond')
|
|
FROM (SELECT n, CASE WHEN n = 0 THEN 20000 ELSE 1000 END AS cnt FROM generate_series(0, 50) n) c
|
|
CROSS JOIN LATERAL generate_series(1, c.cnt) g
|
|
ON CONFLICT DO NOTHING;
|
|
|
|
ANALYZE users; ANALYZE chats; ANALYZE chat_members; ANALYZE messages;
|
|
SQL
|
|
|
|
{
|
|
echo "# EXPLAIN evidence for message history pagination"
|
|
echo "# date: $(date -u +%Y-%m-%dT%H:%M:%SZ) commit: $(git rev-parse --short HEAD 2>/dev/null || echo n/a)"
|
|
echo "# postgres: $($PSQL -tA -c 'SHOW server_version')"
|
|
$PSQL -tA -c "SELECT '# messages total: ' || count(*) || ', in measured chat: ' || count(*) FILTER (WHERE chat_id = '00000000-0000-4000-8000-000000000100') FROM messages"
|
|
echo
|
|
$PSQL <<'SQL'
|
|
-- The same statements as app/storage.py (Store.list_messages), as prepared statements.
|
|
PREPARE first_page(uuid, int) AS
|
|
SELECT id, chat_id, sender_id, text, created_at FROM messages
|
|
WHERE chat_id = $1 ORDER BY created_at, id LIMIT $2;
|
|
PREPARE next_page(uuid, timestamptz, uuid, int) AS
|
|
SELECT id, chat_id, sender_id, text, created_at FROM messages
|
|
WHERE chat_id = $1 AND (created_at, id) > ($2, $3) ORDER BY created_at, id LIMIT $4;
|
|
|
|
\echo '=== 1. WITH index messages_chat_history_idx: first page (limit 50 + 1) ==='
|
|
EXPLAIN (ANALYZE, BUFFERS) EXECUTE first_page('00000000-0000-4000-8000-000000000100', 51);
|
|
|
|
\echo '=== 2. WITH index: deep page, cursor after message #15000 of 20000 ==='
|
|
EXPLAIN (ANALYZE, BUFFERS) EXECUTE next_page('00000000-0000-4000-8000-000000000100',
|
|
timestamptz '2026-01-01' + interval '15000 seconds', md5('explain-0-15000')::uuid, 51);
|
|
|
|
-- Same queries without the index, inside a transaction that is rolled back.
|
|
BEGIN;
|
|
DROP INDEX messages_chat_history_idx;
|
|
\echo '=== 3. WITHOUT index: first page ==='
|
|
EXPLAIN (ANALYZE, BUFFERS) EXECUTE first_page('00000000-0000-4000-8000-000000000100', 51);
|
|
\echo '=== 4. WITHOUT index: deep page ==='
|
|
EXPLAIN (ANALYZE, BUFFERS) EXECUTE next_page('00000000-0000-4000-8000-000000000100',
|
|
timestamptz '2026-01-01' + interval '15000 seconds', md5('explain-0-15000')::uuid, 51);
|
|
ROLLBACK;
|
|
SQL
|
|
} > "$OUT"
|
|
|
|
echo "written $OUT"
|
|
grep -E '^===|Index|Seq Scan|Sort|Buffers|Execution Time' "$OUT"
|