# EXPLAIN (ANALYZE, BUFFERS) of the history query: make explain
# date: 2026-10-21T12:09:10Z, commit: 7aa86d3 (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.92 rows=51 width=67) (actual time=0.027..0.060 rows=51.00 loops=1)
   Buffers: shared hit=51
   ->  Index Scan using messages_pkey on messages  (cost=0.42..5526.02 rows=11988 width=67) (actual time=0.026..0.056 rows=51.00 loops=1)
         Index Cond: ((chat_id = '01a15e5f-93aa-7f38-9ff3-684f0d6ded77'::uuid) AND (id > '00000000-0000-0000-0000-000000000000'::uuid))
         Index Searches: 1
         Buffers: shared hit=51
 Planning:
   Buffers: shared hit=17
 Planning Time: 0.211 ms
 Execution Time: 0.097 ms
(10 rows)


== Deep page: cursor after the 10 001st message of the chat
                                                               QUERY PLAN                                                               
----------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.42..184.60 rows=51 width=67) (actual time=0.018..0.030 rows=51.00 loops=1)
   Buffers: shared hit=6
   ->  Index Scan using messages_pkey on messages  (cost=0.42..650.48 rows=180 width=67) (actual time=0.018..0.026 rows=51.00 loops=1)
         Index Cond: ((chat_id = '01a15e5f-93aa-7f38-9ff3-684f0d6ded77'::uuid) AND (id > '01a15e5f-9557-7527-8fb0-c493cb1229d4'::uuid))
         Index Searches: 1
         Buffers: shared hit=6
 Planning Time: 0.071 ms
 Execution Time: 0.047 ms
(8 rows)


== For contrast, OFFSET pagination of the same page reads every skipped row
                                                                  QUERY PLAN                                                                   
-----------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=2832.42..2832.54 rows=51 width=67) (actual time=6.369..6.376 rows=51.00 loops=1)
   Buffers: shared hit=495
   ->  Sort  (cost=2807.41..2837.39 rows=11989 width=67) (actual time=5.728..6.109 rows=10052.00 loops=1)
         Sort Key: id
         Sort Method: quicksort  Memory: 1978kB
         Buffers: shared hit=495
         ->  Bitmap Heap Scan on messages  (cost=585.33..1995.19 rows=11989 width=67) (actual time=0.717..2.712 rows=12000.00 loops=1)
               Recheck Cond: (chat_id = '01a15e5f-93aa-7f38-9ff3-684f0d6ded77'::uuid)
               Heap Blocks: exact=363
               Buffers: shared hit=495
               ->  Bitmap Index Scan on messages_pkey  (cost=0.00..582.34 rows=11989 width=0) (actual time=0.672..0.673 rows=12000.00 loops=1)
                     Index Cond: (chat_id = '01a15e5f-93aa-7f38-9ff3-684f0d6ded77'::uuid)
                     Index Searches: 1
                     Buffers: shared hit=132
 Planning Time: 0.041 ms
 Execution Time: 6.524 ms
(16 rows)

