pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter Geoghegan <pg@bowt.ie>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix SP-GiST index-only scans with non-returnable keys.
Date: Thu, 19 Nov 2026 21:10:31 +0000
Message-ID: <E1xU9Oo-00000000QoE-45vN@gemulon.postgresql.org> (raw)

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

Branch
------
REL_15_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/e48b96a4fcd17deea664d0b35f39e73ad37f1a01

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



view thread (6+ messages)

Message-ID: <E1xU9Oo-00000000QoE-45vN@gemulon.postgresql.org>
Permalink:  ../E1xU9Oo-00000000QoE-45vN@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xU9Oo-00000000QoE-45vN@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: pg@bowt.ie
  Subject: Re: pgsql: Fix SP-GiST index-only scans with non-returnable keys.
  In-Reply-To: <E1xU9Oo-00000000QoE-45vN@gemulon.postgresql.org>
  References: <E1xU9Oo-00000000QoE-45vN@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox