=== Data volume
 messages_total | messages_in_big_chat 
----------------+----------------------
          68448 |                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.007..0.019 rows=51 loops=1)
   Buffers: shared hit=54
   ->  Index Scan using messages_history on messages (actual time=0.007..0.016 rows=51 loops=1)
         Index Cond: (chat_id = '2ce6ad8d-9425-42cb-aa5f-94f2857a7f39'::uuid)
         Buffers: shared hit=54
 Planning:
   Buffers: shared hit=8
 Planning Time: 0.067 ms
 Execution Time: 0.026 ms


=== 2. Page after the 10000th message by keyset cursor (this is what the app does)
                                                                                                    QUERY PLAN                                                                                                    
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit (actual time=0.007..0.012 rows=51 loops=1)
   Buffers: shared hit=5
   ->  Index Scan using messages_history on messages (actual time=0.006..0.009 rows=51 loops=1)
         Index Cond: ((chat_id = '2ce6ad8d-9425-42cb-aa5f-94f2857a7f39'::uuid) AND (ROW(created_at, id) > ROW('2026-09-01 02:46:40+00'::timestamp with time zone, '09b0c717-4cc7-469c-afb2-fc088519bea2'::uuid)))
         Buffers: shared hit=5
 Planning Time: 0.083 ms
 Execution Time: 0.047 ms


=== 2b. The same page exactly as the app sends it since lab4: with attachment_ids of every message
                                                                                                    QUERY PLAN                                                                                                    
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit (actual time=0.024..0.176 rows=51 loops=1)
   Buffers: shared hit=59
   ->  Index Scan using messages_history on messages m (actual time=0.024..0.173 rows=51 loops=1)
         Index Cond: ((chat_id = '2ce6ad8d-9425-42cb-aa5f-94f2857a7f39'::uuid) AND (ROW(created_at, id) > ROW('2026-09-01 02:46:40+00'::timestamp with time zone, '09b0c717-4cc7-469c-afb2-fc088519bea2'::uuid)))
         Buffers: shared hit=59
         SubPlan 1
           ->  Sort (actual time=0.002..0.002 rows=0 loops=51)
                 Sort Key: a."position"
                 Sort Method: quicksort  Memory: 25kB
                 Buffers: shared hit=54
                 ->  Bitmap Heap Scan on message_attachments a (actual time=0.000..0.000 rows=0 loops=51)
                       Recheck Cond: (message_id = m.id)
                       Buffers: shared hit=51
                       ->  Bitmap Index Scan on message_attachments_message_id_attachment_id_key (actual time=0.000..0.000 rows=0 loops=51)
                             Index Cond: (message_id = m.id)
                             Buffers: shared hit=51
 Planning:
   Buffers: shared hit=63
 Planning Time: 0.210 ms
 Execution Time: 0.193 ms


=== 3. The same page with OFFSET 10000 (the app does NOT do this)
                                   QUERY PLAN                                   
--------------------------------------------------------------------------------
 Limit (actual time=4.087..4.090 rows=51 loops=1)
   Buffers: shared hit=1255
   ->  Sort (actual time=3.670..3.904 rows=10051 loops=1)
         Sort Key: created_at, id
         Sort Method: quicksort  Memory: 2377kB
         Buffers: shared hit=1255
         ->  Seq Scan on messages (actual time=0.006..2.281 rows=15000 loops=1)
               Filter: (chat_id = '2ce6ad8d-9425-42cb-aa5f-94f2857a7f39'::uuid)
               Rows Removed by Filter: 53448
               Buffers: shared hit=1255
 Planning Time: 0.041 ms
 Execution Time: 4.136 ms


=== 4. Query 2 without the index (index is dropped only inside this transaction)
                                                                     QUERY PLAN                                                                      
-----------------------------------------------------------------------------------------------------------------------------------------------------
 Limit (actual time=1.375..1.378 rows=51 loops=1)
   Buffers: shared hit=1006
   ->  Sort (actual time=1.374..1.376 rows=51 loops=1)
         Sort Key: created_at, id
         Sort Method: top-N heapsort  Memory: 31kB
         Buffers: shared hit=1006
         ->  Bitmap Heap Scan on messages (actual time=0.726..1.001 rows=5000 loops=1)
               Recheck Cond: (chat_id = '2ce6ad8d-9425-42cb-aa5f-94f2857a7f39'::uuid)
               Filter: (ROW(created_at, id) > ROW('2026-09-01 02:46:40+00'::timestamp with time zone, '09b0c717-4cc7-469c-afb2-fc088519bea2'::uuid))
               Rows Removed by Filter: 10000
               Heap Blocks: exact=991
               Buffers: shared hit=1006
               ->  Bitmap Index Scan on messages_idempotency_key (actual time=0.217..0.217 rows=15000 loops=1)
                     Index Cond: (chat_id = '2ce6ad8d-9425-42cb-aa5f-94f2857a7f39'::uuid)
                     Buffers: shared hit=15
 Planning:
   Buffers: shared hit=4
 Planning Time: 0.057 ms
 Execution Time: 1.395 ms


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

