This commit is contained in:
2026-10-23 14:47:53 +03:00
parent 65003b6119
commit 84fc48d315
29 changed files with 1940 additions and 24 deletions
+13
View File
@@ -0,0 +1,13 @@
EXPLAIN (ANALYZE, BUFFERS) on 10_000 messages in one chat.
Command (from repo root, stack running):
docker compose run --rm \
-e DATABASE_URL=postgresql://messenger:messenger_dev_only@postgres:5432/messenger \
-e EXPLAIN_MESSAGE_COUNT=10000 \
-v ./scripts:/scripts:ro \
api python /scripts/explain_messages.py
Observed plan uses index messages_chat_history_idx (Bitmap Index Scan on chat_id),
not Seq Scan on all messages. Execution Time ~2.3 ms on local Docker.
Keyset pagination in API: WHERE chat_id = $1 AND (created_at, id) > ($cursor...) LIMIT n+1
+13
View File
@@ -0,0 +1,13 @@
# Track 2 — план запросов и восстановление
Гипотеза: для skewed-нагрузки (70k сообщений в одном чате) keyset по `(chat_id, created_at, id)` даёт стабильное чтение; большой `OFFSET` и лишний индекс `(chat_id)` ухудшают latency/запись.
Повтор:
```sh
py -3 scripts/track2_run.py
py -3 scripts/track2_backup_restore.py
RUN_TRACK2_VERIFY=1 py -3 -m unittest student-tests.test_track2
```
Артефакты: `benchmark.json`, `backup.dump`, `restore_report.json`.
Binary file not shown.
+28
View File
@@ -0,0 +1,28 @@
{
"hypothesis": "Keyset pagination on (chat_id, created_at, id) avoids large offsets; redundant (chat_id)-only index does not improve reads but slows writes.",
"hot_chat_id": "01aeb3d4-8235-4964-8981-ace0cec560df",
"read_plans": {
"keyset_limit_50": {
"node_type": "Index Only Scan",
"index_name": "messages_chat_history_idx",
"execution_ms": 0.349,
"plan_rows": 11
},
"offset_50000_limit_50": {
"node_type": "Index Only Scan",
"index_name": "messages_chat_history_idx",
"execution_ms": 7.043,
"plan_rows": 1
},
"keyset_with_extra_chat_id_index": {
"node_type": "Index Scan",
"index_name": "messages_chat_id_only_idx",
"execution_ms": 9.797,
"plan_rows": 11
}
},
"write_messages_per_second": {
"with_extra_chat_id_index": 18199.5,
"production_indexes_only": 20166.9
}
}
+69
View File
@@ -0,0 +1,69 @@
{
"source": {
"messages": 111012,
"users": 43,
"sample": [
{
"id": "d4bff878-42c2-4ed2-8261-e2fc42583f65",
"chat_id": "01aeb3d4-8235-4964-8981-ace0cec560df",
"text": "w"
},
{
"id": "c561eb24-1381-491f-b78c-b9fe460015b5",
"chat_id": "01aeb3d4-8235-4964-8981-ace0cec560df",
"text": "w"
},
{
"id": "e0789e8a-6c7b-40e9-8141-64d8547de768",
"chat_id": "01aeb3d4-8235-4964-8981-ace0cec560df",
"text": "w"
},
{
"id": "d0e5a399-932d-4006-91b1-f1cc26501af5",
"chat_id": "01aeb3d4-8235-4964-8981-ace0cec560df",
"text": "w"
},
{
"id": "de8288c2-a7fb-496b-8be7-cbfe82d138c5",
"chat_id": "01aeb3d4-8235-4964-8981-ace0cec560df",
"text": "w"
}
]
},
"restored": {
"messages": 111012,
"users": 43,
"sample": [
{
"id": "d4bff878-42c2-4ed2-8261-e2fc42583f65",
"chat_id": "01aeb3d4-8235-4964-8981-ace0cec560df",
"text": "w"
},
{
"id": "c561eb24-1381-491f-b78c-b9fe460015b5",
"chat_id": "01aeb3d4-8235-4964-8981-ace0cec560df",
"text": "w"
},
{
"id": "e0789e8a-6c7b-40e9-8141-64d8547de768",
"chat_id": "01aeb3d4-8235-4964-8981-ace0cec560df",
"text": "w"
},
{
"id": "d0e5a399-932d-4006-91b1-f1cc26501af5",
"chat_id": "01aeb3d4-8235-4964-8981-ace0cec560df",
"text": "w"
},
{
"id": "de8288c2-a7fb-496b-8be7-cbfe82d138c5",
"chat_id": "01aeb3d4-8235-4964-8981-ace0cec560df",
"text": "w"
}
]
},
"counts_match": true,
"sample_match": true,
"restore_seconds": 7.09,
"dump_bytes": 2899617,
"data_loss_boundary": "Logical pg_dump is consistent at dump start; concurrent writes during dump are not included. WAL/PITR not used — RPO equals changes after dump began."
}