# EXPLAIN (ANALYZE, BUFFERS) of the history query: make explain
# date: 2026-10-06T12:59:03Z, commit: 949622e (branch solution/bpi2301-bakulin)
# host: Windows 11, Docker Desktop 28.5.1 (4 CPU, 4111327232 bytes RAM), Go go1.27.1, Python 3.13.1

 big_chat_messages | all_messages 
-------------------+--------------
             12000 |       102003
(1 row)


== First page: no cursor, limit 50 (+1 row that tells whether a next page exists)
                                                                QUERY PLAN                                                                
------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.42..23.86 rows=51 width=67) (actual time=0.020..0.077 rows=51.00 loops=1)
   Buffers: shared hit=51
   ->  Index Scan using messages_pkey on messages  (cost=0.42..5535.89 rows=12045 width=67) (actual time=0.018..0.072 rows=51.00 loops=1)
         Index Cond: ((chat_id = '01a1114d-de5d-7f2e-8176-990d37b9797e'::uuid) AND (id > '00000000-0000-0000-0000-000000000000'::uuid))
         Index Searches: 1
         Buffers: shared hit=51
 Planning:
   Buffers: shared hit=17
 Planning Time: 0.327 ms
 Execution Time: 0.101 ms
(10 rows)


== Deep page: cursor after the 10 001st message of the chat
                                                               QUERY PLAN                                                               
----------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.42..185.00 rows=51 width=67) (actual time=0.021..0.033 rows=51.00 loops=1)
   Buffers: shared hit=6
   ->  Index Scan using messages_pkey on messages  (cost=0.42..655.49 rows=181 width=67) (actual time=0.020..0.029 rows=51.00 loops=1)
         Index Cond: ((chat_id = '01a1114d-de5d-7f2e-8176-990d37b9797e'::uuid) AND (id > '01a1114d-e03e-797e-bdd0-3cb5b8cb3079'::uuid))
         Index Searches: 1
         Buffers: shared hit=6
 Planning Time: 0.093 ms
 Execution Time: 0.050 ms
(8 rows)


== For contrast, OFFSET pagination of the same page reads every skipped row
                                                                  QUERY PLAN                                                                   
-----------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=2837.94..2838.07 rows=51 width=67) (actual time=4.491..4.496 rows=51.00 loops=1)
   Buffers: shared hit=495
   ->  Sort  (cost=2812.94..2843.06 rows=12047 width=67) (actual time=3.874..4.232 rows=10052.00 loops=1)
         Sort Key: id
         Sort Method: quicksort  Memory: 1978kB
         Buffers: shared hit=495
         ->  Bitmap Heap Scan on messages  (cost=585.78..1996.37 rows=12047 width=67) (actual time=0.422..1.796 rows=12000.00 loops=1)
               Recheck Cond: (chat_id = '01a1114d-de5d-7f2e-8176-990d37b9797e'::uuid)
               Heap Blocks: exact=363
               Buffers: shared hit=495
               ->  Bitmap Index Scan on messages_pkey  (cost=0.00..582.77 rows=12047 width=0) (actual time=0.386..0.386 rows=12000.00 loops=1)
                     Index Cond: (chat_id = '01a1114d-de5d-7f2e-8176-990d37b9797e'::uuid)
                     Index Searches: 1
                     Buffers: shared hit=132
 Planning Time: 0.043 ms
 Execution Time: 4.629 ms
(16 rows)

