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

Commit 2078176

Browse files
committed
Fix incorrect checking of deferred exclusion constraint after a HOT update.
If a row that potentially violates a deferred exclusion constraint is HOT-updated later in the same transaction, the exclusion constraint would be reported as violated when the check finally occurs, even if the row(s) the new row originally conflicted with have since been removed. This happened because the wrong TID was passed to check_exclusion_constraint(), causing the live HOT-updated row to be seen as a conflicting row rather than recognized as the row-under-test. Per bug #13148 from Evan Martin. It's been broken since exclusion constraints were invented, so back-patch to all supported branches.
1 parent b4d4ce1 commit 2078176

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

src/backend/commands/constraint.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,10 @@ unique_key_recheck(PG_FUNCTION_ARGS)
8989
* because this trigger gets queued only in response to index insertions;
9090
* which means it does not get queued for HOT updates. The row we are
9191
* called for might now be dead, but have a live HOT child, in which case
92-
* we still need to make the check. Therefore we have to use
93-
* heap_hot_search, not just HeapTupleSatisfiesVisibility as is done in
94-
* the comparable test in RI_FKey_check.
92+
* we still need to make the check --- effectively, we're applying the
93+
* check against the live child row, although we can use the values from
94+
* this row since by definition all columns of interest to us are the
95+
* same.
9596
*
9697
* This might look like just an optimization, because the index AM will
9798
* make this identical test before throwing an error. But it's actually
@@ -159,7 +160,9 @@ unique_key_recheck(PG_FUNCTION_ARGS)
159160
{
160161
/*
161162
* Note: this is not a real insert; it is a check that the index entry
162-
* that has already been inserted is unique.
163+
* that has already been inserted is unique. Passing t_self is
164+
* correct even if t_self is now dead, because that is the TID the
165+
* index will know about.
163166
*/
164167
index_insert(indexRel, values, isnull, &(new_row->t_self),
165168
trigdata->tg_relation, UNIQUE_CHECK_EXISTING);
@@ -168,10 +171,12 @@ unique_key_recheck(PG_FUNCTION_ARGS)
168171
{
169172
/*
170173
* For exclusion constraints we just do the normal check, but now it's
171-
* okay to throw error.
174+
* okay to throw error. In the HOT-update case, we must use the live
175+
* HOT child's TID here, else check_exclusion_constraint will think
176+
* the child is a conflict.
172177
*/
173178
check_exclusion_constraint(trigdata->tg_relation, indexRel, indexInfo,
174-
&(new_row->t_self), values, isnull,
179+
&tmptid, values, isnull,
175180
estate, false);
176181
}
177182

src/test/regress/input/constraints.source

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ DROP TABLE circles;
467467

468468
CREATE TABLE deferred_excl (
469469
f1 int,
470+
f2 int,
470471
CONSTRAINT deferred_excl_con EXCLUDE (f1 WITH =) INITIALLY DEFERRED
471472
);
472473

@@ -482,6 +483,15 @@ INSERT INTO deferred_excl VALUES(3);
482483
INSERT INTO deferred_excl VALUES(3); -- no fail here
483484
COMMIT; -- should fail here
484485

486+
-- bug #13148: deferred constraint versus HOT update
487+
BEGIN;
488+
INSERT INTO deferred_excl VALUES(2, 1); -- no fail here
489+
DELETE FROM deferred_excl WHERE f1 = 2 AND f2 IS NULL; -- remove old row
490+
UPDATE deferred_excl SET f2 = 2 WHERE f1 = 2;
491+
COMMIT; -- should not fail
492+
493+
SELECT * FROM deferred_excl;
494+
485495
ALTER TABLE deferred_excl DROP CONSTRAINT deferred_excl_con;
486496

487497
-- This should fail, but worth testing because of HOT updates

src/test/regress/output/constraints.source

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ DROP TABLE circles;
634634
-- Check deferred exclusion constraint
635635
CREATE TABLE deferred_excl (
636636
f1 int,
637+
f2 int,
637638
CONSTRAINT deferred_excl_con EXCLUDE (f1 WITH =) INITIALLY DEFERRED
638639
);
639640
INSERT INTO deferred_excl VALUES(1);
@@ -654,6 +655,19 @@ INSERT INTO deferred_excl VALUES(3); -- no fail here
654655
COMMIT; -- should fail here
655656
ERROR: conflicting key value violates exclusion constraint "deferred_excl_con"
656657
DETAIL: Key (f1)=(3) conflicts with existing key (f1)=(3).
658+
-- bug #13148: deferred constraint versus HOT update
659+
BEGIN;
660+
INSERT INTO deferred_excl VALUES(2, 1); -- no fail here
661+
DELETE FROM deferred_excl WHERE f1 = 2 AND f2 IS NULL; -- remove old row
662+
UPDATE deferred_excl SET f2 = 2 WHERE f1 = 2;
663+
COMMIT; -- should not fail
664+
SELECT * FROM deferred_excl;
665+
f1 | f2
666+
----+----
667+
1 |
668+
2 | 2
669+
(2 rows)
670+
657671
ALTER TABLE deferred_excl DROP CONSTRAINT deferred_excl_con;
658672
-- This should fail, but worth testing because of HOT updates
659673
UPDATE deferred_excl SET f1 = 3;

0 commit comments

Comments
 (0)