Timing is on.
INSERT 0 1
Time: 11.593 ms
INSERT 0 1
Time: 3.779 ms
INSERT 0 1
Time: 8.619 ms
DO
Time: 1353.910 ms (00:01.354)
ANALYZE
Time: 77.416 ms
=== dataset ===
 messages_in_chat 
------------------
            12000
(1 row)

Time: 4.243 ms
=== index used by the history query ===
        indexname        |                                           indexdef                                            
-------------------------+-----------------------------------------------------------------------------------------------
 messages_pkey           | CREATE UNIQUE INDEX messages_pkey ON public.messages USING btree (id)
 messages_chat_order_idx | CREATE INDEX messages_chat_order_idx ON public.messages USING btree (chat_id, created_at, id)
(2 rows)

Time: 13.962 ms
=== plan: first page (no cursor) ===
                                                                   QUERY PLAN                                                                    
-------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.29..6.36 rows=50 width=73) (actual time=0.025..0.061 rows=50 loops=1)
   Buffers: shared hit=4
   ->  Index Scan using messages_chat_order_idx on messages  (cost=0.29..1459.39 rows=12000 width=73) (actual time=0.022..0.048 rows=50 loops=1)
         Index Cond: (chat_id = '22222222-2222-4222-8222-222222222222'::uuid)
         Buffers: shared hit=4
 Planning:
   Buffers: shared hit=22
 Planning Time: 0.474 ms
 Execution Time: 0.165 ms
(9 rows)

Time: 5.762 ms
Time: 11.210 ms
=== plan: deep keyset page after 10 000 records (what the API sends) ===
                                                                                                      QUERY PLAN                                                                                                      
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.29..15.60 rows=50 width=73) (actual time=0.097..0.133 rows=50 loops=1)
   Buffers: shared hit=4
   ->  Index Scan using messages_chat_order_idx on messages  (cost=0.29..769.32 rows=2510 width=73) (actual time=0.095..0.121 rows=50 loops=1)
         Index Cond: ((chat_id = '22222222-2222-4222-8222-222222222222'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:10.001+00'::timestamp with time zone, 'eab21e64-f0cb-4070-9044-c8c3a2daac6d'::uuid)))
         Buffers: shared hit=4
 Planning Time: 0.270 ms
 Execution Time: 0.208 ms
(7 rows)

Time: 2.785 ms
=== comparison: the OFFSET form this API does not use ===
                                                       QUERY PLAN                                                        
-------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=1201.12..1201.24 rows=50 width=73) (actual time=13.641..13.660 rows=50 loops=1)
   Buffers: shared hit=205
   ->  Sort  (cost=1176.12..1206.12 rows=12000 width=73) (actual time=11.589..12.757 rows=10050 loops=1)
         Sort Key: created_at, id
         Sort Method: quicksort  Memory: 1978kB
         Buffers: shared hit=205
         ->  Seq Scan on messages  (cost=0.00..363.08 rows=12000 width=73) (actual time=0.188..5.317 rows=12000 loops=1)
               Filter: (chat_id = '22222222-2222-4222-8222-222222222222'::uuid)
               Rows Removed by Filter: 646
               Buffers: shared hit=205
 Planning Time: 0.106 ms
 Execution Time: 14.005 ms
(12 rows)

Time: 14.848 ms
