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

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix index-only scan plans, take 2.
Date: 2022-01-03 20:42:55
Message-ID: E1n4UAR-0004BE-05@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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_12_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/9c4f38908447c1c6a910e4a16e0644e0ccade204

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 | 26 ++++++++++-
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, 123 insertions(+), 66 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Tom Lane 2022-01-03 21:12:21 pgsql: Handle mixed returnable and non-returnable columns better in IOS
Previous Message Tom Lane 2022-01-03 20:42:54 pgsql: Fix index-only scan plans, take 2.