Skip to content

Commit 447b1c5

Browse files
committed
Add row_ref_equals hook for check_exclusion_or_unique_constraint to work with rowid
1 parent 166431f commit 447b1c5

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/backend/access/heap/heapam_handler.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ heapam_get_row_ref_type(Relation rel)
8383
return ROW_REF_TID;
8484
}
8585

86+
static bool
87+
heapam_row_ref_equals(Relation rel, Datum tupleidDatum1, Datum tupleidDatum2)
88+
{
89+
ItemPointer tupleid1 = DatumGetItemPointer(tupleidDatum1);
90+
ItemPointer tupleid2 = DatumGetItemPointer(tupleidDatum2);
91+
return ItemPointerEquals(tupleid1, tupleid2);
92+
}
93+
8694
static void
8795
heapam_free_rd_amcache(Relation rel)
8896
{
@@ -2979,6 +2987,7 @@ static const TableAmRoutine heapam_methods = {
29792987

29802988
.slot_callbacks = heapam_slot_callbacks,
29812989
.get_row_ref_type = heapam_get_row_ref_type,
2990+
.row_ref_equals = heapam_row_ref_equals,
29822991
.free_rd_amcache = heapam_free_rd_amcache,
29832992

29842993
.scan_begin = heap_beginscan,

src/backend/executor/execIndexing.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,26 @@ check_exclusion_or_unique_constraint(Relation heap, Relation index,
12121212
continue;
12131213
}
12141214
} else {
1215-
/* TODO: Add same thing for rowid if possible */
1215+
Assert(tupleidDatum > 0);
1216+
Pointer rowid = DatumGetPointer(tupleidDatum);
1217+
1218+
if (PointerIsValid(rowid))
1219+
{
1220+
bool isnull;
1221+
Datum existing_rowid;
1222+
1223+
existing_rowid = slot_getsysattr(existing_slot, RowIdAttributeNumber, &isnull);
1224+
Assert(!isnull);
1225+
1226+
if (table_row_ref_equals(heap, tupleidDatum, existing_rowid))
1227+
{
1228+
if (found_self) /* should not happen */
1229+
elog(ERROR, "found self tuple multiple times in index \"%s\"",
1230+
RelationGetRelationName(index));
1231+
found_self = true;
1232+
continue;
1233+
}
1234+
}
12161235
}
12171236

12181237
/*

src/include/access/tableam.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ typedef struct TableAmRoutine
335335
const TupleTableSlotOps *(*slot_callbacks) (Relation rel);
336336

337337
RowRefType (*get_row_ref_type) (Relation rel);
338+
bool (*row_ref_equals)(Relation rel, Datum tupleidDatum1, Datum tupleidDatum2);
338339

339340
void (*free_rd_amcache) (Relation rel);
340341

@@ -2169,6 +2170,19 @@ table_get_row_ref_type(Relation rel)
21692170
return ROW_REF_TID;
21702171
}
21712172

2173+
static inline bool
2174+
table_row_ref_equals(Relation rel, Datum tupleidDatum1, Datum tupleidDatum2)
2175+
{
2176+
if (rel->rd_tableam)
2177+
return rel->rd_tableam->row_ref_equals(rel, tupleidDatum1, tupleidDatum2);
2178+
else
2179+
{
2180+
ItemPointer tupleid1 = DatumGetItemPointer(tupleidDatum1);
2181+
ItemPointer tupleid2 = DatumGetItemPointer(tupleidDatum2);
2182+
return ItemPointerEquals(tupleid1, tupleid2);
2183+
}
2184+
}
2185+
21722186
static inline void
21732187
table_free_rd_amcache(Relation rel)
21742188
{

0 commit comments

Comments
 (0)