# EXPLAIN evidence for message history pagination
# date: 2026-10-07T14:17:55Z  commit: 65003b6
# postgres: 17.6
# messages total: 70112, in measured chat: 20000

PREPARE
PREPARE
=== 1. WITH index messages_chat_history_idx: first page (limit 50 + 1) ===
                                                                    QUERY PLAN                                                                     
---------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.42..8.60 rows=51 width=78) (actual time=0.014..0.019 rows=51 loops=1)
   Buffers: shared hit=4
   ->  Index Scan using messages_chat_history_idx on messages  (cost=0.42..3227.11 rows=20122 width=78) (actual time=0.013..0.015 rows=51 loops=1)
         Index Cond: (chat_id = '00000000-0000-4000-8000-000000000100'::uuid)
         Buffers: shared hit=4
 Planning:
   Buffers: shared hit=106
 Planning Time: 0.272 ms
 Execution Time: 0.031 ms
(9 rows)

=== 2. WITH index: deep page, cursor after message #15000 of 20000 ===
                                                                                                    QUERY PLAN                                                                                                    
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.42..55.74 rows=51 width=78) (actual time=0.012..0.017 rows=51 loops=1)
   Buffers: shared hit=4
   ->  Index Scan using messages_chat_history_idx on messages  (cost=0.42..1590.70 rows=1466 width=78) (actual time=0.012..0.014 rows=51 loops=1)
         Index Cond: ((chat_id = '00000000-0000-4000-8000-000000000100'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 04:10:00+00'::timestamp with time zone, '47ba2a9c-d238-4807-2008-6ea72cb0f95c'::uuid)))
         Buffers: shared hit=4
 Planning:
   Buffers: shared hit=42
 Planning Time: 0.276 ms
 Execution Time: 0.035 ms
(9 rows)

BEGIN
DROP INDEX
=== 3. WITHOUT index: first page ===
                                                        QUERY PLAN                                                        
--------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=2487.71..2487.84 rows=51 width=78) (actual time=4.235..4.239 rows=51 loops=1)
   Buffers: shared hit=946
   ->  Sort  (cost=2487.71..2538.02 rows=20122 width=78) (actual time=4.234..4.236 rows=51 loops=1)
         Sort Key: created_at, id
         Sort Method: top-N heapsort  Memory: 31kB
         Buffers: shared hit=946
         ->  Seq Scan on messages  (cost=0.00..1816.40 rows=20122 width=78) (actual time=0.011..3.325 rows=20000 loops=1)
               Filter: (chat_id = '00000000-0000-4000-8000-000000000100'::uuid)
               Rows Removed by Filter: 50112
               Buffers: shared hit=940
 Planning:
   Buffers: shared hit=16
 Planning Time: 0.098 ms
 Execution Time: 4.249 ms
(14 rows)

=== 4. WITHOUT index: deep page ===
                                                                                                     QUERY PLAN                                                                                                     
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=2215.87..2216.00 rows=51 width=78) (actual time=3.117..3.121 rows=51 loops=1)
   Buffers: shared hit=940
   ->  Sort  (cost=2215.87..2219.53 rows=1466 width=78) (actual time=3.116..3.118 rows=51 loops=1)
         Sort Key: created_at, id
         Sort Method: top-N heapsort  Memory: 31kB
         Buffers: shared hit=940
         ->  Seq Scan on messages  (cost=0.00..2166.96 rows=1466 width=78) (actual time=0.605..2.853 rows=5000 loops=1)
               Filter: ((chat_id = '00000000-0000-4000-8000-000000000100'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 04:10:00+00'::timestamp with time zone, '47ba2a9c-d238-4807-2008-6ea72cb0f95c'::uuid)))
               Rows Removed by Filter: 65112
               Buffers: shared hit=940
 Planning Time: 0.069 ms
 Execution Time: 3.133 ms
(12 rows)

ROLLBACK
