1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

Avoid race in RelationBuildDesc() affecting CREATE INDEX CONCURRENTLY.

CIC and REINDEX CONCURRENTLY assume backends see their catalog changes
no later than each backend's next transaction start.  That failed to
hold when a backend absorbed a relevant invalidation in the middle of
running RelationBuildDesc() on the CIC index.  Queries that use the
resulting index can silently fail to find rows.  Fix this for future
index builds by making RelationBuildDesc() loop until it finishes
without accepting a relevant invalidation.  It may be necessary to
reindex to recover from past occurrences; REINDEX CONCURRENTLY suffices.
Back-patch to 9.6 (all supported versions).

Noah Misch and Andrey Borodin, reviewed (in earlier versions) by Andres
Freund.

Discussion: https://postgr.es/m/20210730022548.GA1940096@gust.leadboat.com
This commit is contained in:
Noah Misch
2021-10-23 18:36:38 -07:00
parent fb1aa489e4
commit 0869e53d3a
8 changed files with 331 additions and 5 deletions

View File

@ -9,6 +9,8 @@ PGFILEDESC = "amcheck - function for verifying relation integrity"
REGRESS = check check_btree
TAP_TESTS = 1
ifdef USE_PGXS
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)

View File

@ -0,0 +1,78 @@
# Copyright (c) 2021, PostgreSQL Global Development Group
# Test CREATE INDEX CONCURRENTLY with concurrent modifications
use strict;
use warnings;
use Config;
use PostgresNode;
use TestLib;
use Test::More tests => 4;
my ($node, $result);
#
# Test set-up
#
$node = get_new_node('CIC_test');
$node->init;
$node->append_conf('postgresql.conf', 'lock_timeout = 180000');
$node->start;
$node->safe_psql('postgres', q(CREATE EXTENSION amcheck));
$node->safe_psql('postgres', q(CREATE TABLE tbl(i int)));
$node->safe_psql('postgres', q(CREATE INDEX idx ON tbl(i)));
#
# Stress CIC with pgbench
#
# Run background pgbench with CIC. We cannot mix-in this script into single
# pgbench: CIC will deadlock with itself occasionally.
my $pgbench_out = '';
my $pgbench_timer = IPC::Run::timeout(180);
my $pgbench_h = $node->background_pgbench(
'--no-vacuum --client=1 --transactions=200',
{
'002_pgbench_concurrent_cic' => q(
DROP INDEX CONCURRENTLY idx;
CREATE INDEX CONCURRENTLY idx ON tbl(i);
SELECT bt_index_check('idx',true);
)
},
\$pgbench_out,
$pgbench_timer);
# Run pgbench.
$node->pgbench(
'--no-vacuum --client=5 --transactions=200',
0,
[qr{actually processed}],
[qr{^$}],
'concurrent INSERTs',
{
'002_pgbench_concurrent_transaction' => q(
BEGIN;
INSERT INTO tbl VALUES(0);
COMMIT;
),
'002_pgbench_concurrent_transaction_savepoints' => q(
BEGIN;
SAVEPOINT s1;
INSERT INTO tbl VALUES(0);
COMMIT;
)
});
$pgbench_h->pump_nb;
$pgbench_h->finish();
$result =
($Config{osname} eq "MSWin32")
? ($pgbench_h->full_results)[0]
: $pgbench_h->result(0);
is($result, 0, "pgbench with CIC works");
# done
$node->stop;
done_testing();