# EXPLAIN (ANALYZE, BUFFERS) истории: 10 000 сообщений в чате, 50 000 всего (транзакция с ROLLBACK)
# date: 2026-10-21T08:44:31Z
# commit: 1d5c1e0 (dirty)

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 |          50030
(1 row)

=== 1. Keyset, first page (index messages_chat_id_created_at_id_idx)
                                                                         QUERY PLAN                                                                         
------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.41..18.25 rows=51 width=78) (actual time=0.023..0.042 rows=51 loops=1)
   Buffers: shared hit=5
   ->  Index Scan using messages_chat_id_created_at_id_idx on messages  (cost=0.41..3501.16 rows=10008 width=78) (actual time=0.022..0.034 rows=51 loops=1)
         Index Cond: (chat_id = '00000000-0000-4000-8000-0000000000c0'::uuid)
         Buffers: shared hit=5
 Planning:
   Buffers: shared hit=6
 Planning Time: 0.238 ms
 Execution Time: 0.081 ms
(9 rows)

=== 2. Keyset, page after message #9000
                                                                                                    QUERY PLAN                                                                                                    
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.41..161.20 rows=51 width=78) (actual time=0.032..0.058 rows=51 loops=1)
   Buffers: shared hit=6
   ->  Index Scan using messages_chat_id_created_at_id_idx on messages  (cost=0.41..646.73 rows=205 width=78) (actual time=0.030..0.048 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, 'db5c59fc-902f-4cab-9372-0af09bfd735f'::uuid)))
         Buffers: shared hit=6
 Planning Time: 0.222 ms
 Execution Time: 0.165 ms
(7 rows)

=== 3. For comparison: OFFSET 9000 reads and discards all previous rows
                                                        QUERY PLAN                                                        
--------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=1980.85..1980.98 rows=51 width=78) (actual time=14.482..14.494 rows=51 loops=1)
   Buffers: shared hit=668
   ->  Sort  (cost=1958.35..1983.37 rows=10008 width=78) (actual time=13.126..13.872 rows=9051 loops=1)
         Sort Key: created_at, id
         Sort Method: quicksort  Memory: 1713kB
         Buffers: shared hit=668
         ->  Seq Scan on messages  (cost=0.00..1293.38 rows=10008 width=78) (actual time=0.020..9.312 rows=10000 loops=1)
               Filter: (chat_id = '00000000-0000-4000-8000-0000000000c0'::uuid)
               Rows Removed by Filter: 40030
               Buffers: shared hit=668
 Planning Time: 0.151 ms
 Execution Time: 14.824 ms
(12 rows)

=== 4. For comparison: keyset page #9000 without the index
DROP INDEX
                                                                                                     QUERY PLAN                                                                                                     
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=1550.36..1550.49 rows=51 width=78) (actual time=8.143..8.154 rows=51 loops=1)
   Buffers: shared hit=668
   ->  Sort  (cost=1550.36..1550.88 rows=205 width=78) (actual time=8.141..8.145 rows=51 loops=1)
         Sort Key: created_at, id
         Sort Method: top-N heapsort  Memory: 31kB
         Buffers: shared hit=668
         ->  Seq Scan on messages  (cost=0.00..1543.53 rows=205 width=78) (actual time=2.383..7.810 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, 'db5c59fc-902f-4cab-9372-0af09bfd735f'::uuid)))
               Rows Removed by Filter: 49030
               Buffers: shared hit=668
 Planning:
   Buffers: shared hit=4
 Planning Time: 0.280 ms
 Execution Time: 8.207 ms
(14 rows)

ROLLBACK
