Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5a4b8a8

Browse files
committed
Improve contrib/amcheck's tests for CREATE INDEX CONCURRENTLY.
Commitsfdd965d and3cd9c3b tested CREATE INDEX CONCURRENTLY bylaunching two separate pgbench runs concurrently. This was needed sothat 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'sbetter in part because the test code is shorter and more readable, butmostly because it automatically scales things to launch an appropriatenumber of CICs relative to the number of INSERT transactions.As committed, typically half to three-quarters of the CIC transactionswere pointless because the INSERT transactions had already stopped.In passing, remove background_pgbench, which was added to supportthese tests and isn't needed anymore. We can always put it backif we find a use for it later.Back-patch to v12; older pgbench versions lack theconditional-execution features needed for this method.Tom Lane and Andrey BorodinDiscussion:https://postgr.es/m/139687.1635277318@sss.pgh.pa.us
1 parent0a75e11 commit5a4b8a8

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::Moretests=>4;
12+
use Test::Moretests=>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::Moretests=>6;
12+
use Test::Moretests=>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
@@ -1816,55 +1816,6 @@ sub pgbench
18161816

18171817
=pod
18181818
1819-
=item$node->background_pgbench($opts, $files, \$stdout, $timer) => harness
1820-
1821-
InvokeB<pgbench> and return an IPC::Run harness object. The process's stdin
1822-
is empty, and its stdout and stderr go to the $stdout scalar reference. This
1823-
allows the caller to act on other parts of the system whileB<pgbench> is
1824-
running. Errors fromB<pgbench> are the caller's problem.
1825-
1826-
The specified timer object is attached to the harness, as well. It's caller's
1827-
responsibility to select the timeout length, and to restart the timer after
1828-
each command if the timeout is per-command.
1829-
1830-
Be sure to "finish" the harness when done with it.
1831-
1832-
=over
1833-
1834-
=item$opts
1835-
1836-
Options as a string to be split on spaces.
1837-
1838-
=item$files
1839-
1840-
Reference to filename/contents dictionary.
1841-
1842-
=back
1843-
1844-
=cut
1845-
1846-
subbackground_pgbench
1847-
{
1848-
my ($self,$opts,$files,$stdout,$timer) =@_;
1849-
1850-
my@cmd =
1851-
('pgbench',split(/\s+/,$opts),$self->_pgbench_make_files($files));
1852-
1853-
local$ENV{PGHOST} =$self->host;
1854-
local$ENV{PGPORT} =$self->port;
1855-
1856-
my$stdin ="";
1857-
# IPC::Run would otherwise append to existing contents:
1858-
$$stdout =""ifref($stdout);
1859-
1860-
my$harness = IPC::Run::start \@cmd,'<', \$stdin,'>',$stdout,'2>&1',
1861-
$timer;
1862-
1863-
return$harness;
1864-
}
1865-
1866-
=pod
1867-
18681819
=item$node->poll_query_until($dbname, $query [, $expected ])
18691820
18701821
RunB<$query> repeatedly, until it returns theB<$expected> result

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp