# scripts/explain.sh; date: 2026-10-29T22:07:31Z
 messages_total | messages_in_big_chat 
----------------+----------------------
          60328 |                20000
(1 row)

=== 1. First page (limit=50 -> LIMIT 51), index messages_chat_created_id_idx
                                                                       QUERY PLAN                                                                        
---------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.41..8.31 rows=51 width=78) (actual time=0.009..0.014 rows=51.00 loops=1)
   Buffers: shared hit=4
   ->  Index Scan using messages_chat_created_id_idx on messages  (cost=0.41..3088.25 rows=19942 width=78) (actual time=0.008..0.012 rows=51.00 loops=1)
         Index Cond: (chat_id = '00000000-0000-4000-8000-00000000e001'::uuid)
         Index Searches: 1
         Buffers: shared hit=4
 Planning:
   Buffers: shared hit=10
 Planning Time: 0.077 ms
 Execution Time: 0.024 ms
(10 rows)

=== 2. Page in the middle by keyset cursor (after 10 000 messages)
                                                                                                    QUERY PLAN                                                                                                    
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.41..27.77 rows=51 width=78) (actual time=0.008..0.014 rows=51.00 loops=1)
   Buffers: shared hit=5
   ->  Index Scan using messages_chat_created_id_idx on messages  (cost=0.41..1834.26 rows=3419 width=78) (actual time=0.008..0.012 rows=51.00 loops=1)
         Index Cond: ((chat_id = '00000000-0000-4000-8000-00000000e001'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 02:46:40+00'::timestamp with time zone, 'ea1e0a08-377a-4911-aa17-221192625ad3'::uuid)))
         Index Searches: 1
         Buffers: shared hit=5
 Planning Time: 0.035 ms
 Execution Time: 0.042 ms
(8 rows)

=== 3. Same middle page WITHOUT the index (dropped inside a rolled-back transaction)
                                                                                                     QUERY PLAN                                                                                                     
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=1988.81..1988.93 rows=51 width=78) (actual time=3.462..3.467 rows=51.00 loops=1)
   Buffers: shared hit=819
   ->  Sort  (cost=1988.81..1997.35 rows=3419 width=78) (actual time=3.462..3.464 rows=51.00 loops=1)
         Sort Key: created_at, id
         Sort Method: top-N heapsort  Memory: 31kB
         Buffers: shared hit=819
         ->  Seq Scan on messages  (cost=0.00..1874.74 rows=3419 width=78) (actual time=0.498..2.888 rows=10000.00 loops=1)
               Filter: ((chat_id = '00000000-0000-4000-8000-00000000e001'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 02:46:40+00'::timestamp with time zone, 'ea1e0a08-377a-4911-aa17-221192625ad3'::uuid)))
               Rows Removed by Filter: 50328
               Buffers: shared hit=819
 Planning:
   Buffers: shared hit=4
 Planning Time: 0.052 ms
 Execution Time: 3.492 ms
(14 rows)

