# Copyright (c) 2026, Kirillov Vyacheslav by AI, QDB Development Group # # amcheck verification of indexes on CFS (compressed) relations. # # Three scenarios are exercised: # # 1. Point-in-time recovery to an intermediate WAL state in the middle of # a btree page deletion, with both the heap and the index being CFS # relations. Recovery replays the interrupted deletion into # compressed storage, and amcheck must read the index pages back # through CFS decompression. # # 2. CREATE INDEX CONCURRENTLY with concurrent modifications on a CFS # heap. The btree and GIN indexes built by CIC inherit the heap's # CFS policy, so the concurrent INSERT churn and the CIC build both # write through the CFS smgr path, and amcheck reads the resulting # compressed index pages back through CFS decompression. # # 3. CREATE INDEX CONCURRENTLY overlapping two-phase transactions on a # CFS heap, including a server restart with prepared transactions # still open. The restart mid-2PC exercises CFS journal replay, and # amcheck reads the CIC-built compressed index pages back through CFS # decompression. use strict; use warnings FATAL => 'all'; use PostgreSQL::Test::Cluster; use PostgreSQL::Test::Utils; use Test::More; # --------------------------------------------------------------- # PITR to a mid-btree-page-deletion WAL state on CFS relations # --------------------------------------------------------------- { # origin node: generate WAL records of interest. my $origin = PostgreSQL::Test::Cluster->new('origin'); $origin->init(has_archiving => 1, allows_streaming => 1); $origin->append_conf('postgresql.conf', 'autovacuum = off'); $origin->start; $origin->backup('my_backup'); # Create a table with each of 6 PK values spanning 1/4 of a block. Delete # the first four, so one index leaf is eligible for deletion. Make a # replication slot just so pg_walinspect will always have access to later # WAL. my $setup = <safe_psql('postgres', $setup); my $before_vacuum_lsn = $origin->safe_psql('postgres', "SELECT pg_current_wal_lsn()"); # VACUUM to delete the aforementioned leaf page. Force an XLogFlush() by # dropping a permanent table. That way, the XLogReader infrastructure can # always see VACUUM's records, even under synchronous_commit=off. Finally, # find the LSN of that VACUUM's last UNLINK_PAGE record. my $vacuum = <safe_psql('postgres', $vacuum); $origin->stop; die "did not find UNLINK_PAGE record" unless $unlink_lsn; # replica node: amcheck at notable points in the WAL stream my $replica = PostgreSQL::Test::Cluster->new('replica'); $replica->init_from_backup($origin, 'my_backup', has_restoring => 1); $replica->append_conf('postgresql.conf', "recovery_target_lsn = '$unlink_lsn'"); $replica->append_conf('postgresql.conf', 'recovery_target_inclusive = off'); $replica->append_conf('postgresql.conf', 'recovery_target_action = promote'); $replica->start; $replica->poll_query_until('postgres', "SELECT pg_is_in_recovery() = 'f';") or die "Timed out while waiting for PITR promotion"; # recovery done; run amcheck my $debug = "SET client_min_messages = 'debug1'"; my ($rc, $stderr); $rc = $replica->psql( 'postgres', "$debug; SELECT bt_index_parent_check('not_leftmost_pk', true)", stderr => \$stderr); print STDERR $stderr, "\n"; is($rc, 0, "bt_index_parent_check passes on CFS index after PITR"); like( $stderr, qr/interrupted page deletion detected/, "bt_index_parent_check reports the interrupted page deletion"); $rc = $replica->psql( 'postgres', "$debug; SELECT bt_index_check('not_leftmost_pk', true)", stderr => \$stderr); print STDERR $stderr, "\n"; is($rc, 0, "bt_index_check passes on CFS index after PITR"); # The CFS sidecars of the table and its index must be structurally valid # after the interrupted-deletion PITR. for my $rel ('not_leftmost', 'not_leftmost_pk') { is( $replica->safe_psql( 'postgres', "SELECT ok FROM pg_cfs_check('$rel'::regclass, true)"), 't', "$rel sidecars intact after PITR"); } } # --------------------------------------------------------------- # CREATE INDEX CONCURRENTLY with concurrent writes on a CFS heap # --------------------------------------------------------------- { my $node = PostgreSQL::Test::Cluster->new('CIC_test'); $node->init; $node->append_conf('postgresql.conf', 'lock_timeout = ' . (1000 * $PostgreSQL::Test::Utils::timeout_default)); $node->start; $node->safe_psql('postgres', q(CREATE EXTENSION amcheck)); # The heap is a CFS relation; indexes created on it inherit CFS. $node->safe_psql('postgres', q(CREATE TABLE tbl(i int, j jsonb) WITH (algorithm = 'lz4'))); $node->safe_psql('postgres', q(CREATE INDEX idx ON tbl(i))); $node->safe_psql('postgres', q(CREATE INDEX ginidx ON tbl USING gin(j))); # Both pre-existing indexes must inherit CFS reloptions from the heap. like( $node->safe_psql( 'postgres', q(SELECT reloptions::text FROM pg_class WHERE oid = 'idx'::regclass) ), qr/algorithm=lz4/, 'btree index inherited CFS algorithm reloption'); like( $node->safe_psql( 'postgres', q(SELECT reloptions::text FROM pg_class WHERE oid = 'ginidx'::regclass) ), qr/algorithm=lz4/, 'GIN index inherited CFS algorithm reloption'); # # Stress CIC with pgbench. # # pgbench might try to launch more than one instance of the CIC # transaction concurrently. That would deadlock, so use an advisory # lock to ensure only one CIC runs at a time. # $node->pgbench( '--no-vacuum --client=5 --transactions=100', 0, [qr{actually processed}], [qr{^$}], 'concurrent INSERTs and CIC on CFS heap', { '008_pgbench_concurrent_transaction' => q( BEGIN; INSERT INTO tbl VALUES(0, '{"a":[["b",{"x":1}],["b",{"x":2}]],"c":3}'); COMMIT; ), '008_pgbench_concurrent_transaction_savepoints' => q( BEGIN; SAVEPOINT s1; INSERT INTO tbl VALUES(0, '[[14,2,3]]'); COMMIT; ), '008_pgbench_concurrent_cic' => q( SELECT pg_try_advisory_lock(42)::integer AS gotlock \gset \if :gotlock DROP INDEX CONCURRENTLY idx; CREATE INDEX CONCURRENTLY idx ON tbl(i); DROP INDEX CONCURRENTLY ginidx; CREATE INDEX CONCURRENTLY ginidx ON tbl USING gin(j); SELECT bt_index_check('idx',true); SELECT gin_index_check('ginidx'); SELECT pg_advisory_unlock(42); \endif ) }); # Test bt_index_parent_check() with indexes created with # CREATE INDEX CONCURRENTLY. $node->safe_psql('postgres', q(CREATE TABLE quebec(i int primary key) WITH (algorithm = 'lz4'))); # Insert two rows into index $node->safe_psql('postgres', q(INSERT INTO quebec SELECT i FROM generate_series(1, 2) s(i);)); # start background transaction my $in_progress_h = $node->background_psql('postgres'); $in_progress_h->query_safe(q(BEGIN; SELECT pg_current_xact_id();)); # delete one row from table, while background transaction is in progress $node->safe_psql('postgres', q(DELETE FROM quebec WHERE i = 1;)); # create index concurrently, which will skip the deleted row $node->safe_psql('postgres', q(CREATE INDEX CONCURRENTLY oscar ON quebec(i);)); # check index using bt_index_parent_check my $result = $node->psql('postgres', q(SELECT bt_index_parent_check('oscar', heapallindexed => true))); is($result, '0', 'bt_index_parent_check passes on CIC index after removed row (CFS)'); $in_progress_h->quit; $node->stop; } # --------------------------------------------------------------- # CREATE INDEX CONCURRENTLY overlapping 2PC on a CFS heap, # including a restart with prepared transactions # --------------------------------------------------------------- { Test::More->builder->todo_start('filesystem bug') if PostgreSQL::Test::Utils::has_wal_read_bug; my $node = PostgreSQL::Test::Cluster->new('CIC_2PC_test'); $node->init; $node->append_conf('postgresql.conf', 'max_prepared_transactions = 10'); $node->append_conf('postgresql.conf', 'lock_timeout = ' . (1000 * $PostgreSQL::Test::Utils::timeout_default)); $node->start; $node->safe_psql('postgres', q(CREATE EXTENSION amcheck)); # The heap is a CFS relation; CIC indexes inherit CFS. $node->safe_psql('postgres', q(CREATE TABLE tbl(i int, j jsonb) WITH (algorithm = 'lz4'))); # # Run 3 overlapping 2PC transactions with CIC # # We have two concurrent background psql processes: $main_h for INSERTs # and $cic_h for CIC. Also, we use non-background psql for some COMMIT # PREPARED statements. # my $main_h = $node->background_psql('postgres'); $main_h->query_safe( q( BEGIN; INSERT INTO tbl VALUES(0, '[[14,2,3]]'); )); my $cic_h = $node->background_psql('postgres'); $cic_h->query_until( qr/start/, q( \echo start CREATE INDEX CONCURRENTLY idx ON tbl(i); CREATE INDEX CONCURRENTLY ginidx ON tbl USING gin(j); )); $main_h->query_safe( q( PREPARE TRANSACTION 'a'; )); $main_h->query_safe( q( BEGIN; INSERT INTO tbl VALUES(0, '[[14,2,3]]'); )); $node->safe_psql('postgres', q(COMMIT PREPARED 'a';)); $main_h->query_safe( q( PREPARE TRANSACTION 'b'; BEGIN; INSERT INTO tbl VALUES(0, '"mary had a little lamb"'); )); $node->safe_psql('postgres', q(COMMIT PREPARED 'b';)); $main_h->query_safe( q( PREPARE TRANSACTION 'c'; COMMIT PREPARED 'c'; )); $main_h->quit; $cic_h->quit; my $result = $node->psql('postgres', q(SELECT bt_index_check('idx',true))); is($result, '0', 'bt_index_check passes after overlapping 2PC (CFS)'); $result = $node->psql('postgres', q(SELECT gin_index_check('ginidx'))); is($result, '0', 'gin_index_check passes after overlapping 2PC (CFS)'); # # Server restart shall not change whether prepared xact blocks CIC. # The restart replays the CFS journal for any in-flight CFS relations. # $node->safe_psql( 'postgres', q( BEGIN; INSERT INTO tbl VALUES(0, '{"a":[["b",{"x":1}],["b",{"x":2}]],"c":3}'); PREPARE TRANSACTION 'spans_restart'; BEGIN; CREATE TABLE unused (); PREPARE TRANSACTION 'persists_forever'; )); $node->restart; my $reindex_h = $node->background_psql('postgres'); $reindex_h->query_until( qr/start/, q( \echo start DROP INDEX CONCURRENTLY idx; CREATE INDEX CONCURRENTLY idx ON tbl(i); DROP INDEX CONCURRENTLY ginidx; CREATE INDEX CONCURRENTLY ginidx ON tbl USING gin(j); )); $node->safe_psql('postgres', "COMMIT PREPARED 'spans_restart'"); $reindex_h->quit; $result = $node->psql('postgres', q(SELECT bt_index_check('idx',true))); is($result, '0', 'bt_index_check passes after 2PC and restart (CFS)'); $result = $node->psql('postgres', q(SELECT gin_index_check('ginidx'))); is($result, '0', 'gin_index_check passes after 2PC and restart (CFS)'); # # Stress CIC+2PC with pgbench # # pgbench might try to launch more than one instance of the CIC # transaction concurrently. That would deadlock, so use an advisory # lock to ensure only one CIC runs at a time. # Fix broken index first $node->safe_psql('postgres', q(REINDEX TABLE tbl;)); # Run pgbench. $node->pgbench( '--no-vacuum --client=5 --transactions=100', 0, [qr{actually processed}], [qr{^$}], 'concurrent INSERTs w/ 2PC and CIC on CFS heap', { '009_pgbench_concurrent_2pc' => q( BEGIN; INSERT INTO tbl VALUES(0,'null'); PREPARE TRANSACTION 'c:client_id'; COMMIT PREPARED 'c:client_id'; ), '009_pgbench_concurrent_2pc_savepoint' => q( BEGIN; SAVEPOINT s1; INSERT INTO tbl VALUES(0,'[false, "jnvaba", -76, 7, {"_": [1]}, 9]'); PREPARE TRANSACTION 'c:client_id'; COMMIT PREPARED 'c:client_id'; ), '009_pgbench_concurrent_cic' => q( SELECT pg_try_advisory_lock(42)::integer AS gotlock \gset \if :gotlock DROP INDEX CONCURRENTLY idx; CREATE INDEX CONCURRENTLY idx ON tbl(i); SELECT bt_index_check('idx',true); SELECT pg_advisory_unlock(42); \endif ), '009_pgbench_concurrent_ric' => q( SELECT pg_try_advisory_lock(42)::integer AS gotlock \gset \if :gotlock REINDEX INDEX CONCURRENTLY idx; SELECT bt_index_check('idx',true); SELECT pg_advisory_unlock(42); \endif ), '010_pgbench_concurrent_cic' => q( SELECT pg_try_advisory_lock(42)::integer AS gotginlock \gset \if :gotginlock DROP INDEX CONCURRENTLY ginidx; CREATE INDEX CONCURRENTLY ginidx ON tbl USING gin(j); SELECT gin_index_check('ginidx'); SELECT pg_advisory_unlock(42); \endif ), '011_pgbench_concurrent_ric' => q( SELECT pg_try_advisory_lock(42)::integer AS gotginlock \gset \if :gotginlock REINDEX INDEX CONCURRENTLY ginidx; SELECT gin_index_check('ginidx'); SELECT pg_advisory_unlock(42); \endif ) }); $node->stop; } done_testing();