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