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

Commit 4d3d971

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 ea70595 commit 4d3d971

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

src/backend/commands/constraint.c

+11-6
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, false);
176181
}
177182

src/test/regress/input/constraints.source

+10
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ DROP TABLE circles;
456456

457457
CREATE TABLE deferred_excl (
458458
f1 int,
459+
f2 int,
459460
CONSTRAINT deferred_excl_con EXCLUDE (f1 WITH =) INITIALLY DEFERRED
460461
);
461462

@@ -470,6 +471,15 @@ INSERT INTO deferred_excl VALUES(3);
470471
INSERT INTO deferred_excl VALUES(3); -- no fail here
471472
COMMIT; -- should fail here
472473

474+
-- bug #13148: deferred constraint versus HOT update
475+
BEGIN;
476+
INSERT INTO deferred_excl VALUES(2, 1); -- no fail here
477+
DELETE FROM deferred_excl WHERE f1 = 2 AND f2 IS NULL; -- remove old row
478+
UPDATE deferred_excl SET f2 = 2 WHERE f1 = 2;
479+
COMMIT; -- should not fail
480+
481+
SELECT * FROM deferred_excl;
482+
473483
ALTER TABLE deferred_excl DROP CONSTRAINT deferred_excl_con;
474484

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

src/test/regress/output/constraints.source

+14
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ DROP TABLE circles;
620620
-- Check deferred exclusion constraint
621621
CREATE TABLE deferred_excl (
622622
f1 int,
623+
f2 int,
623624
CONSTRAINT deferred_excl_con EXCLUDE (f1 WITH =) INITIALLY DEFERRED
624625
);
625626
INSERT INTO deferred_excl VALUES(1);
@@ -638,6 +639,19 @@ INSERT INTO deferred_excl VALUES(3); -- no fail here
638639
COMMIT; -- should fail here
639640
ERROR: conflicting key value violates exclusion constraint "deferred_excl_con"
640641
DETAIL: Key (f1)=(3) conflicts with existing key (f1)=(3).
642+
-- bug #13148: deferred constraint versus HOT update
643+
BEGIN;
644+
INSERT INTO deferred_excl VALUES(2, 1); -- no fail here
645+
DELETE FROM deferred_excl WHERE f1 = 2 AND f2 IS NULL; -- remove old row
646+
UPDATE deferred_excl SET f2 = 2 WHERE f1 = 2;
647+
COMMIT; -- should not fail
648+
SELECT * FROM deferred_excl;
649+
f1 | f2
650+
----+----
651+
1 |
652+
2 | 2
653+
(2 rows)
654+
641655
ALTER TABLE deferred_excl DROP CONSTRAINT deferred_excl_con;
642656
-- This should fail, but worth testing because of HOT updates
643657
UPDATE deferred_excl SET f1 = 3;

0 commit comments

Comments
 (0)