Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1aa8324

Browse files
committed
Revert "Refactor CopyAttributeOut{CSV,Text}() to use a callback in COPY TO"
This reverts commit 2889fd2, following a discussion with Andres Freund as this callback, being called once per attribute when sending a relation's row, can involve a lot of indirect function calls (more attributes to deal with means more impact). The effects of a dispatch at this level would become more visible when improving the per-row code execution of COPY TO, impacting future potential performance improvements. Discussion: https://postgr.es/m/20240206014125.qofww7ew3dx3v3uk@awork3.anarazel.de
1 parent e4b27b5 commit 1aa8324

File tree

1 file changed

+15
-32
lines changed

1 file changed

+15
-32
lines changed

src/backend/commands/copyto.c

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,6 @@ typedef enum CopyDest
5454
COPY_CALLBACK, /* to callback function */
5555
} CopyDest;
5656

57-
/*
58-
* Per-format callback to send output representation of one attribute for
59-
* a `string`. `use_quote` tracks if quotes are required in the output
60-
* representation.
61-
*/
62-
typedef void (*CopyAttributeOut) (CopyToState cstate, const char *string,
63-
bool use_quote);
64-
6557
/*
6658
* This struct contains all the state variables used throughout a COPY TO
6759
* operation.
@@ -105,7 +97,6 @@ typedef struct CopyToStateData
10597
MemoryContext copycontext; /* per-copy execution context */
10698

10799
FmgrInfo *out_functions; /* lookup info for output functions */
108-
CopyAttributeOut copy_attribute_out; /* output representation callback */
109100
MemoryContext rowcontext; /* per-row evaluation context */
110101
uint64 bytes_processed; /* number of bytes processed so far */
111102
} CopyToStateData;
@@ -126,12 +117,9 @@ static const char BinarySignature[11] = "PGCOPY\n\377\r\n\0";
126117
static void EndCopy(CopyToState cstate);
127118
static void ClosePipeToProgram(CopyToState cstate);
128119
static void CopyOneRowTo(CopyToState cstate, TupleTableSlot *slot);
129-
130-
/* Callbacks for copy_attribute_out */
131-
static void CopyAttributeOutText(CopyToState cstate, const char *string,
132-
bool use_quote);
120+
static void CopyAttributeOutText(CopyToState cstate, const char *string);
133121
static void CopyAttributeOutCSV(CopyToState cstate, const char *string,
134-
bool use_quote);
122+
bool use_quote, bool single_attr);
135123

136124
/* Low-level communications functions */
137125
static void SendCopyBegin(CopyToState cstate);
@@ -445,15 +433,6 @@ BeginCopyTo(ParseState *pstate,
445433
/* Extract options from the statement node tree */
446434
ProcessCopyOptions(pstate, &cstate->opts, false /* is_from */ , options);
447435

448-
/* Set output representation callback */
449-
if (!cstate->opts.binary)
450-
{
451-
if (cstate->opts.csv_mode)
452-
cstate->copy_attribute_out = CopyAttributeOutCSV;
453-
else
454-
cstate->copy_attribute_out = CopyAttributeOutText;
455-
}
456-
457436
/* Process the source/target relation or query */
458437
if (rel)
459438
{
@@ -857,8 +836,11 @@ DoCopyTo(CopyToState cstate)
857836

858837
colname = NameStr(TupleDescAttr(tupDesc, attnum - 1)->attname);
859838

860-
/* Ignore quotes */
861-
cstate->copy_attribute_out(cstate, colname, false);
839+
if (cstate->opts.csv_mode)
840+
CopyAttributeOutCSV(cstate, colname, false,
841+
list_length(cstate->attnumlist) == 1);
842+
else
843+
CopyAttributeOutText(cstate, colname);
862844
}
863845

864846
CopySendEndOfRow(cstate);
@@ -968,9 +950,12 @@ CopyOneRowTo(CopyToState cstate, TupleTableSlot *slot)
968950
{
969951
string = OutputFunctionCall(&out_functions[attnum - 1],
970952
value);
971-
972-
cstate->copy_attribute_out(cstate, string,
973-
cstate->opts.force_quote_flags[attnum - 1]);
953+
if (cstate->opts.csv_mode)
954+
CopyAttributeOutCSV(cstate, string,
955+
cstate->opts.force_quote_flags[attnum - 1],
956+
list_length(cstate->attnumlist) == 1);
957+
else
958+
CopyAttributeOutText(cstate, string);
974959
}
975960
else
976961
{
@@ -1000,8 +985,7 @@ CopyOneRowTo(CopyToState cstate, TupleTableSlot *slot)
1000985
} while (0)
1001986

1002987
static void
1003-
CopyAttributeOutText(CopyToState cstate, const char *string,
1004-
bool use_quote)
988+
CopyAttributeOutText(CopyToState cstate, const char *string)
1005989
{
1006990
const char *ptr;
1007991
const char *start;
@@ -1155,15 +1139,14 @@ CopyAttributeOutText(CopyToState cstate, const char *string,
11551139
*/
11561140
static void
11571141
CopyAttributeOutCSV(CopyToState cstate, const char *string,
1158-
bool use_quote)
1142+
bool use_quote, bool single_attr)
11591143
{
11601144
const char *ptr;
11611145
const char *start;
11621146
char c;
11631147
char delimc = cstate->opts.delim[0];
11641148
char quotec = cstate->opts.quote[0];
11651149
char escapec = cstate->opts.escape[0];
1166-
bool single_attr = (list_length(cstate->attnumlist) == 1);
11671150

11681151
/* force quoting if it matches null_print (before conversion!) */
11691152
if (!use_quote && strcmp(string, cstate->opts.null_print) == 0)

0 commit comments

Comments
 (0)