aboutsummaryrefslogtreecommitdiff
path: root/src/backend/executor/execMain.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2020-01-28 17:26:37 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2020-01-28 17:26:37 -0500
commit01d9676a53af075feb42d2f83ddb8c61186812fc (patch)
treee6249b49e4eedf7ccc2f4b1f3dbaa60a0c92aad1 /src/backend/executor/execMain.c
parent30012a04a6c8127397a8ab71e160d9c7e7fbe874 (diff)
downloadpostgresql-01d9676a53af075feb42d2f83ddb8c61186812fc.tar.gz
postgresql-01d9676a53af075feb42d2f83ddb8c61186812fc.zip
Fix dangling pointer in EvalPlanQual machinery.
EvalPlanQualStart() supposed that it could re-use the relsubs_rowmark and relsubs_done arrays from a prior instantiation. But since they are allocated in the es_query_cxt of the recheckestate, that's just wrong; EvalPlanQualEnd() will blow away that storage. Therefore we were using storage that could have been reallocated to something else, causing all sorts of havoc. I think this was modeled on the old code's handling of es_epqTupleSlot, but since the code was anyway clearing the arrays at re-use, there's clearly no expectation of importing any outside state. So it's just a dubious savings of a couple of pallocs, which is negligible compared to setting up a new planstate tree. Therefore, just allocate the arrays always. (I moved the allocations slightly for readability.) In principle this bug could cause a problem whenever EPQ rechecks are needed in more than one target table of a ModifyTable plan node. In practice it seems not quite so easy to trigger as that; I couldn't readily duplicate a crash with a partitioned target table, for instance. That's probably down to incidental choices about when to free or reallocate stuff. The added isolation test case does seem to reliably show an assertion failure, though. Per report from Oleksii Kliukin. Back-patch to v12 where the bug was introduced (evidently by commit 3fb307bc4). Discussion: https://postgr.es/m/EEF05F66-2871-4786-992B-5F45C92FEE2E@hintbits.com
Diffstat (limited to 'src/backend/executor/execMain.c')
-rw-r--r--src/backend/executor/execMain.c34
1 files changed, 11 insertions, 23 deletions
diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c
index 3a9ce992a3b..ee5c3a60ff3 100644
--- a/src/backend/executor/execMain.c
+++ b/src/backend/executor/execMain.c
@@ -2888,32 +2888,12 @@ EvalPlanQualStart(EPQState *epqstate, Plan *planTree)
}
/*
- * These arrays are reused across different plans set with
- * EvalPlanQualSetPlan(), which is safe because they all use the same
- * parent EState. Therefore we can reuse if already allocated.
- */
- if (epqstate->relsubs_rowmark == NULL)
- {
- Assert(epqstate->relsubs_done == NULL);
- epqstate->relsubs_rowmark = (ExecAuxRowMark **)
- palloc0(rtsize * sizeof(ExecAuxRowMark *));
- epqstate->relsubs_done = (bool *)
- palloc0(rtsize * sizeof(bool));
- }
- else
- {
- Assert(epqstate->relsubs_done != NULL);
- memset(epqstate->relsubs_rowmark, 0,
- rtsize * sizeof(ExecAuxRowMark *));
- memset(epqstate->relsubs_done, 0,
- rtsize * sizeof(bool));
- }
-
- /*
* Build an RTI indexed array of rowmarks, so that
* EvalPlanQualFetchRowMark() can efficiently access the to be fetched
* rowmark.
*/
+ epqstate->relsubs_rowmark = (ExecAuxRowMark **)
+ palloc0(rtsize * sizeof(ExecAuxRowMark *));
foreach(l, epqstate->arowMarks)
{
ExecAuxRowMark *earm = (ExecAuxRowMark *) lfirst(l);
@@ -2922,6 +2902,12 @@ EvalPlanQualStart(EPQState *epqstate, Plan *planTree)
}
/*
+ * Initialize per-relation EPQ tuple states to not-fetched.
+ */
+ epqstate->relsubs_done = (bool *)
+ palloc0(rtsize * sizeof(bool));
+
+ /*
* Initialize the private state information for all the nodes in the part
* of the plan tree we need to run. This opens files, allocates storage
* and leaves us ready to start processing tuples.
@@ -2989,7 +2975,9 @@ EvalPlanQualEnd(EPQState *epqstate)
FreeExecutorState(estate);
/* Mark EPQState idle */
+ epqstate->origslot = NULL;
epqstate->recheckestate = NULL;
epqstate->recheckplanstate = NULL;
- epqstate->origslot = NULL;
+ epqstate->relsubs_rowmark = NULL;
+ epqstate->relsubs_done = NULL;
}