Reduce memory usage on long-running COPY operations

This change ensures that the per-tuple exprcontext (on which per-tuple
state is allocated), is reset for every new tuple processed in a
long-running COPY transaction.
This commit is contained in:
Erik Nordström 2017-08-17 11:03:30 +02:00 committed by Erik Nordström
parent 953346c18b
commit 6a5a7eb398
3 changed files with 16 additions and 12 deletions

View File

@ -56,8 +56,7 @@ create_chunk_result_relation_info(ChunkDispatch *dispatch, Relation rel, Index r
ResultRelInfo *rri,
*rri_orig;
rri = palloc(sizeof(ResultRelInfo));
MemSet(rri, 0, sizeof(ResultRelInfo));
rri = palloc0(sizeof(ResultRelInfo));
NodeSetTag(rri, T_ResultRelInfo);
InitResultRelInfo(rri, rel, rti, 0);
@ -163,4 +162,6 @@ chunk_insert_state_destroy(ChunkInsertState *state)
ExecCloseIndices(state->result_relation_info);
heap_close(state->rel, NoLock);
pfree(state->result_relation_info);
pfree(state);
}

View File

@ -223,6 +223,9 @@ timescaledb_CopyFrom(CopyState cstate, Relation main_rel, List *range_table, Hyp
CHECK_FOR_INTERRUPTS();
/* Reset the per-tuple exprcontext */
ResetPerTupleExprContext(estate);
/* Switch into its memory context */
MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
@ -241,13 +244,6 @@ timescaledb_CopyFrom(CopyState cstate, Relation main_rel, List *range_table, Hyp
*/
tuple->t_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
/* Triggers and stuff need to be invoked in query context. */
MemoryContextSwitchTo(oldcontext);
/* Place tuple in tuple slot --- but slot shouldn't free it */
slot = myslot;
ExecStoreTuple(tuple, slot, InvalidBuffer, false);
/* Calculate the tuple's point in the N-dimensional hyperspace */
point = hyperspace_calculate_point(ht->space, tuple, tupDesc);
@ -266,6 +262,13 @@ timescaledb_CopyFrom(CopyState cstate, Relation main_rel, List *range_table, Hyp
bistate->current_buf = InvalidBuffer;
}
/* Triggers and stuff need to be invoked in query context. */
MemoryContextSwitchTo(oldcontext);
/* Place tuple in tuple slot --- but slot shouldn't free it */
slot = myslot;
ExecStoreTuple(tuple, slot, InvalidBuffer, false);
/*
* Set the result relation in the executor state to the target chunk.
* This makes sure that the tuple gets inserted into the correct

View File

@ -37,9 +37,9 @@ hypertable_get_chunk(Hypertable *h, Point *point)
MemoryContext old;
/*
* chunk_find() must execute on the transaction memory context since
* it allocates a lot of transient data. We don't want this allocated
* on the cache's memory context.
* chunk_find() must execute on a per-tuple memory context since it
* allocates a lot of transient data. We don't want this allocated on
* the cache's memory context.
*/
chunk = chunk_find(h->space, point);