Skip to content

Commit 141cd0e

Browse files
committed
Improve contrib/amcheck's tests for CREATE INDEX CONCURRENTLY.
Commits fdd965d and 3cd9c3b tested CREATE INDEX CONCURRENTLY by launching two separate pgbench runs concurrently. This was needed so that only a single client thread would run CREATE INDEX CONCURRENTLY, avoiding deadlock between two CICs. However, there's a better way, which is to use an advisory lock to prevent concurrent CICs. That's better in part because the test code is shorter and more readable, but mostly because it automatically scales things to launch an appropriate number of CICs relative to the number of INSERT transactions. As committed, typically half to three-quarters of the CIC transactions were pointless because the INSERT transactions had already stopped. In passing, remove background_pgbench, which was added to support these tests and isn't needed anymore. We can always put it back if we find a use for it later. Back-patch to v12; older pgbench versions lack the conditional-execution features needed for this method. Tom Lane and Andrey Borodin Discussion: https://postgr.es/m/139687.1635277318@sss.pgh.pa.us
1 parent 4badc59 commit 141cd0e

File tree

3 files changed

+41
-109
lines changed

3 files changed

+41
-109
lines changed

contrib/amcheck/t/002_cic.pl

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use PostgresNode;
1010
use TestLib;
1111

12-
use Test::More tests => 4;
12+
use Test::More tests => 3;
1313

1414
my ($node, $result);
1515

@@ -25,32 +25,18 @@
2525
$node->safe_psql('postgres', q(CREATE INDEX idx ON tbl(i)));
2626

2727
#
28-
# Stress CIC with pgbench
28+
# Stress CIC with pgbench.
29+
#
30+
# pgbench might try to launch more than one instance of the CIC
31+
# transaction concurrently. That would deadlock, so use an advisory
32+
# lock to ensure only one CIC runs at a time.
2933
#
30-
31-
# Run background pgbench with CIC. We cannot mix-in this script into single
32-
# pgbench: CIC will deadlock with itself occasionally.
33-
my $pgbench_out = '';
34-
my $pgbench_timer = IPC::Run::timeout(180);
35-
my $pgbench_h = $node->background_pgbench(
36-
'--no-vacuum --client=1 --transactions=200',
37-
{
38-
'002_pgbench_concurrent_cic' => q(
39-
DROP INDEX CONCURRENTLY idx;
40-
CREATE INDEX CONCURRENTLY idx ON tbl(i);
41-
SELECT bt_index_check('idx',true);
42-
)
43-
},
44-
\$pgbench_out,
45-
$pgbench_timer);
46-
47-
# Run pgbench.
4834
$node->pgbench(
49-
'--no-vacuum --client=5 --transactions=200',
35+
'--no-vacuum --client=5 --transactions=100',
5036
0,
5137
[qr{actually processed}],
5238
[qr{^$}],
53-
'concurrent INSERTs',
39+
'concurrent INSERTs and CIC',
5440
{
5541
'002_pgbench_concurrent_transaction' => q(
5642
BEGIN;
@@ -62,17 +48,17 @@
6248
SAVEPOINT s1;
6349
INSERT INTO tbl VALUES(0);
6450
COMMIT;
51+
),
52+
'002_pgbench_concurrent_cic' => q(
53+
SELECT pg_try_advisory_lock(42)::integer AS gotlock \gset
54+
\if :gotlock
55+
DROP INDEX CONCURRENTLY idx;
56+
CREATE INDEX CONCURRENTLY idx ON tbl(i);
57+
SELECT bt_index_check('idx',true);
58+
SELECT pg_advisory_unlock(42);
59+
\endif
6560
)
6661
});
6762

68-
$pgbench_h->pump_nb;
69-
$pgbench_h->finish();
70-
$result =
71-
($Config{osname} eq "MSWin32")
72-
? ($pgbench_h->full_results)[0]
73-
: $pgbench_h->result(0);
74-
is($result, 0, "pgbench with CIC works");
75-
76-
# done
7763
$node->stop;
7864
done_testing();

contrib/amcheck/t/003_cic_2pc.pl

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use PostgresNode;
1010
use TestLib;
1111

12-
use Test::More tests => 6;
12+
use Test::More tests => 5;
1313

1414
my ($node, $result);
1515

@@ -132,57 +132,52 @@
132132
#
133133
# Stress CIC+2PC with pgbench
134134
#
135+
# pgbench might try to launch more than one instance of the CIC
136+
# transaction concurrently. That would deadlock, so use an advisory
137+
# lock to ensure only one CIC runs at a time.
135138

136139
# Fix broken index first
137140
$node->safe_psql('postgres', q(REINDEX TABLE tbl;));
138141

