pgsql: Allow indexscans on partial hash indexes with implied quals.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Allow indexscans on partial hash indexes with implied quals.
Date: 2025-11-27 18:10:22
Message-ID: E1vOgRh-001hFp-2F@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Allow indexscans on partial hash indexes with implied quals.

Normally, if a WHERE clause is implied by the predicate of a partial
index, we drop that clause from the set of quals used with the index,
since it's redundant to test it if we're scanning that index.
However, if it's a hash index (or any !amoptionalkey index), this
could result in dropping all available quals for the index's first
key, preventing us from generating an indexscan.

It's fair to question the practical usefulness of this case. Since
hash only supports equality quals, the situation could only arise
if the index's predicate is "WHERE indexkey = constant", implying
that the index contains only one hash value, which would make hash
a really poor choice of index type. However, perhaps there are
other !amoptionalkey index AMs out there with which such cases are
more plausible.

To fix, just don't filter the candidate indexquals this way if
the index is !amoptionalkey. That's a bit hokey because it may
result in testing quals we didn't need to test, but to do it
more accurately we'd have to redundantly identify which candidate
quals are actually usable with the index, something we don't know
at this early stage of planning. Doesn't seem worth the effort.

Reported-by: Sergei Glukhov <s(dot)glukhov(at)postgrespro(dot)ru>
Author: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Reviewed-by: David Rowley <dgrowleyml(at)gmail(dot)com>
Discussion: https://postgr.es/m/e200bf38-6b45-446a-83fd-48617211feff@postgrespro.ru
Backpatch-through: 14

Branch
------
REL_16_STABLE

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

Modified Files
--------------
src/backend/optimizer/path/indxpath.c | 10 ++++++++++
src/test/regress/expected/hash_index.out | 21 +++++++++++++++++++++
src/test/regress/sql/hash_index.sql | 13 +++++++++++++
3 files changed, 44 insertions(+)

Browse pgsql-committers by date

  From Date Subject
Next Message Michael Paquier 2025-11-27 22:40:11 pgsql: Add routines for marking buffers dirty efficiently
Previous Message Fujii Masao 2025-11-27 14:33:22 pgsql: doc: Fix misleading synopsis for CREATE/ALTER PUBLICATION.