1
0
mirror of https://github.com/postgres/postgres.git synced 2025-12-19 17:02:53 +03:00

Fix ON CONFLICT with REINDEX CONCURRENTLY and partitions

When planning queries with ON CONFLICT on partitioned tables, the
indexes to consider as arbiters for each partition are determined based
on those found in the parent table.  However, it's possible for an index
on a partition to be reindexed, and in that case, the auxiliary indexes
created on the partition must be considered as arbiters as well; failing
to do that may result in spurious "duplicate key" errors given
sufficient bad luck.

We fix that in this commit by matching every index that doesn't have a
parent to each initially-determined arbiter index.  Every unparented
matching index is considered an additional arbiter index.

Closely related to the fixes in bc32a12e0d and 2bc7e886fc, and for
identical reasons, not backpatched (for now) even though it's a
longstanding issue.

Author: Mihail Nikalayeu <mihailnikalayeu@gmail.com>
Reviewed-by: Álvaro Herrera <alvherre@kurilemu.de>
Discussion: https://postgr.es/m/CANtu0ojXmqjmEzp-=aJSxjsdE76iAsRgHBoK0QtYHimb_mEfsg@mail.gmail.com
This commit is contained in:
Álvaro Herrera
2025-12-02 13:51:19 +01:00
parent 4f941d432b
commit 90eae926ab
5 changed files with 506 additions and 21 deletions

View File

@@ -18,9 +18,10 @@ ISOLATION = basic \
inplace \
syscache-update-pruned \
index-concurrently-upsert \
index-concurrently-upsert-predicate \
reindex-concurrently-upsert \
reindex-concurrently-upsert-on-constraint \
index-concurrently-upsert-predicate
reindex-concurrently-upsert-partitioned
TAP_TESTS = 1

View File

