Debian Codegraph @

contrib/amcheck/t/002_cic.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # Test CREATE INDEX CONCURRENTLY with concurrent modifications
5 use strict;
6 use warnings FATAL => 'all';
7
8 use PostgreSQL::Test::Cluster;
9 use PostgreSQL::Test::Utils;
10
11 use Test::More;
12
13 my $node;
14
15 #
16 # Test set-up
17 #
18 $node = PostgreSQL::Test::Cluster->new('CIC_test');
19 $node->init;
20 $node->append_conf('postgresql.conf',
21 'lock_timeout = ' . (1000 * $PostgreSQL::Test::Utils::timeout_default));
22 $node->start;
23 $node->safe_psql('postgres', q(CREATE EXTENSION amcheck));
24 $node->safe_psql('postgres', q(CREATE TABLE tbl(i int, j jsonb)));
25 $node->safe_psql('postgres', q(CREATE INDEX idx ON tbl(i)));
26 $node->safe_psql('postgres', q(CREATE INDEX ginidx ON tbl USING gin(j)));
27
28 #
29 # Stress CIC with pgbench.
30 #
31 # pgbench might try to launch more than one instance of the CIC
32 # transaction concurrently. That would deadlock, so use an advisory
33 # lock to ensure only one CIC runs at a time.
34 #
35 $node->pgbench(
36 '--no-vacuum --client=5 --transactions=100',
37 0,
38 [qr{actually processed}],
39 [qr{^$}],
40 'concurrent INSERTs and CIC',
41 {
42 '002_pgbench_concurrent_transaction' => q(
43 BEGIN;
44 INSERT INTO tbl VALUES(0, '{"a":[["b",{"x":1}],["b",{"x":2}]],"c":3}');
45 COMMIT;
46 ),
47 '002_pgbench_concurrent_transaction_savepoints' => q(
48 BEGIN;
49 SAVEPOINT s1;
50 INSERT INTO tbl VALUES(0, '[[14,2,3]]');
51 COMMIT;
52 ),
53 '002_pgbench_concurrent_cic' => q(
54 SELECT pg_try_advisory_lock(42)::integer AS gotlock \gset
55 \if :gotlock
56 DROP INDEX CONCURRENTLY idx;
57 CREATE INDEX CONCURRENTLY idx ON tbl(i);
58 DROP INDEX CONCURRENTLY ginidx;
59 CREATE INDEX CONCURRENTLY ginidx ON tbl USING gin(j);
60 SELECT bt_index_check('idx',true);
61 SELECT gin_index_check('ginidx');
62 SELECT pg_advisory_unlock(42);
63 \endif
64 )
65 });
66
67 # Test bt_index_parent_check() with indexes created with
68 # CREATE INDEX CONCURRENTLY.
69 $node->safe_psql('postgres', q(CREATE TABLE quebec(i int primary key)));
70 # Insert two rows into index
71 $node->safe_psql('postgres',
72 q(INSERT INTO quebec SELECT i FROM generate_series(1, 2) s(i);));
73
74 # start background transaction
75 my $in_progress_h = $node->background_psql('postgres');
76 $in_progress_h->query_safe(q(BEGIN; SELECT pg_current_xact_id();));
77
78 # delete one row from table, while background transaction is in progress
79 $node->safe_psql('postgres', q(DELETE FROM quebec WHERE i = 1;));
80 # create index concurrently, which will skip the deleted row
81 $node->safe_psql('postgres',
82 q(CREATE INDEX CONCURRENTLY oscar ON quebec(i);));
83
84 # check index using bt_index_parent_check
85 my $result = $node->psql('postgres',
86 q(SELECT bt_index_parent_check('oscar', heapallindexed => true)));
87 is($result, '0', 'bt_index_parent_check for CIC after removed row');
88
89 $in_progress_h->quit;
90
91 $node->stop;
92 done_testing();