From 4e427ba697cacb1cb94ab048d8a72d2e5a885a50 Mon Sep 17 00:00:00 2001 From: Zhijie Hou Date: Sat, 28 Nov 2026 10:57:47 +0800 Subject: [PATCH v1] Fix unrecognized scankey subtype error with SP-GiST exclusion constraints. When building ScanKeys to probe an index for conflicting tuples, check_exclusion_or_unique_constraint() passed InvalidOid for sk_subtype. While B-tree, Hash, and range GiST operator classes treat an invalid subtype as the operator class input type, the SP-GiST quadtree consistent functions shared by box_ops and poly_ops switch directly on sk_subtype (BOXOID vs. POLYGONOID) to extract the query bounding box and determine whether a lossy recheck is required. Consequently, enforcing an SP-GiST exclusion constraint on a box or polygon column failed with "ERROR: unrecognized scankey subtype: 0". Fix this by passing the index column's operator class input type (index->rd_opcintype[i]) as sk_subtype in ScanKeyEntryInitialize(), matching the ScanKey contract for index scans. Author: Zhijie Hou Backpatch-through: 14 --- src/backend/executor/execIndexing.c | 2 +- src/test/regress/expected/constraints.out | 19 +++++++++++++++++++ src/test/regress/sql/constraints.sql | 16 ++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/backend/executor/execIndexing.c b/src/backend/executor/execIndexing.c index 02ca46b5cb3..322d495df9b 100644 --- a/src/backend/executor/execIndexing.c +++ b/src/backend/executor/execIndexing.c @@ -797,7 +797,7 @@ check_exclusion_or_unique_constraint(Relation heap, Relation index, isnull[i] ? SK_ISNULL | SK_SEARCHNULL : 0, i + 1, constr_strats[i], - InvalidOid, + index->rd_opcintype[i], index_collations[i], constr_procs[i], values[i]); diff --git a/src/test/regress/expected/constraints.out b/src/test/regress/expected/constraints.out index dede6106a49..77a33944527 100644 --- a/src/test/regress/expected/constraints.out +++ b/src/test/regress/expected/constraints.out @@ -894,6 +894,25 @@ DETAIL: Key (c1, (c2::circle))=(<(0,0),5>, <(0,0),5>) conflicts with key (c1, ( -- try reindexing an existing constraint REINDEX INDEX circles_c1_c2_excl; DROP TABLE circles; +-- Check SP-GiST exclusion constraints (both exact box_ops and lossy poly_ops) +CREATE TABLE spgist_excl ( + b box, + p polygon, + EXCLUDE USING spgist (b WITH &&), + EXCLUDE USING spgist (p WITH &&) +); +INSERT INTO spgist_excl VALUES ('((0,0),(1,1))', '((0,0),(0,3),(3,0))'); +-- succeed: polygon bounding boxes overlap, but the polygons themselves do not +INSERT INTO spgist_excl VALUES ('((2,2),(3,3))', '((1,3),(3,3),(3,1))'); +-- fail, box overlaps +INSERT INTO spgist_excl VALUES ('((0.5,0.5),(1.5,1.5))', '((10,10),(10,11),(11,10))'); +ERROR: conflicting key value violates exclusion constraint "spgist_excl_b_excl" +DETAIL: Key (b)=((1.5,1.5),(0.5,0.5)) conflicts with existing key (b)=((1,1),(0,0)). +-- fail, polygon overlaps +INSERT INTO spgist_excl VALUES ('((4,4),(5,5))', '((0,0),(0,1),(1,0))'); +ERROR: conflicting key value violates exclusion constraint "spgist_excl_p_excl" +DETAIL: Key (p)=(((0,0),(0,1),(1,0))) conflicts with existing key (p)=(((0,0),(0,3),(3,0))). +DROP TABLE spgist_excl; -- Check deferred exclusion constraint CREATE TABLE deferred_excl ( f1 int, diff --git a/src/test/regress/sql/constraints.sql b/src/test/regress/sql/constraints.sql index adb55040c71..b55f565ac09 100644 --- a/src/test/regress/sql/constraints.sql +++ b/src/test/regress/sql/constraints.sql @@ -649,6 +649,22 @@ REINDEX INDEX circles_c1_c2_excl; DROP TABLE circles; +-- Check SP-GiST exclusion constraints (both exact box_ops and lossy poly_ops) +CREATE TABLE spgist_excl ( + b box, + p polygon, + EXCLUDE USING spgist (b WITH &&), + EXCLUDE USING spgist (p WITH &&) +); +INSERT INTO spgist_excl VALUES ('((0,0),(1,1))', '((0,0),(0,3),(3,0))'); +-- succeed: polygon bounding boxes overlap, but the polygons themselves do not +INSERT INTO spgist_excl VALUES ('((2,2),(3,3))', '((1,3),(3,3),(3,1))'); +-- fail, box overlaps +INSERT INTO spgist_excl VALUES ('((0.5,0.5),(1.5,1.5))', '((10,10),(10,11),(11,10))'); +-- fail, polygon overlaps +INSERT INTO spgist_excl VALUES ('((4,4),(5,5))', '((0,0),(0,1),(1,0))'); +DROP TABLE spgist_excl; + -- Check deferred exclusion constraint CREATE TABLE deferred_excl ( -- 2.54.0 (Apple Git-157)