== Объём данных
 messages_total | messages_in_big_chat | messages_heap | history_index 
----------------+----------------------+---------------+---------------
         138769 |                20000 | 14 MB         | 8000 kB
(1 row)


== 1. Первая страница истории (limit 50 + 1), как list_for_chat
                                                     QUERY PLAN                                                      
---------------------------------------------------------------------------------------------------------------------
 Limit (actual time=0.008..0.012 rows=51.00 loops=1)
   Buffers: shared hit=4
   ->  Index Scan using messages_chat_id_created_at_id_idx on messages (actual time=0.007..0.009 rows=51.00 loops=1)
         Index Cond: (chat_id = '01a10705-59d7-70b2-aa32-64965b148b34'::uuid)
         Index Searches: 1
         Buffers: shared hit=4
 Planning:
   Buffers: shared hit=6
 Planning Time: 0.092 ms
 Execution Time: 0.026 ms
(10 rows)


== 2. Страница из середины по cursor (после 10 000-го), как list_for_chat_after
                                                                                                    QUERY PLAN                                                                                                    
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit (actual time=0.007..0.012 rows=51.00 loops=1)
   Buffers: shared hit=5
   ->  Index Scan using messages_chat_id_created_at_id_idx on messages (actual time=0.007..0.010 rows=51.00 loops=1)
         Index Cond: ((chat_id = '01a10705-59d7-70b2-aa32-64965b148b34'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 02:46:40+00'::timestamp with time zone, '01a10705-59eb-73d8-aa15-9a1a496ee297'::uuid)))
         Index Searches: 1
         Buffers: shared hit=5
 Planning Time: 0.044 ms
 Execution Time: 0.036 ms
(8 rows)


== 3. Тот же запрос как generic plan prepared statement (так его исполняет asyncpg)
                                                     QUERY PLAN                                                      
---------------------------------------------------------------------------------------------------------------------
 Limit (actual time=0.008..0.012 rows=51.00 loops=1)
   Buffers: shared hit=5
   ->  Index Scan using messages_chat_id_created_at_id_idx on messages (actual time=0.007..0.009 rows=51.00 loops=1)
         Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
         Index Searches: 1
         Buffers: shared hit=5
 Planning Time: 0.041 ms
 Execution Time: 0.022 ms
(8 rows)


== 4. MAX(created_at) при вставке сообщения (монотонное время чата)
                                                                QUERY PLAN                                                                
------------------------------------------------------------------------------------------------------------------------------------------
 Result (actual time=0.008..0.008 rows=1.00 loops=1)
   Buffers: shared hit=4
   InitPlan 1
     ->  Limit (actual time=0.007..0.007 rows=1.00 loops=1)
           Buffers: shared hit=4
           ->  Index Only Scan Backward using messages_chat_id_created_at_id_idx on messages (actual time=0.007..0.007 rows=1.00 loops=1)
                 Index Cond: (chat_id = '01a10705-59d7-70b2-aa32-64965b148b34'::uuid)
                 Heap Fetches: 0
                 Index Searches: 1
                 Buffers: shared hit=4
 Planning:
   Buffers: shared hit=3
 Planning Time: 0.041 ms
 Execution Time: 0.015 ms
(14 rows)


== 5. Для сравнения: тот же запрос из середины БЕЗ индекса истории
   (DROP INDEX внутри транзакции с ROLLBACK — индекс не теряется)
                                                                                                        QUERY PLAN                                                                                                        
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit (actual time=6.360..7.265 rows=51.00 loops=1)
   Buffers: shared hit=1792
   ->  Gather Merge (actual time=6.360..7.262 rows=51.00 loops=1)
         Workers Planned: 1
         Workers Launched: 1
         Buffers: shared hit=1792
         ->  Sort (actual time=2.069..2.073 rows=25.50 loops=2)
               Sort Key: created_at, id
               Sort Method: top-N heapsort  Memory: 31kB
               Buffers: shared hit=1792
               Worker 0:  Sort Method: quicksort  Memory: 25kB
               ->  Parallel Seq Scan on messages (actual time=0.183..1.840 rows=5000.00 loops=2)
                     Filter: ((chat_id = '01a10705-59d7-70b2-aa32-64965b148b34'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 02:46:40+00'::timestamp with time zone, '01a10705-59eb-73d8-aa15-9a1a496ee297'::uuid)))
                     Rows Removed by Filter: 64384
                     Buffers: shared hit=1777
 Planning:
   Buffers: shared hit=4
 Planning Time: 0.045 ms
 Execution Time: 7.283 ms
(19 rows)

