Исправление по замечанию: отзыв сессии выдерживает гонку с наполнением кэша

This commit is contained in:
alenchha
2026-10-23 23:15:21 +03:00
parent 1a60b9cd1f
commit 4ee3cbb4ea
16 changed files with 288 additions and 179 deletions
+35 -35
View File
@@ -7,7 +7,7 @@ valkey valkey/valkey:8.1-alpine@sha256:081c2f5cb575efc901aa80ff9cdbd1ec6a301682f
=== Засев 10000 сообщений с неравномерными чатами ===
seed_duration | seeded_rows | target_total | messages_total
-----------------+-------------+--------------+----------------
00:00:00.000975 | 0 | 10000 | 52887
00:00:00.001099 | 0 | 10000 | 53293
(1 row)
@@ -19,70 +19,70 @@ valkey valkey/valkey:8.1-alpine@sha256:081c2f5cb575efc901aa80ff9cdbd1ec6a301682f
messages_total | chats_with_messages | table_size | indexes_size
----------------+---------------------+------------+--------------
52887 | 14 | 5280 kB | 7024 kB
53293 | 14 | 5320 kB | 7152 kB
(1 row)
--- распределение истории по чатам: 5 самых больших и 5 самых маленьких ---
chat_id | rows
--------------------------------------+-------
174a0477-1fd5-47a9-ab90-ee8b7ef903af | 36305
fb87d20c-528a-4bb7-9cd5-e1d4486bdfca | 16537
3e751b33-08b7-4433-a2be-bb355af7bf62 | 25
11bd8f2b-db27-4c63-89f0-469ee53c6cad | 5
30e5554f-e2e0-411e-b5bd-ab86e1fd9c5f | 3
69ee7d45-68b4-4606-befe-efaeecdedd32 | 38293
e95dc135-101e-442f-baf7-b21f0ed7edaf | 14955
20948a76-f794-4765-9fcc-7a9ebccafd2e | 25
b2bd268f-f9b6-40fb-8039-1bde44c0f9ee | 5
f4d3517a-4afa-4461-a4fc-16c6302f3d7c | 3
(5 rows)
chat_id | rows
--------------------------------------+------
b490286c-39b8-4940-92cf-23817def915a | 1
6fc32369-98df-4387-b07f-00dfa6e0b391 | 1
f4c1fdcc-0ddd-4fb5-a962-c8901deac9e2 | 1
903d0035-1bda-4ac6-9fbf-52f28fe0b94c | 1
fb6df905-d5d2-4300-b74f-1f0196f9e2b0 | 1
6dbc6664-e048-4858-9ce5-f507afb9fd7c | 1
ac1c8e3d-4e29-44fc-b770-d3ef6f2351ae | 1
5a65b658-2555-4ac9-b10a-9a081f81f37b | 1
17c53c09-19a0-4c0c-87f8-2f8c66468119 | 1
451812c7-05dc-4b1d-9ddb-3679c4ccd15f | 1
(5 rows)
--- A. keyset, первая страница самого большого чата ---
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.41..5.46 rows=50 width=67) (actual time=0.005..0.011 rows=50 loops=1)
Buffers: shared hit=21
-> Index Scan using messages_history_idx on messages (cost=0.41..3662.26 rows=36288 width=67) (actual time=0.005..0.009 rows=50 loops=1)
Index Cond: (chat_id = '174a0477-1fd5-47a9-ab90-ee8b7ef903af'::uuid)
Buffers: shared hit=21
Limit (cost=0.41..5.43 rows=50 width=67) (actual time=0.005..0.013 rows=50 loops=1)
Buffers: shared hit=24
-> Index Scan using messages_history_idx on messages (cost=0.41..3813.50 rows=38014 width=67) (actual time=0.005..0.010 rows=50 loops=1)
Index Cond: (chat_id = '69ee7d45-68b4-4606-befe-efaeecdedd32'::uuid)
Buffers: shared hit=24
Planning:
Buffers: shared hit=8
Planning Time: 0.049 ms
Execution Time: 0.018 ms
Planning Time: 0.036 ms
Execution Time: 0.021 ms
(9 rows)
--- B. keyset, страница на глубине 90% истории (так работает cursor API) ---
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.41..8.17 rows=50 width=67) (actual time=0.006..0.014 rows=50 loops=1)
Buffers: shared hit=54
-> Index Scan using messages_history_idx on messages (cost=0.41..2148.72 rows=13857 width=67) (actual time=0.005..0.012 rows=50 loops=1)
Index Cond: ((chat_id = '174a0477-1fd5-47a9-ab90-ee8b7ef903af'::uuid) AND (ROW(created_at, id) > ROW('2026-10-23 14:37:21.481035+00'::timestamp with time zone, 'b34cffc1-608f-4281-89b6-d598a0000a8d'::uuid)))
Buffers: shared hit=54
Planning Time: 0.022 ms
Execution Time: 0.034 ms
Limit (cost=0.41..8.31 rows=50 width=67) (actual time=0.006..0.013 rows=50 loops=1)
Buffers: shared hit=53
-> Index Scan using messages_history_idx on messages (cost=0.41..2141.23 rows=13555 width=67) (actual time=0.006..0.011 rows=50 loops=1)
Index Cond: ((chat_id = '69ee7d45-68b4-4606-befe-efaeecdedd32'::uuid) AND (ROW(created_at, id) > ROW('2026-10-23 20:11:34.584521+00'::timestamp with time zone, 'aa6010bb-02c6-4d17-986b-0f5103547576'::uuid)))
Buffers: shared hit=53
Planning Time: 0.023 ms
Execution Time: 0.032 ms
(7 rows)
--- C. OFFSET на той же глубине (так API не делает: цена растёт со страницей) ---
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=3297.57..3302.61 rows=50 width=67) (actual time=4.132..4.139 rows=50 loops=1)
Buffers: shared hit=33082
-> Index Scan using messages_history_idx on messages (cost=0.41..3662.26 rows=36288 width=67) (actual time=0.012..3.519 rows=32724 loops=1)
Index Cond: (chat_id = '174a0477-1fd5-47a9-ab90-ee8b7ef903af'::uuid)
Buffers: shared hit=33082
Planning Time: 0.020 ms
Execution Time: 4.146 ms
Limit (cost=3457.31..3462.32 rows=50 width=67) (actual time=4.463..4.470 rows=50 loops=1)
Buffers: shared hit=34920
-> Index Scan using messages_history_idx on messages (cost=0.41..3813.50 rows=38014 width=67) (actual time=0.005..3.799 rows=34513 loops=1)
Index Cond: (chat_id = '69ee7d45-68b4-4606-befe-efaeecdedd32'::uuid)
Buffers: shared hit=34920
Planning Time: 0.016 ms
Execution Time: 4.476 ms
(7 rows)
--- индексы таблицы messages ---
index | size | scans
----------------------+---------+-------
messages_history_idx | 4720 kB | 12
messages_pkey | 2304 kB | 0
messages_history_idx | 4832 kB | 12
messages_pkey | 2320 kB | 0
(2 rows)