
== Data volume
 total_messages | target_chat_messages 
----------------+----------------------
          70003 |                20000
(1 row)


== 1. First page: WHERE chat_id = ? ORDER BY created_at, id LIMIT 51
                                                    QUERY PLAN                                                    
------------------------------------------------------------------------------------------------------------------
 Limit (actual time=0.007..0.012 rows=51 loops=1)
   Buffers: shared hit=4
   ->  Index Scan using messages_chat_id_created_at_id_idx on messages (actual time=0.006..0.009 rows=51 loops=1)
         Index Cond: (chat_id = '00000000-0000-0000-0001-000000000000'::uuid)
         Buffers: shared hit=4
 Planning:
   Buffers: shared hit=6
 Planning Time: 0.036 ms
 Execution Time: 0.022 ms
(9 rows)


== 2. Deep page after the 15 000th message: (created_at, id) > (?, ?)
                                                                                                    QUERY PLAN                                                                                                    
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit (actual time=0.006..0.010 rows=51 loops=1)
   Buffers: shared hit=4
   ->  Index Scan using messages_chat_id_created_at_id_idx on messages (actual time=0.005..0.008 rows=51 loops=1)
         Index Cond: ((chat_id = '00000000-0000-0000-0001-000000000000'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 04:10:00+00'::timestamp with time zone, 'd5bb7c5e-b8e9-43e6-864a-c12a22b3e27d'::uuid)))
         Buffers: shared hit=4
 Planning Time: 0.023 ms
 Execution Time: 0.029 ms
(7 rows)


== 3. Same deep page with OFFSET instead of a cursor (for comparison, not used by the API)
                                                     QUERY PLAN                                                      
---------------------------------------------------------------------------------------------------------------------
 Limit (actual time=1.024..1.028 rows=51 loops=1)
   Buffers: shared hit=296
   ->  Index Scan using messages_chat_id_created_at_id_idx on messages (actual time=0.007..0.768 rows=15051 loops=1)
         Index Cond: (chat_id = '00000000-0000-0000-0001-000000000000'::uuid)
         Buffers: shared hit=296
 Planning Time: 0.021 ms
 Execution Time: 1.036 ms
(7 rows)


== 4. Deep page without messages_chat_id_created_at_id_idx (index scans disabled for this query only)
                                                                                                     QUERY PLAN                                                                                                     
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit (actual time=2.097..2.101 rows=51 loops=1)
   Buffers: shared hit=914
   ->  Sort (actual time=2.096..2.098 rows=51 loops=1)
         Sort Key: created_at, id
         Sort Method: top-N heapsort  Memory: 31kB
         Buffers: shared hit=914
         ->  Seq Scan on messages (actual time=0.423..1.891 rows=5000 loops=1)
               Filter: ((chat_id = '00000000-0000-0000-0001-000000000000'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 04:10:00+00'::timestamp with time zone, 'd5bb7c5e-b8e9-43e6-864a-c12a22b3e27d'::uuid)))
               Rows Removed by Filter: 65003
               Buffers: shared hit=914
 Planning Time: 0.020 ms
 Execution Time: 2.108 ms
(12 rows)

