mirror of
https://github.com/timescale/timescaledb.git
synced 2025-05-17 19:13:16 +08:00
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:
parent
953346c18b
commit
6a5a7eb398
@ -56,8 +56,7 @@ create_chunk_result_relation_info(ChunkDispatch *dispatch, Relation rel, Index r
|
|||||||
ResultRelInfo *rri,
|
ResultRelInfo *rri,
|
||||||
*rri_orig;
|
*rri_orig;
|
||||||
|
|
||||||
rri = palloc(sizeof(ResultRelInfo));
|
rri = palloc0(sizeof(ResultRelInfo));
|
||||||
MemSet(rri, 0, sizeof(ResultRelInfo));
|
|
||||||
NodeSetTag(rri, T_ResultRelInfo);
|
NodeSetTag(rri, T_ResultRelInfo);
|
||||||
|
|
||||||
InitResultRelInfo(rri, rel, rti, 0);
|
InitResultRelInfo(rri, rel, rti, 0);
|
||||||
@ -163,4 +162,6 @@ chunk_insert_state_destroy(ChunkInsertState *state)
|
|||||||
|
|
||||||
ExecCloseIndices(state->result_relation_info);
|
ExecCloseIndices(state->result_relation_info);
|
||||||
heap_close(state->rel, NoLock);
|
heap_close(state->rel, NoLock);
|
||||||
|
pfree(state->result_relation_info);
|
||||||
|
pfree(state);
|
||||||
}
|
}
|
||||||
|
17
src/copy.c
17
src/copy.c
@ -223,6 +223,9 @@ timescaledb_CopyFrom(CopyState cstate, Relation main_rel, List *range_table, Hyp
|
|||||||
|
|
||||||
CHECK_FOR_INTERRUPTS();
|
CHECK_FOR_INTERRUPTS();
|
||||||
|
|
||||||
|
/* Reset the per-tuple exprcontext */
|
||||||
|
ResetPerTupleExprContext(estate);
|
||||||
|
|
||||||
/* Switch into its memory context */
|
/* Switch into its memory context */
|
||||||
MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
|
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);
|
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 */
|
/* Calculate the tuple's point in the N-dimensional hyperspace */
|
||||||
point = hyperspace_calculate_point(ht->space, tuple, tupDesc);
|
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;
|
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.
|
* Set the result relation in the executor state to the target chunk.
|
||||||
* This makes sure that the tuple gets inserted into the correct
|
* This makes sure that the tuple gets inserted into the correct
|
||||||
|
@ -37,9 +37,9 @@ hypertable_get_chunk(Hypertable *h, Point *point)
|
|||||||
MemoryContext old;
|
MemoryContext old;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* chunk_find() must execute on the transaction memory context since
|
* chunk_find() must execute on a per-tuple memory context since it
|
||||||
* it allocates a lot of transient data. We don't want this allocated
|
* allocates a lot of transient data. We don't want this allocated on
|
||||||
* on the cache's memory context.
|
* the cache's memory context.
|
||||||
*/
|
*/
|
||||||
chunk = chunk_find(h->space, point);
|
chunk = chunk_find(h->space, point);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user