Skip to content

Commit

Permalink
Merge ab71c17 into 7b2cda7
Browse files Browse the repository at this point in the history
  • Loading branch information
onderkalaci committed Mar 6, 2015
2 parents 7b2cda7 + ab71c17 commit 4e00ae5
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pg_shard.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
#include "utils/relcache.h"
#include "utils/snapmgr.h"
#include "utils/tuplestore.h"
#include "utils/memutils.h"


/* controls use of locks to enforce safe commutativity */
Expand Down Expand Up @@ -1455,6 +1456,11 @@ StoreQueryResult(PGconn *connection, TupleDesc tupleDescriptor,
AttInMetadata *attributeInputMetadata = TupleDescGetAttInMetadata(tupleDescriptor);
uint32 expectedColumnCount = tupleDescriptor->natts;
char **columnArray = (char **) palloc0(expectedColumnCount * sizeof(char *));
MemoryContext ioContext = AllocSetContextCreate(CurrentMemoryContext,
"StoreQueryResult",
ALLOCSET_DEFAULT_MINSIZE,
ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE);

Assert(tupleStore != NULL);

Expand Down Expand Up @@ -1488,6 +1494,7 @@ StoreQueryResult(PGconn *connection, TupleDesc tupleDescriptor,
for (rowIndex = 0; rowIndex < rowCount; rowIndex++)
{
HeapTuple heapTuple = NULL;
MemoryContext oldContext = NULL;
memset(columnArray, 0, columnCount * sizeof(char *));

for (columnIndex = 0; columnIndex < columnCount; columnIndex++)
Expand All @@ -1502,8 +1509,19 @@ StoreQueryResult(PGconn *connection, TupleDesc tupleDescriptor,
}
}

/*
* Switch to a temporary memory context that we reset after each tuple. This
* protects us from any memory leaks that might be present in I/O functions
* called by BuildTupleFromCStrings.
*/
oldContext = MemoryContextSwitchTo(ioContext);

heapTuple = BuildTupleFromCStrings(attributeInputMetadata, columnArray);

MemoryContextSwitchTo(oldContext);

tuplestore_puttuple(tupleStore, heapTuple);
MemoryContextReset(ioContext);
}

PQclear(result);
Expand Down Expand Up @@ -1595,6 +1613,7 @@ TupleStoreToTable(RangeVar *tableRangeVar, List *storeToTableColumnList,
CommandCounterIncrement();

ExecClearTuple(storeTableSlot);
heap_freetuple(tableTuple);
}

ExecDropSingleTupleTableSlot(storeTableSlot);
Expand Down

0 comments on commit 4e00ae5

Please sign in to comment.