diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c index 878e8e9ea13..6f467c65c10 100644 --- a/src/backend/executor/execMain.c +++ b/src/backend/executor/execMain.c @@ -1727,8 +1727,12 @@ ExecutePlan(EState *estate, */ if (TupIsNull(slot)) { - /* Allow nodes to release or shut down resources. */ - (void) ExecShutdownNode(planstate); + /* + * If we know we won't need to back up, we can release + * resources at this point. + */ + if (!(estate->es_top_eflags & EXEC_FLAG_BACKWARD)) + (void) ExecShutdownNode(planstate); break; } @@ -1774,8 +1778,12 @@ ExecutePlan(EState *estate, current_tuple_count++; if (numberTuples && numberTuples == current_tuple_count) { - /* Allow nodes to release or shut down resources. */ - (void) ExecShutdownNode(planstate); + /* + * If we know we won't need to back up, we can release + * resources at this point. + */ + if (!(estate->es_top_eflags & EXEC_FLAG_BACKWARD)) + (void) ExecShutdownNode(planstate); break; } } diff --git a/src/backend/executor/nodeLimit.c b/src/backend/executor/nodeLimit.c index cf1851e235f..6e74bb23d06 100644 --- a/src/backend/executor/nodeLimit.c +++ b/src/backend/executor/nodeLimit.c @@ -134,8 +134,14 @@ ExecLimit(PlanState *pstate) node->position - node->offset >= node->count) { node->lstate = LIMIT_WINDOWEND; - /* Allow nodes to release or shut down resources. */ - (void) ExecShutdownNode(outerPlan); + + /* + * If we know we won't need to back up, we can release + * resources at this point. + */ + if (!(node->ps.state->es_top_eflags & EXEC_FLAG_BACKWARD)) + (void) ExecShutdownNode(outerPlan); + return NULL; }