Create an executor state, and use its memory context

pull/390/head
Metin Doslu 2016-03-22 14:17:40 -07:00
parent 6ef48e657b
commit 14e71b1b0a
1 changed files with 16 additions and 23 deletions

View File

@ -149,6 +149,9 @@ CitusCopyFrom(CopyStmt *copyStatement, char *completionTag)
RangeVar *relation = copyStatement->relation; RangeVar *relation = copyStatement->relation;
Oid tableId = RangeVarGetRelid(relation, NoLock, false); Oid tableId = RangeVarGetRelid(relation, NoLock, false);
char *relationName = get_rel_name(tableId); char *relationName = get_rel_name(tableId);
EState *executorState = NULL;
MemoryContext executorTupleContext = NULL;
ExprContext *executorExpressionContext = NULL;
List *shardIntervalList = NULL; List *shardIntervalList = NULL;
ListCell *shardIntervalCell = NULL; ListCell *shardIntervalCell = NULL;
char partitionMethod = '\0'; char partitionMethod = '\0';
@ -157,7 +160,6 @@ CitusCopyFrom(CopyStmt *copyStatement, char *completionTag)
HASH_SEQ_STATUS status; HASH_SEQ_STATUS status;
ShardConnections *shardConnections = NULL; ShardConnections *shardConnections = NULL;
List *connectionList = NIL; List *connectionList = NIL;
MemoryContext tupleContext = NULL;
CopyState copyState = NULL; CopyState copyState = NULL;
TupleDesc tupleDescriptor = NULL; TupleDesc tupleDescriptor = NULL;
uint32 columnCount = 0; uint32 columnCount = 0;
@ -174,7 +176,6 @@ CitusCopyFrom(CopyStmt *copyStatement, char *completionTag)
ErrorContextCallback errorCallback; ErrorContextCallback errorCallback;
CopyOutState copyOutState = NULL; CopyOutState copyOutState = NULL;
FmgrInfo *columnOutputFunctions = NULL; FmgrInfo *columnOutputFunctions = NULL;
ExprContext *expressionContext = NULL;
/* disallow COPY to/from file or program except for superusers */ /* disallow COPY to/from file or program except for superusers */
if (copyStatement->filename != NULL && !superuser()) if (copyStatement->filename != NULL && !superuser())
@ -283,26 +284,14 @@ CitusCopyFrom(CopyStmt *copyStatement, char *completionTag)
errorCallback.previous = error_context_stack; errorCallback.previous = error_context_stack;
error_context_stack = &errorCallback; error_context_stack = &errorCallback;
/* executorState = CreateExecutorState();
* We create a new memory context called tuple context, and read and write executorTupleContext = GetPerTupleMemoryContext(executorState);
* each row's values within this memory context. After each read and write, executorExpressionContext = GetPerTupleExprContext(executorState);
* we reset the memory context. That way, we immediately release memory
* allocated for each row, and don't bloat memory usage with large input
* files.
*/
tupleContext = AllocSetContextCreate(CurrentMemoryContext,
"COPY Row Memory Context",
ALLOCSET_DEFAULT_MINSIZE,
ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE);
expressionContext = CreateStandaloneExprContext();
expressionContext->ecxt_per_tuple_memory = tupleContext;
copyOutState = (CopyOutState) palloc0(sizeof(CopyOutStateData)); copyOutState = (CopyOutState) palloc0(sizeof(CopyOutStateData));
copyOutState->binary = true; copyOutState->binary = true;
copyOutState->fe_msgbuf = makeStringInfo(); copyOutState->fe_msgbuf = makeStringInfo();
copyOutState->rowcontext = tupleContext; copyOutState->rowcontext = executorTupleContext;
columnOutputFunctions = ColumnOutputFunctions(tupleDescriptor, copyOutState->binary); columnOutputFunctions = ColumnOutputFunctions(tupleDescriptor, copyOutState->binary);
@ -318,17 +307,18 @@ CitusCopyFrom(CopyStmt *copyStatement, char *completionTag)
bool found = false; bool found = false;
MemoryContext oldContext = NULL; MemoryContext oldContext = NULL;
oldContext = MemoryContextSwitchTo(tupleContext); ResetPerTupleExprContext(executorState);
oldContext = MemoryContextSwitchTo(executorTupleContext);
/* parse a row from the input */ /* parse a row from the input */
nextRowFound = NextCopyFrom(copyState, expressionContext, nextRowFound = NextCopyFrom(copyState, executorExpressionContext,
columnValues,columnNulls, NULL); columnValues,columnNulls, NULL);
MemoryContextSwitchTo(oldContext); MemoryContextSwitchTo(oldContext);
if (!nextRowFound) if (!nextRowFound)
{ {
MemoryContextReset(tupleContext);
break; break;
} }
@ -381,8 +371,6 @@ CitusCopyFrom(CopyStmt *copyStatement, char *completionTag)
SendCopyDataToPlacements(copyOutState->fe_msgbuf, shardConnections); SendCopyDataToPlacements(copyOutState->fe_msgbuf, shardConnections);
processedRowCount += 1; processedRowCount += 1;
MemoryContextReset(tupleContext);
} }
/* send binary footers to all shards */ /* send binary footers to all shards */
@ -406,6 +394,11 @@ CitusCopyFrom(CopyStmt *copyStatement, char *completionTag)
PrepareTransactions(connectionList); PrepareTransactions(connectionList);
} }
pfree(columnValues);
pfree(columnNulls);
FreeExecutorState(executorState);
CHECK_FOR_INTERRUPTS(); CHECK_FOR_INTERRUPTS();
} }
PG_CATCH(); PG_CATCH();