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 <[email protected]>
Author: Bilal Kahraman <[email protected]>
Discussion: 
https://postgr.es/m/CAPkprsxXAapaJJHYVL=xlnntkdh6szkxoxknedy3zchlqow...@mail.gmail.com
Discussion: 
https://postgr.es/m/cahwvjhhccsjcbfdgfemru_x8n_o0_kn_mmx5g+uanytyxkx...@mail.gmail.com
Discussion: 
https://postgr.es/m/capkprsycowgy4tfu_14ax-3lyls+d4++4a4umgs0qygjw+h...@mail.gmail.com
Backpatch-through: 15

Branch
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/9492bfd0072a6cf3d7db72ff72d35db73e913bad

Modified Files
--------------
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(-)

Reply via email to