postgres.git / summary / log / commit / refs
commit 9492bfd0072a6cf3d7db72ff72d35db73e913bad
Author: Peter Geoghegan <pg@bowt.ie>
Date: Thu Nov 19 21:10:00 2026 +0000
Fix SP-GiST index-only scans with non-returnable keys.
SP-GiST index-only scans always set returnData for the key column's
consistent functions, even when its opclass cannot return its keys. An
index-only scan that used such an opclass would therefore return
whatever leaf_consistent gave it as the key column of the tuple that it
reconstructs. This could lead to the scan returning the wrong values
for INCLUDE columns. It could also cause "can't happen" errors and hard
crashes.
Fix by setting returnData only when the opclass can return its keys.
Otherwise the scan just uses NULL as the key column of the heap tuple
that holds reconstructed values. This is safe because the index AM API
allows that for a column that amcanreturn reports as not returnable, and
the planner never lets an index-only scan reference such a column.
The problem has been reachable since commit ff963b393 added poly_ops,
which is still the only in-core opclass that cannot return its keys.
Commit 09c1c6ab4 made it much easier to hit when it added support for
INCLUDE columns to SP-GiST.
Reported-by: Rui Zhao <zhaorui126@gmail.com>
Author: Bilal Kahraman <kahramannbilal@gmail.com>
Discussion: https://postgr.es/m/CAPkprsxXAapaJJHYVL=xLnntKDH6SZKxoxkNeDy3ZcHLqowfsw@mail.gmail.com
Discussion: https://postgr.es/m/CAHWVJhHccsjcBFdgFemRu_x8n_o0_kn_MMx5g+uAnYtYxKxO5w@mail.gmail.com
Discussion: https://postgr.es/m/CAPkprsycOwGY4tfU_14aX-3LyLS+D4++4a4umGS0QYgjW+hzMg@mail.gmail.com
Backpatch-through: 15
src/backend/access/spgist/spgscan.c | 9 +++++----
src/test/regress/expected/create_index_spgist.out | 20 ++++++++++++++++++++
src/test/regress/sql/create_index_spgist.sql | 11 +++++++++++
3 files changed, 36 insertions(+), 4 deletions(-)
diff --git a/src/backend/access/spgist/spgscan.c b/src/backend/access/spgist/spgscan.c
index 03293a7816e..f01542b2a3d 100644
--- a/src/backend/access/spgist/spgscan.c
+++ b/src/backend/access/spgist/spgscan.c
@@ -477,7 +477,7 @@ spgNewHeapItem(SpGistScanOpaque so, int level, SpGistLeafTuple leafTuple,
*/
if (so->want_itup)
{
- item->value = isnull ? (Datum) 0 :
+ item->value = (isnull || !so->state.config.canReturnData) ? (Datum) 0 :
datumCopy(leafValue, so->state.attType.attbyval,
so->state.attType.attlen);
@@ -549,7 +549,7 @@ spgLeafTest(SpGistScanOpaque so, SpGistSearchItem *item,
in.reconstructedValue = item->value;
in.traversalValue = item->traversalValue;
in.level = item->level;
- in.returnData = so->want_itup;
+ in.returnData = so->want_itup && so->state.config.canReturnData;
in.leafDatum = SGLTDATUM(leafTuple, &so->state);
out.leafValue = (Datum) 0;
@@ -617,7 +617,7 @@ spgInitInnerConsistentIn(spgInnerConsistentIn *in,
in->traversalMemoryContext = so->traversalCxt;
in->traversalValue = item->traversalValue;
in->level = item->level;
- in->returnData = so->want_itup;
+ in->returnData = so->want_itup && so->state.config.canReturnData;
in->allTheSame = innerTuple->allTheSame;
in->hasPrefix = (innerTuple->prefixSize > 0);
in->prefixDatum = SGITDATUM(innerTuple, &so->state);
@@ -1012,8 +1012,9 @@ storeGettuple(SpGistScanOpaque so, ItemPointer heapPtr,
spgDeformLeafTuple(leafTuple, so->state.leafTupDesc,
leafDatums, leafIsnulls, isnull);
+ /* A key the opclass can't reconstruct is returned as NULL */
leafDatums[spgKeyColumn] = leafValue;
- leafIsnulls[spgKeyColumn] = isnull;
+ leafIsnulls[spgKeyColumn] = isnull || !so->state.config.canReturnData;
so->reconTups[so->nPtrs] = heap_form_tuple(so->reconTupDesc,
leafDatums,
diff --git a/src/test/regress/expected/create_index_spgist.out b/src/test/regress/expected/create_index_spgist.out
index 5c04df9c01b..795f1e52eb3 100644
--- a/src/test/regress/expected/create_index_spgist.out
+++ b/src/test/regress/expected/create_index_spgist.out
@@ -584,6 +584,26 @@ SELECT p, dist FROM quad_point_tbl_ord_seq1 ORDER BY p <-> '0,0' LIMIT 10;
(10 rows)
RESET extra_float_digits;
+-- test index-only scan of an included column when the opclass (poly_ops)
+-- cannot return the key column
+CREATE TEMP TABLE spgist_poly_include (id int, p polygon);
+INSERT INTO spgist_poly_include
+ VALUES (7, polygon(box(point(1,1), point(2,2))));
+CREATE INDEX spgist_poly_include_idx ON spgist_poly_include
+ USING spgist (p) INCLUDE (id);
+EXPLAIN (COSTS OFF)
+SELECT id FROM spgist_poly_include;
+ QUERY PLAN
+----------------------------------------------------------------------
+ Index Only Scan using spgist_poly_include_idx on spgist_poly_include
+(1 row)
+
+SELECT id FROM spgist_poly_include;
+ id
+----
+ 7
+(1 row)
+
-- check ORDER BY distance to NULL
SELECT (SELECT p FROM kd_point_tbl ORDER BY p <-> pt, p <-> '0,0' LIMIT 1)
FROM (VALUES (point '1,2'), (NULL), ('1234,5678')) pts(pt);
diff --git a/src/test/regress/sql/create_index_spgist.sql b/src/test/regress/sql/create_index_spgist.sql
index 660bfc61930..c608e5444d8 100644
--- a/src/test/regress/sql/create_index_spgist.sql
+++ b/src/test/regress/sql/create_index_spgist.sql
@@ -234,6 +234,17 @@ SELECT p, dist FROM quad_point_tbl_ord_seq1 ORDER BY p <-> '0,0' LIMIT 10;
SELECT p, dist FROM quad_point_tbl_ord_seq1 ORDER BY p <-> '0,0' LIMIT 10;
RESET extra_float_digits;
+-- test index-only scan of an included column when the opclass (poly_ops)
+-- cannot return the key column
+CREATE TEMP TABLE spgist_poly_include (id int, p polygon);
+INSERT INTO spgist_poly_include
+ VALUES (7, polygon(box(point(1,1), point(2,2))));
+CREATE INDEX spgist_poly_include_idx ON spgist_poly_include
+ USING spgist (p) INCLUDE (id);
+EXPLAIN (COSTS OFF)
+SELECT id FROM spgist_poly_include;
+SELECT id FROM spgist_poly_include;
+
-- check ORDER BY distance to NULL
SELECT (SELECT p FROM kd_point_tbl ORDER BY p <-> pt, p <-> '0,0' LIMIT 1)
FROM (VALUES (point '1,2'), (NULL), ('1234,5678')) pts(pt);
[parent: 1e8a3033a985]