ЛР 2 — БПИ2303 — Морару — на 5 (трек 2)

TypeScript/Node API на PostgreSQL с сессиями в Valkey (AOF), миграции,
compose-стенд, restart hook, тесты уровня 4 и эксперимент трека 2
(планы запроса на 240k сообщений, стоимость записи, backup/restore).
This commit is contained in:
celis
2026-10-29 20:58:56 +03:00
parent 65003b6119
commit cac37bddec
29 changed files with 2229 additions and 18 deletions
+8
View File
@@ -0,0 +1,8 @@
# 2026-10-29T17:42:34Z commit 65003b6+wip
python3 scripts/check_contract.py
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
skip 001_init
.gitignore:5:secrets/ secrets/database_url
.gitignore:2:.env .env
.gitignore:15:backups/ backups/
.gitignore:33:dist/ dist/
+7
View File
@@ -0,0 +1,7 @@
PASS test_registration_race - 20 parallel registrations of one username -> 1x201, 19x409, 1 row
PASS test_chat_rollback - chat with unknown member -> 400, no chat/member rows (transaction rolled back)
PASS test_pagination_and_injection - 57 messages over limit=7 pages: exact order, no dupes; foreign/garbage cursor -> 400; SQL text stored verbatim
PASS test_wrong_password_and_logout - wrong password / unknown user -> identical 401 Basic; logout -> 204, then 401 twice
PASS test_expiry - SESSION_TTL_SECONDS=2: 200 right after login, 401 after 3 s on both API processes
PASS test_valkey_outage - valkey stopped -> ready/me 503 unavailable in 0.02s, anonymous 401, registration 201; recovered with same token
PASS test_postgres_outage - postgres stopped -> ready/me 503 unavailable in 2.01s, anonymous 401, registration 503; recovered with same token
+17
View File
@@ -0,0 +1,17 @@
{
"lab": 2,
"workload": "closed-loop POST messages; no automatic retries",
"requested_duration_seconds": 15.0,
"elapsed_seconds": 15.15,
"concurrency": 32,
"successful_requests": 7553,
"failed_requests": 0,
"error_ratio": 0.0,
"successful_rps": 498.545,
"successful_latency_ms": {
"p50": 54.672,
"p95": 123.874,
"p99": 213.309
},
"note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead."
}
+14
View File
@@ -0,0 +1,14 @@
# before
api 2026-10-29T17:36:26.445238878Z
postgres 2026-10-29T17:37:54.996532602Z
valkey 2026-10-29T17:37:48.230161722Z
python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://127.0.0.1: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.
# after (all three StartedAt changed, volumes kept)
api 2026-10-29T17:46:36.369988683Z
postgres 2026-10-29T17:46:31.719791235Z
valkey 2026-10-29T17:46:31.735641919Z
DRIVER VOLUME NAME
local messenger-lab2_pgdata
local messenger-lab2_valkeydata
+1
View File
@@ -0,0 +1 @@
max API connections observed during 32-client load: 8 (DB_POOL_MAX=8)
+3
View File
@@ -0,0 +1,3 @@
# 2026-10-29T17:46:26Z
python3 tests/smoke.py --base-url "http://127.0.0.1:8080"
PASS: lab2 public API scenario; infrastructure and grading evidence remain separate.
@@ -0,0 +1,20 @@
Limit (cost=12317.65..12323.60 rows=51 width=269) (actual time=18.151..22.092 rows=51 loops=1)
Buffers: shared hit=9184
-> Gather Merge (cost=12317.65..15225.19 rows=24920 width=269) (actual time=18.150..22.087 rows=51 loops=1)
Workers Planned: 2
Workers Launched: 2
Buffers: shared hit=9184
-> Sort (cost=11317.63..11348.78 rows=12460 width=269) (actual time=15.433..15.437 rows=39 loops=3)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 57kB
Buffers: shared hit=9184
Worker 0: Sort Method: top-N heapsort Memory: 64kB
Worker 1: Sort Method: top-N heapsort Memory: 64kB
-> Parallel Seq Scan on messages (cost=0.00..10901.94 rows=12460 width=269) (actual time=0.013..13.836 rows=10000 loops=3)
Filter: ((chat_id = '43815b30-3214-43ab-907b-8302428adf62'::uuid) AND (ROW(created_at, id) > ROW('2026-09-14 15:04:07.432+00'::timestamp with time zone, 'b4df760e-4412-48fc-90cc-814d6b6c9617'::uuid)))
Rows Removed by Filter: 69906
Buffers: shared hit=9154
Planning:
Buffers: shared hit=121
Planning Time: 1.035 ms
Execution Time: 22.224 ms
@@ -0,0 +1,25 @@
Limit (cost=11649.09..11655.04 rows=51 width=269) (actual time=31.363..35.221 rows=51 loops=1)
Buffers: shared hit=9056
-> Gather Merge (cost=11649.09..14602.13 rows=25310 width=269) (actual time=31.361..35.215 rows=51 loops=1)
Workers Planned: 2
Workers Launched: 2
Buffers: shared hit=9056
-> Sort (cost=10649.07..10680.71 rows=12655 width=269) (actual time=26.965..26.970 rows=42 loops=3)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 57kB
Buffers: shared hit=9056
Worker 0: Sort Method: top-N heapsort Memory: 57kB
Worker 1: Sort Method: top-N heapsort Memory: 57kB
-> Parallel Bitmap Heap Scan on messages (cost=783.77..10226.87 rows=12655 width=269) (actual time=2.405..24.972 rows=10000 loops=3)
Recheck Cond: (chat_id = '43815b30-3214-43ab-907b-8302428adf62'::uuid)
Filter: (ROW(created_at, id) > ROW('2026-09-14 15:04:07.432+00'::timestamp with time zone, 'b4df760e-4412-48fc-90cc-814d6b6c9617'::uuid))
Rows Removed by Filter: 10000
Heap Blocks: exact=3179
Buffers: shared hit=9026
-> Bitmap Index Scan on exp_idx (cost=0.00..776.18 rows=60768 width=0) (actual time=3.679..3.680 rows=60000 loops=1)
Index Cond: (chat_id = '43815b30-3214-43ab-907b-8302428adf62'::uuid)
Buffers: shared hit=55
Planning:
Buffers: shared hit=134
Planning Time: 0.990 ms
Execution Time: 35.634 ms
@@ -0,0 +1,16 @@
Limit (cost=1.71..69.21 rows=51 width=268) (actual time=0.158..0.183 rows=51 loops=1)
Buffers: shared hit=21
-> Incremental Sort (cost=1.71..40391.63 rows=30516 width=268) (actual time=0.145..0.168 rows=51 loops=1)
Sort Key: created_at, id
Presorted Key: created_at
Full-sort Groups: 2 Sort Method: quicksort Average Memory: 41kB Peak Memory: 41kB
Buffers: shared hit=21
-> Index Scan using exp_idx on messages (cost=0.42..39018.41 rows=30516 width=268) (actual time=0.039..0.070 rows=52 loops=1)
Index Cond: ((chat_id = '43815b30-3214-43ab-907b-8302428adf62'::uuid) AND (created_at >= '2026-09-14 15:04:07.432+00'::timestamp with time zone))
Filter: (ROW(created_at, id) > ROW('2026-09-14 15:04:07.432+00'::timestamp with time zone, 'b4df760e-4412-48fc-90cc-814d6b6c9617'::uuid))
Rows Removed by Filter: 1
Buffers: shared hit=12
Planning:
Buffers: shared hit=150
Planning Time: 1.476 ms
Execution Time: 0.278 ms
@@ -0,0 +1,9 @@
Limit (cost=0.42..64.91 rows=51 width=267) (actual time=0.032..0.059 rows=51 loops=1)
Buffers: shared hit=11
-> Index Scan using messages_chat_created_id_idx on messages (cost=0.42..38137.55 rows=30161 width=267) (actual time=0.030..0.054 rows=51 loops=1)
Index Cond: ((chat_id = '43815b30-3214-43ab-907b-8302428adf62'::uuid) AND (ROW(created_at, id) > ROW('2026-09-14 15:04:07.432+00'::timestamp with time zone, 'b4df760e-4412-48fc-90cc-814d6b6c9617'::uuid)))
Buffers: shared hit=11
Planning:
Buffers: shared hit=144
Planning Time: 1.388 ms
Execution Time: 0.107 ms
+495
View File
@@ -0,0 +1,495 @@
{
"env": {
"postgres": "17.6",
"shared_buffers": "128MB",
"synchronous_commit": "on",
"docker": "OrbStack, 11 CPU, 9434726400 B RAM"
},
"dataset": {
"messages_total": 239717,
"track2_chats": 300,
"chat_size_max": 60000,
"chat_size_p50": 146,
"chat_size_min": 64,
"top10_share": 0.618,
"messages_table_bytes": 73785344,
"pk_index_bytes": 9633792
},
"chats": {
"large": {
"size": 60000,
"offset": 30000
},
"medium": {
"size": 1013,
"offset": 506
},
"small": {
"size": 64,
"offset": 32
}
},
"variants": {
"V0 PK only": {
"index_build_s": 0.0,
"index_bytes": 0,
"queries": {
"first page (API) / large": {
"median_ms": 25.559,
"min_ms": 22.098,
"buffers": 9030,
"rows_touched": 239718,
"plan": "Limit > Gather Merge > Sort > Seq Scan"
},
"first page (API) / medium": {
"median_ms": 25.16,
"min_ms": 21.963,
"buffers": 9030,
"rows_touched": 239718,
"plan": "Limit > Gather Merge > Sort > Seq Scan"
},
"first page (API) / small": {
"median_ms": 29.764,
"min_ms": 23.438,
"buffers": 9030,
"rows_touched": 239715,
"plan": "Limit > Gather Merge > Sort > Seq Scan"
},
"middle page keyset (API) / large": {
"median_ms": 25.172,
"min_ms": 23.475,
"buffers": 9030,
"rows_touched": 239718,
"plan": "Limit > Gather Merge > Sort > Seq Scan"
},
"middle page keyset (API) / medium": {
"median_ms": 23.408,
"min_ms": 19.072,
"buffers": 9030,
"rows_touched": 239718,
"plan": "Limit > Gather Merge > Sort > Seq Scan"
},
"middle page keyset (API) / small": {
"median_ms": 21.248,
"min_ms": 18.796,
"buffers": 9030,
"rows_touched": 239715,
"plan": "Limit > Gather Merge > Sort > Seq Scan"
},
"middle page keyset + created_at >= / large": {
"median_ms": 28.129,
"min_ms": 26.33,
"buffers": 9030,
"rows_touched": 239718,
"plan": "Limit > Gather Merge > Sort > Seq Scan"
},
"middle page keyset + created_at >= / medium": {
"median_ms": 26.974,
"min_ms": 23.012,
"buffers": 9030,
"rows_touched": 239718,
"plan": "Limit > Gather Merge > Sort > Seq Scan"
},
"middle page keyset + created_at >= / small": {
"median_ms": 24.638,
"min_ms": 23.061,
"buffers": 9030,
"rows_touched": 239715,
"plan": "Limit > Gather Merge > Sort > Seq Scan"
},
"middle page OFFSET / large": {
"median_ms": 81.353,
"min_ms": 64.787,
"buffers": 9088,
"rows_touched": 239718,
"plan": "Limit > Gather Merge > Sort > Seq Scan"
},
"middle page OFFSET / medium": {
"median_ms": 25.937,
"min_ms": 25.476,
"buffers": 9088,
"rows_touched": 239718,
"plan": "Limit > Gather Merge > Sort > Seq Scan"
},
"middle page OFFSET / small": {
"median_ms": 32.843,
"min_ms": 25.142,
"buffers": 9088,
"rows_touched": 239715,
"plan": "Limit > Gather Merge > Sort > Seq Scan"
}
},
"write": {
"bulk_50k_ms_median": 2158.3,
"bulk_50k_ms": [
1779.713,
2495.609,
2158.347
],
"pgbench_tps_median": 2237.1,
"pgbench_tps": [
717.137925,
2237.08345,
2524.645924
],
"pgbench_latency_ms_median": 3.576
}
},
"V1 (chat_id)": {
"index_build_s": 0.299,
"index_bytes": 1744896,
"queries": {
"first page (API) / large": {
"median_ms": 35.605,
"min_ms": 31.245,
"buffers": 9055,
"rows_touched": 60000,
"plan": "Limit > Gather Merge > Sort > Bitmap Heap Scan > Bitmap Index Scan exp_idx"
},
"first page (API) / medium": {
"median_ms": 4.349,
"min_ms": 4.113,
"buffers": 965,
"rows_touched": 1013,
"plan": "Limit > Sort > Bitmap Heap Scan > Bitmap Index Scan exp_idx"
},
"first page (API) / small": {
"median_ms": 0.547,
"min_ms": 0.416,
"buffers": 72,
"rows_touched": 64,
"plan": "Limit > Sort > Bitmap Heap Scan > Bitmap Index Scan exp_idx"
},
"middle page keyset (API) / large": {
"median_ms": 24.591,
"min_ms": 22.198,
"buffers": 9055,
"rows_touched": 60000,
"plan": "Limit > Gather Merge > Sort > Bitmap Heap Scan > Bitmap Index Scan exp_idx"
},
"middle page keyset (API) / medium": {
"median_ms": 3.597,
"min_ms": 2.878,
"buffers": 965,
"rows_touched": 1013,
"plan": "Limit > Sort > Bitmap Heap Scan > Bitmap Index Scan exp_idx"
},
"middle page keyset (API) / small": {
"median_ms": 0.638,
"min_ms": 0.396,
"buffers": 72,
"rows_touched": 64,
"plan": "Limit > Sort > Bitmap Heap Scan > Bitmap Index Scan exp_idx"
},
"middle page keyset + created_at >= / large": {
"median_ms": 35.804,
"min_ms": 32.3,
"buffers": 9031,
"rows_touched": 60000,
"plan": "Limit > Sort > Bitmap Heap Scan > Bitmap Index Scan exp_idx"
},
"middle page keyset + created_at >= / medium": {
"median_ms": 3.128,
"min_ms": 2.654,
"buffers": 965,
"rows_touched": 1013,
"plan": "Limit > Sort > Bitmap Heap Scan > Bitmap Index Scan exp_idx"
},
"middle page keyset + created_at >= / small": {
"median_ms": 0.437,
"min_ms": 0.35,
"buffers": 72,
"rows_touched": 64,
"plan": "Limit > Sort > Bitmap Heap Scan > Bitmap Index Scan exp_idx"
},
"middle page OFFSET / large": {
"median_ms": 42.147,
"min_ms": 36.78,
"buffers": 9055,
"rows_touched": 60000,
"plan": "Limit > Gather Merge > Sort > Bitmap Heap Scan > Bitmap Index Scan exp_idx"
},
"middle page OFFSET / medium": {
"median_ms": 3.011,
"min_ms": 2.724,
"buffers": 965,
"rows_touched": 1013,
"plan": "Limit > Sort > Bitmap Heap Scan > Bitmap Index Scan exp_idx"
},
"middle page OFFSET / small": {
"median_ms": 0.399,
"min_ms": 0.367,
"buffers": 72,
"rows_touched": 64,
"plan": "Limit > Sort > Bitmap Heap Scan > Bitmap Index Scan exp_idx"
}
},
"write": {
"bulk_50k_ms_median": 1173.6,
"bulk_50k_ms": [
1173.602,
1115.078,
1515.873
],
"pgbench_tps_median": 1820.2,
"pgbench_tps": [
1820.161379,
2070.254695,
915.294613
],
"pgbench_latency_ms_median": 4.395
}
},
"V2 (chat_id, created_at)": {
"index_build_s": 0.345,
"index_bytes": 9756672,
"queries": {
"first page (API) / large": {
"median_ms": 0.143,
"min_ms": 0.134,
"buffers": 25,
"rows_touched": 52,
"plan": "Limit > Incremental Sort > Index Scan exp_idx"
},
"first page (API) / medium": {
"median_ms": 0.344,
"min_ms": 0.299,
"buffers": 63,
"rows_touched": 52,
"plan": "Limit > Incremental Sort > Index Scan exp_idx"
},
"first page (API) / small": {
"median_ms": 0.386,
"min_ms": 0.342,
"buffers": 63,
"rows_touched": 52,
"plan": "Limit > Incremental Sort > Index Scan exp_idx"
},
"middle page keyset (API) / large": {
"median_ms": 0.193,
"min_ms": 0.151,
"buffers": 21,
"rows_touched": 53,
"plan": "Limit > Incremental Sort > Index Scan exp_idx"
},
"middle page keyset (API) / medium": {
"median_ms": 0.604,
"min_ms": 0.307,
"buffers": 66,
"rows_touched": 53,
"plan": "Limit > Incremental Sort > Index Scan exp_idx"
},
"middle page keyset (API) / small": {
"median_ms": 0.37,
"min_ms": 0.279,
"buffers": 44,
"rows_touched": 32,
"plan": "Limit > Incremental Sort > Index Scan exp_idx"
},
"middle page keyset + created_at >= / large": {
"median_ms": 0.345,
"min_ms": 0.203,
"buffers": 21,
"rows_touched": 53,
"plan": "Limit > Incremental Sort > Index Scan exp_idx"
},
"middle page keyset + created_at >= / medium": {
"median_ms": 0.488,
"min_ms": 0.365,
"buffers": 66,
"rows_touched": 53,
"plan": "Limit > Incremental Sort > Index Scan exp_idx"
},
"middle page keyset + created_at >= / small": {
"median_ms": 0.425,
"min_ms": 0.389,
"buffers": 41,
"rows_touched": 32,
"plan": "Limit > Sort > Bitmap Heap Scan > Bitmap Index Scan exp_idx"
},
"middle page OFFSET / large": {
"median_ms": 80.175,
"min_ms": 70.332,
"buffers": 9088,
"rows_touched": 239718,
"plan": "Limit > Gather Merge > Sort > Seq Scan"
},
"middle page OFFSET / medium": {
"median_ms": 2.533,
"min_ms": 2.31,
"buffers": 554,
"rows_touched": 558,
"plan": "Limit > Incremental Sort > Index Scan exp_idx"
},
"middle page OFFSET / small": {
"median_ms": 0.738,
"min_ms": 0.45,
"buffers": 75,
"rows_touched": 64,
"plan": "Limit > Incremental Sort > Index Scan exp_idx"
}
},
"write": {
"bulk_50k_ms_median": 1747.7,
"bulk_50k_ms": [
2169.21,
1747.667,
1210.19
],
"pgbench_tps_median": 1337.9,
"pgbench_tps": [
1337.947649,
1265.008351,
1626.184355
],
"pgbench_latency_ms_median": 5.979
}
},
"V3 (chat_id, created_at, id)": {
"index_build_s": 0.485,
"index_bytes": 14139392,
"queries": {
"first page (API) / large": {
"median_ms": 0.223,
"min_ms": 0.129,
"buffers": 15,
"rows_touched": 51,
"plan": "Limit > Index Scan messages_chat_created_id_idx"
},
"first page (API) / medium": {
"median_ms": 0.422,
"min_ms": 0.242,
"buffers": 53,
"rows_touched": 51,
"plan": "Limit > Index Scan messages_chat_created_id_idx"
},
"first page (API) / small": {
"median_ms": 0.541,
"min_ms": 0.322,
"buffers": 53,
"rows_touched": 51,
"plan": "Limit > Index Scan messages_chat_created_id_idx"
},
"middle page keyset (API) / large": {
"median_ms": 0.262,
"min_ms": 0.115,
"buffers": 11,
"rows_touched": 51,
"plan": "Limit > Index Scan messages_chat_created_id_idx"
},
"middle page keyset (API) / medium": {
"median_ms": 0.454,
"min_ms": 0.433,
"buffers": 55,
"rows_touched": 51,
"plan": "Limit > Index Scan messages_chat_created_id_idx"
},
"middle page keyset (API) / small": {
"median_ms": 0.491,
"min_ms": 0.455,
"buffers": 35,
"rows_touched": 31,
"plan": "Limit > Index Scan messages_chat_created_id_idx"
},
"middle page keyset + created_at >= / large": {
"median_ms": 0.116,
"min_ms": 0.097,
"buffers": 11,
"rows_touched": 51,
"plan": "Limit > Index Scan messages_chat_created_id_idx"
},
"middle page keyset + created_at >= / medium": {
"median_ms": 0.233,
"min_ms": 0.194,
"buffers": 55,
"rows_touched": 51,
"plan": "Limit > Index Scan messages_chat_created_id_idx"
},
"middle page keyset + created_at >= / small": {
"median_ms": 0.35,
"min_ms": 0.237,
"buffers": 41,
"rows_touched": 31,
"plan": "Limit > Sort > Bitmap Heap Scan > Bitmap Index Scan messages_chat_created_id_idx"
},
"middle page OFFSET / large": {
"median_ms": 15.321,
"min_ms": 13.814,
"buffers": 6145,
"rows_touched": 30051,
"plan": "Limit > Index Scan messages_chat_created_id_idx"
},
"middle page OFFSET / medium": {
"median_ms": 1.883,
"min_ms": 1.576,
"buffers": 545,
"rows_touched": 557,
"plan": "Limit > Index Scan messages_chat_created_id_idx"
},
"middle page OFFSET / small": {
"median_ms": 0.377,
"min_ms": 0.248,
"buffers": 67,
"rows_touched": 64,
"plan": "Limit > Index Scan messages_chat_created_id_idx"
}
},
"write": {
"bulk_50k_ms_median": 1061.7,
"bulk_50k_ms": [
899.73,
1367.955,
1061.711
],
"pgbench_tps_median": 1659.2,
"pgbench_tps": [
1851.131479,
981.989419,
1659.177954
],
"pgbench_latency_ms_median": 4.822
}
}
},
"backup_restore": {
"dump_bytes": 16135498,
"dump_s": 3.37,
"restore_s": {
"jobs_1": 3.76,
"jobs_4": 4.01
},
"messages_before_dump": 239717,
"during_dump_committed_in_source": 13,
"during_dump_in_restore": 1,
"fingerprint_equal": true,
"api_page_equal": true,
"fingerprint": [
[
"users",
"1019",
"42d7e548b2d4ee91736961faca386d0a"
],
[
"chats",
"306",
"d2541ecae4fa59a1f1192498d0f5b4e0"
],
[
"chat_members",
"3295",
"4f1cf6a7c65e7a56c072c20877d7d28f"
],
[
"messages",
"239717",
"1c0251c775eecc3ffc43c28b81084eea"
],
[
"schema_migrations",
"1",
"550e0a1f7ef8568bdbe21b29ffb5399b"
]
],
"restored_plan_middle_page_large": "Limit > Index Scan messages_chat_created_id_idx"
}
}
+53
View File
@@ -0,0 +1,53 @@
# Track 2 results
Generated by experiments/track2/run.py from results.json. Environment: OrbStack, 11 CPU, 9434726400 B RAM; PostgreSQL 17.6, shared_buffers 128MB, synchronous_commit on.
Dataset: 239717 messages, 300 synthetic chats, sizes max 60000 / median 146 / min 64, top-10 chats hold 0.618 of track2 messages, table 73785344 B.
## Read: median ms of 5 EXPLAIN ANALYZE runs / shared buffers / rows read by scans
| query / chat | V0 PK only | V1 (chat_id) | V2 (chat_id, created_at) | V3 (chat_id, created_at, id) |
|---|---|---|---|---|
| first page (API) / large | 25.56 / 9030 / 239718 | 35.60 / 9055 / 60000 | 0.14 / 25 / 52 | 0.22 / 15 / 51 |
| first page (API) / medium | 25.16 / 9030 / 239718 | 4.35 / 965 / 1013 | 0.34 / 63 / 52 | 0.42 / 53 / 51 |
| first page (API) / small | 29.76 / 9030 / 239715 | 0.55 / 72 / 64 | 0.39 / 63 / 52 | 0.54 / 53 / 51 |
| middle page keyset (API) / large | 25.17 / 9030 / 239718 | 24.59 / 9055 / 60000 | 0.19 / 21 / 53 | 0.26 / 11 / 51 |
| middle page keyset (API) / medium | 23.41 / 9030 / 239718 | 3.60 / 965 / 1013 | 0.60 / 66 / 53 | 0.45 / 55 / 51 |
| middle page keyset (API) / small | 21.25 / 9030 / 239715 | 0.64 / 72 / 64 | 0.37 / 44 / 32 | 0.49 / 35 / 31 |
| middle page keyset + created_at >= / large | 28.13 / 9030 / 239718 | 35.80 / 9031 / 60000 | 0.34 / 21 / 53 | 0.12 / 11 / 51 |
| middle page keyset + created_at >= / medium | 26.97 / 9030 / 239718 | 3.13 / 965 / 1013 | 0.49 / 66 / 53 | 0.23 / 55 / 51 |
| middle page keyset + created_at >= / small | 24.64 / 9030 / 239715 | 0.44 / 72 / 64 | 0.42 / 41 / 32 | 0.35 / 41 / 31 |
| middle page OFFSET / large | 81.35 / 9088 / 239718 | 42.15 / 9055 / 60000 | 80.17 / 9088 / 239718 | 15.32 / 6145 / 30051 |
| middle page OFFSET / medium | 25.94 / 9088 / 239718 | 3.01 / 965 / 1013 | 2.53 / 554 / 558 | 1.88 / 545 / 557 |
| middle page OFFSET / small | 32.84 / 9088 / 239715 | 0.40 / 72 / 64 | 0.74 / 75 / 64 | 0.38 / 67 / 64 |
Plans of the API query (middle page, large chat):
- V0 PK only: `Limit > Gather Merge > Sort > Seq Scan`
- V1 (chat_id): `Limit > Gather Merge > Sort > Bitmap Heap Scan > Bitmap Index Scan exp_idx`
- V2 (chat_id, created_at): `Limit > Incremental Sort > Index Scan exp_idx`
- V3 (chat_id, created_at, id): `Limit > Index Scan messages_chat_created_id_idx`
## Write cost
| variant | index size, MiB | build, s | bulk 50k INSERT, ms (median of 3) | pgbench 1-row INSERT, TPS (median of 3x10 s, 8 clients) | avg latency, ms |
|---|---|---|---|---|---|
| V0 PK only | 0.0 | 0.00 | 2158 | 2237 | 3.576 |
| V1 (chat_id) | 1.7 | 0.30 | 1174 | 1820 | 4.395 |
| V2 (chat_id, created_at) | 9.3 | 0.34 | 1748 | 1338 | 5.979 |
| V3 (chat_id, created_at, id) | 13.5 | 0.48 | 1062 | 1659 | 4.822 |
## Backup -> restore into new storage -> reconcile
- pg_dump -Fc: 3.37 s, 16135498 B; pg_restore into a new container + new volume: {'jobs_1': 3.76, 'jobs_4': 4.01}
- fingerprints (count + md5 of every table, 20 sampled messages) equal: True; API page of the large chat equal: True
- writes committed during pg_dump: 13 in source, 1 in restore
- restored plan of the API query: `Limit > Index Scan messages_chat_created_id_idx`
| table | rows | md5 |
|---|---|---|
| users | 1019 | 42d7e548b2d4ee91736961faca386d0a |
| chats | 306 | d2541ecae4fa59a1f1192498d0f5b4e0 |
| chat_members | 3295 | 4f1cf6a7c65e7a56c072c20877d7d28f |
| messages | 239717 | 1c0251c775eecc3ffc43c28b81084eea |
| schema_migrations | 1 | 550e0a1f7ef8568bdbe21b29ffb5399b |