=== recreate bench ===
CREATE TABLE
CREATE TABLE
CREATE INDEX
CREATE TABLE
CREATE INDEX
CREATE TABLE
CREATE INDEX
=== seed uneven 100000 messages ===
INSERT 0 1
INSERT 0 4
INSERT 0 4
INSERT 0 100
INSERT 0 100
INSERT 0 100000
  title   | messages 
----------+----------
 big      |    80000
 mid      |    15000
 small    |     4000
 tiny-1   |       10
 tiny-10  |       10
 tiny-100 |       10
 tiny-11  |       10
 tiny-12  |       10
(8 rows)

 messages_total 
----------------
         100000
(1 row)

 synchronous_commit 
--------------------
 on
(1 row)

=== cursor in the big chat ===
=== no secondary index: seq scan of the whole table ===
DROP INDEX
=== EXPLAIN seq_scan chat=bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbb1 ===
ANALYZE
                                                                                                           QUERY PLAN                                                                                                            
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=4100.97..4106.72 rows=50 width=56) (actual time=11.330..13.785 rows=50 loops=1)
   Buffers: shared hit=1248
   ->  Gather Merge  (cost=4100.97..6800.71 rows=23476 width=56) (actual time=11.329..13.780 rows=50 loops=1)
         Workers Planned: 1
         Workers Launched: 1
         Buffers: shared hit=1248
         ->  Sort  (cost=3100.96..3159.65 rows=23476 width=56) (actual time=9.597..9.599 rows=34 loops=2)
               Sort Key: created_at, id
               Sort Method: top-N heapsort  Memory: 28kB
               Buffers: shared hit=1248
               Worker 0:  Sort Method: top-N heapsort  Memory: 28kB
               ->  Parallel Seq Scan on messages  (cost=0.00..2321.10 rows=23476 width=56) (actual time=2.023..6.650 rows=20000 loops=2)
                     Filter: ((chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbb1'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.050001+00'::timestamp with time zone, 'c471f25a-9805-4beb-9a46-c9564042507c'::uuid)))
                     Rows Removed by Filter: 30000
                     Buffers: shared hit=1233
 Planning:
   Buffers: shared hit=32
 Planning Time: 0.152 ms
 Execution Time: 13.824 ms
(19 rows)

=== index on (created_at, id) only ===
CREATE INDEX
=== EXPLAIN time_only_big chat=bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbb1 ===
ANALYZE
                                                                        QUERY PLAN                                                                        
----------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.42..7.84 rows=50 width=56) (actual time=0.020..0.038 rows=50 loops=1)
   Buffers: shared hit=2 read=3
   ->  Index Scan using messages_time_idx on messages  (cost=0.42..5946.46 rows=40067 width=56) (actual time=0.018..0.033 rows=50 loops=1)
         Index Cond: (ROW(created_at, id) > ROW('2026-01-01 00:00:00.050001+00'::timestamp with time zone, 'c471f25a-9805-4beb-9a46-c9564042507c'::uuid))
         Filter: (chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbb1'::uuid)
         Buffers: shared hit=2 read=3
 Planning:
   Buffers: shared hit=30 read=1
 Planning Time: 0.166 ms
 Execution Time: 0.066 ms
(10 rows)

=== EXPLAIN time_only_small chat=bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbb3 ===
ANALYZE
                                                                        QUERY PLAN                                                                        
----------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.42..146.66 rows=50 width=56) (actual time=0.045..0.252 rows=50 loops=1)
   Buffers: shared hit=57 read=6
   ->  Index Scan using messages_time_idx on messages  (cost=0.42..5888.31 rows=2013 width=56) (actual time=0.043..0.247 rows=50 loops=1)
         Index Cond: (ROW(created_at, id) > ROW('2026-01-01 00:00:00.050001+00'::timestamp with time zone, 'c471f25a-9805-4beb-9a46-c9564042507c'::uuid))
         Filter: (chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbb3'::uuid)
         Rows Removed by Filter: 1245
         Buffers: shared hit=57 read=6
 Planning:
   Buffers: shared hit=29
 Planning Time: 0.223 ms
 Execution Time: 0.297 ms
(11 rows)

=== insert cost with time index ===
NOTICE:  insert_5000_time_index_ms 109.111
DO
DELETE 5000
DROP INDEX
CREATE INDEX
=== EXPLAIN history_big chat=bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbb1 ===
ANALYZE
                                                                                                       QUERY PLAN                                                                                                        
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.42..9.42 rows=50 width=56) (actual time=0.030..0.049 rows=50 loops=1)
   Buffers: shared hit=2 read=3
   ->  Index Scan using messages_history_idx on messages  (cost=0.42..7225.92 rows=40147 width=56) (actual time=0.029..0.044 rows=50 loops=1)
         Index Cond: ((chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbb1'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.050001+00'::timestamp with time zone, 'c471f25a-9805-4beb-9a46-c9564042507c'::uuid)))
         Buffers: shared hit=2 read=3
 Planning:
   Buffers: shared hit=32 read=1
 Planning Time: 0.387 ms
 Execution Time: 0.089 ms
(9 rows)

=== EXPLAIN history_small chat=bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbb3 ===
ANALYZE
                                                                                                       QUERY PLAN                                                                                                        
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.42..116.76 rows=50 width=56) (actual time=0.022..0.047 rows=50 loops=1)
   Buffers: shared hit=3 read=2
   ->  Index Scan using messages_history_idx on messages  (cost=0.42..4549.26 rows=1955 width=56) (actual time=0.021..0.042 rows=50 loops=1)
         Index Cond: ((chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbb3'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.050001+00'::timestamp with time zone, 'c471f25a-9805-4beb-9a46-c9564042507c'::uuid)))
         Buffers: shared hit=3 read=2
 Planning:
   Buffers: shared hit=27
 Planning Time: 0.144 ms
 Execution Time: 0.073 ms
(9 rows)

=== insert cost with history index ===
NOTICE:  insert_5000_history_index_ms 100.134
DO
DELETE 5000
=== snapshot before dump ===
=== dump and restore ===
CREATE DATABASE
PASS track2
