# EXPLAIN (ANALYZE, BUFFERS) истории: 10 000 сообщений в чате, 50 000 всего (транзакция с ROLLBACK) # date: 2026-10-21T12:20:13Z # commit: e20a5a4 Pager usage is off. BEGIN INSERT 0 1 INSERT 0 1 INSERT 0 20 INSERT 0 21 INSERT 0 50000 ANALYZE target_chat_messages | total_messages ----------------------+---------------- 10000 | 50021 (1 row) === 1. Keyset, first page (index messages_chat_id_created_at_id_idx) QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=0.41..18.24 rows=51 width=78) (actual time=0.013..0.020 rows=51 loops=1) Buffers: shared hit=4 -> Index Scan using messages_chat_id_created_at_id_idx on messages (cost=0.41..3449.17 rows=9866 width=78) (actual time=0.012..0.016 rows=51 loops=1) Index Cond: (chat_id = '00000000-0000-4000-8000-0000000000c0'::uuid) Buffers: shared hit=4 Planning: Buffers: shared hit=6 Planning Time: 0.163 ms Execution Time: 0.045 ms (9 rows) === 2. Keyset, page after message #9000 QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Limit (cost=0.41..162.43 rows=51 width=78) (actual time=0.013..0.023 rows=51 loops=1) Buffers: shared hit=5 -> Index Scan using messages_chat_id_created_at_id_idx on messages (cost=0.41..626.22 rows=197 width=78) (actual time=0.012..0.019 rows=51 loops=1) Index Cond: ((chat_id = '00000000-0000-4000-8000-0000000000c0'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:09+00'::timestamp with time zone, 'cdb7ecf8-7fe4-4e7a-b5d8-93c918ad26d7'::uuid))) Buffers: shared hit=5 Planning Time: 0.226 ms Execution Time: 0.083 ms (7 rows) === 3. For comparison: OFFSET 9000 reads and discards all previous rows QUERY PLAN ------------------------------------------------------------------------------------------------------------------------- Limit (cost=1969.29..1969.41 rows=51 width=78) (actual time=5.473..5.479 rows=51 loops=1) Buffers: shared hit=667 -> Sort (cost=1946.79..1971.45 rows=9866 width=78) (actual time=4.909..5.222 rows=9051 loops=1) Sort Key: created_at, id Sort Method: quicksort Memory: 1713kB Buffers: shared hit=667 -> Seq Scan on messages (cost=0.00..1292.26 rows=9866 width=78) (actual time=0.044..3.543 rows=10000 loops=1) Filter: (chat_id = '00000000-0000-4000-8000-0000000000c0'::uuid) Rows Removed by Filter: 40021 Buffers: shared hit=667 Planning Time: 0.042 ms Execution Time: 5.598 ms (12 rows) === 4. For comparison: keyset page #9000 without the index DROP INDEX QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=1548.94..1549.07 rows=51 width=78) (actual time=3.516..3.522 rows=51 loops=1) Buffers: shared hit=667 -> Sort (cost=1548.94..1549.43 rows=197 width=78) (actual time=3.515..3.517 rows=51 loops=1) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 31kB Buffers: shared hit=667 -> Seq Scan on messages (cost=0.00..1542.37 rows=197 width=78) (actual time=0.821..3.429 rows=1000 loops=1) Filter: ((chat_id = '00000000-0000-4000-8000-0000000000c0'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:09+00'::timestamp with time zone, 'cdb7ecf8-7fe4-4e7a-b5d8-93c918ad26d7'::uuid))) Rows Removed by Filter: 49021 Buffers: shared hit=667 Planning: Buffers: shared hit=4 Planning Time: 0.097 ms Execution Time: 3.552 ms (14 rows) ROLLBACK