=== Data volume
 messages_total | messages_in_big_chat 
----------------+----------------------
          20250 |                15000


=== History index
                                        indexdef                                        
----------------------------------------------------------------------------------------
 CREATE INDEX messages_history ON public.messages USING btree (chat_id, created_at, id)


=== 1. First page: limit=50, the app asks for 51 rows to know if there is a next page
                                           QUERY PLAN                                           
------------------------------------------------------------------------------------------------
 Limit (actual time=0.005..0.011 rows=51 loops=1)
   Buffers: shared hit=4
   ->  Index Scan using messages_history on messages (actual time=0.004..0.008 rows=51 loops=1)
         Index Cond: (chat_id = 'edccb516-a15c-427e-a8d3-9b1b2d9ea77c'::uuid)
         Buffers: shared hit=4
 Planning:
   Buffers: shared hit=6
 Planning Time: 0.055 ms
 Execution Time: 0.020 ms


=== 2. Page after the 10000th message by keyset cursor (this is what the app does)
                                                                                                    QUERY PLAN                                                                                                    
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit (actual time=0.006..0.011 rows=51 loops=1)
   Buffers: shared hit=4
   ->  Index Scan using messages_history on messages (actual time=0.006..0.008 rows=51 loops=1)
         Index Cond: ((chat_id = 'edccb516-a15c-427e-a8d3-9b1b2d9ea77c'::uuid) AND (ROW(created_at, id) > ROW('2026-09-01 02:46:40+00'::timestamp with time zone, '3b67880a-c02c-48ac-aec2-1a0331e99fee'::uuid)))
         Buffers: shared hit=4
 Planning Time: 0.038 ms
 Execution Time: 0.028 ms


=== 3. The same page with OFFSET 10000 (the app does NOT do this)
                                            QUERY PLAN                                             
---------------------------------------------------------------------------------------------------
 Limit (actual time=0.747..0.752 rows=51 loops=1)
   Buffers: shared hit=199
   ->  Index Scan using messages_history on messages (actual time=0.006..0.566 rows=10051 loops=1)
         Index Cond: (chat_id = 'edccb516-a15c-427e-a8d3-9b1b2d9ea77c'::uuid)
         Buffers: shared hit=199
 Planning Time: 0.033 ms
 Execution Time: 0.765 ms


=== 4. Query 2 without the index (index is dropped only inside this transaction)
                                                                                                     QUERY PLAN                                                                                                     
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit (actual time=0.895..0.898 rows=51 loops=1)
   Buffers: shared hit=251
   ->  Sort (actual time=0.895..0.896 rows=51 loops=1)
         Sort Key: created_at, id
         Sort Method: top-N heapsort  Memory: 31kB
         Buffers: shared hit=251
         ->  Seq Scan on messages (actual time=0.320..0.719 rows=5000 loops=1)
               Filter: ((chat_id = 'edccb516-a15c-427e-a8d3-9b1b2d9ea77c'::uuid) AND (ROW(created_at, id) > ROW('2026-09-01 02:46:40+00'::timestamp with time zone, '3b67880a-c02c-48ac-aec2-1a0331e99fee'::uuid)))
               Rows Removed by Filter: 15250
               Buffers: shared hit=251
 Planning:
   Buffers: shared hit=4
 Planning Time: 0.043 ms
 Execution Time: 0.912 ms


=== ROLLBACK: test data removed, index is back
 explain_users_left 
--------------------
                  0

