From 4c0c33365e61a451402c27beff177552fcbda4f0 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 093ad22f5..f7b9eabf7 100644 --- a/src/backend/distributed/executor/adaptive_executor.c +++ b/src/backend/distributed/executor/adaptive_executor.c @@ -1638,8 +1638,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) { @@ -1656,16 +1656,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; @@ -1731,8 +1739,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); } @@ -1744,7 +1760,11 @@ RunDistributedExecution(DistributedExecution *execution) */ UnclaimAllSessionConnections(execution->sessionList); - FreeWaitEventSet(execution->waitEventSet); + if (execution->waitEventSet != NULL) + { + FreeWaitEventSet(execution->waitEventSet); + execution->waitEventSet = NULL; + } PG_RE_THROW(); }