docker compose exec -T postgres psql -X -q -U messenger -d messenger -v ON_ERROR_STOP=1 < infra/sql/explain_history.sql
=== Объём данных
 messages_total | messages_in_big_chat 
----------------+----------------------
          21614 |                12000


=== Индекс истории (migrations/001_core.sql)
                                            indexdef                                             
-------------------------------------------------------------------------------------------------
 CREATE INDEX messages_chat_history_idx ON public.messages USING btree (chat_id, created_at, id)


=== 1. Первая страница (limit=50, приложение просит 51 строку, чтобы понять, есть ли следующая)
                                               QUERY PLAN                                                
---------------------------------------------------------------------------------------------------------
 Limit (actual time=0.013..0.026 rows=51 loops=1)
   Buffers: shared hit=4
   ->  Index Scan using messages_chat_history_idx on messages (actual time=0.012..0.022 rows=51 loops=1)
         Index Cond: (chat_id = 'a94bac8d-03ea-4bc6-a6f6-54c7fa77fe8f'::uuid)
         Buffers: shared hit=4
 Planning:
   Buffers: shared hit=6
 Planning Time: 0.175 ms
 Execution Time: 0.041 ms


=== 2. Страница из середины истории по keyset-курсору (после 6000-го сообщения)
                                                                                                    QUERY PLAN                                                                                                    
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit (actual time=0.018..0.033 rows=51 loops=1)
   Buffers: shared hit=4
   ->  Index Scan using messages_chat_history_idx on messages (actual time=0.016..0.028 rows=51 loops=1)
         Index Cond: ((chat_id = 'a94bac8d-03ea-4bc6-a6f6-54c7fa77fe8f'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 01:40:00+00'::timestamp with time zone, '42ee0870-5605-4719-8545-f4a6a0c5fa07'::uuid)))
         Buffers: shared hit=4
 Planning Time: 0.169 ms
 Execution Time: 0.078 ms


=== 3. Для сравнения: та же страница через OFFSET (так приложение НЕ делает)
                                                QUERY PLAN                                                 
-----------------------------------------------------------------------------------------------------------
 Limit (actual time=1.277..1.287 rows=51 loops=1)
   Buffers: shared hit=161
   ->  Index Scan using messages_chat_history_idx on messages (actual time=0.075..1.066 rows=6051 loops=1)
         Index Cond: (chat_id = 'a94bac8d-03ea-4bc6-a6f6-54c7fa77fe8f'::uuid)
         Buffers: shared hit=161
 Planning Time: 0.165 ms
 Execution Time: 1.321 ms


=== 4. Для сравнения: запрос 2 без индекса (индекс удалён только внутри этой транзакции)
                                                                                                     QUERY PLAN                                                                                                     
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit (actual time=2.335..2.341 rows=51 loops=1)
   Buffers: shared hit=291
   ->  Sort (actual time=2.334..2.337 rows=51 loops=1)
         Sort Key: created_at, id
         Sort Method: top-N heapsort  Memory: 31kB
         Buffers: shared hit=291
         ->  Seq Scan on messages (actual time=0.559..1.828 rows=6000 loops=1)
               Filter: ((chat_id = 'a94bac8d-03ea-4bc6-a6f6-54c7fa77fe8f'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 01:40:00+00'::timestamp with time zone, '42ee0870-5605-4719-8545-f4a6a0c5fa07'::uuid)))
               Rows Removed by Filter: 15614
               Buffers: shared hit=291
 Planning:
   Buffers: shared hit=4
 Planning Time: 0.158 ms
 Execution Time: 2.370 ms


=== ROLLBACK: синтетические данные удалены, индекс на месте
