🌐 AI搜索 & 代理 主页
Skip to content

Commit e79b276

Browse files
committed
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.glukhov@postgrespro.ru> Author: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: David Rowley <dgrowleyml@gmail.com> Discussion: https://postgr.es/m/e200bf38-6b45-446a-83fd-48617211feff@postgrespro.ru Backpatch-through: 14
1 parent d797766 commit e79b276

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/backend/optimizer/path/indxpath.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3353,6 +3353,16 @@ check_index_predicates(PlannerInfo *root, RelOptInfo *rel)
33533353
if (is_target_rel)
33543354
continue;
33553355

3356+
/*
3357+
* If index is !amoptionalkey, also leave indrestrictinfo as set
3358+
* above. Otherwise we risk removing all quals for the first index
3359+
* key and then not being able to generate an indexscan at all. It
3360+
* would be better to be more selective, but we've not yet identified
3361+
* which if any of the quals match the first index key.
3362+
*/
3363+
if (!index->amoptionalkey)
3364+
continue;
3365+
33563366
/* Else compute indrestrictinfo as the non-implied quals */
33573367
index->indrestrictinfo = NIL;
33583368
foreach(lcr, rel->baserestrictinfo)

src/test/regress/expected/hash_index.out

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ CREATE INDEX hash_name_index ON hash_name_heap USING hash (random name_ops);
4040
CREATE INDEX hash_txt_index ON hash_txt_heap USING hash (random text_ops);
4141
CREATE INDEX hash_f8_index ON hash_f8_heap USING hash (random float8_ops)
4242
WITH (fillfactor=60);
43+
CREATE INDEX hash_i4_partial_index ON hash_i4_heap USING hash (seqno)
44+
WHERE seqno = 9999;
4345
--
4446
-- Also try building functional, expressional, and partial indexes on
4547
-- tables that already contain data.
@@ -131,6 +133,25 @@ SELECT * FROM hash_f8_heap
131133
-------+--------
132134
(0 rows)
133135

136+
--
137+
-- partial hash index
138+
--
139+
EXPLAIN (COSTS OFF)
140+
SELECT * FROM hash_i4_heap
141+
WHERE seqno = 9999;
142+
QUERY PLAN
143+
--------------------------------------------------------
144+
Index Scan using hash_i4_partial_index on hash_i4_heap
145+
Index Cond: (seqno = 9999)
146+
(2 rows)
147+
148+
SELECT * FROM hash_i4_heap
149+
WHERE seqno = 9999;
150+
seqno | random
151+
-------+------------
152+
9999 | 1227676208
153+
(1 row)
154+
134155
--
135156
-- hash index
136157
-- grep '^90[^0-9]' hashovfl.data

src/test/regress/sql/hash_index.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ CREATE INDEX hash_txt_index ON hash_txt_heap USING hash (random text_ops);
5353
CREATE INDEX hash_f8_index ON hash_f8_heap USING hash (random float8_ops)
5454
WITH (fillfactor=60);
5555

56+
CREATE INDEX hash_i4_partial_index ON hash_i4_heap USING hash (seqno)
57+
WHERE seqno = 9999;
58+
5659
--
5760
-- Also try building functional, expressional, and partial indexes on
5861
-- tables that already contain data.
@@ -117,6 +120,16 @@ SELECT * FROM hash_f8_heap
117120
SELECT * FROM hash_f8_heap
118121
WHERE hash_f8_heap.random = '88888888'::float8;
119122

123+
--
124+
-- partial hash index
125+
--
126+
EXPLAIN (COSTS OFF)
127+
SELECT * FROM hash_i4_heap
128+
WHERE seqno = 9999;
129+
130+
SELECT * FROM hash_i4_heap
131+
WHERE seqno = 9999;
132+
120133
--
121134
-- hash index
122135
-- grep '^90[^0-9]' hashovfl.data

0 commit comments

Comments
 (0)