139-
# Run background pgbench with CIC. We cannot mix-in this script into single
140-
# pgbench: CIC will deadlock with itself occasionally.
141-
my $pgbench_out = '';
142-
my $pgbench_timer = IPC::Run::timeout(180);
143-
my $pgbench_h = $node->background_pgbench(
144-
'--no-vacuum --client=1 --transactions=100',
145-
{
146-
'002_pgbench_concurrent_cic' => q(
147-
DROP INDEX CONCURRENTLY idx;
148-
CREATE INDEX CONCURRENTLY idx ON tbl(i);
149-
SELECT bt_index_check('idx',true);
150-
)
151-
},
152-
\$pgbench_out,
153-
$pgbench_timer);
154-
155142
# Run pgbench.
156143
$node->pgbench(
157144
'--no-vacuum --client=5 --transactions=100',
158145
0,
159146
[qr{actually processed}],
160147
[qr{^$}],
161-
'concurrent INSERTs w/ 2PC',
148+
'concurrent INSERTs w/ 2PC and CIC',
162149
{
163-
'002_pgbench_concurrent_2pc' => q(
150+
'003_pgbench_concurrent_2pc' => q(
164151
BEGIN;
165152
INSERT INTO tbl VALUES(0);
166153
PREPARE TRANSACTION 'c:client_id';
167154
COMMIT PREPARED 'c:client_id';
168155
),
169-
'002_pgbench_concurrent_2pc_savepoint' => q(
156+
'003_pgbench_concurrent_2pc_savepoint' => q(
170157
BEGIN;
171158
SAVEPOINT s1;
172159
INSERT INTO tbl VALUES(0);
173160
PREPARE TRANSACTION 'c:client_id';
174161
COMMIT PREPARED 'c:client_id';
162+
),
163+
'003_pgbench_concurrent_cic' => q(
164+
SELECT pg_try_advisory_lock(42)::integer AS gotlock \gset
165+
\if :gotlock
166+
DROP INDEX CONCURRENTLY idx;
167+
CREATE INDEX CONCURRENTLY idx ON tbl(i);
168+
SELECT bt_index_check('idx',true);
169+
SELECT pg_advisory_unlock(42);
170+
\endif
171+
),
172+
'004_pgbench_concurrent_ric' => q(
173+
SELECT pg_try_advisory_lock(42)::integer AS gotlock \gset
174+
\if :gotlock
175+
REINDEX INDEX CONCURRENTLY idx;
176+
SELECT bt_index_check('idx',true);
177+
SELECT pg_advisory_unlock(42);
178+
\endif
175179
)
176180
});
177181

178-
$pgbench_h->pump_nb;
179-
$pgbench_h->finish();
180-
$result =
181-
($Config{osname} eq "MSWin32")
182-
? ($pgbench_h->full_results)[0]
183-
: $pgbench_h->result(0);
184-
is($result, 0, "pgbench with CIC works");
185-
186-
# done
187182
$node->stop;
188183
done_testing();

src/test/perl/PostgresNode.pm

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,55 +1708,6 @@ sub pgbench
17081708

17091709
=pod
17101710
1711-
=item $node->background_pgbench($opts, $files, \$stdout, $timer) => harness
1712-
1713-
Invoke B<pgbench> and return an IPC::Run harness object. The process's stdin
1714-
is empty, and its stdout and stderr go to the $stdout scalar reference. This
1715-
allows the caller to act on other parts of the system while B<pgbench> is
1716-
running. Errors from B<pgbench> are the caller's problem.
1717-
1718-
The specified timer object is attached to the harness, as well. It's caller's
1719-
responsibility to select the timeout length, and to restart the timer after
1720-
each command if the timeout is per-command.
1721-
1722-
Be sure to "finish" the harness when done with it.
1723-
1724-
=over
1725-
1726-
=item $opts
1727-
1728-
Options as a string to be split on spaces.
1729-
1730-
=item $files
1731-
1732-
Reference to filename/contents dictionary.
1733-
1734-
=back
1735-
1736-
=cut
1737-
1738-
sub background_pgbench
1739-
{
1740-
my ($self, $opts, $files, $stdout, $timer) = @_;
1741-
1742-
my @cmd =
1743-
('pgbench', split(/\s+/, $opts), $self->_pgbench_make_files($files));
1744-
1745-
local $ENV{PGHOST} = $self->host;
1746-
local $ENV{PGPORT} = $self->port;
1747-
1748-
my $stdin = "";
1749-
# IPC::Run would otherwise append to existing contents:
1750-
$$stdout = "" if ref($stdout);
1751-
1752-
my $harness = IPC::Run::start \@cmd, '<', \$stdin, '>', $stdout, '2>&1',
1753-
$timer;
1754-
1755-
return $harness;
1756-
}
1757-
1758-
=pod
1759-
17601711
=item $node->poll_query_until($dbname, $query [, $expected ])
17611712
17621713
Run B<$query> repeatedly, until it returns the B<$expected> result

0 commit comments

Comments
 (0)