ЛР2: PostgreSQL, сессии, Redis-кэш, трек 5.2

This commit is contained in:
2026-10-26 21:20:34 +03:00
parent 65003b6119
commit cf4a7ad142
56 changed files with 4209 additions and 14 deletions
+66
View File
@@ -0,0 +1,66 @@
{
"measured_at": "2026-10-26T17:30:45+00:00",
"dump": {
"format": "custom (-Fc -Z 6)",
"seconds": 0.783,
"bytes": 7220447
},
"restore": {
"parallel_jobs": 4,
"seconds": 0.969
},
"verify_seconds": 2.372,
"rto_dump_plus_restore_plus_verify_seconds": 4.124,
"table_checks": {
"users": {
"source": "2000",
"restored": "2000",
"match": true
},
"chats": {
"source": "300",
"restored": "300",
"match": true
},
"chat_members": {
"source": "7720",
"restored": "7720",
"match": true
},
"messages_static": {
"source": "120000",
"restored": "120000",
"match": true
},
"schema_migrations": {
"source": "2",
"restored": "2",
"match": true
},
"indexes": {
"source": "13",
"restored": "13",
"match": true
},
"constraints": {
"source": "23",
"restored": "23",
"match": true
}
},
"sample_messages": {
"count": 200,
"identical": true
},
"history_page_hot_chat_offset_1000_identical": true,
"concurrent_writes": {
"acked_before_dump_started": 76,
"acked_when_dump_finished": 137,
"acked_total": 221,
"present_in_source": 221,
"present_in_restored": 80,
"restored_is_contiguous_prefix": true,
"lost_relative_to_source": 141
},
"all_match": true
}
+27
View File
@@ -0,0 +1,27 @@
{
"generated_at": "2026-10-26T17:30:01+00:00",
"params": {
"seed": 0.42,
"users": 2000,
"chats": 300,
"messages": 120000,
"zipf_s": 1.1
},
"generation_seconds": 14.58,
"messages_total": 120000,
"chats_with_messages": 300,
"chat_size_top5": [
24294,
11289,
7264,
5382,
4266
],
"chat_size_p50": 98,
"chat_size_min": 38,
"top1_share": 0.2024,
"top10_share": 0.5441,
"messages_table_mb": 28.3,
"server_version": "16.15 (Ubuntu 16.15-0ubuntu0.24.04.1)",
"shared_buffers": "128MB"
}
+39
View File
@@ -0,0 +1,39 @@
-- btree(chat_id); hot chat, 24294 messages; keyset cursor at 50%
Limit (cost=4708.68..4708.80 rows=51 width=213) (actual time=14.248..14.259 rows=51 loops=1)
Buffers: shared hit=3642
-> Sort (cost=4708.68..4738.72 rows=12019 width=213) (actual time=14.247..14.254 rows=51 loops=1)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 42kB
Buffers: shared hit=3642
-> Bitmap Heap Scan on messages (cost=271.12..4307.70 rows=12019 width=213) (actual time=6.934..12.540 rows=12147 loops=1)
Recheck Cond: (chat_id = '0ab807ce-24eb-4da8-8458-ef0836fb3470'::uuid)
Filter: (ROW(created_at, id) > ROW('2026-02-04 13:58:00+03'::timestamp with time zone, 'dc3c4cd9-029a-40dd-976c-64cd71098149'::uuid))
Rows Removed by Filter: 12147
Heap Blocks: exact=3614
Buffers: shared hit=3636
-> Bitmap Index Scan on hist_idx (cost=0.00..268.11 rows=23976 width=0) (actual time=1.156..1.156 rows=24294 loops=1)
Index Cond: (chat_id = '0ab807ce-24eb-4da8-8458-ef0836fb3470'::uuid)
Buffers: shared hit=22
Planning:
Buffers: shared hit=132
Planning Time: 0.426 ms
Execution Time: 14.305 ms
-- same chat, OFFSET 12147
Limit (cost=5968.35..5968.48 rows=51 width=213) (actual time=24.772..24.782 rows=51 loops=1)
Buffers: shared hit=3642, temp read=535 written=676
-> Sort (cost=5937.98..5997.92 rows=23976 width=213) (actual time=22.894..24.401 rows=12198 loops=1)
Sort Key: created_at, id
Sort Method: external merge Disk: 5392kB
Buffers: shared hit=3642, temp read=535 written=676
-> Bitmap Heap Scan on messages (cost=274.11..4190.81 rows=23976 width=213) (actual time=1.657..11.046 rows=24294 loops=1)
Recheck Cond: (chat_id = '0ab807ce-24eb-4da8-8458-ef0836fb3470'::uuid)
Heap Blocks: exact=3614
Buffers: shared hit=3636
-> Bitmap Index Scan on hist_idx (cost=0.00..268.11 rows=23976 width=0) (actual time=1.179..1.179 rows=24294 loops=1)
Index Cond: (chat_id = '0ab807ce-24eb-4da8-8458-ef0836fb3470'::uuid)
Buffers: shared hit=22
Planning:
Buffers: shared hit=133
Planning Time: 0.460 ms
Execution Time: 25.426 ms
@@ -0,0 +1,29 @@
-- btree(chat_id, created_at, id) — выбран в миграции 0001; hot chat, 24294 messages; keyset cursor at 50%
Limit (cost=0.42..62.81 rows=51 width=213) (actual time=0.032..0.053 rows=51 loops=1)
Buffers: shared hit=12
-> Index Scan using hist_idx on messages (cost=0.42..15039.53 rows=12293 width=213) (actual time=0.031..0.049 rows=51 loops=1)
Index Cond: ((chat_id = '0ab807ce-24eb-4da8-8458-ef0836fb3470'::uuid) AND (ROW(created_at, id) > ROW('2026-02-04 13:58:00+03'::timestamp with time zone, 'dc3c4cd9-029a-40dd-976c-64cd71098149'::uuid)))
Buffers: shared hit=12
Planning:
Buffers: shared hit=143
Planning Time: 0.506 ms
Execution Time: 0.083 ms
-- same chat, OFFSET 12147
Limit (cost=6633.54..6633.67 rows=51 width=213) (actual time=32.900..32.912 rows=51 loops=1)
Buffers: shared hit=3796, temp read=535 written=676
-> Sort (cost=6603.18..6664.33 rows=24460 width=213) (actual time=29.824..32.370 rows=12198 loops=1)
Sort Key: created_at, id
Sort Method: external merge Disk: 5392kB
Buffers: shared hit=3796, temp read=535 written=676
-> Bitmap Heap Scan on messages (cost=897.98..4820.73 rows=24460 width=213) (actual time=2.958..14.794 rows=24294 loops=1)
Recheck Cond: (chat_id = '0ab807ce-24eb-4da8-8458-ef0836fb3470'::uuid)
Heap Blocks: exact=3614
Buffers: shared hit=3790
-> Bitmap Index Scan on hist_idx (cost=0.00..891.87 rows=24460 width=0) (actual time=2.442..2.442 rows=24294 loops=1)
Index Cond: (chat_id = '0ab807ce-24eb-4da8-8458-ef0836fb3470'::uuid)
Buffers: shared hit=176
Planning:
Buffers: shared hit=144
Planning Time: 0.500 ms
Execution Time: 33.741 ms
+563
View File
@@ -0,0 +1,563 @@
{
"targets": [
{
"label": "hot",
"chat_id": "0ab807ce-24eb-4da8-8458-ef0836fb3470",
"messages": 24294,
"offset": 12147,
"cursor_ts": "2026-02-04 13:58:00+03",
"cursor_id": "dc3c4cd9-029a-40dd-976c-64cd71098149"
},
{
"label": "median",
"chat_id": "5455628c-0b94-4d4a-95bf-74145b899408",
"messages": 99,
"offset": 49,
"cursor_ts": "2026-02-04 03:36:40+03",
"cursor_id": "01b51bb5-cfb2-4fce-a665-02087836dd8c"
},
{
"label": "small",
"chat_id": "74d7b4ab-e4ce-4065-b97b-1b00a2383364",
"messages": 38,
"offset": 19,
"cursor_ts": "2026-02-04 08:40:15+03",
"cursor_id": "b94bd4dd-e28c-4bce-8065-8a07a85a72bd"
}
],
"repeats": 15,
"variants": {
"V0_no_index": {
"note": "только PK(id): нет индекса по chat_id",
"index_bytes": 0,
"queries": {
"hot/keyset_first": {
"exec_ms_median": 18.126,
"exec_ms_p90": 19.806,
"shared_hit": 3705,
"shared_read": 0,
"plan": [
"Limit",
"Gather Merge",
"Sort",
"Seq Scan"
]
},
"hot/keyset_middle": {
"exec_ms_median": 18.343,
"exec_ms_p90": 22.394,
"shared_hit": 3647,
"shared_read": 0,
"plan": [
"Limit",
"Gather Merge",
"Sort",
"Seq Scan"
]
},
"hot/offset_middle": {
"exec_ms_median": 28.459,
"exec_ms_p90": 33.177,
"shared_hit": 3617,
"shared_read": 0,
"plan": [
"Limit",
"Sort",
"Seq Scan"
]
},
"median/keyset_first": {
"exec_ms_median": 14.429,
"exec_ms_p90": 15.984,
"shared_hit": 3617,
"shared_read": 0,
"plan": [
"Limit",
"Sort",
"Seq Scan"
]
},
"median/keyset_middle": {
"exec_ms_median": 15.644,
"exec_ms_p90": 16.287,
"shared_hit": 3647,
"shared_read": 0,
"plan": [
"Limit",
"Gather Merge",
"Sort",
"Seq Scan"
]
},
"median/offset_middle": {
"exec_ms_median": 14.285,
"exec_ms_p90": 15.051,
"shared_hit": 3617,
"shared_read": 0,
"plan": [
"Limit",
"Sort",
"Seq Scan"
]
},
"small/keyset_first": {
"exec_ms_median": 13.895,
"exec_ms_p90": 17.684,
"shared_hit": 3617,
"shared_read": 0,
"plan": [
"Limit",
"Sort",
"Seq Scan"
]
},
"small/keyset_middle": {
"exec_ms_median": 17.617,
"exec_ms_p90": 19.349,
"shared_hit": 3647,
"shared_read": 0,
"plan": [
"Limit",
"Gather Merge",
"Sort",
"Seq Scan"
]
},
"small/offset_middle": {
"exec_ms_median": 14.198,
"exec_ms_p90": 16.743,
"shared_hit": 3617,
"shared_read": 0,
"plan": [
"Limit",
"Sort",
"Seq Scan"
]
}
}
},
"V1_chat": {
"note": "btree(chat_id)",
"index_bytes": 876544,
"queries": {
"hot/keyset_first": {
"exec_ms_median": 12.668,
"exec_ms_p90": 15.04,
"shared_hit": 3636,
"shared_read": 0,
"plan": [
"Limit",
"Sort",
"Bitmap Heap Scan",
"Bitmap Index Scan using hist_idx"
]
},
"hot/keyset_middle": {
"exec_ms_median": 12.078,
"exec_ms_p90": 13.361,
"shared_hit": 3636,
"shared_read": 0,
"plan": [
"Limit",
"Sort",
"Bitmap Heap Scan",
"Bitmap Index Scan using hist_idx"
]
},
"hot/offset_middle": {
"exec_ms_median": 20.116,
"exec_ms_p90": 26.066,
"shared_hit": 3636,
"shared_read": 0,
"plan": [
"Limit",
"Sort",
"Bitmap Heap Scan",
"Bitmap Index Scan using hist_idx"
]
},
"median/keyset_first": {
"exec_ms_median": 0.163,
"exec_ms_p90": 0.192,
"shared_hit": 99,
"shared_read": 0,
"plan": [
"Limit",
"Sort",
"Bitmap Heap Scan",
"Bitmap Index Scan using hist_idx"
]
},
"median/keyset_middle": {
"exec_ms_median": 0.081,
"exec_ms_p90": 0.109,
"shared_hit": 99,
"shared_read": 0,
"plan": [
"Limit",
"Sort",
"Bitmap Heap Scan",
"Bitmap Index Scan using hist_idx"
]
},
"median/offset_middle": {
"exec_ms_median": 0.075,
"exec_ms_p90": 0.105,
"shared_hit": 99,
"shared_read": 0,
"plan": [
"Limit",
"Sort",
"Bitmap Heap Scan",
"Bitmap Index Scan using hist_idx"
]
},
"small/keyset_first": {
"exec_ms_median": 0.056,
"exec_ms_p90": 0.061,
"shared_hit": 40,
"shared_read": 0,
"plan": [
"Limit",
"Sort",
"Bitmap Heap Scan",
"Bitmap Index Scan using hist_idx"
]
},
"small/keyset_middle": {
"exec_ms_median": 0.038,
"exec_ms_p90": 0.041,
"shared_hit": 40,
"shared_read": 0,
"plan": [
"Limit",
"Sort",
"Bitmap Heap Scan",
"Bitmap Index Scan using hist_idx"
]
},
"small/offset_middle": {
"exec_ms_median": 0.038,
"exec_ms_p90": 0.041,
"shared_hit": 40,
"shared_read": 0,
"plan": [
"Limit",
"Sort",
"Bitmap Heap Scan",
"Bitmap Index Scan using hist_idx"
]
}
}
},
"V2_chat_created": {
"note": "btree(chat_id, created_at)",
"index_bytes": 4898816,
"queries": {
"hot/keyset_first": {
"exec_ms_median": 0.036,
"exec_ms_p90": 0.061,
"shared_hit": 10,
"shared_read": 0,
"plan": [
"Limit",
"Incremental Sort",
"Index Scan using hist_idx"
]
},
"hot/keyset_middle": {
"exec_ms_median": 0.038,
"exec_ms_p90": 0.049,
"shared_hit": 11,
"shared_read": 0,
"plan": [
"Limit",
"Incremental Sort",
"Index Scan using hist_idx"
]
},
"hot/offset_middle": {
"exec_ms_median": 21.445,
"exec_ms_p90": 25.194,
"shared_hit": 3737,
"shared_read": 0,
"plan": [
"Limit",
"Sort",
"Bitmap Heap Scan",
"Bitmap Index Scan using hist_idx"
]
},
"median/keyset_first": {
"exec_ms_median": 0.048,
"exec_ms_p90": 0.052,
"shared_hit": 54,
"shared_read": 0,
"plan": [
"Limit",
"Incremental Sort",
"Index Scan using hist_idx"
]
},
"median/keyset_middle": {
"exec_ms_median": 0.07,
"exec_ms_p90": 0.102,
"shared_hit": 53,
"shared_read": 0,
"plan": [
"Limit",
"Sort",
"Bitmap Heap Scan",
"Bitmap Index Scan using hist_idx"
]
},
"median/offset_middle": {
"exec_ms_median": 0.135,
"exec_ms_p90": 0.16,
"shared_hit": 100,
"shared_read": 0,
"plan": [
"Limit",
"Sort",
"Bitmap Heap Scan",
"Bitmap Index Scan using hist_idx"
]
},
"small/keyset_first": {
"exec_ms_median": 0.054,
"exec_ms_p90": 0.082,
"shared_hit": 41,
"shared_read": 0,
"plan": [
"Limit",
"Incremental Sort",
"Index Scan using hist_idx"
]
},
"small/keyset_middle": {
"exec_ms_median": 0.048,
"exec_ms_p90": 0.056,
"shared_hit": 23,
"shared_read": 0,
"plan": [
"Limit",
"Sort",
"Bitmap Heap Scan",
"Bitmap Index Scan using hist_idx"
]
},
"small/offset_middle": {
"exec_ms_median": 0.05,
"exec_ms_p90": 0.059,
"shared_hit": 41,
"shared_read": 0,
"plan": [
"Limit",
"Incremental Sort",
"Index Scan using hist_idx"
]
}
}
},
"V3_chat_created_id": {
"note": "btree(chat_id, created_at, id) — выбран в миграции 0001",
"index_bytes": 7094272,
"queries": {
"hot/keyset_first": {
"exec_ms_median": 0.023,
"exec_ms_p90": 0.034,
"shared_hit": 10,
"shared_read": 0,
"plan": [
"Limit",
"Index Scan using hist_idx"
]
},
"hot/keyset_middle": {
"exec_ms_median": 0.033,
"exec_ms_p90": 0.038,
"shared_hit": 12,
"shared_read": 0,
"plan": [
"Limit",
"Index Scan using hist_idx"
]
},
"hot/offset_middle": {
"exec_ms_median": 25.014,
"exec_ms_p90": 28.762,
"shared_hit": 3790,
"shared_read": 0,
"plan": [
"Limit",
"Sort",
"Bitmap Heap Scan",
"Bitmap Index Scan using hist_idx"
]
},
"median/keyset_first": {
"exec_ms_median": 0.028,
"exec_ms_p90": 0.036,
"shared_hit": 53,
"shared_read": 0,
"plan": [
"Limit",
"Index Scan using hist_idx"
]
},
"median/keyset_middle": {
"exec_ms_median": 0.05,
"exec_ms_p90": 0.077,
"shared_hit": 52,
"shared_read": 0,
"plan": [
"Limit",
"Sort",
"Bitmap Heap Scan",
"Bitmap Index Scan using hist_idx"
]
},
"median/offset_middle": {
"exec_ms_median": 0.128,
"exec_ms_p90": 0.133,
"shared_hit": 100,
"shared_read": 0,
"plan": [
"Limit",
"Sort",
"Bitmap Heap Scan",
"Bitmap Index Scan using hist_idx"
]
},
"small/keyset_first": {
"exec_ms_median": 0.026,
"exec_ms_p90": 0.03,
"shared_hit": 42,
"shared_read": 0,
"plan": [
"Limit",
"Index Scan using hist_idx"
]
},
"small/keyset_middle": {
"exec_ms_median": 0.028,
"exec_ms_p90": 0.029,
"shared_hit": 22,
"shared_read": 0,
"plan": [
"Limit",
"Sort",
"Bitmap Heap Scan",
"Bitmap Index Scan using hist_idx"
]
},
"small/offset_middle": {
"exec_ms_median": 0.023,
"exec_ms_p90": 0.038,
"shared_hit": 42,
"shared_read": 0,
"plan": [
"Limit",
"Index Scan using hist_idx"
]
}
}
},
"V4_covering": {
"note": "covering: + INCLUDE(sender_id, text)",
"index_bytes": 31449088,
"queries": {
"hot/keyset_first": {
"exec_ms_median": 0.031,
"exec_ms_p90": 0.033,
"shared_hit": 5,
"shared_read": 0,
"plan": [
"Limit",
"Index Only Scan using hist_idx"
]
},
"hot/keyset_middle": {
"exec_ms_median": 0.035,
"exec_ms_p90": 0.036,
"shared_hit": 6,
"shared_read": 0,
"plan": [
"Limit",
"Index Only Scan using hist_idx"
]
},
"hot/offset_middle": {
"exec_ms_median": 2.14,
"exec_ms_p90": 2.4,
"shared_hit": 390,
"shared_read": 0,
"plan": [
"Limit",
"Index Only Scan using hist_idx"
]
},
"median/keyset_first": {
"exec_ms_median": 0.032,
"exec_ms_p90": 0.052,
"shared_hit": 6,
"shared_read": 0,
"plan": [
"Limit",
"Index Only Scan using hist_idx"
]
},
"median/keyset_middle": {
"exec_ms_median": 0.034,
"exec_ms_p90": 0.035,
"shared_hit": 5,
"shared_read": 0,
"plan": [
"Limit",
"Index Only Scan using hist_idx"
]
},
"median/offset_middle": {
"exec_ms_median": 0.042,
"exec_ms_p90": 0.044,
"shared_hit": 7,
"shared_read": 0,
"plan": [
"Limit",
"Index Only Scan using hist_idx"
]
},
"small/keyset_first": {
"exec_ms_median": 0.027,
"exec_ms_p90": 0.029,
"shared_hit": 5,
"shared_read": 0,
"plan": [
"Limit",
"Index Only Scan using hist_idx"
]
},
"small/keyset_middle": {
"exec_ms_median": 0.022,
"exec_ms_p90": 0.022,
"shared_hit": 4,
"shared_read": 0,
"plan": [
"Limit",
"Index Only Scan using hist_idx"
]
},
"small/offset_middle": {
"exec_ms_median": 0.025,
"exec_ms_p90": 0.025,
"shared_hit": 5,
"shared_read": 0,
"plan": [
"Limit",
"Index Only Scan using hist_idx"
]
}
}
}
}
}
+82
View File
@@ -0,0 +1,82 @@
{
"rows_per_run": 50000,
"runs": 5,
"note": "один INSERT на строку внутри одной транзакции (без fsync на COMMIT); FK исключён, чтобы изолировать стоимость поддержки индекса истории",
"variants": {
"V0_no_index": {
"note": "только PK(id): нет индекса по chat_id",
"ms_median": 640.3,
"us_per_insert": 12.81,
"wal_bytes_per_insert": 360.4,
"index_bytes_after_run": 0,
"runs_ms": [
533.5,
646.4,
640.3,
641.4,
612.8
],
"overhead_vs_no_index_pct": 0.0
},
"V1_chat": {
"note": "btree(chat_id)",
"ms_median": 778.0,
"us_per_insert": 15.56,
"wal_bytes_per_insert": 438.4,
"index_bytes_after_run": 458752,
"runs_ms": [
778.0,
995.6,
826.4,
660.6,
758.6
],
"overhead_vs_no_index_pct": 21.5
},
"V2_chat_created": {
"note": "btree(chat_id, created_at)",
"ms_median": 772.0,
"us_per_insert": 15.44,
"wal_bytes_per_insert": 471.0,
"index_bytes_after_run": 3284992,
"runs_ms": [
805.3,
772.0,
756.2,
757.3,
817.3
],
"overhead_vs_no_index_pct": 20.5
},
"V3_chat_created_id": {
"note": "btree(chat_id, created_at, id) — выбран в миграции 0001",
"ms_median": 744.0,
"us_per_insert": 14.88,
"wal_bytes_per_insert": 501.8,
"index_bytes_after_run": 4743168,
"runs_ms": [
763.3,
676.1,
744.0,
749.5,
707.4
],
"overhead_vs_no_index_pct": 16.2
},
"V4_covering": {
"note": "covering: + INCLUDE(sender_id, text)",
"ms_median": 928.8,
"us_per_insert": 18.58,
"wal_bytes_per_insert": 849.2,
"index_bytes_after_run": 21970944,
"runs_ms": [
928.8,
973.1,
970.6,
872.0,
857.7
],
"overhead_vs_no_index_pct": 45.0
}
}
}