=== Стенд и версии ===
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)
Darwin arm64
Docker version 29.7.2, build a7dcaa6

=== 1. Засев истории: цель 120000 сообщений, чаты неравномерные ===
  seed_duration  | seeded_rows | target_total | messages_total 
-----------------+-------------+--------------+----------------
 00:00:00.616654 |       67113 |       120000 |         120000
(1 row)


=== 2. Сравнение индексов и цены записи (каждый вариант в откатываемой транзакции) ===
 messages_total 
----------------
         120000
(1 row)

############ Вариант A: messages_history_idx (chat_id, created_at, id) ############
 index_size 
------------
 11 MB
(1 row)

                                                                                                       QUERY PLAN                                                                                                        
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.42..47.47 rows=50 width=90) (actual time=0.009..0.020 rows=50 loops=1)
   Buffers: shared hit=54
   ->  Index Scan using messages_history_idx on messages  (cost=0.42..6827.62 rows=7255 width=90) (actual time=0.008..0.018 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:
   Buffers: shared hit=19
 Planning Time: 0.086 ms
 Execution Time: 0.042 ms
(9 rows)

 pages_measured 
----------------
           5000
(1 row)

 hundred_pages_duration 
------------------------
 00:00:00.00105
(1 row)

 insert_10k_duration 
---------------------
 00:00:00.083522
(1 row)

############ Вариант B: только (chat_id) ############
 index_size 
------------
 848 kB
(1 row)

                                                                         QUERY PLAN                                                                         
------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=3231.76..3231.89 rows=50 width=90) (actual time=1.856..1.860 rows=50 loops=1)
   Buffers: shared hit=451 read=33
   I/O Timings: shared read=0.074
   ->  Sort  (cost=3231.76..3249.63 rows=7146 width=90) (actual time=1.855..1.857 rows=50 loops=1)
         Sort Key: created_at, id
         Sort Method: top-N heapsort  Memory: 33kB
         Buffers: shared hit=451 read=33
         I/O Timings: shared read=0.074
         ->  Bitmap Heap Scan on messages  (cost=408.67..2994.38 rows=7146 width=90) (actual time=1.514..1.710 rows=3630 loops=1)
               Recheck Cond: (chat_id = '174a0477-1fd5-47a9-ab90-ee8b7ef903af'::uuid)
               Filter: (ROW(created_at, id) > ROW('2026-10-23 14:37:21.481035+00'::timestamp with time zone, 'b34cffc1-608f-4281-89b6-d598a0000a8d'::uuid))
               Rows Removed by Filter: 32675
               Heap Blocks: exact=451
               Buffers: shared hit=451 read=33
               I/O Timings: shared read=0.074
               ->  Bitmap Index Scan on messages_chat_only_idx  (cost=0.00..406.88 rows=36612 width=0) (actual time=0.362..0.362 rows=36305 loops=1)
                     Index Cond: (chat_id = '174a0477-1fd5-47a9-ab90-ee8b7ef903af'::uuid)
                     Buffers: shared read=33
                     I/O Timings: shared read=0.074
 Planning:
   Buffers: shared hit=28 read=1
   I/O Timings: shared read=0.009
 Planning Time: 0.126 ms
 Execution Time: 1.879 ms
(24 rows)

 pages_measured 
----------------
           5000
(1 row)

 hundred_pages_duration 
------------------------
 00:00:02.460907
(1 row)

 insert_10k_duration 
---------------------
 00:00:00.077769
(1 row)

############ Вариант C: (chat_id, created_at, id) INCLUDE (sender_id, text) ############
 index_size 
------------
 14 MB
(1 row)

                                                                                                       QUERY PLAN                                                                                                        
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.42..43.36 rows=50 width=90) (actual time=0.039..0.045 rows=50 loops=1)
   Buffers: shared hit=1 read=3
   I/O Timings: shared read=0.012
   ->  Index Only Scan using messages_history_covering_idx on messages  (cost=0.42..6095.87 rows=7098 width=90) (actual time=0.038..0.041 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)))
         Heap Fetches: 0
         Buffers: shared hit=1 read=3
         I/O Timings: shared read=0.012
 Planning:
   Buffers: shared hit=29 read=1
   I/O Timings: shared read=0.007
 Planning Time: 0.144 ms
 Execution Time: 0.056 ms
(13 rows)

 pages_measured 
----------------
           5000
(1 row)

 hundred_pages_duration 
------------------------
 00:00:00.000723
(1 row)

 insert_10k_duration 
---------------------
 00:00:00.085783
(1 row)

############ Схема после эксперимента (ROLLBACK вернул индексы) ############
      indexname       
----------------------
 messages_history_idx
 messages_pkey
(2 rows)


Самый большой чат: 174a0477-1fd5-47a9-ab90-ee8b7ef903af

=== 3. Состояние источника до бэкапа ===
   relation   |  rows  
--------------+--------
 chat_members |   1036
 chats        |     68
 messages     | 120000
 sessions     |     45
 users        |     76
(5 rows)

 big_chat_rows |           big_chat_md5           
---------------+----------------------------------
         36305 | 1841fdf890f6207bc78cb973bedeec27
(1 row)

                  id                  |          created_at           |   text   
--------------------------------------+-------------------------------+----------
 1d15f9ed-952e-45e7-9553-dcd3bc3dd76e | 2026-10-23 14:36:54.449674+00 | load-2-0
 4bc8c702-07e1-42d2-be14-8cdd6dad31a7 | 2026-10-23 14:36:54.450312+00 | load-0-0
 3993ceca-7c29-400d-a1e9-287137672829 | 2026-10-23 14:36:54.450773+00 | load-1-0
(3 rows)

 marker_rows 
-------------
           0
(1 row)


=== 4. Бэкап pg_dump -Fc ===
backup_seconds=0.741
dump_size=5.7M

=== 5. Запись после бэкапа: граница потери данных ===
в источнике маркеров: 1

=== 6. Новое хранилище: отдельный контейнер и отдельный том ===
/var/run/postgresql:5432 - accepting connections

=== 7. Восстановление в новое хранилище ===
restore_seconds=0.357

=== 8. Сверка восстановленной копии ===
   relation   |  rows  
--------------+--------
 chat_members |   1036
 chats        |     68
 messages     | 120000
 sessions     |     45
 users        |     76
(5 rows)

 big_chat_rows |           big_chat_md5           
---------------+----------------------------------
         36305 | 1841fdf890f6207bc78cb973bedeec27
(1 row)

                  id                  |          created_at           |   text   
--------------------------------------+-------------------------------+----------
 1d15f9ed-952e-45e7-9553-dcd3bc3dd76e | 2026-10-23 14:36:54.449674+00 | load-2-0
 4bc8c702-07e1-42d2-be14-8cdd6dad31a7 | 2026-10-23 14:36:54.450312+00 | load-0-0
 3993ceca-7c29-400d-a1e9-287137672829 | 2026-10-23 14:36:54.450773+00 | load-1-0
(3 rows)

 marker_rows 
-------------
           0
(1 row)


=== 9. Итог ===
backup_seconds=0.741 restore_seconds=0.357
messages_source_without_marker=120000 messages_restored=120000
big_chat_md5_source=1841fdf890f6207bc78cb973bedeec27
big_chat_md5_restored=1841fdf890f6207bc78cb973bedeec27
RESULT: восстановленная копия совпадает с источником на момент бэкапа
маркеров после бэкапа в восстановленной копии: 0 (ожидается 0: всё, что подтверждено после снимка, в дампе отсутствует)
