Skip to content

Commit 78ce115

Browse files
Fix copy from directory table. (#416)
We use temporary tupledesction in copy from directory table, which will have no side effect on relcache.
1 parent f73e8c6 commit 78ce115

File tree

1 file changed

+55
-35
lines changed

1 file changed

+55
-35
lines changed

src/backend/commands/copyfrom.c

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,14 @@ SendCopyFromForwardedTuple(CopyFromState cstate,
193193
char *line,
194194
int line_len,
195195
Datum *values,
196-
bool *nulls);
196+
bool *nulls,
197+
bool is_directory_table);
197198
static void SendCopyFromForwardedHeader(CopyFromState cstate, CdbCopy *cdbCopy);
198199
static void SendCopyFromForwardedError(CopyFromState cstate, CdbCopy *cdbCopy, char *errmsg);
199200

200201
static bool NextCopyFromDispatch(CopyFromState cstate, ExprContext *econtext,
201202
Datum *values, bool *nulls);
202-
static bool NextCopyFromExecute(CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls);
203+
static bool NextCopyFromExecute(CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls, bool is_directory_table);
203204
static bool NextCopyFromRawFieldsX(CopyFromState cstate, char ***fields, int *nfields,
204205
int stop_processing_at_field);
205206
static bool NextCopyFromX(CopyFromState cstate, ExprContext *econtext,
@@ -1026,7 +1027,8 @@ CopyFromDirectoryTable(CopyFromState cstate)
10261027
cstate->line_buf.data,
10271028
cstate->line_buf.len,
10281029
myslot->tts_values,
1029-
myslot->tts_isnull);
1030+
myslot->tts_isnull,
1031+
true);
10301032
{
10311033
int64 total_completed_from_qes;
10321034
int64 total_rejected_from_qes;
@@ -1054,34 +1056,16 @@ CopyFromDirectoryTable(CopyFromState cstate)
10541056

10551057
econtext = GetPerTupleExprContext(estate);
10561058

1057-
if (NextCopyFromExecute(cstate, econtext, myslot->tts_values, myslot->tts_isnull))
1059+
if (NextCopyFromExecute(cstate, econtext, myslot->tts_values, myslot->tts_isnull, true))
10581060
{
1059-
if (tupdesc)
1060-
pfree(tupdesc);
1061-
1062-
tupdesc = CreateTemplateTupleDesc(DIRECTORY_TABLE_COLUMNS);
1063-
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "relative_path",
1064-
TEXTOID, -1, 0);
1065-
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "size",
1066-
INT8OID, -1, 0);
1067-
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "last_modified",
1068-
TIMESTAMPTZOID, -1, 0);
1069-
TupleDescInitEntry(tupdesc, (AttrNumber) 4, "md5",
1070-
TEXTOID, -1, 0);
1071-
TupleDescInitEntry(tupdesc, (AttrNumber) 5, "tag",
1072-
TEXTOID, -1 ,0);
1073-
1074-
tmpslot = MakeSingleTupleTableSlot(tupdesc, &TTSOpsVirtual);
1061+
tmpslot = MakeSingleTupleTableSlot(cstate->rel->rd_att, &TTSOpsVirtual);
10751062

10761063
for (i = 0; i < DIRECTORY_TABLE_COLUMNS; i++)
10771064
{
10781065
tmpslot->tts_values[i] = myslot->tts_values[i];
10791066
tmpslot->tts_isnull[i] = myslot->tts_isnull[i];
10801067
}
10811068

1082-
cstate->rel->rd_att = CreateTupleDescCopy(tupdesc);
1083-
cstate->rel->rd_att->tdrefcount = 1; /* mark as refcounted */
1084-
10851069
/*
10861070
* Reset the per-tuple exprcontext. We do this after every tuple, to
10871071
* clean-up after expression evaluations etc.
@@ -1129,9 +1113,6 @@ CopyFromDirectoryTable(CopyFromState cstate)
11291113

11301114
list_free(recheckIndexes);
11311115

1132-
if (tupdesc)
1133-
pfree(tupdesc);
1134-
11351116
if (UFileExists(dirTable->spcId, orgiFileName))
11361117
{
11371118
UFileUnlink(dirTable->spcId, orgiFileName);
@@ -1174,7 +1155,8 @@ CopyFromDirectoryTable(CopyFromState cstate)
11741155
errmsg("unable to sync file \"%s\": %s", glob_copystmt->dirfilename, UFileGetLastError(file))));
11751156

11761157
UFileClose(file);
1177-
1158+
1159+
ReleaseTupleDesc(cstate->rel->rd_att);
11781160
ExecClearTuple(myslot);
11791161
ExecClearTuple(tmpslot);
11801162

@@ -1345,8 +1327,6 @@ BeginCopyFromDirectoryTable(ParseState *pstate,
13451327

13461328
num_phys_attrs = tupDesc->natts;
13471329

1348-
cstate->rel->rd_att = CreateTupleDescCopy(tupDesc);
1349-
cstate->rel->rd_att->tdrefcount = 1; /* mark as refcounted */
13501330
cstate->attnumlist = CopyGetAttnums(tupDesc, cstate->rel, NIL);
13511331

