ЛР2 на 5: Redis-кэш сессий, отказоустойчивость, трек 2 (индексы и восстановление)
Skeleton contracts / contracts (pull_request) Canceled after 0s

Уровень 4:
- Redis как кэш сессий (cache-aside, источник истины — PostgreSQL): TTL <= остатка сессии,
  метка revoked при logout + SET NX против гонки, при отказе Redis — работа через базу
  и фоновая проверка доступности
- тесты: гонка регистрации, откат посреди транзакции, недоступность БД и Redis (44 теста)
- EXPLAIN (ANALYZE, BUFFERS) на 22 000 сообщений, history_query общий для API и скрипта
- эксперименты отказа Redis/PostgreSQL под нагрузкой; dns_opt у API, чтобы отказ БД
  давал быстрый 503, а не зависание на поиске имени остановленного контейнера

Уровень 5, трек 2:
- 105 000 сообщений в неравномерных чатах, 4 варианта индекса + OFFSET против keyset,
  стоимость записи (время, WAL), два независимых прогона
- pg_dump -> pg_restore в новый контейнер -> сверка md5 всех таблиц и выборки сообщений,
  RTO 14 с, демонстрация RPO
This commit is contained in:
2026-10-29 15:42:37 +03:00
parent 5ce43c3315
commit ac1317d22e
36 changed files with 2932 additions and 62 deletions
+90
View File
@@ -0,0 +1,90 @@
# 2026-10-29T10:48:16Z PostgreSQL 17.10
# messages: 22024 rows total, 5560 kB with indexes; big chat 12000, 20 small chats x 500; ANALYZE done
# indexes on messages:
# CREATE INDEX messages_chat_history_idx ON public.messages USING btree (chat_id, created_at, id)
# CREATE UNIQUE INDEX messages_pkey ON public.messages USING btree (id)
# SUMMARY (top node, rows removed by filter, buffers of the top node, execution time)
# 1. big chat, first page (no cursor): Index Scan using messages_chat_history_idx; removed=0; buffers hit=5 read=0; 0.237 ms
# 2. big chat, middle page (cursor after #6000): Index Scan using messages_chat_history_idx; removed=0; buffers hit=5 read=0; 0.282 ms
# 3. big chat, last page (cursor after #11949): Index Scan using messages_chat_history_idx; removed=0; buffers hit=5 read=0; 0.241 ms
# 4. small chat, first page: Index Scan using messages_chat_history_idx; removed=0; buffers hit=6 read=0; 0.226 ms
# 5. for comparison: OFFSET pagination to the same depth as case 3 (NOT used by the API): Seq Scan on messages; removed=10024; buffers hit=296 read=0; 12.874 ms
## 1. big chat, first page (no cursor)
-- SQL generated by the API (history_query):
SELECT messages.id, messages.chat_id, messages.sender_id, messages.created_at, messages.text
FROM messages
WHERE messages.chat_id = %(chat_id_1)s::UUID ORDER BY messages.created_at, messages.id
LIMIT %(param_1)s::INTEGER
Limit (cost=0.41..8.65 rows=51 width=79) (actual time=0.054..0.075 rows=51 loops=1)
Buffers: shared hit=5
-> Index Scan using messages_chat_history_idx on messages (cost=0.41..1938.38 rows=12000 width=79) (actual time=0.052..0.066 rows=51 loops=1)
Index Cond: (chat_id = '46e81345-6190-4647-a359-24a7b2b54811'::uuid)
Buffers: shared hit=5
Planning:
Buffers: shared hit=6
Planning Time: 0.548 ms
Execution Time: 0.237 ms
## 2. big chat, middle page (cursor after #6000)
-- SQL generated by the API (history_query):
SELECT messages.id, messages.chat_id, messages.sender_id, messages.created_at, messages.text
FROM messages
WHERE messages.chat_id = %(chat_id_1)s::UUID AND (messages.created_at, messages.id) > (%(param_1)s::TIMESTAMP WITH TIME ZONE, %(param_2)s::UUID) ORDER BY messages.created_at, messages.id
LIMIT %(param_3)s::INTEGER
Limit (cost=0.41..10.57 rows=51 width=79) (actual time=0.044..0.064 rows=51 loops=1)
Buffers: shared hit=5
-> Index Scan using messages_chat_history_idx on messages (cost=0.41..1736.13 rows=8717 width=79) (actual time=0.043..0.055 rows=51 loops=1)
Index Cond: ((chat_id = '46e81345-6190-4647-a359-24a7b2b54811'::uuid) AND (ROW(created_at, id) > ROW('2026-10-29 10:48:07.896359+00'::timestamp with time zone, '402e4609-d59c-4a14-8778-c1b2ea8f741c'::uuid)))
Buffers: shared hit=5
Planning Time: 0.140 ms
Execution Time: 0.282 ms
## 3. big chat, last page (cursor after #11949)
-- SQL generated by the API (history_query):
SELECT messages.id, messages.chat_id, messages.sender_id, messages.created_at, messages.text
FROM messages
WHERE messages.chat_id = %(chat_id_1)s::UUID AND (messages.created_at, messages.id) > (%(param_1)s::TIMESTAMP WITH TIME ZONE, %(param_2)s::UUID) ORDER BY messages.created_at, messages.id
LIMIT %(param_3)s::INTEGER
Limit (cost=0.41..14.36 rows=51 width=79) (actual time=0.103..0.131 rows=50 loops=1)
Buffers: shared hit=5
-> Index Scan using messages_chat_history_idx on messages (cost=0.41..1509.60 rows=5517 width=79) (actual time=0.101..0.119 rows=50 loops=1)
Index Cond: ((chat_id = '46e81345-6190-4647-a359-24a7b2b54811'::uuid) AND (ROW(created_at, id) > ROW('2026-10-29 10:48:14.005334+00'::timestamp with time zone, 'd99d5478-4411-4166-b023-54ff684194b4'::uuid)))
Buffers: shared hit=5
Planning Time: 0.199 ms
Execution Time: 0.241 ms
## 4. small chat, first page
-- SQL generated by the API (history_query):
SELECT messages.id, messages.chat_id, messages.sender_id, messages.created_at, messages.text
FROM messages
WHERE messages.chat_id = %(chat_id_1)s::UUID ORDER BY messages.created_at, messages.id
LIMIT %(param_1)s::INTEGER
Limit (cost=0.41..108.66 rows=51 width=79) (actual time=0.101..0.127 rows=51 loops=1)
Buffers: shared hit=6
-> Index Scan using messages_chat_history_idx on messages (cost=0.41..1061.66 rows=500 width=79) (actual time=0.099..0.118 rows=51 loops=1)
Index Cond: (chat_id = '074dc7b2-e596-4686-b75e-ee925fd2bd87'::uuid)
Buffers: shared hit=6
Planning Time: 0.144 ms
Execution Time: 0.226 ms
## 5. for comparison: OFFSET pagination to the same depth as case 3 (NOT used by the API)
SELECT * FROM messages WHERE chat_id = %(c)s ORDER BY created_at, id OFFSET %(o)s LIMIT %(l)s
Limit (cost=1414.22..1414.34 rows=51 width=79) (actual time=11.721..11.735 rows=51 loops=1)
Buffers: shared hit=296
-> Sort (cost=1384.34..1414.34 rows=12000 width=79) (actual time=9.709..10.792 rows=12000 loops=1)
Sort Key: created_at, id
Sort Method: quicksort Memory: 1978kB
Buffers: shared hit=296
-> Seq Scan on messages (cost=0.00..571.30 rows=12000 width=79) (actual time=0.025..3.964 rows=12000 loops=1)
Filter: (chat_id = '46e81345-6190-4647-a359-24a7b2b54811'::uuid)
Rows Removed by Filter: 10024
Buffers: shared hit=296
Planning Time: 0.127 ms
Execution Time: 12.874 ms
@@ -0,0 +1,84 @@
{
"service": "postgres",
"method": "4 closed-loop workers: GET /users/me + POST message; client timeout 5 s; docker compose stop postgres at t=5.1s, start at t=24.6s",
"docker_stop_seconds": 1.41,
"health_while_down": {
"live": 200,
"ready": 503
},
"before": {
"seconds": 5.1,
"me": {
"requests": 161,
"statuses": {
"200": 161
},
"ok_latency_ms_p50": 49.0,
"ok_latency_ms_p95": 72.0,
"failed_latency_ms_max": null
},
"write": {
"requests": 157,
"statuses": {
"201": 157
},
"ok_latency_ms_p50": 72.0,
"ok_latency_ms_p95": 103.0,
"failed_latency_ms_max": null
}
},
"while_down": {
"seconds": 19.5,
"me": {
"requests": 52,
"statuses": {
"200": 52
},
"ok_latency_ms_p50": 49.0,
"ok_latency_ms_p95": 77.0,
"failed_latency_ms_max": null
},
"write": {
"requests": 56,
"statuses": {
"201": 17,
"503": 31,
"error": 8
},
"ok_latency_ms_p50": 87.0,
"ok_latency_ms_p95": 4141.0,
"failed_latency_ms_max": 5052.0
}
},
"after_start": {
"seconds": 16.6,
"me": {
"requests": 466,
"statuses": {
"200": 466
},
"ok_latency_ms_p50": 48.0,
"ok_latency_ms_p95": 72.0,
"failed_latency_ms_max": null
},
"write": {
"requests": 466,
"statuses": {
"201": 466
},
"ok_latency_ms_p50": 73.0,
"ok_latency_ms_p95": 99.0,
"failed_latency_ms_max": null
}
},
"first_failure_seconds_after_stop": 0.51,
"last_failure_seconds_after_start": 0.0,
"first_successful_write_seconds_after_start": 2.17,
"redis_session_keys_before_after": [
null,
null
],
"api_log_redis_warnings": [
"Redis недоступен (ConnectionError), сессии читаются из PostgreSQL; проверка доступности каждые 5 с"
]
}
+82
View File
@@ -0,0 +1,82 @@
{
"service": "postgres",
"method": "4 closed-loop workers: GET /users/me + POST message; client timeout 5 s; docker compose stop postgres at t=5.1s, start at t=17.5s",
"docker_stop_seconds": 1.27,
"health_while_down": {
"live": 200,
"ready": 503
},
"before": {
"seconds": 5.1,
"me": {
"requests": 163,
"statuses": {
"200": 163
},
"ok_latency_ms_p50": 46.0,
"ok_latency_ms_p95": 71.0,
"failed_latency_ms_max": null
},
"write": {
"requests": 162,
"statuses": {
"201": 162
},
"ok_latency_ms_p50": 76.0,
"ok_latency_ms_p95": 101.0,
"failed_latency_ms_max": null
}
},
"while_down": {
"seconds": 12.4,
"me": {
"requests": 65,
"statuses": {
"200": 65
},
"ok_latency_ms_p50": 43.0,
"ok_latency_ms_p95": 64.0,
"failed_latency_ms_max": null
},
"write": {
"requests": 66,
"statuses": {
"201": 10,
"503": 56
},
"ok_latency_ms_p50": 85.0,
"ok_latency_ms_p95": 156.0,
"failed_latency_ms_max": 3089.0
}
},
"after_start": {
"seconds": 16.6,
"me": {
"requests": 496,
"statuses": {
"200": 496
},
"ok_latency_ms_p50": 48.0,
"ok_latency_ms_p95": 73.0,
"failed_latency_ms_max": null
},
"write": {
"requests": 496,
"statuses": {
"503": 4,
"201": 492
},
"ok_latency_ms_p50": 72.0,
"ok_latency_ms_p95": 104.0,
"failed_latency_ms_max": 1066.0
}
},
"first_failure_seconds_after_stop": 0.54,
"last_failure_seconds_after_start": 1.56,
"first_successful_write_seconds_after_start": 1.38,
"redis_session_keys_before_after": [
null,
null
],
"api_log_redis_warnings": []
}
+82
View File
@@ -0,0 +1,82 @@
{
"service": "redis",
"method": "4 closed-loop workers: GET /users/me + POST message; client timeout 5 s; docker compose stop redis at t=5.6s, start at t=16.9s",
"docker_stop_seconds": 1.18,
"health_while_down": {
"live": 200,
"ready": 200
},
"before": {
"seconds": 5.6,
"me": {
"requests": 168,
"statuses": {
"200": 168
},
"ok_latency_ms_p50": 50.0,
"ok_latency_ms_p95": 75.0,
"failed_latency_ms_max": null
},
"write": {
"requests": 166,
"statuses": {
"201": 166
},
"ok_latency_ms_p50": 78.0,
"ok_latency_ms_p95": 107.0,
"failed_latency_ms_max": null
}
},
"while_down": {
"seconds": 11.3,
"me": {
"requests": 299,
"statuses": {
"200": 299
},
"ok_latency_ms_p50": 64.0,
"ok_latency_ms_p95": 90.0,
"failed_latency_ms_max": null
},
"write": {
"requests": 300,
"statuses": {
"201": 300
},
"ok_latency_ms_p50": 86.0,
"ok_latency_ms_p95": 115.0,
"failed_latency_ms_max": null
}
},
"after_start": {
"seconds": 11.2,
"me": {
"requests": 325,
"statuses": {
"200": 325
},
"ok_latency_ms_p50": 56.0,
"ok_latency_ms_p95": 85.0,
"failed_latency_ms_max": null
},
"write": {
"requests": 326,
"statuses": {
"201": 326
},
"ok_latency_ms_p50": 79.0,
"ok_latency_ms_p95": 106.0,
"failed_latency_ms_max": null
}
},
"first_failure_seconds_after_stop": null,
"last_failure_seconds_after_start": 0.0,
"first_successful_write_seconds_after_start": 0.09,
"redis_session_keys_before_after": [
5,
1
],
"api_log_redis_warnings": [
"Redis недоступен (ConnectionError), сессии читаются из PostgreSQL; проверка доступности каждые 5 с"
]
}
+14 -17
View File
@@ -1,20 +1,17 @@
# test-persistence (Windows: scripts\restart.cmd = same commands as scripts/restart.sh)
date: 2026-10-16T10:05:33
# 2026-10-29T13:56:19 persistence: restart api, postgres, redis with volumes kept; same bearer token after restart
## containers BEFORE
/mtusi-lab2-api-1 id=4dcf421abd1d started=2026-10-16T07:04:52.820712243Z status=running
/mtusi-lab2-postgres-1 id=64cc9db56db3 started=2026-10-16T07:04:41.833181321Z status=running
## test output (exit 0, 22.3s)
/mtusi-lab2-api-1 running StartedAt=2026-10-29T10:54:06.160154929Z
/mtusi-lab2-migrate-1 exited StartedAt=2026-10-29T10:54:01.857928321Z
/mtusi-lab2-postgres-1 running StartedAt=2026-10-29T10:54:56.204288024Z
/mtusi-lab2-redis-1 running StartedAt=2026-10-29T10:50:55.728984099Z
volume mtusi-lab2_postgres_data CreatedAt=2026-10-15T14:23:31Z
$ python tests/resilience.py --mode persistence --action-script scripts/restart.cmd
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
## containers AFTER (new StartedAt = really restarted)
/mtusi-lab2-api-1 id=4dcf421abd1d started=2026-10-16T07:05:25.634228444Z status=running
/mtusi-lab2-postgres-1 id=64cc9db56db3 started=2026-10-16T07:05:15.448515673Z status=running
## volume (same, not recreated)
mtusi-lab2_postgres_data created=2026-10-15T14:23:31Z
## rows in PostgreSQL after restart
users=15 chats=5 messages=9 active_sessions=8
exit=0
## containers AFTER (new StartedAt = really restarted; Redis comes back empty, sessions are re-read from PostgreSQL)
/mtusi-lab2-api-1 running StartedAt=2026-10-29T10:56:37.367116728Z
/mtusi-lab2-migrate-1 exited StartedAt=2026-10-29T10:56:33.062187191Z
/mtusi-lab2-postgres-1 running StartedAt=2026-10-29T10:56:26.947421567Z
/mtusi-lab2-redis-1 running StartedAt=2026-10-29T10:56:26.357852423Z
volume mtusi-lab2_postgres_data CreatedAt=2026-10-15T14:23:31Z
+57
View File
@@ -0,0 +1,57 @@
# 2026-10-29T13:55:35 student tests (PostgreSQL DB messenger_test, Redis DB 15)
$ .venv\Scripts\python.exe -m pytest -v
============================= test session starts =============================
platform win32 -- Python 3.14.3, pytest-9.1.1, pluggy-1.7.0 -- D:\диск С\Документы\unic\VP\lab2\mtusi2026lab2\.venv\Scripts\python.exe
rootdir: D:\диск С\Документы\unic\VP\lab2\mtusi2026lab2
configfile: pytest.ini
testpaths: student-tests
plugins: anyio-4.15.1
collecting ... collected 44 items
student-tests/test_auth_api.py::test_login_returns_session PASSED [ 2%]
student-tests/test_auth_api.py::test_jwt_payload_contains_sub_exp_jti PASSED [ 4%]
student-tests/test_auth_api.py::test_wrong_password_and_unknown_user_give_same_401 PASSED [ 6%]
student-tests/test_auth_api.py::test_malformed_basic_returns_401 PASSED [ 9%]
student-tests/test_auth_api.py::test_login_with_body_returns_400_and_creates_no_session PASSED [ 11%]
student-tests/test_auth_api.py::test_password_with_colon_and_unicode PASSED [ 13%]
student-tests/test_auth_api.py::test_protected_endpoint_requires_bearer PASSED [ 15%]
student-tests/test_auth_api.py::test_x_user_id_does_not_override_token PASSED [ 18%]
student-tests/test_auth_api.py::test_forged_token_is_rejected PASSED [ 20%]
student-tests/test_auth_api.py::test_logout_revokes_only_current_token PASSED [ 22%]
student-tests/test_auth_api.py::test_session_expires PASSED [ 25%]
student-tests/test_chats_api.py::test_unknown_member_returns_400_and_nothing_is_saved PASSED [ 27%]
student-tests/test_chats_api.py::test_created_chat_is_stored_with_members PASSED [ 29%]
student-tests/test_chats_api.py::test_foreign_chat_returns_404 PASSED [ 31%]
student-tests/test_chats_api.py::test_member_sees_chat PASSED [ 34%]
student-tests/test_chats_api.py::test_creator_must_be_member PASSED [ 36%]
student-tests/test_chats_api.py::test_duplicate_members_return_400 PASSED [ 38%]
student-tests/test_failures_api.py::test_parallel_registration_of_same_username_creates_one_user PASSED [ 40%]
student-tests/test_failures_api.py::test_chat_creation_rolls_back_when_member_insert_fails PASSED [ 43%]
student-tests/test_failures_api.py::test_database_down_gives_limited_503_and_no_anonymous_access PASSED [ 45%]
student-tests/test_failures_api.py::test_database_down_cached_session_still_authenticates PASSED [ 47%]
student-tests/test_messages_api.py::test_empty_message_returns_400 PASSED [ 50%]
student-tests/test_messages_api.py::test_sender_is_taken_from_token PASSED [ 52%]
student-tests/test_messages_api.py::test_pagination_over_several_pages PASSED [ 54%]
student-tests/test_messages_api.py::test_cursor_from_another_chat_returns_400 PASSED [ 56%]
student-tests/test_messages_api.py::test_invalid_limit_returns_400 PASSED [ 59%]
student-tests/test_session_cache.py::test_login_puts_session_into_cache_without_secrets PASSED [ 61%]
student-tests/test_session_cache.py::test_cached_session_is_served_without_database PASSED [ 63%]
student-tests/test_session_cache.py::test_cache_miss_reads_database_once_and_fills_cache PASSED [ 65%]
student-tests/test_session_cache.py::test_logout_revokes_cached_session PASSED [ 68%]
student-tests/test_session_cache.py::test_slow_reader_cannot_resurrect_session_after_logout PASSED [ 70%]
student-tests/test_session_cache.py::test_revoked_token_from_database_is_not_cached PASSED [ 72%]
student-tests/test_session_cache.py::test_redis_down_falls_back_to_database PASSED [ 75%]
student-tests/test_session_cache.py::test_redis_down_does_not_slow_every_request PASSED [ 77%]
student-tests/test_session_cache.py::test_cache_comes_back_after_redis_recovers PASSED [ 79%]
student-tests/test_users_api.py::test_invalid_json_returns_400 PASSED [ 81%]
student-tests/test_users_api.py::test_extra_field_and_wrong_type_return_400 PASSED [ 84%]
student-tests/test_users_api.py::test_duplicate_username_returns_409 PASSED [ 86%]
student-tests/test_users_api.py::test_unicode_display_name_is_kept PASSED [ 88%]
student-tests/test_users_api.py::test_password_is_required_and_length_checked PASSED [ 90%]
student-tests/test_users_api.py::test_password_is_hashed_and_never_returned PASSED [ 93%]
student-tests/test_users_api.py::test_me_returns_current_user PASSED [ 95%]
student-tests/test_users_api.py::test_x_user_id_no_longer_authenticates PASSED [ 97%]
student-tests/test_users_api.py::test_every_response_has_request_id PASSED [100%]
============================= 44 passed in 39.30s =============================
exit=0
+7
View File
@@ -0,0 +1,7 @@
# 2026-10-29T13:55:32 base commit 5ce43c3 (+ Redis/level-4 changes); stack: api + postgres + redis
$ python scripts/check_contract.py
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
exit=0
$ python tests/smoke.py --base-url http://localhost:8080
PASS: lab2 public API scenario; infrastructure and grading evidence remain separate.
exit=0
+62
View File
@@ -0,0 +1,62 @@
{
"generated_at": "2026-10-29T12:35:58Z",
"source": "compose service postgres, database messenger_bench",
"target": "new container mtusi-lab2-restore-check (same image, empty volume mtusi-lab2-restore-check-data, no published ports)",
"backup": {
"tool": "pg_dump -Fc (one consistent snapshot)",
"seconds": 2.23,
"bytes": 6480746,
"sha256": "4b01c0959421964e0ce0a3e12f673cbb94717ced122a4d070ff14d9d959b815a"
},
"restore": {
"tool": "pg_restore -j 4 --no-owner --no-privileges + ANALYZE",
"container_start_seconds": 7.39,
"restore_seconds": 6.59,
"rto_seconds": 13.98,
"restored_database_size": "75 MB"
},
"verification": {
"row_counts_equal": true,
"table_md5_equal": true,
"sample_messages_equal": true,
"sample_size": 36,
"counts": {
"alembic_version": 1,
"users": 300,
"sessions": 0,
"chats": 555,
"chat_members": 2216,
"messages": 105000
},
"md5_source": {
"alembic_version": "3cbb53ae3485d46443b969ffd1937453",
"users": "a103f13c50fd6edec2002008237c1821",
"sessions": "empty",
"chats": "abe832994eb322fd0f751e9fb616c98b",
"chat_members": "07c4e6a49afe0c4cbbd38866f89e284b",
"messages": "a1fd01da121bf535f2e84d2cb05c6831"
},
"md5_restored": {
"alembic_version": "3cbb53ae3485d46443b969ffd1937453",
"users": "a103f13c50fd6edec2002008237c1821",
"sessions": "empty",
"chats": "abe832994eb322fd0f751e9fb616c98b",
"chat_members": "07c4e6a49afe0c4cbbd38866f89e284b",
"messages": "a1fd01da121bf535f2e84d2cb05c6831"
},
"alembic_version_source": "0001",
"alembic_version_restored": "0001",
"indexes_on_messages": [
"messages_chat_history_idx",
"messages_pkey"
],
"history_query_plan_on_restored": "Limit (cost=0.42..24.71 rows=51 width=531) (actual time=0.188..1.148 rows=51 loops=1)\n Buffers: shared hit=51 read=4\n -> Index Scan using messages_chat_history_idx on messages (cost=0.42..28505.57 rows=59853 width=531) (actual time=0.186..1.134 rows=51 loops=1)\n Index Cond: (chat_id = '58d533ef-79b6-4078-a5b2-286c4076859e'::uuid)\n Buffers: shared hit=51 read=4\nPlanning:\n Buffers: shared hit=146\nPlanning Time: 5.411 ms\nExecution Time: 1.258 ms"
},
"data_loss_boundary": {
"messages_in_backup": 105000,
"messages_written_after_backup": 100,
"messages_in_source_at_failure_time": 105100,
"late_messages_present_in_restore": 0,
"lost_if_restored_from_this_backup": 100
}
}
+385
View File
@@ -0,0 +1,385 @@
{
"generated_at": "2026-10-29T11:15:19Z",
"database": "messenger_bench",
"postgres_settings": {
"server_version": "17.10",
"shared_buffers": "128MB",
"work_mem": "4MB",
"random_page_cost": "4",
"synchronous_commit": "on"
},
"messages_total": 105000,
"chat_classes": [
{
"class": "huge",
"chats": 1,
"messages_each": 60000
},
{
"class": "medium",
"chats": 4,
"messages_each": 5000
},
{
"class": "small",
"chats": 50,
"messages_each": 300
},
{
"class": "tiny",
"chats": 500,
"messages_each": 20
}
],
"representative_chats": [
{
"class": "huge",
"messages": 60000
},
{
"class": "medium",
"messages": 5000
},
{
"class": "small",
"messages": 300
},
{
"class": "tiny",
"messages": 20
}
],
"data_loaded_this_run": false,
"load_seconds": null,
"runs_per_query": 15,
"page_limit": 51,
"variants": {
"none": {
"index_bytes": 0,
"table_bytes": 60137472,
"reads": {
"huge/first": {
"chat_messages": 60000,
"median_ms": 45.406,
"p95_ms": 50.12,
"buffers": 7423,
"plan": "Limit -> Gather Merge -> Sort -> Seq Scan"
},
"huge/deep_90pct": {
"chat_messages": 60000,
"median_ms": 37.304,
"p95_ms": 40.512,
"buffers": 7365,
"plan": "Limit -> Gather Merge -> Sort -> Seq Scan"
},
"medium/first": {
"chat_messages": 5000,
"median_ms": 29.434,
"p95_ms": 42.16,
"buffers": 7335,
"plan": "Limit -> Sort -> Seq Scan"
},
"medium/deep_90pct": {
"chat_messages": 5000,
"median_ms": 36.372,
"p95_ms": 49.917,
"buffers": 7365,
"plan": "Limit -> Gather Merge -> Sort -> Seq Scan"
},
"small/first": {
"chat_messages": 300,
"median_ms": 26.969,
"p95_ms": 31.007,
"buffers": 7335,
"plan": "Limit -> Sort -> Seq Scan"
},
"small/deep_90pct": {
"chat_messages": 300,
"median_ms": 31.33,
"p95_ms": 33.104,
"buffers": 7365,
"plan": "Limit -> Gather Merge -> Sort -> Seq Scan"
},
"tiny/first": {
"chat_messages": 20,
"median_ms": 25.529,
"p95_ms": 29.511,
"buffers": 7335,
"plan": "Limit -> Sort -> Seq Scan"
},
"tiny/deep_90pct": {
"chat_messages": 20,
"median_ms": 35.777,
"p95_ms": 45.186,
"buffers": 7365,
"plan": "Limit -> Gather Merge -> Sort -> Seq Scan"
}
},
"writes": {
"bulk_rows": 20000,
"bulk_repeats": 3,
"bulk_seconds_each": [
2.611,
2.51,
2.599
],
"bulk_seconds": 2.599,
"bulk_rows_per_second": 7695,
"bulk_wal_bytes_per_row": 671,
"single_rows": 1000,
"single_latency_ms_median": 4.626,
"single_latency_ms_p95": 5.489,
"single_wal_bytes_per_row": 313
}
},
"chat_id": {
"index_bytes": 770048,
"table_bytes": 60137472,
"reads": {
"huge/first": {
"chat_messages": 60000,
"median_ms": 47.125,
"p95_ms": 64.228,
"buffers": 7423,
"plan": "Limit -> Gather Merge -> Sort -> Seq Scan"
},
"huge/deep_90pct": {
"chat_messages": 60000,
"median_ms": 27.676,
"p95_ms": 29.85,
"buffers": 4248,
"plan": "Limit -> Sort -> Bitmap Heap Scan -> Bitmap Index Scan (bench_chat_id_idx)"
},
"medium/first": {
"chat_messages": 5000,
"median_ms": 3.352,
"p95_ms": 5.07,
"buffers": 379,
"plan": "Limit -> Sort -> Bitmap Heap Scan -> Bitmap Index Scan (bench_chat_id_idx)"
},
"medium/deep_90pct": {
"chat_messages": 5000,
"median_ms": 2.615,
"p95_ms": 2.993,
"buffers": 379,
"plan": "Limit -> Sort -> Bitmap Heap Scan -> Bitmap Index Scan (bench_chat_id_idx)"
},
"small/first": {
"chat_messages": 300,
"median_ms": 0.586,
"p95_ms": 0.647,
"buffers": 25,
"plan": "Limit -> Sort -> Index Scan (bench_chat_id_idx)"
},
"small/deep_90pct": {
"chat_messages": 300,
"median_ms": 0.313,
"p95_ms": 0.343,
"buffers": 25,
"plan": "Limit -> Sort -> Index Scan (bench_chat_id_idx)"
},
"tiny/first": {
"chat_messages": 20,
"median_ms": 0.169,
"p95_ms": 0.258,
"buffers": 4,
"plan": "Limit -> Sort -> Index Scan (bench_chat_id_idx)"
},
"tiny/deep_90pct": {
"chat_messages": 20,
"median_ms": 0.175,
"p95_ms": 0.185,
"buffers": 4,
"plan": "Limit -> Sort -> Index Scan (bench_chat_id_idx)"
}
},
"writes": {
"bulk_rows": 20000,
"bulk_repeats": 3,
"bulk_seconds_each": [
2.701,
2.651,
2.689
],
"bulk_seconds": 2.689,
"bulk_rows_per_second": 7438,
"bulk_wal_bytes_per_row": 747,
"single_rows": 1000,
"single_latency_ms_median": 5.317,
"single_latency_ms_p95": 6.276,
"single_wal_bytes_per_row": 403
}
},
"chat_created_id": {
"index_bytes": 6201344,
"table_bytes": 60137472,
"reads": {
"huge/first": {
"chat_messages": 60000,
"median_ms": 0.26,
"p95_ms": 0.321,
"buffers": 54,
"plan": "Limit -> Index Scan (messages_chat_history_idx)"
},
"huge/deep_90pct": {
"chat_messages": 60000,
"median_ms": 0.252,
"p95_ms": 0.381,
"buffers": 54,
"plan": "Limit -> Index Scan (messages_chat_history_idx)"
},
"medium/first": {
"chat_messages": 5000,
"median_ms": 0.201,
"p95_ms": 0.298,
"buffers": 54,
"plan": "Limit -> Index Scan (messages_chat_history_idx)"
},
"medium/deep_90pct": {
"chat_messages": 5000,
"median_ms": 0.233,
"p95_ms": 0.329,
"buffers": 54,
"plan": "Limit -> Index Scan (messages_chat_history_idx)"
},
"small/first": {
"chat_messages": 300,
"median_ms": 0.183,
"p95_ms": 0.288,
"buffers": 53,
"plan": "Limit -> Index Scan (messages_chat_history_idx)"
},
"small/deep_90pct": {
"chat_messages": 300,
"median_ms": 0.177,
"p95_ms": 0.247,
"buffers": 31,
"plan": "Limit -> Index Scan (messages_chat_history_idx)"
},
"tiny/first": {
"chat_messages": 20,
"median_ms": 0.135,
"p95_ms": 0.204,
"buffers": 17,
"plan": "Limit -> Index Scan (messages_chat_history_idx)"
},
"tiny/deep_90pct": {
"chat_messages": 20,
"median_ms": 0.164,
"p95_ms": 0.301,
"buffers": 4,
"plan": "Limit -> Index Scan (messages_chat_history_idx)"
},
"huge/first/OFFSET": {
"chat_messages": 60000,
"median_ms": 0.249,
"p95_ms": 0.378,
"buffers": 54,
"plan": "Limit -> Index Scan (messages_chat_history_idx)"
},
"huge/deep_90pct/OFFSET": {
"chat_messages": 60000,
"median_ms": 262.605,
"p95_ms": 267.052,
"buffers": 7423,
"plan": "Limit -> Gather Merge -> Sort -> Seq Scan"
}
},
"writes": {
"bulk_rows": 20000,
"bulk_repeats": 3,
"bulk_seconds_each": [
2.712,
3.025,
2.641
],
"bulk_seconds": 2.712,
"bulk_rows_per_second": 7375,
"bulk_wal_bytes_per_row": 820,
"single_rows": 1000,
"single_latency_ms_median": 4.708,
"single_latency_ms_p95": 6.076,
"single_wal_bytes_per_row": 457
}
},
"covering": {
"index_bytes": 30416896,
"table_bytes": 60137472,
"reads": {
"huge/first": {
"chat_messages": 60000,
"median_ms": 0.152,
"p95_ms": 0.246,
"buffers": 5,
"plan": "Limit -> Index Only Scan (bench_history_covering_idx)"
},
"huge/deep_90pct": {
"chat_messages": 60000,
"median_ms": 0.182,
"p95_ms": 0.288,
"buffers": 5,
"plan": "Limit -> Index Only Scan (bench_history_covering_idx)"
},
"medium/first": {
"chat_messages": 5000,
"median_ms": 0.174,
"p95_ms": 0.269,
"buffers": 6,
"plan": "Limit -> Index Only Scan (bench_history_covering_idx)"
},
"medium/deep_90pct": {
"chat_messages": 5000,
"median_ms": 0.185,
"p95_ms": 0.27,
"buffers": 6,
"plan": "Limit -> Index Only Scan (bench_history_covering_idx)"
},
"small/first": {
"chat_messages": 300,
"median_ms": 0.183,
"p95_ms": 0.27,
"buffers": 6,
"plan": "Limit -> Index Only Scan (bench_history_covering_idx)"
},
"small/deep_90pct": {
"chat_messages": 300,
"median_ms": 0.171,
"p95_ms": 0.215,
"buffers": 5,
"plan": "Limit -> Index Only Scan (bench_history_covering_idx)"
},
"tiny/first": {
"chat_messages": 20,
"median_ms": 0.165,
"p95_ms": 0.282,
"buffers": 4,
"plan": "Limit -> Index Only Scan (bench_history_covering_idx)"
},
"tiny/deep_90pct": {
"chat_messages": 20,
"median_ms": 0.152,
"p95_ms": 0.188,
"buffers": 4,
"plan": "Limit -> Index Only Scan (bench_history_covering_idx)"
}
},
"writes": {
"bulk_rows": 20000,
"bulk_repeats": 3,
"bulk_seconds_each": [
2.958,
3.204,
2.822
],
"bulk_seconds": 2.958,
"bulk_rows_per_second": 6761,
"bulk_wal_bytes_per_row": 1232,
"single_rows": 1000,
"single_latency_ms_median": 4.679,
"single_latency_ms_p95": 5.922,
"single_wal_bytes_per_row": 557
}
}
},
"total_seconds": 87.6
}
+34
View File
@@ -0,0 +1,34 @@
# Трек 2: индексы истории (2026-10-29T11:15:19Z)
PostgreSQL 17.10, shared_buffers 128MB, work_mem 4MB; 105000 сообщений; медиана из 15 замеров, тёплый кэш.
## Чтение страницы истории (запрос API), медиана мс / буферы
| чат (сообщений) / страница | none | chat_id | chat_created_id | covering |
| --- | --- | --- | --- | --- |
| huge/first (60000) | 45.406 / 7423 | 47.125 / 7423 | 0.26 / 54 | 0.152 / 5 |
| huge/deep_90pct (60000) | 37.304 / 7365 | 27.676 / 4248 | 0.252 / 54 | 0.182 / 5 |
| medium/first (5000) | 29.434 / 7335 | 3.352 / 379 | 0.201 / 54 | 0.174 / 6 |
| medium/deep_90pct (5000) | 36.372 / 7365 | 2.615 / 379 | 0.233 / 54 | 0.185 / 6 |
| small/first (300) | 26.969 / 7335 | 0.586 / 25 | 0.183 / 53 | 0.183 / 6 |
| small/deep_90pct (300) | 31.33 / 7365 | 0.313 / 25 | 0.177 / 31 | 0.171 / 5 |
| tiny/first (20) | 25.529 / 7335 | 0.169 / 4 | 0.135 / 17 | 0.165 / 4 |
| tiny/deep_90pct (20) | 35.777 / 7365 | 0.175 / 4 | 0.164 / 4 | 0.152 / 4 |
| huge/first/OFFSET (60000) | — | — | 0.249 / 54 | — |
| huge/deep_90pct/OFFSET (60000) | — | — | 262.605 / 7423 | — |
## Узел плана (глубокая страница самого большого чата)
- **none**: Limit -> Gather Merge -> Sort -> Seq Scan
- **chat_id**: Limit -> Sort -> Bitmap Heap Scan -> Bitmap Index Scan (bench_chat_id_idx)
- **chat_created_id**: Limit -> Index Scan (messages_chat_history_idx)
- **covering**: Limit -> Index Only Scan (bench_history_covering_idx)
## Размер и стоимость записи
| вариант | индекс, MiB | bulk, строк/с | bulk WAL, байт/строку | одиночный INSERT+COMMIT p50 / p95, мс | одиночный WAL, байт/строку |
| --- | --- | --- | --- | --- | --- |
| none | 0.0 | 7695 | 671 | 4.626 / 5.489 | 313 |
| chat_id | 0.7 | 7438 | 747 | 5.317 / 6.276 | 403 |
| chat_created_id | 5.9 | 7375 | 820 | 4.708 / 6.076 | 457 |
| covering | 29.0 | 6761 | 1232 | 4.679 / 5.922 | 557 |
+385
View File
@@ -0,0 +1,385 @@
{
"generated_at": "2026-10-29T12:34:53Z",
"database": "messenger_bench",
"postgres_settings": {
"server_version": "17.10",
"shared_buffers": "128MB",
"work_mem": "4MB",
"random_page_cost": "4",
"synchronous_commit": "on"
},
"messages_total": 105000,
"chat_classes": [
{
"class": "huge",
"chats": 1,
"messages_each": 60000
},
{
"class": "medium",
"chats": 4,
"messages_each": 5000
},
{
"class": "small",
"chats": 50,
"messages_each": 300
},
{
"class": "tiny",
"chats": 500,
"messages_each": 20
}
],
"representative_chats": [
{
"class": "huge",
"messages": 60000
},
{
"class": "medium",
"messages": 5000
},
{
"class": "small",
"messages": 300
},
{
"class": "tiny",
"messages": 20
}
],
"data_loaded_this_run": true,
"load_seconds": 28.3,
"runs_per_query": 15,
"page_limit": 51,
"variants": {
"none": {
"index_bytes": 0,
"table_bytes": 60153856,
"reads": {
"huge/first": {
"chat_messages": 60000,
"median_ms": 85.401,
"p95_ms": 89.219,
"buffers": 7425,
"plan": "Limit -> Gather Merge -> Sort -> Seq Scan"
},
"huge/deep_90pct": {
"chat_messages": 60000,
"median_ms": 79.308,
"p95_ms": 94.264,
"buffers": 7367,
"plan": "Limit -> Gather Merge -> Sort -> Seq Scan"
},
"medium/first": {
"chat_messages": 5000,
"median_ms": 29.138,
"p95_ms": 31.576,
"buffers": 7337,
"plan": "Limit -> Sort -> Seq Scan"
},
"medium/deep_90pct": {
"chat_messages": 5000,
"median_ms": 82.038,
"p95_ms": 87.422,
"buffers": 7367,
"plan": "Limit -> Gather Merge -> Sort -> Seq Scan"
},
"small/first": {
"chat_messages": 300,
"median_ms": 28.056,
"p95_ms": 39.0,
"buffers": 7337,
"plan": "Limit -> Sort -> Seq Scan"
},
"small/deep_90pct": {
"chat_messages": 300,
"median_ms": 83.681,
"p95_ms": 98.374,
"buffers": 7367,
"plan": "Limit -> Gather Merge -> Sort -> Seq Scan"
},
"tiny/first": {
"chat_messages": 20,
"median_ms": 26.3,
"p95_ms": 36.74,
"buffers": 7337,
"plan": "Limit -> Sort -> Seq Scan"
},
"tiny/deep_90pct": {
"chat_messages": 20,
"median_ms": 78.407,
"p95_ms": 89.693,
"buffers": 7367,
"plan": "Limit -> Gather Merge -> Sort -> Seq Scan"
}
},
"writes": {
"bulk_rows": 20000,
"bulk_repeats": 3,
"bulk_seconds_each": [
2.525,
2.416,
2.631
],
"bulk_seconds": 2.525,
"bulk_rows_per_second": 7919,
"bulk_wal_bytes_per_row": 673,
"single_rows": 1000,
"single_latency_ms_median": 4.655,
"single_latency_ms_p95": 5.628,
"single_wal_bytes_per_row": 313
}
},
"chat_id": {
"index_bytes": 770048,
"table_bytes": 60153856,
"reads": {
"huge/first": {
"chat_messages": 60000,
"median_ms": 83.848,
"p95_ms": 91.434,
"buffers": 7425,
"plan": "Limit -> Gather Merge -> Sort -> Seq Scan"
},
"huge/deep_90pct": {
"chat_messages": 60000,
"median_ms": 29.628,
"p95_ms": 36.168,
"buffers": 4248,
"plan": "Limit -> Sort -> Bitmap Heap Scan -> Bitmap Index Scan (bench_chat_id_idx)"
},
"medium/first": {
"chat_messages": 5000,
"median_ms": 4.607,
"p95_ms": 5.011,
"buffers": 398,
"plan": "Limit -> Sort -> Bitmap Heap Scan -> Bitmap Index Scan (bench_chat_id_idx)"
},
"medium/deep_90pct": {
"chat_messages": 5000,
"median_ms": 2.937,
"p95_ms": 4.636,
"buffers": 398,
"plan": "Limit -> Sort -> Bitmap Heap Scan -> Bitmap Index Scan (bench_chat_id_idx)"
},
"small/first": {
"chat_messages": 300,
"median_ms": 0.614,
"p95_ms": 0.846,
"buffers": 32,
"plan": "Limit -> Sort -> Index Scan (bench_chat_id_idx)"
},
"small/deep_90pct": {
"chat_messages": 300,
"median_ms": 0.391,
"p95_ms": 0.501,
"buffers": 32,
"plan": "Limit -> Sort -> Index Scan (bench_chat_id_idx)"
},
"tiny/first": {
"chat_messages": 20,
"median_ms": 0.247,
"p95_ms": 0.443,
"buffers": 4,
"plan": "Limit -> Sort -> Index Scan (bench_chat_id_idx)"
},
"tiny/deep_90pct": {
"chat_messages": 20,
"median_ms": 0.171,
"p95_ms": 0.224,
"buffers": 4,
"plan": "Limit -> Sort -> Index Scan (bench_chat_id_idx)"
}
},
"writes": {
"bulk_rows": 20000,
"bulk_repeats": 3,
"bulk_seconds_each": [
2.817,
2.826,
2.826
],
"bulk_seconds": 2.826,
"bulk_rows_per_second": 7077,
"bulk_wal_bytes_per_row": 746,
"single_rows": 1000,
"single_latency_ms_median": 5.336,
"single_latency_ms_p95": 6.237,
"single_wal_bytes_per_row": 403
}
},
"chat_created_id": {
"index_bytes": 6201344,
"table_bytes": 60153856,
"reads": {
"huge/first": {
"chat_messages": 60000,
"median_ms": 0.306,
"p95_ms": 0.455,
"buffers": 55,
"plan": "Limit -> Index Scan (messages_chat_history_idx)"
},
"huge/deep_90pct": {
"chat_messages": 60000,
"median_ms": 0.309,
"p95_ms": 0.361,
"buffers": 54,
"plan": "Limit -> Index Scan (messages_chat_history_idx)"
},
"medium/first": {
"chat_messages": 5000,
"median_ms": 0.319,
"p95_ms": 0.459,
"buffers": 55,
"plan": "Limit -> Index Scan (messages_chat_history_idx)"
},
"medium/deep_90pct": {
"chat_messages": 5000,
"median_ms": 0.262,
"p95_ms": 0.509,
"buffers": 55,
"plan": "Limit -> Index Scan (messages_chat_history_idx)"
},
"small/first": {
"chat_messages": 300,
"median_ms": 0.321,
"p95_ms": 0.381,
"buffers": 53,
"plan": "Limit -> Index Scan (messages_chat_history_idx)"
},
"small/deep_90pct": {
"chat_messages": 300,
"median_ms": 0.269,
"p95_ms": 0.351,
"buffers": 31,
"plan": "Limit -> Index Scan (messages_chat_history_idx)"
},
"tiny/first": {
"chat_messages": 20,
"median_ms": 0.231,
"p95_ms": 0.378,
"buffers": 13,
"plan": "Limit -> Index Scan (messages_chat_history_idx)"
},
"tiny/deep_90pct": {
"chat_messages": 20,
"median_ms": 0.185,
"p95_ms": 0.225,
"buffers": 4,
"plan": "Limit -> Index Scan (messages_chat_history_idx)"
},
"huge/first/OFFSET": {
"chat_messages": 60000,
"median_ms": 0.352,
"p95_ms": 0.476,
"buffers": 55,
"plan": "Limit -> Index Scan (messages_chat_history_idx)"
},
"huge/deep_90pct/OFFSET": {
"chat_messages": 60000,
"median_ms": 300.29,
"p95_ms": 325.584,
"buffers": 7425,
"plan": "Limit -> Gather Merge -> Sort -> Seq Scan"
}
},
"writes": {
"bulk_rows": 20000,
"bulk_repeats": 3,
"bulk_seconds_each": [
2.929,
2.957,
2.978
],
"bulk_seconds": 2.957,
"bulk_rows_per_second": 6764,
"bulk_wal_bytes_per_row": 821,
"single_rows": 1000,
"single_latency_ms_median": 5.429,
"single_latency_ms_p95": 6.584,
"single_wal_bytes_per_row": 457
}
},
"covering": {
"index_bytes": 30416896,
"table_bytes": 60153856,
"reads": {
"huge/first": {
"chat_messages": 60000,
"median_ms": 0.217,
"p95_ms": 0.289,
"buffers": 5,
"plan": "Limit -> Index Only Scan (bench_history_covering_idx)"
},
"huge/deep_90pct": {
"chat_messages": 60000,
"median_ms": 0.232,
"p95_ms": 0.291,
"buffers": 5,
"plan": "Limit -> Index Only Scan (bench_history_covering_idx)"
},
"medium/first": {
"chat_messages": 5000,
"median_ms": 0.174,
"p95_ms": 0.289,
"buffers": 5,
"plan": "Limit -> Index Only Scan (bench_history_covering_idx)"
},
"medium/deep_90pct": {
"chat_messages": 5000,
"median_ms": 0.206,
"p95_ms": 0.272,
"buffers": 6,
"plan": "Limit -> Index Only Scan (bench_history_covering_idx)"
},
"small/first": {
"chat_messages": 300,
"median_ms": 0.196,
"p95_ms": 0.225,
"buffers": 6,
"plan": "Limit -> Index Only Scan (bench_history_covering_idx)"
},
"small/deep_90pct": {
"chat_messages": 300,
"median_ms": 0.199,
"p95_ms": 0.263,
"buffers": 5,
"plan": "Limit -> Index Only Scan (bench_history_covering_idx)"
},
"tiny/first": {
"chat_messages": 20,
"median_ms": 0.172,
"p95_ms": 0.263,
"buffers": 4,
"plan": "Limit -> Index Only Scan (bench_history_covering_idx)"
},
"tiny/deep_90pct": {
"chat_messages": 20,
"median_ms": 0.159,
"p95_ms": 0.214,
"buffers": 4,
"plan": "Limit -> Index Only Scan (bench_history_covering_idx)"
}
},
"writes": {
"bulk_rows": 20000,
"bulk_repeats": 3,
"bulk_seconds_each": [
3.179,
3.103,
3.091
],
"bulk_seconds": 3.103,
"bulk_rows_per_second": 6445,
"bulk_wal_bytes_per_row": 1230,
"single_rows": 1000,
"single_latency_ms_median": 4.845,
"single_latency_ms_p95": 6.264,
"single_wal_bytes_per_row": 558
}
}
},
"total_seconds": 125.1
}
+34
View File
@@ -0,0 +1,34 @@
# Трек 2: индексы истории (2026-10-29T12:34:53Z)
PostgreSQL 17.10, shared_buffers 128MB, work_mem 4MB; 105000 сообщений; медиана из 15 замеров, тёплый кэш.
## Чтение страницы истории (запрос API), медиана мс / буферы
| чат (сообщений) / страница | none | chat_id | chat_created_id | covering |
| --- | --- | --- | --- | --- |
| huge/first (60000) | 85.401 / 7425 | 83.848 / 7425 | 0.306 / 55 | 0.217 / 5 |
| huge/deep_90pct (60000) | 79.308 / 7367 | 29.628 / 4248 | 0.309 / 54 | 0.232 / 5 |
| medium/first (5000) | 29.138 / 7337 | 4.607 / 398 | 0.319 / 55 | 0.174 / 5 |
| medium/deep_90pct (5000) | 82.038 / 7367 | 2.937 / 398 | 0.262 / 55 | 0.206 / 6 |
| small/first (300) | 28.056 / 7337 | 0.614 / 32 | 0.321 / 53 | 0.196 / 6 |
| small/deep_90pct (300) | 83.681 / 7367 | 0.391 / 32 | 0.269 / 31 | 0.199 / 5 |
| tiny/first (20) | 26.3 / 7337 | 0.247 / 4 | 0.231 / 13 | 0.172 / 4 |
| tiny/deep_90pct (20) | 78.407 / 7367 | 0.171 / 4 | 0.185 / 4 | 0.159 / 4 |
| huge/first/OFFSET (60000) | — | — | 0.352 / 55 | — |
| huge/deep_90pct/OFFSET (60000) | — | — | 300.29 / 7425 | — |
## Узел плана (глубокая страница самого большого чата)
- **none**: Limit -> Gather Merge -> Sort -> Seq Scan
- **chat_id**: Limit -> Sort -> Bitmap Heap Scan -> Bitmap Index Scan (bench_chat_id_idx)
- **chat_created_id**: Limit -> Index Scan (messages_chat_history_idx)
- **covering**: Limit -> Index Only Scan (bench_history_covering_idx)
## Размер и стоимость записи
| вариант | индекс, MiB | bulk, строк/с | bulk WAL, байт/строку | одиночный INSERT+COMMIT p50 / p95, мс | одиночный WAL, байт/строку |
| --- | --- | --- | --- | --- | --- |
| none | 0.0 | 7919 | 673 | 4.655 / 5.628 | 313 |
| chat_id | 0.7 | 7077 | 746 | 5.336 / 6.237 | 403 |
| chat_created_id | 5.9 | 6764 | 821 | 5.429 / 6.584 | 457 |
| covering | 29.0 | 6445 | 1230 | 4.845 / 6.264 | 558 |
+77
View File
@@ -0,0 +1,77 @@
## none: huge chat (60000 msgs), deep page (90%), API query
Limit (cost=9183.85..9189.80 rows=51 width=532) (actual time=67.276..87.978 rows=51 loops=1)
Buffers: shared hit=7367
-> Gather Merge (cost=9183.85..9751.83 rows=4868 width=532) (actual time=67.273..87.966 rows=51 loops=1)
Workers Planned: 2
Workers Launched: 2
Buffers: shared hit=7367
-> Sort (cost=8183.83..8189.91 rows=2434 width=532) (actual time=13.388..13.393 rows=17 loops=3)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 85kB
Buffers: shared hit=7367
Worker 0: Sort Method: quicksort Memory: 25kB
Worker 1: Sort Method: quicksort Memory: 25kB
-> Parallel Seq Scan on messages (cost=0.00..8102.62 rows=2434 width=532) (actual time=0.009..12.206 rows=2000 loops=3)
Filter: ((chat_id = '58d533ef-79b6-4078-a5b2-286c4076859e'::uuid) AND (ROW(created_at, id) > ROW('2026-09-28 01:41:51.032324+00'::timestamp with time zone, 'db8caccb-93cc-4062-b61e-ddeb6b88f5c4'::uuid)))
Rows Removed by Filter: 33000
Buffers: shared hit=7337
Planning Time: 0.290 ms
Execution Time: 88.155 ms
## chat_id: huge chat (60000 msgs), deep page (90%), API query
Limit (cost=9260.53..9260.66 rows=51 width=532) (actual time=26.160..26.180 rows=51 loops=1)
Buffers: shared hit=4248
-> Sort (cost=9260.53..9275.84 rows=6124 width=532) (actual time=26.158..26.170 rows=51 loops=1)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 85kB
Buffers: shared hit=4248
-> Bitmap Heap Scan on messages (cost=668.24..9056.22 rows=6124 width=532) (actual time=5.633..24.171 rows=5999 loops=1)
Recheck Cond: (chat_id = '58d533ef-79b6-4078-a5b2-286c4076859e'::uuid)
Filter: (ROW(created_at, id) > ROW('2026-09-28 01:41:51.032324+00'::timestamp with time zone, 'db8caccb-93cc-4062-b61e-ddeb6b88f5c4'::uuid))
Rows Removed by Filter: 54001
Heap Blocks: exact=4194
Buffers: shared hit=4248
-> Bitmap Index Scan on bench_chat_id_idx (cost=0.00..666.71 rows=60056 width=0) (actual time=4.493..4.493 rows=60000 loops=1)
Index Cond: (chat_id = '58d533ef-79b6-4078-a5b2-286c4076859e'::uuid)
Buffers: shared hit=54
Planning Time: 0.143 ms
Execution Time: 26.636 ms
## chat_created_id: huge chat, OFFSET 54000 (NOT used by the API)
Limit (cost=22459.09..22459.20 rows=1 width=532) (actual time=259.657..291.046 rows=51 loops=1)
Buffers: shared hit=7425, temp read=3869 written=3991
-> Gather Merge (cost=16653.81..22459.09 rows=49756 width=532) (actual time=171.715..286.418 rows=54051 loops=1)
Workers Planned: 2
Workers Launched: 2
Buffers: shared hit=7425, temp read=3869 written=3991
-> Sort (cost=15653.79..15715.99 rows=24878 width=532) (actual time=124.502..136.749 rows=18090 loops=3)
Sort Key: created_at, id
Sort Method: external merge Disk: 14880kB
Buffers: shared hit=7425, temp read=3869 written=3991
Worker 0: Sort Method: external merge Disk: 8488kB
Worker 1: Sort Method: external merge Disk: 8472kB
-> Parallel Seq Scan on messages (cost=0.00..7883.88 rows=24878 width=532) (actual time=0.039..16.722 rows=20000 loops=3)
Filter: (chat_id = '58d533ef-79b6-4078-a5b2-286c4076859e'::uuid)
Rows Removed by Filter: 15000
Buffers: shared hit=7337
Planning Time: 0.145 ms
Execution Time: 311.469 ms
## chat_created_id: huge chat (60000 msgs), deep page (90%), API query
Limit (cost=0.42..125.73 rows=51 width=532) (actual time=0.062..0.155 rows=51 loops=1)
Buffers: shared hit=54
-> Index Scan using messages_chat_history_idx on messages (cost=0.42..14974.22 rows=6094 width=532) (actual time=0.061..0.147 rows=51 loops=1)
Index Cond: ((chat_id = '58d533ef-79b6-4078-a5b2-286c4076859e'::uuid) AND (ROW(created_at, id) > ROW('2026-09-28 01:41:51.032324+00'::timestamp with time zone, 'db8caccb-93cc-4062-b61e-ddeb6b88f5c4'::uuid)))
Buffers: shared hit=54
Planning Time: 0.188 ms
Execution Time: 0.264 ms
## covering: huge chat (60000 msgs), deep page (90%), API query
Limit (cost=0.42..8.67 rows=51 width=529) (actual time=0.077..0.104 rows=51 loops=1)
Buffers: shared hit=5
-> Index Only Scan using bench_history_covering_idx on messages (cost=0.42..986.22 rows=6090 width=529) (actual time=0.076..0.095 rows=51 loops=1)
Index Cond: ((chat_id = '58d533ef-79b6-4078-a5b2-286c4076859e'::uuid) AND (ROW(created_at, id) > ROW('2026-09-28 01:41:51.032324+00'::timestamp with time zone, 'db8caccb-93cc-4062-b61e-ddeb6b88f5c4'::uuid)))
Heap Fetches: 0
Buffers: shared hit=5
Planning Time: 0.145 ms
Execution Time: 0.187 ms