sh student-tests/explain.sh
=== Стенд ===
api mtusi-messenger:lab2 Up About a minute (healthy)
postgres postgres:17.7-alpine@sha256:bb377b7239d2774ac8cc76f481596ce96c5a6b5e9d141f6d0a0ee371a6e7c0f2 Up About a minute (healthy)
valkey valkey/valkey:8.1-alpine@sha256:081c2f5cb575efc901aa80ff9cdbd1ec6a301682fd35e1ebb4b0990a4a4a8507 Up Less than a second (health: starting)

=== Засев 10000 сообщений с неравномерными чатами ===
  seed_duration  | seeded_rows | target_total | messages_total 
-----------------+-------------+--------------+----------------
 00:00:00.000975 |           0 |        10000 |          52887
(1 row)


=== План запроса истории ===
                                       postgres_version                                        
-----------------------------------------------------------------------------------------------
 PostgreSQL 17.7 on aarch64-unknown-linux-musl, compiled by gcc (Alpine 15.2.0) 15.2.0, 64-bit
(1 row)

 messages_total | chats_with_messages | table_size | indexes_size 
----------------+---------------------+------------+--------------
          52887 |                  14 | 5280 kB    | 7024 kB
(1 row)

--- распределение истории по чатам: 5 самых больших и 5 самых маленьких ---
               chat_id                | rows  
--------------------------------------+-------
 174a0477-1fd5-47a9-ab90-ee8b7ef903af | 36305
 fb87d20c-528a-4bb7-9cd5-e1d4486bdfca | 16537
 3e751b33-08b7-4433-a2be-bb355af7bf62 |    25
 11bd8f2b-db27-4c63-89f0-469ee53c6cad |     5
 30e5554f-e2e0-411e-b5bd-ab86e1fd9c5f |     3
(5 rows)

               chat_id                | rows 
--------------------------------------+------
 b490286c-39b8-4940-92cf-23817def915a |    1
 6fc32369-98df-4387-b07f-00dfa6e0b391 |    1
 f4c1fdcc-0ddd-4fb5-a962-c8901deac9e2 |    1
 903d0035-1bda-4ac6-9fbf-52f28fe0b94c |    1
 fb6df905-d5d2-4300-b74f-1f0196f9e2b0 |    1
(5 rows)

--- A. keyset, первая страница самого большого чата ---
                                                                  QUERY PLAN                                                                  
----------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.41..5.46 rows=50 width=67) (actual time=0.005..0.011 rows=50 loops=1)
   Buffers: shared hit=21
   ->  Index Scan using messages_history_idx on messages  (cost=0.41..3662.26 rows=36288 width=67) (actual time=0.005..0.009 rows=50 loops=1)
         Index Cond: (chat_id = '174a0477-1fd5-47a9-ab90-ee8b7ef903af'::uuid)
         Buffers: shared hit=21
 Planning:
   Buffers: shared hit=8
 Planning Time: 0.049 ms
 Execution Time: 0.018 ms
(9 rows)

--- B. keyset, страница на глубине 90% истории (так работает cursor API) ---
                                                                                                       QUERY PLAN                                                                                                        
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.41..8.17 rows=50 width=67) (actual time=0.006..0.014 rows=50 loops=1)
   Buffers: shared hit=54
   ->  Index Scan using messages_history_idx on messages  (cost=0.41..2148.72 rows=13857 width=67) (actual time=0.005..0.012 rows=50 loops=1)
         Index Cond: ((chat_id = '174a0477-1fd5-47a9-ab90-ee8b7ef903af'::uuid) AND (ROW(created_at, id) > ROW('2026-10-23 14:37:21.481035+00'::timestamp with time zone, 'b34cffc1-608f-4281-89b6-d598a0000a8d'::uuid)))
         Buffers: shared hit=54
 Planning Time: 0.022 ms
 Execution Time: 0.034 ms
(7 rows)

--- C. OFFSET на той же глубине (так API не делает: цена растёт со страницей) ---
                                                                   QUERY PLAN                                                                    
-------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=3297.57..3302.61 rows=50 width=67) (actual time=4.132..4.139 rows=50 loops=1)
   Buffers: shared hit=33082
   ->  Index Scan using messages_history_idx on messages  (cost=0.41..3662.26 rows=36288 width=67) (actual time=0.012..3.519 rows=32724 loops=1)
         Index Cond: (chat_id = '174a0477-1fd5-47a9-ab90-ee8b7ef903af'::uuid)
         Buffers: shared hit=33082
 Planning Time: 0.020 ms
 Execution Time: 4.146 ms
(7 rows)

--- индексы таблицы messages ---
        index         |  size   | scans 
----------------------+---------+-------
 messages_history_idx | 4720 kB |    12
 messages_pkey        | 2304 kB |     0
(2 rows)