13521332
/*
@@ -1929,7 +1909,7 @@ CopyFrom(CopyFromState cstate)
19291909

19301910
if (cstate->dispatch_mode == COPY_EXECUTOR)
19311911
{
1932-
if (!NextCopyFromExecute(cstate, econtext, myslot->tts_values, myslot->tts_isnull))
1912+
if (!NextCopyFromExecute(cstate, econtext, myslot->tts_values, myslot->tts_isnull, false))
19331913
break;
19341914

19351915
/*
@@ -2161,7 +2141,8 @@ CopyFrom(CopyFromState cstate)
21612141
cstate->line_buf.data,
21622142
cstate->line_buf.len,
21632143
myslot->tts_values,
2164-
myslot->tts_isnull);
2144+
myslot->tts_isnull,
2145+
false);
21652146
skip_tuple = true;
21662147
processed++;
21672148
}
@@ -3409,7 +3390,7 @@ NextCopyFromDispatch(CopyFromState cstate, ExprContext *econtext,
34093390
* rows from the QD.
34103391
*/
34113392
static bool
3412-
NextCopyFromExecute(CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls)
3393+
NextCopyFromExecute(CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls, bool is_directory_table)
34133394
{
34143395
TupleDesc tupDesc;
34153396
AttrNumber num_phys_attrs,
@@ -3420,7 +3401,26 @@ NextCopyFromExecute(CopyFromState cstate, ExprContext *econtext, Datum *values,
34203401
int r;
34213402
bool got_error;
34223403

3423-
tupDesc = RelationGetDescr(cstate->rel);
3404+
if (!is_directory_table)
3405+
{
3406+
tupDesc = RelationGetDescr(cstate->rel);
3407+
}
3408+
else
3409+
{
3410+
tupDesc = CreateTemplateTupleDesc(6);
3411+
TupleDescInitEntry(tupDesc, (AttrNumber) 1, "relative_path",
3412+
TEXTOID, -1, 0);
3413+
TupleDescInitEntry(tupDesc, (AttrNumber) 2, "size",
3414+
INT8OID, -1, 0);
3415+
TupleDescInitEntry(tupDesc, (AttrNumber) 3, "last_modified",
3416+
TIMESTAMPTZOID, -1, 0);
3417+
TupleDescInitEntry(tupDesc, (AttrNumber) 4, "md5",
3418+
TEXTOID, -1, 0);
3419+
TupleDescInitEntry(tupDesc, (AttrNumber) 5, "tag",
3420+
TEXTOID, -1 ,0);
3421+
TupleDescInitEntry(tupDesc, (AttrNumber) 6, "file",
3422+
TEXTOID, -1, 0);
3423+
}
34243424
num_phys_attrs = tupDesc->natts;
34253425
attr_count = list_length(cstate->attnumlist);
34263426

@@ -3727,7 +3727,8 @@ SendCopyFromForwardedTuple(CopyFromState cstate,
37273727
char *line,
37283728
int line_len,
37293729
Datum *values,
3730-
bool *nulls)
3730+
bool *nulls,
3731+
bool is_directory_table)
37313732
{
37323733
TupleDesc tupDesc;
37333734
FormData_pg_attribute *attr;
@@ -3740,7 +3741,26 @@ SendCopyFromForwardedTuple(CopyFromState cstate,
37403741
if (!OidIsValid(RelationGetRelid(rel)))
37413742
elog(ERROR, "invalid target table OID in COPY");
37423743

3743-
tupDesc = RelationGetDescr(rel);
3744+
if (!is_directory_table)
3745+
{
3746+
tupDesc = RelationGetDescr(rel);
3747+
}
3748+
else
3749+
{
3750+
tupDesc = CreateTemplateTupleDesc(6);
3751+
TupleDescInitEntry(tupDesc, (AttrNumber) 1, "relative_path",
3752+
TEXTOID, -1, 0);
3753+
TupleDescInitEntry(tupDesc, (AttrNumber) 2, "size",
3754+
INT8OID, -1, 0);
3755+
TupleDescInitEntry(tupDesc, (AttrNumber) 3, "last_modified",
3756+
TIMESTAMPTZOID, -1, 0);
3757+
TupleDescInitEntry(tupDesc, (AttrNumber) 4, "md5",
3758+
TEXTOID, -1, 0);
3759+
TupleDescInitEntry(tupDesc, (AttrNumber) 5, "tag",
3760+
TEXTOID, -1 ,0);
3761+
TupleDescInitEntry(tupDesc, (AttrNumber) 6, "file",
3762+
TEXTOID, -1, 0);
3763+
}
37443764
attr = tupDesc->attrs;
37453765
num_phys_attrs = tupDesc->natts;
37463766

0 commit comments

Comments
 (0)