Skip to content

Commit 2cd0acf

Browse files
author
Amit Kapila
committed
Prohibit shutting down resources if there is a possibility of back up.
Currently, we release the asynchronous resources as soon as it is evident that no more rows will be needed e.g. when a Limit is filled. This can be problematic especially for custom and foreign scans where we can scan backward. Fix that by disallowing the shutting down of resources in such cases. Reported-by: Robert Haas Analysed-by: Robert Haas and Amit Kapila Author: Amit Kapila Reviewed-by: Robert Haas Backpatch-through: 9.6 where this code was introduced Discussion: https://postgr.es/m/86137f17-1dfb-42f9-7421-82fd786b04a1@anayrat.info
1 parent 07172d5 commit 2cd0acf

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/backend/executor/execMain.c

+12-4
Original file line numberDiff line numberDiff line change
@@ -1726,8 +1726,12 @@ ExecutePlan(EState *estate,
17261726
*/
17271727
if (TupIsNull(slot))
17281728
{
1729-
/* Allow nodes to release or shut down resources. */
1730-
(void) ExecShutdownNode(planstate);
1729+
/*
1730+
* If we know we won't need to back up, we can release
1731+
* resources at this point.
1732+
*/
1733+
if (!(estate->es_top_eflags & EXEC_FLAG_BACKWARD))
1734+
(void) ExecShutdownNode(planstate);
17311735
break;
17321736
}
17331737

@@ -1773,8 +1777,12 @@ ExecutePlan(EState *estate,
17731777
current_tuple_count++;
17741778
if (numberTuples && numberTuples == current_tuple_count)
17751779
{
1776-
/* Allow nodes to release or shut down resources. */
1777-
(void) ExecShutdownNode(planstate);
1780+
/*
1781+
* If we know we won't need to back up, we can release
1782+
* resources at this point.
1783+
*/
1784+
if (!(estate->es_top_eflags & EXEC_FLAG_BACKWARD))
1785+
(void) ExecShutdownNode(planstate);
17781786
break;
17791787
}
17801788
}

src/backend/executor/nodeLimit.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,14 @@ ExecLimit(PlanState *pstate)
134134
node->position - node->offset >= node->count)
135135
{
136136
node->lstate = LIMIT_WINDOWEND;
137-
/* Allow nodes to release or shut down resources. */
138-
(void) ExecShutdownNode(outerPlan);
137+
138+
/*
139+
* If we know we won't need to back up, we can release
140+
* resources at this point.
141+
*/
142+
if (!(node->ps.state->es_top_eflags & EXEC_FLAG_BACKWARD))
143+
(void) ExecShutdownNode(outerPlan);
144+
139145
return NULL;
140146
}
141147

0 commit comments

Comments
 (0)