Set scan direction appropriately for SubPlans (bug #15336)
authorAndrew Gierth <rhodiumtoad@postgresql.org>
Fri, 17 Aug 2018 14:04:26 +0000 (15:04 +0100)
committerAndrew Gierth <rhodiumtoad@postgresql.org>
Fri, 17 Aug 2018 15:19:10 +0000 (16:19 +0100)
When executing a SubPlan in an expression, the EState's direction
field was left alone, resulting in an attempt to execute the subplan
backwards if it was encountered during a backwards scan of a cursor.
Also, though much less likely, it was possible to reach the execution
of an InitPlan while in backwards-scan state.

Repair by saving/restoring estate->es_direction and forcing forward
scan mode in the relevant places.

Backpatch all the way, since this has been broken since 8.3 (prior to
commit c7ff7663e, SubPlans had their own EStates rather than sharing
the parent plan's, so there was no confusion over scan direction).

Per bug #15336 reported by Vladimir Baranoff; analysis and patch by
me, review by Tom Lane.

Discussion: https://postgr.es/m/153449812167.1304.1741624125628126322@wrigleys.postgresql.org

src/backend/executor/nodeSubplan.c
src/test/regress/expected/subselect.out
src/test/regress/sql/subselect.sql

index 2cf169f956620c5720c8d0a29c6f1f7b205b309e..b4883c4f8dfb3a3ed6789477edba4a1331691b52 100644 (file)
@@ -73,6 +73,9 @@ ExecSubPlan(SubPlanState *node,
            ExprDoneCond *isDone)
 {
    SubPlan    *subplan = (SubPlan *) node->xprstate.expr;
+   EState     *estate = node->planstate->state;
+   ScanDirection dir = estate->es_direction;
+   Datum       retval;
 
    /* Set default values for result flags: non-null, not a set result */
    *isNull = false;
@@ -85,11 +88,19 @@ ExecSubPlan(SubPlanState *node,
    if (subplan->setParam != NIL && subplan->subLinkType != MULTIEXPR_SUBLINK)
        elog(ERROR, "cannot set parent params from subquery");
 
+   /* Force forward-scan mode for evaluation */
+   estate->es_direction = ForwardScanDirection;
+
    /* Select appropriate evaluation strategy */
    if (subplan->useHashTable)
-       return ExecHashSubPlan(node, econtext, isNull);
+       retval = ExecHashSubPlan(node, econtext, isNull);
    else
-       return ExecScanSubPlan(node, econtext, isNull);
+       retval = ExecScanSubPlan(node, econtext, isNull);
+
+   /* restore scan direction */
+   estate->es_direction = dir;
+
+   return retval;
 }
 
 /*
@@ -944,6 +955,8 @@ ExecSetParamPlan(SubPlanState *node, ExprContext *econtext)
    SubPlan    *subplan = (SubPlan *) node->xprstate.expr;
    PlanState  *planstate = node->planstate;
    SubLinkType subLinkType = subplan->subLinkType;
+   EState     *estate = planstate->state;
+   ScanDirection dir = estate->es_direction;
    MemoryContext oldcontext;
    TupleTableSlot *slot;
    ListCell   *pvar;
@@ -957,6 +970,12 @@ ExecSetParamPlan(SubPlanState *node, ExprContext *econtext)
    if (subLinkType == CTE_SUBLINK)
        elog(ERROR, "CTE subplans should not be executed via ExecSetParamPlan");
 
+   /*
+    * Enforce forward scan direction regardless of caller. It's hard but not
+    * impossible to get here in backward scan, so make it work anyway.
+    */
+   estate->es_direction = ForwardScanDirection;
+
    /* Initialize ArrayBuildStateAny in caller's context, if needed */
    if (subLinkType == ARRAY_SUBLINK)
        astate = initArrayResultAny(subplan->firstColType,
@@ -1110,6 +1129,9 @@ ExecSetParamPlan(SubPlanState *node, ExprContext *econtext)
    }
 
    MemoryContextSwitchTo(oldcontext);
+
+   /* restore scan direction */
+   estate->es_direction = dir;
 }
 
 /*
index bf076f3df38db70bd5c7c2098ba7a28f2116ea45..22d8e65c41e0c0f89d48d3e141b53407bbcff0fb 100644 (file)
@@ -933,3 +933,20 @@ select nextval('ts1');
       11
 (1 row)
 
+--
+-- Ensure that backward scan direction isn't propagated into
+-- expression subqueries (bug #15336)
+--
+begin;
+declare c1 scroll cursor for
+ select * from generate_series(1,4) i
+  where i <> all (values (2),(3));
+move forward all in c1;
+fetch backward all in c1;
+ i 
+---
+ 4
+ 1
+(2 rows)
+
+commit;
index 74c4ef745d4dd97231d8427e9655cfda80397dbd..5fb0f2b84f702e35ae24dc73736a5a5dc2813610 100644 (file)
@@ -496,3 +496,19 @@ select * from
   order by 1;
 
 select nextval('ts1');
+
+--
+-- Ensure that backward scan direction isn't propagated into
+-- expression subqueries (bug #15336)
+--
+
+begin;
+
+declare c1 scroll cursor for
+ select * from generate_series(1,4) i
+  where i <> all (values (2),(3));
+
+move forward all in c1;
+fetch backward all in c1;
+
+commit;