PostgreSQL Source Code  git master
copy.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * copy.c
4  * Implements the COPY utility command
5  *
6  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/commands/copy.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/heapam.h"
22 #include "access/htup_details.h"
23 #include "access/sysattr.h"
24 #include "access/tableam.h"
25 #include "access/xact.h"
26 #include "access/xlog.h"
27 #include "catalog/dependency.h"
28 #include "catalog/pg_authid.h"
29 #include "catalog/pg_type.h"
30 #include "commands/copy.h"
31 #include "commands/defrem.h"
32 #include "commands/trigger.h"
33 #include "executor/execPartition.h"
34 #include "executor/executor.h"
36 #include "executor/tuptable.h"
37 #include "foreign/fdwapi.h"
38 #include "libpq/libpq.h"
39 #include "libpq/pqformat.h"
40 #include "mb/pg_wchar.h"
41 #include "miscadmin.h"
42 #include "nodes/makefuncs.h"
43 #include "optimizer/optimizer.h"
44 #include "parser/parse_coerce.h"
45 #include "parser/parse_collate.h"
46 #include "parser/parse_expr.h"
47 #include "parser/parse_relation.h"
48 #include "port/pg_bswap.h"
49 #include "rewrite/rewriteHandler.h"
50 #include "storage/fd.h"
51 #include "tcop/tcopprot.h"
52 #include "utils/builtins.h"
53 #include "utils/lsyscache.h"
54 #include "utils/memutils.h"
55 #include "utils/partcache.h"
56 #include "utils/portal.h"
57 #include "utils/rel.h"
58 #include "utils/rls.h"
59 #include "utils/snapmgr.h"
60 
61 #define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
62 #define OCTVALUE(c) ((c) - '0')
63 
64 /*
65  * Represents the different source/dest cases we need to worry about at
66  * the bottom level
67  */
68 typedef enum CopyDest
69 {
70  COPY_FILE, /* to/from file (or a piped program) */
71  COPY_OLD_FE, /* to/from frontend (2.0 protocol) */
72  COPY_NEW_FE, /* to/from frontend (3.0 protocol) */
73  COPY_CALLBACK /* to/from callback function */
74 } CopyDest;
75 
76 /*
77  * Represents the end-of-line terminator type of the input
78  */
79 typedef enum EolType
80 {
85 } EolType;
86 
87 /*
88  * Represents the heap insert method to be used during COPY FROM.
89  */
90 typedef enum CopyInsertMethod
91 {
92  CIM_SINGLE, /* use table_tuple_insert or fdw routine */
93  CIM_MULTI, /* always use table_multi_insert */
94  CIM_MULTI_CONDITIONAL /* use table_multi_insert only if valid */
96 
97 /*
98  * This struct contains all the state variables used throughout a COPY
99  * operation. For simplicity, we use the same struct for all variants of COPY,
100  * even though some fields are used in only some cases.
101  *
102  * Multi-byte encodings: all supported client-side encodings encode multi-byte
103  * characters by having the first byte's high bit set. Subsequent bytes of the
104  * character can have the high bit not set. When scanning data in such an
105  * encoding to look for a match to a single-byte (ie ASCII) character, we must
106  * use the full pg_encoding_mblen() machinery to skip over multibyte
107  * characters, else we might find a false match to a trailing byte. In
108  * supported server encodings, there is no possibility of a false match, and
109  * it's faster to make useless comparisons to trailing bytes than it is to
110  * invoke pg_encoding_mblen() to skip over them. encoding_embeds_ascii is true
111  * when we have to do it the hard way.
112  */
113 typedef struct CopyStateData
114 {
115  /* low-level state data */
116  CopyDest copy_dest; /* type of copy source/destination */
117  FILE *copy_file; /* used if copy_dest == COPY_FILE */
118  StringInfo fe_msgbuf; /* used for all dests during COPY TO, only for
119  * dest == COPY_NEW_FE in COPY FROM */
120  bool is_copy_from; /* COPY TO, or COPY FROM? */
121  bool reached_eof; /* true if we read to end of copy data (not
122  * all copy_dest types maintain this) */
123  EolType eol_type; /* EOL type of input */
124  int file_encoding; /* file or remote side's character encoding */
125  bool need_transcoding; /* file encoding diff from server? */
126  bool encoding_embeds_ascii; /* ASCII can be non-first byte? */
127 
128  /* parameters from the COPY command */
129  Relation rel; /* relation to copy to or from */
130  QueryDesc *queryDesc; /* executable query to copy from */
131  List *attnumlist; /* integer list of attnums to copy */
132  char *filename; /* filename, or NULL for STDIN/STDOUT */
133  bool is_program; /* is 'filename' a program to popen? */
134  copy_data_source_cb data_source_cb; /* function for reading data */
135  bool binary; /* binary format? */
136  bool freeze; /* freeze rows on loading? */
137  bool csv_mode; /* Comma Separated Value format? */
138  bool header_line; /* CSV header line? */
139  char *null_print; /* NULL marker string (server encoding!) */
140  int null_print_len; /* length of same */
141  char *null_print_client; /* same converted to file encoding */
142  char *delim; /* column delimiter (must be 1 byte) */
143  char *quote; /* CSV quote char (must be 1 byte) */
144  char *escape; /* CSV escape char (must be 1 byte) */
145  List *force_quote; /* list of column names */
146  bool force_quote_all; /* FORCE_QUOTE *? */
147  bool *force_quote_flags; /* per-column CSV FQ flags */
148  List *force_notnull; /* list of column names */
149  bool *force_notnull_flags; /* per-column CSV FNN flags */
150  List *force_null; /* list of column names */
151  bool *force_null_flags; /* per-column CSV FN flags */
152  bool convert_selectively; /* do selective binary conversion? */
153  List *convert_select; /* list of column names (can be NIL) */
154  bool *convert_select_flags; /* per-column CSV/TEXT CS flags */
155  Node *whereClause; /* WHERE condition (or NULL) */
156 
157  /* these are just for error messages, see CopyFromErrorCallback */
158  const char *cur_relname; /* table name for error messages */
159  uint64 cur_lineno; /* line number for error messages */
160  const char *cur_attname; /* current att for error messages */
161  const char *cur_attval; /* current att value for error messages */
162 
163  /*
164  * Working state for COPY TO/FROM
165  */
166  MemoryContext copycontext; /* per-copy execution context */
167 
168  /*
169  * Working state for COPY TO
170  */
171  FmgrInfo *out_functions; /* lookup info for output functions */
172  MemoryContext rowcontext; /* per-row evaluation context */
173 
174  /*
175  * Working state for COPY FROM
176  */
178  FmgrInfo *in_functions; /* array of input functions for each attrs */
179  Oid *typioparams; /* array of element types for in_functions */
180  int *defmap; /* array of default att numbers */
181  ExprState **defexprs; /* array of default att expressions */
182  bool volatile_defexprs; /* is any of defexprs volatile? */
185 
187 
188  /*
189  * These variables are used to reduce overhead in textual COPY FROM.
190  *
191  * attribute_buf holds the separated, de-escaped text for each field of
192  * the current line. The CopyReadAttributes functions return arrays of
193  * pointers into this buffer. We avoid palloc/pfree overhead by re-using
194  * the buffer on each cycle.
195  */
197 
198  /* field raw data pointers found by COPY FROM */
199 
201  char **raw_fields;
202 
203  /*
204  * Similarly, line_buf holds the whole input line being processed. The
205  * input cycle is first to read the whole line into line_buf, convert it
206  * to server encoding there, and then extract the individual attribute
207  * fields into attribute_buf. line_buf is preserved unmodified so that we
208  * can display it in error messages if appropriate.
209  */
211  bool line_buf_converted; /* converted to server encoding? */
212  bool line_buf_valid; /* contains the row being processed? */
213 
214  /*
215  * Finally, raw_buf holds raw data read from the data source (file or
216  * client connection). CopyReadLine parses this data sufficiently to
217  * locate line boundaries, then transfers the data to line_buf and
218  * converts it. Note: we guarantee that there is a \0 at
219  * raw_buf[raw_buf_len].
220  */
221 #define RAW_BUF_SIZE 65536 /* we palloc RAW_BUF_SIZE+1 bytes */
222  char *raw_buf;
223  int raw_buf_index; /* next byte to process */
224  int raw_buf_len; /* total # of bytes stored */
225 } CopyStateData;
226 
227 /* DestReceiver for COPY (query) TO */
228 typedef struct
229 {
230  DestReceiver pub; /* publicly-known function pointers */
231  CopyState cstate; /* CopyStateData for the command */
232  uint64 processed; /* # of tuples processed */
233 } DR_copy;
234 
235 
236 /*
237  * No more than this many tuples per CopyMultiInsertBuffer
238  *
239  * Caution: Don't make this too big, as we could end up with this many
240  * CopyMultiInsertBuffer items stored in CopyMultiInsertInfo's
241  * multiInsertBuffers list. Increasing this can cause quadratic growth in
242  * memory requirements during copies into partitioned tables with a large
243  * number of partitions.
244  */
245 #define MAX_BUFFERED_TUPLES 1000
246 
247 /*
248  * Flush buffers if there are >= this many bytes, as counted by the input
249  * size, of tuples stored.
250  */
251 #define MAX_BUFFERED_BYTES 65535
252 
253 /* Trim the list of buffers back down to this number after flushing */
254 #define MAX_PARTITION_BUFFERS 32
255 
256 /* Stores multi-insert data related to a single relation in CopyFrom. */
257 typedef struct CopyMultiInsertBuffer
258 {
259  TupleTableSlot *slots[MAX_BUFFERED_TUPLES]; /* Array to store tuples */
260  ResultRelInfo *resultRelInfo; /* ResultRelInfo for 'relid' */
261  BulkInsertState bistate; /* BulkInsertState for this rel */
262  int nused; /* number of 'slots' containing tuples */
263  uint64 linenos[MAX_BUFFERED_TUPLES]; /* Line # of tuple in copy
264  * stream */
266 
267 /*
268  * Stores one or many CopyMultiInsertBuffers and details about the size and
269  * number of tuples which are stored in them. This allows multiple buffers to
270  * exist at once when COPYing into a partitioned table.
271  */
272 typedef struct CopyMultiInsertInfo
273 {
274  List *multiInsertBuffers; /* List of tracked CopyMultiInsertBuffers */
275  int bufferedTuples; /* number of tuples buffered over all buffers */
276  int bufferedBytes; /* number of bytes from all buffered tuples */
277  CopyState cstate; /* Copy state for this CopyMultiInsertInfo */
278  EState *estate; /* Executor state used for COPY */
279  CommandId mycid; /* Command Id used for COPY */
280  int ti_options; /* table insert options */
282 
283 
284 /*
285  * These macros centralize code used to process line_buf and raw_buf buffers.
286  * They are macros because they often do continue/break control and to avoid
287  * function call overhead in tight COPY loops.
288  *
289  * We must use "if (1)" because the usual "do {...} while(0)" wrapper would
290  * prevent the continue/break processing from working. We end the "if (1)"
291  * with "else ((void) 0)" to ensure the "if" does not unintentionally match
292  * any "else" in the calling code, and to avoid any compiler warnings about
293  * empty statements. See http://www.cit.gu.edu.au/~anthony/info/C/C.macros.
294  */
295 
296 /*
297  * This keeps the character read at the top of the loop in the buffer
298  * even if there is more than one read-ahead.
299  */
300 #define IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(extralen) \
301 if (1) \
302 { \
303  if (raw_buf_ptr + (extralen) >= copy_buf_len && !hit_eof) \
304  { \
305  raw_buf_ptr = prev_raw_ptr; /* undo fetch */ \
306  need_data = true; \
307  continue; \
308  } \
309 } else ((void) 0)
310 
311 /* This consumes the remainder of the buffer and breaks */
312 #define IF_NEED_REFILL_AND_EOF_BREAK(extralen) \
313 if (1) \
314 { \
315  if (raw_buf_ptr + (extralen) >= copy_buf_len && hit_eof) \
316  { \
317  if (extralen) \
318  raw_buf_ptr = copy_buf_len; /* consume the partial character */ \
319  /* backslash just before EOF, treat as data char */ \
320  result = true; \
321  break; \
322  } \
323 } else ((void) 0)
324 
325 /*
326  * Transfer any approved data to line_buf; must do this to be sure
327  * there is some room in raw_buf.
328  */
329 #define REFILL_LINEBUF \
330 if (1) \
331 { \
332  if (raw_buf_ptr > cstate->raw_buf_index) \
333  { \
334  appendBinaryStringInfo(&cstate->line_buf, \
335  cstate->raw_buf + cstate->raw_buf_index, \
336  raw_buf_ptr - cstate->raw_buf_index); \
337  cstate->raw_buf_index = raw_buf_ptr; \
338  } \
339 } else ((void) 0)
340 
341 /* Undo any read-ahead and jump out of the block. */
342 #define NO_END_OF_COPY_GOTO \
343 if (1) \
344 { \
345  raw_buf_ptr = prev_raw_ptr + 1; \
346  goto not_end_of_copy; \
347 } else ((void) 0)
348 
349 static const char BinarySignature[11] = "PGCOPY\n\377\r\n\0";
350 
351 
352 /* non-export function prototypes */
353 static CopyState BeginCopy(ParseState *pstate, bool is_from, Relation rel,
354  RawStmt *raw_query, Oid queryRelId, List *attnamelist,
355  List *options);
356 static void EndCopy(CopyState cstate);
357 static void ClosePipeToProgram(CopyState cstate);
358 static CopyState BeginCopyTo(ParseState *pstate, Relation rel, RawStmt *query,
359  Oid queryRelId, const char *filename, bool is_program,
360  List *attnamelist, List *options);
361 static void EndCopyTo(CopyState cstate);
362 static uint64 DoCopyTo(CopyState cstate);
363 static uint64 CopyTo(CopyState cstate);
364 static void CopyOneRowTo(CopyState cstate, TupleTableSlot *slot);
365 static bool CopyReadLine(CopyState cstate);
366 static bool CopyReadLineText(CopyState cstate);
367 static int CopyReadAttributesText(CopyState cstate);
368 static int CopyReadAttributesCSV(CopyState cstate);
370  int column_no, FmgrInfo *flinfo,
371  Oid typioparam, int32 typmod,
372  bool *isnull);
373 static void CopyAttributeOutText(CopyState cstate, char *string);
374 static void CopyAttributeOutCSV(CopyState cstate, char *string,
375  bool use_quote, bool single_attr);
376 static List *CopyGetAttnums(TupleDesc tupDesc, Relation rel,
377  List *attnamelist);
378 static char *limit_printout_length(const char *str);
379 
380 /* Low-level communications functions */
381 static void SendCopyBegin(CopyState cstate);
382 static void ReceiveCopyBegin(CopyState cstate);
383 static void SendCopyEnd(CopyState cstate);
384 static void CopySendData(CopyState cstate, const void *databuf, int datasize);
385 static void CopySendString(CopyState cstate, const char *str);
386 static void CopySendChar(CopyState cstate, char c);
387 static void CopySendEndOfRow(CopyState cstate);
388 static int CopyGetData(CopyState cstate, void *databuf,
389  int minread, int maxread);
390 static void CopySendInt32(CopyState cstate, int32 val);
391 static bool CopyGetInt32(CopyState cstate, int32 *val);
392 static void CopySendInt16(CopyState cstate, int16 val);
393 static bool CopyGetInt16(CopyState cstate, int16 *val);
394 
395 
396 /*
397  * Send copy start/stop messages for frontend copies. These have changed
398  * in past protocol redesigns.
399  */
400 static void
402 {
404  {
405  /* new way */
407  int natts = list_length(cstate->attnumlist);
408  int16 format = (cstate->binary ? 1 : 0);
409  int i;
410 
411  pq_beginmessage(&buf, 'H');
412  pq_sendbyte(&buf, format); /* overall format */
413  pq_sendint16(&buf, natts);
414  for (i = 0; i < natts; i++)
415  pq_sendint16(&buf, format); /* per-column formats */
416  pq_endmessage(&buf);
417  cstate->copy_dest = COPY_NEW_FE;
418  }
419  else
420  {
421  /* old way */
422  if (cstate->binary)
423  ereport(ERROR,
424  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
425  errmsg("COPY BINARY is not supported to stdout or from stdin")));
426  pq_putemptymessage('H');
427  /* grottiness needed for old COPY OUT protocol */
428  pq_startcopyout();
429  cstate->copy_dest = COPY_OLD_FE;
430  }
431 }
432 
433 static void
435 {
437  {
438  /* new way */
440  int natts = list_length(cstate->attnumlist);
441  int16 format = (cstate->binary ? 1 : 0);
442  int i;
443 
444  pq_beginmessage(&buf, 'G');
445  pq_sendbyte(&buf, format); /* overall format */
446  pq_sendint16(&buf, natts);
447  for (i = 0; i < natts; i++)
448  pq_sendint16(&buf, format); /* per-column formats */
449  pq_endmessage(&buf);
450  cstate->copy_dest = COPY_NEW_FE;
451  cstate->fe_msgbuf = makeStringInfo();
452  }
453  else
454  {
455  /* old way */
456  if (cstate->binary)
457  ereport(ERROR,
458  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
459  errmsg("COPY BINARY is not supported to stdout or from stdin")));
460  pq_putemptymessage('G');
461  /* any error in old protocol will make us lose sync */
462  pq_startmsgread();
463  cstate->copy_dest = COPY_OLD_FE;
464  }
465  /* We *must* flush here to ensure FE knows it can send. */
466  pq_flush();
467 }
468 
469 static void
471 {
472  if (cstate->copy_dest == COPY_NEW_FE)
473  {
474  /* Shouldn't have any unsent data */
475  Assert(cstate->fe_msgbuf->len == 0);
476  /* Send Copy Done message */
477  pq_putemptymessage('c');
478  }
479  else
480  {
481  CopySendData(cstate, "\\.", 2);
482  /* Need to flush out the trailer (this also appends a newline) */
483  CopySendEndOfRow(cstate);
484  pq_endcopyout(false);
485  }
486 }
487 
488 /*----------
489  * CopySendData sends output data to the destination (file or frontend)
490  * CopySendString does the same for null-terminated strings
491  * CopySendChar does the same for single characters
492  * CopySendEndOfRow does the appropriate thing at end of each data row
493  * (data is not actually flushed except by CopySendEndOfRow)
494  *
495  * NB: no data conversion is applied by these functions
496  *----------
497  */
498 static void
499 CopySendData(CopyState cstate, const void *databuf, int datasize)
500 {
501  appendBinaryStringInfo(cstate->fe_msgbuf, databuf, datasize);
502 }
503 
504 static void
505 CopySendString(CopyState cstate, const char *str)
506 {
507  appendBinaryStringInfo(cstate->fe_msgbuf, str, strlen(str));
508 }
509 
510 static void
511 CopySendChar(CopyState cstate, char c)
512 {
514 }
515 
516 static void
518 {
519  StringInfo fe_msgbuf = cstate->fe_msgbuf;
520 
521  switch (cstate->copy_dest)
522  {
523  case COPY_FILE:
524  if (!cstate->binary)
525  {
526  /* Default line termination depends on platform */
527 #ifndef WIN32
528  CopySendChar(cstate, '\n');
529 #else
530  CopySendString(cstate, "\r\n");
531 #endif
532  }
533 
534  if (fwrite(fe_msgbuf->data, fe_msgbuf->len, 1,
535  cstate->copy_file) != 1 ||
536  ferror(cstate->copy_file))
537  {
538  if (cstate->is_program)
539  {
540  if (errno == EPIPE)
541  {
542  /*
543  * The pipe will be closed automatically on error at
544  * the end of transaction, but we might get a better
545  * error message from the subprocess' exit code than
546  * just "Broken Pipe"
547  */
548  ClosePipeToProgram(cstate);
549 
550  /*
551  * If ClosePipeToProgram() didn't throw an error, the
552  * program terminated normally, but closed the pipe
553  * first. Restore errno, and throw an error.
554  */
555  errno = EPIPE;
556  }
557  ereport(ERROR,
559  errmsg("could not write to COPY program: %m")));
560  }
561  else
562  ereport(ERROR,
564  errmsg("could not write to COPY file: %m")));
565  }
566  break;
567  case COPY_OLD_FE:
568  /* The FE/BE protocol uses \n as newline for all platforms */
569  if (!cstate->binary)
570  CopySendChar(cstate, '\n');
571 
572  if (pq_putbytes(fe_msgbuf->data, fe_msgbuf->len))
573  {
574  /* no hope of recovering connection sync, so FATAL */
575  ereport(FATAL,
576  (errcode(ERRCODE_CONNECTION_FAILURE),
577  errmsg("connection lost during COPY to stdout")));
578  }
579  break;
580  case COPY_NEW_FE:
581  /* The FE/BE protocol uses \n as newline for all platforms */
582  if (!cstate->binary)
583  CopySendChar(cstate, '\n');
584 
585  /* Dump the accumulated row as one CopyData message */
586  (void) pq_putmessage('d', fe_msgbuf->data, fe_msgbuf->len);
587  break;
588  case COPY_CALLBACK:
589  Assert(false); /* Not yet supported. */
590  break;
591  }
592 
593  resetStringInfo(fe_msgbuf);
594 }
595 
596 /*
597  * CopyGetData reads data from the source (file or frontend)
598  *
599  * We attempt to read at least minread, and at most maxread, bytes from
600  * the source. The actual number of bytes read is returned; if this is
601  * less than minread, EOF was detected.
602  *
603  * Note: when copying from the frontend, we expect a proper EOF mark per
604  * protocol; if the frontend simply drops the connection, we raise error.
605  * It seems unwise to allow the COPY IN to complete normally in that case.
606  *
607  * NB: no data conversion is applied here.
608  */
609 static int
610 CopyGetData(CopyState cstate, void *databuf, int minread, int maxread)
611 {
612  int bytesread = 0;
613 
614  switch (cstate->copy_dest)
615  {
616  case COPY_FILE:
617  bytesread = fread(databuf, 1, maxread, cstate->copy_file);
618  if (ferror(cstate->copy_file))
619  ereport(ERROR,
621  errmsg("could not read from COPY file: %m")));
622  if (bytesread == 0)
623  cstate->reached_eof = true;
624  break;
625  case COPY_OLD_FE:
626 
627  /*
628  * We cannot read more than minread bytes (which in practice is 1)
629  * because old protocol doesn't have any clear way of separating
630  * the COPY stream from following data. This is slow, but not any
631  * slower than the code path was originally, and we don't care
632  * much anymore about the performance of old protocol.
633  */
634  if (pq_getbytes((char *) databuf, minread))
635  {
636  /* Only a \. terminator is legal EOF in old protocol */
637  ereport(ERROR,
638  (errcode(ERRCODE_CONNECTION_FAILURE),
639  errmsg("unexpected EOF on client connection with an open transaction")));
640  }
641  bytesread = minread;
642  break;
643  case COPY_NEW_FE:
644  while (maxread > 0 && bytesread < minread && !cstate->reached_eof)
645  {
646  int avail;
647 
648  while (cstate->fe_msgbuf->cursor >= cstate->fe_msgbuf->len)
649  {
650  /* Try to receive another message */
651  int mtype;
652 
653  readmessage:
655  pq_startmsgread();
656  mtype = pq_getbyte();
657  if (mtype == EOF)
658  ereport(ERROR,
659  (errcode(ERRCODE_CONNECTION_FAILURE),
660  errmsg("unexpected EOF on client connection with an open transaction")));
661  if (pq_getmessage(cstate->fe_msgbuf, 0))
662  ereport(ERROR,
663  (errcode(ERRCODE_CONNECTION_FAILURE),
664  errmsg("unexpected EOF on client connection with an open transaction")));
666  switch (mtype)
667  {
668  case 'd': /* CopyData */
669  break;
670  case 'c': /* CopyDone */
671  /* COPY IN correctly terminated by frontend */
672  cstate->reached_eof = true;
673  return bytesread;
674  case 'f': /* CopyFail */
675  ereport(ERROR,
676  (errcode(ERRCODE_QUERY_CANCELED),
677  errmsg("COPY from stdin failed: %s",
678  pq_getmsgstring(cstate->fe_msgbuf))));
679  break;
680  case 'H': /* Flush */
681  case 'S': /* Sync */
682 
683  /*
684  * Ignore Flush/Sync for the convenience of client
685  * libraries (such as libpq) that may send those
686  * without noticing that the command they just
687  * sent was COPY.
688  */
689  goto readmessage;
690  default:
691  ereport(ERROR,
692  (errcode(ERRCODE_PROTOCOL_VIOLATION),
693  errmsg("unexpected message type 0x%02X during COPY from stdin",
694  mtype)));
695  break;
696  }
697  }
698  avail = cstate->fe_msgbuf->len - cstate->fe_msgbuf->cursor;
699  if (avail > maxread)
700  avail = maxread;
701  pq_copymsgbytes(cstate->fe_msgbuf, databuf, avail);
702  databuf = (void *) ((char *) databuf + avail);
703  maxread -= avail;
704  bytesread += avail;
705  }
706  break;
707  case COPY_CALLBACK:
708  bytesread = cstate->data_source_cb(databuf, minread, maxread);
709  break;
710  }
711 
712  return bytesread;
713 }
714 
715 
716 /*
717  * These functions do apply some data conversion
718  */
719 
720 /*
721  * CopySendInt32 sends an int32 in network byte order
722  */
723 static void
725 {
726  uint32 buf;
727 
728  buf = pg_hton32((uint32) val);
729  CopySendData(cstate, &buf, sizeof(buf));
730 }
731 
732 /*
733  * CopyGetInt32 reads an int32 that appears in network byte order
734  *
735  * Returns true if OK, false if EOF
736  */
737 static bool
739 {
740  uint32 buf;
741 
742  if (CopyGetData(cstate, &buf, sizeof(buf), sizeof(buf)) != sizeof(buf))
743  {
744  *val = 0; /* suppress compiler warning */
745  return false;
746  }
747  *val = (int32) pg_ntoh32(buf);
748  return true;
749 }
750 
751 /*
752  * CopySendInt16 sends an int16 in network byte order
753  */
754 static void
756 {
757  uint16 buf;
758 
759  buf = pg_hton16((uint16) val);
760  CopySendData(cstate, &buf, sizeof(buf));
761 }
762 
763 /*
764  * CopyGetInt16 reads an int16 that appears in network byte order
765  */
766 static bool
768 {
769  uint16 buf;
770 
771  if (CopyGetData(cstate, &buf, sizeof(buf), sizeof(buf)) != sizeof(buf))
772  {
773  *val = 0; /* suppress compiler warning */
774  return false;
775  }
776  *val = (int16) pg_ntoh16(buf);
777  return true;
778 }
779 
780 
781 /*
782  * CopyLoadRawBuf loads some more data into raw_buf
783  *
784  * Returns true if able to obtain at least one more byte, else false.
785  *
786  * If raw_buf_index < raw_buf_len, the unprocessed bytes are transferred
787  * down to the start of the buffer and then we load more data after that.
788  * This case is used only when a frontend multibyte character crosses a
789  * bufferload boundary.
790  */
791 static bool
793 {
794  int nbytes;
795  int inbytes;
796 
797  if (cstate->raw_buf_index < cstate->raw_buf_len)
798  {
799  /* Copy down the unprocessed data */
800  nbytes = cstate->raw_buf_len - cstate->raw_buf_index;
801  memmove(cstate->raw_buf, cstate->raw_buf + cstate->raw_buf_index,
802  nbytes);
803  }
804  else
805  nbytes = 0; /* no data need be saved */
806 
807  inbytes = CopyGetData(cstate, cstate->raw_buf + nbytes,
808  1, RAW_BUF_SIZE - nbytes);
809  nbytes += inbytes;
810  cstate->raw_buf[nbytes] = '\0';
811  cstate->raw_buf_index = 0;
812  cstate->raw_buf_len = nbytes;
813  return (inbytes > 0);
814 }
815 
816 
817 /*
818  * DoCopy executes the SQL COPY statement
819  *
820  * Either unload or reload contents of table <relation>, depending on <from>.
821  * (<from> = true means we are inserting into the table.) In the "TO" case
822  * we also support copying the output of an arbitrary SELECT, INSERT, UPDATE
823  * or DELETE query.
824  *
825  * If <pipe> is false, transfer is between the table and the file named
826  * <filename>. Otherwise, transfer is between the table and our regular
827  * input/output stream. The latter could be either stdin/stdout or a
828  * socket, depending on whether we're running under Postmaster control.
829  *
830  * Do not allow a Postgres user without the 'pg_read_server_files' or
831  * 'pg_write_server_files' role to read from or write to a file.
832  *
833  * Do not allow the copy if user doesn't have proper permission to access
834  * the table or the specifically requested columns.
835  */
836 void
837 DoCopy(ParseState *pstate, const CopyStmt *stmt,
838  int stmt_location, int stmt_len,
839  uint64 *processed)
840 {
841  CopyState cstate;
842  bool is_from = stmt->is_from;
843  bool pipe = (stmt->filename == NULL);
844  Relation rel;
845  Oid relid;
846  RawStmt *query = NULL;
847  Node *whereClause = NULL;
848 
849  /*
850  * Disallow COPY to/from file or program except to users with the
851  * appropriate role.
852  */
853  if (!pipe)
854  {
855  if (stmt->is_program)
856  {
857  if (!is_member_of_role(GetUserId(), DEFAULT_ROLE_EXECUTE_SERVER_PROGRAM))
858  ereport(ERROR,
859  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
860  errmsg("must be superuser or a member of the pg_execute_server_program role to COPY to or from an external program"),
861  errhint("Anyone can COPY to stdout or from stdin. "
862  "psql's \\copy command also works for anyone.")));
863  }
864  else
865  {
866  if (is_from && !is_member_of_role(GetUserId(), DEFAULT_ROLE_READ_SERVER_FILES))
867  ereport(ERROR,
868  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
869  errmsg("must be superuser or a member of the pg_read_server_files role to COPY from a file"),
870  errhint("Anyone can COPY to stdout or from stdin. "
871  "psql's \\copy command also works for anyone.")));
872 
873  if (!is_from && !is_member_of_role(GetUserId(), DEFAULT_ROLE_WRITE_SERVER_FILES))
874  ereport(ERROR,
875  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
876  errmsg("must be superuser or a member of the pg_write_server_files role to COPY to a file"),
877  errhint("Anyone can COPY to stdout or from stdin. "
878  "psql's \\copy command also works for anyone.")));
879  }
880  }
881 
882  if (stmt->relation)
883  {
884  LOCKMODE lockmode = is_from ? RowExclusiveLock : AccessShareLock;
885  ParseNamespaceItem *nsitem;
886  RangeTblEntry *rte;
887  TupleDesc tupDesc;
888  List *attnums;
889  ListCell *cur;
890 
891  Assert(!stmt->query);
892 
893  /* Open and lock the relation, using the appropriate lock type. */
894  rel = table_openrv(stmt->relation, lockmode);
895 
896  relid = RelationGetRelid(rel);
897 
898  nsitem = addRangeTableEntryForRelation(pstate, rel, lockmode,
899  NULL, false, false);
900  rte = nsitem->p_rte;
901  rte->requiredPerms = (is_from ? ACL_INSERT : ACL_SELECT);
902 
903  if (stmt->whereClause)
904  {
905  /* add nsitem to query namespace */
906  addNSItemToQuery(pstate, nsitem, false, true, true);
907 
908  /* Transform the raw expression tree */
909  whereClause = transformExpr(pstate, stmt->whereClause, EXPR_KIND_COPY_WHERE);
910 
911  /* Make sure it yields a boolean result. */
912  whereClause = coerce_to_boolean(pstate, whereClause, "WHERE");
913 
914  /* we have to fix its collations too */
915  assign_expr_collations(pstate, whereClause);
916 
917  whereClause = eval_const_expressions(NULL, whereClause);
918 
919  whereClause = (Node *) canonicalize_qual((Expr *) whereClause, false);
920  whereClause = (Node *) make_ands_implicit((Expr *) whereClause);
921  }
922 
923  tupDesc = RelationGetDescr(rel);
924  attnums = CopyGetAttnums(tupDesc, rel, stmt->attlist);
925  foreach(cur, attnums)
926  {
927  int attno = lfirst_int(cur) -
929 
930  if (is_from)
931  rte->insertedCols = bms_add_member(rte->insertedCols, attno);
932  else
933  rte->selectedCols = bms_add_member(rte->selectedCols, attno);
934  }
935  ExecCheckRTPerms(pstate->p_rtable, true);
936 
937  /*
938  * Permission check for row security policies.
939  *
940  * check_enable_rls will ereport(ERROR) if the user has requested
941  * something invalid and will otherwise indicate if we should enable
942  * RLS (returns RLS_ENABLED) or not for this COPY statement.
943  *
944  * If the relation has a row security policy and we are to apply it
945  * then perform a "query" copy and allow the normal query processing
946  * to handle the policies.
947  *
948  * If RLS is not enabled for this, then just fall through to the
949  * normal non-filtering relation handling.
950  */
951  if (check_enable_rls(rte->relid, InvalidOid, false) == RLS_ENABLED)
952  {
954  ColumnRef *cr;
955  ResTarget *target;
956  RangeVar *from;
957  List *targetList = NIL;
958 
959  if (is_from)
960  ereport(ERROR,
961  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
962  errmsg("COPY FROM not supported with row-level security"),
963  errhint("Use INSERT statements instead.")));
964 
965  /*
966  * Build target list
967  *
968  * If no columns are specified in the attribute list of the COPY
969  * command, then the target list is 'all' columns. Therefore, '*'
970  * should be used as the target list for the resulting SELECT
971  * statement.
972  *
973  * In the case that columns are specified in the attribute list,
974  * create a ColumnRef and ResTarget for each column and add them
975  * to the target list for the resulting SELECT statement.
976  */
977  if (!stmt->attlist)
978  {
979  cr = makeNode(ColumnRef);
981  cr->location = -1;
982 
983  target = makeNode(ResTarget);
984  target->name = NULL;
985  target->indirection = NIL;
986  target->val = (Node *) cr;
987  target->location = -1;
988 
989  targetList = list_make1(target);
990  }
991  else
992  {
993  ListCell *lc;
994 
995  foreach(lc, stmt->attlist)
996  {
997  /*
998  * Build the ColumnRef for each column. The ColumnRef
999  * 'fields' property is a String 'Value' node (see
1000  * nodes/value.h) that corresponds to the column name
1001  * respectively.
1002  */
1003  cr = makeNode(ColumnRef);
1004  cr->fields = list_make1(lfirst(lc));
1005  cr->location = -1;
1006 
1007  /* Build the ResTarget and add the ColumnRef to it. */
1008  target = makeNode(ResTarget);
1009  target->name = NULL;
1010  target->indirection = NIL;
1011  target->val = (Node *) cr;
1012  target->location = -1;
1013 
1014  /* Add each column to the SELECT statement's target list */
1015  targetList = lappend(targetList, target);
1016  }
1017  }
1018 
1019  /*
1020  * Build RangeVar for from clause, fully qualified based on the
1021  * relation which we have opened and locked.
1022  */
1025  -1);
1026 
1027  /* Build query */
1028  select = makeNode(SelectStmt);
1029  select->targetList = targetList;
1030  select->fromClause = list_make1(from);
1031 
1032  query = makeNode(RawStmt);
1033  query->stmt = (Node *) select;
1034  query->stmt_location = stmt_location;
1035  query->stmt_len = stmt_len;
1036 
1037  /*
1038  * Close the relation for now, but keep the lock on it to prevent
1039  * changes between now and when we start the query-based COPY.
1040  *
1041  * We'll reopen it later as part of the query-based COPY.
1042  */
1043  table_close(rel, NoLock);
1044  rel = NULL;
1045  }
1046  }
1047  else
1048  {
1049  Assert(stmt->query);
1050 
1051  query = makeNode(RawStmt);
1052  query->stmt = stmt->query;
1053  query->stmt_location = stmt_location;
1054  query->stmt_len = stmt_len;
1055 
1056  relid = InvalidOid;
1057  rel = NULL;
1058  }
1059 
1060  if (is_from)
1061  {
1062  Assert(rel);
1063 
1064  /* check read-only transaction and parallel mode */
1065  if (XactReadOnly && !rel->rd_islocaltemp)
1066  PreventCommandIfReadOnly("COPY FROM");
1067 
1068  cstate = BeginCopyFrom(pstate, rel, stmt->filename, stmt->is_program,
1069  NULL, stmt->attlist, stmt->options);
1070  cstate->whereClause = whereClause;
1071  *processed = CopyFrom(cstate); /* copy from file to database */
1072  EndCopyFrom(cstate);
1073  }
1074  else
1075  {
1076  cstate = BeginCopyTo(pstate, rel, query, relid,
1077  stmt->filename, stmt->is_program,
1078  stmt->attlist, stmt->options);
1079  *processed = DoCopyTo(cstate); /* copy from database to file */
1080  EndCopyTo(cstate);
1081  }
1082 
1083  /*
1084  * Close the relation. If reading, we can release the AccessShareLock we
1085  * got; if writing, we should hold the lock until end of transaction to
1086  * ensure that updates will be committed before lock is released.
1087  */
1088  if (rel != NULL)
1089  table_close(rel, (is_from ? NoLock : AccessShareLock));
1090 }
1091 
1092 /*
1093  * Process the statement option list for COPY.
1094  *
1095  * Scan the options list (a list of DefElem) and transpose the information
1096  * into cstate, applying appropriate error checking.
1097  *
1098  * cstate is assumed to be filled with zeroes initially.
1099  *
1100  * This is exported so that external users of the COPY API can sanity-check
1101  * a list of options. In that usage, cstate should be passed as NULL
1102  * (since external users don't know sizeof(CopyStateData)) and the collected
1103  * data is just leaked until CurrentMemoryContext is reset.
1104  *
1105  * Note that additional checking, such as whether column names listed in FORCE
1106  * QUOTE actually exist, has to be applied later. This just checks for
1107  * self-consistency of the options list.
1108  */
1109 void
1111  CopyState cstate,
1112  bool is_from,
1113  List *options)
1114 {
1115  bool format_specified = false;
1116  ListCell *option;
1117 
1118  /* Support external use for option sanity checking */
1119  if (cstate == NULL)
1120  cstate = (CopyStateData *) palloc0(sizeof(CopyStateData));
1121 
1122  cstate->is_copy_from = is_from;
1123 
1124  cstate->file_encoding = -1;
1125 
1126  /* Extract options from the statement node tree */
1127  foreach(option, options)
1128  {
1129  DefElem *defel = lfirst_node(DefElem, option);
1130 
1131  if (strcmp(defel->defname, "format") == 0)
1132  {
1133  char *fmt = defGetString(defel);
1134 
1135  if (format_specified)
1136  ereport(ERROR,
1137  (errcode(ERRCODE_SYNTAX_ERROR),
1138  errmsg("conflicting or redundant options"),
1139  parser_errposition(pstate, defel->location)));
1140  format_specified = true;
1141  if (strcmp(fmt, "text") == 0)
1142  /* default format */ ;
1143  else if (strcmp(fmt, "csv") == 0)
1144  cstate->csv_mode = true;
1145  else if (strcmp(fmt, "binary") == 0)
1146  cstate->binary = true;
1147  else
1148  ereport(ERROR,
1149  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1150  errmsg("COPY format \"%s\" not recognized", fmt),
1151  parser_errposition(pstate, defel->location)));
1152  }
1153  else if (strcmp(defel->defname, "freeze") == 0)
1154  {
1155  if (cstate->freeze)
1156  ereport(ERROR,
1157  (errcode(ERRCODE_SYNTAX_ERROR),
1158  errmsg("conflicting or redundant options"),
1159  parser_errposition(pstate, defel->location)));
1160  cstate->freeze = defGetBoolean(defel);
1161  }
1162  else if (strcmp(defel->defname, "delimiter") == 0)
1163  {
1164  if (cstate->delim)
1165  ereport(ERROR,
1166  (errcode(ERRCODE_SYNTAX_ERROR),
1167  errmsg("conflicting or redundant options"),
1168  parser_errposition(pstate, defel->location)));
1169  cstate->delim = defGetString(defel);
1170  }
1171  else if (strcmp(defel->defname, "null") == 0)
1172  {
1173  if (cstate->null_print)
1174  ereport(ERROR,
1175  (errcode(ERRCODE_SYNTAX_ERROR),
1176  errmsg("conflicting or redundant options"),
1177  parser_errposition(pstate, defel->location)));
1178  cstate->null_print = defGetString(defel);
1179  }
1180  else if (strcmp(defel->defname, "header") == 0)
1181  {
1182  if (cstate->header_line)
1183  ereport(ERROR,
1184  (errcode(ERRCODE_SYNTAX_ERROR),
1185  errmsg("conflicting or redundant options"),
1186  parser_errposition(pstate, defel->location)));
1187  cstate->header_line = defGetBoolean(defel);
1188  }
1189  else if (strcmp(defel->defname, "quote") == 0)
1190  {
1191  if (cstate->quote)
1192  ereport(ERROR,
1193  (errcode(ERRCODE_SYNTAX_ERROR),
1194  errmsg("conflicting or redundant options"),
1195  parser_errposition(pstate, defel->location)));
1196  cstate->quote = defGetString(defel);
1197  }
1198  else if (strcmp(defel->defname, "escape") == 0)
1199  {
1200  if (cstate->escape)
1201  ereport(ERROR,
1202  (errcode(ERRCODE_SYNTAX_ERROR),
1203  errmsg("conflicting or redundant options"),
1204  parser_errposition(pstate, defel->location)));
1205  cstate->escape = defGetString(defel);
1206  }
1207  else if (strcmp(defel->defname, "force_quote") == 0)
1208  {
1209  if (cstate->force_quote || cstate->force_quote_all)
1210  ereport(ERROR,
1211  (errcode(ERRCODE_SYNTAX_ERROR),
1212  errmsg("conflicting or redundant options"),
1213  parser_errposition(pstate, defel->location)));
1214  if (defel->arg && IsA(defel->arg, A_Star))
1215  cstate->force_quote_all = true;
1216  else if (defel->arg && IsA(defel->arg, List))
1217  cstate->force_quote = castNode(List, defel->arg);
1218  else
1219  ereport(ERROR,
1220  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1221  errmsg("argument to option \"%s\" must be a list of column names",
1222  defel->defname),
1223  parser_errposition(pstate, defel->location)));
1224  }
1225  else if (strcmp(defel->defname, "force_not_null") == 0)
1226  {
1227  if (cstate->force_notnull)
1228  ereport(ERROR,
1229  (errcode(ERRCODE_SYNTAX_ERROR),
1230  errmsg("conflicting or redundant options"),
1231  parser_errposition(pstate, defel->location)));
1232  if (defel->arg && IsA(defel->arg, List))
1233  cstate->force_notnull = castNode(List, defel->arg);
1234  else
1235  ereport(ERROR,
1236  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1237  errmsg("argument to option \"%s\" must be a list of column names",
1238  defel->defname),
1239  parser_errposition(pstate, defel->location)));
1240  }
1241  else if (strcmp(defel->defname, "force_null") == 0)
1242  {
1243  if (cstate->force_null)
1244  ereport(ERROR,
1245  (errcode(ERRCODE_SYNTAX_ERROR),
1246  errmsg("conflicting or redundant options")));
1247  if (defel->arg && IsA(defel->arg, List))
1248  cstate->force_null = castNode(List, defel->arg);
1249  else
1250  ereport(ERROR,
1251  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1252  errmsg("argument to option \"%s\" must be a list of column names",
1253  defel->defname),
1254  parser_errposition(pstate, defel->location)));
1255  }
1256  else if (strcmp(defel->defname, "convert_selectively") == 0)
1257  {
1258  /*
1259  * Undocumented, not-accessible-from-SQL option: convert only the
1260  * named columns to binary form, storing the rest as NULLs. It's
1261  * allowed for the column list to be NIL.
1262  */
1263  if (cstate->convert_selectively)
1264  ereport(ERROR,
1265  (errcode(ERRCODE_SYNTAX_ERROR),
1266  errmsg("conflicting or redundant options"),
1267  parser_errposition(pstate, defel->location)));
1268  cstate->convert_selectively = true;
1269  if (defel->arg == NULL || IsA(defel->arg, List))
1270  cstate->convert_select = castNode(List, defel->arg);
1271  else
1272  ereport(ERROR,
1273  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1274  errmsg("argument to option \"%s\" must be a list of column names",
1275  defel->defname),
1276  parser_errposition(pstate, defel->location)));
1277  }
1278  else if (strcmp(defel->defname, "encoding") == 0)
1279  {
1280  if (cstate->file_encoding >= 0)
1281  ereport(ERROR,
1282  (errcode(ERRCODE_SYNTAX_ERROR),
1283  errmsg("conflicting or redundant options"),
1284  parser_errposition(pstate, defel->location)));
1286  if (cstate->file_encoding < 0)
1287  ereport(ERROR,
1288  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1289  errmsg("argument to option \"%s\" must be a valid encoding name",
1290  defel->defname),
1291  parser_errposition(pstate, defel->location)));
1292  }
1293  else
1294  ereport(ERROR,
1295  (errcode(ERRCODE_SYNTAX_ERROR),
1296  errmsg("option \"%s\" not recognized",
1297  defel->defname),
1298  parser_errposition(pstate, defel->location)));
1299  }
1300 
1301  /*
1302  * Check for incompatible options (must do these two before inserting
1303  * defaults)
1304  */
1305  if (cstate->binary && cstate->delim)
1306  ereport(ERROR,
1307  (errcode(ERRCODE_SYNTAX_ERROR),
1308  errmsg("cannot specify DELIMITER in BINARY mode")));
1309 
1310  if (cstate->binary && cstate->null_print)
1311  ereport(ERROR,
1312  (errcode(ERRCODE_SYNTAX_ERROR),
1313  errmsg("cannot specify NULL in BINARY mode")));
1314 
1315  /* Set defaults for omitted options */
1316  if (!cstate->delim)
1317  cstate->delim = cstate->csv_mode ? "," : "\t";
1318 
1319  if (!cstate->null_print)
1320  cstate->null_print = cstate->csv_mode ? "" : "\\N";
1321  cstate->null_print_len = strlen(cstate->null_print);
1322 
1323  if (cstate->csv_mode)
1324  {
1325  if (!cstate->quote)
1326  cstate->quote = "\"";
1327  if (!cstate->escape)
1328  cstate->escape = cstate->quote;
1329  }
1330 
1331  /* Only single-byte delimiter strings are supported. */
1332  if (strlen(cstate->delim) != 1)
1333  ereport(ERROR,
1334  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1335  errmsg("COPY delimiter must be a single one-byte character")));
1336 
1337  /* Disallow end-of-line characters */
1338  if (strchr(cstate->delim, '\r') != NULL ||
1339  strchr(cstate->delim, '\n') != NULL)
1340  ereport(ERROR,
1341  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1342  errmsg("COPY delimiter cannot be newline or carriage return")));
1343 
1344  if (strchr(cstate->null_print, '\r') != NULL ||
1345  strchr(cstate->null_print, '\n') != NULL)
1346  ereport(ERROR,
1347  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1348  errmsg("COPY null representation cannot use newline or carriage return")));
1349 
1350  /*
1351  * Disallow unsafe delimiter characters in non-CSV mode. We can't allow
1352  * backslash because it would be ambiguous. We can't allow the other
1353  * cases because data characters matching the delimiter must be
1354  * backslashed, and certain backslash combinations are interpreted
1355  * non-literally by COPY IN. Disallowing all lower case ASCII letters is
1356  * more than strictly necessary, but seems best for consistency and
1357  * future-proofing. Likewise we disallow all digits though only octal
1358  * digits are actually dangerous.
1359  */
1360  if (!cstate->csv_mode &&
1361  strchr("\\.abcdefghijklmnopqrstuvwxyz0123456789",
1362  cstate->delim[0]) != NULL)
1363  ereport(ERROR,
1364  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1365  errmsg("COPY delimiter cannot be \"%s\"", cstate->delim)));
1366 
1367  /* Check header */
1368  if (!cstate->csv_mode && cstate->header_line)
1369  ereport(ERROR,
1370  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1371  errmsg("COPY HEADER available only in CSV mode")));
1372 
1373  /* Check quote */
1374  if (!cstate->csv_mode && cstate->quote != NULL)
1375  ereport(ERROR,
1376  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1377  errmsg("COPY quote available only in CSV mode")));
1378 
1379  if (cstate->csv_mode && strlen(cstate->quote) != 1)
1380  ereport(ERROR,
1381  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1382  errmsg("COPY quote must be a single one-byte character")));
1383 
1384  if (cstate->csv_mode && cstate->delim[0] == cstate->quote[0])
1385  ereport(ERROR,
1386  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1387  errmsg("COPY delimiter and quote must be different")));
1388 
1389  /* Check escape */
1390  if (!cstate->csv_mode && cstate->escape != NULL)
1391  ereport(ERROR,
1392  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1393  errmsg("COPY escape available only in CSV mode")));
1394 
1395  if (cstate->csv_mode && strlen(cstate->escape) != 1)
1396  ereport(ERROR,
1397  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1398  errmsg("COPY escape must be a single one-byte character")));
1399 
1400  /* Check force_quote */
1401  if (!cstate->csv_mode && (cstate->force_quote || cstate->force_quote_all))
1402  ereport(ERROR,
1403  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1404  errmsg("COPY force quote available only in CSV mode")));
1405  if ((cstate->force_quote || cstate->force_quote_all) && is_from)
1406  ereport(ERROR,
1407  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1408  errmsg("COPY force quote only available using COPY TO")));
1409 
1410  /* Check force_notnull */
1411  if (!cstate->csv_mode && cstate->force_notnull != NIL)
1412  ereport(ERROR,
1413  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1414  errmsg("COPY force not null available only in CSV mode")));
1415  if (cstate->force_notnull != NIL && !is_from)
1416  ereport(ERROR,
1417  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1418  errmsg("COPY force not null only available using COPY FROM")));
1419 
1420  /* Check force_null */
1421  if (!cstate->csv_mode && cstate->force_null != NIL)
1422  ereport(ERROR,
1423  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1424  errmsg("COPY force null available only in CSV mode")));
1425 
1426  if (cstate->force_null != NIL && !is_from)
1427  ereport(ERROR,
1428  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1429  errmsg("COPY force null only available using COPY FROM")));
1430 
1431  /* Don't allow the delimiter to appear in the null string. */
1432  if (strchr(cstate->null_print, cstate->delim[0]) != NULL)
1433  ereport(ERROR,
1434  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1435  errmsg("COPY delimiter must not appear in the NULL specification")));
1436 
1437  /* Don't allow the CSV quote char to appear in the null string. */
1438  if (cstate->csv_mode &&
1439  strchr(cstate->null_print, cstate->quote[0]) != NULL)
1440  ereport(ERROR,
1441  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1442  errmsg("CSV quote character must not appear in the NULL specification")));
1443 }
1444 
1445 /*
1446  * Common setup routines used by BeginCopyFrom and BeginCopyTo.
1447  *
1448  * Iff <binary>, unload or reload in the binary format, as opposed to the
1449  * more wasteful but more robust and portable text format.
1450  *
1451  * Iff <oids>, unload or reload the format that includes OID information.
1452  * On input, we accept OIDs whether or not the table has an OID column,
1453  * but silently drop them if it does not. On output, we report an error
1454  * if the user asks for OIDs in a table that has none (not providing an
1455  * OID column might seem friendlier, but could seriously confuse programs).
1456  *
1457  * If in the text format, delimit columns with delimiter <delim> and print
1458  * NULL values as <null_print>.
1459  */
1460 static CopyState
1462  bool is_from,
1463  Relation rel,
1464  RawStmt *raw_query,
1465  Oid queryRelId,
1466  List *attnamelist,
1467  List *options)
1468 {
1469  CopyState cstate;
1470  TupleDesc tupDesc;
1471  int num_phys_attrs;
1472  MemoryContext oldcontext;
1473 
1474  /* Allocate workspace and zero all fields */
1475  cstate = (CopyStateData *) palloc0(sizeof(CopyStateData));
1476 
1477  /*
1478  * We allocate everything used by a cstate in a new memory context. This
1479  * avoids memory leaks during repeated use of COPY in a query.
1480  */
1482  "COPY",
1484 
1485  oldcontext = MemoryContextSwitchTo(cstate->copycontext);
1486 
1487  /* Extract options from the statement node tree */
1488  ProcessCopyOptions(pstate, cstate, is_from, options);
1489 
1490  /* Process the source/target relation or query */
1491  if (rel)
1492  {
1493  Assert(!raw_query);
1494 
1495  cstate->rel = rel;
1496 
1497  tupDesc = RelationGetDescr(cstate->rel);
1498  }
1499  else
1500  {
1501  List *rewritten;
1502  Query *query;
1503  PlannedStmt *plan;
1504  DestReceiver *dest;
1505 
1506  Assert(!is_from);
1507  cstate->rel = NULL;
1508 
1509  /*
1510  * Run parse analysis and rewrite. Note this also acquires sufficient
1511  * locks on the source table(s).
1512  *
1513  * Because the parser and planner tend to scribble on their input, we
1514  * make a preliminary copy of the source querytree. This prevents
1515  * problems in the case that the COPY is in a portal or plpgsql
1516  * function and is executed repeatedly. (See also the same hack in
1517  * DECLARE CURSOR and PREPARE.) XXX FIXME someday.
1518  */
1519  rewritten = pg_analyze_and_rewrite(copyObject(raw_query),
1520  pstate->p_sourcetext, NULL, 0,
1521  NULL);
1522 
1523  /* check that we got back something we can work with */
1524  if (rewritten == NIL)
1525  {
1526  ereport(ERROR,
1527  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1528  errmsg("DO INSTEAD NOTHING rules are not supported for COPY")));
1529  }
1530  else if (list_length(rewritten) > 1)
1531  {
1532  ListCell *lc;
1533 
1534  /* examine queries to determine which error message to issue */
1535  foreach(lc, rewritten)
1536  {
1537  Query *q = lfirst_node(Query, lc);
1538 
1540  ereport(ERROR,
1541  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1542  errmsg("conditional DO INSTEAD rules are not supported for COPY")));
1544  ereport(ERROR,
1545  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1546  errmsg("DO ALSO rules are not supported for the COPY")));
1547  }
1548 
1549  ereport(ERROR,
1550  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1551  errmsg("multi-statement DO INSTEAD rules are not supported for COPY")));
1552  }
1553 
1554  query = linitial_node(Query, rewritten);
1555 
1556  /* The grammar allows SELECT INTO, but we don't support that */
1557  if (query->utilityStmt != NULL &&
1559  ereport(ERROR,
1560  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1561  errmsg("COPY (SELECT INTO) is not supported")));
1562 
1563  Assert(query->utilityStmt == NULL);
1564 
1565  /*
1566  * Similarly the grammar doesn't enforce the presence of a RETURNING
1567  * clause, but this is required here.
1568  */
1569  if (query->commandType != CMD_SELECT &&
1570  query->returningList == NIL)
1571  {
1572  Assert(query->commandType == CMD_INSERT ||
1573  query->commandType == CMD_UPDATE ||
1574  query->commandType == CMD_DELETE);
1575 
1576  ereport(ERROR,
1577  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1578  errmsg("COPY query must have a RETURNING clause")));
1579  }
1580 
1581  /* plan the query */
1582  plan = pg_plan_query(query, CURSOR_OPT_PARALLEL_OK, NULL);
1583 
1584  /*
1585  * With row level security and a user using "COPY relation TO", we
1586  * have to convert the "COPY relation TO" to a query-based COPY (eg:
1587  * "COPY (SELECT * FROM relation) TO"), to allow the rewriter to add
1588  * in any RLS clauses.
1589  *
1590  * When this happens, we are passed in the relid of the originally
1591  * found relation (which we have locked). As the planner will look up
1592  * the relation again, we double-check here to make sure it found the
1593  * same one that we have locked.
1594  */
1595  if (queryRelId != InvalidOid)
1596  {
1597  /*
1598  * Note that with RLS involved there may be multiple relations,
1599  * and while the one we need is almost certainly first, we don't
1600  * make any guarantees of that in the planner, so check the whole
1601  * list and make sure we find the original relation.
1602  */
1603  if (!list_member_oid(plan->relationOids, queryRelId))
1604  ereport(ERROR,
1605  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1606  errmsg("relation referenced by COPY statement has changed")));
1607  }
1608 
1609  /*
1610  * Use a snapshot with an updated command ID to ensure this query sees
1611  * results of any previously executed queries.
1612  */
1615 
1616  /* Create dest receiver for COPY OUT */
1618  ((DR_copy *) dest)->cstate = cstate;
1619 
1620  /* Create a QueryDesc requesting no output */
1621  cstate->queryDesc = CreateQueryDesc(plan, pstate->p_sourcetext,
1624  dest, NULL, NULL, 0);
1625 
1626  /*
1627  * Call ExecutorStart to prepare the plan for execution.
1628  *
1629  * ExecutorStart computes a result tupdesc for us
1630  */
1631  ExecutorStart(cstate->queryDesc, 0);
1632 
1633  tupDesc = cstate->queryDesc->tupDesc;
1634  }
1635 
1636  /* Generate or convert list of attributes to process */
1637  cstate->attnumlist = CopyGetAttnums(tupDesc, cstate->rel, attnamelist);
1638 
1639  num_phys_attrs = tupDesc->natts;
1640 
1641  /* Convert FORCE_QUOTE name list to per-column flags, check validity */
1642  cstate->force_quote_flags = (bool *) palloc0(num_phys_attrs * sizeof(bool));
1643  if (cstate->force_quote_all)
1644  {
1645  int i;
1646 
1647  for (i = 0; i < num_phys_attrs; i++)
1648  cstate->force_quote_flags[i] = true;
1649  }
1650  else if (cstate->force_quote)
1651  {
1652  List *attnums;
1653  ListCell *cur;
1654 
1655  attnums = CopyGetAttnums(tupDesc, cstate->rel, cstate->force_quote);
1656 
1657  foreach(cur, attnums)
1658  {
1659  int attnum = lfirst_int(cur);
1660  Form_pg_attribute attr = TupleDescAttr(tupDesc, attnum - 1);
1661 
1662  if (!list_member_int(cstate->attnumlist, attnum))
1663  ereport(ERROR,
1664  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1665  errmsg("FORCE_QUOTE column \"%s\" not referenced by COPY",
1666  NameStr(attr->attname))));
1667  cstate->force_quote_flags[attnum - 1] = true;
1668  }
1669  }
1670 
1671  /* Convert FORCE_NOT_NULL name list to per-column flags, check validity */
1672  cstate->force_notnull_flags = (bool *) palloc0(num_phys_attrs * sizeof(bool));
1673  if (cstate->force_notnull)
1674  {
1675  List *attnums;
1676  ListCell *cur;
1677 
1678  attnums = CopyGetAttnums(tupDesc, cstate->rel, cstate->force_notnull);
1679 
1680  foreach(cur, attnums)
1681  {
1682  int attnum = lfirst_int(cur);
1683  Form_pg_attribute attr = TupleDescAttr(tupDesc, attnum - 1);
1684 
1685  if (!list_member_int(cstate->attnumlist, attnum))
1686  ereport(ERROR,
1687  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1688  errmsg("FORCE_NOT_NULL column \"%s\" not referenced by COPY",
1689  NameStr(attr->attname))));
1690  cstate->force_notnull_flags[attnum - 1] = true;
1691  }
1692  }
1693 
1694  /* Convert FORCE_NULL name list to per-column flags, check validity */
1695  cstate->force_null_flags = (bool *) palloc0(num_phys_attrs * sizeof(bool));
1696  if (cstate->force_null)
1697  {
1698  List *attnums;
1699  ListCell *cur;
1700 
1701  attnums = CopyGetAttnums(tupDesc, cstate->rel, cstate->force_null);
1702 
1703  foreach(cur, attnums)
1704  {
1705  int attnum = lfirst_int(cur);
1706  Form_pg_attribute attr = TupleDescAttr(tupDesc, attnum - 1);
1707 
1708  if (!list_member_int(cstate->attnumlist, attnum))
1709  ereport(ERROR,
1710  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1711  errmsg("FORCE_NULL column \"%s\" not referenced by COPY",
1712  NameStr(attr->attname))));
1713  cstate->force_null_flags[attnum - 1] = true;
1714  }
1715  }
1716 
1717  /* Convert convert_selectively name list to per-column flags */
1718  if (cstate->convert_selectively)
1719  {
1720  List *attnums;
1721  ListCell *cur;
1722 
1723  cstate->convert_select_flags = (bool *) palloc0(num_phys_attrs * sizeof(bool));
1724 
1725  attnums = CopyGetAttnums(tupDesc, cstate->rel, cstate->convert_select);
1726 
1727  foreach(cur, attnums)
1728  {
1729  int attnum = lfirst_int(cur);
1730  Form_pg_attribute attr = TupleDescAttr(tupDesc, attnum - 1);
1731 
1732  if (!list_member_int(cstate->attnumlist, attnum))
1733  ereport(ERROR,
1734  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1735  errmsg_internal("selected column \"%s\" not referenced by COPY",
1736  NameStr(attr->attname))));
1737  cstate->convert_select_flags[attnum - 1] = true;
1738  }
1739  }
1740 
1741  /* Use client encoding when ENCODING option is not specified. */
1742  if (cstate->file_encoding < 0)
1744 
1745  /*
1746  * Set up encoding conversion info. Even if the file and server encodings
1747  * are the same, we must apply pg_any_to_server() to validate data in
1748  * multibyte encodings.
1749  */
1750  cstate->need_transcoding =
1751  (cstate->file_encoding != GetDatabaseEncoding() ||
1753  /* See Multibyte encoding comment above */
1755 
1756  cstate->copy_dest = COPY_FILE; /* default */
1757 
1758  MemoryContextSwitchTo(oldcontext);
1759 
1760  return cstate;
1761 }
1762 
1763 /*
1764  * Closes the pipe to an external program, checking the pclose() return code.
1765  */
1766 static void
1768 {
1769  int pclose_rc;
1770 
1771  Assert(cstate->is_program);
1772 
1773  pclose_rc = ClosePipeStream(cstate->copy_file);
1774  if (pclose_rc == -1)
1775  ereport(ERROR,
1777  errmsg("could not close pipe to external command: %m")));
1778  else if (pclose_rc != 0)
1779  {
1780  /*
1781  * If we ended a COPY FROM PROGRAM before reaching EOF, then it's
1782  * expectable for the called program to fail with SIGPIPE, and we
1783  * should not report that as an error. Otherwise, SIGPIPE indicates a
1784  * problem.
1785  */
1786  if (cstate->is_copy_from && !cstate->reached_eof &&
1787  wait_result_is_signal(pclose_rc, SIGPIPE))
1788  return;
1789 
1790  ereport(ERROR,
1791  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
1792  errmsg("program \"%s\" failed",
1793  cstate->filename),
1794  errdetail_internal("%s", wait_result_to_str(pclose_rc))));
1795  }
1796 }
1797 
1798 /*
1799  * Release resources allocated in a cstate for COPY TO/FROM.
1800  */
1801 static void
1803 {
1804  if (cstate->is_program)
1805  {
1806  ClosePipeToProgram(cstate);
1807  }
1808  else
1809  {
1810  if (cstate->filename != NULL && FreeFile(cstate->copy_file))
1811  ereport(ERROR,
1813  errmsg("could not close file \"%s\": %m",
1814  cstate->filename)));
1815  }
1816 
1818  pfree(cstate);
1819 }
1820 
1821 /*
1822  * Setup CopyState to read tuples from a table or a query for COPY TO.
1823  */
1824 static CopyState
1826  Relation rel,
1827  RawStmt *query,
1828  Oid queryRelId,
1829  const char *filename,
1830  bool is_program,
1831  List *attnamelist,
1832  List *options)
1833 {
1834  CopyState cstate;
1835  bool pipe = (filename == NULL);
1836  MemoryContext oldcontext;
1837 
1838  if (rel != NULL && rel->rd_rel->relkind != RELKIND_RELATION)
1839  {
1840  if (rel->rd_rel->relkind == RELKIND_VIEW)
1841  ereport(ERROR,
1842  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1843  errmsg("cannot copy from view \"%s\"",
1845  errhint("Try the COPY (SELECT ...) TO variant.")));
1846  else if (rel->rd_rel->relkind == RELKIND_MATVIEW)
1847  ereport(ERROR,
1848  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1849  errmsg("cannot copy from materialized view \"%s\"",
1851  errhint("Try the COPY (SELECT ...) TO variant.")));
1852  else if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1853  ereport(ERROR,
1854  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1855  errmsg("cannot copy from foreign table \"%s\"",
1857  errhint("Try the COPY (SELECT ...) TO variant.")));
1858  else if (rel->rd_rel->relkind == RELKIND_SEQUENCE)
1859  ereport(ERROR,
1860  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1861  errmsg("cannot copy from sequence \"%s\"",
1862  RelationGetRelationName(rel))));
1863  else if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1864  ereport(ERROR,
1865  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1866  errmsg("cannot copy from partitioned table \"%s\"",
1868  errhint("Try the COPY (SELECT ...) TO variant.")));
1869  else
1870  ereport(ERROR,
1871  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1872  errmsg("cannot copy from non-table relation \"%s\"",
1873  RelationGetRelationName(rel))));
1874  }
1875 
1876  cstate = BeginCopy(pstate, false, rel, query, queryRelId, attnamelist,
1877  options);
1878  oldcontext = MemoryContextSwitchTo(cstate->copycontext);
1879 
1880  if (pipe)
1881  {
1882  Assert(!is_program); /* the grammar does not allow this */
1884  cstate->copy_file = stdout;
1885  }
1886  else
1887  {
1888  cstate->filename = pstrdup(filename);
1889  cstate->is_program = is_program;
1890 
1891  if (is_program)
1892  {
1893  cstate->copy_file = OpenPipeStream(cstate->filename, PG_BINARY_W);
1894  if (cstate->copy_file == NULL)
1895  ereport(ERROR,
1897  errmsg("could not execute command \"%s\": %m",
1898  cstate->filename)));
1899  }
1900  else
1901  {
1902  mode_t oumask; /* Pre-existing umask value */
1903  struct stat st;
1904 
1905  /*
1906  * Prevent write to relative path ... too easy to shoot oneself in
1907  * the foot by overwriting a database file ...
1908  */
1909  if (!is_absolute_path(filename))
1910  ereport(ERROR,
1911  (errcode(ERRCODE_INVALID_NAME),
1912  errmsg("relative path not allowed for COPY to file")));
1913 
1914  oumask = umask(S_IWGRP | S_IWOTH);
1915  PG_TRY();
1916  {
1917  cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_W);
1918  }
1919  PG_FINALLY();
1920  {
1921  umask(oumask);
1922  }
1923  PG_END_TRY();
1924  if (cstate->copy_file == NULL)
1925  {
1926  /* copy errno because ereport subfunctions might change it */
1927  int save_errno = errno;
1928 
1929  ereport(ERROR,
1931  errmsg("could not open file \"%s\" for writing: %m",
1932  cstate->filename),
1933  (save_errno == ENOENT || save_errno == EACCES) ?
1934  errhint("COPY TO instructs the PostgreSQL server process to write a file. "
1935  "You may want a client-side facility such as psql's \\copy.") : 0));
1936  }
1937 
1938  if (fstat(fileno(cstate->copy_file), &st))
1939  ereport(ERROR,
1941  errmsg("could not stat file \"%s\": %m",
1942  cstate->filename)));
1943 
1944  if (S_ISDIR(st.st_mode))
1945  ereport(ERROR,
1946  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1947  errmsg("\"%s\" is a directory", cstate->filename)));
1948  }
1949  }
1950 
1951  MemoryContextSwitchTo(oldcontext);
1952 
1953  return cstate;
1954 }
1955 
1956 /*
1957  * This intermediate routine exists mainly to localize the effects of setjmp
1958  * so we don't need to plaster a lot of variables with "volatile".
1959  */
1960 static uint64
1962 {
1963  bool pipe = (cstate->filename == NULL);
1964  bool fe_copy = (pipe && whereToSendOutput == DestRemote);
1965  uint64 processed;
1966 
1967  PG_TRY();
1968  {
1969  if (fe_copy)
1970  SendCopyBegin(cstate);
1971 
1972  processed = CopyTo(cstate);
1973 
1974  if (fe_copy)
1975  SendCopyEnd(cstate);
1976  }
1977  PG_CATCH();
1978  {
1979  /*
1980  * Make sure we turn off old-style COPY OUT mode upon error. It is
1981  * okay to do this in all cases, since it does nothing if the mode is
1982  * not on.
1983  */
1984  pq_endcopyout(true);
1985  PG_RE_THROW();
1986  }
1987  PG_END_TRY();
1988 
1989  return processed;
1990 }
1991 
1992 /*
1993  * Clean up storage and release resources for COPY TO.
1994  */
1995 static void
1997 {
1998  if (cstate->queryDesc != NULL)
1999  {
2000  /* Close down the query and free resources. */
2001  ExecutorFinish(cstate->queryDesc);
2002  ExecutorEnd(cstate->queryDesc);
2003  FreeQueryDesc(cstate->queryDesc);
2005  }
2006 
2007  /* Clean up storage */
2008  EndCopy(cstate);
2009 }
2010 
2011 /*
2012  * Copy from relation or query TO file.
2013  */
2014 static uint64
2016 {
2017  TupleDesc tupDesc;
2018  int num_phys_attrs;
2019  ListCell *cur;
2020  uint64 processed;
2021 
2022  if (cstate->rel)
2023  tupDesc = RelationGetDescr(cstate->rel);
2024  else
2025  tupDesc = cstate->queryDesc->tupDesc;
2026  num_phys_attrs = tupDesc->natts;
2027  cstate->null_print_client = cstate->null_print; /* default */
2028 
2029  /* We use fe_msgbuf as a per-row buffer regardless of copy_dest */
2030  cstate->fe_msgbuf = makeStringInfo();
2031 
2032  /* Get info about the columns we need to process. */
2033  cstate->out_functions = (FmgrInfo *) palloc(num_phys_attrs * sizeof(FmgrInfo));
2034  foreach(cur, cstate->attnumlist)
2035  {
2036  int attnum = lfirst_int(cur);
2037  Oid out_func_oid;
2038  bool isvarlena;
2039  Form_pg_attribute attr = TupleDescAttr(tupDesc, attnum - 1);
2040 
2041  if (cstate->binary)
2042  getTypeBinaryOutputInfo(attr->atttypid,
2043  &out_func_oid,
2044  &isvarlena);
2045  else
2046  getTypeOutputInfo(attr->atttypid,
2047  &out_func_oid,
2048  &isvarlena);
2049  fmgr_info(out_func_oid, &cstate->out_functions[attnum - 1]);
2050  }
2051 
2052  /*
2053  * Create a temporary memory context that we can reset once per row to
2054  * recover palloc'd memory. This avoids any problems with leaks inside
2055  * datatype output routines, and should be faster than retail pfree's
2056  * anyway. (We don't need a whole econtext as CopyFrom does.)
2057  */
2059  "COPY TO",
2061 
2062  if (cstate->binary)
2063  {
2064  /* Generate header for a binary copy */
2065  int32 tmp;
2066 
2067  /* Signature */
2068  CopySendData(cstate, BinarySignature, 11);
2069  /* Flags field */
2070  tmp = 0;
2071  CopySendInt32(cstate, tmp);
2072  /* No header extension */
2073  tmp = 0;
2074  CopySendInt32(cstate, tmp);
2075  }
2076  else
2077  {
2078  /*
2079  * For non-binary copy, we need to convert null_print to file
2080  * encoding, because it will be sent directly with CopySendString.
2081  */
2082  if (cstate->need_transcoding)
2083  cstate->null_print_client = pg_server_to_any(cstate->null_print,
2084  cstate->null_print_len,
2085  cstate->file_encoding);
2086 
2087  /* if a header has been requested send the line */
2088  if (cstate->header_line)
2089  {
2090  bool hdr_delim = false;
2091 
2092  foreach(cur, cstate->attnumlist)
2093  {
2094  int attnum = lfirst_int(cur);
2095  char *colname;
2096 
2097  if (hdr_delim)
2098  CopySendChar(cstate, cstate->delim[0]);
2099  hdr_delim = true;
2100 
2101  colname = NameStr(TupleDescAttr(tupDesc, attnum - 1)->attname);
2102 
2103  CopyAttributeOutCSV(cstate, colname, false,
2104  list_length(cstate->attnumlist) == 1);
2105  }
2106 
2107  CopySendEndOfRow(cstate);
2108  }
2109  }
2110 
2111  if (cstate->rel)
2112  {
2113  TupleTableSlot *slot;
2114  TableScanDesc scandesc;
2115 
2116  scandesc = table_beginscan(cstate->rel, GetActiveSnapshot(), 0, NULL);
2117  slot = table_slot_create(cstate->rel, NULL);
2118 
2119  processed = 0;
2120  while (table_scan_getnextslot(scandesc, ForwardScanDirection, slot))
2121  {
2123 
2124  /* Deconstruct the tuple ... */
2125  slot_getallattrs(slot);
2126 
2127  /* Format and send the data */
2128  CopyOneRowTo(cstate, slot);
2129  processed++;
2130  }
2131 
2133  table_endscan(scandesc);
2134  }
2135  else
2136  {
2137  /* run the plan --- the dest receiver will send tuples */
2138  ExecutorRun(cstate->queryDesc, ForwardScanDirection, 0L, true);
2139  processed = ((DR_copy *) cstate->queryDesc->dest)->processed;
2140  }
2141 
2142  if (cstate->binary)
2143  {
2144  /* Generate trailer for a binary copy */
2145  CopySendInt16(cstate, -1);
2146  /* Need to flush out the trailer */
2147  CopySendEndOfRow(cstate);
2148  }
2149 
2151 
2152  return processed;
2153 }
2154 
2155 /*
2156  * Emit one row during CopyTo().
2157  */
2158 static void
2160 {
2161  bool need_delim = false;
2163  MemoryContext oldcontext;
2164  ListCell *cur;
2165  char *string;
2166 
2167  MemoryContextReset(cstate->rowcontext);
2168  oldcontext = MemoryContextSwitchTo(cstate->rowcontext);
2169 
2170  if (cstate->binary)
2171  {
2172  /* Binary per-tuple header */
2173  CopySendInt16(cstate, list_length(cstate->attnumlist));
2174  }
2175 
2176  /* Make sure the tuple is fully deconstructed */
2177  slot_getallattrs(slot);
2178 
2179  foreach(cur, cstate->attnumlist)
2180  {
2181  int attnum = lfirst_int(cur);
2182  Datum value = slot->tts_values[attnum - 1];
2183  bool isnull = slot->tts_isnull[attnum - 1];
2184 
2185  if (!cstate->binary)
2186  {
2187  if (need_delim)
2188  CopySendChar(cstate, cstate->delim[0]);
2189  need_delim = true;
2190  }
2191 
2192  if (isnull)
2193  {
2194  if (!cstate->binary)
2195  CopySendString(cstate, cstate->null_print_client);
2196  else
2197  CopySendInt32(cstate, -1);
2198  }
2199  else
2200  {
2201  if (!cstate->binary)
2202  {
2203  string = OutputFunctionCall(&out_functions[attnum - 1],
2204  value);
2205  if (cstate->csv_mode)
2206  CopyAttributeOutCSV(cstate, string,
2207  cstate->force_quote_flags[attnum - 1],
2208  list_length(cstate->attnumlist) == 1);
2209  else
2210  CopyAttributeOutText(cstate, string);
2211  }
2212  else
2213  {
2214  bytea *outputbytes;
2215 
2216  outputbytes = SendFunctionCall(&out_functions[attnum - 1],
2217  value);
2218  CopySendInt32(cstate, VARSIZE(outputbytes) - VARHDRSZ);
2219  CopySendData(cstate, VARDATA(outputbytes),
2220  VARSIZE(outputbytes) - VARHDRSZ);
2221  }
2222  }
2223  }
2224 
2225  CopySendEndOfRow(cstate);
2226 
2227  MemoryContextSwitchTo(oldcontext);
2228 }
2229 
2230 
2231 /*
2232  * error context callback for COPY FROM
2233  *
2234  * The argument for the error context must be CopyState.
2235  */
2236 void
2238 {
2239  CopyState cstate = (CopyState) arg;
2240  char curlineno_str[32];
2241 
2242  snprintf(curlineno_str, sizeof(curlineno_str), UINT64_FORMAT,
2243  cstate->cur_lineno);
2244 
2245  if (cstate->binary)
2246  {
2247  /* can't usefully display the data */
2248  if (cstate->cur_attname)
2249  errcontext("COPY %s, line %s, column %s",
2250  cstate->cur_relname, curlineno_str,
2251  cstate->cur_attname);
2252  else
2253  errcontext("COPY %s, line %s",
2254  cstate->cur_relname, curlineno_str);
2255  }
2256  else
2257  {
2258  if (cstate->cur_attname && cstate->cur_attval)
2259  {
2260  /* error is relevant to a particular column */
2261  char *attval;
2262 
2263  attval = limit_printout_length(cstate->cur_attval);
2264  errcontext("COPY %s, line %s, column %s: \"%s\"",
2265  cstate->cur_relname, curlineno_str,
2266  cstate->cur_attname, attval);
2267  pfree(attval);
2268  }
2269  else if (cstate->cur_attname)
2270  {
2271  /* error is relevant to a particular column, value is NULL */
2272  errcontext("COPY %s, line %s, column %s: null input",
2273  cstate->cur_relname, curlineno_str,
2274  cstate->cur_attname);
2275  }
2276  else
2277  {
2278  /*
2279  * Error is relevant to a particular line.
2280  *
2281  * If line_buf still contains the correct line, and it's already
2282  * transcoded, print it. If it's still in a foreign encoding, it's
2283  * quite likely that the error is precisely a failure to do
2284  * encoding conversion (ie, bad data). We dare not try to convert
2285  * it, and at present there's no way to regurgitate it without
2286  * conversion. So we have to punt and just report the line number.
2287  */
2288  if (cstate->line_buf_valid &&
2289  (cstate->line_buf_converted || !cstate->need_transcoding))
2290  {
2291  char *lineval;
2292 
2293  lineval = limit_printout_length(cstate->line_buf.data);
2294  errcontext("COPY %s, line %s: \"%s\"",
2295  cstate->cur_relname, curlineno_str, lineval);
2296  pfree(lineval);
2297  }
2298  else
2299  {
2300  errcontext("COPY %s, line %s",
2301  cstate->cur_relname, curlineno_str);
2302  }
2303  }
2304  }
2305 }
2306 
2307 /*
2308  * Make sure we don't print an unreasonable amount of COPY data in a message.
2309  *
2310  * It would seem a lot easier to just use the sprintf "precision" limit to
2311  * truncate the string. However, some versions of glibc have a bug/misfeature
2312  * that vsnprintf will always fail (return -1) if it is asked to truncate
2313  * a string that contains invalid byte sequences for the current encoding.
2314  * So, do our own truncation. We return a pstrdup'd copy of the input.
2315  */
2316 static char *
2318 {
2319 #define MAX_COPY_DATA_DISPLAY 100
2320 
2321  int slen = strlen(str);
2322  int len;
2323  char *res;
2324 
2325  /* Fast path if definitely okay */
2326  if (slen <= MAX_COPY_DATA_DISPLAY)
2327  return pstrdup(str);
2328 
2329  /* Apply encoding-dependent truncation */
2330  len = pg_mbcliplen(str, slen, MAX_COPY_DATA_DISPLAY);
2331 
2332  /*
2333  * Truncate, and add "..." to show we truncated the input.
2334  */
2335  res = (char *) palloc(len + 4);
2336  memcpy(res, str, len);
2337  strcpy(res + len, "...");
2338 
2339  return res;
2340 }
2341 
2342 /*
2343  * Allocate memory and initialize a new CopyMultiInsertBuffer for this
2344  * ResultRelInfo.
2345  */
2346 static CopyMultiInsertBuffer *
2348 {
2349  CopyMultiInsertBuffer *buffer;
2350 
2351  buffer = (CopyMultiInsertBuffer *) palloc(sizeof(CopyMultiInsertBuffer));
2352  memset(buffer->slots, 0, sizeof(TupleTableSlot *) * MAX_BUFFERED_TUPLES);
2353  buffer->resultRelInfo = rri;
2354  buffer->bistate = GetBulkInsertState();
2355  buffer->nused = 0;
2356 
2357  return buffer;
2358 }
2359 
2360 /*
2361  * Make a new buffer for this ResultRelInfo.
2362  */
2363 static inline void
2365  ResultRelInfo *rri)
2366 {
2367  CopyMultiInsertBuffer *buffer;
2368 
2369  buffer = CopyMultiInsertBufferInit(rri);
2370 
2371  /* Setup back-link so we can easily find this buffer again */
2372  rri->ri_CopyMultiInsertBuffer = buffer;
2373  /* Record that we're tracking this buffer */
2374  miinfo->multiInsertBuffers = lappend(miinfo->multiInsertBuffers, buffer);
2375 }
2376 
2377 /*
2378  * Initialize an already allocated CopyMultiInsertInfo.
2379  *
2380  * If rri is a non-partitioned table then a CopyMultiInsertBuffer is set up
2381  * for that table.
2382  */
2383 static void
2385  CopyState cstate, EState *estate, CommandId mycid,
2386  int ti_options)
2387 {
2388  miinfo->multiInsertBuffers = NIL;
2389  miinfo->bufferedTuples = 0;
2390  miinfo->bufferedBytes = 0;
2391  miinfo->cstate = cstate;
2392  miinfo->estate = estate;
2393  miinfo->mycid = mycid;
2394  miinfo->ti_options = ti_options;
2395 
2396  /*
2397  * Only setup the buffer when not dealing with a partitioned table.
2398  * Buffers for partitioned tables will just be setup when we need to send
2399  * tuples their way for the first time.
2400  */
2401  if (rri->ri_RelationDesc->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
2402  CopyMultiInsertInfoSetupBuffer(miinfo, rri);
2403 }
2404 
2405 /*
2406  * Returns true if the buffers are full
2407  */
2408 static inline bool
2410 {
2411  if (miinfo->bufferedTuples >= MAX_BUFFERED_TUPLES ||
2412  miinfo->bufferedBytes >= MAX_BUFFERED_BYTES)
2413  return true;
2414  return false;
2415 }
2416 
2417 /*
2418  * Returns true if we have no buffered tuples
2419  */
2420 static inline bool
2422 {
2423  return miinfo->bufferedTuples == 0;
2424 }
2425 
2426 /*
2427  * Write the tuples stored in 'buffer' out to the table.
2428  */
2429 static inline void
2431  CopyMultiInsertBuffer *buffer)
2432 {
2433  MemoryContext oldcontext;
2434  int i;
2435  uint64 save_cur_lineno;
2436  CopyState cstate = miinfo->cstate;
2437  EState *estate = miinfo->estate;
2438  CommandId mycid = miinfo->mycid;
2439  int ti_options = miinfo->ti_options;
2440  bool line_buf_valid = cstate->line_buf_valid;
2441  int nused = buffer->nused;
2442  ResultRelInfo *resultRelInfo = buffer->resultRelInfo;
2443  TupleTableSlot **slots = buffer->slots;
2444 
2445  /* Set es_result_relation_info to the ResultRelInfo we're flushing. */
2446  estate->es_result_relation_info = resultRelInfo;
2447 
2448  /*
2449  * Print error context information correctly, if one of the operations
2450  * below fail.
2451  */
2452  cstate->line_buf_valid = false;
2453  save_cur_lineno = cstate->cur_lineno;
2454 
2455  /*
2456  * table_multi_insert may leak memory, so switch to short-lived memory
2457  * context before calling it.
2458  */
2459  oldcontext = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
2460  table_multi_insert(resultRelInfo->ri_RelationDesc,
2461  slots,
2462  nused,
2463  mycid,
2464  ti_options,
2465  buffer->bistate);
2466  MemoryContextSwitchTo(oldcontext);
2467 
2468  for (i = 0; i < nused; i++)
2469  {
2470  /*
2471  * If there are any indexes, update them for all the inserted tuples,
2472  * and run AFTER ROW INSERT triggers.
2473  */
2474  if (resultRelInfo->ri_NumIndices > 0)
2475  {
2476  List *recheckIndexes;
2477 
2478  cstate->cur_lineno = buffer->linenos[i];
2479  recheckIndexes =
2480  ExecInsertIndexTuples(buffer->slots[i], estate, false, NULL,
2481  NIL);
2482  ExecARInsertTriggers(estate, resultRelInfo,
2483  slots[i], recheckIndexes,
2484  cstate->transition_capture);
2485  list_free(recheckIndexes);
2486  }
2487 
2488  /*
2489  * There's no indexes, but see if we need to run AFTER ROW INSERT
2490  * triggers anyway.
2491  */
2492  else if (resultRelInfo->ri_TrigDesc != NULL &&
2493  (resultRelInfo->ri_TrigDesc->trig_insert_after_row ||
2494  resultRelInfo->ri_TrigDesc->trig_insert_new_table))
2495  {
2496  cstate->cur_lineno = buffer->linenos[i];
2497  ExecARInsertTriggers(estate, resultRelInfo,
2498  slots[i], NIL, cstate->transition_capture);
2499  }
2500 
2501  ExecClearTuple(slots[i]);
2502  }
2503 
2504  /* Mark that all slots are free */
2505  buffer->nused = 0;
2506 
2507  /* reset cur_lineno and line_buf_valid to what they were */
2508  cstate->line_buf_valid = line_buf_valid;
2509  cstate->cur_lineno = save_cur_lineno;
2510 }
2511 
2512 /*
2513  * Drop used slots and free member for this buffer.
2514  *
2515  * The buffer must be flushed before cleanup.
2516  */
2517 static inline void
2519  CopyMultiInsertBuffer *buffer)
2520 {
2521  int i;
2522 
2523  /* Ensure buffer was flushed */
2524  Assert(buffer->nused == 0);
2525 
2526  /* Remove back-link to ourself */
2527  buffer->resultRelInfo->ri_CopyMultiInsertBuffer = NULL;
2528 
2529  FreeBulkInsertState(buffer->bistate);
2530 
2531  /* Since we only create slots on demand, just drop the non-null ones. */
2532  for (i = 0; i < MAX_BUFFERED_TUPLES && buffer->slots[i] != NULL; i++)
2533  ExecDropSingleTupleTableSlot(buffer->slots[i]);
2534 
2536  miinfo->ti_options);
2537 
2538  pfree(buffer);
2539 }
2540 
2541 /*
2542  * Write out all stored tuples in all buffers out to the tables.
2543  *
2544  * Once flushed we also trim the tracked buffers list down to size by removing
2545  * the buffers created earliest first.
2546  *
2547  * Callers should pass 'curr_rri' is the ResultRelInfo that's currently being
2548  * used. When cleaning up old buffers we'll never remove the one for
2549  * 'curr_rri'.
2550  */
2551 static inline void
2553 {
2554  ListCell *lc;
2555 
2556  foreach(lc, miinfo->multiInsertBuffers)
2557  {
2559 
2560  CopyMultiInsertBufferFlush(miinfo, buffer);
2561  }
2562 
2563  miinfo->bufferedTuples = 0;
2564  miinfo->bufferedBytes = 0;
2565 
2566  /*
2567  * Trim the list of tracked buffers down if it exceeds the limit. Here we
2568  * remove buffers starting with the ones we created first. It seems more
2569  * likely that these older ones are less likely to be needed than ones
2570  * that were just created.
2571  */
2573  {
2574  CopyMultiInsertBuffer *buffer;
2575 
2576  buffer = (CopyMultiInsertBuffer *) linitial(miinfo->multiInsertBuffers);
2577 
2578  /*
2579  * We never want to remove the buffer that's currently being used, so
2580  * if we happen to find that then move it to the end of the list.
2581  */
2582  if (buffer->resultRelInfo == curr_rri)
2583  {
2585  miinfo->multiInsertBuffers = lappend(miinfo->multiInsertBuffers, buffer);
2586  buffer = (CopyMultiInsertBuffer *) linitial(miinfo->multiInsertBuffers);
2587  }
2588 
2589  CopyMultiInsertBufferCleanup(miinfo, buffer);
2591  }
2592 }
2593 
2594 /*
2595  * Cleanup allocated buffers and free memory
2596  */
2597 static inline void
2599 {
2600  ListCell *lc;
2601 
2602  foreach(lc, miinfo->multiInsertBuffers)
2603  CopyMultiInsertBufferCleanup(miinfo, lfirst(lc));
2604 
2605  list_free(miinfo->multiInsertBuffers);
2606 }
2607 
2608 /*
2609  * Get the next TupleTableSlot that the next tuple should be stored in.
2610  *
2611  * Callers must ensure that the buffer is not full.
2612  */
2613 static inline TupleTableSlot *
2615  ResultRelInfo *rri)
2616 {
2618  int nused = buffer->nused;
2619 
2620  Assert(buffer != NULL);
2621  Assert(nused < MAX_BUFFERED_TUPLES);
2622 
2623  if (buffer->slots[nused] == NULL)
2624  buffer->slots[nused] = table_slot_create(rri->ri_RelationDesc, NULL);
2625  return buffer->slots[nused];
2626 }
2627 
2628 /*
2629  * Record the previously reserved TupleTableSlot that was reserved by
2630  * CopyMultiInsertInfoNextFreeSlot as being consumed.
2631  */
2632 static inline void
2634  TupleTableSlot *slot, int tuplen, uint64 lineno)
2635 {
2637 
2638  Assert(buffer != NULL);
2639  Assert(slot == buffer->slots[buffer->nused]);
2640 
2641  /* Store the line number so we can properly report any errors later */
2642  buffer->linenos[buffer->nused] = lineno;
2643 
2644  /* Record this slot as being used */
2645  buffer->nused++;
2646 
2647  /* Update how many tuples are stored and their size */
2648  miinfo->bufferedTuples++;
2649  miinfo->bufferedBytes += tuplen;
2650 }
2651 
2652 /*
2653  * Copy FROM file to relation.
2654  */
2655 uint64
2657 {
2658  ResultRelInfo *resultRelInfo;
2659  ResultRelInfo *target_resultRelInfo;
2660  ResultRelInfo *prevResultRelInfo = NULL;
2661  EState *estate = CreateExecutorState(); /* for ExecConstraints() */
2662  ModifyTableState *mtstate;
2663  ExprContext *econtext;
2664  TupleTableSlot *singleslot = NULL;
2665  MemoryContext oldcontext = CurrentMemoryContext;
2666 
2667  PartitionTupleRouting *proute = NULL;
2668  ErrorContextCallback errcallback;
2669  CommandId mycid = GetCurrentCommandId(true);
2670  int ti_options = 0; /* start with default options for insert */
2671  BulkInsertState bistate = NULL;
2672  CopyInsertMethod insertMethod;
2673  CopyMultiInsertInfo multiInsertInfo = {0}; /* pacify compiler */
2674  uint64 processed = 0;
2675  bool has_before_insert_row_trig;
2676  bool has_instead_insert_row_trig;
2677  bool leafpart_use_multi_insert = false;
2678 
2679  Assert(cstate->rel);
2680 
2681  /*
2682  * The target must be a plain, foreign, or partitioned relation, or have
2683  * an INSTEAD OF INSERT row trigger. (Currently, such triggers are only
2684  * allowed on views, so we only hint about them in the view case.)
2685  */
2686  if (cstate->rel->rd_rel->relkind != RELKIND_RELATION &&
2687  cstate->rel->rd_rel->relkind != RELKIND_FOREIGN_TABLE &&
2688  cstate->rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE &&
2689  !(cstate->rel->trigdesc &&
2691  {
2692  if (cstate->rel->rd_rel->relkind == RELKIND_VIEW)
2693  ereport(ERROR,
2694  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2695  errmsg("cannot copy to view \"%s\"",
2696  RelationGetRelationName(cstate->rel)),
2697  errhint("To enable copying to a view, provide an INSTEAD OF INSERT trigger.")));
2698  else if (cstate->rel->rd_rel->relkind == RELKIND_MATVIEW)
2699  ereport(ERROR,
2700  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2701  errmsg("cannot copy to materialized view \"%s\"",
2702  RelationGetRelationName(cstate->rel))));
2703  else if (cstate->rel->rd_rel->relkind == RELKIND_SEQUENCE)
2704  ereport(ERROR,
2705  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2706  errmsg("cannot copy to sequence \"%s\"",
2707  RelationGetRelationName(cstate->rel))));
2708  else
2709  ereport(ERROR,
2710  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2711  errmsg("cannot copy to non-table relation \"%s\"",
2712  RelationGetRelationName(cstate->rel))));
2713  }
2714 
2715  /*----------
2716  * Check to see if we can avoid writing WAL
2717  *
2718  * If archive logging/streaming is not enabled *and* either
2719  * - table was created in same transaction as this COPY
2720  * - data is being written to relfilenode created in this transaction
2721  * then we can skip writing WAL. It's safe because if the transaction
2722  * doesn't commit, we'll discard the table (or the new relfilenode file).
2723  * If it does commit, we'll have done the table_finish_bulk_insert() at
2724  * the bottom of this routine first.
2725  *
2726  * As mentioned in comments in utils/rel.h, the in-same-transaction test
2727  * is not always set correctly, since in rare cases rd_newRelfilenodeSubid
2728  * can be cleared before the end of the transaction. The exact case is
2729  * when a relation sets a new relfilenode twice in same transaction, yet
2730  * the second one fails in an aborted subtransaction, e.g.
2731  *
2732  * BEGIN;
2733  * TRUNCATE t;
2734  * SAVEPOINT save;
2735  * TRUNCATE t;
2736  * ROLLBACK TO save;
2737  * COPY ...
2738  *
2739  * Also, if the target file is new-in-transaction, we assume that checking
2740  * FSM for free space is a waste of time, even if we must use WAL because
2741  * of archiving. This could possibly be wrong, but it's unlikely.
2742  *
2743  * The comments for table_tuple_insert and RelationGetBufferForTuple
2744  * specify that skipping WAL logging is only safe if we ensure that our
2745  * tuples do not go into pages containing tuples from any other
2746  * transactions --- but this must be the case if we have a new table or
2747  * new relfilenode, so we need no additional work to enforce that.
2748  *
2749  * We currently don't support this optimization if the COPY target is a
2750  * partitioned table as we currently only lazily initialize partition
2751  * information when routing the first tuple to the partition. We cannot
2752  * know at this stage if we can perform this optimization. It should be
2753  * possible to improve on this, but it does mean maintaining heap insert
2754  * option flags per partition and setting them when we first open the
2755  * partition.
2756  *
2757  * This optimization is not supported for relation types which do not
2758  * have any physical storage, with foreign tables and views using
2759  * INSTEAD OF triggers entering in this category. Partitioned tables
2760  * are not supported as per the description above.
2761  *----------
2762  */
2763  /* createSubid is creation check, newRelfilenodeSubid is truncation check */
2764  if (RELKIND_HAS_STORAGE(cstate->rel->rd_rel->relkind) &&
2767  {
2768  ti_options |= TABLE_INSERT_SKIP_FSM;
2769  if (!XLogIsNeeded())
2770  ti_options |= TABLE_INSERT_SKIP_WAL;
2771  }
2772 
2773  /*
2774  * Optimize if new relfilenode was created in this subxact or one of its
2775  * committed children and we won't see those rows later as part of an
2776  * earlier scan or command. The subxact test ensures that if this subxact
2777  * aborts then the frozen rows won't be visible after xact cleanup. Note
2778  * that the stronger test of exactly which subtransaction created it is
2779  * crucial for correctness of this optimization. The test for an earlier
2780  * scan or command tolerates false negatives. FREEZE causes other sessions
2781  * to see rows they would not see under MVCC, and a false negative merely
2782  * spreads that anomaly to the current session.
2783  */
2784  if (cstate->freeze)
2785  {
2786  /*
2787  * We currently disallow COPY FREEZE on partitioned tables. The
2788  * reason for this is that we've simply not yet opened the partitions
2789  * to determine if the optimization can be applied to them. We could
2790  * go and open them all here, but doing so may be quite a costly
2791  * overhead for small copies. In any case, we may just end up routing
2792  * tuples to a small number of partitions. It seems better just to
2793  * raise an ERROR for partitioned tables.
2794  */
2795  if (cstate->rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
2796  {
2797  ereport(ERROR,
2798  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2799  errmsg("cannot perform COPY FREEZE on a partitioned table")));
2800  }
2801 
2802  /*
2803  * Tolerate one registration for the benefit of FirstXactSnapshot.
2804  * Scan-bearing queries generally create at least two registrations,
2805  * though relying on that is fragile, as is ignoring ActiveSnapshot.
2806  * Clear CatalogSnapshot to avoid counting its registration. We'll
2807  * still detect ongoing catalog scans, each of which separately
2808  * registers the snapshot it uses.
2809  */
2812  ereport(ERROR,
2813  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
2814  errmsg("cannot perform COPY FREEZE because of prior transaction activity")));
2815 
2816  if (cstate->rel->rd_createSubid != GetCurrentSubTransactionId() &&
2818  ereport(ERROR,
2819  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2820  errmsg("cannot perform COPY FREEZE because the table was not created or truncated in the current subtransaction")));
2821 
2822  ti_options |= TABLE_INSERT_FROZEN;
2823  }
2824 
2825  /*
2826  * We need a ResultRelInfo so we can use the regular executor's
2827  * index-entry-making machinery. (There used to be a huge amount of code
2828  * here that basically duplicated execUtils.c ...)
2829  */
2830  resultRelInfo = makeNode(ResultRelInfo);
2831  InitResultRelInfo(resultRelInfo,
2832  cstate->rel,
2833  1, /* must match rel's position in range_table */
2834  NULL,
2835  0);
2836  target_resultRelInfo = resultRelInfo;
2837 
2838  /* Verify the named relation is a valid target for INSERT */
2839  CheckValidResultRel(resultRelInfo, CMD_INSERT);
2840 
2841  ExecOpenIndices(resultRelInfo, false);
2842 
2843  estate->es_result_relations = resultRelInfo;
2844  estate->es_num_result_relations = 1;
2845  estate->es_result_relation_info = resultRelInfo;
2846 
2847  ExecInitRangeTable(estate, cstate->range_table);
2848 
2849  /*
2850  * Set up a ModifyTableState so we can let FDW(s) init themselves for
2851  * foreign-table result relation(s).
2852  */
2853  mtstate = makeNode(ModifyTableState);
2854  mtstate->ps.plan = NULL;
2855  mtstate->ps.state = estate;
2856  mtstate->operation = CMD_INSERT;
2857  mtstate->resultRelInfo = estate->es_result_relations;
2858 
2859  if (resultRelInfo->ri_FdwRoutine != NULL &&
2860  resultRelInfo->ri_FdwRoutine->BeginForeignInsert != NULL)
2861  resultRelInfo->ri_FdwRoutine->BeginForeignInsert(mtstate,
2862  resultRelInfo);
2863 
2864  /* Prepare to catch AFTER triggers. */
2866 
2867  /*
2868  * If there are any triggers with transition tables on the named relation,
2869  * we need to be prepared to capture transition tuples.
2870  *
2871  * Because partition tuple routing would like to know about whether
2872  * transition capture is active, we also set it in mtstate, which is
2873  * passed to ExecFindPartition() below.
2874  */
2875  cstate->transition_capture = mtstate->mt_transition_capture =
2877  RelationGetRelid(cstate->rel),
2878  CMD_INSERT);
2879 
2880  /*
2881  * If the named relation is a partitioned table, initialize state for
2882  * CopyFrom tuple routing.
2883  */
2884  if (cstate->rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
2885  proute = ExecSetupPartitionTupleRouting(estate, NULL, cstate->rel);
2886 
2887  if (cstate->whereClause)
2888  cstate->qualexpr = ExecInitQual(castNode(List, cstate->whereClause),
2889  &mtstate->ps);
2890 
2891  /*
2892  * It's generally more efficient to prepare a bunch of tuples for
2893  * insertion, and insert them in one table_multi_insert() call, than call
2894  * table_tuple_insert() separately for every tuple. However, there are a
2895  * number of reasons why we might not be able to do this. These are
2896  * explained below.
2897  */
2898  if (resultRelInfo->ri_TrigDesc != NULL &&
2899  (resultRelInfo->ri_TrigDesc->trig_insert_before_row ||
2900  resultRelInfo->ri_TrigDesc->trig_insert_instead_row))
2901  {
2902  /*
2903  * Can't support multi-inserts when there are any BEFORE/INSTEAD OF
2904  * triggers on the table. Such triggers might query the table we're
2905  * inserting into and act differently if the tuples that have already
2906  * been processed and prepared for insertion are not there.
2907  */
2908  insertMethod = CIM_SINGLE;
2909  }
2910  else if (proute != NULL && resultRelInfo->ri_TrigDesc != NULL &&
2911  resultRelInfo->ri_TrigDesc->trig_insert_new_table)
2912  {
2913  /*
2914  * For partitioned tables we can't support multi-inserts when there
2915  * are any statement level insert triggers. It might be possible to
2916  * allow partitioned tables with such triggers in the future, but for
2917  * now, CopyMultiInsertInfoFlush expects that any before row insert
2918  * and statement level insert triggers are on the same relation.
2919  */
2920  insertMethod = CIM_SINGLE;
2921  }
2922  else if (resultRelInfo->ri_FdwRoutine != NULL ||
2923  cstate->volatile_defexprs)
2924  {
2925  /*
2926  * Can't support multi-inserts to foreign tables or if there are any
2927  * volatile default expressions in the table. Similarly to the
2928  * trigger case above, such expressions may query the table we're
2929  * inserting into.
2930  *
2931  * Note: It does not matter if any partitions have any volatile
2932  * default expressions as we use the defaults from the target of the
2933  * COPY command.
2934  */
2935  insertMethod = CIM_SINGLE;
2936  }
2937  else if (contain_volatile_functions(cstate->whereClause))
2938  {
2939  /*
2940  * Can't support multi-inserts if there are any volatile function
2941  * expressions in WHERE clause. Similarly to the trigger case above,
2942  * such expressions may query the table we're inserting into.
2943  */
2944  insertMethod = CIM_SINGLE;
2945  }
2946  else
2947  {
2948  /*
2949  * For partitioned tables, we may still be able to perform bulk
2950  * inserts. However, the possibility of this depends on which types
2951  * of triggers exist on the partition. We must disable bulk inserts
2952  * if the partition is a foreign table or it has any before row insert
2953  * or insert instead triggers (same as we checked above for the parent
2954  * table). Since the partition's resultRelInfos are initialized only
2955  * when we actually need to insert the first tuple into them, we must
2956  * have the intermediate insert method of CIM_MULTI_CONDITIONAL to
2957  * flag that we must later determine if we can use bulk-inserts for
2958  * the partition being inserted into.
2959  */
2960  if (proute)
2961  insertMethod = CIM_MULTI_CONDITIONAL;
2962  else
2963  insertMethod = CIM_MULTI;
2964 
2965  CopyMultiInsertInfoInit(&multiInsertInfo, resultRelInfo, cstate,
2966  estate, mycid, ti_options);
2967  }
2968 
2969  /*
2970  * If not using batch mode (which allocates slots as needed) set up a
2971  * tuple slot too. When inserting into a partitioned table, we also need
2972  * one, even if we might batch insert, to read the tuple in the root
2973  * partition's form.
2974  */
2975  if (insertMethod == CIM_SINGLE || insertMethod == CIM_MULTI_CONDITIONAL)
2976  {
2977  singleslot = table_slot_create(resultRelInfo->ri_RelationDesc,
2978  &estate->es_tupleTable);
2979  bistate = GetBulkInsertState();
2980  }
2981 
2982  has_before_insert_row_trig = (resultRelInfo->ri_TrigDesc &&
2983  resultRelInfo->ri_TrigDesc->trig_insert_before_row);
2984 
2985  has_instead_insert_row_trig = (resultRelInfo->ri_TrigDesc &&
2986  resultRelInfo->ri_TrigDesc->trig_insert_instead_row);
2987 
2988  /*
2989  * Check BEFORE STATEMENT insertion triggers. It's debatable whether we
2990  * should do this for COPY, since it's not really an "INSERT" statement as
2991  * such. However, executing these triggers maintains consistency with the
2992  * EACH ROW triggers that we already fire on COPY.
2993  */
2994  ExecBSInsertTriggers(estate, resultRelInfo);
2995 
2996  econtext = GetPerTupleExprContext(estate);
2997 
2998  /* Set up callback to identify error line number */
2999  errcallback.callback = CopyFromErrorCallback;
3000  errcallback.arg = (void *) cstate;
3001  errcallback.previous = error_context_stack;
3002  error_context_stack = &errcallback;
3003 
3004  for (;;)
3005  {
3006  TupleTableSlot *myslot;
3007  bool skip_tuple;
3008 
3010 
3011  /*
3012  * Reset the per-tuple exprcontext. We do this after every tuple, to
3013  * clean-up after expression evaluations etc.
3014  */
3015  ResetPerTupleExprContext(estate);
3016 
3017  /* select slot to (initially) load row into */
3018  if (insertMethod == CIM_SINGLE || proute)
3019  {
3020  myslot = singleslot;
3021  Assert(myslot != NULL);
3022  }
3023  else
3024  {
3025  Assert(resultRelInfo == target_resultRelInfo);
3026  Assert(insertMethod == CIM_MULTI);
3027 
3028  myslot = CopyMultiInsertInfoNextFreeSlot(&multiInsertInfo,
3029  resultRelInfo);
3030  }
3031 
3032  /*
3033  * Switch to per-tuple context before calling NextCopyFrom, which does
3034  * evaluate default expressions etc. and requires per-tuple context.
3035  */
3037 
3038  ExecClearTuple(myslot);
3039 
3040  /* Directly store the values/nulls array in the slot */
3041  if (!NextCopyFrom(cstate, econtext, myslot->tts_values, myslot->tts_isnull))
3042  break;
3043 
3044  ExecStoreVirtualTuple(myslot);
3045 
3046  /*
3047  * Constraints and where clause might reference the tableoid column,
3048  * so (re-)initialize tts_tableOid before evaluating them.
3049  */
3050  myslot->tts_tableOid = RelationGetRelid(target_resultRelInfo->ri_RelationDesc);
3051 
3052  /* Triggers and stuff need to be invoked in query context. */
3053  MemoryContextSwitchTo(oldcontext);
3054 
3055  if (cstate->whereClause)
3056  {
3057  econtext->ecxt_scantuple = myslot;
3058  /* Skip items that don't match COPY's WHERE clause */
3059  if (!ExecQual(cstate->qualexpr, econtext))
3060  continue;
3061  }
3062 
3063  /* Determine the partition to insert the tuple into */
3064  if (proute)
3065  {
3066  TupleConversionMap *map;
3067 
3068  /*
3069  * Attempt to find a partition suitable for this tuple.
3070  * ExecFindPartition() will raise an error if none can be found or
3071  * if the found partition is not suitable for INSERTs.
3072  */
3073  resultRelInfo = ExecFindPartition(mtstate, target_resultRelInfo,
3074  proute, myslot, estate);
3075 
3076  if (prevResultRelInfo != resultRelInfo)
3077  {
3078  /* Determine which triggers exist on this partition */
3079  has_before_insert_row_trig = (resultRelInfo->ri_TrigDesc &&
3080  resultRelInfo->ri_TrigDesc->trig_insert_before_row);
3081 
3082  has_instead_insert_row_trig = (resultRelInfo->ri_TrigDesc &&
3083  resultRelInfo->ri_TrigDesc->trig_insert_instead_row);
3084 
3085  /*
3086  * Disable multi-inserts when the partition has BEFORE/INSTEAD
3087  * OF triggers, or if the partition is a foreign partition.
3088  */
3089  leafpart_use_multi_insert = insertMethod == CIM_MULTI_CONDITIONAL &&
3090  !has_before_insert_row_trig &&
3091  !has_instead_insert_row_trig &&
3092  resultRelInfo->ri_FdwRoutine == NULL;
3093 
3094  /* Set the multi-insert buffer to use for this partition. */
3095  if (leafpart_use_multi_insert)
3096  {
3097  if (resultRelInfo->ri_CopyMultiInsertBuffer == NULL)
3098  CopyMultiInsertInfoSetupBuffer(&multiInsertInfo,
3099  resultRelInfo);
3100  }
3101  else if (insertMethod == CIM_MULTI_CONDITIONAL &&
3102  !CopyMultiInsertInfoIsEmpty(&multiInsertInfo))
3103  {
3104  /*
3105  * Flush pending inserts if this partition can't use
3106  * batching, so rows are visible to triggers etc.
3107  */
3108  CopyMultiInsertInfoFlush(&multiInsertInfo, resultRelInfo);
3109  }
3110 
3111  if (bistate != NULL)
3112  ReleaseBulkInsertStatePin(bistate);
3113  prevResultRelInfo = resultRelInfo;
3114  }
3115 
3116  /*
3117  * For ExecInsertIndexTuples() to work on the partition's indexes
3118  */
3119  estate->es_result_relation_info = resultRelInfo;
3120 
3121  /*
3122  * If we're capturing transition tuples, we might need to convert
3123  * from the partition rowtype to root rowtype.
3124  */
3125  if (cstate->transition_capture != NULL)
3126  {
3127  if (has_before_insert_row_trig)
3128  {
3129  /*
3130  * If there are any BEFORE triggers on the partition,
3131  * we'll have to be ready to convert their result back to
3132  * tuplestore format.
3133  */
3135  cstate->transition_capture->tcs_map =
3136  resultRelInfo->ri_PartitionInfo->pi_PartitionToRootMap;
3137  }
3138  else
3139  {
3140  /*
3141  * Otherwise, just remember the original unconverted
3142  * tuple, to avoid a needless round trip conversion.
3143  */
3145  cstate->transition_capture->tcs_map = NULL;
3146  }
3147  }
3148 
3149  /*
3150  * We might need to convert from the root rowtype to the partition
3151  * rowtype.
3152  */
3153  map = resultRelInfo->ri_PartitionInfo->pi_RootToPartitionMap;
3154  if (insertMethod == CIM_SINGLE || !leafpart_use_multi_insert)
3155  {
3156  /* non batch insert */
3157  if (map != NULL)
3158  {
3159  TupleTableSlot *new_slot;
3160 
3161  new_slot = resultRelInfo->ri_PartitionInfo->pi_PartitionTupleSlot;
3162  myslot = execute_attr_map_slot(map->attrMap, myslot, new_slot);
3163  }
3164  }
3165  else
3166  {
3167  /*
3168  * Prepare to queue up tuple for later batch insert into
3169  * current partition.
3170  */
3171  TupleTableSlot *batchslot;
3172 
3173  /* no other path available for partitioned table */
3174  Assert(insertMethod == CIM_MULTI_CONDITIONAL);
3175 
3176  batchslot = CopyMultiInsertInfoNextFreeSlot(&multiInsertInfo,
3177  resultRelInfo);
3178 
3179  if (map != NULL)
3180  myslot = execute_attr_map_slot(map->attrMap, myslot,
3181  batchslot);
3182  else
3183  {
3184  /*
3185  * This looks more expensive than it is (Believe me, I
3186  * optimized it away. Twice.). The input is in virtual
3187  * form, and we'll materialize the slot below - for most
3188  * slot types the copy performs the work materialization
3189  * would later require anyway.
3190  */
3191  ExecCopySlot(batchslot, myslot);
3192  myslot = batchslot;
3193  }
3194  }
3195 
3196  /* ensure that triggers etc see the right relation */
3197  myslot->tts_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
3198  }
3199 
3200  skip_tuple = false;
3201 
3202  /* BEFORE ROW INSERT Triggers */
3203  if (has_before_insert_row_trig)
3204  {
3205  if (!ExecBRInsertTriggers(estate, resultRelInfo, myslot))
3206  skip_tuple = true; /* "do nothing" */
3207  }
3208 
3209  if (!skip_tuple)
3210  {
3211  /*
3212  * If there is an INSTEAD OF INSERT ROW trigger, let it handle the
3213  * tuple. Otherwise, proceed with inserting the tuple into the
3214  * table or foreign table.
3215  */
3216  if (has_instead_insert_row_trig)
3217  {
3218  ExecIRInsertTriggers(estate, resultRelInfo, myslot);
3219  }
3220  else
3221  {
3222  /* Compute stored generated columns */
3223  if (resultRelInfo->ri_RelationDesc->rd_att->constr &&
3225  ExecComputeStoredGenerated(estate, myslot);
3226 
3227  /*
3228  * If the target is a plain table, check the constraints of
3229  * the tuple.
3230  */
3231  if (resultRelInfo->ri_FdwRoutine == NULL &&
3232  resultRelInfo->ri_RelationDesc->rd_att->constr)
3233  ExecConstraints(resultRelInfo, myslot, estate);
3234 
3235  /*
3236  * Also check the tuple against the partition constraint, if
3237  * there is one; except that if we got here via tuple-routing,
3238  * we don't need to if there's no BR trigger defined on the
3239  * partition.
3240  */
3241  if (resultRelInfo->ri_PartitionCheck &&
3242  (proute == NULL || has_before_insert_row_trig))
3243  ExecPartitionCheck(resultRelInfo, myslot, estate, true);
3244 
3245  /* Store the slot in the multi-insert buffer, when enabled. */
3246  if (insertMethod == CIM_MULTI || leafpart_use_multi_insert)
3247  {
3248  /*
3249  * The slot previously might point into the per-tuple
3250  * context. For batching it needs to be longer lived.
3251  */
3252  ExecMaterializeSlot(myslot);
3253 
3254  /* Add this tuple to the tuple buffer */
3255  CopyMultiInsertInfoStore(&multiInsertInfo,
3256  resultRelInfo, myslot,
3257  cstate->line_buf.len,
3258  cstate->cur_lineno);
3259 
3260  /*
3261  * If enough inserts have queued up, then flush all
3262  * buffers out to their tables.
3263  */
3264  if (CopyMultiInsertInfoIsFull(&multiInsertInfo))
3265  CopyMultiInsertInfoFlush(&multiInsertInfo, resultRelInfo);
3266  }
3267  else
3268  {
3269  List *recheckIndexes = NIL;
3270 
3271  /* OK, store the tuple */
3272  if (resultRelInfo->ri_FdwRoutine != NULL)
3273  {
3274  myslot = resultRelInfo->ri_FdwRoutine->ExecForeignInsert(estate,
3275  resultRelInfo,
3276  myslot,
3277  NULL);
3278 
3279  if (myslot == NULL) /* "do nothing" */
3280  continue; /* next tuple please */
3281 
3282  /*
3283  * AFTER ROW Triggers might reference the tableoid
3284  * column, so (re-)initialize tts_tableOid before
3285  * evaluating them.
3286  */
3287  myslot->tts_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
3288  }
3289  else
3290  {
3291  /* OK, store the tuple and create index entries for it */
3292  table_tuple_insert(resultRelInfo->ri_RelationDesc,
3293  myslot, mycid, ti_options, bistate);
3294 
3295  if (resultRelInfo->ri_NumIndices > 0)
3296  recheckIndexes = ExecInsertIndexTuples(myslot,
3297  estate,
3298  false,
3299  NULL,
3300  NIL);
3301  }
3302 
3303  /* AFTER ROW INSERT Triggers */
3304  ExecARInsertTriggers(estate, resultRelInfo, myslot,
3305  recheckIndexes, cstate->transition_capture);
3306 
3307  list_free(recheckIndexes);
3308  }
3309  }
3310 
3311  /*
3312  * We count only tuples not suppressed by a BEFORE INSERT trigger
3313  * or FDW; this is the same definition used by nodeModifyTable.c
3314  * for counting tuples inserted by an INSERT command.
3315  */
3316  processed++;
3317  }
3318  }
3319 
3320  /* Flush any remaining buffered tuples */
3321  if (insertMethod != CIM_SINGLE)
3322  {
3323  if (!CopyMultiInsertInfoIsEmpty(&multiInsertInfo))
3324  CopyMultiInsertInfoFlush(&multiInsertInfo, NULL);
3325  }
3326 
3327  /* Done, clean up */
3328  error_context_stack = errcallback.previous;
3329 
3330  if (bistate != NULL)
3331  FreeBulkInsertState(bistate);
3332 
3333  MemoryContextSwitchTo(oldcontext);
3334 
3335  /*
3336  * In the old protocol, tell pqcomm that we can process normal protocol
3337  * messages again.
3338  */
3339  if (cstate->copy_dest == COPY_OLD_FE)
3340  pq_endmsgread();
3341 
3342  /* Execute AFTER STATEMENT insertion triggers */
3343  ExecASInsertTriggers(estate, target_resultRelInfo, cstate->transition_capture);
3344 
3345  /* Handle queued AFTER triggers */
3346  AfterTriggerEndQuery(estate);
3347 
3348  ExecResetTupleTable(estate->es_tupleTable, false);
3349 
3350  /* Allow the FDW to shut down */
3351  if (target_resultRelInfo->ri_FdwRoutine != NULL &&
3352  target_resultRelInfo->ri_FdwRoutine->EndForeignInsert != NULL)
3353  target_resultRelInfo->ri_FdwRoutine->EndForeignInsert(estate,
3354  target_resultRelInfo);
3355 
3356  /* Tear down the multi-insert buffer data */
3357  if (insertMethod != CIM_SINGLE)
3358  CopyMultiInsertInfoCleanup(&multiInsertInfo);
3359 
3360  ExecCloseIndices(target_resultRelInfo);
3361 
3362  /* Close all the partitioned tables, leaf partitions, and their indices */
3363  if (proute)
3364  ExecCleanupTupleRouting(mtstate, proute);
3365 
3366  /* Close any trigger target relations */
3367  ExecCleanUpTriggerState(estate);
3368 
3369  FreeExecutorState(estate);
3370 
3371  return processed;
3372 }
3373 
3374 /*
3375  * Setup to read tuples from a file for COPY FROM.
3376  *
3377  * 'rel': Used as a template for the tuples
3378  * 'filename': Name of server-local file to read
3379  * 'attnamelist': List of char *, columns to include. NIL selects all cols.
3380  * 'options': List of DefElem. See copy_opt_item in gram.y for selections.
3381  *
3382  * Returns a CopyState, to be passed to NextCopyFrom and related functions.
3383  */
3384 CopyState
3386  Relation rel,
3387  const char *filename,
3388  bool is_program,
3390  List *attnamelist,
3391  List *options)
3392 {
3393  CopyState cstate;
3394  bool pipe = (filename == NULL);
3395  TupleDesc tupDesc;
3396  AttrNumber num_phys_attrs,
3397  num_defaults;
3399  Oid *typioparams;
3400  int attnum;
3401  Oid in_func_oid;
3402  int *defmap;
3403  ExprState **defexprs;
3404  MemoryContext oldcontext;
3405  bool volatile_defexprs;
3406 
3407  cstate = BeginCopy(pstate, true, rel, NULL, InvalidOid, attnamelist, options);
3408  oldcontext = MemoryContextSwitchTo(cstate->copycontext);
3409 
3410  /* Initialize state variables */
3411  cstate->reached_eof = false;
3412  cstate->eol_type = EOL_UNKNOWN;
3413  cstate->cur_relname = RelationGetRelationName(cstate->rel);
3414  cstate->cur_lineno = 0;
3415  cstate->cur_attname = NULL;
3416  cstate->cur_attval = NULL;
3417 
3418  /* Set up variables to avoid per-attribute overhead. */
3419  initStringInfo(&cstate->attribute_buf);
3420  initStringInfo(&cstate->line_buf);
3421  cstate->line_buf_converted = false;
3422  cstate->raw_buf = (char *) palloc(RAW_BUF_SIZE + 1);
3423  cstate->raw_buf_index = cstate->raw_buf_len = 0;
3424 
3425  /* Assign range table, we'll need it in CopyFrom. */
3426  if (pstate)
3427  cstate->range_table = pstate->p_rtable;
3428 
3429  tupDesc = RelationGetDescr(cstate->rel);
3430  num_phys_attrs = tupDesc->natts;
3431  num_defaults = 0;
3432  volatile_defexprs = false;
3433 
3434  /*
3435  * Pick up the required catalog information for each attribute in the
3436  * relation, including the input function, the element type (to pass to
3437  * the input function), and info about defaults and constraints. (Which
3438  * input function we use depends on text/binary format choice.)
3439  */
3440  in_functions = (FmgrInfo *) palloc(num_phys_attrs * sizeof(FmgrInfo));
3441  typioparams = (Oid *) palloc(num_phys_attrs * sizeof(Oid));
3442  defmap = (int *) palloc(num_phys_attrs * sizeof(int));
3443  defexprs = (ExprState **) palloc(num_phys_attrs * sizeof(ExprState *));
3444 
3445  for (attnum = 1; attnum <= num_phys_attrs; attnum++)
3446  {
3447  Form_pg_attribute att = TupleDescAttr(tupDesc, attnum - 1);
3448 
3449  /* We don't need info for dropped attributes */
3450  if (att->attisdropped)
3451  continue;
3452 
3453  /* Fetch the input function and typioparam info */
3454  if (cstate->binary)
3455  getTypeBinaryInputInfo(att->atttypid,
3456  &in_func_oid, &typioparams[attnum - 1]);
3457  else
3458  getTypeInputInfo(att->atttypid,
3459  &in_func_oid, &typioparams[attnum - 1]);
3460  fmgr_info(in_func_oid, &in_functions[attnum - 1]);
3461 
3462  /* Get default info if needed */
3463  if (!list_member_int(cstate->attnumlist, attnum) && !att->attgenerated)
3464  {
3465  /* attribute is NOT to be copied from input */
3466  /* use default value if one exists */
3467  Expr *defexpr = (Expr *) build_column_default(cstate->rel,
3468  attnum);
3469 
3470  if (defexpr != NULL)
3471  {
3472  /* Run the expression through planner */
3473  defexpr = expression_planner(defexpr);
3474 
3475  /* Initialize executable expression in copycontext */
3476  defexprs[num_defaults] = ExecInitExpr(defexpr, NULL);
3477  defmap[num_defaults] = attnum - 1;
3478  num_defaults++;
3479 
3480  /*
3481  * If a default expression looks at the table being loaded,
3482  * then it could give the wrong answer when using
3483  * multi-insert. Since database access can be dynamic this is
3484  * hard to test for exactly, so we use the much wider test of
3485  * whether the default expression is volatile. We allow for
3486  * the special case of when the default expression is the
3487  * nextval() of a sequence which in this specific case is
3488  * known to be safe for use with the multi-insert
3489  * optimization. Hence we use this special case function
3490  * checker rather than the standard check for
3491  * contain_volatile_functions().
3492  */
3493  if (!volatile_defexprs)
3494  volatile_defexprs = contain_volatile_functions_not_nextval((Node *) defexpr);
3495  }
3496  }
3497  }
3498 
3499  /* We keep those variables in cstate. */
3500  cstate->in_functions = in_functions;
3501  cstate->typioparams = typioparams;
3502  cstate->defmap = defmap;
3503  cstate->defexprs = defexprs;
3505  cstate->num_defaults = num_defaults;
3506  cstate->is_program = is_program;
3507 
3508  if (data_source_cb)
3509  {
3510  cstate->copy_dest = COPY_CALLBACK;
3511  cstate->data_source_cb = data_source_cb;
3512  }
3513  else if (pipe)
3514  {
3515  Assert(!is_program); /* the grammar does not allow this */
3517  ReceiveCopyBegin(cstate);
3518  else
3519  cstate->copy_file = stdin;
3520  }
3521  else
3522  {
3523  cstate->filename = pstrdup(filename);
3524 
3525  if (cstate->is_program)
3526  {
3527  cstate->copy_file = OpenPipeStream(cstate->filename, PG_BINARY_R);
3528  if (cstate->copy_file == NULL)
3529  ereport(ERROR,
3531  errmsg("could not execute command \"%s\": %m",
3532  cstate->filename)));
3533  }
3534  else
3535  {
3536  struct stat st;
3537 
3538  cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_R);
3539  if (cstate->copy_file == NULL)
3540  {
3541  /* copy errno because ereport subfunctions might change it */
3542  int save_errno = errno;
3543 
3544  ereport(ERROR,
3546  errmsg("could not open file \"%s\" for reading: %m",
3547  cstate->filename),
3548  (save_errno == ENOENT || save_errno == EACCES) ?
3549  errhint("COPY FROM instructs the PostgreSQL server process to read a file. "
3550  "You may want a client-side facility such as psql's \\copy.") : 0));
3551  }
3552 
3553  if (fstat(fileno(cstate->copy_file), &st))
3554  ereport(ERROR,
3556  errmsg("could not stat file \"%s\": %m",
3557  cstate->filename)));
3558 
3559  if (S_ISDIR(st.st_mode))
3560  ereport(ERROR,
3561  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3562  errmsg("\"%s\" is a directory", cstate->filename)));
3563  }
3564  }
3565 
3566  if (cstate->binary)
3567  {
3568  /* Read and verify binary header */
3569  char readSig[11];
3570  int32 tmp;
3571 
3572  /* Signature */
3573  if (CopyGetData(cstate, readSig, 11, 11) != 11 ||
3574  memcmp(readSig, BinarySignature, 11) != 0)
3575  ereport(ERROR,
3576  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3577  errmsg("COPY file signature not recognized")));
3578  /* Flags field */
3579  if (!CopyGetInt32(cstate, &tmp))
3580  ereport(ERROR,
3581  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3582  errmsg("invalid COPY file header (missing flags)")));
3583  if ((tmp & (1 << 16)) != 0)
3584  ereport(ERROR,
3585  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3586  errmsg("invalid COPY file header (WITH OIDS)")));
3587  tmp &= ~(1 << 16);
3588  if ((tmp >> 16) != 0)
3589  ereport(ERROR,
3590  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3591  errmsg("unrecognized critical flags in COPY file header")));
3592  /* Header extension length */
3593  if (!CopyGetInt32(cstate, &tmp) ||
3594  tmp < 0)
3595  ereport(ERROR,
3596  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3597  errmsg("invalid COPY file header (missing length)")));
3598  /* Skip extension header, if present */
3599  while (tmp-- > 0)
3600  {
3601  if (CopyGetData(cstate, readSig, 1, 1) != 1)
3602  ereport(ERROR,
3603  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3604  errmsg("invalid COPY file header (wrong length)")));
3605  }
3606  }
3607 
3608  /* create workspace for CopyReadAttributes results */
3609  if (!cstate->binary)
3610  {
3611  AttrNumber attr_count = list_length(cstate->attnumlist);
3612 
3613  cstate->max_fields = attr_count;
3614  cstate->raw_fields = (char **) palloc(attr_count * sizeof(char *));
3615  }
3616 
3617  MemoryContextSwitchTo(oldcontext);
3618 
3619  return cstate;
3620 }
3621 
3622 /*
3623  * Read raw fields in the next line for COPY FROM in text or csv mode.
3624  * Return false if no more lines.
3625  *
3626  * An internal temporary buffer is returned via 'fields'. It is valid until
3627  * the next call of the function. Since the function returns all raw fields
3628  * in the input file, 'nfields' could be different from the number of columns
3629  * in the relation.
3630  *
3631  * NOTE: force_not_null option are not applied to the returned fields.
3632  */
3633 bool
3634 NextCopyFromRawFields(CopyState cstate, char ***fields, int *nfields)
3635 {
3636  int fldct;
3637  bool done;
3638 
3639  /* only available for text or csv input */
3640  Assert(!cstate->binary);
3641 
3642  /* on input just throw the header line away */
3643  if (cstate->cur_lineno == 0 && cstate->header_line)
3644  {
3645  cstate->cur_lineno++;
3646  if (CopyReadLine(cstate))
3647  return false; /* done */
3648  }
3649 
3650  cstate->cur_lineno++;
3651 
3652  /* Actually read the line into memory here */
3653  done = CopyReadLine(cstate);
3654 
3655  /*
3656  * EOF at start of line means we're done. If we see EOF after some
3657  * characters, we act as though it was newline followed by EOF, ie,
3658  * process the line and then exit loop on next iteration.
3659  */
3660  if (done && cstate->line_buf.len == 0)
3661  return false;
3662 
3663  /* Parse the line into de-escaped field values */
3664  if (cstate->csv_mode)
3665  fldct = CopyReadAttributesCSV(cstate);
3666  else
3667  fldct = CopyReadAttributesText(cstate);
3668 
3669  *fields = cstate->raw_fields;
3670  *nfields = fldct;
3671  return true;
3672 }
3673 
3674 /*
3675  * Read next tuple from file for COPY FROM. Return false if no more tuples.
3676  *
3677  * 'econtext' is used to evaluate default expression for each columns not
3678  * read from the file. It can be NULL when no default values are used, i.e.
3679  * when all columns are read from the file.
3680  *
3681  * 'values' and 'nulls' arrays must be the same length as columns of the
3682  * relation passed to BeginCopyFrom. This function fills the arrays.
3683  * Oid of the tuple is returned with 'tupleOid' separately.
3684  */
3685 bool
3687  Datum *values, bool *nulls)
3688 {
3689  TupleDesc tupDesc;
3690  AttrNumber num_phys_attrs,
3691  attr_count,
3692  num_defaults = cstate->num_defaults;
3693  FmgrInfo *in_functions = cstate->in_functions;
3694  Oid *typioparams = cstate->typioparams;
3695  int i;
3696  int *defmap = cstate->defmap;
3697  ExprState **defexprs = cstate->defexprs;
3698 
3699  tupDesc = RelationGetDescr(cstate->rel);
3700  num_phys_attrs = tupDesc->natts;
3701  attr_count = list_length(cstate->attnumlist);
3702 
3703  /* Initialize all values for row to NULL */
3704  MemSet(values, 0, num_phys_attrs * sizeof(Datum));
3705  MemSet(nulls, true, num_phys_attrs * sizeof(bool));
3706 
3707  if (!cstate->binary)
3708  {
3709  char **field_strings;
3710  ListCell *cur;
3711  int fldct;
3712  int fieldno;
3713  char *string;
3714 
3715  /* read raw fields in the next line */
3716  if (!NextCopyFromRawFields(cstate, &field_strings, &fldct))
3717  return false;
3718 
3719  /* check for overflowing fields */
3720  if (attr_count > 0 && fldct > attr_count)
3721  ereport(ERROR,
3722  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3723  errmsg("extra data after last expected column")));
3724 
3725  fieldno = 0;
3726 
3727  /* Loop to read the user attributes on the line. */
3728  foreach(cur, cstate->attnumlist)
3729  {
3730  int attnum = lfirst_int(cur);
3731  int m = attnum - 1;
3732  Form_pg_attribute att = TupleDescAttr(tupDesc, m);
3733 
3734  if (fieldno >= fldct)
3735  ereport(ERROR,
3736  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3737  errmsg("missing data for column \"%s\"",
3738  NameStr(att->attname))));
3739  string = field_strings[fieldno++];
3740 
3741  if (cstate->convert_select_flags &&
3742  !cstate->convert_select_flags[m])
3743  {
3744  /* ignore input field, leaving column as NULL */
3745  continue;
3746  }
3747 
3748  if (cstate->csv_mode)
3749  {
3750  if (string == NULL &&
3751  cstate->force_notnull_flags[m])
3752  {
3753  /*
3754  * FORCE_NOT_NULL option is set and column is NULL -
3755  * convert it to the NULL string.
3756  */
3757  string = cstate->null_print;
3758  }
3759  else if (string != NULL && cstate->force_null_flags[m]
3760  && strcmp(string, cstate->null_print) == 0)
3761  {
3762  /*
3763  * FORCE_NULL option is set and column matches the NULL
3764  * string. It must have been quoted, or otherwise the
3765  * string would already have been set to NULL. Convert it
3766  * to NULL as specified.
3767  */
3768  string = NULL;
3769  }
3770  }
3771 
3772  cstate->cur_attname = NameStr(att->attname);
3773  cstate->cur_attval = string;
3774  values[m] = InputFunctionCall(&in_functions[m],
3775  string,
3776  typioparams[m],
3777  att->atttypmod);
3778  if (string != NULL)
3779  nulls[m] = false;
3780  cstate->cur_attname = NULL;
3781  cstate->cur_attval = NULL;
3782  }
3783 
3784  Assert(fieldno == attr_count);
3785  }
3786  else
3787  {
3788  /* binary */
3789  int16 fld_count;
3790  ListCell *cur;
3791 
3792  cstate->cur_lineno++;
3793 
3794  if (!CopyGetInt16(cstate, &fld_count))
3795  {
3796  /* EOF detected (end of file, or protocol-level EOF) */
3797  return false;
3798  }
3799 
3800  if (fld_count == -1)
3801  {
3802  /*
3803  * Received EOF marker. In a V3-protocol copy, wait for the
3804  * protocol-level EOF, and complain if it doesn't come
3805  * immediately. This ensures that we correctly handle CopyFail,
3806  * if client chooses to send that now.
3807  *
3808  * Note that we MUST NOT try to read more data in an old-protocol
3809  * copy, since there is no protocol-level EOF marker then. We
3810  * could go either way for copy from file, but choose to throw
3811  * error if there's data after the EOF marker, for consistency
3812  * with the new-protocol case.
3813  */
3814  char dummy;
3815 
3816  if (cstate->copy_dest != COPY_OLD_FE &&
3817  CopyGetData(cstate, &dummy, 1, 1) > 0)
3818  ereport(ERROR,
3819  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3820  errmsg("received copy data after EOF marker")));
3821  return false;
3822  }
3823 
3824  if (fld_count != attr_count)
3825  ereport(ERROR,
3826  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3827  errmsg("row field count is %d, expected %d",
3828  (int) fld_count, attr_count)));
3829 
3830  i = 0;
3831  foreach(cur, cstate->attnumlist)
3832  {
3833  int attnum = lfirst_int(cur);
3834  int m = attnum - 1;
3835  Form_pg_attribute att = TupleDescAttr(tupDesc, m);
3836 
3837  cstate->cur_attname = NameStr(att->attname);
3838  i++;
3839  values[m] = CopyReadBinaryAttribute(cstate,
3840  i,
3841  &in_functions[m],
3842  typioparams[m],
3843  att->atttypmod,
3844  &nulls[m]);
3845  cstate->cur_attname = NULL;
3846  }
3847  }
3848 
3849  /*
3850  * Now compute and insert any defaults available for the columns not
3851  * provided by the input data. Anything not processed here or above will
3852  * remain NULL.
3853  */
3854  for (i = 0; i < num_defaults; i++)
3855  {
3856  /*
3857  * The caller must supply econtext and have switched into the
3858  * per-tuple memory context in it.
3859  */
3860  Assert(econtext != NULL);
3862 
3863  values[defmap[i]] = ExecEvalExpr(defexprs[i], econtext,
3864  &nulls[defmap[i]]);
3865  }
3866 
3867  return true;
3868 }
3869 
3870 /*
3871  * Clean up storage and release resources for COPY FROM.
3872  */
3873 void
3875 {
3876  /* No COPY FROM related resources except memory. */
3877 
3878  EndCopy(cstate);
3879 }
3880 
3881 /*
3882  * Read the next input line and stash it in line_buf, with conversion to
3883  * server encoding.
3884  *
3885  * Result is true if read was terminated by EOF, false if terminated
3886  * by newline. The terminating newline or EOF marker is not included
3887  * in the final value of line_buf.
3888  */
3889 static bool
3891 {
3892  bool result;
3893 
3894  resetStringInfo(&cstate->line_buf);
3895  cstate->line_buf_valid = true;
3896 
3897  /* Mark that encoding conversion hasn't occurred yet */
3898  cstate->line_buf_converted = false;
3899 
3900  /* Parse data and transfer into line_buf */
3901  result = CopyReadLineText(cstate);
3902 
3903  if (result)
3904  {
3905  /*
3906  * Reached EOF. In protocol version 3, we should ignore anything
3907  * after \. up to the protocol end of copy data. (XXX maybe better
3908  * not to treat \. as special?)
3909  */
3910  if (cstate->copy_dest == COPY_NEW_FE)
3911  {
3912  do
3913  {
3914  cstate->raw_buf_index = cstate->raw_buf_len;
3915  } while (CopyLoadRawBuf(cstate));
3916  }
3917  }
3918  else
3919  {
3920  /*
3921  * If we didn't hit EOF, then we must have transferred the EOL marker
3922  * to line_buf along with the data. Get rid of it.
3923  */
3924  switch (cstate->eol_type)
3925  {
3926  case EOL_NL:
3927  Assert(cstate->line_buf.len >= 1);
3928  Assert(cstate->line_buf.data[cstate->line_buf.len - 1] == '\n');
3929  cstate->line_buf.len--;
3930  cstate->line_buf.data[cstate->line_buf.len] = '\0';
3931  break;
3932  case EOL_CR:
3933  Assert(cstate->line_buf.len >= 1);
3934  Assert(cstate->line_buf.data[cstate->line_buf.len - 1] == '\r');
3935  cstate->line_buf.len--;
3936  cstate->line_buf.data[cstate->line_buf.len] = '\0';
3937  break;
3938  case EOL_CRNL:
3939  Assert(cstate->line_buf.len >= 2);
3940  Assert(cstate->line_buf.data[cstate->line_buf.len - 2] == '\r');
3941  Assert(cstate->line_buf.data[cstate->line_buf.len - 1] == '\n');
3942  cstate->line_buf.len -= 2;
3943  cstate->line_buf.data[cstate->line_buf.len] = '\0';
3944  break;
3945  case EOL_UNKNOWN:
3946  /* shouldn't get here */
3947  Assert(false);
3948  break;
3949  }
3950  }
3951 
3952  /* Done reading the line. Convert it to server encoding. */
3953  if (cstate->need_transcoding)
3954  {
3955  char *cvt;
3956 
3957  cvt = pg_any_to_server(cstate->line_buf.data,
3958  cstate->line_buf.len,
3959  cstate->file_encoding);
3960  if (cvt != cstate->line_buf.data)
3961  {
3962  /* transfer converted data back to line_buf */
3963  resetStringInfo(&cstate->line_buf);
3964  appendBinaryStringInfo(&cstate->line_buf, cvt, strlen(cvt));
3965  pfree(cvt);
3966  }
3967  }
3968 
3969  /* Now it's safe to use the buffer in error messages */
3970  cstate->line_buf_converted = true;
3971 
3972  return result;
3973 }
3974 
3975 /*
3976  * CopyReadLineText - inner loop of CopyReadLine for text mode
3977  */
3978 static bool
3980 {
3981  char *copy_raw_buf;
3982  int raw_buf_ptr;
3983  int copy_buf_len;
3984  bool need_data = false;
3985  bool hit_eof = false;
3986  bool result = false;
3987  char mblen_str[2];
3988 
3989  /* CSV variables */
3990  bool first_char_in_line = true;
3991  bool in_quote = false,
3992  last_was_esc = false;
3993  char quotec = '\0';
3994  char escapec = '\0';
3995 
3996  if (cstate->csv_mode)
3997  {
3998  quotec = cstate->quote[0];
3999  escapec = cstate->escape[0];
4000  /* ignore special escape processing if it's the same as quotec */
4001  if (quotec == escapec)
4002  escapec = '\0';
4003  }
4004 
4005  mblen_str[1] = '\0';
4006 
4007  /*
4008  * The objective of this loop is to transfer the entire next input line
4009  * into line_buf. Hence, we only care for detecting newlines (\r and/or
4010  * \n) and the end-of-copy marker (\.).
4011  *
4012  * In CSV mode, \r and \n inside a quoted field are just part of the data
4013  * value and are put in line_buf. We keep just enough state to know if we
4014  * are currently in a quoted field or not.
4015  *
4016  * These four characters, and the CSV escape and quote characters, are
4017  * assumed the same in frontend and backend encodings.
4018  *
4019  * For speed, we try to move data from raw_buf to line_buf in chunks
4020  * rather than one character at a time. raw_buf_ptr points to the next
4021  * character to examine; any characters from raw_buf_index to raw_buf_ptr
4022  * have been determined to be part of the line, but not yet transferred to
4023  * line_buf.
4024  *
4025  * For a little extra speed within the loop, we copy raw_buf and
4026  * raw_buf_len into local variables.
4027  */
4028  copy_raw_buf = cstate->raw_buf;
4029  raw_buf_ptr = cstate->raw_buf_index;
4030  copy_buf_len = cstate->raw_buf_len;
4031 
4032  for (;;)
4033  {
4034  int prev_raw_ptr;
4035  char c;
4036 
4037  /*
4038  * Load more data if needed. Ideally we would just force four bytes
4039  * of read-ahead and avoid the many calls to
4040  * IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(), but the COPY_OLD_FE protocol
4041  * does not allow us to read too far ahead or we might read into the
4042  * next data, so we read-ahead only as far we know we can. One
4043  * optimization would be to read-ahead four byte here if
4044  * cstate->copy_dest != COPY_OLD_FE, but it hardly seems worth it,
4045  * considering the size of the buffer.
4046  */
4047  if (raw_buf_ptr >= copy_buf_len || need_data)
4048  {
4050 
4051  /*
4052  * Try to read some more data. This will certainly reset
4053  * raw_buf_index to zero, and raw_buf_ptr must go with it.
4054  */
4055  if (!CopyLoadRawBuf(cstate))
4056  hit_eof = true;
4057  raw_buf_ptr = 0;
4058  copy_buf_len = cstate->raw_buf_len;
4059 
4060  /*
4061  * If we are completely out of data, break out of the loop,
4062  * reporting EOF.
4063  */
4064  if (copy_buf_len <= 0)
4065  {
4066  result = true;
4067  break;
4068  }
4069  need_data = false;
4070  }
4071 
4072  /* OK to fetch a character */
4073  prev_raw_ptr = raw_buf_ptr;
4074  c = copy_raw_buf[raw_buf_ptr++];
4075 
4076  if (cstate->csv_mode)
4077  {
4078  /*
4079  * If character is '\\' or '\r', we may need to look ahead below.
4080  * Force fetch of the next character if we don't already have it.
4081  * We need to do this before changing CSV state, in case one of
4082  * these characters is also the quote or escape character.
4083  *
4084  * Note: old-protocol does not like forced prefetch, but it's OK
4085  * here since we cannot validly be at EOF.
4086  */
4087  if (c == '\\' || c == '\r')
4088  {
4090  }
4091 
4092  /*
4093  * Dealing with quotes and escapes here is mildly tricky. If the
4094  * quote char is also the escape char, there's no problem - we
4095  * just use the char as a toggle. If they are different, we need
4096  * to ensure that we only take account of an escape inside a
4097  * quoted field and immediately preceding a quote char, and not
4098  * the second in an escape-escape sequence.
4099  */
4100  if (in_quote && c == escapec)
4101  last_was_esc = !last_was_esc;
4102  if (c == quotec && !last_was_esc)
4103  in_quote = !in_quote;
4104  if (c != escapec)
4105  last_was_esc = false;
4106 
4107  /*
4108  * Updating the line count for embedded CR and/or LF chars is
4109  * necessarily a little fragile - this test is probably about the
4110  * best we can do. (XXX it's arguable whether we should do this
4111  * at all --- is cur_lineno a physical or logical count?)
4112  */
4113  if (in_quote && c == (cstate->eol_type == EOL_NL ? '\n' : '\r'))
4114  cstate->cur_lineno++;
4115  }
4116 
4117  /* Process \r */
4118  if (c == '\r' && (!cstate->csv_mode || !in_quote))
4119  {
4120  /* Check for \r\n on first line, _and_ handle \r\n. */
4121  if (cstate->eol_type == EOL_UNKNOWN ||
4122  cstate->eol_type == EOL_CRNL)
4123  {
4124  /*
4125  * If need more data, go back to loop top to load it.
4126  *
4127  * Note that if we are at EOF, c will wind up as '\0' because
4128  * of the guaranteed pad of raw_buf.
4129  */
4131 
4132  /* get next char */
4133  c = copy_raw_buf[raw_buf_ptr];
4134 
4135  if (c == '\n')
4136  {
4137  raw_buf_ptr++; /* eat newline */
4138  cstate->eol_type = EOL_CRNL; /* in case not set yet */
4139  }
4140  else
4141  {
4142  /* found \r, but no \n */
4143  if (cstate->eol_type == EOL_CRNL)
4144  ereport(ERROR,
4145  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4146  !cstate->csv_mode ?
4147  errmsg("literal carriage return found in data") :
4148  errmsg("unquoted carriage return found in data"),
4149  !cstate->csv_mode ?
4150  errhint("Use \"\\r\" to represent carriage return.") :
4151  errhint("Use quoted CSV field to represent carriage return.")));
4152 
4153  /*
4154  * if we got here, it is the first line and we didn't find
4155  * \n, so don't consume the peeked character
4156  */
4157  cstate->eol_type = EOL_CR;
4158  }
4159  }
4160  else if (cstate->eol_type == EOL_NL)
4161  ereport(ERROR,
4162  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4163  !cstate->csv_mode ?
4164  errmsg("literal carriage return found in data") :
4165  errmsg("unquoted carriage return found in data"),
4166  !cstate->csv_mode ?
4167  errhint("Use \"\\r\" to represent carriage return.") :
4168  errhint("Use quoted CSV field to represent carriage return.")));
4169  /* If reach here, we have found the line terminator */
4170  break;
4171  }
4172 
4173  /* Process \n */
4174  if (c == '\n' && (!cstate->csv_mode || !in_quote))
4175  {
4176  if (cstate->eol_type == EOL_CR || cstate->eol_type == EOL_CRNL)
4177  ereport(ERROR,
4178  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4179  !cstate->csv_mode ?
4180  errmsg("literal newline found in data") :
4181  errmsg("unquoted newline found in data"),
4182  !cstate->csv_mode ?
4183  errhint("Use \"\\n\" to represent newline.") :
4184  errhint("Use quoted CSV field to represent newline.")));
4185  cstate->eol_type = EOL_NL; /* in case not set yet */
4186  /* If reach here, we have found the line terminator */
4187  break;
4188  }
4189 
4190  /*
4191  * In CSV mode, we only recognize \. alone on a line. This is because
4192  * \. is a valid CSV data value.
4193  */
4194  if (c == '\\' && (!cstate->csv_mode || first_char_in_line))
4195  {
4196  char c2;
4197 
4200 
4201  /* -----
4202  * get next character
4203  * Note: we do not change c so if it isn't \., we can fall
4204  * through and continue processing for file encoding.
4205  * -----
4206  */
4207  c2 = copy_raw_buf[raw_buf_ptr];
4208 
4209  if (c2 == '.')
4210  {
4211  raw_buf_ptr++; /* consume the '.' */
4212 
4213  /*
4214  * Note: if we loop back for more data here, it does not
4215  * matter that the CSV state change checks are re-executed; we
4216  * will come back here with no important state changed.
4217  */
4218  if (cstate->eol_type == EOL_CRNL)
4219  {
4220  /* Get the next character */
4222  /* if hit_eof, c2 will become '\0' */
4223  c2 = copy_raw_buf[raw_buf_ptr++];
4224 
4225  if (c2 == '\n')
4226  {
4227  if (!cstate->csv_mode)
4228  ereport(ERROR,
4229  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4230  errmsg("end-of-copy marker does not match previous newline style")));
4231  else
4233  }
4234  else if (c2 != '\r')
4235  {
4236  if (!cstate->csv_mode)
4237  ereport(ERROR,
4238  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4239  errmsg("end-of-copy marker corrupt")));
4240  else
4242  }
4243  }
4244 
4245  /* Get the next character */
4247  /* if hit_eof, c2 will become '\0' */
4248  c2 = copy_raw_buf[raw_buf_ptr++];
4249 
4250  if (c2 != '\r' && c2 != '\n')
4251  {
4252  if (!cstate->csv_mode)
4253  ereport(ERROR,
4254  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4255  errmsg("end-of-copy marker corrupt")));
4256  else
4258  }
4259 
4260  if ((cstate->eol_type == EOL_NL && c2 != '\n') ||
4261  (cstate->eol_type == EOL_CRNL && c2 != '\n') ||
4262  (cstate->eol_type == EOL_CR && c2 != '\r'))
4263  {
4264  ereport(ERROR,
4265  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4266  errmsg("end-of-copy marker does not match previous newline style")));
4267  }
4268 
4269  /*
4270  * Transfer only the data before the \. into line_buf, then
4271  * discard the data and the \. sequence.
4272  */
4273  if (prev_raw_ptr > cstate->raw_buf_index)
4275  cstate->raw_buf + cstate->raw_buf_index,
4276  prev_raw_ptr - cstate->raw_buf_index);
4277  cstate->raw_buf_index = raw_buf_ptr;
4278  result = true; /* report EOF */
4279  break;
4280  }
4281  else if (!cstate->csv_mode)
4282 
4283  /*
4284  * If we are here, it means we found a backslash followed by
4285  * something other than a period. In non-CSV mode, anything
4286  * after a backslash is special, so we skip over that second
4287  * character too. If we didn't do that \\. would be
4288  * considered an eof-of copy, while in non-CSV mode it is a
4289  * literal backslash followed by a period. In CSV mode,
4290  * backslashes are not special, so we want to process the
4291  * character after the backslash just like a normal character,
4292  * so we don't increment in those cases.
4293  */
4294  raw_buf_ptr++;
4295  }
4296 
4297  /*
4298  * This label is for CSV cases where \. appears at the start of a
4299  * line, but there is more text after it, meaning it was a data value.
4300  * We are more strict for \. in CSV mode because \. could be a data
4301  * value, while in non-CSV mode, \. cannot be a data value.
4302  */
4303 not_end_of_copy:
4304 
4305  /*
4306  * Process all bytes of a multi-byte character as a group.
4307  *
4308  * We only support multi-byte sequences where the first byte has the
4309  * high-bit set, so as an optimization we can avoid this block
4310  * entirely if it is not set.
4311  */
4312  if (cstate->encoding_embeds_ascii && IS_HIGHBIT_SET(c))
4313  {
4314  int mblen;
4315 
4316  /*
4317  * It is enough to look at the first byte in all our encodings, to
4318  * get the length. (GB18030 is a bit special, but still works for
4319  * our purposes; see comment in pg_gb18030_mblen())
4320  */
4321  mblen_str[0] = c;
4322  mblen = pg_encoding_mblen(cstate->file_encoding, mblen_str);
4323 
4325  IF_NEED_REFILL_AND_EOF_BREAK(mblen - 1);
4326  raw_buf_ptr += mblen - 1;
4327  }
4328  first_char_in_line = false;
4329  } /* end of outer loop */
4330 
4331  /*
4332  * Transfer any still-uncopied data to line_buf.
4333  */
4335 
4336  return result;
4337 }
4338 
4339 /*
4340  * Return decimal value for a hexadecimal digit
4341  */
4342 static int
4344 {
4345  if (isdigit((unsigned char) hex))
4346  return hex - '0';
4347  else
4348  return tolower((unsigned char) hex) - 'a' + 10;
4349 }
4350 
4351 /*
4352  * Parse the current line into separate attributes (fields),
4353  * performing de-escaping as needed.
4354  *
4355  * The input is in line_buf. We use attribute_buf to hold the result
4356  * strings. cstate->raw_fields[k] is set to point to the k'th attribute
4357  * string, or NULL when the input matches the null marker string.
4358  * This array is expanded as necessary.
4359  *
4360  * (Note that the caller cannot check for nulls since the returned
4361  * string would be the post-de-escaping equivalent, which may look
4362  * the same as some valid data string.)
4363  *
4364  * delim is the column delimiter string (must be just one byte for now).
4365  * null_print is the null marker string. Note that this is compared to
4366  * the pre-de-escaped input string.
4367  *
4368  * The return value is the number of fields actually read.
4369  */
4370 static int
4372 {
4373  char delimc = cstate->delim[0];
4374  int fieldno;
4375  char *output_ptr;
4376  char *cur_ptr;
4377  char *line_end_ptr;
4378 
4379  /*
4380  * We need a special case for zero-column tables: check that the input
4381  * line is empty, and return.
4382  */
4383  if (cstate->max_fields <= 0)
4384  {
4385  if (cstate->line_buf.len != 0)
4386  ereport(ERROR,
4387  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4388  errmsg("extra data after last expected column")));
4389  return 0;
4390  }
4391 
4392  resetStringInfo(&cstate->attribute_buf);
4393 
4394  /*
4395  * The de-escaped attributes will certainly not be longer than the input
4396  * data line, so we can just force attribute_buf to be large enough and
4397  * then transfer data without any checks for enough space. We need to do
4398  * it this way because enlarging attribute_buf mid-stream would invalidate
4399  * pointers already stored into cstate->raw_fields[].
4400  */
4401  if (cstate->attribute_buf.maxlen <= cstate->line_buf.len)
4402  enlargeStringInfo(&cstate->attribute_buf, cstate->line_buf.len);
4403  output_ptr = cstate->attribute_buf.data;
4404 
4405  /* set pointer variables for loop */
4406  cur_ptr = cstate->line_buf.data;
4407  line_end_ptr = cstate->line_buf.data + cstate->line_buf.len;
4408 
4409  /* Outer loop iterates over fields */
4410  fieldno = 0;
4411  for (;;)
4412  {
4413  bool found_delim = false;
4414  char *start_ptr;
4415  char *end_ptr;
4416  int input_len;
4417  bool saw_non_ascii = false;
4418 
4419  /* Make sure there is enough space for the next value */
4420  if (fieldno >= cstate->max_fields)
4421  {
4422  cstate->max_fields *= 2;
4423  cstate->raw_fields =
4424  repalloc(cstate->raw_fields, cstate->max_fields * sizeof(char *));
4425  }
4426 
4427  /* Remember start of field on both input and output sides */
4428  start_ptr = cur_ptr;
4429  cstate->raw_fields[fieldno] = output_ptr;
4430 
4431  /*
4432  * Scan data for field.
4433  *
4434  * Note that in this loop, we are scanning to locate the end of field
4435  * and also speculatively performing de-escaping. Once we find the
4436  * end-of-field, we can match the raw field contents against the null
4437  * marker string. Only after that comparison fails do we know that
4438  * de-escaping is actually the right thing to do; therefore we *must
4439  * not* throw any syntax errors before we've done the null-marker
4440  * check.
4441  */
4442  for (;;)
4443  {
4444  char c;
4445 
4446  end_ptr = cur_ptr;
4447  if (cur_ptr >= line_end_ptr)
4448  break;
4449  c = *cur_ptr++;
4450  if (c == delimc)
4451  {
4452  found_delim = true;
4453  break;
4454  }
4455  if (c == '\\')
4456  {
4457  if (cur_ptr >= line_end_ptr)
4458  break;
4459  c = *cur_ptr++;
4460  switch (c)
4461  {
4462  case '0':
4463  case '1':
4464  case '2':
4465  case '3':
4466  case '4':
4467  case '5':
4468  case '6':
4469  case '7':
4470  {
4471  /* handle \013 */
4472  int val;
4473 
4474  val = OCTVALUE(c);
4475  if (cur_ptr < line_end_ptr)
4476  {
4477  c = *cur_ptr;
4478  if (ISOCTAL(c))
4479  {
4480  cur_ptr++;
4481  val = (val << 3) + OCTVALUE(c);
4482  if (cur_ptr < line_end_ptr)
4483  {
4484  c = *cur_ptr;
4485  if (ISOCTAL(c))
4486  {
4487  cur_ptr++;
4488  val = (val << 3) + OCTVALUE(c);
4489  }
4490  }
4491  }
4492  }
4493  c = val & 0377;
4494  if (c == '\0' || IS_HIGHBIT_SET(c))
4495  saw_non_ascii = true;
4496  }
4497  break;
4498  case 'x':
4499  /* Handle \x3F */
4500  if (cur_ptr < line_end_ptr)
4501  {
4502  char hexchar = *cur_ptr;
4503 
4504  if (isxdigit((unsigned char) hexchar))
4505  {
4506  int val = GetDecimalFromHex(hexchar);
4507 
4508  cur_ptr++;
4509  if (cur_ptr < line_end_ptr)
4510  {
4511  hexchar = *cur_ptr;
4512  if (isxdigit((unsigned char) hexchar))
4513  {
4514  cur_ptr++;
4515  val = (val << 4) + GetDecimalFromHex(hexchar);
4516  }
4517  }
4518  c = val & 0xff;
4519  if (c == '\0' || IS_HIGHBIT_SET(c))
4520  saw_non_ascii = true;
4521  }
4522  }
4523  break;
4524  case 'b':
4525  c = '\b';
4526  break;
4527  case 'f':
4528  c = '\f';
4529  break;
4530  case 'n':
4531  c = '\n';
4532  break;
4533  case 'r':
4534  c = '\r';
4535  break;
4536  case 't':
4537  c = '\t';
4538  break;
4539  case 'v':
4540  c = '\v';
4541  break;
4542 
4543  /*
4544  * in all other cases, take the char after '\'
4545  * literally
4546  */
4547  }
4548  }
4549 
4550  /* Add c to output string */
4551  *output_ptr++ = c;
4552  }
4553 
4554  /* Check whether raw input matched null marker */
4555  input_len = end_ptr - start_ptr;
4556  if (input_len == cstate->null_print_len &&
4557  strncmp(start_ptr, cstate->null_print, input_len) == 0)
4558  cstate->raw_fields[fieldno] = NULL;
4559  else
4560  {
4561  /*
4562  * At this point we know the field is supposed to contain data.
4563  *
4564  * If we de-escaped any non-7-bit-ASCII chars, make sure the
4565  * resulting string is valid data for the db encoding.
4566  */
4567  if (saw_non_ascii)
4568  {
4569  char *fld = cstate->raw_fields[fieldno];
4570 
4571  pg_verifymbstr(fld, output_ptr - fld, false);
4572  }
4573  }
4574 
4575  /* Terminate attribute value in output area */
4576  *output_ptr++ = '\0';
4577 
4578  fieldno++;
4579  /* Done if we hit EOL instead of a delim */
4580  if (!found_delim)
4581  break;
4582  }
4583 
4584  /* Clean up state of attribute_buf */
4585  output_ptr--;
4586  Assert(*output_ptr == '\0');
4587  cstate->attribute_buf.len = (output_ptr - cstate->attribute_buf.data);
4588 
4589  return fieldno;
4590 }
4591 
4592 /*
4593  * Parse the current line into separate attributes (fields),
4594  * performing de-escaping as needed. This has exactly the same API as
4595  * CopyReadAttributesText, except we parse the fields according to
4596  * "standard" (i.e. common) CSV usage.
4597  */
4598 static int
4600 {
4601  char delimc = cstate->delim[0];
4602  char quotec = cstate->quote[0];
4603  char escapec = cstate->escape[0];
4604  int fieldno;
4605  char *output_ptr;
4606  char *cur_ptr;
4607  char *line_end_ptr;
4608 
4609  /*
4610  * We need a special case for zero-column tables: check that the input
4611  * line is empty, and return.
4612  */
4613  if (cstate->max_fields <= 0)
4614  {
4615  if (cstate->line_buf.len != 0)
4616  ereport(ERROR,
4617  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4618  errmsg("extra data after last expected column")));
4619  return 0;
4620  }
4621 
4622  resetStringInfo(&cstate->attribute_buf);
4623 
4624  /*
4625  * The de-escaped attributes will certainly not be longer than the input
4626  * data line, so we can just force attribute_buf to be large enough and
4627  * then transfer data without any checks for enough space. We need to do
4628  * it this way because enlarging attribute_buf mid-stream would invalidate
4629  * pointers already stored into cstate->raw_fields[].
4630  */
4631  if (cstate->attribute_buf.maxlen <= cstate->line_buf.len)
4632  enlargeStringInfo(&cstate->attribute_buf, cstate->line_buf.len);
4633  output_ptr = cstate->attribute_buf.data;
4634 
4635  /* set pointer variables for loop */
4636  cur_ptr = cstate->line_buf.data;
4637  line_end_ptr = cstate->line_buf.data + cstate->line_buf.len;
4638 
4639  /* Outer loop iterates over fields */
4640  fieldno = 0;
4641  for (;;)
4642  {
4643  bool found_delim = false;
4644  bool saw_quote = false;
4645  char *start_ptr;
4646  char *end_ptr;
4647  int input_len;
4648 
4649  /* Make sure there is enough space for the next value */
4650  if (fieldno >= cstate->max_fields)
4651  {
4652  cstate->max_fields *= 2;
4653  cstate->raw_fields =
4654  repalloc(cstate->raw_fields, cstate->max_fields * sizeof(char *));
4655  }
4656 
4657  /* Remember start of field on both input and output sides */
4658  start_ptr = cur_ptr;
4659  cstate->raw_fields[fieldno] = output_ptr;
4660 
4661  /*
4662  * Scan data for field,
4663  *
4664  * The loop starts in "not quote" mode and then toggles between that
4665  * and "in quote" mode. The loop exits normally if it is in "not
4666  * quote" mode and a delimiter or line end is seen.
4667  */
4668  for (;;)
4669  {
4670  char c;
4671 
4672  /* Not in quote */
4673  for (;;)
4674  {
4675  end_ptr = cur_ptr;
4676  if (cur_ptr >= line_end_ptr)
4677  goto endfield;
4678  c = *cur_ptr++;
4679  /* unquoted field delimiter */
4680  if (c == delimc)
4681  {
4682  found_delim = true;
4683  goto endfield;
4684  }
4685  /* start of quoted field (or part of field) */
4686  if (c == quotec)
4687  {
4688  saw_quote = true;
4689  break;
4690  }
4691  /* Add c to output string */
4692  *output_ptr++ = c;
4693  }
4694 
4695  /* In quote */
4696  for (;;)
4697  {
4698  end_ptr = cur_ptr;
4699  if (cur_ptr >= line_end_ptr)
4700  ereport(ERROR,
4701  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4702  errmsg("unterminated CSV quoted field")));
4703 
4704  c = *cur_ptr++;
4705 
4706  /* escape within a quoted field */
4707  if (c == escapec)
4708  {
4709  /*
4710  * peek at the next char if available, and escape it if it
4711  * is an escape char or a quote char
4712  */
4713  if (cur_ptr < line_end_ptr)
4714  {
4715  char nextc = *cur_ptr;
4716 
4717  if (nextc == escapec || nextc == quotec)
4718  {
4719  *output_ptr++ = nextc;
4720  cur_ptr++;
4721  continue;
4722  }
4723  }
4724  }
4725 
4726  /*
4727  * end of quoted field. Must do this test after testing for
4728  * escape in case quote char and escape char are the same
4729  * (which is the common case).
4730  */
4731  if (c == quotec)
4732  break;
4733 
4734  /* Add c to output string */
4735  *output_ptr++ = c;
4736  }
4737  }
4738 endfield:
4739 
4740  /* Terminate attribute value in output area */
4741  *output_ptr++ = '\0';
4742 
4743  /* Check whether raw input matched null marker */
4744  input_len = end_ptr - start_ptr;
4745  if (!saw_quote && input_len == cstate->null_print_len &&
4746  strncmp(start_ptr, cstate->null_print, input_len) == 0)
4747  cstate->raw_fields[fieldno] = NULL;
4748 
4749  fieldno++;
4750  /* Done if we hit EOL instead of a delim */
4751  if (!found_delim)
4752  break;
4753  }
4754 
4755  /* Clean up state of attribute_buf */
4756  output_ptr--;
4757  Assert(*output_ptr == '\0');
4758  cstate->attribute_buf.len = (output_ptr - cstate->attribute_buf.data);
4759 
4760  return fieldno;
4761 }
4762 
4763 
4764 /*
4765  * Read a binary attribute
4766  */
4767 static Datum
4769  int column_no, FmgrInfo *flinfo,
4770  Oid typioparam, int32 typmod,
4771  bool *isnull)
4772 {
4773  int32 fld_size;
4774  Datum result;
4775 
4776  if (!CopyGetInt32(cstate, &fld_size))
4777  ereport(ERROR,
4778  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4779  errmsg("unexpected EOF in COPY data")));
4780  if (fld_size == -1)
4781  {
4782  *isnull = true;
4783  return ReceiveFunctionCall(flinfo, NULL, typioparam, typmod);
4784  }
4785  if (fld_size < 0)
4786  ereport(ERROR,
4787  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4788  errmsg("invalid field size")));
4789 
4790  /* reset attribute_buf to empty, and load raw data in it */
4791  resetStringInfo(&cstate->attribute_buf);
4792 
4793  enlargeStringInfo(&cstate->attribute_buf, fld_size);
4794  if (CopyGetData(cstate, cstate->attribute_buf.data,
4795  fld_size, fld_size) != fld_size)
4796  ereport(ERROR,
4797  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4798  errmsg("unexpected EOF in COPY data")));
4799 
4800  cstate->attribute_buf.len = fld_size;
4801  cstate->attribute_buf.data[fld_size] = '\0';
4802 
4803  /* Call the column type's binary input converter */
4804  result = ReceiveFunctionCall(flinfo, &cstate->attribute_buf,
4805  typioparam, typmod);
4806 
4807  /* Trouble if it didn't eat the whole buffer */
4808  if (cstate->attribute_buf.cursor != cstate->attribute_buf.len)
4809  ereport(ERROR,
4810  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
4811  errmsg("incorrect binary data format")));
4812 
4813  *isnull = false;
4814  return result;
4815 }
4816 
4817 /*
4818  * Send text representation of one attribute, with conversion and escaping
4819  */
4820 #define DUMPSOFAR() \
4821  do { \
4822  if (ptr > start) \
4823  CopySendData(cstate, start, ptr - start); \
4824  } while (0)
4825 
4826 static void
4827 CopyAttributeOutText(CopyState cstate, char *string)
4828 {
4829  char *ptr;
4830  char *start;
4831  char c;
4832  char delimc = cstate->delim[0];
4833 
4834  if (cstate->need_transcoding)
4835  ptr = pg_server_to_any(string, strlen(string), cstate->file_encoding);
4836  else
4837  ptr = string;
4838 
4839  /*
4840  * We have to grovel through the string searching for control characters
4841  * and instances of the delimiter character. In most cases, though, these
4842  * are infrequent. To avoid overhead from calling CopySendData once per
4843  * character, we dump out all characters between escaped characters in a
4844  * single call. The loop invariant is that the data from "start" to "ptr"
4845  * can be sent literally, but hasn't yet been.
4846  *
4847  * We can skip pg_encoding_mblen() overhead when encoding is safe, because
4848  * in valid backend encodings, extra bytes of a multibyte character never
4849  * look like ASCII. This loop is sufficiently performance-critical that
4850  * it's worth making two copies of it to get the IS_HIGHBIT_SET() test out
4851  * of the normal safe-encoding path.
4852  */
4853  if (cstate->encoding_embeds_ascii)
4854  {
4855  start = ptr;
4856  while ((c = *ptr) != '\0')
4857  {
4858  if ((unsigned char) c < (unsigned char) 0x20)
4859  {
4860  /*
4861  * \r and \n must be escaped, the others are traditional. We
4862  * prefer to dump these using the C-like notation, rather than
4863  * a backslash and the literal character, because it makes the
4864  * dump file a bit more proof against Microsoftish data
4865  * mangling.
4866  */
4867  switch (c)
4868  {
4869  case '\b':
4870  c = 'b';
4871  break;
4872  case '\f':
4873  c = 'f';
4874  break;
4875  case '\n':
4876  c = 'n';
4877  break;
4878  case '\r':
4879  c = 'r';
4880  break;
4881  case '\t':
4882  c = 't';
4883  break;
4884  case '\v':
4885  c = 'v';
4886  break;
4887  default:
4888  /* If it's the delimiter, must backslash it */
4889  if (c == delimc)
4890  break;
4891  /* All ASCII control chars are length 1 */
4892  ptr++;
4893  continue; /* fall to end of loop */
4894  }
4895  /* if we get here, we need to convert the control char */
4896  DUMPSOFAR();
4897  CopySendChar(cstate, '\\');
4898  CopySendChar(cstate, c);
4899  start = ++ptr; /* do not include char in next run */
4900  }
4901  else if (c == '\\' || c == delimc)
4902  {
4903  DUMPSOFAR();
4904  CopySendChar(cstate, '\\');
4905  start = ptr++; /* we include char in next run */
4906  }
4907  else if (IS_HIGHBIT_SET(c))
4908  ptr += pg_encoding_mblen(cstate->file_encoding, ptr);
4909  else
4910  ptr++;
4911  }
4912  }
4913  else
4914  {
4915  start = ptr;
4916  while ((c = *ptr) != '\0')
4917  {
4918  if ((unsigned char) c < (unsigned char) 0x20)
4919  {
4920  /*
4921  * \r and \n must be escaped, the others are traditional. We
4922  * prefer to dump these using the C-like notation, rather than
4923  * a backslash and the literal character, because it makes the
4924  * dump file a bit more proof against Microsoftish data
4925  * mangling.
4926  */
4927  switch (c)
4928  {
4929  case '\b':
4930  c = 'b';
4931  break;
4932  case '\f':
4933  c = 'f';
4934  break;
4935  case '\n':
4936  c = 'n';
4937  break;
4938  case '\r':
4939  c = 'r';
4940  break;
4941  case '\t':
4942  c = 't';
4943  break;
4944  case '\v':
4945  c = 'v';
4946  break;
4947  default:
4948  /* If it's the delimiter, must backslash it */
4949  if (c == delimc)
4950  break;
4951  /* All ASCII control chars are length 1 */
4952  ptr++;
4953  continue; /* fall to end of loop */
4954  }
4955  /* if we get here, we need to convert the control char */
4956  DUMPSOFAR();
4957  CopySendChar(cstate, '\\');
4958  CopySendChar(cstate, c);
4959  start = ++ptr; /* do not include char in next run */
4960  }
4961  else if (c == '\\' || c == delimc)
4962  {
4963  DUMPSOFAR();
4964  CopySendChar(cstate, '\\');
4965  start = ptr++; /* we include char in next run */
4966  }
4967  else
4968  ptr++;
4969  }
4970  }
4971 
4972  DUMPSOFAR();
4973 }
4974 
4975 /*
4976  * Send text representation of one attribute, with conversion and
4977  * CSV-style escaping
4978  */
4979 static void
4980 CopyAttributeOutCSV(CopyState cstate, char *string,
4981  bool use_quote, bool single_attr)
4982 {
4983  char *ptr;
4984  char *start;
4985  char c;
4986  char delimc = cstate->delim[0];
4987  char quotec = cstate->quote[0];
4988  char escapec = cstate->escape[0];
4989 
4990  /* force quoting if it matches null_print (before conversion!) */
4991  if (!use_quote && strcmp(string, cstate->null_print) == 0)
4992  use_quote = true;
4993 
4994  if (cstate->need_transcoding)
4995  ptr = pg_server_to_any(string, strlen(string), cstate->file_encoding);
4996  else
4997  ptr = string;
4998 
4999  /*
5000  * Make a preliminary pass to discover if it needs quoting
5001  */
5002  if (!use_quote)
5003  {
5004  /*
5005  * Because '\.' can be a data value, quote it if it appears alone on a
5006  * line so it is not interpreted as the end-of-data marker.
5007  */
5008  if (single_attr && strcmp(ptr, "\\.") == 0)
5009  use_quote = true;
5010  else
5011  {
5012  char *tptr = ptr;
5013 
5014  while ((c = *tptr) != '\0')
5015  {
5016  if (c == delimc || c == quotec || c == '\n' || c == '\r')
5017  {
5018  use_quote = true;
5019  break;
5020  }
5021  if (IS_HIGHBIT_SET(c) && cstate->encoding_embeds_ascii)
5022  tptr += pg_encoding_mblen(cstate->file_encoding, tptr);
5023  else
5024  tptr++;
5025  }
5026  }
5027  }
5028 
5029  if (use_quote)
5030  {
5031  CopySendChar(cstate, quotec);
5032 
5033  /*
5034  * We adopt the same optimization strategy as in CopyAttributeOutText
5035  */
5036  start = ptr;
5037  while ((c = *ptr) != '\0')
5038  {
5039  if (c == quotec || c == escapec)
5040  {
5041  DUMPSOFAR();
5042  CopySendChar(cstate, escapec);
5043  start = ptr; /* we include char in next run */
5044  }
5045  if (IS_HIGHBIT_SET(c) && cstate->encoding_embeds_ascii)
5046  ptr += pg_encoding_mblen(cstate->file_encoding, ptr);
5047  else
5048  ptr++;
5049  }
5050  DUMPSOFAR();
5051 
5052  CopySendChar(cstate, quotec);
5053  }
5054  else
5055  {
5056  /* If it doesn't need quoting, we can just dump it as-is */
5057  CopySendString(cstate, ptr);
5058  }
5059 }
5060 
5061 /*
5062  * CopyGetAttnums - build an integer list of attnums to be copied
5063  *
5064  * The input attnamelist is either the user-specified column list,
5065  * or NIL if there was none (in which case we want all the non-dropped
5066  * columns).
5067  *
5068  * We don't include generated columns in the generated full list and we don't
5069  * allow them to be specified explicitly. They don't make sense for COPY
5070  * FROM, but we could possibly allow them for COPY TO. But this way it's at
5071  * least ensured that whatever we copy out can be copied back in.
5072  *
5073  * rel can be NULL ... it's only used for error reports.
5074  */
5075 static List *
5076 CopyGetAttnums(TupleDesc tupDesc, Relation rel, List *attnamelist)
5077 {
5078  List *attnums = NIL;
5079 
5080  if (attnamelist == NIL)
5081  {
5082  /* Generate default column list */
5083  int attr_count = tupDesc->natts;
5084  int i;
5085 
5086  for (i = 0; i < attr_count; i++)
5087  {
5088  if (TupleDescAttr(tupDesc, i)->attisdropped)
5089  continue;
5090  if (TupleDescAttr(tupDesc, i)->attgenerated)
5091  continue;
5092  attnums = lappend_int(attnums, i + 1);
5093  }
5094  }
5095  else
5096  {
5097  /* Validate the user-supplied list and extract attnums */
5098  ListCell *l;
5099 
5100  foreach(l, attnamelist)
5101  {
5102  char *name = strVal(lfirst(l));
5103  int attnum;
5104  int i;
5105 
5106  /* Lookup column name */
5107  attnum = InvalidAttrNumber;
5108  for (i = 0; i < tupDesc->natts; i++)
5109  {
5110  Form_pg_attribute att = TupleDescAttr(tupDesc, i);
5111 
5112  if (att->attisdropped)
5113  continue;
5114  if (namestrcmp(&(att->attname), name) == 0)
5115  {
5116  if (att->attgenerated)
5117  ereport(ERROR,
5118  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
5119  errmsg("column \"%s\" is a generated column",
5120  name),
5121  errdetail("Generated columns cannot be used in COPY.")));
5122  attnum = att->attnum;
5123  break;
5124  }
5125  }
5126  if (attnum == InvalidAttrNumber)
5127  {
5128  if (rel != NULL)
5129  ereport(ERROR,
5130  (errcode(ERRCODE_UNDEFINED_COLUMN),
5131  errmsg("column \"%s\" of relation \"%s\" does not exist",
5132  name, RelationGetRelationName(rel))));
5133  else
5134  ereport(ERROR,
5135  (errcode(ERRCODE_UNDEFINED_COLUMN),
5136  errmsg("column \"%s\" does not exist",
5137  name)));
5138  }
5139  /* Check for duplicates */
5140  if (list_member_int(attnums, attnum))
5141  ereport(ERROR,
5142  (errcode(ERRCODE_DUPLICATE_COLUMN),
5143  errmsg("column \"%s\" specified more than once",
5144  name)));
5145  attnums = lappend_int(attnums, attnum);
5146  }
5147  }
5148 
5149  return attnums;
5150 }
5151 
5152 
5153 /*
5154  * copy_dest_startup --- executor startup
5155  */
5156 static void
5157 copy_dest_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
5158 {
5159  /* no-op */
5160 }
5161 
5162 /*
5163  * copy_dest_receive --- receive one tuple
5164  */
5165 static bool
5167 {
5168  DR_copy *myState = (DR_copy *) self;
5169  CopyState cstate = myState->cstate;
5170 
5171  /* Send the data */
5172  CopyOneRowTo(cstate, slot);
5173  myState->processed++;
5174 
5175  return true;
5176 }
5177 
5178 /*
5179  * copy_dest_shutdown --- executor end
5180  */
5181 static void
5183 {
5184  /* no-op */
5185 }
5186 
5187 /*
5188  * copy_dest_destroy --- release DestReceiver object
5189  */
5190 static void
5192 {
5193  pfree(self);
5194 }
5195 
5196 /*
5197  * CreateCopyDestReceiver -- create a suitable DestReceiver object
5198  */
5199 DestReceiver *
5201 {
5202  DR_copy *self = (DR_copy *) palloc(sizeof(DR_copy));
5203 
5204  self->pub.receiveSlot = copy_dest_receive;
5205  self->pub.rStartup = copy_dest_startup;
5206  self->pub.rShutdown = copy_dest_shutdown;
5207  self->pub.rDestroy = copy_dest_destroy;
5208  self->pub.mydest = DestCopyOut;
5209 
5210  self->cstate = NULL; /* will be set later */
5211  self->processed = 0;
5212 
5213  return (DestReceiver *) self;
5214 }
TupleTableSlot * table_slot_create(Relation relation, List **reglist)
Definition: tableam.c:77
struct CopyMultiInsertBuffer CopyMultiInsertBuffer
signed short int16
Definition: c.h:346
List * indirection
Definition: parsenodes.h:441
bool NextCopyFromRawFields(CopyState cstate, char ***fields, int *nfields)
Definition: copy.c:3634
int ri_NumIndices
Definition: execnodes.h:413
Node * whereClause
Definition: copy.c:155
static TupleTableSlot * ExecCopySlot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
Definition: tuptable.h:475
#define NIL
Definition: pg_list.h:65
Node * whereClause
Definition: parsenodes.h:2016
Oid tts_tableOid
Definition: tuptable.h:131
static void pq_sendint16(StringInfo buf, uint16 i)
Definition: pqformat.h:137
uint32 CommandId
Definition: c.h:528
void ExecInitRangeTable(EState *estate, List *rangeTable)
Definition: execUtils.c:724
static Datum CopyReadBinaryAttribute(CopyState cstate, int column_no, FmgrInfo *flinfo, Oid typioparam, int32 typmod, bool *isnull)
Definition: copy.c:4768
static int GetDecimalFromHex(char hex)
Definition: copy.c:4343
Definition: fmgr.h:56
#define MAX_COPY_DATA_DISPLAY
bool csv_mode
Definition: copy.c:137
static void SendCopyEnd(CopyState cstate)
Definition: copy.c:470
void InitResultRelInfo(ResultRelInfo *resultRelInfo, Relation resultRelationDesc, Index resultRelationIndex, Relation partition_root, int instrument_options)
Definition: execMain.c:1277
Relation ri_RelationDesc
Definition: execnodes.h:410
List * range_table
Definition: copy.c:183
static CopyState BeginCopy(ParseState *pstate, bool is_from, Relation rel, RawStmt *raw_query, Oid queryRelId, List *attnamelist, List *options)
Definition: copy.c:1461
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:783
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
static bool CopyReadLineText(CopyState cstate)
Definition: copy.c:3979
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
#define AllocSetContextCreate
Definition: memutils.h:170
#define HOLD_CANCEL_INTERRUPTS()
Definition: miscadmin.h:124
static void CopyMultiInsertInfoStore(CopyMultiInsertInfo *miinfo, ResultRelInfo *rri, TupleTableSlot *slot, int tuplen, uint64 lineno)
Definition: copy.c:2633
bool contain_volatile_functions_not_nextval(Node *clause)
Definition: clauses.c:773
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
Node * val
Definition: parsenodes.h:442
static bool CopyGetInt32(CopyState cstate, int32 *val)
Definition: copy.c:738
int errhint(const char *fmt,...)
Definition: elog.c:1069
void ExecARInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot, List *recheckIndexes, TransitionCaptureState *transition_capture)
Definition: trigger.c:2592
EState * estate
Definition: copy.c:278
int pg_char_to_encoding(const char *name)
Definition: encnames.c:550
char ** raw_fields
Definition: copy.c:201
Definition: copy.c:82
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2674
#define VARDATA(PTR)
Definition: postgres.h:302
static void EndCopy(CopyState cstate)
Definition: copy.c:1802
bool binary
Definition: copy.c:135
struct CopyMultiInsertBuffer * ri_CopyMultiInsertBuffer
Definition: execnodes.h:487
#define pq_flush()
Definition: libpq.h:39
void CopyFromErrorCallback(void *arg)
Definition: copy.c:2237
List * attlist
Definition: parsenodes.h:2010
List * fromClause
Definition: parsenodes.h:1596
#define ISOCTAL(c)
Definition: copy.c:61
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
#define OCTVALUE(c)
Definition: copy.c:62
#define ResetPerTupleExprContext(estate)
Definition: executor.h:510
#define RelationGetDescr(relation)
Definition: rel.h:454
int LOCKMODE
Definition: lockdefs.h:26
char * name
Definition: parsenodes.h:440
Oid GetUserId(void)
Definition: miscinit.c:380
#define TABLE_INSERT_FROZEN
Definition: tableam.h:132
bool need_transcoding
Definition: copy.c:125
#define castNode(_type_, nodeptr)
Definition: nodes.h:594
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:105
FmgrInfo * in_functions
Definition: copy.c:178
AttrNumber num_defaults
Definition: copy.c:177
List * attnumlist
Definition: copy.c:131
#define VARSIZE(PTR)
Definition: postgres.h:303
const char * pq_getmsgstring(StringInfo msg)
Definition: pqformat.c:581
char * filename
Definition: copy.c:132
BeginForeignInsert_function BeginForeignInsert
Definition: fdwapi.h:215
ResultRelInfo * resultRelInfo
Definition: execnodes.h:1168
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
struct CopyMultiInsertInfo CopyMultiInsertInfo
CopyState BeginCopyFrom(ParseState *pstate, Relation rel, const char *filename, bool is_program, copy_data_source_cb data_source_cb, List *attnamelist, List *options)
Definition: copy.c:3385
#define VARHDRSZ
Definition: c.h:562
ExecForeignInsert_function ExecForeignInsert
Definition: fdwapi.h:211
struct PartitionRoutingInfo * ri_PartitionInfo
Definition: execnodes.h:484
List * relationOids
Definition: plannodes.h:86
char * pstrdup(const char *in)
Definition: mcxt.c:1186
#define pg_hton16(x)
Definition: pg_bswap.h:120
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:231
static void ReceiveCopyBegin(CopyState cstate)
Definition: copy.c:434
#define XLogIsNeeded()
Definition: xlog.h:181
#define pg_ntoh16(x)
Definition: pg_bswap.h:124
Definition: copy.c:228
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
static void CopyMultiInsertInfoSetupBuffer(CopyMultiInsertInfo *miinfo, ResultRelInfo *rri)
Definition: copy.c:2364
#define MAX_BUFFERED_TUPLES
Definition: copy.c:245
bool rd_islocaltemp
Definition: rel.h:60
CommandId mycid
Definition: copy.c:279
#define S_IWOTH
Definition: win32_port.h:287
Expr * expression_planner(Expr *expr)
Definition: planner.c:6051
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:143
uint64 linenos[MAX_BUFFERED_TUPLES]
Definition: copy.c:263
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:145
DestReceiver pub
Definition: copy.c:230
void ProcessCopyOptions(ParseState *pstate, CopyState cstate, bool is_from, List *options)
Definition: copy.c:1110
void ExecConstraints(ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate)
Definition: execMain.c:1896
StringInfoData line_buf
Definition: copy.c:210
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define IF_NEED_REFILL_AND_EOF_BREAK(extralen)
Definition: copy.c:312
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:841
#define AccessShareLock
Definition: lockdefs.h:36
static struct @145 value
void addNSItemToQuery(ParseState *pstate, ParseNamespaceItem *nsitem, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
struct CopyStateData CopyStateData
Definition: nodes.h:525
#define strVal(v)
Definition: value.h:54
struct cursor * cur
Definition: ecpg.c:28
int raw_buf_index
Definition: copy.c:223
static void CopyAttributeOutText(CopyState cstate, char *string)
Definition: copy.c:4827
bool line_buf_valid
Definition: copy.c:212
bool ThereAreNoPriorRegisteredSnapshots(void)
Definition: snapmgr.c:1689
CopyState cstate
Definition: copy.c:231
int errcode(int sqlerrcode)
Definition: elog.c:608
#define PG_BINARY_W
Definition: c.h:1224
int namestrcmp(Name name, const char *str)
Definition: name.c:287
#define MemSet(start, val, len)
Definition: c.h:962
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition: clauses.c:2252
uint64 CopyFrom(CopyState cstate)
Definition: copy.c:2656
CmdType operation
Definition: execnodes.h:1160
SubTransactionId rd_newRelfilenodeSubid
Definition: rel.h:81
void pq_putemptymessage(char msgtype)
Definition: pqformat.c:390
static void table_finish_bulk_insert(Relation rel, int options)
Definition: tableam.h:1336
Datum * tts_values
Definition: tuptable.h:126
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
static void ClosePipeToProgram(CopyState cstate)
Definition: copy.c:1767
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
static int CopyReadAttributesCSV(CopyState cstate)
Definition: copy.c:4599
void PopActiveSnapshot(void)
Definition: snapmgr.c:814
AclMode requiredPerms
Definition: parsenodes.h:1117
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:723
#define SIGPIPE
Definition: win32_port.h:159
EState * state
Definition: execnodes.h:941
bool * force_quote_flags
Definition: copy.c:147
static bool table_scan_getnextslot(TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot)
Definition: tableam.h:890
Form_pg_class rd_rel
Definition: rel.h:84
unsigned int Oid
Definition: postgres_ext.h:31
List * pg_analyze_and_rewrite(RawStmt *parsetree, const char *query_string, Oid *paramTypes, int numParams, QueryEnvironment *queryEnv)
Definition: postgres.c:676
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:365
char * delim
Definition: copy.c:142
#define PG_PROTOCOL_MAJOR(v)
Definition: pqcomm.h:104
Node * utilityStmt
Definition: parsenodes.h:120
bool is_program
Definition: parsenodes.h:2013
#define linitial_node(type, l)
Definition: pg_list.h:198
bool volatile_defexprs
Definition: copy.c:182
void(* callback)(void *arg)
Definition: elog.h:256
struct ErrorContextCallback * previous
Definition: elog.h:255
static void CopyMultiInsertInfoInit(CopyMultiInsertInfo *miinfo, ResultRelInfo *rri, CopyState cstate, EState *estate, CommandId mycid, int ti_options)
Definition: copy.c:2384
#define PG_BINARY_R
Definition: c.h:1223
ResultRelInfo * resultRelInfo
Definition: copy.c:260
static void copy_dest_destroy(DestReceiver *self)
Definition: copy.c:5191
bool * force_null_flags
Definition: copy.c:151
char * wait_result_to_str(int exitstatus)
Definition: wait_error.c:32
MemoryContext rowcontext
Definition: copy.c:172
bool line_buf_converted
Definition: copy.c:211
static void table_tuple_insert(Relation rel, TupleTableSlot *slot, CommandId cid, int options, struct BulkInsertStateData *bistate)
Definition: tableam.h:1141
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:87
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:207
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:654
signed int int32
Definition: c.h:347
int ClosePipeStream(FILE *file)
Definition: fd.c:2651
int errdetail_internal(const char *fmt,...)
Definition: elog.c:982
bool * convert_select_flags
Definition: copy.c:154
static void CopySendChar(CopyState cstate, char c)
Definition: copy.c:511
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1575
void ExecOpenIndices(ResultRelInfo *resultRelInfo, bool speculative)
Definition: execIndexing.c:151
ParseNamespaceItem * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
void ExecComputeStoredGenerated(EState *estate, TupleTableSlot *slot)
int location
Definition: parsenodes.h:236
CopyDest copy_dest
Definition: copy.c:116
int location
Definition: parsenodes.h:443
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:161
#define REFILL_LINEBUF
Definition: copy.c:329
ErrorContextCallback * error_context_stack
Definition: elog.c:91
uint64 cur_lineno
Definition: copy.c:159
#define list_make1(x1)
Definition: pg_list.h:227
char * null_print
Definition: copy.c:139
const char * cur_attname
Definition: copy.c:160
void assign_expr_collations(ParseState *pstate, Node *expr)
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:462
Definition: copy.c:70