Allow indexscans on partial hash indexes with implied quals.
authorTom Lane <[email protected]>
Thu, 27 Nov 2025 18:09:59 +0000 (13:09 -0500)
committerTom Lane <[email protected]>
Thu, 27 Nov 2025 18:09:59 +0000 (13:09 -0500)
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 <[email protected]>
Author: Tom Lane <[email protected]>
Reviewed-by: David Rowley <[email protected]>
Discussion: https://postgr.es/m/e200bf38-6b45-446a-83fd-48617211feff@postgrespro.ru
Backpatch-through: 14

src/backend/optimizer/path/indxpath.c
src/test/regress/expected/create_index.out
src/test/regress/expected/hash_index.out
src/test/regress/sql/create_index.sql
src/test/regress/sql/hash_index.sql

index 8c395be3456c05529c66494c845e7ed70f9a86c3..2ba687c32569f93ffe88fc521cb6c68b7bac5cda 100644 (file)
@@ -3424,6 +3424,16 @@ check_index_predicates(PlannerInfo *root, RelOptInfo *rel)
        if (is_target_rel)
            continue;
 
+       /*
+        * If index is !amoptionalkey, also leave indrestrictinfo as set
+        * above.  Otherwise we risk removing all quals for the first index
+        * key and then not being able to generate an indexscan at all.  It
+        * would be better to be more selective, but we've not yet identified
+        * which if any of the quals match the first index key.
+        */
+       if (!index->amoptionalkey)
+           continue;
+
        /* Else compute indrestrictinfo as the non-implied quals */
        index->indrestrictinfo = NIL;
        foreach(lcr, rel->baserestrictinfo)
index 74507ebd21793371e6a604b03ce606114fad4449..be80774dd4fbe965a6e7a29aec0f8256bd3cb8cd 100644 (file)
@@ -1271,6 +1271,8 @@ CREATE INDEX hash_i4_index ON hash_i4_heap USING hash (random int4_ops);
 CREATE INDEX hash_name_index ON hash_name_heap USING hash (random name_ops);
 CREATE INDEX hash_txt_index ON hash_txt_heap USING hash (random text_ops);
 CREATE INDEX hash_f8_index ON hash_f8_heap USING hash (random float8_ops) WITH (fillfactor=60);
+CREATE INDEX hash_i4_partial_index ON hash_i4_heap USING hash (seqno)
+  WHERE seqno = 9999;
 CREATE UNLOGGED TABLE unlogged_hash_table (id int4);
 CREATE INDEX unlogged_hash_index ON unlogged_hash_table USING hash (id int4_ops);
 DROP TABLE unlogged_hash_table;
index e23de21b41c36a98b092d8ec3591e3ea06409bf4..19bb94921fd62e00bb220d05045d4560ffd1612b 100644 (file)
@@ -82,6 +82,25 @@ SELECT * FROM hash_f8_heap
 -------+--------
 (0 rows)
 
+--
+-- partial hash index
+--
+EXPLAIN (COSTS OFF)
+SELECT * FROM hash_i4_heap
+   WHERE seqno = 9999;
+                       QUERY PLAN                       
+--------------------------------------------------------
+ Index Scan using hash_i4_partial_index on hash_i4_heap
+   Index Cond: (seqno = 9999)
+(2 rows)
+
+SELECT * FROM hash_i4_heap
+   WHERE seqno = 9999;
+ seqno |   random   
+-------+------------
+  9999 | 1227676208
+(1 row)
+
 --
 -- hash index
 -- grep '^90[^0-9]' hashovfl.data
index fc73301679ac1c12c8d6ade92d87feff53e2fcba..b4c49405ae21ef2c548d63f59481ee59f0cab562 100644 (file)
@@ -374,6 +374,9 @@ CREATE INDEX hash_txt_index ON hash_txt_heap USING hash (random text_ops);
 
 CREATE INDEX hash_f8_index ON hash_f8_heap USING hash (random float8_ops) WITH (fillfactor=60);
 
+CREATE INDEX hash_i4_partial_index ON hash_i4_heap USING hash (seqno)
+  WHERE seqno = 9999;
+
 CREATE UNLOGGED TABLE unlogged_hash_table (id int4);
 CREATE INDEX unlogged_hash_index ON unlogged_hash_table USING hash (id int4_ops);
 DROP TABLE unlogged_hash_table;
index 4d1aa020a9670b51c709311d9920b68b54264fae..cf05ec02751c610085c8ad4e811a607c1b135305 100644 (file)
@@ -55,6 +55,16 @@ SELECT * FROM hash_f8_heap
 SELECT * FROM hash_f8_heap
    WHERE hash_f8_heap.random = '88888888'::float8;
 
+--
+-- partial hash index
+--
+EXPLAIN (COSTS OFF)
+SELECT * FROM hash_i4_heap
+   WHERE seqno = 9999;
+
+SELECT * FROM hash_i4_heap
+   WHERE seqno = 9999;
+
 --
 -- hash index
 -- grep '^90[^0-9]' hashovfl.data