Thread: pgsql: Fix index-only scan plans, take 2.

pgsql: Fix index-only scan plans, take 2.

From
Tom Lane
Date:
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

Branch
------
REL_14_STABLE

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

Modified Files
--------------
src/backend/commands/explain.c           |  2 +-
src/backend/executor/nodeIndexonlyscan.c |  8 ++--
src/backend/nodes/copyfuncs.c            |  1 +
src/backend/nodes/outfuncs.c             |  1 +
src/backend/nodes/readfuncs.c            |  1 +
src/backend/optimizer/plan/createplan.c  | 80 +++++++++++---------------------
src/backend/optimizer/plan/setrefs.c     | 27 ++++++++++-
src/backend/optimizer/plan/subselect.c   |  2 +
src/include/nodes/execnodes.h            |  4 +-
src/include/nodes/plannodes.h            | 22 +++++++--
src/test/regress/expected/gist.out       | 31 +++++++++++++
src/test/regress/sql/gist.sql            | 11 +++++
12 files changed, 124 insertions(+), 66 deletions(-)