BEGIN
INSERT 0 0
INSERT 0 0
INSERT 0 0
INSERT 0 0
COMMIT
ANALYZE
1. Первая страница (limit 50)
                                                                 QUERY PLAN                                                                  
---------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.29..5.33 rows=51 width=64) (actual time=0.015..0.020 rows=51 loops=1)
   Buffers: shared hit=3
   ->  Index Scan using messages_history_idx on messages  (cost=0.29..989.57 rows=10000 width=64) (actual time=0.015..0.017 rows=51 loops=1)
         Index Cond: (chat_id = '00000000-0000-4000-8000-0000000000c1'::uuid)
         Buffers: shared hit=3
 Planning:
   Buffers: shared hit=23
 Planning Time: 0.108 ms
 Execution Time: 0.037 ms
(9 rows)

2. Страница из середины истории (keyset cursor)
                                                                                                       QUERY PLAN                                                                                                        
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.29..6.63 rows=51 width=64) (actual time=0.007..0.011 rows=51 loops=1)
   Buffers: shared hit=3
   ->  Index Scan using messages_history_idx on messages  (cost=0.29..620.78 rows=4991 width=64) (actual time=0.006..0.009 rows=51 loops=1)
         Index Cond: ((chat_id = '00000000-0000-4000-8000-0000000000c1'::uuid) AND (ROW(created_at, id) > ROW('2026-11-03 06:25:42.350396+00'::timestamp with time zone, 'e5c90185-a58b-4164-b1b3-cb15e30b97e1'::uuid)))
         Buffers: shared hit=3
 Planning Time: 0.027 ms
 Execution Time: 0.034 ms
(7 rows)

3. Для сравнения: без индекса messages_history_idx
BEGIN
DROP INDEX
                                                       QUERY PLAN                                                        
-------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=581.83..581.96 rows=51 width=64) (actual time=0.915..0.919 rows=51 loops=1)
   Buffers: shared hit=123
   ->  Sort  (cost=581.83..606.83 rows=10000 width=64) (actual time=0.915..0.916 rows=51 loops=1)
         Sort Key: created_at, id
         Sort Method: top-N heapsort  Memory: 31kB
         Buffers: shared hit=123
         ->  Seq Scan on messages  (cost=0.00..248.21 rows=10000 width=64) (actual time=0.004..0.485 rows=10000 loops=1)
               Filter: (chat_id = '00000000-0000-4000-8000-0000000000c1'::uuid)
               Rows Removed by Filter: 17
               Buffers: shared hit=123
 Planning:
   Buffers: shared hit=4
 Planning Time: 0.037 ms
 Execution Time: 0.941 ms
(14 rows)

ROLLBACK
