date: 2026-10-28T17:16:18Z commit: 6468ee2 $ make explain # scripts/sql/explain-history.sql chat_messages --------------- 20000 (1 row) all_messages -------------- 30005 (1 row) history_index_size -------------------- 1736 kB (1 row) == Первая страница, limit=50 (запрос HISTORY_FIRST приложения, LIMIT limit+1) QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=0.29..5.37 rows=51 width=102) (actual time=0.008..0.013 rows=51 loops=1) Buffers: shared hit=3 -> Index Scan using messages_history_idx on messages (cost=0.29..1994.42 rows=20001 width=102) (actual time=0.007..0.010 rows=51 loops=1) Index Cond: (chat_id = '00000000-0000-7000-8000-00000000c001'::uuid) Buffers: shared hit=3 Planning: Buffers: shared hit=24 Planning Time: 0.081 ms Execution Time: 0.022 ms (9 rows) == Глубокая страница: keyset после 10 000-го сообщения (запрос HISTORY_AFTER приложения) QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Limit (cost=0.29..9.99 rows=51 width=102) (actual time=0.007..0.012 rows=51 loops=1) Buffers: shared hit=4 -> Index Scan using messages_history_idx on messages (cost=0.29..1269.18 rows=6670 width=102) (actual time=0.007..0.010 rows=51 loops=1) Index Cond: ((chat_id = '00000000-0000-7000-8000-00000000c001'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 02:46:40+00'::timestamp with time zone, '00000000-0000-7000-8001-000000002710'::uuid))) Buffers: shared hit=4 Planning Time: 0.034 ms Execution Time: 0.032 ms (7 rows) == Та же глубокая страница через OFFSET 10000 (не используется приложением, для сравнения) QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=997.30..1002.39 rows=51 width=102) (actual time=0.879..0.885 rows=51 loops=1) Buffers: shared hit=247 -> Index Scan using messages_history_idx on messages (cost=0.29..1994.42 rows=20001 width=102) (actual time=0.010..0.674 rows=10051 loops=1) Index Cond: (chat_id = '00000000-0000-7000-8000-00000000c001'::uuid) Buffers: shared hit=247 Planning Time: 0.046 ms Execution Time: 0.897 ms (7 rows) == Проверка: keyset и OFFSET возвращают одну и ту же страницу same_page ----------- t (1 row) == Контрольный план без индекса истории (DROP INDEX внутри транзакции, затем ROLLBACK) QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=1246.61..1246.74 rows=51 width=102) (actual time=1.473..1.478 rows=51 loops=1) Buffers: shared hit=505 -> Sort (cost=1246.61..1263.29 rows=6670 width=102) (actual time=1.473..1.473 rows=51 loops=1) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 31kB Buffers: shared hit=505 -> Seq Scan on messages (cost=0.00..1024.09 rows=6670 width=102) (actual time=0.291..1.073 rows=10000 loops=1) Filter: ((chat_id = '00000000-0000-7000-8000-00000000c001'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 02:46:40+00'::timestamp with time zone, '00000000-0000-7000-8001-000000002710'::uuid))) Rows Removed by Filter: 20005 Buffers: shared hit=499 Planning: Buffers: shared hit=14 Planning Time: 0.084 ms Execution Time: 1.486 ms (14 rows)