From fc87b8da4468094191df6c5be1944f851a709e52 Mon Sep 17 00:00:00 2001 From: Marco Slot Date: Sun, 21 Jul 2019 01:57:49 +0200 Subject: [PATCH] Avoid creating a redundant event set at the start --- .../distributed/executor/adaptive_executor.c | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/src/backend/distributed/executor/adaptive_executor.c b/src/backend/distributed/executor/adaptive_executor.c index 13b3e790a..4d805ceae 100644 --- a/src/backend/distributed/executor/adaptive_executor.c +++ b/src/backend/distributed/executor/adaptive_executor.c @@ -1629,8 +1629,8 @@ RunDistributedExecution(DistributedExecution *execution) /* additional 2 is for postmaster and latch */ int eventSetSize = list_length(execution->sessionList) + 2; - execution->waitEventSet = BuildWaitEventSet(execution->sessionList); - events = palloc0(eventSetSize * sizeof(WaitEvent)); + /* always (re)build the wait event set the first time */ + execution->connectionSetChanged = true; while (execution->unfinishedTaskCount > 0 && !cancellationReceived) { @@ -1647,16 +1647,24 @@ RunDistributedExecution(DistributedExecution *execution) if (execution->connectionSetChanged) { - FreeWaitEventSet(execution->waitEventSet); + if (execution->waitEventSet != NULL) + { + FreeWaitEventSet(execution->waitEventSet); + execution->waitEventSet = NULL; + } + + if (events != NULL) + { + /* + * The execution might take a while, so explicitly free at this point + * because we don't need anymore. + */ + pfree(events); + events = NULL; + } execution->waitEventSet = BuildWaitEventSet(execution->sessionList); - /* - * The execution might take a while, so explicitly free at this point - * because we don't need anymore. - */ - pfree(events); - /* recalculate (and allocate) since the sessions have changed */ eventSetSize = list_length(execution->sessionList) + 2; @@ -1722,8 +1730,16 @@ RunDistributedExecution(DistributedExecution *execution) } } - pfree(events); - FreeWaitEventSet(execution->waitEventSet); + if (events != NULL) + { + pfree(events); + } + + if (execution->waitEventSet != NULL) + { + FreeWaitEventSet(execution->waitEventSet); + execution->waitEventSet = NULL; + } CleanUpSessions(execution); } @@ -1735,7 +1751,11 @@ RunDistributedExecution(DistributedExecution *execution) */ UnclaimAllSessionConnections(execution->sessionList); - FreeWaitEventSet(execution->waitEventSet); + if (execution->waitEventSet != NULL) + { + FreeWaitEventSet(execution->waitEventSet); + execution->waitEventSet = NULL; + } PG_RE_THROW(); }