LCOV - code coverage report
Current view: top level - src/backend/commands - copyto.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 85.7 % 575 493
Test Date: 2026-07-15 17:15:34 Functions: 94.4 % 36 34
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 72.0 % 421 303

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * copyto.c
       4                 :             :  *      COPY <table> TO file/program/client
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :             :  *
       9                 :             :  *
      10                 :             :  * IDENTIFICATION
      11                 :             :  *    src/backend/commands/copyto.c
      12                 :             :  *
      13                 :             :  *-------------------------------------------------------------------------
      14                 :             :  */
      15                 :             : #include "postgres.h"
      16                 :             : 
      17                 :             : #include <ctype.h>
      18                 :             : #include <unistd.h>
      19                 :             : #include <sys/stat.h>
      20                 :             : 
      21                 :             : #include "access/table.h"
      22                 :             : #include "access/tableam.h"
      23                 :             : #include "access/tupconvert.h"
      24                 :             : #include "catalog/pg_inherits.h"
      25                 :             : #include "commands/copyapi.h"
      26                 :             : #include "commands/progress.h"
      27                 :             : #include "executor/execdesc.h"
      28                 :             : #include "executor/executor.h"
      29                 :             : #include "executor/tuptable.h"
      30                 :             : #include "funcapi.h"
      31                 :             : #include "libpq/libpq.h"
      32                 :             : #include "libpq/pqformat.h"
      33                 :             : #include "mb/pg_wchar.h"
      34                 :             : #include "miscadmin.h"
      35                 :             : #include "pgstat.h"
      36                 :             : #include "storage/fd.h"
      37                 :             : #include "tcop/tcopprot.h"
      38                 :             : #include "utils/json.h"
      39                 :             : #include "utils/lsyscache.h"
      40                 :             : #include "utils/memutils.h"
      41                 :             : #include "utils/rel.h"
      42                 :             : #include "utils/snapmgr.h"
      43                 :             : #include "utils/wait_event.h"
      44                 :             : 
      45                 :             : /*
      46                 :             :  * Represents the different dest cases we need to worry about at
      47                 :             :  * the bottom level
      48                 :             :  */
      49                 :             : typedef enum CopyDest
      50                 :             : {
      51                 :             :     COPY_FILE,                  /* to file (or a piped program) */
      52                 :             :     COPY_FRONTEND,              /* to frontend */
      53                 :             :     COPY_CALLBACK,              /* to callback function */
      54                 :             : } CopyDest;
      55                 :             : 
      56                 :             : /*
      57                 :             :  * This struct contains all the state variables used throughout a COPY TO
      58                 :             :  * operation.
      59                 :             :  *
      60                 :             :  * Multi-byte encodings: all supported client-side encodings encode multi-byte
      61                 :             :  * characters by having the first byte's high bit set. Subsequent bytes of the
      62                 :             :  * character can have the high bit not set. When scanning data in such an
      63                 :             :  * encoding to look for a match to a single-byte (ie ASCII) character, we must
      64                 :             :  * use the full pg_encoding_mblen() machinery to skip over multibyte
      65                 :             :  * characters, else we might find a false match to a trailing byte. In
      66                 :             :  * supported server encodings, there is no possibility of a false match, and
      67                 :             :  * it's faster to make useless comparisons to trailing bytes than it is to
      68                 :             :  * invoke pg_encoding_mblen() to skip over them. encoding_embeds_ascii is true
      69                 :             :  * when we have to do it the hard way.
      70                 :             :  */
      71                 :             : typedef struct CopyToStateData
      72                 :             : {
      73                 :             :     /* format-specific routines */
      74                 :             :     const CopyToRoutine *routine;
      75                 :             : 
      76                 :             :     /* low-level state data */
      77                 :             :     CopyDest    copy_dest;      /* type of copy source/destination */
      78                 :             :     FILE       *copy_file;      /* used if copy_dest == COPY_FILE */
      79                 :             :     StringInfo  fe_msgbuf;      /* used for all dests during COPY TO */
      80                 :             : 
      81                 :             :     int         file_encoding;  /* file or remote side's character encoding */
      82                 :             :     bool        need_transcoding;   /* file encoding diff from server? */
      83                 :             :     bool        encoding_embeds_ascii;  /* ASCII can be non-first byte? */
      84                 :             : 
      85                 :             :     /* parameters from the COPY command */
      86                 :             :     Relation    rel;            /* relation to copy to */
      87                 :             :     QueryDesc  *queryDesc;      /* executable query to copy from */
      88                 :             :     List       *attnumlist;     /* integer list of attnums to copy */
      89                 :             :     char       *filename;       /* filename, or NULL for STDOUT */
      90                 :             :     bool        is_program;     /* is 'filename' a program to popen? */
      91                 :             :     bool        json_row_delim_needed;  /* need delimiter before next row */
      92                 :             :     StringInfo  json_buf;       /* reusable buffer for JSON output,
      93                 :             :                                  * initialized in BeginCopyTo */
      94                 :             :     TupleDesc   tupDesc;        /* Descriptor for JSON output; for a column
      95                 :             :                                  * list this is a projected descriptor */
      96                 :             :     Datum      *json_projvalues;    /* pre-allocated projection values, or
      97                 :             :                                      * NULL */
      98                 :             :     bool       *json_projnulls; /* pre-allocated projection nulls, or NULL */
      99                 :             :     copy_data_dest_cb data_dest_cb; /* function for writing data */
     100                 :             : 
     101                 :             :     CopyFormatOptions opts;
     102                 :             :     Node       *whereClause;    /* WHERE condition (or NULL) */
     103                 :             :     List       *partitions;     /* OID list of partitions to copy data from */
     104                 :             : 
     105                 :             :     /*
     106                 :             :      * Working state
     107                 :             :      */
     108                 :             :     MemoryContext copycontext;  /* per-copy execution context */
     109                 :             : 
     110                 :             :     FmgrInfo   *out_functions;  /* lookup info for output functions */
     111                 :             :     MemoryContext rowcontext;   /* per-row evaluation context */
     112                 :             :     uint64      bytes_processed;    /* number of bytes processed so far */
     113                 :             : } CopyToStateData;
     114                 :             : 
     115                 :             : /* DestReceiver for COPY (query) TO */
     116                 :             : typedef struct
     117                 :             : {
     118                 :             :     DestReceiver pub;           /* publicly-known function pointers */
     119                 :             :     CopyToState cstate;         /* CopyToStateData for the command */
     120                 :             :     uint64      processed;      /* # of tuples processed */
     121                 :             : } DR_copy;
     122                 :             : 
     123                 :             : /* NOTE: there's a copy of this in copyfromparse.c */
     124                 :             : static const char BinarySignature[11] = "PGCOPY\n\377\r\n\0";
     125                 :             : 
     126                 :             : 
     127                 :             : /* non-export function prototypes */
     128                 :             : static void EndCopy(CopyToState cstate);
     129                 :             : static void ClosePipeToProgram(CopyToState cstate);
     130                 :             : static void CopyOneRowTo(CopyToState cstate, TupleTableSlot *slot);
     131                 :             : static void CopyAttributeOutText(CopyToState cstate, const char *string);
     132                 :             : static void CopyAttributeOutCSV(CopyToState cstate, const char *string,
     133                 :             :                                 bool use_quote);
     134                 :             : static void CopyRelationTo(CopyToState cstate, Relation rel, Relation root_rel,
     135                 :             :                            uint64 *processed);
     136                 :             : 
     137                 :             : /* built-in format-specific routines */
     138                 :             : static void CopyToTextLikeStart(CopyToState cstate, TupleDesc tupDesc);
     139                 :             : static void CopyToTextLikeOutFunc(CopyToState cstate, Oid atttypid, FmgrInfo *finfo);
     140                 :             : static void CopyToTextOneRow(CopyToState cstate, TupleTableSlot *slot);
     141                 :             : static void CopyToCSVOneRow(CopyToState cstate, TupleTableSlot *slot);
     142                 :             : static void CopyToTextLikeOneRow(CopyToState cstate, TupleTableSlot *slot,
     143                 :             :                                  bool is_csv);
     144                 :             : static void CopyToTextLikeEnd(CopyToState cstate);
     145                 :             : static void CopyToJsonOneRow(CopyToState cstate, TupleTableSlot *slot);
     146                 :             : static void CopyToJsonEnd(CopyToState cstate);
     147                 :             : static void CopyToBinaryStart(CopyToState cstate, TupleDesc tupDesc);
     148                 :             : static void CopyToBinaryOutFunc(CopyToState cstate, Oid atttypid, FmgrInfo *finfo);
     149                 :             : static void CopyToBinaryOneRow(CopyToState cstate, TupleTableSlot *slot);
     150                 :             : static void CopyToBinaryEnd(CopyToState cstate);
     151                 :             : 
     152                 :             : /* Low-level communications functions */
     153                 :             : static void SendCopyBegin(CopyToState cstate);
     154                 :             : static void SendCopyEnd(CopyToState cstate);
     155                 :             : static void CopySendData(CopyToState cstate, const void *databuf, int datasize);
     156                 :             : static void CopySendString(CopyToState cstate, const char *str);
     157                 :             : static void CopySendChar(CopyToState cstate, char c);
     158                 :             : static void CopySendEndOfRow(CopyToState cstate);
     159                 :             : static void CopySendTextLikeEndOfRow(CopyToState cstate);
     160                 :             : static void CopySendInt32(CopyToState cstate, int32 val);
     161                 :             : static void CopySendInt16(CopyToState cstate, int16 val);
     162                 :             : 
     163                 :             : /*
     164                 :             :  * COPY TO routines for built-in formats.
     165                 :             :  */
     166                 :             : 
     167                 :             : /* text format */
     168                 :             : static const CopyToRoutine CopyToRoutineText = {
     169                 :             :     .CopyToStart = CopyToTextLikeStart,
     170                 :             :     .CopyToOutFunc = CopyToTextLikeOutFunc,
     171                 :             :     .CopyToOneRow = CopyToTextOneRow,
     172                 :             :     .CopyToEnd = CopyToTextLikeEnd,
     173                 :             : };
     174                 :             : 
     175                 :             : /* CSV format */
     176                 :             : static const CopyToRoutine CopyToRoutineCSV = {
     177                 :             :     .CopyToStart = CopyToTextLikeStart,
     178                 :             :     .CopyToOutFunc = CopyToTextLikeOutFunc,
     179                 :             :     .CopyToOneRow = CopyToCSVOneRow,
     180                 :             :     .CopyToEnd = CopyToTextLikeEnd,
     181                 :             : };
     182                 :             : 
     183                 :             : /* json format */
     184                 :             : static const CopyToRoutine CopyToRoutineJson = {
     185                 :             :     .CopyToStart = CopyToTextLikeStart,
     186                 :             :     .CopyToOutFunc = CopyToTextLikeOutFunc,
     187                 :             :     .CopyToOneRow = CopyToJsonOneRow,
     188                 :             :     .CopyToEnd = CopyToJsonEnd,
     189                 :             : };
     190                 :             : 
     191                 :             : /* binary format */
     192                 :             : static const CopyToRoutine CopyToRoutineBinary = {
     193                 :             :     .CopyToStart = CopyToBinaryStart,
     194                 :             :     .CopyToOutFunc = CopyToBinaryOutFunc,
     195                 :             :     .CopyToOneRow = CopyToBinaryOneRow,
     196                 :             :     .CopyToEnd = CopyToBinaryEnd,
     197                 :             : };
     198                 :             : 
     199                 :             : /* Return a COPY TO routine for the given options */
     200                 :             : static const CopyToRoutine *
     201                 :        5278 : CopyToGetRoutine(const CopyFormatOptions *opts)
     202                 :             : {
     203         [ +  + ]:        5278 :     if (opts->format == COPY_FORMAT_CSV)
     204                 :          92 :         return &CopyToRoutineCSV;
     205         [ +  + ]:        5186 :     else if (opts->format == COPY_FORMAT_BINARY)
     206                 :           9 :         return &CopyToRoutineBinary;
     207         [ +  + ]:        5177 :     else if (opts->format == COPY_FORMAT_JSON)
     208                 :         104 :         return &CopyToRoutineJson;
     209                 :             : 
     210                 :             :     /* default is text */
     211                 :        5073 :     return &CopyToRoutineText;
     212                 :             : }
     213                 :             : 
     214                 :             : /* Implementation of the start callback for text, CSV, and json formats */
     215                 :             : static void
     216                 :        5180 : CopyToTextLikeStart(CopyToState cstate, TupleDesc tupDesc)
     217                 :             : {
     218                 :             :     /*
     219                 :             :      * For non-binary copy, we need to convert null_print to file encoding,
     220                 :             :      * because it will be sent directly with CopySendString.
     221                 :             :      */
     222         [ +  + ]:        5180 :     if (cstate->need_transcoding)
     223                 :          17 :         cstate->opts.null_print_client = pg_server_to_any(cstate->opts.null_print,
     224                 :             :                                                           cstate->opts.null_print_len,
     225                 :             :                                                           cstate->file_encoding);
     226                 :             : 
     227                 :             :     /* if a header has been requested send the line */
     228         [ +  + ]:        5180 :     if (cstate->opts.header_line == COPY_HEADER_TRUE)
     229                 :             :     {
     230                 :             :         ListCell   *cur;
     231                 :          28 :         bool        hdr_delim = false;
     232                 :             : 
     233                 :             :         Assert(cstate->opts.format != COPY_FORMAT_JSON);
     234                 :             : 
     235   [ +  -  +  +  :          76 :         foreach(cur, cstate->attnumlist)
                   +  + ]
     236                 :             :         {
     237                 :          48 :             int         attnum = lfirst_int(cur);
     238                 :             :             char       *colname;
     239                 :             : 
     240         [ +  + ]:          48 :             if (hdr_delim)
     241                 :          20 :                 CopySendChar(cstate, cstate->opts.delim[0]);
     242                 :          48 :             hdr_delim = true;
     243                 :             : 
     244                 :          48 :             colname = NameStr(TupleDescAttr(tupDesc, attnum - 1)->attname);
     245                 :             : 
     246         [ +  + ]:          48 :             if (cstate->opts.format == COPY_FORMAT_CSV)
     247                 :          16 :                 CopyAttributeOutCSV(cstate, colname, false);
     248                 :             :             else
     249                 :          32 :                 CopyAttributeOutText(cstate, colname);
     250                 :             :         }
     251                 :             : 
     252                 :          28 :         CopySendTextLikeEndOfRow(cstate);
     253                 :             :     }
     254                 :             : 
     255                 :             :     /*
     256                 :             :      * If FORCE_ARRAY has been specified, send the opening bracket.
     257                 :             :      */
     258   [ +  +  +  + ]:        5180 :     if (cstate->opts.format == COPY_FORMAT_JSON && cstate->opts.force_array)
     259                 :             :     {
     260                 :          20 :         CopySendChar(cstate, '[');
     261                 :          20 :         CopySendTextLikeEndOfRow(cstate);
     262                 :             :     }
     263                 :        5180 : }
     264                 :             : 
     265                 :             : /*
     266                 :             :  * Implementation of the outfunc callback for text, CSV, and json formats. Assign
     267                 :             :  * the output function data to the given *finfo.
     268                 :             :  */
     269                 :             : static void
     270                 :       17445 : CopyToTextLikeOutFunc(CopyToState cstate, Oid atttypid, FmgrInfo *finfo)
     271                 :             : {
     272                 :             :     Oid         func_oid;
     273                 :             :     bool        is_varlena;
     274                 :             : 
     275                 :             :     /* Set output function for an attribute */
     276                 :       17445 :     getTypeOutputInfo(atttypid, &func_oid, &is_varlena);
     277                 :       17445 :     fmgr_info(func_oid, finfo);
     278                 :       17445 : }
     279                 :             : 
     280                 :             : /* Implementation of the per-row callback for text format */
     281                 :             : static void
     282                 :     1840764 : CopyToTextOneRow(CopyToState cstate, TupleTableSlot *slot)
     283                 :             : {
     284                 :     1840764 :     CopyToTextLikeOneRow(cstate, slot, false);
     285                 :     1840764 : }
     286                 :             : 
     287                 :             : /* Implementation of the per-row callback for CSV format */
     288                 :             : static void
     289                 :         228 : CopyToCSVOneRow(CopyToState cstate, TupleTableSlot *slot)
     290                 :             : {
     291                 :         228 :     CopyToTextLikeOneRow(cstate, slot, true);
     292                 :         228 : }
     293                 :             : 
     294                 :             : /*
     295                 :             :  * Workhorse for CopyToTextOneRow() and CopyToCSVOneRow().
     296                 :             :  *
     297                 :             :  * We use pg_always_inline to reduce function call overhead
     298                 :             :  * and to help compilers to optimize away the 'is_csv' condition.
     299                 :             :  */
     300                 :             : static pg_always_inline void
     301                 :     1840992 : CopyToTextLikeOneRow(CopyToState cstate,
     302                 :             :                      TupleTableSlot *slot,
     303                 :             :                      bool is_csv)
     304                 :             : {
     305                 :     1840992 :     bool        need_delim = false;
     306                 :     1840992 :     FmgrInfo   *out_functions = cstate->out_functions;
     307                 :             : 
     308   [ +  +  +  +  :    10921543 :     foreach_int(attnum, cstate->attnumlist)
                   +  + ]
     309                 :             :     {
     310                 :     7239559 :         Datum       value = slot->tts_values[attnum - 1];
     311                 :     7239559 :         bool        isnull = slot->tts_isnull[attnum - 1];
     312                 :             : 
     313         [ +  + ]:     7239559 :         if (need_delim)
     314                 :     5398636 :             CopySendChar(cstate, cstate->opts.delim[0]);
     315                 :     7239559 :         need_delim = true;
     316                 :             : 
     317         [ +  + ]:     7239559 :         if (isnull)
     318                 :             :         {
     319                 :      613850 :             CopySendString(cstate, cstate->opts.null_print_client);
     320                 :             :         }
     321                 :             :         else
     322                 :             :         {
     323                 :             :             char       *string;
     324                 :             : 
     325                 :     6625709 :             string = OutputFunctionCall(&out_functions[attnum - 1],
     326                 :             :                                         value);
     327                 :             : 
     328         [ +  + ]:     6625709 :             if (is_csv)
     329                 :         412 :                 CopyAttributeOutCSV(cstate, string,
     330                 :         412 :                                     cstate->opts.force_quote_flags[attnum - 1]);
     331                 :             :             else
     332                 :     6625297 :                 CopyAttributeOutText(cstate, string);
     333                 :             :         }
     334                 :             :     }
     335                 :             : 
     336                 :     1840992 :     CopySendTextLikeEndOfRow(cstate);
     337                 :     1840992 : }
     338                 :             : 
     339                 :             : /* Implementation of the end callback for text and CSV formats */
     340                 :             : static void
     341                 :        5075 : CopyToTextLikeEnd(CopyToState cstate)
     342                 :             : {
     343                 :             :     /* Nothing to do here */
     344                 :        5075 : }
     345                 :             : 
     346                 :             : /* Implementation of the end callback for json format */
     347                 :             : static void
     348                 :         104 : CopyToJsonEnd(CopyToState cstate)
     349                 :             : {
     350         [ +  + ]:         104 :     if (cstate->opts.force_array)
     351                 :             :     {
     352                 :          20 :         CopySendChar(cstate, ']');
     353                 :          20 :         CopySendTextLikeEndOfRow(cstate);
     354                 :             :     }
     355                 :         104 : }
     356                 :             : 
     357                 :             : /* Implementation of per-row callback for json format */
     358                 :             : static void
     359                 :         332 : CopyToJsonOneRow(CopyToState cstate, TupleTableSlot *slot)
     360                 :             : {
     361                 :             :     Datum       rowdata;
     362                 :             : 
     363                 :         332 :     resetStringInfo(cstate->json_buf);
     364                 :             : 
     365         [ +  + ]:         332 :     if (cstate->json_projvalues != NULL)
     366                 :             :     {
     367                 :             :         /*
     368                 :             :          * Column list case: project selected column values into sequential
     369                 :             :          * positions matching the custom TupleDesc, then form a new tuple.
     370                 :             :          */
     371                 :             :         HeapTuple   tup;
     372                 :         140 :         int         i = 0;
     373                 :             : 
     374   [ +  -  +  +  :         560 :         foreach_int(attnum, cstate->attnumlist)
                   +  + ]
     375                 :             :         {
     376                 :         280 :             cstate->json_projvalues[i] = slot->tts_values[attnum - 1];
     377                 :         280 :             cstate->json_projnulls[i] = slot->tts_isnull[attnum - 1];
     378                 :         280 :             i++;
     379                 :             :         }
     380                 :             : 
     381                 :         140 :         tup = heap_form_tuple(cstate->tupDesc,
     382                 :         140 :                               cstate->json_projvalues,
     383                 :         140 :                               cstate->json_projnulls);
     384                 :             : 
     385                 :             :         /*
     386                 :             :          * heap_form_tuple already stamps the datum-length, type-id, and
     387                 :             :          * type-mod fields on t_data, so we can use it directly as a composite
     388                 :             :          * Datum without the extra pallocmemcpy that heap_copy_tuple_as_datum
     389                 :             :          * would do.  Any TOAST pointers in the projected values will be
     390                 :             :          * detoasted by the per-column output functions called from
     391                 :             :          * composite_to_json.
     392                 :             :          */
     393                 :         140 :         rowdata = HeapTupleGetDatum(tup);
     394                 :             :     }
     395                 :             :     else
     396                 :             :     {
     397                 :             :         /*
     398                 :             :          * Full table or query without column list.  For queries, the slot's
     399                 :             :          * TupleDesc may carry RECORDOID, which is not registered in the type
     400                 :             :          * cache and would cause composite_to_json's lookup_rowtype_tupdesc
     401                 :             :          * call to fail.  Build a HeapTuple stamped with the blessed
     402                 :             :          * descriptor so the type can be looked up correctly.
     403                 :             :          */
     404   [ +  +  +  + ]:         192 :         if (!cstate->rel && slot->tts_tupleDescriptor->tdtypeid == RECORDOID)
     405                 :          48 :         {
     406                 :          48 :             HeapTuple   tup = heap_form_tuple(cstate->tupDesc,
     407                 :          48 :                                               slot->tts_values,
     408                 :          48 :                                               slot->tts_isnull);
     409                 :             : 
     410                 :          48 :             rowdata = HeapTupleGetDatum(tup);
     411                 :             :         }
     412                 :             :         else
     413                 :         144 :             rowdata = ExecFetchSlotHeapTupleDatum(slot);
     414                 :             :     }
     415                 :             : 
     416                 :         332 :     composite_to_json(rowdata, cstate->json_buf, false);
     417                 :             : 
     418         [ +  + ]:         332 :     if (cstate->opts.force_array)
     419                 :             :     {
     420         [ +  + ]:          48 :         if (cstate->json_row_delim_needed)
     421                 :          32 :             CopySendChar(cstate, ',');
     422                 :             :         else
     423                 :             :         {
     424                 :             :             /* first row needs no delimiter */
     425                 :          16 :             CopySendChar(cstate, ' ');
     426                 :          16 :             cstate->json_row_delim_needed = true;
     427                 :             :         }
     428                 :             :     }
     429                 :             : 
     430                 :             :     /*
     431                 :             :      * Convert the JSON output to the target encoding if needed.  Unlike the
     432                 :             :      * text and CSV paths which convert per-attribute via CopyAttributeOut*,
     433                 :             :      * composite_to_json() emits the whole row as one buffer, so we transcode
     434                 :             :      * it here in a single call before sending.
     435                 :             :      */
     436         [ +  + ]:         332 :     if (cstate->need_transcoding)
     437                 :             :     {
     438                 :             :         char       *converted;
     439                 :             : 
     440                 :           8 :         converted = pg_server_to_any(cstate->json_buf->data,
     441                 :           8 :                                      cstate->json_buf->len,
     442                 :             :                                      cstate->file_encoding);
     443                 :           8 :         CopySendData(cstate, converted, strlen(converted));
     444         [ +  - ]:           8 :         if (converted != cstate->json_buf->data)
     445                 :           8 :             pfree(converted);
     446                 :             :     }
     447                 :             :     else
     448                 :         324 :         CopySendData(cstate, cstate->json_buf->data, cstate->json_buf->len);
     449                 :             : 
     450                 :         332 :     CopySendTextLikeEndOfRow(cstate);
     451                 :         332 : }
     452                 :             : 
     453                 :             : /*
     454                 :             :  * Implementation of the start callback for binary format. Send a header
     455                 :             :  * for a binary copy.
     456                 :             :  */
     457                 :             : static void
     458                 :           8 : CopyToBinaryStart(CopyToState cstate, TupleDesc tupDesc)
     459                 :             : {
     460                 :             :     int32       tmp;
     461                 :             : 
     462                 :             :     /* Signature */
     463                 :           8 :     CopySendData(cstate, BinarySignature, 11);
     464                 :             :     /* Flags field */
     465                 :           8 :     tmp = 0;
     466                 :           8 :     CopySendInt32(cstate, tmp);
     467                 :             :     /* No header extension */
     468                 :           8 :     tmp = 0;
     469                 :           8 :     CopySendInt32(cstate, tmp);
     470                 :           8 : }
     471                 :             : 
     472                 :             : /*
     473                 :             :  * Implementation of the outfunc callback for binary format. Assign
     474                 :             :  * the binary output function to the given *finfo.
     475                 :             :  */
     476                 :             : static void
     477                 :          38 : CopyToBinaryOutFunc(CopyToState cstate, Oid atttypid, FmgrInfo *finfo)
     478                 :             : {
     479                 :             :     Oid         func_oid;
     480                 :             :     bool        is_varlena;
     481                 :             : 
     482                 :             :     /* Set output function for an attribute */
     483                 :          38 :     getTypeBinaryOutputInfo(atttypid, &func_oid, &is_varlena);
     484                 :          37 :     fmgr_info(func_oid, finfo);
     485                 :          37 : }
     486                 :             : 
     487                 :             : /* Implementation of the per-row callback for binary format */
     488                 :             : static void
     489                 :          19 : CopyToBinaryOneRow(CopyToState cstate, TupleTableSlot *slot)
     490                 :             : {
     491                 :          19 :     FmgrInfo   *out_functions = cstate->out_functions;
     492                 :             : 
     493                 :             :     /* Binary per-tuple header */
     494                 :          19 :     CopySendInt16(cstate, list_length(cstate->attnumlist));
     495                 :             : 
     496   [ +  -  +  +  :         139 :     foreach_int(attnum, cstate->attnumlist)
                   +  + ]
     497                 :             :     {
     498                 :         101 :         Datum       value = slot->tts_values[attnum - 1];
     499                 :         101 :         bool        isnull = slot->tts_isnull[attnum - 1];
     500                 :             : 
     501         [ +  + ]:         101 :         if (isnull)
     502                 :             :         {
     503                 :          20 :             CopySendInt32(cstate, -1);
     504                 :             :         }
     505                 :             :         else
     506                 :             :         {
     507                 :             :             bytea      *outputbytes;
     508                 :             : 
     509                 :          81 :             outputbytes = SendFunctionCall(&out_functions[attnum - 1],
     510                 :             :                                            value);
     511                 :          81 :             CopySendInt32(cstate, VARSIZE(outputbytes) - VARHDRSZ);
     512                 :          81 :             CopySendData(cstate, VARDATA(outputbytes),
     513                 :          81 :                          VARSIZE(outputbytes) - VARHDRSZ);
     514                 :             :         }
     515                 :             :     }
     516                 :             : 
     517                 :          19 :     CopySendEndOfRow(cstate);
     518                 :          19 : }
     519                 :             : 
     520                 :             : /* Implementation of the end callback for binary format */
     521                 :             : static void
     522                 :           8 : CopyToBinaryEnd(CopyToState cstate)
     523                 :             : {
     524                 :             :     /* Generate trailer for a binary copy */
     525                 :           8 :     CopySendInt16(cstate, -1);
     526                 :             :     /* Need to flush out the trailer */
     527                 :           8 :     CopySendEndOfRow(cstate);
     528                 :           8 : }
     529                 :             : 
     530                 :             : /*
     531                 :             :  * Send copy start/stop messages for frontend copies.  These have changed
     532                 :             :  * in past protocol redesigns.
     533                 :             :  */
     534                 :             : static void
     535                 :        5131 : SendCopyBegin(CopyToState cstate)
     536                 :             : {
     537                 :             :     StringInfoData buf;
     538                 :        5131 :     int         natts = list_length(cstate->attnumlist);
     539                 :        5131 :     int16       format = (cstate->opts.format == COPY_FORMAT_BINARY ? 1 : 0);
     540                 :             :     int         i;
     541                 :             : 
     542                 :        5131 :     pq_beginmessage(&buf, PqMsg_CopyOutResponse);
     543                 :        5131 :     pq_sendbyte(&buf, format);  /* overall format */
     544         [ +  + ]:        5131 :     if (cstate->opts.format != COPY_FORMAT_JSON)
     545                 :             :     {
     546                 :        5035 :         pq_sendint16(&buf, natts);
     547         [ +  + ]:       22129 :         for (i = 0; i < natts; i++)
     548                 :       17094 :             pq_sendint16(&buf, format); /* per-column formats */
     549                 :             :     }
     550                 :             :     else
     551                 :             :     {
     552                 :             :         /*
     553                 :             :          * For JSON format, report one text-format column.  Each CopyData
     554                 :             :          * message contains one complete JSON object, not individual column
     555                 :             :          * values, so the per-column count is always 1.
     556                 :             :          */
     557                 :          96 :         pq_sendint16(&buf, 1);
     558                 :          96 :         pq_sendint16(&buf, 0);
     559                 :             :     }
     560                 :             : 
     561                 :        5131 :     pq_endmessage(&buf);
     562                 :        5131 :     cstate->copy_dest = COPY_FRONTEND;
     563                 :        5131 : }
     564                 :             : 
     565                 :             : static void
     566                 :        5129 : SendCopyEnd(CopyToState cstate)
     567                 :             : {
     568                 :             :     /* Shouldn't have any unsent data */
     569                 :             :     Assert(cstate->fe_msgbuf->len == 0);
     570                 :             :     /* Send Copy Done message */
     571                 :        5129 :     pq_putemptymessage(PqMsg_CopyDone);
     572                 :        5129 : }
     573                 :             : 
     574                 :             : /*----------
     575                 :             :  * CopySendData sends output data to the destination (file or frontend)
     576                 :             :  * CopySendString does the same for null-terminated strings
     577                 :             :  * CopySendChar does the same for single characters
     578                 :             :  * CopySendEndOfRow does the appropriate thing at end of each data row
     579                 :             :  *  (data is not actually flushed except by CopySendEndOfRow)
     580                 :             :  *
     581                 :             :  * NB: no data conversion is applied by these functions
     582                 :             :  *----------
     583                 :             :  */
     584                 :             : static void
     585                 :     6518807 : CopySendData(CopyToState cstate, const void *databuf, int datasize)
     586                 :             : {
     587                 :     6518807 :     appendBinaryStringInfo(cstate->fe_msgbuf, databuf, datasize);
     588                 :     6518807 : }
     589                 :             : 
     590                 :             : static void
     591                 :      614058 : CopySendString(CopyToState cstate, const char *str)
     592                 :             : {
     593                 :      614058 :     appendBinaryStringInfo(cstate->fe_msgbuf, str, strlen(str));
     594                 :      614058 : }
     595                 :             : 
     596                 :             : static void
     597                 :     7257428 : CopySendChar(CopyToState cstate, char c)
     598                 :             : {
     599         [ +  + ]:     7257428 :     appendStringInfoCharMacro(cstate->fe_msgbuf, c);
     600                 :     7257428 : }
     601                 :             : 
     602                 :             : static void
     603                 :     1841419 : CopySendEndOfRow(CopyToState cstate)
     604                 :             : {
     605                 :     1841419 :     StringInfo  fe_msgbuf = cstate->fe_msgbuf;
     606                 :             : 
     607   [ +  +  +  - ]:     1841419 :     switch (cstate->copy_dest)
     608                 :             :     {
     609                 :        8203 :         case COPY_FILE:
     610                 :        8203 :             pgstat_report_wait_start(WAIT_EVENT_COPY_TO_WRITE);
     611         [ +  - ]:        8203 :             if (fwrite(fe_msgbuf->data, fe_msgbuf->len, 1,
     612         [ -  + ]:        8203 :                        cstate->copy_file) != 1 ||
     613                 :        8203 :                 ferror(cstate->copy_file))
     614                 :             :             {
     615         [ #  # ]:           0 :                 if (cstate->is_program)
     616                 :             :                 {
     617         [ #  # ]:           0 :                     if (errno == EPIPE)
     618                 :             :                     {
     619                 :             :                         /*
     620                 :             :                          * The pipe will be closed automatically on error at
     621                 :             :                          * the end of transaction, but we might get a better
     622                 :             :                          * error message from the subprocess' exit code than
     623                 :             :                          * just "Broken Pipe"
     624                 :             :                          */
     625                 :           0 :                         ClosePipeToProgram(cstate);
     626                 :             : 
     627                 :             :                         /*
     628                 :             :                          * If ClosePipeToProgram() didn't throw an error, the
     629                 :             :                          * program terminated normally, but closed the pipe
     630                 :             :                          * first. Restore errno, and throw an error.
     631                 :             :                          */
     632                 :           0 :                         errno = EPIPE;
     633                 :             :                     }
     634         [ #  # ]:           0 :                     ereport(ERROR,
     635                 :             :                             (errcode_for_file_access(),
     636                 :             :                              errmsg("could not write to COPY program: %m")));
     637                 :             :                 }
     638                 :             :                 else
     639         [ #  # ]:           0 :                     ereport(ERROR,
     640                 :             :                             (errcode_for_file_access(),
     641                 :             :                              errmsg("could not write to COPY file: %m")));
     642                 :             :             }
     643                 :        8203 :             pgstat_report_wait_end();
     644                 :        8203 :             break;
     645                 :     1833213 :         case COPY_FRONTEND:
     646                 :             :             /* Dump the accumulated row as one CopyData message */
     647                 :     1833213 :             (void) pq_putmessage(PqMsg_CopyData, fe_msgbuf->data, fe_msgbuf->len);
     648                 :     1833213 :             break;
     649                 :           3 :         case COPY_CALLBACK:
     650                 :           3 :             cstate->data_dest_cb(fe_msgbuf->data, fe_msgbuf->len);
     651                 :           3 :             break;
     652                 :             :     }
     653                 :             : 
     654                 :             :     /* Update the progress */
     655                 :     1841419 :     cstate->bytes_processed += fe_msgbuf->len;
     656                 :     1841419 :     pgstat_progress_update_param(PROGRESS_COPY_BYTES_PROCESSED, cstate->bytes_processed);
     657                 :             : 
     658                 :     1841419 :     resetStringInfo(fe_msgbuf);
     659                 :     1841419 : }
     660                 :             : 
     661                 :             : /*
     662                 :             :  * Wrapper function of CopySendEndOfRow for text, CSV, and json formats. Sends the
     663                 :             :  * line termination and do common appropriate things for the end of row.
     664                 :             :  */
     665                 :             : static inline void
     666                 :     1841392 : CopySendTextLikeEndOfRow(CopyToState cstate)
     667                 :             : {
     668      [ +  +  + ]:     1841392 :     switch (cstate->copy_dest)
     669                 :             :     {
     670                 :        8187 :         case COPY_FILE:
     671                 :             :             /* Default line termination depends on platform */
     672                 :             : #ifndef WIN32
     673                 :        8187 :             CopySendChar(cstate, '\n');
     674                 :             : #else
     675                 :             :             CopySendString(cstate, "\r\n");
     676                 :             : #endif
     677                 :        8187 :             break;
     678                 :     1833202 :         case COPY_FRONTEND:
     679                 :             :             /* The FE/BE protocol uses \n as newline for all platforms */
     680                 :     1833202 :             CopySendChar(cstate, '\n');
     681                 :     1833202 :             break;
     682                 :           3 :         default:
     683                 :           3 :             break;
     684                 :             :     }
     685                 :             : 
     686                 :             :     /* Now take the actions related to the end of a row */
     687                 :     1841392 :     CopySendEndOfRow(cstate);
     688                 :     1841392 : }
     689                 :             : 
     690                 :             : /*
     691                 :             :  * These functions do apply some data conversion
     692                 :             :  */
     693                 :             : 
     694                 :             : /*
     695                 :             :  * CopySendInt32 sends an int32 in network byte order
     696                 :             :  */
     697                 :             : static inline void
     698                 :         117 : CopySendInt32(CopyToState cstate, int32 val)
     699                 :             : {
     700                 :             :     uint32      buf;
     701                 :             : 
     702                 :         117 :     buf = pg_hton32((uint32) val);
     703                 :         117 :     CopySendData(cstate, &buf, sizeof(buf));
     704                 :         117 : }
     705                 :             : 
     706                 :             : /*
     707                 :             :  * CopySendInt16 sends an int16 in network byte order
     708                 :             :  */
     709                 :             : static inline void
     710                 :          27 : CopySendInt16(CopyToState cstate, int16 val)
     711                 :             : {
     712                 :             :     uint16      buf;
     713                 :             : 
     714                 :          27 :     buf = pg_hton16((uint16) val);
     715                 :          27 :     CopySendData(cstate, &buf, sizeof(buf));
     716                 :          27 : }
     717                 :             : 
     718                 :             : /*
     719                 :             :  * Closes the pipe to an external program, checking the pclose() return code.
     720                 :             :  */
     721                 :             : static void
     722                 :           0 : ClosePipeToProgram(CopyToState cstate)
     723                 :             : {
     724                 :             :     int         pclose_rc;
     725                 :             : 
     726                 :             :     Assert(cstate->is_program);
     727                 :             : 
     728                 :           0 :     pclose_rc = ClosePipeStream(cstate->copy_file);
     729         [ #  # ]:           0 :     if (pclose_rc == -1)
     730         [ #  # ]:           0 :         ereport(ERROR,
     731                 :             :                 (errcode_for_file_access(),
     732                 :             :                  errmsg("could not close pipe to external command: %m")));
     733         [ #  # ]:           0 :     else if (pclose_rc != 0)
     734                 :             :     {
     735         [ #  # ]:           0 :         ereport(ERROR,
     736                 :             :                 (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
     737                 :             :                  errmsg("program \"%s\" failed",
     738                 :             :                         cstate->filename),
     739                 :             :                  errdetail_internal("%s", wait_result_to_str(pclose_rc))));
     740                 :             :     }
     741                 :           0 : }
     742                 :             : 
     743                 :             : /*
     744                 :             :  * Release resources allocated in a cstate for COPY TO.
     745                 :             :  */
     746                 :             : static void
     747                 :        5187 : EndCopy(CopyToState cstate)
     748                 :             : {
     749         [ -  + ]:        5187 :     if (cstate->is_program)
     750                 :             :     {
     751                 :           0 :         ClosePipeToProgram(cstate);
     752                 :             :     }
     753                 :             :     else
     754                 :             :     {
     755   [ +  +  -  + ]:        5187 :         if (cstate->filename != NULL && FreeFile(cstate->copy_file))
     756         [ #  # ]:           0 :             ereport(ERROR,
     757                 :             :                     (errcode_for_file_access(),
     758                 :             :                      errmsg("could not close file \"%s\": %m",
     759                 :             :                             cstate->filename)));
     760                 :             :     }
     761                 :             : 
     762                 :        5187 :     pgstat_progress_end_command();
     763                 :             : 
     764                 :        5187 :     MemoryContextDelete(cstate->copycontext);
     765                 :             : 
     766         [ +  + ]:        5187 :     if (cstate->partitions)
     767                 :          23 :         list_free(cstate->partitions);
     768                 :             : 
     769                 :        5187 :     pfree(cstate);
     770                 :        5187 : }
     771                 :             : 
     772                 :             : /*
     773                 :             :  * Setup CopyToState to read tuples from a table or a query for COPY TO.
     774                 :             :  *
     775                 :             :  * 'rel': Relation to be copied
     776                 :             :  * 'raw_query': Query whose results are to be copied
     777                 :             :  * 'queryRelId': OID of base relation to convert to a query (for RLS)
     778                 :             :  * 'filename': Name of server-local file to write, NULL for STDOUT
     779                 :             :  * 'is_program': true if 'filename' is program to execute
     780                 :             :  * 'data_dest_cb': Callback that processes the output data
     781                 :             :  * 'attnamelist': List of char *, columns to include. NIL selects all cols.
     782                 :             :  * 'options': List of DefElem. See copy_opt_item in gram.y for selections.
     783                 :             :  *
     784                 :             :  * Returns a CopyToState, to be passed to DoCopyTo() and related functions.
     785                 :             :  */
     786                 :             : CopyToState
     787                 :        5387 : BeginCopyTo(ParseState *pstate,
     788                 :             :             Relation rel,
     789                 :             :             RawStmt *raw_query,
     790                 :             :             Oid queryRelId,
     791                 :             :             const char *filename,
     792                 :             :             bool is_program,
     793                 :             :             copy_data_dest_cb data_dest_cb,
     794                 :             :             List *attnamelist,
     795                 :             :             List *options)
     796                 :             : {
     797                 :             :     CopyToState cstate;
     798   [ +  +  +  + ]:        5387 :     bool        pipe = (filename == NULL && data_dest_cb == NULL);
     799                 :             :     TupleDesc   tupDesc;
     800                 :             :     int         num_phys_attrs;
     801                 :             :     MemoryContext oldcontext;
     802                 :        5387 :     const int   progress_cols[] = {
     803                 :             :         PROGRESS_COPY_COMMAND,
     804                 :             :         PROGRESS_COPY_TYPE
     805                 :             :     };
     806                 :        5387 :     int64       progress_vals[] = {
     807                 :             :         PROGRESS_COPY_COMMAND_TO,
     808                 :             :         0
     809                 :             :     };
     810                 :        5387 :     List       *children = NIL;
     811                 :             : 
     812   [ +  +  +  + ]:        5387 :     if (rel != NULL && rel->rd_rel->relkind != RELKIND_RELATION)
     813                 :             :     {
     814         [ +  + ]:          40 :         if (rel->rd_rel->relkind == RELKIND_VIEW)
     815         [ +  - ]:           8 :             ereport(ERROR,
     816                 :             :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
     817                 :             :                      errmsg("cannot copy from view \"%s\"",
     818                 :             :                             RelationGetRelationName(rel)),
     819                 :             :                      errhint("Try the COPY (SELECT ...) TO variant.")));
     820         [ +  + ]:          32 :         else if (rel->rd_rel->relkind == RELKIND_MATVIEW)
     821                 :             :         {
     822         [ +  + ]:           8 :             if (!RelationIsPopulated(rel))
     823         [ +  - ]:           4 :                 ereport(ERROR,
     824                 :             :                         errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     825                 :             :                         errmsg("cannot copy from unpopulated materialized view \"%s\"",
     826                 :             :                                RelationGetRelationName(rel)),
     827                 :             :                         errhint("Use the REFRESH MATERIALIZED VIEW command."));
     828                 :             :         }
     829         [ -  + ]:          24 :         else if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
     830         [ #  # ]:           0 :             ereport(ERROR,
     831                 :             :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
     832                 :             :                      errmsg("cannot copy from foreign table \"%s\"",
     833                 :             :                             RelationGetRelationName(rel)),
     834                 :             :                      errhint("Try the COPY (SELECT ...) TO variant.")));
     835         [ -  + ]:          24 :         else if (rel->rd_rel->relkind == RELKIND_SEQUENCE)
     836         [ #  # ]:           0 :             ereport(ERROR,
     837                 :             :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
     838                 :             :                      errmsg("cannot copy from sequence \"%s\"",
     839                 :             :                             RelationGetRelationName(rel))));
     840         [ +  - ]:          24 :         else if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
     841                 :             :         {
     842                 :             :             /*
     843                 :             :              * Collect OIDs of relation containing data, so that later
     844                 :             :              * DoCopyTo can copy the data from them.
     845                 :             :              */
     846                 :          24 :             children = find_all_inheritors(RelationGetRelid(rel), AccessShareLock, NULL);
     847                 :             : 
     848   [ +  -  +  +  :         120 :             foreach_oid(child, children)
                   +  + ]
     849                 :             :             {
     850                 :          74 :                 char        relkind = get_rel_relkind(child);
     851                 :             : 
     852         [ +  + ]:          74 :                 if (relkind == RELKIND_FOREIGN_TABLE)
     853                 :             :                 {
     854                 :           1 :                     char       *relation_name = get_rel_name(child);
     855                 :             : 
     856         [ +  - ]:           1 :                     ereport(ERROR,
     857                 :             :                             errcode(ERRCODE_WRONG_OBJECT_TYPE),
     858                 :             :                             errmsg("cannot copy from foreign table \"%s\"", relation_name),
     859                 :             :                             errdetail("Partition \"%s\" is a foreign table in partitioned table \"%s\"",
     860                 :             :                                       relation_name, RelationGetRelationName(rel)),
     861                 :             :                             errhint("Try the COPY (SELECT ...) TO variant."));
     862                 :             :                 }
     863                 :             : 
     864                 :             :                 /* Exclude tables with no data */
     865   [ +  +  -  + ]:          73 :                 if (RELKIND_HAS_PARTITIONS(relkind))
     866                 :          35 :                     children = foreach_delete_current(children, child);
     867                 :             :             }
     868                 :             :         }
     869                 :             :         else
     870         [ #  # ]:           0 :             ereport(ERROR,
     871                 :             :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
     872                 :             :                      errmsg("cannot copy from non-table relation \"%s\"",
     873                 :             :                             RelationGetRelationName(rel))));
     874                 :             :     }
     875                 :             : 
     876                 :             : 
     877                 :             :     /* Allocate workspace and zero all fields */
     878                 :        5374 :     cstate = palloc0_object(CopyToStateData);
     879                 :             : 
     880                 :             :     /*
     881                 :             :      * We allocate everything used by a cstate in a new memory context. This
     882                 :             :      * avoids memory leaks during repeated use of COPY in a query.
     883                 :             :      */
     884                 :        5374 :     cstate->copycontext = AllocSetContextCreate(CurrentMemoryContext,
     885                 :             :                                                 "COPY",
     886                 :             :                                                 ALLOCSET_DEFAULT_SIZES);
     887                 :             : 
     888                 :        5374 :     oldcontext = MemoryContextSwitchTo(cstate->copycontext);
     889                 :             : 
     890                 :             :     /* Extract options from the statement node tree */
     891                 :        5374 :     ProcessCopyOptions(pstate, &cstate->opts, false /* is_from */ , options);
     892                 :             : 
     893                 :             :     /* Set format routine */
     894                 :        5278 :     cstate->routine = CopyToGetRoutine(&cstate->opts);
     895                 :             : 
     896                 :             :     /* Process the source/target relation or query */
     897         [ +  + ]:        5278 :     if (rel)
     898                 :             :     {
     899                 :             :         Assert(!raw_query);
     900                 :             : 
     901                 :        4871 :         cstate->rel = rel;
     902                 :             : 
     903                 :        4871 :         tupDesc = RelationGetDescr(cstate->rel);
     904                 :        4871 :         cstate->partitions = children;
     905                 :        4871 :         cstate->tupDesc = tupDesc;
     906                 :             :     }
     907                 :             :     else
     908                 :             :     {
     909                 :             :         List       *rewritten;
     910                 :             :         Query      *query;
     911                 :             :         PlannedStmt *plan;
     912                 :             :         DestReceiver *dest;
     913                 :             : 
     914                 :         407 :         cstate->rel = NULL;
     915                 :         407 :         cstate->partitions = NIL;
     916                 :             : 
     917                 :             :         /*
     918                 :             :          * Run parse analysis and rewrite.  Note this also acquires sufficient
     919                 :             :          * locks on the source table(s).
     920                 :             :          */
     921                 :         407 :         rewritten = pg_analyze_and_rewrite_fixedparams(raw_query,
     922                 :             :                                                        pstate->p_sourcetext, NULL, 0,
     923                 :             :                                                        NULL);
     924                 :             : 
     925                 :             :         /* check that we got back something we can work with */
     926         [ +  + ]:         399 :         if (rewritten == NIL)
     927                 :             :         {
     928         [ +  - ]:          12 :             ereport(ERROR,
     929                 :             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     930                 :             :                      errmsg("DO INSTEAD NOTHING rules are not supported for COPY")));
     931                 :             :         }
     932         [ +  + ]:         387 :         else if (list_length(rewritten) > 1)
     933                 :             :         {
     934                 :             :             ListCell   *lc;
     935                 :             : 
     936                 :             :             /* examine queries to determine which error message to issue */
     937   [ +  -  +  +  :          68 :             foreach(lc, rewritten)
                   +  + ]
     938                 :             :             {
     939                 :          56 :                 Query      *q = lfirst_node(Query, lc);
     940                 :             : 
     941         [ +  + ]:          56 :                 if (q->querySource == QSRC_QUAL_INSTEAD_RULE)
     942         [ +  - ]:          12 :                     ereport(ERROR,
     943                 :             :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     944                 :             :                              errmsg("conditional DO INSTEAD rules are not supported for COPY")));
     945         [ +  + ]:          44 :                 if (q->querySource == QSRC_NON_INSTEAD_RULE)
     946         [ +  - ]:          12 :                     ereport(ERROR,
     947                 :             :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     948                 :             :                              errmsg("DO ALSO rules are not supported for COPY")));
     949                 :             :             }
     950                 :             : 
     951         [ +  - ]:          12 :             ereport(ERROR,
     952                 :             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     953                 :             :                      errmsg("multi-statement DO INSTEAD rules are not supported for COPY")));
     954                 :             :         }
     955                 :             : 
     956                 :         351 :         query = linitial_node(Query, rewritten);
     957                 :             : 
     958                 :             :         /* The grammar allows SELECT INTO, but we don't support that */
     959         [ +  + ]:         351 :         if (query->utilityStmt != NULL &&
     960         [ +  + ]:          12 :             IsA(query->utilityStmt, CreateTableAsStmt))
     961         [ +  - ]:           8 :             ereport(ERROR,
     962                 :             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     963                 :             :                      errmsg("COPY (SELECT INTO) is not supported")));
     964                 :             : 
     965                 :             :         /* The only other utility command we could see is NOTIFY */
     966         [ +  + ]:         343 :         if (query->utilityStmt != NULL)
     967         [ +  - ]:           4 :             ereport(ERROR,
     968                 :             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     969                 :             :                      errmsg("COPY query must not be a utility command")));
     970                 :             : 
     971                 :             :         /*
     972                 :             :          * Similarly the grammar doesn't enforce the presence of a RETURNING
     973                 :             :          * clause, but this is required here.
     974                 :             :          */
     975         [ +  + ]:         339 :         if (query->commandType != CMD_SELECT &&
     976         [ +  + ]:          69 :             query->returningList == NIL)
     977                 :             :         {
     978                 :             :             Assert(query->commandType == CMD_INSERT ||
     979                 :             :                    query->commandType == CMD_UPDATE ||
     980                 :             :                    query->commandType == CMD_DELETE ||
     981                 :             :                    query->commandType == CMD_MERGE);
     982                 :             : 
     983         [ +  - ]:          16 :             ereport(ERROR,
     984                 :             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     985                 :             :                      errmsg("COPY query must have a RETURNING clause")));
     986                 :             :         }
     987                 :             : 
     988                 :             :         /* plan the query */
     989                 :         323 :         plan = pg_plan_query(query, pstate->p_sourcetext,
     990                 :             :                              CURSOR_OPT_PARALLEL_OK, NULL, NULL);
     991                 :             : 
     992                 :             :         /*
     993                 :             :          * With row-level security and a user using "COPY relation TO", we
     994                 :             :          * have to convert the "COPY relation TO" to a query-based COPY (eg:
     995                 :             :          * "COPY (SELECT * FROM ONLY relation) TO"), to allow the rewriter to
     996                 :             :          * add in any RLS clauses.
     997                 :             :          *
     998                 :             :          * When this happens, we are passed in the relid of the originally
     999                 :             :          * found relation (which we have locked).  As the planner will look up
    1000                 :             :          * the relation again, we double-check here to make sure it found the
    1001                 :             :          * same one that we have locked.
    1002                 :             :          */
    1003         [ +  + ]:         322 :         if (queryRelId != InvalidOid)
    1004                 :             :         {
    1005                 :             :             /*
    1006                 :             :              * Note that with RLS involved there may be multiple relations,
    1007                 :             :              * and while the one we need is almost certainly first, we don't
    1008                 :             :              * make any guarantees of that in the planner, so check the whole
    1009                 :             :              * list and make sure we find the original relation.
    1010                 :             :              */
    1011         [ -  + ]:          52 :             if (!list_member_oid(plan->relationOids, queryRelId))
    1012         [ #  # ]:           0 :                 ereport(ERROR,
    1013                 :             :                         (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    1014                 :             :                          errmsg("relation referenced by COPY statement has changed")));
    1015                 :             :         }
    1016                 :             : 
    1017                 :             :         /*
    1018                 :             :          * Use a snapshot with an updated command ID to ensure this query sees
    1019                 :             :          * results of any previously executed queries.
    1020                 :             :          */
    1021                 :         322 :         PushCopiedSnapshot(GetActiveSnapshot());
    1022                 :         322 :         UpdateActiveSnapshotCommandId();
    1023                 :             : 
    1024                 :             :         /* Create dest receiver for COPY OUT */
    1025                 :         322 :         dest = CreateDestReceiver(DestCopyOut);
    1026                 :         322 :         ((DR_copy *) dest)->cstate = cstate;
    1027                 :             : 
    1028                 :             :         /* Create a QueryDesc requesting no output */
    1029                 :         322 :         cstate->queryDesc = CreateQueryDesc(plan, pstate->p_sourcetext,
    1030                 :             :                                             GetActiveSnapshot(),
    1031                 :             :                                             InvalidSnapshot,
    1032                 :             :                                             dest, NULL, NULL, 0);
    1033                 :             : 
    1034                 :             :         /*
    1035                 :             :          * Call ExecutorStart to prepare the plan for execution.
    1036                 :             :          *
    1037                 :             :          * ExecutorStart computes a result tupdesc for us
    1038                 :             :          */
    1039                 :         322 :         ExecutorStart(cstate->queryDesc, 0);
    1040                 :             : 
    1041                 :         318 :         tupDesc = cstate->queryDesc->tupDesc;
    1042                 :         318 :         tupDesc = BlessTupleDesc(tupDesc);
    1043                 :         318 :         cstate->tupDesc = tupDesc;
    1044                 :             :     }
    1045                 :             : 
    1046                 :             :     /* Generate or convert list of attributes to process */
    1047                 :        5189 :     cstate->attnumlist = CopyGetAttnums(tupDesc, cstate->rel, attnamelist);
    1048                 :             : 
    1049                 :             :     /* Set up JSON-specific state */
    1050         [ +  + ]:        5189 :     if (cstate->opts.format == COPY_FORMAT_JSON)
    1051                 :             :     {
    1052                 :         104 :         cstate->json_buf = makeStringInfo();
    1053                 :             : 
    1054                 :             :         /*
    1055                 :             :          * Build a projected TupleDesc describing only the selected columns so
    1056                 :             :          * that composite_to_json() emits the right column names and types;
    1057                 :             :          * needed when an explicit column list was given (possibly with a
    1058                 :             :          * different column order) or when generated columns are excluded from
    1059                 :             :          * the output.
    1060                 :             :          */
    1061   [ +  +  +  + ]:         104 :         if (rel && (attnamelist != NIL ||
    1062         [ +  + ]:          32 :                     list_length(cstate->attnumlist) < tupDesc->natts))
    1063                 :             :         {
    1064                 :          44 :             int         natts = list_length(cstate->attnumlist);
    1065                 :             :             TupleDesc   resultDesc;
    1066                 :             : 
    1067                 :          44 :             resultDesc = CreateTemplateTupleDesc(natts);
    1068                 :             : 
    1069   [ +  -  +  +  :         180 :             foreach_int(attnum, cstate->attnumlist)
                   +  + ]
    1070                 :             :             {
    1071                 :          92 :                 Form_pg_attribute attr = TupleDescAttr(tupDesc, attnum - 1);
    1072                 :             : 
    1073                 :          92 :                 TupleDescInitEntry(resultDesc,
    1074                 :          92 :                                    foreach_current_index(attnum) + 1,
    1075                 :          92 :                                    NameStr(attr->attname),
    1076                 :             :                                    attr->atttypid,
    1077                 :             :                                    attr->atttypmod,
    1078                 :          92 :                                    attr->attndims);
    1079                 :             :             }
    1080                 :             : 
    1081                 :          44 :             TupleDescFinalize(resultDesc);
    1082                 :          44 :             cstate->tupDesc = BlessTupleDesc(resultDesc);
    1083                 :             : 
    1084                 :             :             /*
    1085                 :             :              * Pre-allocate arrays for projecting selected column values into
    1086                 :             :              * sequential positions matching the custom TupleDesc.
    1087                 :             :              */
    1088                 :          44 :             cstate->json_projvalues = palloc_array(Datum, natts);
    1089                 :          44 :             cstate->json_projnulls = palloc_array(bool, natts);
    1090                 :             :         }
    1091                 :             :     }
    1092                 :             : 
    1093                 :        5189 :     num_phys_attrs = tupDesc->natts;
    1094                 :             : 
    1095                 :             :     /* Convert FORCE_QUOTE name list to per-column flags, check validity */
    1096                 :        5189 :     cstate->opts.force_quote_flags = (bool *) palloc0(num_phys_attrs * sizeof(bool));
    1097         [ +  + ]:        5189 :     if (cstate->opts.force_quote_all)
    1098                 :             :     {
    1099   [ +  -  -  +  :          12 :         MemSet(cstate->opts.force_quote_flags, true, num_phys_attrs * sizeof(bool));
          -  -  -  -  -  
                      - ]
    1100                 :             :     }
    1101         [ +  + ]:        5177 :     else if (cstate->opts.force_quote)
    1102                 :             :     {
    1103                 :             :         List       *attnums;
    1104                 :             :         ListCell   *cur;
    1105                 :             : 
    1106                 :          16 :         attnums = CopyGetAttnums(tupDesc, cstate->rel, cstate->opts.force_quote);
    1107                 :             : 
    1108   [ +  -  +  +  :          32 :         foreach(cur, attnums)
                   +  + ]
    1109                 :             :         {
    1110                 :          16 :             int         attnum = lfirst_int(cur);
    1111                 :          16 :             Form_pg_attribute attr = TupleDescAttr(tupDesc, attnum - 1);
    1112                 :             : 
    1113         [ -  + ]:          16 :             if (!list_member_int(cstate->attnumlist, attnum))
    1114         [ #  # ]:           0 :                 ereport(ERROR,
    1115                 :             :                         (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
    1116                 :             :                 /*- translator: %s is the name of a COPY option, e.g. FORCE_NOT_NULL */
    1117                 :             :                          errmsg("%s column \"%s\" not referenced by COPY",
    1118                 :             :                                 "FORCE_QUOTE", NameStr(attr->attname))));
    1119                 :          16 :             cstate->opts.force_quote_flags[attnum - 1] = true;
    1120                 :             :         }
    1121                 :             :     }
    1122                 :             : 
    1123                 :             :     /* Use client encoding when ENCODING option is not specified. */
    1124         [ +  + ]:        5189 :     if (cstate->opts.file_encoding < 0)
    1125                 :        5165 :         cstate->file_encoding = pg_get_client_encoding();
    1126                 :             :     else
    1127                 :          24 :         cstate->file_encoding = cstate->opts.file_encoding;
    1128                 :             : 
    1129                 :             :     /*
    1130                 :             :      * Set up encoding conversion info if the file and server encodings differ
    1131                 :             :      * (see also pg_server_to_any).
    1132                 :             :      */
    1133         [ +  + ]:        5189 :     if (cstate->file_encoding == GetDatabaseEncoding() ||
    1134         [ +  + ]:          21 :         cstate->file_encoding == PG_SQL_ASCII)
    1135                 :        5172 :         cstate->need_transcoding = false;
    1136                 :             :     else
    1137                 :          17 :         cstate->need_transcoding = true;
    1138                 :             : 
    1139                 :             :     /* See Multibyte encoding comment above */
    1140   [ +  +  +  - ]:        5189 :     cstate->encoding_embeds_ascii = PG_ENCODING_IS_CLIENT_ONLY(cstate->file_encoding);
    1141                 :             : 
    1142                 :        5189 :     cstate->copy_dest = COPY_FILE;   /* default */
    1143                 :             : 
    1144         [ +  + ]:        5189 :     if (data_dest_cb)
    1145                 :             :     {
    1146                 :           1 :         progress_vals[1] = PROGRESS_COPY_TYPE_CALLBACK;
    1147                 :           1 :         cstate->copy_dest = COPY_CALLBACK;
    1148                 :           1 :         cstate->data_dest_cb = data_dest_cb;
    1149                 :             :     }
    1150         [ +  + ]:        5188 :     else if (pipe)
    1151                 :             :     {
    1152                 :        5131 :         progress_vals[1] = PROGRESS_COPY_TYPE_PIPE;
    1153                 :             : 
    1154                 :             :         Assert(!is_program);    /* the grammar does not allow this */
    1155         [ -  + ]:        5131 :         if (whereToSendOutput != DestRemote)
    1156                 :           0 :             cstate->copy_file = stdout;
    1157                 :             :     }
    1158                 :             :     else
    1159                 :             :     {
    1160                 :          57 :         cstate->filename = pstrdup(filename);
    1161                 :          57 :         cstate->is_program = is_program;
    1162                 :             : 
    1163         [ -  + ]:          57 :         if (is_program)
    1164                 :             :         {
    1165                 :           0 :             progress_vals[1] = PROGRESS_COPY_TYPE_PROGRAM;
    1166                 :           0 :             cstate->copy_file = OpenPipeStream(cstate->filename, PG_BINARY_W);
    1167         [ #  # ]:           0 :             if (cstate->copy_file == NULL)
    1168         [ #  # ]:           0 :                 ereport(ERROR,
    1169                 :             :                         (errcode_for_file_access(),
    1170                 :             :                          errmsg("could not execute command \"%s\": %m",
    1171                 :             :                                 cstate->filename)));
    1172                 :             :         }
    1173                 :             :         else
    1174                 :             :         {
    1175                 :             :             mode_t      oumask; /* Pre-existing umask value */
    1176                 :             :             struct stat st;
    1177                 :             : 
    1178                 :          57 :             progress_vals[1] = PROGRESS_COPY_TYPE_FILE;
    1179                 :             : 
    1180                 :             :             /*
    1181                 :             :              * Prevent write to relative path ... too easy to shoot oneself in
    1182                 :             :              * the foot by overwriting a database file ...
    1183                 :             :              */
    1184         [ -  + ]:          57 :             if (!is_absolute_path(filename))
    1185         [ #  # ]:           0 :                 ereport(ERROR,
    1186                 :             :                         (errcode(ERRCODE_INVALID_NAME),
    1187                 :             :                          errmsg("relative path not allowed for COPY to file")));
    1188                 :             : 
    1189                 :          57 :             oumask = umask(S_IWGRP | S_IWOTH);
    1190         [ +  - ]:          57 :             PG_TRY();
    1191                 :             :             {
    1192                 :          57 :                 cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_W);
    1193                 :             :             }
    1194                 :           0 :             PG_FINALLY();
    1195                 :             :             {
    1196                 :          57 :                 umask(oumask);
    1197                 :             :             }
    1198         [ -  + ]:          57 :             PG_END_TRY();
    1199         [ -  + ]:          57 :             if (cstate->copy_file == NULL)
    1200                 :             :             {
    1201                 :             :                 /* copy errno because ereport subfunctions might change it */
    1202                 :           0 :                 int         save_errno = errno;
    1203                 :             : 
    1204   [ #  #  #  #  :           0 :                 ereport(ERROR,
                   #  # ]
    1205                 :             :                         (errcode_for_file_access(),
    1206                 :             :                          errmsg("could not open file \"%s\" for writing: %m",
    1207                 :             :                                 cstate->filename),
    1208                 :             :                          (save_errno == ENOENT || save_errno == EACCES) ?
    1209                 :             :                          errhint("COPY TO instructs the PostgreSQL server process to write a file. "
    1210                 :             :                                  "You may want a client-side facility such as psql's \\copy.") : 0));
    1211                 :             :             }
    1212                 :             : 
    1213         [ -  + ]:          57 :             if (fstat(fileno(cstate->copy_file), &st))
    1214         [ #  # ]:           0 :                 ereport(ERROR,
    1215                 :             :                         (errcode_for_file_access(),
    1216                 :             :                          errmsg("could not stat file \"%s\": %m",
    1217                 :             :                                 cstate->filename)));
    1218                 :             : 
    1219         [ -  + ]:          57 :             if (S_ISDIR(st.st_mode))
    1220         [ #  # ]:           0 :                 ereport(ERROR,
    1221                 :             :                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    1222                 :             :                          errmsg("\"%s\" is a directory", cstate->filename)));
    1223                 :             :         }
    1224                 :             :     }
    1225                 :             : 
    1226                 :             :     /* initialize progress */
    1227                 :        5189 :     pgstat_progress_start_command(PROGRESS_COMMAND_COPY,
    1228         [ +  + ]:        5189 :                                   cstate->rel ? RelationGetRelid(cstate->rel) : InvalidOid);
    1229                 :        5189 :     pgstat_progress_update_multi_param(2, progress_cols, progress_vals);
    1230                 :             : 
    1231                 :        5189 :     cstate->bytes_processed = 0;
    1232                 :             : 
    1233                 :        5189 :     MemoryContextSwitchTo(oldcontext);
    1234                 :             : 
    1235                 :        5189 :     return cstate;
    1236                 :             : }
    1237                 :             : 
    1238                 :             : /*
    1239                 :             :  * Clean up storage and release resources for COPY TO.
    1240                 :             :  */
    1241                 :             : void
    1242                 :        5187 : EndCopyTo(CopyToState cstate)
    1243                 :             : {
    1244         [ +  + ]:        5187 :     if (cstate->queryDesc != NULL)
    1245                 :             :     {
    1246                 :             :         /* Close down the query and free resources. */
    1247                 :         318 :         ExecutorFinish(cstate->queryDesc);
    1248                 :         318 :         ExecutorEnd(cstate->queryDesc);
    1249                 :         318 :         FreeQueryDesc(cstate->queryDesc);
    1250                 :         318 :         PopActiveSnapshot();
    1251                 :             :     }
    1252                 :             : 
    1253                 :             :     /* Clean up storage */
    1254                 :        5187 :     EndCopy(cstate);
    1255                 :        5187 : }
    1256                 :             : 
    1257                 :             : /*
    1258                 :             :  * Copy from relation or query TO file.
    1259                 :             :  *
    1260                 :             :  * Returns the number of rows processed.
    1261                 :             :  */
    1262                 :             : uint64
    1263                 :        5189 : DoCopyTo(CopyToState cstate)
    1264                 :             : {
    1265   [ +  +  +  + ]:        5189 :     bool        pipe = (cstate->filename == NULL && cstate->data_dest_cb == NULL);
    1266   [ +  +  +  - ]:        5189 :     bool        fe_copy = (pipe && whereToSendOutput == DestRemote);
    1267                 :             :     TupleDesc   tupDesc;
    1268                 :             :     int         num_phys_attrs;
    1269                 :             :     ListCell   *cur;
    1270                 :        5189 :     uint64      processed = 0;
    1271                 :             : 
    1272         [ +  + ]:        5189 :     if (fe_copy)
    1273                 :        5131 :         SendCopyBegin(cstate);
    1274                 :             : 
    1275         [ +  + ]:        5189 :     if (cstate->rel)
    1276                 :        4871 :         tupDesc = RelationGetDescr(cstate->rel);
    1277                 :             :     else
    1278                 :         318 :         tupDesc = cstate->queryDesc->tupDesc;
    1279                 :        5189 :     num_phys_attrs = tupDesc->natts;
    1280                 :        5189 :     cstate->opts.null_print_client = cstate->opts.null_print; /* default */
    1281                 :             : 
    1282                 :             :     /* We use fe_msgbuf as a per-row buffer regardless of copy_dest */
    1283                 :        5189 :     cstate->fe_msgbuf = makeStringInfo();
    1284                 :             : 
    1285                 :             :     /* Get info about the columns we need to process. */
    1286                 :        5189 :     cstate->out_functions = (FmgrInfo *) palloc(num_phys_attrs * sizeof(FmgrInfo));
    1287   [ +  +  +  +  :       22671 :     foreach(cur, cstate->attnumlist)
                   +  + ]
    1288                 :             :     {
    1289                 :       17483 :         int         attnum = lfirst_int(cur);
    1290                 :       17483 :         Form_pg_attribute attr = TupleDescAttr(tupDesc, attnum - 1);
    1291                 :             : 
    1292                 :       17483 :         cstate->routine->CopyToOutFunc(cstate, attr->atttypid,
    1293                 :       17483 :                                        &cstate->out_functions[attnum - 1]);
    1294                 :             :     }
    1295                 :             : 
    1296                 :             :     /*
    1297                 :             :      * Create a temporary memory context that we can reset once per row to
    1298                 :             :      * recover palloc'd memory.  This avoids any problems with leaks inside
    1299                 :             :      * datatype output routines, and should be faster than retail pfree's
    1300                 :             :      * anyway.  (We don't need a whole econtext as CopyFrom does.)
    1301                 :             :      */
    1302                 :        5188 :     cstate->rowcontext = AllocSetContextCreate(CurrentMemoryContext,
    1303                 :             :                                                "COPY TO",
    1304                 :             :                                                ALLOCSET_DEFAULT_SIZES);
    1305                 :             : 
    1306                 :        5188 :     cstate->routine->CopyToStart(cstate, tupDesc);
    1307                 :             : 
    1308         [ +  + ]:        5188 :     if (cstate->rel)
    1309                 :             :     {
    1310                 :             :         /*
    1311                 :             :          * If COPY TO source table is a partitioned table, then open each
    1312                 :             :          * partition and process each individual partition.
    1313                 :             :          */
    1314         [ +  + ]:        4870 :         if (cstate->rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
    1315                 :             :         {
    1316   [ +  -  +  +  :          84 :             foreach_oid(child, cstate->partitions)
                   +  + ]
    1317                 :             :             {
    1318                 :             :                 Relation    scan_rel;
    1319                 :             : 
    1320                 :             :                 /* We already got the lock in BeginCopyTo */
    1321                 :          38 :                 scan_rel = table_open(child, NoLock);
    1322                 :          38 :                 CopyRelationTo(cstate, scan_rel, cstate->rel, &processed);
    1323                 :          38 :                 table_close(scan_rel, NoLock);
    1324                 :             :             }
    1325                 :             :         }
    1326                 :             :         else
    1327                 :        4847 :             CopyRelationTo(cstate, cstate->rel, NULL, &processed);
    1328                 :             :     }
    1329                 :             :     else
    1330                 :             :     {
    1331                 :             :         /* run the plan --- the dest receiver will send tuples */
    1332                 :         318 :         ExecutorRun(cstate->queryDesc, ForwardScanDirection, 0);
    1333                 :         318 :         processed = ((DR_copy *) cstate->queryDesc->dest)->processed;
    1334                 :             :     }
    1335                 :             : 
    1336                 :        5187 :     cstate->routine->CopyToEnd(cstate);
    1337                 :             : 
    1338                 :        5187 :     MemoryContextDelete(cstate->rowcontext);
    1339                 :             : 
    1340         [ +  + ]:        5187 :     if (fe_copy)
    1341                 :        5129 :         SendCopyEnd(cstate);
    1342                 :             : 
    1343                 :        5187 :     return processed;
    1344                 :             : }
    1345                 :             : 
    1346                 :             : /*
    1347                 :             :  * Scans a single table and exports its rows to the COPY destination.
    1348                 :             :  *
    1349                 :             :  * root_rel can be set to the root table of rel if rel is a partition
    1350                 :             :  * table so that we can send tuples in root_rel's rowtype, which might
    1351                 :             :  * differ from individual partitions.
    1352                 :             :  */
    1353                 :             : static void
    1354                 :        4885 : CopyRelationTo(CopyToState cstate, Relation rel, Relation root_rel, uint64 *processed)
    1355                 :             : {
    1356                 :             :     TupleTableSlot *slot;
    1357                 :             :     TableScanDesc scandesc;
    1358                 :        4885 :     AttrMap    *map = NULL;
    1359                 :        4885 :     TupleTableSlot *root_slot = NULL;
    1360                 :             : 
    1361                 :        4885 :     scandesc = table_beginscan(rel, GetActiveSnapshot(), 0, NULL,
    1362                 :             :                                SO_NONE);
    1363                 :        4884 :     slot = table_slot_create(rel, NULL);
    1364                 :             : 
    1365                 :             :     /*
    1366                 :             :      * If we are exporting partition data here, we check if converting tuples
    1367                 :             :      * to the root table's rowtype, because a partition might have column
    1368                 :             :      * order different than its root table.
    1369                 :             :      */
    1370         [ +  + ]:        4884 :     if (root_rel != NULL)
    1371                 :             :     {
    1372                 :          38 :         root_slot = table_slot_create(root_rel, NULL);
    1373                 :          38 :         map = build_attrmap_by_name_if_req(RelationGetDescr(rel),
    1374                 :             :                                            RelationGetDescr(root_rel),
    1375                 :             :                                            false);
    1376                 :             :     }
    1377                 :             : 
    1378         [ +  + ]:     1841445 :     while (table_scan_getnextslot(scandesc, ForwardScanDirection, slot))
    1379                 :             :     {
    1380                 :             :         TupleTableSlot *copyslot;
    1381                 :             : 
    1382         [ -  + ]:     1836561 :         CHECK_FOR_INTERRUPTS();
    1383                 :             : 
    1384         [ +  + ]:     1836561 :         if (map != NULL)
    1385                 :          26 :             copyslot = execute_attr_map_slot(map, slot, root_slot);
    1386                 :             :         else
    1387                 :             :         {
    1388                 :             :             /* Deconstruct the tuple */
    1389                 :     1836535 :             slot_getallattrs(slot);
    1390                 :     1836535 :             copyslot = slot;
    1391                 :             :         }
    1392                 :             : 
    1393                 :             :         /* Format and send the data */
    1394                 :     1836561 :         CopyOneRowTo(cstate, copyslot);
    1395                 :             : 
    1396                 :             :         /*
    1397                 :             :          * Increment the number of processed tuples, and report the progress.
    1398                 :             :          */
    1399                 :     1836561 :         pgstat_progress_update_param(PROGRESS_COPY_TUPLES_PROCESSED,
    1400                 :     1836561 :                                      ++(*processed));
    1401                 :             :     }
    1402                 :             : 
    1403                 :        4884 :     ExecDropSingleTupleTableSlot(slot);
    1404                 :             : 
    1405         [ +  + ]:        4884 :     if (root_slot != NULL)
    1406                 :          38 :         ExecDropSingleTupleTableSlot(root_slot);
    1407                 :             : 
    1408         [ +  + ]:        4884 :     if (map != NULL)
    1409                 :          11 :         free_attrmap(map);
    1410                 :             : 
    1411                 :        4884 :     table_endscan(scandesc);
    1412                 :        4884 : }
    1413                 :             : 
    1414                 :             : /*
    1415                 :             :  * Emit one row during DoCopyTo().
    1416                 :             :  */
    1417                 :             : static inline void
    1418                 :     1841343 : CopyOneRowTo(CopyToState cstate, TupleTableSlot *slot)
    1419                 :             : {
    1420                 :             :     MemoryContext oldcontext;
    1421                 :             : 
    1422                 :     1841343 :     MemoryContextReset(cstate->rowcontext);
    1423                 :     1841343 :     oldcontext = MemoryContextSwitchTo(cstate->rowcontext);
    1424                 :             : 
    1425                 :             :     /* Make sure the tuple is fully deconstructed */
    1426                 :     1841343 :     slot_getallattrs(slot);
    1427                 :             : 
    1428                 :     1841343 :     cstate->routine->CopyToOneRow(cstate, slot);
    1429                 :             : 
    1430                 :     1841343 :     MemoryContextSwitchTo(oldcontext);
    1431                 :     1841343 : }
    1432                 :             : 
    1433                 :             : /*
    1434                 :             :  * Send text representation of one attribute, with conversion and escaping
    1435                 :             :  */
    1436                 :             : #define DUMPSOFAR() \
    1437                 :             :     do { \
    1438                 :             :         if (ptr > start) \
    1439                 :             :             CopySendData(cstate, start, ptr - start); \
    1440                 :             :     } while (0)
    1441                 :             : 
    1442                 :             : static void
    1443                 :     6625329 : CopyAttributeOutText(CopyToState cstate, const char *string)
    1444                 :             : {
    1445                 :             :     const char *ptr;
    1446                 :             :     const char *start;
    1447                 :             :     char        c;
    1448                 :     6625329 :     char        delimc = cstate->opts.delim[0];
    1449                 :             : 
    1450         [ +  + ]:     6625329 :     if (cstate->need_transcoding)
    1451                 :           4 :         ptr = pg_server_to_any(string, strlen(string), cstate->file_encoding);
    1452                 :             :     else
    1453                 :     6625325 :         ptr = string;
    1454                 :             : 
    1455                 :             :     /*
    1456                 :             :      * We have to grovel through the string searching for control characters
    1457                 :             :      * and instances of the delimiter character.  In most cases, though, these
    1458                 :             :      * are infrequent.  To avoid overhead from calling CopySendData once per
    1459                 :             :      * character, we dump out all characters between escaped characters in a
    1460                 :             :      * single call.  The loop invariant is that the data from "start" to "ptr"
    1461                 :             :      * can be sent literally, but hasn't yet been.
    1462                 :             :      *
    1463                 :             :      * We can skip pg_encoding_mblen() overhead when encoding is safe, because
    1464                 :             :      * in valid backend encodings, extra bytes of a multibyte character never
    1465                 :             :      * look like ASCII.  This loop is sufficiently performance-critical that
    1466                 :             :      * it's worth making two copies of it to get the IS_HIGHBIT_SET() test out
    1467                 :             :      * of the normal safe-encoding path.
    1468                 :             :      */
    1469         [ +  + ]:     6625329 :     if (cstate->encoding_embeds_ascii)
    1470                 :             :     {
    1471                 :           4 :         start = ptr;
    1472         [ +  + ]:          12 :         while ((c = *ptr) != '\0')
    1473                 :             :         {
    1474         [ -  + ]:           8 :             if ((unsigned char) c < (unsigned char) 0x20)
    1475                 :             :             {
    1476                 :             :                 /*
    1477                 :             :                  * \r and \n must be escaped, the others are traditional. We
    1478                 :             :                  * prefer to dump these using the C-like notation, rather than
    1479                 :             :                  * a backslash and the literal character, because it makes the
    1480                 :             :                  * dump file a bit more proof against Microsoftish data
    1481                 :             :                  * mangling.
    1482                 :             :                  */
    1483   [ #  #  #  #  :           0 :                 switch (c)
                #  #  # ]
    1484                 :             :                 {
    1485                 :           0 :                     case '\b':
    1486                 :           0 :                         c = 'b';
    1487                 :           0 :                         break;
    1488                 :           0 :                     case '\f':
    1489                 :           0 :                         c = 'f';
    1490                 :           0 :                         break;
    1491                 :           0 :                     case '\n':
    1492                 :           0 :                         c = 'n';
    1493                 :           0 :                         break;
    1494                 :           0 :                     case '\r':
    1495                 :           0 :                         c = 'r';
    1496                 :           0 :                         break;
    1497                 :           0 :                     case '\t':
    1498                 :           0 :                         c = 't';
    1499                 :           0 :                         break;
    1500                 :           0 :                     case '\v':
    1501                 :           0 :                         c = 'v';
    1502                 :           0 :                         break;
    1503                 :           0 :                     default:
    1504                 :             :                         /* If it's the delimiter, must backslash it */
    1505         [ #  # ]:           0 :                         if (c == delimc)
    1506                 :           0 :                             break;
    1507                 :             :                         /* All ASCII control chars are length 1 */
    1508                 :           0 :                         ptr++;
    1509                 :           0 :                         continue;   /* fall to end of loop */
    1510                 :             :                 }
    1511                 :             :                 /* if we get here, we need to convert the control char */
    1512         [ #  # ]:           0 :                 DUMPSOFAR();
    1513                 :           0 :                 CopySendChar(cstate, '\\');
    1514                 :           0 :                 CopySendChar(cstate, c);
    1515                 :           0 :                 start = ++ptr;  /* do not include char in next run */
    1516                 :             :             }
    1517   [ +  -  -  + ]:           8 :             else if (c == '\\' || c == delimc)
    1518                 :             :             {
    1519         [ #  # ]:           0 :                 DUMPSOFAR();
    1520                 :           0 :                 CopySendChar(cstate, '\\');
    1521                 :           0 :                 start = ptr++;  /* we include char in next run */
    1522                 :             :             }
    1523         [ +  + ]:           8 :             else if (IS_HIGHBIT_SET(c))
    1524                 :           4 :                 ptr += pg_encoding_mblen(cstate->file_encoding, ptr);
    1525                 :             :             else
    1526                 :           4 :                 ptr++;
    1527                 :             :         }
    1528                 :             :     }
    1529                 :             :     else
    1530                 :             :     {
    1531                 :     6625325 :         start = ptr;
    1532         [ +  + ]:    72373486 :         while ((c = *ptr) != '\0')
    1533                 :             :         {
    1534         [ +  + ]:    65748161 :             if ((unsigned char) c < (unsigned char) 0x20)
    1535                 :             :             {
    1536                 :             :                 /*
    1537                 :             :                  * \r and \n must be escaped, the others are traditional. We
    1538                 :             :                  * prefer to dump these using the C-like notation, rather than
    1539                 :             :                  * a backslash and the literal character, because it makes the
    1540                 :             :                  * dump file a bit more proof against Microsoftish data
    1541                 :             :                  * mangling.
    1542                 :             :                  */
    1543   [ -  -  +  -  :        7117 :                 switch (c)
                +  -  - ]
    1544                 :             :                 {
    1545                 :           0 :                     case '\b':
    1546                 :           0 :                         c = 'b';
    1547                 :           0 :                         break;
    1548                 :           0 :                     case '\f':
    1549                 :           0 :                         c = 'f';
    1550                 :           0 :                         break;
    1551                 :        6043 :                     case '\n':
    1552                 :        6043 :                         c = 'n';
    1553                 :        6043 :                         break;
    1554                 :           0 :                     case '\r':
    1555                 :           0 :                         c = 'r';
    1556                 :           0 :                         break;
    1557                 :        1074 :                     case '\t':
    1558                 :        1074 :                         c = 't';
    1559                 :        1074 :                         break;
    1560                 :           0 :                     case '\v':
    1561                 :           0 :                         c = 'v';
    1562                 :           0 :                         break;
    1563                 :           0 :                     default:
    1564                 :             :                         /* If it's the delimiter, must backslash it */
    1565         [ #  # ]:           0 :                         if (c == delimc)
    1566                 :           0 :                             break;
    1567                 :             :                         /* All ASCII control chars are length 1 */
    1568                 :           0 :                         ptr++;
    1569                 :           0 :                         continue;   /* fall to end of loop */
    1570                 :             :                 }
    1571                 :             :                 /* if we get here, we need to convert the control char */
    1572         [ +  + ]:        7117 :                 DUMPSOFAR();
    1573                 :        7117 :                 CopySendChar(cstate, '\\');
    1574                 :        7117 :                 CopySendChar(cstate, c);
    1575                 :        7117 :                 start = ++ptr;  /* do not include char in next run */
    1576                 :             :             }
    1577   [ +  +  -  + ]:    65741044 :             else if (c == '\\' || c == delimc)
    1578                 :             :             {
    1579         [ +  + ]:        2517 :                 DUMPSOFAR();
    1580                 :        2517 :                 CopySendChar(cstate, '\\');
    1581                 :        2517 :                 start = ptr++;  /* we include char in next run */
    1582                 :             :             }
    1583                 :             :             else
    1584                 :    65738527 :                 ptr++;
    1585                 :             :         }
    1586                 :             :     }
    1587                 :             : 
    1588         [ +  + ]:     6625329 :     DUMPSOFAR();
    1589                 :     6625329 : }
    1590                 :             : 
    1591                 :             : /*
    1592                 :             :  * Send text representation of one attribute, with conversion and
    1593                 :             :  * CSV-style escaping
    1594                 :             :  */
    1595                 :             : static void
    1596                 :         428 : CopyAttributeOutCSV(CopyToState cstate, const char *string,
    1597                 :             :                     bool use_quote)
    1598                 :             : {
    1599                 :             :     const char *ptr;
    1600                 :             :     const char *start;
    1601                 :             :     char        c;
    1602                 :         428 :     char        delimc = cstate->opts.delim[0];
    1603                 :         428 :     char        quotec = cstate->opts.quote[0];
    1604                 :         428 :     char        escapec = cstate->opts.escape[0];
    1605                 :         428 :     bool        single_attr = (list_length(cstate->attnumlist) == 1);
    1606                 :             : 
    1607                 :             :     /* force quoting if it matches null_print (before conversion!) */
    1608   [ +  +  +  + ]:         428 :     if (!use_quote && strcmp(string, cstate->opts.null_print) == 0)
    1609                 :          36 :         use_quote = true;
    1610                 :             : 
    1611         [ +  + ]:         428 :     if (cstate->need_transcoding)
    1612                 :           4 :         ptr = pg_server_to_any(string, strlen(string), cstate->file_encoding);
    1613                 :             :     else
    1614                 :         424 :         ptr = string;
    1615                 :             : 
    1616                 :             :     /*
    1617                 :             :      * Make a preliminary pass to discover if it needs quoting
    1618                 :             :      */
    1619         [ +  + ]:         428 :     if (!use_quote)
    1620                 :             :     {
    1621                 :             :         /*
    1622                 :             :          * Quote '\.' if it appears alone on a line, so that it will not be
    1623                 :             :          * interpreted as an end-of-data marker.  (PG 18 and up will not
    1624                 :             :          * interpret '\.' in CSV that way, except in embedded-in-SQL data; but
    1625                 :             :          * we want the data to be loadable by older versions too.  Also, this
    1626                 :             :          * avoids breaking clients that are still using PQgetline().)
    1627                 :             :          */
    1628   [ +  +  +  + ]:         304 :         if (single_attr && strcmp(ptr, "\\.") == 0)
    1629                 :           4 :             use_quote = true;
    1630                 :             :         else
    1631                 :             :         {
    1632                 :         300 :             const char *tptr = ptr;
    1633                 :             : 
    1634         [ +  + ]:        1504 :             while ((c = *tptr) != '\0')
    1635                 :             :             {
    1636   [ +  +  +  +  :        1296 :                 if (c == delimc || c == quotec || c == '\n' || c == '\r')
             +  +  +  + ]
    1637                 :             :                 {
    1638                 :          92 :                     use_quote = true;
    1639                 :          92 :                     break;
    1640                 :             :                 }
    1641   [ +  +  +  + ]:        1204 :                 if (IS_HIGHBIT_SET(c) && cstate->encoding_embeds_ascii)
    1642                 :           4 :                     tptr += pg_encoding_mblen(cstate->file_encoding, tptr);
    1643                 :             :                 else
    1644                 :        1200 :                     tptr++;
    1645                 :             :             }
    1646                 :             :         }
    1647                 :             :     }
    1648                 :             : 
    1649         [ +  + ]:         428 :     if (use_quote)
    1650                 :             :     {
    1651                 :         220 :         CopySendChar(cstate, quotec);
    1652                 :             : 
    1653                 :             :         /*
    1654                 :             :          * We adopt the same optimization strategy as in CopyAttributeOutText
    1655                 :             :          */
    1656                 :         220 :         start = ptr;
    1657         [ +  + ]:        1704 :         while ((c = *ptr) != '\0')
    1658                 :             :         {
    1659   [ +  +  +  + ]:        1484 :             if (c == quotec || c == escapec)
    1660                 :             :             {
    1661         [ +  + ]:         104 :                 DUMPSOFAR();
    1662                 :         104 :                 CopySendChar(cstate, escapec);
    1663                 :         104 :                 start = ptr;    /* we include char in next run */
    1664                 :             :             }
    1665   [ +  +  +  - ]:        1484 :             if (IS_HIGHBIT_SET(c) && cstate->encoding_embeds_ascii)
    1666                 :           4 :                 ptr += pg_encoding_mblen(cstate->file_encoding, ptr);
    1667                 :             :             else
    1668                 :        1480 :                 ptr++;
    1669                 :             :         }
    1670         [ +  + ]:         220 :         DUMPSOFAR();
    1671                 :             : 
    1672                 :         220 :         CopySendChar(cstate, quotec);
    1673                 :             :     }
    1674                 :             :     else
    1675                 :             :     {
    1676                 :             :         /* If it doesn't need quoting, we can just dump it as-is */
    1677                 :         208 :         CopySendString(cstate, ptr);
    1678                 :             :     }
    1679                 :         428 : }
    1680                 :             : 
    1681                 :             : /*
    1682                 :             :  * copy_dest_startup --- executor startup
    1683                 :             :  */
    1684                 :             : static void
    1685                 :         318 : copy_dest_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
    1686                 :             : {
    1687                 :             :     /* no-op */
    1688                 :         318 : }
    1689                 :             : 
    1690                 :             : /*
    1691                 :             :  * copy_dest_receive --- receive one tuple
    1692                 :             :  */
    1693                 :             : static bool
    1694                 :        4782 : copy_dest_receive(TupleTableSlot *slot, DestReceiver *self)
    1695                 :             : {
    1696                 :        4782 :     DR_copy    *myState = (DR_copy *) self;
    1697                 :        4782 :     CopyToState cstate = myState->cstate;
    1698                 :             : 
    1699                 :             :     /* Send the data */
    1700                 :        4782 :     CopyOneRowTo(cstate, slot);
    1701                 :             : 
    1702                 :             :     /* Increment the number of processed tuples, and report the progress */
    1703                 :        4782 :     pgstat_progress_update_param(PROGRESS_COPY_TUPLES_PROCESSED,
    1704                 :        4782 :                                  ++myState->processed);
    1705                 :             : 
    1706                 :        4782 :     return true;
    1707                 :             : }
    1708                 :             : 
    1709                 :             : /*
    1710                 :             :  * copy_dest_shutdown --- executor end
    1711                 :             :  */
    1712                 :             : static void
    1713                 :         318 : copy_dest_shutdown(DestReceiver *self)
    1714                 :             : {
    1715                 :             :     /* no-op */
    1716                 :         318 : }
    1717                 :             : 
    1718                 :             : /*
    1719                 :             :  * copy_dest_destroy --- release DestReceiver object
    1720                 :             :  */
    1721                 :             : static void
    1722                 :           0 : copy_dest_destroy(DestReceiver *self)
    1723                 :             : {
    1724                 :           0 :     pfree(self);
    1725                 :           0 : }
    1726                 :             : 
    1727                 :             : /*
    1728                 :             :  * CreateCopyDestReceiver -- create a suitable DestReceiver object
    1729                 :             :  */
    1730                 :             : DestReceiver *
    1731                 :         322 : CreateCopyDestReceiver(void)
    1732                 :             : {
    1733                 :         322 :     DR_copy    *self = palloc_object(DR_copy);
    1734                 :             : 
    1735                 :         322 :     self->pub.receiveSlot = copy_dest_receive;
    1736                 :         322 :     self->pub.rStartup = copy_dest_startup;
    1737                 :         322 :     self->pub.rShutdown = copy_dest_shutdown;
    1738                 :         322 :     self->pub.rDestroy = copy_dest_destroy;
    1739                 :         322 :     self->pub.mydest = DestCopyOut;
    1740                 :             : 
    1741                 :         322 :     self->cstate = NULL;     /* will be set later */
    1742                 :         322 :     self->processed = 0;
    1743                 :             : 
    1744                 :         322 :     return (DestReceiver *) self;
    1745                 :             : }
        

Generated by: LCOV version 2.0-1