Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
HAWQ-1825. avoid of reallocating resource for copy statement in udf
Browse files Browse the repository at this point in the history
  • Loading branch information
ztao1987 committed Jan 3, 2022
1 parent d5caf58 commit f36f114
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/backend/commands/copy.c
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
initStringInfo(&(cstate->executor_err_context));
}

QueryResource *savedResource = NULL;
if (is_from) /* copy from file to database */
{
bool pipe = (cstate->filename == NULL);
Expand Down Expand Up @@ -1590,7 +1591,6 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
GpPolicy *target_policy = NULL;
int min_target_segment_num = 0;
int max_target_segment_num = 0;
QueryResource *savedResource = NULL;
bool isMagmaHashTable = false;

target_policy = GpPolicyFetch(CurrentMemoryContext, relid);
Expand All @@ -1613,8 +1613,12 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
}
pfree(target_policy);

cstate->resource = AllocateResource(QRL_ONCE, 1, 1, max_target_segment_num, min_target_segment_num,NULL,0);
savedResource = GetActiveQueryResource();
if (!savedResource) {
cstate->resource = AllocateResource(QRL_ONCE, 1, 1, max_target_segment_num, min_target_segment_num,NULL,0);
} else {
cstate->resource = AllocateResource(QRL_INHERIT, 0, 0, 0, 0, NULL, 0);
}
SetActiveQueryResource(cstate->resource);
all_relids = lappend_oid(all_relids, relid);

Expand Down Expand Up @@ -1653,7 +1657,6 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
cstate->cdbsreh->errtbl,
err_segnos);
}
SetActiveQueryResource(savedResource);
}
else
{
Expand Down Expand Up @@ -1780,6 +1783,18 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
FreeResource(cstate->resource);
cstate->resource = NULL;
}
/**
* copy from:
* if 'copy from' allocate resource with QRL_ONCE, reset ActiveQueryResource
* back to NULL then analyze will allocate resource with QRL_ONCE.
* if 'copy from' inherit resource with QRL_INHERIT from UDF, reset
* ActiveQueryResource to original resource from UDF which will be inherited
* by analyze.
*
* copy to:
* 'copy to' has already done all work with resource before this reset.
*/
SetActiveQueryResource(savedResource);

/* Clean up storage (probably not really necessary) */
processed = cstate->processed;
Expand Down Expand Up @@ -1988,10 +2003,16 @@ DoCopyTo(CopyState cstate)
}
lFullRelOids = lappend_oid(lFullRelOids, cstate->rel->rd_id);

target_segment_num = calculate_virtual_segment_number(lFullRelOids);
elog(LOG, "virtual segment number of copy to is: %d\n", target_segment_num);

cstate->resource = AllocateResource(QRL_ONCE, 1, 1, target_segment_num, target_segment_num,NULL,0);
QueryResource *savedResource = NULL;
savedResource = GetActiveQueryResource();
if (!savedResource) {
target_segment_num = calculate_virtual_segment_number(lFullRelOids);
elog(LOG, "virtual segment number of copy to is: %d\n", target_segment_num);
cstate->resource = AllocateResource(QRL_ONCE, 1, 1, target_segment_num, target_segment_num,NULL,0);
} else {
cstate->resource = AllocateResource(QRL_INHERIT, 0, 0, 0, 0, NULL, 0);
}
SetActiveQueryResource(cstate->resource);
CopyToDispatch(cstate);
}
else
Expand Down

0 comments on commit f36f114

Please sign in to comment.