diff --git a/evidence/make-contract-go.txt b/evidence/make-contract-go.txt new file mode 100644 index 0000000..1216af4 Binary files /dev/null and b/evidence/make-contract-go.txt differ diff --git a/evidence/make-failure-go.txt b/evidence/make-failure-go.txt new file mode 100644 index 0000000..1e8071c --- /dev/null +++ b/evidence/make-failure-go.txt @@ -0,0 +1,9 @@ +> make failure-go +go test -tags=failure ./student-tests/... -v -args -base-url=http://localhost:8080 +=== RUN TestRedisDown +--- PASS: TestRedisDown (2.12s) +=== RUN --- PASS: TestDatabaseDown (10.90s) +=== RUN TestSessionExpiry +--- PASS: TestSessionExpiry (5.77s) +PASS +ok mtusi2026-messenger/student-tests 18.803s diff --git a/evidence/make-race-go.txt b/evidence/make-race-go.txt new file mode 100644 index 0000000..6cadf69 --- /dev/null +++ b/evidence/make-race-go.txt @@ -0,0 +1,9 @@ +> make race-go +go test -tags=race ./student-tests/... -race -v -args -base-url=http://localhost:8080 +=== RUN TestConcurrentMessages + race_test.go:98: PASS: 10 concurrent clients sent 100 messages, exactly 100 unique messages persisted +--- PASS: TestConcurrentMessages (0.31s) +=== RUN TestConcurrentRegistration +--- PASS: TestConcurrentRegistration (0.12s) +PASS +ok mtusi2026-messenger/student-tests 1.443s diff --git a/evidence/make-test-persistence.txt b/evidence/make-test-persistence.txt new file mode 100644 index 0000000..b20f0a2 --- /dev/null +++ b/evidence/make-test-persistence.txt @@ -0,0 +1,4 @@ +> make test-persistence ACTION_SCRIPT=scripts/restart.sh +python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8080" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/make-test.txt b/evidence/make-test.txt new file mode 100644 index 0000000..74f1b6c --- /dev/null +++ b/evidence/make-test.txt @@ -0,0 +1,3 @@ +> make test +python3 tests/smoke.py --base-url "http://localhost:8080" +PASS: lab2 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/migrate-repeat.txt b/evidence/migrate-repeat.txt new file mode 100644 index 0000000..a201a7c --- /dev/null +++ b/evidence/migrate-repeat.txt @@ -0,0 +1,4 @@ +> docker compose run --rm --no-deps migrate + Container mtusi2026lab2-migrate-run-481a17e62bda Creating + Container mtusi2026lab2-migrate-run-481a17e62bda Created +2026/10/07 20:39:09 goose: no migrations to run. current version: 1 diff --git a/evidence/track2-backup-restore.txt b/evidence/track2-backup-restore.txt new file mode 100644 index 0000000..17196ae --- /dev/null +++ b/evidence/track2-backup-restore.txt @@ -0,0 +1,70 @@ +research=# DROP TABLE messages_seed; +DROP TABLE +Time: 4.369 ms +research=# CREATE INDEX idx_messages_chat_history ON messages (chat_id, created_at, id); +CREATE INDEX +Time: 75.459 ms +research=# ANALYZE messages; +ANALYZE +Time: 92.896 ms +research=# SELECT count(*) FROM messages; + count +-------- + 100000 +(1 row) + +Time: 7.997 ms +research=# SELECT md5(string_agg(id::text || chat_id::text || created_at::text || text, '' ORDER BY id)) AS full_hash FROM messages; + full_hash +---------------------------------- + 3b0b6c7f0769218c3d3d1a7b660f521c +(1 row) + +Time: 74.820 ms +research=# SELECT md5(string_agg(id::text || text, '' ORDER BY created_at, id)) AS page_hash FROM (SELECT * FROM messages WHERE chat_id = (SELECT id FROM chats WHERE title = 'big') AND (created_at, id) > ('2026-01-15 00:00+00', '00000000-0000-0000-0000-000000000000') ORDER BY created_at, id LIMIT 50) p; + page_hash +---------------------------------- + 1b111bf87f61b52ef916f31037c4acb5 +(1 row) + +Time: 0.736 ms +research=# \q + +> Measure-Command { docker compose exec postgres pg_dump -U messenger -d research -Fc -f /tmp/research.dump } | Select-Object TotalSeconds + +TotalSeconds +------------ + 0,6917214 +> docker compose exec postgres psql -U messenger -d postgres -c "CREATE DATABASE research_restore;" +CREATE DATABASE +> Measure-Command { docker compose exec postgres pg_restore -U messenger -d research_restore /tmp/research.dump } | Select-Object TotalSeconds + +TotalSeconds +------------ + 0,5392463 +> docker compose exec postgres ls -lh /tmp/research.dump +-rw-r--r-- 1 root root 4.1M Oct 7 19:48 /tmp/research.dump +> docker compose exec postgres psql -U messenger -d research_restore +psql (18.6 (Debian 18.6-1.pgdg13+2)) +Type "help" for help. + +research_restore=# SELECT count(*) FROM messages; + count +-------- + 100000 +(1 row) + +research_restore=# SELECT md5(string_agg(id::text || chat_id::text || created_at::text || text, '' ORDER BY id)) AS full_hash FROM messages; + full_hash +---------------------------------- + 3b0b6c7f0769218c3d3d1a7b660f521c +(1 row) + +research_restore=# SELECT md5(string_agg(id::text || text, '' ORDER BY created_at, id)) AS page_hash FROM (SELECT * FROM messages WHERE chat_id = (SELECT id FROM chats WHERE title = 'big') AND (created_at, id) > ('2026-01-15 00:00+00', '00000000-0000-0000-0000-000000000000') ORDER BY created_at, id LIMIT 50) p; + page_hash +---------------------------------- + 1b111bf87f61b52ef916f31037c4acb5 +(1 row) + +research_restore=# \q +> docker compose exec postgres rm /tmp/research.dump diff --git a/evidence/track2-explain.txt b/evidence/track2-explain.txt new file mode 100644 index 0000000..7c57f99 --- /dev/null +++ b/evidence/track2-explain.txt @@ -0,0 +1,297 @@ +C. (chat_id, id) +research=# EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM messages WHERE chat_id = (SELECT id FROM chats WHERE title = 'big') AND (created_at, id) > ('2026-01-15 00:00+00', '00000000-0000-0000-0000-000000000000') ORDER BY created_at, id LIMIT 50; + QUERY PLAN +-------------------------------------------------------------------------------------------------------------------------------------------------------- + Limit (cost=810.77..810.89 rows=50 width=151) (actual time=11.161..11.167 rows=50.00 loops=1) + Buffers: shared hit=1730 + InitPlan 1 + -> Seq Scan on chats (cost=0.00..4.58 rows=1 width=16) (actual time=0.012..0.024 rows=1.00 loops=1) + Filter: (title = 'big'::text) + Rows Removed by Filter: 205 + Buffers: shared hit=2 + -> Sort (cost=806.19..806.68 rows=193 width=151) (actual time=11.159..11.162 rows=50.00 loops=1) + Sort Key: messages.created_at, messages.id + Sort Method: top-N heapsort Memory: 36kB + Buffers: shared hit=1730 + -> Index Scan using idx_messages_chat_id on messages (cost=0.42..799.78 rows=193 width=151) (actual time=2.648..8.533 rows=39841.00 loops=1) + Index Cond: (chat_id = (InitPlan 1).col1) + Filter: (ROW(created_at, id) > ROW('2026-01-15 00:00:00+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)) + Rows Removed by Filter: 20159 + Index Searches: 1 + Buffers: shared hit=1730 + Planning Time: 0.115 ms + Execution Time: 11.189 ms +(19 rows) + +Time: 11.614 ms +research=# EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM messages WHERE chat_id = (SELECT id FROM chats WHERE title = 'big') AND (created_at, id) > ('2026-01-15 00:00+00', '00000000-0000-0000-0000-000000000000') ORDER BY created_at, id LIMIT 50; + QUERY PLAN +--------------------------------------------------------------------------------------------------------------------------------------------------------- + Limit (cost=810.77..810.89 rows=50 width=151) (actual time=13.602..13.609 rows=50.00 loops=1) + Buffers: shared hit=1730 + InitPlan 1 + -> Seq Scan on chats (cost=0.00..4.58 rows=1 width=16) (actual time=0.034..0.044 rows=1.00 loops=1) + Filter: (title = 'big'::text) + Rows Removed by Filter: 205 + Buffers: shared hit=2 + -> Sort (cost=806.19..806.68 rows=193 width=151) (actual time=13.601..13.604 rows=50.00 loops=1) + Sort Key: messages.created_at, messages.id + Sort Method: top-N heapsort Memory: 36kB + Buffers: shared hit=1730 + -> Index Scan using idx_messages_chat_id on messages (cost=0.42..799.78 rows=193 width=151) (actual time=3.012..10.954 rows=39841.00 loops=1) + Index Cond: (chat_id = (InitPlan 1).col1) + Filter: (ROW(created_at, id) > ROW('2026-01-15 00:00:00+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)) + Rows Removed by Filter: 20159 + Index Searches: 1 + Buffers: shared hit=1730 + Planning Time: 0.207 ms + Execution Time: 13.645 ms +(19 rows) + +Time: 14.423 ms +research=# EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM messages WHERE chat_id = (SELECT id FROM chats WHERE title = 'small-1') AND (created_at, id) > ('2026-01-01 00:30+00', '00000000-0000-0000-0000-000000000000') ORDER BY created_at, id LIMIT 50; + QUERY PLAN +----------------------------------------------------------------------------------------------------------------------------------------------------- + Limit (cost=819.50..819.63 rows=50 width=151) (actual time=0.140..0.148 rows=46.00 loops=1) + Buffers: shared hit=10 + InitPlan 1 + -> Seq Scan on chats (cost=0.00..4.58 rows=1 width=16) (actual time=0.011..0.020 rows=1.00 loops=1) + Filter: (title = 'small-1'::text) + Rows Removed by Filter: 205 + Buffers: shared hit=2 + -> Sort (cost=814.93..816.07 rows=456 width=151) (actual time=0.139..0.142 rows=46.00 loops=1) + Sort Key: messages.created_at, messages.id + Sort Method: quicksort Memory: 35kB + Buffers: shared hit=10 + -> Index Scan using idx_messages_chat_id on messages (cost=0.42..799.78 rows=456 width=151) (actual time=0.039..0.047 rows=46.00 loops=1) + Index Cond: (chat_id = (InitPlan 1).col1) + Filter: (ROW(created_at, id) > ROW('2026-01-01 00:30:00+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)) + Rows Removed by Filter: 29 + Index Searches: 1 + Buffers: shared hit=10 + Planning Time: 0.084 ms + Execution Time: 0.167 ms +(19 rows) + +Time: 0.532 ms +research=# EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM messages WHERE chat_id = (SELECT id FROM chats WHERE title = 'small-1') AND (created_at, id) > ('2026-01-01 00:30+00', '00000000-0000-0000-0000-000000000000') ORDER BY created_at, id LIMIT 50; + QUERY PLAN +----------------------------------------------------------------------------------------------------------------------------------------------------- + Limit (cost=819.50..819.63 rows=50 width=151) (actual time=0.126..0.131 rows=46.00 loops=1) + Buffers: shared hit=10 + InitPlan 1 + -> Seq Scan on chats (cost=0.00..4.58 rows=1 width=16) (actual time=0.032..0.042 rows=1.00 loops=1) + Filter: (title = 'small-1'::text) + Rows Removed by Filter: 205 + Buffers: shared hit=2 + -> Sort (cost=814.93..816.07 rows=456 width=151) (actual time=0.125..0.127 rows=46.00 loops=1) + Sort Key: messages.created_at, messages.id + Sort Method: quicksort Memory: 35kB + Buffers: shared hit=10 + -> Index Scan using idx_messages_chat_id on messages (cost=0.42..799.78 rows=456 width=151) (actual time=0.075..0.086 rows=46.00 loops=1) + Index Cond: (chat_id = (InitPlan 1).col1) + Filter: (ROW(created_at, id) > ROW('2026-01-01 00:30:00+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)) + Rows Removed by Filter: 29 + Index Searches: 1 + Buffers: shared hit=10 + Planning Time: 0.250 ms + Execution Time: 0.166 ms +(19 rows) + +Time: 1.156 ms + + +B. (chat_id, created_at, id) +research=# DROP INDEX IF EXISTS idx_messages_chat_id; +DROP INDEX +Time: 3.644 ms +research=# CREATE INDEX idx_messages_chat_history ON messages (chat_id, created_at, id); +CREATE INDEX +Time: 69.022 ms +research=# ANALYZE messages; +ANALYZE +Time: 103.435 ms +research=# EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM messages WHERE chat_id = (SELECT id FROM chats WHERE title = 'big') AND (created_at, id) > ('2026-01-15 00:00+00', '00000000-0000-0000-0000-000000000000') ORDER BY created_at, id LIMIT 50; + QUERY PLAN +--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Limit (cost=4.99..93.65 rows=50 width=151) (actual time=0.055..0.069 rows=50.00 loops=1) + Buffers: shared hit=4 read=4 + InitPlan 1 + -> Seq Scan on chats (cost=0.00..4.58 rows=1 width=16) (actual time=0.009..0.019 rows=1.00 loops=1) + Filter: (title = 'big'::text) + Rows Removed by Filter: 205 + Buffers: shared hit=2 + -> Index Scan using idx_messages_chat_history on messages (cost=0.42..340.85 rows=192 width=151) (actual time=0.054..0.065 rows=50.00 loops=1) + Index Cond: ((chat_id = (InitPlan 1).col1) AND (ROW(created_at, id) > ROW('2026-01-15 00:00:00+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))) + Index Searches: 1 + Buffers: shared hit=4 read=4 + Planning: + Buffers: shared hit=21 read=1 + Planning Time: 0.333 ms + Execution Time: 0.092 ms +(15 rows) + +Time: 1.018 ms +research=# EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM messages WHERE chat_id = (SELECT id FROM chats WHERE title = 'big') AND (created_at, id) > ('2026-01-15 00:00+00', '00000000-0000-0000-0000-000000000000') ORDER BY created_at, id LIMIT 50; + QUERY PLAN +--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Limit (cost=4.99..93.65 rows=50 width=151) (actual time=0.033..0.045 rows=50.00 loops=1) + Buffers: shared hit=8 + InitPlan 1 + -> Seq Scan on chats (cost=0.00..4.58 rows=1 width=16) (actual time=0.011..0.020 rows=1.00 loops=1) + Filter: (title = 'big'::text) + Rows Removed by Filter: 205 + Buffers: shared hit=2 + -> Index Scan using idx_messages_chat_history on messages (cost=0.42..340.85 rows=192 width=151) (actual time=0.032..0.041 rows=50.00 loops=1) + Index Cond: ((chat_id = (InitPlan 1).col1) AND (ROW(created_at, id) > ROW('2026-01-15 00:00:00+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))) + Index Searches: 1 + Buffers: shared hit=8 + Planning Time: 0.106 ms + Execution Time: 0.061 ms +(13 rows) + +Time: 0.464 ms +research=# EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM messages WHERE chat_id = (SELECT id FROM chats WHERE title = 'small-1') AND (created_at, id) > ('2026-01-01 00:30+00', '00000000-0000-0000-0000-000000000000') ORDER BY created_at, id LIMIT 50; + QUERY PLAN +--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Limit (cost=4.99..88.31 rows=50 width=151) (actual time=0.139..0.152 rows=46.00 loops=1) + Buffers: shared hit=7 read=2 + InitPlan 1 + -> Seq Scan on chats (cost=0.00..4.58 rows=1 width=16) (actual time=0.015..0.026 rows=1.00 loops=1) + Filter: (title = 'small-1'::text) + Rows Removed by Filter: 205 + Buffers: shared hit=2 + -> Index Scan using idx_messages_chat_history on messages (cost=0.42..758.65 rows=455 width=151) (actual time=0.137..0.147 rows=46.00 loops=1) + Index Cond: ((chat_id = (InitPlan 1).col1) AND (ROW(created_at, id) > ROW('2026-01-01 00:30:00+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))) + Index Searches: 1 + Buffers: shared hit=7 read=2 + Planning Time: 0.130 ms + Execution Time: 0.174 ms +(13 rows) + +Time: 0.581 ms +research=# EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM messages WHERE chat_id = (SELECT id FROM chats WHERE title = 'small-1') AND (created_at, id) > ('2026-01-01 00:30+00', '00000000-0000-0000-0000-000000000000') ORDER BY created_at, id LIMIT 50; + QUERY PLAN +--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Limit (cost=4.99..88.31 rows=50 width=151) (actual time=0.064..0.073 rows=46.00 loops=1) + Buffers: shared hit=9 + InitPlan 1 + -> Seq Scan on chats (cost=0.00..4.58 rows=1 width=16) (actual time=0.030..0.039 rows=1.00 loops=1) + Filter: (title = 'small-1'::text) + Rows Removed by Filter: 205 + Buffers: shared hit=2 + -> Index Scan using idx_messages_chat_history on messages (cost=0.42..758.65 rows=455 width=151) (actual time=0.063..0.069 rows=46.00 loops=1) + Index Cond: ((chat_id = (InitPlan 1).col1) AND (ROW(created_at, id) > ROW('2026-01-01 00:30:00+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))) + Index Searches: 1 + Buffers: shared hit=9 + Planning Time: 0.208 ms + Execution Time: 0.100 ms +(13 rows) + +Time: 0.808 ms + +A. Only PK +research=# DROP INDEX IF EXISTS idx_messages_chat_history; +DROP INDEX +Time: 5.715 ms +research=# DROP INDEX IF EXISTS idx_messages_chat_id; +NOTICE: index "idx_messages_chat_id" does not exist, skipping +DROP INDEX +Time: 0.228 ms +research=# ANALYZE messages; +ANALYZE +Time: 102.311 ms +research=# EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM messages WHERE chat_id = (SELECT id FROM chats WHERE title = 'big') AND (created_at, id) > ('2026-01-15 00:00+00', '00000000-0000-0000-0000-000000000000') ORDER BY created_at, id LIMIT 50; + QUERY PLAN +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Limit (cost=4022.99..4023.11 rows=50 width=150) (actual time=10.619..10.625 rows=50.00 loops=1) + Buffers: shared hit=2264 + InitPlan 1 + -> Seq Scan on chats (cost=0.00..4.58 rows=1 width=16) (actual time=0.007..0.019 rows=1.00 loops=1) + Filter: (title = 'big'::text) + Rows Removed by Filter: 205 + Buffers: shared hit=2 + -> Sort (cost=4018.41..4018.89 rows=193 width=150) (actual time=10.616..10.619 rows=50.00 loops=1) + Sort Key: messages.created_at, messages.id + Sort Method: top-N heapsort Memory: 36kB + Buffers: shared hit=2264 + -> Seq Scan on messages (cost=0.00..4012.00 rows=193 width=150) (actual time=1.644..8.293 rows=39841.00 loops=1) + Filter: ((chat_id = (InitPlan 1).col1) AND (ROW(created_at, id) > ROW('2026-01-15 00:00:00+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))) + Rows Removed by Filter: 60159 + Buffers: shared hit=2264 + Planning: + Buffers: shared hit=20 + Planning Time: 0.264 ms + Execution Time: 10.659 ms +(19 rows) + +Time: 11.410 ms +research=# EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM messages WHERE chat_id = (SELECT id FROM chats WHERE title = 'big') AND (created_at, id) > ('2026-01-15 00:00+00', '00000000-0000-0000-0000-000000000000') ORDER BY created_at, id LIMIT 50; + QUERY PLAN +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Limit (cost=4022.99..4023.11 rows=50 width=150) (actual time=10.266..10.274 rows=50.00 loops=1) + Buffers: shared hit=2264 + InitPlan 1 + -> Seq Scan on chats (cost=0.00..4.58 rows=1 width=16) (actual time=0.006..0.016 rows=1.00 loops=1) + Filter: (title = 'big'::text) + Rows Removed by Filter: 205 + Buffers: shared hit=2 + -> Sort (cost=4018.41..4018.89 rows=193 width=150) (actual time=10.265..10.269 rows=50.00 loops=1) + Sort Key: messages.created_at, messages.id + Sort Method: top-N heapsort Memory: 36kB + Buffers: shared hit=2264 + -> Seq Scan on messages (cost=0.00..4012.00 rows=193 width=150) (actual time=1.597..8.150 rows=39841.00 loops=1) + Filter: ((chat_id = (InitPlan 1).col1) AND (ROW(created_at, id) > ROW('2026-01-15 00:00:00+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))) + Rows Removed by Filter: 60159 + Buffers: shared hit=2264 + Planning Time: 0.082 ms + Execution Time: 10.300 ms +(17 rows) + +Time: 11.458 ms +research=# EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM messages WHERE chat_id = (SELECT id FROM chats WHERE title = 'small-1') AND (created_at, id) > ('2026-01-01 00:30+00', '00000000-0000-0000-0000-000000000000') ORDER BY created_at, id LIMIT 50; + QUERY PLAN +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Limit (cost=4031.69..4031.81 rows=50 width=150) (actual time=6.337..6.344 rows=46.00 loops=1) + Buffers: shared hit=2264 + InitPlan 1 + -> Seq Scan on chats (cost=0.00..4.58 rows=1 width=16) (actual time=0.007..0.019 rows=1.00 loops=1) + Filter: (title = 'small-1'::text) + Rows Removed by Filter: 205 + Buffers: shared hit=2 + -> Sort (cost=4027.11..4028.25 rows=455 width=150) (actual time=6.335..6.339 rows=46.00 loops=1) + Sort Key: messages.created_at, messages.id + Sort Method: quicksort Memory: 35kB + Buffers: shared hit=2264 + -> Seq Scan on messages (cost=0.00..4012.00 rows=455 width=150) (actual time=4.345..6.255 rows=46.00 loops=1) + Filter: ((chat_id = (InitPlan 1).col1) AND (ROW(created_at, id) > ROW('2026-01-01 00:30:00+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))) + Rows Removed by Filter: 99954 + Buffers: shared hit=2264 + Planning Time: 0.105 ms + Execution Time: 6.366 ms +(17 rows) + +Time: 6.763 ms +research=# EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM messages WHERE chat_id = (SELECT id FROM chats WHERE title = 'small-1') AND (created_at, id) > ('2026-01-01 00:30+00', '00000000-0000-0000-0000-000000000000') ORDER BY created_at, id LIMIT 50; + QUERY PLAN +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Limit (cost=4031.69..4031.81 rows=50 width=150) (actual time=6.219..6.227 rows=46.00 loops=1) + Buffers: shared hit=2264 + InitPlan 1 + -> Seq Scan on chats (cost=0.00..4.58 rows=1 width=16) (actual time=0.009..0.021 rows=1.00 loops=1) + Filter: (title = 'small-1'::text) + Rows Removed by Filter: 205 + Buffers: shared hit=2 + -> Sort (cost=4027.11..4028.25 rows=455 width=150) (actual time=6.217..6.221 rows=46.00 loops=1) + Sort Key: messages.created_at, messages.id + Sort Method: quicksort Memory: 35kB + Buffers: shared hit=2264 + -> Seq Scan on messages (cost=0.00..4012.00 rows=455 width=150) (actual time=4.517..6.205 rows=46.00 loops=1) + Filter: ((chat_id = (InitPlan 1).col1) AND (ROW(created_at, id) > ROW('2026-01-01 00:30:00+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))) + Rows Removed by Filter: 99954 + Buffers: shared hit=2264 + Planning Time: 0.110 ms + Execution Time: 6.248 ms +(17 rows) + +Time: 6.727 ms diff --git a/evidence/track2-insert-cost.txt b/evidence/track2-insert-cost.txt new file mode 100644 index 0000000..175c596 --- /dev/null +++ b/evidence/track2-insert-cost.txt @@ -0,0 +1,168 @@ +> docker compose exec postgres psql -U messenger -d postgres -c "CREATE DATABASE research;" +CREATE DATABASE +> docker compose run --rm --no-deps --entrypoint sh migrate -c 'URL=$(sed s,/messenger?,/research?, /run/secrets/database_url); goose -dir /migrations postgres $URL up' +Container mtusi2026lab2-migrate-run-37c6222942bf Creating +Container mtusi2026lab2-migrate-run-37c6222942bf Created +2026/10/07 19:30:06 OK 00001_init.sql (10.96ms) +2026/10/07 19:30:06 goose: successfully migrated database to version: 1 +> docker compose exec postgres psql -U messenger -d research +psql (18.6 (Debian 18.6-1.pgdg13+2)) +Type "help" for help. + +research=# INSERT INTO users (id, username, display_name, password_hash) VALUES (uuidv7(), 'research_user', 'Research User', 'x'); +INSERT 0 1 +research=# INSERT INTO chats (id, title) SELECT uuidv7(), 'big'; +INSERT 0 1 +research=# INSERT INTO chats (id, title) SELECT uuidv7(), 'mid-' || g FROM generate_series(1, 5) g; +INSERT 0 5 +research=# INSERT INTO chats (id, title) SELECT uuidv7(), 'small-' || g FROM generate_series(1, 200) g; +INSERT 0 200 +research=# CREATE TABLE messages_seed AS +SELECT uuidv7() AS id, + c.id AS chat_id, + (SELECT id FROM users WHERE username = 'research_user') AS sender_id, + 'message ' || n || ' ' || repeat(md5(random()::text), 1 + (n % 4)) AS text, + timestamptz '2026-01-01 00:00:00+00' + n * interval '1 minute' AS created_at +FROM chats c +CROSS JOIN LATERAL generate_series(1, CASE split_part(c.title, '-', 1) + WHEN 'big' THEN 60000 WHEN 'mid' THEN 5000 ELSE 75 END) AS n; +SELECT 100000 + +A. Only PK +research=# SELECT 'VARIANT A' AS variant; +DROP INDEX IF EXISTS idx_messages_chat_history; +DROP INDEX IF EXISTS idx_messages_chat_id; + +TRUNCATE messages; +INSERT INTO messages SELECT id, chat_id, sender_id, text, created_at FROM messages_seed; +TRUNCATE messages; +INSERT INTO messages SELECT id, chat_id, sender_id, text, created_at FROM messages_seed; +TRUNCATE messages; +INSERT INTO messages SELECT id, chat_id, sender_id, text, created_at FROM messages_seed; + +ANALYZE messages; +SELECT pg_size_pretty(pg_relation_size('messages')) AS table, indexrelid::regclass AS idx, pg_size_pretty(pg_relation_size(indexrelid)) AS size FROM pg_index WHERE indrelid = 'messages'::regclass; + variant +----------- + VARIANT A +(1 row) + +Time: 0.345 ms +DROP INDEX +Time: 5.736 ms +NOTICE: index "idx_messages_chat_id" does not exist, skipping +DROP INDEX +Time: 0.153 ms +TRUNCATE TABLE +Time: 3.614 ms +INSERT 0 100000 +Time: 989.859 ms +TRUNCATE TABLE +Time: 6.348 ms +INSERT 0 100000 +Time: 976.844 ms +TRUNCATE TABLE +Time: 6.268 ms +INSERT 0 100000 +Time: 977.896 ms +ANALYZE +Time: 103.989 ms + table | idx | size +-------+---------------+--------- + 18 MB | messages_pkey | 3104 kB +(1 row) + +Time: 0.765 ms + +B. (chat_id, created_at, id) +research=# SELECT 'VARIANT B' AS variant; +DROP INDEX IF EXISTS idx_messages_chat_id; +CREATE INDEX idx_messages_chat_history ON messages (chat_id, created_at, id); + +TRUNCATE messages; +INSERT INTO messages SELECT id, chat_id, sender_id, text, created_at FROM messages_seed; +TRUNCATE messages; +INSERT INTO messages SELECT id, chat_id, sender_id, text, created_at FROM messages_seed; +TRUNCATE messages; +INSERT INTO messages SELECT id, chat_id, sender_id, text, created_at FROM messages_seed; + +ANALYZE messages; +SELECT pg_size_pretty(pg_relation_size('messages')) AS table, indexrelid::regclass AS idx, pg_size_pretty(pg_relation_size(indexrelid)) AS size FROM pg_index WHERE indrelid = 'messages'::regclass; + variant +----------- + VARIANT B +(1 row) + +Time: 0.411 ms +NOTICE: index "idx_messages_chat_id" does not exist, skipping +DROP INDEX +Time: 0.144 ms +CREATE INDEX +Time: 165.646 ms +TRUNCATE TABLE +Time: 9.681 ms +INSERT 0 100000 +Time: 1655.348 ms (00:01.655) +TRUNCATE TABLE +Time: 6.620 ms +INSERT 0 100000 +Time: 1640.366 ms (00:01.640) +TRUNCATE TABLE +Time: 7.094 ms +INSERT 0 100000 +Time: 1156.808 ms (00:01.157) +ANALYZE +Time: 92.376 ms + table | idx | size +-------+---------------------------+--------- + 18 MB | messages_pkey | 3104 kB + 18 MB | idx_messages_chat_history | 5776 kB +(2 rows) + +Time: 0.560 ms + +C. (chat_id, id) +research=# SELECT 'VARIANT C' AS variant; +DROP INDEX IF EXISTS idx_messages_chat_history; +CREATE INDEX idx_messages_chat_id ON messages (chat_id, id); + +TRUNCATE messages; +INSERT INTO messages SELECT id, chat_id, sender_id, text, created_at FROM messages_seed; +TRUNCATE messages; +INSERT INTO messages SELECT id, chat_id, sender_id, text, created_at FROM messages_seed; +TRUNCATE messages; +INSERT INTO messages SELECT id, chat_id, sender_id, text, created_at FROM messages_seed; + +ANALYZE messages; +SELECT pg_size_pretty(pg_relation_size('messages')) AS table, indexrelid::regclass AS idx, pg_size_pretty(pg_relation_size(indexrelid)) AS size FROM pg_index WHERE indrelid = 'messages'::regclass; + variant +----------- + VARIANT C +(1 row) + +Time: 0.362 ms +DROP INDEX +Time: 4.459 ms +CREATE INDEX +Time: 61.503 ms +TRUNCATE TABLE +Time: 6.046 ms +INSERT 0 100000 +Time: 1222.658 ms (00:01.223) +TRUNCATE TABLE +Time: 6.482 ms +INSERT 0 100000 +Time: 1181.280 ms (00:01.181) +TRUNCATE TABLE +Time: 6.286 ms +INSERT 0 100000 +Time: 1185.066 ms (00:01.185) +ANALYZE +Time: 98.084 ms + table | idx | size +-------+----------------------+--------- + 18 MB | messages_pkey | 3104 kB + 18 MB | idx_messages_chat_id | 4880 kB +(2 rows) + +Time: 0.490 ms