1
0
mirror of https://github.com/postgres/postgres.git synced 2025-09-02 04:21:28 +03:00

Fix index-only scan plans, take 2.

Commit 4ace45677 failed to fix the problem fully, because the
same issue of attempting to fetch a non-returnable index column
can occur when rechecking the indexqual after using a lossy index
operator.  Moreover, it broke EXPLAIN for such indexquals (which
indicates a gap in our test cases :-().

Revert the code changes of 4ace45677 in favor of adding a new field
to struct IndexOnlyScan, containing a version of the indexqual that
can be executed against the index-returned tuple without using any
non-returnable columns.  (The restrictions imposed by check_index_only
guarantee this is possible, although we may have to recompute indexed
expressions.)  Support construction of that during setrefs.c
processing by marking IndexOnlyScan.indextlist entries as resjunk
if they can't be returned, rather than removing them entirely.
(We could alternatively require setrefs.c to look up the IndexOptInfo
again, but abusing resjunk this way seems like a reasonably safe way
to avoid needing to do that.)

This solution isn't great from an API-stability standpoint: if there
are any extensions out there that build IndexOnlyScan structs directly,
they'll be broken in the next minor releases.  However, only a very
invasive extension would be likely to do such a thing.  There's no
change in the Path representation, so typical planner extensions
shouldn't have a problem.

As before, back-patch to all supported branches.

Discussion: https://postgr.es/m/3179992.1641150853@sss.pgh.pa.us
Discussion: https://postgr.es/m/17350-b5bdcf476e5badbb@postgresql.org
This commit is contained in:
Tom Lane
2022-01-03 15:42:27 -05:00
parent 52d50261db
commit d228af79d0
12 changed files with 124 additions and 66 deletions

View File

@@ -340,6 +340,37 @@ where p <@ box(point(5, 5), point(5.3, 5.3));
<(5.3,5.3),1>
(7 rows)
-- Similarly, test that index rechecks involving a non-returnable column
-- are done correctly.
explain (verbose, costs off)
select p from gist_tbl where circle(p,1) @> circle(point(0,0),0.95);
QUERY PLAN
---------------------------------------------------------------------------------------
Index Only Scan using gist_tbl_multi_index on public.gist_tbl
Output: p
Index Cond: ((circle(gist_tbl.p, '1'::double precision)) @> '<(0,0),0.95>'::circle)
(3 rows)
select p from gist_tbl where circle(p,1) @> circle(point(0,0),0.95);
p
-------
(0,0)
(1 row)
-- This case isn't supported, but it should at least EXPLAIN correctly.
explain (verbose, costs off)
select p from gist_tbl order by circle(p,1) <-> point(0,0) limit 1;
QUERY PLAN
------------------------------------------------------------------------------------
Limit
Output: p, ((circle(p, '1'::double precision) <-> '(0,0)'::point))
-> Index Only Scan using gist_tbl_multi_index on public.gist_tbl
Output: p, (circle(p, '1'::double precision) <-> '(0,0)'::point)
Order By: ((circle(gist_tbl.p, '1'::double precision)) <-> '(0,0)'::point)
(5 rows)
select p from gist_tbl order by circle(p,1) <-> point(0,0) limit 1;
ERROR: lossy distance functions are not supported in index-only scans
-- Clean up
reset enable_seqscan;
reset enable_bitmapscan;

View File

@@ -153,6 +153,17 @@ where p <@ box(point(5, 5), point(5.3, 5.3));
select circle(p,1) from gist_tbl
where p <@ box(point(5, 5), point(5.3, 5.3));
-- Similarly, test that index rechecks involving a non-returnable column
-- are done correctly.
explain (verbose, costs off)
select p from gist_tbl where circle(p,1) @> circle(point(0,0),0.95);
select p from gist_tbl where circle(p,1) @> circle(point(0,0),0.95);
-- This case isn't supported, but it should at least EXPLAIN correctly.
explain (verbose, costs off)
select p from gist_tbl order by circle(p,1) <-> point(0,0) limit 1;
select p from gist_tbl order by circle(p,1) <-> point(0,0) limit 1;
-- Clean up
reset enable_seqscan;
reset enable_bitmapscan;