Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7f580aa

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 46dea24 commit 7f580aa

File tree

3 files changed

+41
-108
lines changed

3 files changed

+41
-108
lines changed

contrib/amcheck/t/002_cic.pl

+17-31
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use PostgreSQL::Test::Cluster;
1010
use PostgreSQL::Test::Utils;
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

+24-29
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use PostgreSQL::Test::Cluster;
1010
use PostgreSQL::Test::Utils;
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/PostgreSQL/Test/Cluster.pm

-48
Original file line numberDiff line numberDiff line change
@@ -2070,54 +2070,6 @@ sub pgbench
20702070

20712071
=pod
20722072
2073-
=item $node->background_pgbench($opts, $files, \$stdout, $timer) => harness
2074-
2075-
Invoke B<pgbench> and return an IPC::Run harness object. The process's stdin
2076-
is empty, and its stdout and stderr go to the $stdout scalar reference. This
2077-
allows the caller to act on other parts of the system while B<pgbench> is
2078-
running. Errors from B<pgbench> are the caller's problem.
2079-
2080-
The specified timer object is attached to the harness, as well. It's caller's
2081-
responsibility to select the timeout length, and to restart the timer after
2082-
each command if the timeout is per-command.
2083-
2084-
Be sure to "finish" the harness when done with it.
2085-
2086-
=over
2087-
2088-
=item $opts
2089-
2090-
Options as a string to be split on spaces.
2091-
2092-
=item $files
2093-
2094-
Reference to filename/contents dictionary.
2095-
2096-
=back
2097-
2098-
=cut
2099-
2100-
sub background_pgbench
2101-
{
2102-
my ($self, $opts, $files, $stdout, $timer) = @_;
2103-
2104-
my @cmd =
2105-
('pgbench', split(/\s+/, $opts), $self->_pgbench_make_files($files));
2106-
2107-
local %ENV = $self->_get_env();
2108-
2109-
my $stdin = "";
2110-
# IPC::Run would otherwise append to existing contents:
2111-
$$stdout = "" if ref($stdout);
2112-
2113-
my $harness = IPC::Run::start \@cmd, '<', \$stdin, '>', $stdout, '2>&1',
2114-
$timer;
2115-
2116-
return $harness;
2117-
}
2118-
2119-
=pod
2120-
21212073
=item $node->connect_ok($connstr, $test_name, %params)
21222074
21232075
Attempt a connection with a custom connection string. This is expected

0 commit comments

Comments
 (0)