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

Commit 06bd311

Browse files
committed
Revert "Refactor CopyReadAttributes{CSV,Text}() to use a callback in COPY FROM"
This reverts commit 95fb5b4, for reasons similar to what led to 1aa8324. In this case, the callback was called once per row, which is less worse than the previous callback introduced for COPY TO called once per argument for each row, still the patch set discussed to plug in custom routines to the COPY paths would be able to know which subroutine to use depending on its CopyFromState, so this led to a suboptimal approach at the end. For now, this part is reverted to consider better which approach to use. Discussion: https://postgr.es/m/20240206014125.qofww7ew3dx3v3uk@awork3.anarazel.de
1 parent d0071f9 commit 06bd311

File tree

3 files changed

+16
-30
lines changed

3 files changed

+16
-30
lines changed

src/backend/commands/copyfrom.c

-5
Original file line numberDiff line numberDiff line change
@@ -1776,11 +1776,6 @@ BeginCopyFrom(ParseState *pstate,
17761776

17771777
cstate->max_fields = attr_count;
17781778
cstate->raw_fields = (char **) palloc(attr_count * sizeof(char *));
1779-
1780-
if (cstate->opts.csv_mode)
1781-
cstate->copy_read_attributes = CopyReadAttributesCSV;
1782-
else
1783-
cstate->copy_read_attributes = CopyReadAttributesText;
17841779
}
17851780

17861781
MemoryContextSwitchTo(oldcontext);

src/backend/commands/copyfromparse.c

+16-8
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
* is copied into 'line_buf', with quotes and escape characters still
2626
* intact.
2727
*
28-
* 4. CopyReadAttributesText/CSV() function (via copy_read_attribute) takes
29-
* the input line from 'line_buf', and splits it into fields, unescaping
30-
* the data as required. The fields are stored in 'attribute_buf', and
31-
* 'raw_fields' array holds pointers to each field.
28+
* 4. CopyReadAttributesText/CSV() function takes the input line from
29+
* 'line_buf', and splits it into fields, unescaping the data as required.
30+
* The fields are stored in 'attribute_buf', and 'raw_fields' array holds
31+
* pointers to each field.
3232
*
3333
* If encoding conversion is not required, a shortcut is taken in step 2 to
3434
* avoid copying the data unnecessarily. The 'input_buf' pointer is set to
@@ -152,6 +152,8 @@ static const char BinarySignature[11] = "PGCOPY\n\377\r\n\0";
152152
/* non-export function prototypes */
153153
static bool CopyReadLine(CopyFromState cstate);
154154
static bool CopyReadLineText(CopyFromState cstate);
155+
static int CopyReadAttributesText(CopyFromState cstate);
156+
static int CopyReadAttributesCSV(CopyFromState cstate);
155157
static Datum CopyReadBinaryAttribute(CopyFromState cstate, FmgrInfo *flinfo,
156158
Oid typioparam, int32 typmod,
157159
bool *isnull);
@@ -773,7 +775,10 @@ NextCopyFromRawFields(CopyFromState cstate, char ***fields, int *nfields)
773775
{
774776
int fldnum;
775777

776-
fldct = cstate->copy_read_attributes(cstate);
778+
if (cstate->opts.csv_mode)
779+
fldct = CopyReadAttributesCSV(cstate);
780+
else
781+
fldct = CopyReadAttributesText(cstate);
777782

778783
if (fldct != list_length(cstate->attnumlist))
779784
ereport(ERROR,
@@ -825,7 +830,10 @@ NextCopyFromRawFields(CopyFromState cstate, char ***fields, int *nfields)
825830
return false;
826831

827832
/* Parse the line into de-escaped field values */
828-
fldct = cstate->copy_read_attributes(cstate);
833+
if (cstate->opts.csv_mode)
834+
fldct = CopyReadAttributesCSV(cstate);
835+
else
836+
fldct = CopyReadAttributesText(cstate);
829837

830838
*fields = cstate->raw_fields;
831839
*nfields = fldct;
@@ -1494,7 +1502,7 @@ GetDecimalFromHex(char hex)
14941502
*
14951503
* The return value is the number of fields actually read.
14961504
*/
1497-
int
1505+
static int
14981506
CopyReadAttributesText(CopyFromState cstate)
14991507
{
15001508
char delimc = cstate->opts.delim[0];
@@ -1748,7 +1756,7 @@ CopyReadAttributesText(CopyFromState cstate)
17481756
* CopyReadAttributesText, except we parse the fields according to
17491757
* "standard" (i.e. common) CSV usage.
17501758
*/
1751-
int
1759+
static int
17521760
CopyReadAttributesCSV(CopyFromState cstate)
17531761
{
17541762
char delimc = cstate->opts.delim[0];

src/include/commands/copyfrom_internal.h

-17
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,6 @@ typedef enum CopyInsertMethod
5252
* ExecForeignBatchInsert only if valid */
5353
} CopyInsertMethod;
5454

55-
/*
56-
* Per-format callback to parse a line into separate fields.
57-
*
58-
* Returns the number of fields read.
59-
*/
60-
typedef int (*CopyReadAttributes) (CopyFromState cstate);
61-
6255
/*
6356
* This struct contains all the state variables used throughout a COPY FROM
6457
* operation.
@@ -137,12 +130,6 @@ typedef struct CopyFromStateData
137130
int max_fields;
138131
char **raw_fields;
139132

140-
/*
141-
* Per-format callback to parse lines, then fill raw_fields and
142-
* attribute_buf.
143-
*/
144-
CopyReadAttributes copy_read_attributes;
145-
146133
/*
147134
* Similarly, line_buf holds the whole input line being processed. The
148135
* input cycle is first to read the whole line into line_buf, and then
@@ -196,8 +183,4 @@ typedef struct CopyFromStateData
196183
extern void ReceiveCopyBegin(CopyFromState cstate);
197184
extern void ReceiveCopyBinaryHeader(CopyFromState cstate);
198185

199-
/* Callbacks for copy_read_attributes */
200-
extern int CopyReadAttributesCSV(CopyFromState cstate);
201-
extern int CopyReadAttributesText(CopyFromState cstate);
202-
203186
#endif /* COPYFROM_INTERNAL_H */

0 commit comments

Comments
 (0)