Revert "Refactor CopyReadAttributes{CSV,Text}() to use a callback in COPY FROM"
authorMichael Paquier <michael@paquier.xyz>
Wed, 14 Feb 2024 01:07:22 +0000 (10:07 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 14 Feb 2024 01:07:22 +0000 (10:07 +0900)
This reverts commit 95fb5b49024, for reasons similar to what led to
1aa8324b81fa.  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

src/backend/commands/copyfrom.c
src/backend/commands/copyfromparse.c
src/include/commands/copyfrom_internal.h

index 41f6bc43e490b70fe46165d41ef5c5b95924d2db..1fe70b913382760c52097557b9a3e4e4b9d773f4 100644 (file)
@@ -1776,11 +1776,6 @@ BeginCopyFrom(ParseState *pstate,
 
                cstate->max_fields = attr_count;
                cstate->raw_fields = (char **) palloc(attr_count * sizeof(char *));
-
-               if (cstate->opts.csv_mode)
-                       cstate->copy_read_attributes = CopyReadAttributesCSV;
-               else
-                       cstate->copy_read_attributes = CopyReadAttributesText;
        }
 
        MemoryContextSwitchTo(oldcontext);
index 906756362e92232125bf17af7d4f2acc016fa782..7cacd0b752c984c516f27c9bc92cdf8590553b59 100644 (file)
  *    is copied into 'line_buf', with quotes and escape characters still
  *    intact.
  *
- * 4. CopyReadAttributesText/CSV() function (via copy_read_attribute) takes
- *    the input line from 'line_buf', and splits it into fields, unescaping
- *    the data as required.  The fields are stored in 'attribute_buf', and
- *    'raw_fields' array holds pointers to each field.
+ * 4. CopyReadAttributesText/CSV() function takes the input line from
+ *    'line_buf', and splits it into fields, unescaping the data as required.
+ *    The fields are stored in 'attribute_buf', and 'raw_fields' array holds
+ *    pointers to each field.
  *
  * If encoding conversion is not required, a shortcut is taken in step 2 to
  * 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";
 /* non-export function prototypes */
 static bool CopyReadLine(CopyFromState cstate);
 static bool CopyReadLineText(CopyFromState cstate);
+static int     CopyReadAttributesText(CopyFromState cstate);
+static int     CopyReadAttributesCSV(CopyFromState cstate);
 static Datum CopyReadBinaryAttribute(CopyFromState cstate, FmgrInfo *flinfo,
                                                                         Oid typioparam, int32 typmod,
                                                                         bool *isnull);
@@ -773,7 +775,10 @@ NextCopyFromRawFields(CopyFromState cstate, char ***fields, int *nfields)
                {
                        int                     fldnum;
 
-                       fldct = cstate->copy_read_attributes(cstate);
+                       if (cstate->opts.csv_mode)
+                               fldct = CopyReadAttributesCSV(cstate);
+                       else
+                               fldct = CopyReadAttributesText(cstate);
 
                        if (fldct != list_length(cstate->attnumlist))
                                ereport(ERROR,
@@ -825,7 +830,10 @@ NextCopyFromRawFields(CopyFromState cstate, char ***fields, int *nfields)
                return false;
 
        /* Parse the line into de-escaped field values */
-       fldct = cstate->copy_read_attributes(cstate);
+       if (cstate->opts.csv_mode)
+               fldct = CopyReadAttributesCSV(cstate);
+       else
+               fldct = CopyReadAttributesText(cstate);
 
        *fields = cstate->raw_fields;
        *nfields = fldct;
@@ -1494,7 +1502,7 @@ GetDecimalFromHex(char hex)
  *
  * The return value is the number of fields actually read.
  */
-int
+static int
 CopyReadAttributesText(CopyFromState cstate)
 {
        char            delimc = cstate->opts.delim[0];
@@ -1748,7 +1756,7 @@ CopyReadAttributesText(CopyFromState cstate)
  * CopyReadAttributesText, except we parse the fields according to
  * "standard" (i.e. common) CSV usage.
  */
-int
+static int
 CopyReadAttributesCSV(CopyFromState cstate)
 {
        char            delimc = cstate->opts.delim[0];
index 759f8e3d0909a16e29bbdbb94e1c9c8b79005d88..cad52fcc78370140e27db65afd2f0233f588e88b 100644 (file)
@@ -52,13 +52,6 @@ typedef enum CopyInsertMethod
                                                                 * ExecForeignBatchInsert only if valid */
 } CopyInsertMethod;
 
-/*
- * Per-format callback to parse a line into separate fields.
- *
- * Returns the number of fields read.
- */
-typedef int (*CopyReadAttributes) (CopyFromState cstate);
-
 /*
  * This struct contains all the state variables used throughout a COPY FROM
  * operation.
@@ -137,12 +130,6 @@ typedef struct CopyFromStateData
        int                     max_fields;
        char      **raw_fields;
 
-       /*
-        * Per-format callback to parse lines, then fill raw_fields and
-        * attribute_buf.
-        */
-       CopyReadAttributes copy_read_attributes;
-
        /*
         * Similarly, line_buf holds the whole input line being processed. The
         * input cycle is first to read the whole line into line_buf, and then
@@ -196,8 +183,4 @@ typedef struct CopyFromStateData
 extern void ReceiveCopyBegin(CopyFromState cstate);
 extern void ReceiveCopyBinaryHeader(CopyFromState cstate);
 
-/* Callbacks for copy_read_attributes */
-extern int     CopyReadAttributesCSV(CopyFromState cstate);
-extern int     CopyReadAttributesText(CopyFromState cstate);
-
 #endif                                                 /* COPYFROM_INTERNAL_H */