Skip to content

Commit 0d9b0bd

Browse files
author
Antonin Houska
committed
Fixed memory leak in process_tasks().
'task_cxt' should be reset whenever we go back to the 'restart' label. While working on that, simplified retrieval of the current memory context.
1 parent a461a6a commit 0d9b0bd

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed

worker.c

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -982,26 +982,17 @@ process_tasks(MemoryContext task_cxt)
982982
static int next_task = 0;
983983
WorkerTask *task;
984984

985-
initStringInfo(&query);
986-
987985
restart:
986+
MemoryContextReset(task_cxt);
988987
cl_index = NULL;
989988
rel_tbsp = NULL;
990989
ind_tbsps = NULL;
991-
if (tasks)
992-
{
993-
for (i = 0; i < ntasks; i++)
994-
{
995-
TaskDetails *this = &tasks[i];
996-
997-
if (this->ind_tbsps)
998-
pfree(this->ind_tbsps);
999-
}
1000-
pfree(tasks);
1001-
}
1002990
tasks = NULL;
1003991
ntasks = 0;
1004-
resetStringInfo(&query);
992+
993+
oldcxt = MemoryContextSwitchTo(task_cxt);
994+
initStringInfo(&query);
995+
MemoryContextSwitchTo(oldcxt);
1005996

1006997
Assert(MyWorkerTask == NULL);
1007998

@@ -1033,8 +1024,7 @@ process_tasks(MemoryContext task_cxt)
10331024
* Create a single-element array of tasks for further processing.
10341025
*/
10351026
ntasks = 1;
1036-
oldcxt = CurrentMemoryContext;
1037-
MemoryContextSwitchTo(task_cxt);
1027+
oldcxt = MemoryContextSwitchTo(task_cxt);
10381028
tasks = (TaskDetails *) palloc(sizeof(TaskDetails));
10391029

10401030
/*
@@ -1116,8 +1106,7 @@ LIMIT %d", TASK_BATCH_SIZE);
11161106
* Create the tuple descriptor, slot and the task details in a
11171107
* separate memory context, so that it survives the SPI session.
11181108
*/
1119-
oldcxt = CurrentMemoryContext;
1120-
MemoryContextSwitchTo(task_cxt);
1109+
oldcxt = MemoryContextSwitchTo(task_cxt);
11211110
tupdesc = CreateTupleDescCopy(SPI_tuptable->tupdesc);
11221111
#if PG_VERSION_NUM >= 120000
11231112
slot = MakeSingleTupleTableSlot(tupdesc, &TTSOpsHeapTuple);
@@ -1346,7 +1335,7 @@ LIMIT %d", TASK_BATCH_SIZE);
13461335
fmgr_info(outfunc, &fmgrinfo);
13471336
start_ts_str = OutputFunctionCall(&fmgrinfo, TimestampTzGetDatum(start_ts));
13481337
/* Make sure the string survives TopTransactionContext. */
1349-
MemoryContextSwitchTo(task_cxt);
1338+
oldcxt = MemoryContextSwitchTo(task_cxt);
13501339
start_ts_str = pstrdup(start_ts_str);
13511340
MemoryContextSwitchTo(oldcxt);
13521341
CommitTransactionCommand();

0 commit comments

Comments
 (0)