Fix index-only scan plans, take 2.
authorTom Lane <[email protected]>
Mon, 3 Jan 2022 20:42:27 +0000 (15:42 -0500)
committerTom Lane <[email protected]>
Mon, 3 Jan 2022 20:42:27 +0000 (15:42 -0500)
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

index 68838ab0df2277474f3ad13de5e3e1681b1f5d54..1b616b4e45036f61f29ee3ad9a7e50fd0781ddb3 100644 (file)
@@ -1393,7 +1393,7 @@ ExplainNode(PlanState *planstate, List *ancestors,
        case T_IndexOnlyScan:
            show_scan_qual(((IndexOnlyScan *) plan)->indexqual,
                           "Index Cond", planstate, ancestors, es);
-           if (((IndexOnlyScan *) plan)->indexqual)
+           if (((IndexOnlyScan *) plan)->recheckqual)
                show_instrumentation_count("Rows Removed by Index Recheck", 2,
                                           planstate, es);
            show_scan_qual(((IndexOnlyScan *) plan)->indexorderby,
index 338429bd589c5c612edf3dadef03265f97885fce..7de5b43f9b7fc7b9e7dddd19883c16ec430548cc 100644 (file)
@@ -208,14 +208,12 @@ IndexOnlyNext(IndexOnlyScanState *node)
 
        /*
         * If the index was lossy, we have to recheck the index quals.
-        * (Currently, this can never happen, but we should support the case
-        * for possible future use, eg with GiST indexes.)
         */
        if (scandesc->xs_recheck)
        {
            econtext->ecxt_scantuple = slot;
            ResetExprContext(econtext);
-           if (!ExecQual(node->indexqual, econtext))
+           if (!ExecQual(node->recheckqual, econtext))
            {
                /* Fails recheck, so drop it and loop back for another */
                InstrCountFiltered2(node, 1);
@@ -524,8 +522,8 @@ ExecInitIndexOnlyScan(IndexOnlyScan *node, EState *estate, int eflags)
     */
    indexstate->ss.ps.qual =
        ExecInitQual(node->scan.plan.qual, (PlanState *) indexstate);
-   indexstate->indexqual =
-       ExecInitQual(node->indexqual, (PlanState *) indexstate);
+   indexstate->recheckqual =
+       ExecInitQual(node->recheckqual, (PlanState *) indexstate);
 
    /*
     * tuple table initialization
index f5355272a7ce3191b560dee99d0935bb04b15fc7..1ed8e7d50a4591aaf2ce715386eba5ea9d84e60c 100644 (file)
@@ -507,6 +507,7 @@ _copyIndexOnlyScan(const IndexOnlyScan *from)
     */
    COPY_SCALAR_FIELD(indexid);
    COPY_NODE_FIELD(indexqual);
+   COPY_NODE_FIELD(recheckqual);
    COPY_NODE_FIELD(indexorderby);
    COPY_NODE_FIELD(indextlist);
    COPY_SCALAR_FIELD(indexorderdir);
index 58371ba68171f38e4b973935e479a1ea672a3a87..0c0b4f64fb4664e7517c214a2cc6598abbb659c4 100644 (file)
@@ -570,6 +570,7 @@ _outIndexOnlyScan(StringInfo str, const IndexOnlyScan *node)
 
    WRITE_OID_FIELD(indexid);
    WRITE_NODE_FIELD(indexqual);
+   WRITE_NODE_FIELD(recheckqual);
    WRITE_NODE_FIELD(indexorderby);
    WRITE_NODE_FIELD(indextlist);
    WRITE_ENUM_FIELD(indexorderdir, ScanDirection);
index 515326ffcd1c17a0d3c91d177e74de110ccf22a6..678d01acec13ce00db4ae3649f92a11b2f3cadc2 100644 (file)
@@ -1767,6 +1767,7 @@ _readIndexOnlyScan(void)
 
    READ_OID_FIELD(indexid);
    READ_NODE_FIELD(indexqual);
+   READ_NODE_FIELD(recheckqual);
    READ_NODE_FIELD(indexorderby);
    READ_NODE_FIELD(indextlist);
    READ_ENUM_FIELD(indexorderdir, ScanDirection);
index 4dd0f4a1c973baa2ca31dead42a83a33b31e27aa..29e5f99193c79a7a395fc6818bef4281a7352d59 100644 (file)
@@ -20,7 +20,6 @@
 #include <math.h>
 
 #include "access/sysattr.h"
-#include "catalog/pg_am.h"
 #include "catalog/pg_class.h"
 #include "foreign/fdwapi.h"
 #include "miscadmin.h"
@@ -173,10 +172,10 @@ static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid,
               ScanDirection indexscandir);
 static IndexOnlyScan *make_indexonlyscan(List *qptlist, List *qpqual,
                   Index scanrelid, Oid indexid,
-                  List *indexqual, List *indexorderby,
+                  List *indexqual, List *recheckqual,
+                  List *indexorderby,
                   List *indextlist,
                   ScanDirection indexscandir);
-static List *make_indexonly_tlist(IndexOptInfo *indexinfo);
 static BitmapIndexScan *make_bitmap_indexscan(Index scanrelid, Oid indexid,
                      List *indexqual,
                      List *indexqualorig);
@@ -565,7 +564,7 @@ create_scan_plan(PlannerInfo *root, Path *best_path, int flags)
        if (best_path->pathtype == T_IndexOnlyScan)
        {
            /* For index-only scan, the preferred tlist is the index's */
-           tlist = copyObject(make_indexonly_tlist(((IndexPath *) best_path)->indexinfo));
+           tlist = copyObject(((IndexPath *) best_path)->indexinfo->indextlist);
 
            /*
             * Transfer sortgroupref data to the replacement tlist, if
@@ -2562,7 +2561,8 @@ create_indexscan_plan(PlannerInfo *root,
    List       *indexquals = best_path->indexquals;
    List       *indexorderbys = best_path->indexorderbys;
    Index       baserelid = best_path->path.parent->relid;
-   Oid         indexoid = best_path->indexinfo->indexoid;
+   IndexOptInfo *indexinfo = best_path->indexinfo;
+   Oid         indexoid = indexinfo->indexoid;
    List       *qpqual;
    List       *stripped_indexquals;
    List       *fixed_indexquals;
@@ -2695,6 +2695,24 @@ create_indexscan_plan(PlannerInfo *root,
        }
    }
 
+   /*
+    * For an index-only scan, we must mark indextlist entries as resjunk if
+    * they are columns that the index AM can't return; this cues setrefs.c to
+    * not generate references to those columns.
+    */
+   if (indexonly)
+   {
+       int         i = 0;
+
+       foreach(l, indexinfo->indextlist)
+       {
+           TargetEntry *indextle = (TargetEntry *) lfirst(l);
+
+           indextle->resjunk = !indexinfo->canreturn[i];
+           i++;
+       }
+   }
+
    /* Finally ready to build the plan node */
    if (indexonly)
        scan_plan = (Scan *) make_indexonlyscan(tlist,
@@ -2702,8 +2720,9 @@ create_indexscan_plan(PlannerInfo *root,
                                                baserelid,
                                                indexoid,
                                                fixed_indexquals,
+                                               stripped_indexquals,
                                                fixed_indexorderbys,
-                                               make_indexonly_tlist(best_path->indexinfo),
+                                               indexinfo->indextlist,
                                                best_path->indexscandir);
    else
        scan_plan = (Scan *) make_indexscan(tlist,
@@ -4914,6 +4933,7 @@ make_indexonlyscan(List *qptlist,
                   Index scanrelid,
                   Oid indexid,
                   List *indexqual,
+                  List *recheckqual,
                   List *indexorderby,
                   List *indextlist,
                   ScanDirection indexscandir)
@@ -4928,6 +4948,7 @@ make_indexonlyscan(List *qptlist,
    node->scan.scanrelid = scanrelid;
    node->indexid = indexid;
    node->indexqual = indexqual;
+   node->recheckqual = recheckqual;
    node->indexorderby = indexorderby;
    node->indextlist = indextlist;
    node->indexorderdir = indexscandir;
@@ -4935,53 +4956,6 @@ make_indexonlyscan(List *qptlist,
    return node;
 }
 
-/*
- * make_indexonly_tlist
- *
- * Construct the indextlist for an IndexOnlyScan plan node.
- * We must replace any column that can't be returned by the index AM
- * with a null Const of the appropriate datatype.  This is necessary
- * to prevent setrefs.c from trying to use the value of such a column,
- * and anyway it makes the indextlist a better representative of what
- * the indexscan will really return.  (We do this here, not where the
- * IndexOptInfo is originally constructed, because earlier planner
- * steps need to know what is in such columns.)
- */
-static List *
-make_indexonly_tlist(IndexOptInfo *indexinfo)
-{
-   List       *result;
-   int         i;
-   ListCell   *lc;
-
-   /* We needn't work hard for the common case of btrees. */
-   if (indexinfo->relam == BTREE_AM_OID)
-       return indexinfo->indextlist;
-
-   result = NIL;
-   i = 0;
-   foreach(lc, indexinfo->indextlist)
-   {
-       TargetEntry *indextle = (TargetEntry *) lfirst(lc);
-
-       if (indexinfo->canreturn[i])
-           result = lappend(result, indextle);
-       else
-       {
-           TargetEntry *newtle = makeNode(TargetEntry);
-           Node       *texpr = (Node *) indextle->expr;
-
-           memcpy(newtle, indextle, sizeof(TargetEntry));
-           newtle->expr = (Expr *) makeNullConst(exprType(texpr),
-                                                 exprTypmod(texpr),
-                                                 exprCollation(texpr));
-           result = lappend(result, newtle);
-       }
-       i++;
-   }
-   return result;
-}
-
 static BitmapIndexScan *
 make_bitmap_indexscan(Index scanrelid,
                      Oid indexid,
index 97ec2f2c4e4b31e60f5793dc9cd060bc30aa0d86..610986221db66a607e8ffcf648e445d235713bd3 100644 (file)
@@ -1015,8 +1015,26 @@ set_indexonlyscan_references(PlannerInfo *root,
                             int rtoffset)
 {
    indexed_tlist *index_itlist;
+   List       *stripped_indextlist;
+   ListCell   *lc;
+
+   /*
+    * Vars in the plan node's targetlist, qual, and recheckqual must only
+    * reference columns that the index AM can actually return.  To ensure
+    * this, remove non-returnable columns (which are marked as resjunk) from
+    * the indexed tlist.  We can just drop them because the indexed_tlist
+    * machinery pays attention to TLE resnos, not physical list position.
+    */
+   stripped_indextlist = NIL;
+   foreach(lc, plan->indextlist)
+   {
+       TargetEntry *indextle = (TargetEntry *) lfirst(lc);
 
-   index_itlist = build_tlist_index(plan->indextlist);
+       if (!indextle->resjunk)
+           stripped_indextlist = lappend(stripped_indextlist, indextle);
+   }
+
+   index_itlist = build_tlist_index(stripped_indextlist);
 
    plan->scan.scanrelid += rtoffset;
    plan->scan.plan.targetlist = (List *)
@@ -1031,6 +1049,12 @@ set_indexonlyscan_references(PlannerInfo *root,
                       index_itlist,
                       INDEX_VAR,
                       rtoffset);
+   plan->recheckqual = (List *)
+       fix_upper_expr(root,
+                      (Node *) plan->recheckqual,
+                      index_itlist,
+                      INDEX_VAR,
+                      rtoffset);
    /* indexqual is already transformed to reference index columns */
    plan->indexqual = fix_scan_list(root, plan->indexqual, rtoffset);
    /* indexorderby is already transformed to reference index columns */
index 09e6aa471fda73fcc97c14fd51eebbbcfc691cd3..e5b5692b5a7da7705bd1eddfa0a44dcc64992884 100644 (file)
@@ -2077,6 +2077,8 @@ finalize_plan(PlannerInfo *root, Plan *plan,
        case T_IndexOnlyScan:
            finalize_primnode((Node *) ((IndexOnlyScan *) plan)->indexqual,
                              &context);
+           finalize_primnode((Node *) ((IndexOnlyScan *) plan)->recheckqual,
+                             &context);
            finalize_primnode((Node *) ((IndexOnlyScan *) plan)->indexorderby,
                              &context);
 
index 10e39013353298d8e221d94ae47eff89dfbbc783..3b3cd34a8a078db853168e4b062816011fa5bd29 100644 (file)
@@ -1222,7 +1222,7 @@ typedef struct IndexScanState
 /* ----------------
  *  IndexOnlyScanState information
  *
- *     indexqual          execution state for indexqual expressions
+ *     recheckqual        execution state for recheckqual expressions
  *     ScanKeys           Skey structures for index quals
  *     NumScanKeys        number of ScanKeys
  *     OrderByKeys        Skey structures for index ordering operators
@@ -1241,7 +1241,7 @@ typedef struct IndexScanState
 typedef struct IndexOnlyScanState
 {
    ScanState   ss;             /* its first field is NodeTag */
-   ExprState  *indexqual;
+   ExprState  *recheckqual;
    ScanKey     ioss_ScanKeys;
    int         ioss_NumScanKeys;
    ScanKey     ioss_OrderByKeys;
index b3350e10499bb30d1b1facbee070be93165e9b4a..a873f18d13fcdf6ec83d7131d38310bcce5493d2 100644 (file)
@@ -402,15 +402,28 @@ typedef struct IndexScan
  * index-only scan, in which the data comes from the index not the heap.
  * Because of this, *all* Vars in the plan node's targetlist, qual, and
  * index expressions reference index columns and have varno = INDEX_VAR.
- * Hence we do not need separate indexqualorig and indexorderbyorig lists,
- * since their contents would be equivalent to indexqual and indexorderby.
+ *
+ * We could almost use indexqual directly against the index's output tuple
+ * when rechecking lossy index operators, but that won't work for quals on
+ * index columns that are not retrievable.  Hence, recheckqual is needed
+ * for rechecks: it expresses the same condition as indexqual, but using
+ * only index columns that are retrievable.  (We will not generate an
+ * index-only scan if this is not possible.  An example is that if an
+ * index has table column "x" in a retrievable index column "ind1", plus
+ * an expression f(x) in a non-retrievable column "ind2", an indexable
+ * query on f(x) will use "ind2" in indexqual and f(ind1) in recheckqual.
+ * Without the "ind1" column, an index-only scan would be disallowed.)
+ *
+ * We don't currently need a recheckable equivalent of indexorderby,
+ * because we don't support lossy operators in index ORDER BY.
  *
  * To help EXPLAIN interpret the index Vars for display, we provide
  * indextlist, which represents the contents of the index as a targetlist
  * with one TLE per index column.  Vars appearing in this list reference
  * the base table, and this is the only field in the plan node that may
- * contain such Vars.  Note however that index columns that the AM can't
- * reconstruct are replaced by null Consts in indextlist.
+ * contain such Vars.  Also, for the convenience of setrefs.c, TLEs in
+ * indextlist are marked as resjunk if they correspond to columns that
+ * the index AM cannot reconstruct.
  * ----------------
  */
 typedef struct IndexOnlyScan
@@ -421,6 +434,7 @@ typedef struct IndexOnlyScan
    List       *indexorderby;   /* list of index ORDER BY exprs */
    List       *indextlist;     /* TargetEntry list describing index's cols */
    ScanDirection indexorderdir;    /* forward or backward or don't care */
+   List       *recheckqual;    /* index quals in recheckable form */
 } IndexOnlyScan;
 
 /* ----------------
index f5d378d734570fbfb47c7ea4e3d23dc47302d908..374873b6836a0f1b9443e748c9491a6dd6496a3f 100644 (file)
@@ -248,6 +248,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;
index 9ca4441a114d0c1e37311cc4e6ace6a37c3a98a4..487791bc812b078d7715ac280e0d71bcc16c5fc6 100644 (file)
@@ -125,6 +125,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;