Fix index-only scan plans, take 2.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 3 Jan 2022 20:42:27 +0000 (15:42 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 3 Jan 2022 20:42:27 +0000 (15:42 -0500)
commit9a3ddeb519e8d91b670fa923619b212a9670389c
tree152d16414709b9e695cd42229c42b5630be65547
parent4b160492b9b8345747dfcad468eb89e9ccc0a876
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
12 files changed:
src/backend/commands/explain.c
src/backend/executor/nodeIndexonlyscan.c
src/backend/nodes/copyfuncs.c
src/backend/nodes/outfuncs.c
src/backend/nodes/readfuncs.c
src/backend/optimizer/plan/createplan.c
src/backend/optimizer/plan/setrefs.c
src/backend/optimizer/plan/subselect.c
src/include/nodes/execnodes.h
src/include/nodes/plannodes.h
src/test/regress/expected/gist.out
src/test/regress/sql/gist.sql