From eb41a249c9ad0674174c001ab486cbcd371614c8 Mon Sep 17 00:00:00 2001 From: Metin Doslu Date: Fri, 18 Mar 2016 14:46:46 -0700 Subject: [PATCH] Refactor memory context reset --- src/backend/distributed/commands/multi_copy.c | 17 +++++------------ .../worker/worker_partition_protocol.c | 1 + 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/backend/distributed/commands/multi_copy.c b/src/backend/distributed/commands/multi_copy.c index 69a4a2804..4cc3f738c 100644 --- a/src/backend/distributed/commands/multi_copy.c +++ b/src/backend/distributed/commands/multi_copy.c @@ -156,7 +156,6 @@ CitusCopyFrom(CopyStmt *copyStatement, char *completionTag) HTAB *shardConnectionHash = NULL; List *connectionList = NIL; MemoryContext tupleContext = NULL; - MemoryContext outputRowContext = NULL; CopyState copyState = NULL; TupleDesc tupleDescriptor = NULL; uint32 columnCount = 0; @@ -290,22 +289,15 @@ CitusCopyFrom(CopyStmt *copyStatement, char *completionTag) * files. */ tupleContext = AllocSetContextCreate(CurrentMemoryContext, - "COPY FROM Row Memory Context", + "COPY Row Memory Context", ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE); - /* we use outputRowContext to serialize row to send to workers */ - outputRowContext = AllocSetContextCreate(CurrentMemoryContext, - "COPY TO Row Memory Context", - ALLOCSET_DEFAULT_MINSIZE, - ALLOCSET_DEFAULT_INITSIZE, - ALLOCSET_DEFAULT_MAXSIZE); - rowOutputState = (OutputCopyState) palloc0(sizeof(OutputCopyStateData)); rowOutputState->binary = true; rowOutputState->fe_msgbuf = makeStringInfo(); - rowOutputState->rowcontext = outputRowContext; + rowOutputState->rowcontext = tupleContext; columnOutputFunctions = ColumnOutputFunctions(tupleDescriptor, rowOutputState->binary); @@ -919,6 +911,8 @@ ColumnOutputFunctions(TupleDesc rowDescriptor, bool binaryFormat) * and appends the data to the row output state object's message buffer. * This function is modeled after the CopyOneRowTo() function in * commands/copy.c, but only implements a subset of that functionality. + * Note that the caller of this function should reset row memory context + * to not bloat memory usage. */ void OutputRow(Datum *valueArray, bool *isNullArray, TupleDesc rowDescriptor, @@ -928,9 +922,8 @@ OutputRow(Datum *valueArray, bool *isNullArray, TupleDesc rowDescriptor, uint32 columnIndex = 0; uint32 columnCount = 0; - /* reset previous tuple's output data, and the temporary memory context */ + /* reset previous tuple's output data */ resetStringInfo(rowOutputState->fe_msgbuf); - MemoryContextReset(rowOutputState->rowcontext); oldContext = MemoryContextSwitchTo(rowOutputState->rowcontext); diff --git a/src/backend/distributed/worker/worker_partition_protocol.c b/src/backend/distributed/worker/worker_partition_protocol.c index 662fa92f9..1690eeee9 100644 --- a/src/backend/distributed/worker/worker_partition_protocol.c +++ b/src/backend/distributed/worker/worker_partition_protocol.c @@ -823,6 +823,7 @@ FilterAndPartitionTable(const char *filterQuery, FileOutputStreamWrite(partitionFile, rowText); resetStringInfo(rowText); + MemoryContextReset(rowOutputState->rowcontext); } SPI_freetuptable(SPI_tuptable);