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.001309 |           0 |        10000 |          53764
(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 
----------------+---------------------+------------+--------------
          53764 |                  14 | 5368 kB    | 6320 kB
(1 row)

--- распределение истории по чатам: 5 самых больших и 5 самых маленьких ---
               chat_id                | rows  
--------------------------------------+-------
 e8b55eec-ce6b-4cc4-9003-e6f1f33ddd32 | 32944
 c995246c-35ed-422d-b097-c71f8829a0e0 | 20775
 d73dc509-7b93-4fc3-b6de-b7079bcc294f |    25
 7485e75e-455e-4a7b-b164-3dbc84099fc0 |     5
 63d536a7-78d4-4502-92fa-9bbac4e7c5c4 |     3
(5 rows)

               chat_id                | rows 
--------------------------------------+------
 143b0ced-4c1a-4742-a4ad-02c4b606ba0f |    1
 b4faa937-91fc-4f41-b25e-f3f692878c1f |    1
 84d473db-544b-4a24-995b-43a4852c68ab |    1
 574fc146-1610-484a-85a9-cb084afa7d28 |    1
 836ed5bf-8ccb-419e-96d5-2c4fe7342292 |    1
(5 rows)

--- A. keyset, первая страница самого большого чата ---
                                                                  QUERY PLAN                                                                  
----------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.41..6.89 rows=50 width=67) (actual time=0.006..0.016 rows=50 loops=1)
   Buffers: shared hit=24
   ->  Index Scan using messages_history_idx on messages  (cost=0.41..4269.16 rows=32968 width=67) (actual time=0.006..0.014 rows=50 loops=1)
         Index Cond: (chat_id = 'e8b55eec-ce6b-4cc4-9003-e6f1f33ddd32'::uuid)
         Buffers: shared hit=24
 Planning:
   Buffers: shared hit=6
 Planning Time: 0.046 ms
 Execution Time: 0.026 ms
(9 rows)

--- B. keyset, страница на глубине 90% истории (так работает cursor API) ---
                                                                                                       QUERY PLAN                                                                                                        
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.41..11.51 rows=50 width=67) (actual time=0.017..0.025 rows=50 loops=1)
   Buffers: shared hit=53
   ->  Index Scan using messages_history_idx on messages  (cost=0.41..3272.65 rows=14746 width=67) (actual time=0.017..0.023 rows=50 loops=1)
         Index Cond: ((chat_id = 'e8b55eec-ce6b-4cc4-9003-e6f1f33ddd32'::uuid) AND (ROW(created_at, id) > ROW('2026-10-19 15:51:38.025965+00'::timestamp with time zone, '0375fd16-e024-4391-93a5-0d5a1a21786d'::uuid)))
         Buffers: shared hit=53
 Planning Time: 0.027 ms
 Execution Time: 0.063 ms
(7 rows)

--- C. OFFSET на той же глубине (так API не делает: цена растёт со страницей) ---
                                                                   QUERY PLAN                                                                    
-------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=3839.41..3845.89 rows=50 width=67) (actual time=4.152..4.159 rows=50 loops=1)
   Buffers: shared hit=29813
   ->  Index Scan using messages_history_idx on messages  (cost=0.41..4269.16 rows=32968 width=67) (actual time=0.008..3.474 rows=29699 loops=1)
         Index Cond: (chat_id = 'e8b55eec-ce6b-4cc4-9003-e6f1f33ddd32'::uuid)
         Buffers: shared hit=29813
 Planning Time: 0.018 ms
 Execution Time: 4.172 ms
(7 rows)

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

