| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test CREATE INDEX CONCURRENTLY with concurrent prepared-xact 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 |
Test::More->builder->todo_start('filesystem bug') |
| 14 |
if PostgreSQL::Test::Utils::has_wal_read_bug; |
| 15 |
|
| 16 |
my ($node, $result); |
| 17 |
|
| 18 |
# |
| 19 |
# Test set-up |
| 20 |
# |
| 21 |
$node = PostgreSQL::Test::Cluster->new('CIC_2PC_test'); |
| 22 |
$node->init; |
| 23 |
$node->append_conf('postgresql.conf', 'max_prepared_transactions = 10'); |
| 24 |
$node->append_conf('postgresql.conf', |
| 25 |
'lock_timeout = ' . (1000 * $PostgreSQL::Test::Utils::timeout_default)); |
| 26 |
$node->start; |
| 27 |
$node->safe_psql('postgres', q(CREATE EXTENSION amcheck)); |
| 28 |
$node->safe_psql('postgres', q(CREATE TABLE tbl(i int, j jsonb))); |
| 29 |
|
| 30 |
|
| 31 |
# |
| 32 |
# Run 3 overlapping 2PC transactions with CIC |
| 33 |
# |
| 34 |
# We have two concurrent background psql processes: $main_h for INSERTs and |
| 35 |
# $cic_h for CIC. Also, we use non-background psql for some COMMIT PREPARED |
| 36 |
# statements. |
| 37 |
# |
| 38 |
|
| 39 |
my $main_h = $node->background_psql('postgres'); |
| 40 |
|
| 41 |
$main_h->query_safe( |
| 42 |
q( |
| 43 |
BEGIN; |
| 44 |
INSERT INTO tbl VALUES(0, '[[14,2,3]]'); |
| 45 |
)); |
| 46 |
|
| 47 |
my $cic_h = $node->background_psql('postgres'); |
| 48 |
|
| 49 |
$cic_h->query_until( |
| 50 |
qr/start/, q( |
| 51 |
\echo start |
| 52 |
CREATE INDEX CONCURRENTLY idx ON tbl(i); |
| 53 |
CREATE INDEX CONCURRENTLY ginidx ON tbl USING gin(j); |
| 54 |
)); |
| 55 |
|
| 56 |
$main_h->query_safe( |
| 57 |
q( |
| 58 |
PREPARE TRANSACTION 'a'; |
| 59 |
)); |
| 60 |
|
| 61 |
$main_h->query_safe( |
| 62 |
q( |
| 63 |
BEGIN; |
| 64 |
INSERT INTO tbl VALUES(0, '[[14,2,3]]'); |
| 65 |
)); |
| 66 |
|
| 67 |
$node->safe_psql('postgres', q(COMMIT PREPARED 'a';)); |
| 68 |
|
| 69 |
$main_h->query_safe( |
| 70 |
q( |
| 71 |
PREPARE TRANSACTION 'b'; |
| 72 |
BEGIN; |
| 73 |
INSERT INTO tbl VALUES(0, '"mary had a little lamb"'); |
| 74 |
)); |
| 75 |
|
| 76 |
$node->safe_psql('postgres', q(COMMIT PREPARED 'b';)); |
| 77 |
|
| 78 |
$main_h->query_safe( |
| 79 |
q( |
| 80 |
PREPARE TRANSACTION 'c'; |
| 81 |
COMMIT PREPARED 'c'; |
| 82 |
)); |
| 83 |
|
| 84 |
$main_h->quit; |
| 85 |
$cic_h->quit; |
| 86 |
|
| 87 |
$result = $node->psql('postgres', q(SELECT bt_index_check('idx',true))); |
| 88 |
is($result, '0', 'bt_index_check after overlapping 2PC'); |
| 89 |
|
| 90 |
$result = $node->psql('postgres', q(SELECT gin_index_check('ginidx'))); |
| 91 |
is($result, '0', 'gin_index_check after overlapping 2PC'); |
| 92 |
|
| 93 |
|
| 94 |
# |
| 95 |
# Server restart shall not change whether prepared xact blocks CIC |
| 96 |
# |
| 97 |
|
| 98 |
$node->safe_psql( |
| 99 |
'postgres', q( |
| 100 |
BEGIN; |
| 101 |
INSERT INTO tbl VALUES(0, '{"a":[["b",{"x":1}],["b",{"x":2}]],"c":3}'); |
| 102 |
PREPARE TRANSACTION 'spans_restart'; |
| 103 |
BEGIN; |
| 104 |
CREATE TABLE unused (); |
| 105 |
PREPARE TRANSACTION 'persists_forever'; |
| 106 |
)); |
| 107 |
$node->restart; |
| 108 |
|
| 109 |
my $reindex_h = $node->background_psql('postgres'); |
| 110 |
$reindex_h->query_until( |
| 111 |
qr/start/, q( |
| 112 |
\echo start |
| 113 |
DROP INDEX CONCURRENTLY idx; |
| 114 |
CREATE INDEX CONCURRENTLY idx ON tbl(i); |
| 115 |
DROP INDEX CONCURRENTLY ginidx; |
| 116 |
CREATE INDEX CONCURRENTLY ginidx ON tbl USING gin(j); |
| 117 |
)); |
| 118 |
|
| 119 |
$node->safe_psql('postgres', "COMMIT PREPARED 'spans_restart'"); |
| 120 |
$reindex_h->quit; |
| 121 |
$result = $node->psql('postgres', q(SELECT bt_index_check('idx',true))); |
| 122 |
is($result, '0', 'bt_index_check after 2PC and restart'); |
| 123 |
$result = $node->psql('postgres', q(SELECT gin_index_check('ginidx'))); |
| 124 |
is($result, '0', 'gin_index_check after 2PC and restart'); |
| 125 |
|
| 126 |
|
| 127 |
# |
| 128 |
# Stress CIC+2PC with pgbench |
| 129 |
# |
| 130 |
# pgbench might try to launch more than one instance of the CIC |
| 131 |
# transaction concurrently. That would deadlock, so use an advisory |
| 132 |
# lock to ensure only one CIC runs at a time. |
| 133 |
|
| 134 |
# Fix broken index first |
| 135 |
$node->safe_psql('postgres', q(REINDEX TABLE tbl;)); |
| 136 |
|
| 137 |
# Run pgbench. |
| 138 |
$node->pgbench( |
| 139 |
'--no-vacuum --client=5 --transactions=100', |
| 140 |
0, |
| 141 |
[qr{actually processed}], |
| 142 |
[qr{^$}], |
| 143 |
'concurrent INSERTs w/ 2PC and CIC', |
| 144 |
{ |
| 145 |
'003_pgbench_concurrent_2pc' => q( |
| 146 |
BEGIN; |
| 147 |
INSERT INTO tbl VALUES(0,'null'); |
| 148 |
PREPARE TRANSACTION 'c:client_id'; |
| 149 |
COMMIT PREPARED 'c:client_id'; |
| 150 |
), |
| 151 |
'003_pgbench_concurrent_2pc_savepoint' => q( |
| 152 |
BEGIN; |
| 153 |
SAVEPOINT s1; |
| 154 |
INSERT INTO tbl VALUES(0,'[false, "jnvaba", -76, 7, {"_": [1]}, 9]'); |
| 155 |
PREPARE TRANSACTION 'c:client_id'; |
| 156 |
COMMIT PREPARED 'c:client_id'; |
| 157 |
), |
| 158 |
'003_pgbench_concurrent_cic' => q( |
| 159 |
SELECT pg_try_advisory_lock(42)::integer AS gotlock \gset |
| 160 |
\if :gotlock |
| 161 |
DROP INDEX CONCURRENTLY idx; |
| 162 |
CREATE INDEX CONCURRENTLY idx ON tbl(i); |
| 163 |
SELECT bt_index_check('idx',true); |
| 164 |
SELECT pg_advisory_unlock(42); |
| 165 |
\endif |
| 166 |
), |
| 167 |
'004_pgbench_concurrent_ric' => q( |
| 168 |
SELECT pg_try_advisory_lock(42)::integer AS gotlock \gset |
| 169 |
\if :gotlock |
| 170 |
REINDEX INDEX CONCURRENTLY idx; |
| 171 |
SELECT bt_index_check('idx',true); |
| 172 |
SELECT pg_advisory_unlock(42); |
| 173 |
\endif |
| 174 |
), |
| 175 |
'005_pgbench_concurrent_cic' => q( |
| 176 |
SELECT pg_try_advisory_lock(42)::integer AS gotginlock \gset |
| 177 |
\if :gotginlock |
| 178 |
DROP INDEX CONCURRENTLY ginidx; |
| 179 |
CREATE INDEX CONCURRENTLY ginidx ON tbl USING gin(j); |
| 180 |
SELECT gin_index_check('ginidx'); |
| 181 |
SELECT pg_advisory_unlock(42); |
| 182 |
\endif |
| 183 |
), |
| 184 |
'006_pgbench_concurrent_ric' => q( |
| 185 |
SELECT pg_try_advisory_lock(42)::integer AS gotginlock \gset |
| 186 |
\if :gotginlock |
| 187 |
REINDEX INDEX CONCURRENTLY ginidx; |
| 188 |
SELECT gin_index_check('ginidx'); |
| 189 |
SELECT pg_advisory_unlock(42); |
| 190 |
\endif |
| 191 |
) |
| 192 |
|
| 193 |
}); |
| 194 |
|
| 195 |
$node->stop; |
| 196 |
done_testing(); |