=== Стенд и версии ===
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.63315 |       66236 |       120000 |         120000
(1 row)


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

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

                                                                                                       QUERY PLAN                                                                                                        
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.42..37.36 rows=50 width=89) (actual time=0.015..0.025 rows=50 loops=1)
   Buffers: shared hit=53
   ->  Index Scan using messages_history_idx on messages  (cost=0.42..5594.06 rows=7570 width=89) (actual time=0.014..0.022 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:
   Buffers: shared hit=17
 Planning Time: 0.098 ms
 Execution Time: 0.048 ms
(9 rows)

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

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

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

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

                                                                         QUERY PLAN                                                                         
------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=3134.85..3134.97 rows=50 width=89) (actual time=1.691..1.695 rows=50 loops=1)
   Buffers: shared hit=411 read=30
   I/O Timings: shared read=0.076
   ->  Sort  (cost=3134.85..3153.65 rows=7522 width=89) (actual time=1.691..1.692 rows=50 loops=1)
         Sort Key: created_at, id
         Sort Method: top-N heapsort  Memory: 33kB
         Buffers: shared hit=411 read=30
         I/O Timings: shared read=0.076
         ->  Bitmap Heap Scan on messages  (cost=368.71..2884.97 rows=7522 width=89) (actual time=1.378..1.560 rows=3294 loops=1)
               Recheck Cond: (chat_id = 'e8b55eec-ce6b-4cc4-9003-e6f1f33ddd32'::uuid)
               Filter: (ROW(created_at, id) > ROW('2026-10-19 15:51:38.025965+00'::timestamp with time zone, '0375fd16-e024-4391-93a5-0d5a1a21786d'::uuid))
               Rows Removed by Filter: 29650
               Heap Blocks: exact=411
               Buffers: shared hit=411 read=30
               I/O Timings: shared read=0.076
               ->  Bitmap Index Scan on messages_chat_only_idx  (cost=0.00..366.83 rows=32872 width=0) (actual time=0.333..0.333 rows=32944 loops=1)
                     Index Cond: (chat_id = 'e8b55eec-ce6b-4cc4-9003-e6f1f33ddd32'::uuid)
                     Buffers: shared read=30
                     I/O Timings: shared read=0.076
 Planning:
   Buffers: shared hit=26 read=1
   I/O Timings: shared read=0.009
 Planning Time: 0.130 ms
 Execution Time: 1.728 ms
(24 rows)

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

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

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

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

                                                                                                       QUERY PLAN                                                                                                        
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.42..34.19 rows=50 width=89) (actual time=0.017..0.021 rows=50 loops=1)
   Buffers: shared hit=1 read=3
   I/O Timings: shared read=0.006
   ->  Index Only Scan using messages_history_covering_idx on messages  (cost=0.42..5110.68 rows=7565 width=89) (actual time=0.016..0.019 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)))
         Heap Fetches: 0
         Buffers: shared hit=1 read=3
         I/O Timings: shared read=0.006
 Planning:
   Buffers: shared hit=28 read=1
   I/O Timings: shared read=0.005
 Planning Time: 0.184 ms
 Execution Time: 0.031 ms
(13 rows)

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

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

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

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


Самый большой чат: e8b55eec-ce6b-4cc4-9003-e6f1f33ddd32

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

 big_chat_rows |           big_chat_md5           
---------------+----------------------------------
         32944 | 6c6725a76404fbb0b8b5b509707e30b5
(1 row)

                  id                  |          created_at           |   text   
--------------------------------------+-------------------------------+----------
 8ecedfe9-56ef-410d-98b3-e3fc414128d9 | 2026-10-19 15:51:11.010631+00 | load-1-0
 424bfa46-6e49-4b60-8ca3-ac3c3f9be02a | 2026-10-19 15:51:11.011234+00 | load-3-0
 05b0e1a2-5862-461a-b8aa-1f91f647e18d | 2026-10-19 15:51:11.01201+00  | load-2-0
(3 rows)

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


=== 4. Бэкап pg_dump -Fc ===
backup_seconds=0.761
dump_size=5.6M

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

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

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

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

 big_chat_rows |           big_chat_md5           
---------------+----------------------------------
         32944 | 6c6725a76404fbb0b8b5b509707e30b5
(1 row)

                  id                  |          created_at           |   text   
--------------------------------------+-------------------------------+----------
 8ecedfe9-56ef-410d-98b3-e3fc414128d9 | 2026-10-19 15:51:11.010631+00 | load-1-0
 424bfa46-6e49-4b60-8ca3-ac3c3f9be02a | 2026-10-19 15:51:11.011234+00 | load-3-0
 05b0e1a2-5862-461a-b8aa-1f91f647e18d | 2026-10-19 15:51:11.01201+00  | load-2-0
(3 rows)

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


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