@@ -0,0 +1,238 @@
Parsed test spec with 4 sessions
starting permutation: s3_setup_wait_before_set_dead s3_start_reindex s1_start_upsert s4_wakeup_to_set_dead s2_start_upsert s4_wakeup_s1 s4_wakeup_s2
injection_points_attach
-----------------------
(1 row)
injection_points_attach
-----------------------
(1 row)
injection_points_set_local
--------------------------
(1 row)
step s3_setup_wait_before_set_dead:
SELECT injection_points_attach('reindex-relation-concurrently-before-set-dead', 'wait');
injection_points_attach
-----------------------
(1 row)
step s3_start_reindex:
REINDEX INDEX CONCURRENTLY test.tbl_partition_pkey;
<waiting ...>
step s1_start_upsert:
INSERT INTO test.tbl VALUES (13, now()) ON CONFLICT (i) DO UPDATE SET updated_at = now();
<waiting ...>
step s4_wakeup_to_set_dead:
SELECT injection_points_detach('reindex-relation-concurrently-before-set-dead');
SELECT injection_points_wakeup('reindex-relation-concurrently-before-set-dead');
injection_points_detach
-----------------------
(1 row)
injection_points_wakeup
-----------------------
(1 row)
step s2_start_upsert:
INSERT INTO test.tbl VALUES (13, now()) ON CONFLICT (i) DO UPDATE SET updated_at = now();
<waiting ...>
step s4_wakeup_s1:
SELECT injection_points_detach('check-exclusion-or-unique-constraint-no-conflict');
SELECT injection_points_wakeup('check-exclusion-or-unique-constraint-no-conflict');
injection_points_detach
-----------------------
(1 row)
injection_points_wakeup
-----------------------
(1 row)
step s1_start_upsert: <... completed>
step s4_wakeup_s2:
SELECT injection_points_detach('exec-insert-before-insert-speculative');
SELECT injection_points_wakeup('exec-insert-before-insert-speculative');
injection_points_detach
-----------------------
(1 row)
injection_points_wakeup
-----------------------
(1 row)
step s2_start_upsert: <... completed>
step s3_start_reindex: <... completed>
starting permutation: s3_setup_wait_before_swap s3_start_reindex s1_start_upsert s4_wakeup_to_swap s2_start_upsert s4_wakeup_s2 s4_wakeup_s1
injection_points_attach
-----------------------
(1 row)
injection_points_attach
-----------------------
(1 row)
injection_points_set_local
--------------------------
(1 row)
step s3_setup_wait_before_swap:
SELECT injection_points_attach('reindex-relation-concurrently-before-swap', 'wait');
injection_points_attach
-----------------------
(1 row)
step s3_start_reindex:
REINDEX INDEX CONCURRENTLY test.tbl_partition_pkey;
<waiting ...>
step s1_start_upsert:
INSERT INTO test.tbl VALUES (13, now()) ON CONFLICT (i) DO UPDATE SET updated_at = now();
<waiting ...>
step s4_wakeup_to_swap:
SELECT injection_points_detach('reindex-relation-concurrently-before-swap');
SELECT injection_points_wakeup('reindex-relation-concurrently-before-swap');
injection_points_detach
-----------------------
(1 row)
injection_points_wakeup
-----------------------
(1 row)
step s2_start_upsert:
INSERT INTO test.tbl VALUES (13, now()) ON CONFLICT (i) DO UPDATE SET updated_at = now();
<waiting ...>
step s4_wakeup_s2:
SELECT injection_points_detach('exec-insert-before-insert-speculative');
SELECT injection_points_wakeup('exec-insert-before-insert-speculative');
injection_points_detach
-----------------------
(1 row)
injection_points_wakeup
-----------------------
(1 row)
step s4_wakeup_s1:
SELECT injection_points_detach('check-exclusion-or-unique-constraint-no-conflict');
SELECT injection_points_wakeup('check-exclusion-or-unique-constraint-no-conflict');
injection_points_detach
-----------------------
(1 row)
injection_points_wakeup
-----------------------
(1 row)
step s1_start_upsert: <... completed>
step s2_start_upsert: <... completed>
step s3_start_reindex: <... completed>
starting permutation: s3_setup_wait_before_set_dead s3_start_reindex s1_start_upsert s2_start_upsert s4_wakeup_s1 s4_wakeup_to_set_dead s4_wakeup_s2
injection_points_attach
-----------------------
(1 row)
injection_points_attach
-----------------------
(1 row)
injection_points_set_local
--------------------------
(1 row)
step s3_setup_wait_before_set_dead:
SELECT injection_points_attach('reindex-relation-concurrently-before-set-dead', 'wait');
injection_points_attach
-----------------------
(1 row)
step s3_start_reindex:
REINDEX INDEX CONCURRENTLY test.tbl_partition_pkey;
<waiting ...>
step s1_start_upsert:
INSERT INTO test.tbl VALUES (13, now()) ON CONFLICT (i) DO UPDATE SET updated_at = now();
<waiting ...>
step s2_start_upsert:
INSERT INTO test.tbl VALUES (13, now()) ON CONFLICT (i) DO UPDATE SET updated_at = now();
<waiting ...>
step s4_wakeup_s1:
SELECT injection_points_detach('check-exclusion-or-unique-constraint-no-conflict');
SELECT injection_points_wakeup('check-exclusion-or-unique-constraint-no-conflict');
injection_points_detach
-----------------------
(1 row)
injection_points_wakeup
-----------------------
(1 row)
step s1_start_upsert: <... completed>
step s4_wakeup_to_set_dead:
SELECT injection_points_detach('reindex-relation-concurrently-before-set-dead');
SELECT injection_points_wakeup('reindex-relation-concurrently-before-set-dead');
injection_points_detach
-----------------------
(1 row)
injection_points_wakeup
-----------------------
(1 row)
step s4_wakeup_s2:
SELECT injection_points_detach('exec-insert-before-insert-speculative');
SELECT injection_points_wakeup('exec-insert-before-insert-speculative');
injection_points_detach
-----------------------
(1 row)
injection_points_wakeup
-----------------------
(1 row)
step s2_start_upsert: <... completed>
step s3_start_reindex: <... completed>

View File

