ЛР2: PostgreSQL, сессии и Valkey (на 4)
Skeleton contracts / contracts (pull_request) Canceled after 0s

API мессенджера на FastAPI + asyncpg, миграции, Argon2id, Bearer-сессии
в PostgreSQL с кэшем Valkey, compose-стенд, restart.sh, тесты уровня 4,
EXPLAIN и отчёт.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-29 18:49:32 +03:00
co-authored by Claude Opus 5.5
parent 65003b6119
commit 0c5846173b
31 changed files with 1880 additions and 15 deletions
+102
View File
@@ -0,0 +1,102 @@
# EXPLAIN evidence, 2026-10-29T15:35:04Z, commit 65003b6
== Объём данных
total_messages | big_chat_messages | table_size | index_size
----------------+-------------------+------------+------------
50029 | 20000 | 5096 kB | 4112 kB
(1 row)
== 1. Первая страница (запрос API без cursor, limit=50 -> LIMIT 51)
QUERY PLAN
------------------------------------------------------------------------------------------------------------
Limit (actual time=0.017..0.046 rows=51 loops=1)
Buffers: shared hit=6
-> Index Scan using messages_chat_created_id_idx on messages (actual time=0.016..0.037 rows=51 loops=1)
Index Cond: (chat_id = 'ad832a44-34ce-416b-a5f8-f8e528316fd6'::uuid)
Buffers: shared hit=6
Planning:
Buffers: shared hit=6
Planning Time: 0.156 ms
Execution Time: 0.072 ms
(9 rows)
== 2. Страница 301 через keyset cursor (запрос API с cursor)
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (actual time=0.015..0.036 rows=51 loops=1)
Buffers: shared hit=5
-> Index Scan using messages_chat_created_id_idx on messages (actual time=0.015..0.029 rows=51 loops=1)
Index Cond: ((chat_id = 'ad832a44-34ce-416b-a5f8-f8e528316fd6'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 04:10:00+00'::timestamp with time zone, '1e3006cf-e446-47db-80e3-a34c031790a8'::uuid)))
Buffers: shared hit=5
Planning Time: 0.232 ms
Execution Time: 0.086 ms
(7 rows)
== 3. Для сравнения: та же страница через OFFSET (НЕ используется в API)
QUERY PLAN
---------------------------------------------------------------------------------------------------------------
Limit (actual time=4.002..4.015 rows=51 loops=1)
Buffers: shared hit=313
-> Index Scan using messages_chat_created_id_idx on messages (actual time=0.017..3.277 rows=15051 loops=1)
Index Cond: (chat_id = 'ad832a44-34ce-416b-a5f8-f8e528316fd6'::uuid)
Buffers: shared hit=313
Planning Time: 0.088 ms
Execution Time: 4.035 ms
(7 rows)
== 4. Для сравнения: keyset-запрос без индекса (DROP INDEX внутри транзакции, затем ROLLBACK)
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (actual time=6.187..6.193 rows=51 loops=1)
Buffers: shared hit=637
-> Sort (actual time=6.185..6.188 rows=51 loops=1)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 31kB
Buffers: shared hit=637
-> Seq Scan on messages (actual time=1.966..5.520 rows=5000 loops=1)
Filter: ((chat_id = 'ad832a44-34ce-416b-a5f8-f8e528316fd6'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 04:10:00+00'::timestamp with time zone, '1e3006cf-e446-47db-80e3-a34c031790a8'::uuid)))
Rows Removed by Filter: 45029
Buffers: shared hit=637
Planning:
Buffers: shared hit=5
Planning Time: 0.096 ms
Execution Time: 6.267 ms
(14 rows)
== 5. Список чатов пользователя (запрос API GET /chats, limit=50)
QUERY PLAN
-----------------------------------------------------------------------------------------------------
Limit (actual time=0.230..0.420 rows=31 loops=1)
Buffers: shared hit=33
-> Result (actual time=0.229..0.415 rows=31 loops=1)
Buffers: shared hit=33
-> Sort (actual time=0.145..0.148 rows=31 loops=1)
Sort Key: c.created_at, c.id
Sort Method: quicksort Memory: 27kB
Buffers: shared hit=2
-> Hash Join (actual time=0.088..0.097 rows=31 loops=1)
Hash Cond: (c.id = m.chat_id)
Buffers: shared hit=2
-> Seq Scan on chats c (actual time=0.005..0.008 rows=42 loops=1)
Buffers: shared hit=1
-> Hash (actual time=0.015..0.016 rows=31 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 10kB
Buffers: shared hit=1
-> Seq Scan on chat_members m (actual time=0.006..0.010 rows=31 loops=1)
Filter: (user_id = '264c4b72-36e7-41e2-895d-18a04eb55baf'::uuid)
Rows Removed by Filter: 19
Buffers: shared hit=1
SubPlan 1
-> Sort (actual time=0.006..0.006 rows=1 loops=31)
Sort Key: cm.user_id
Sort Method: quicksort Memory: 25kB
Buffers: shared hit=31
-> Seq Scan on chat_members cm (actual time=0.003..0.004 rows=1 loops=31)
Filter: (chat_id = c.id)
Rows Removed by Filter: 49
Buffers: shared hit=31
Planning:
Buffers: shared hit=51
Planning Time: 0.700 ms
Execution Time: 0.495 ms
(33 rows)