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


== 1. First page: WHERE chat_id = ? ORDER BY created_at, id LIMIT 51
                                                    QUERY PLAN                                                    
------------------------------------------------------------------------------------------------------------------
 Limit (actual time=0.005..0.022 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.020 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.045 ms
 Execution Time: 0.029 ms
(9 rows)


== 2. Deep page after the 15 000th message: (created_at, id) > (?, ?)
                                                                                                    QUERY PLAN                                                                                                    
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit (actual time=0.005..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.007 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, '5ec4eb0a-04cf-487e-9ec0-16b7367e29df'::uuid)))
         Buffers: shared hit=4
 Planning Time: 0.027 ms
 Execution Time: 0.025 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.076..1.080 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.800 rows=15051 loops=1)
         Index Cond: (chat_id = '00000000-0000-0000-0001-000000000000'::uuid)
         Buffers: shared hit=296
 Planning Time: 0.025 ms
 Execution Time: 1.093 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.151..2.154 rows=51 loops=1)
   Buffers: shared hit=914
   ->  Sort (actual time=2.151..2.152 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.420..1.969 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, '5ec4eb0a-04cf-487e-9ec0-16b7367e29df'::uuid)))
               Rows Removed by Filter: 65006
               Buffers: shared hit=914
 Planning Time: 0.021 ms
 Execution Time: 2.162 ms
(12 rows)