@@ -49,9 +49,10 @@ tests += {
'inplace',
'syscache-update-pruned',
'index-concurrently-upsert',
'index-concurrently-upsert-predicate',
'reindex-concurrently-upsert',
'reindex-concurrently-upsert-on-constraint',
'index-concurrently-upsert-predicate',
'reindex-concurrently-upsert-partitioned',
],
'runningcheck': false, # see syscache-update-pruned
# Some tests wait for all snapshots, so avoid parallel execution

View File

@@ -0,0 +1,113 @@
# This test verifies INSERT ON CONFLICT DO UPDATE behavior on partitioned
# tables concurrent with REINDEX CONCURRENTLY.
#
# - s1: UPSERT a tuple
# - s2: UPSERT the same tuple
# - s3: concurrently REINDEX the primary key index
#
# - s4: controls concurrency via injection points
setup
{
CREATE EXTENSION injection_points;
CREATE SCHEMA test;
CREATE TABLE test.tbl(i int primary key, updated_at timestamp) PARTITION BY RANGE (i);
CREATE TABLE test.tbl_partition PARTITION OF test.tbl
FOR VALUES FROM (0) TO (10000)
WITH (parallel_workers = 0);
}
teardown
{
DROP SCHEMA test CASCADE;
DROP EXTENSION injection_points;
}
session s1
setup
{
SELECT injection_points_set_local();
SELECT injection_points_attach('check-exclusion-or-unique-constraint-no-conflict', 'wait');
}
step s1_start_upsert
{
INSERT INTO test.tbl VALUES (13, now()) ON CONFLICT (i) DO UPDATE SET updated_at = now();
}
session s2
setup
{
SELECT injection_points_set_local();
SELECT injection_points_attach('exec-insert-before-insert-speculative', 'wait');
}
step s2_start_upsert
{
INSERT INTO test.tbl VALUES (13, now()) ON CONFLICT (i) DO UPDATE SET updated_at = now();
}
session s3
setup
{
SELECT injection_points_set_local();
}
step s3_setup_wait_before_set_dead
{
SELECT injection_points_attach('reindex-relation-concurrently-before-set-dead', 'wait');
}
step s3_setup_wait_before_swap
{
SELECT injection_points_attach('reindex-relation-concurrently-before-swap', 'wait');
}
step s3_start_reindex
{
REINDEX INDEX CONCURRENTLY test.tbl_partition_pkey;
}
session s4
step s4_wakeup_to_swap
{
SELECT injection_points_detach('reindex-relation-concurrently-before-swap');
SELECT injection_points_wakeup('reindex-relation-concurrently-before-swap');
}
step s4_wakeup_s1
{
SELECT injection_points_detach('check-exclusion-or-unique-constraint-no-conflict');
SELECT injection_points_wakeup('check-exclusion-or-unique-constraint-no-conflict');
}
step s4_wakeup_s2
{
SELECT injection_points_detach('exec-insert-before-insert-speculative');
SELECT injection_points_wakeup('exec-insert-before-insert-speculative');
}
step s4_wakeup_to_set_dead
{
SELECT injection_points_detach('reindex-relation-concurrently-before-set-dead');
SELECT injection_points_wakeup('reindex-relation-concurrently-before-set-dead');
}
permutation
s3_setup_wait_before_set_dead
s3_start_reindex(s1_start_upsert, s2_start_upsert)
s1_start_upsert
s4_wakeup_to_set_dead
s2_start_upsert(s1_start_upsert)
s4_wakeup_s1
s4_wakeup_s2
permutation
s3_setup_wait_before_swap
s3_start_reindex(s1_start_upsert, s2_start_upsert)
s1_start_upsert
s4_wakeup_to_swap
s2_start_upsert(s1_start_upsert)
s4_wakeup_s2
s4_wakeup_s1
permutation
s3_setup_wait_before_set_dead
s3_start_reindex(s1_start_upsert, s2_start_upsert)
s1_start_upsert
s2_start_upsert(s1_start_upsert)
s4_wakeup_s1
s4_wakeup_to_set_dead
s4_wakeup_s2