PostgreSQL Source Code  git master
copyfromparse.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * copyfromparse.c
4  * Parse CSV/text/binary format for COPY FROM.
5  *
6  * This file contains routines to parse the text, CSV and binary input
7  * formats. The main entry point is NextCopyFrom(), which parses the
8  * next input line and returns it as Datums.
9  *
10  * In text/CSV mode, the parsing happens in multiple stages:
11  *
12  * [data source] --> raw_buf --> input_buf --> line_buf --> attribute_buf
13  * 1. 2. 3. 4.
14  *
15  * 1. CopyLoadRawBuf() reads raw data from the input file or client, and
16  * places it into 'raw_buf'.
17  *
18  * 2. CopyConvertBuf() calls the encoding conversion function to convert
19  * the data in 'raw_buf' from client to server encoding, placing the
20  * converted result in 'input_buf'.
21  *
22  * 3. CopyReadLine() parses the data in 'input_buf', one line at a time.
23  * It is responsible for finding the next newline marker, taking quote and
24  * escape characters into account according to the COPY options. The line
25  * is copied into 'line_buf', with quotes and escape characters still
26  * intact.
27  *
28  * 4. CopyReadAttributesText/CSV() function takes the input line from
29  * 'line_buf', and splits it into fields, unescaping the data as required.
30  * The fields are stored in 'attribute_buf', and 'raw_fields' array holds
31  * pointers to each field.
32  *
33  * If encoding conversion is not required, a shortcut is taken in step 2 to
34  * avoid copying the data unnecessarily. The 'input_buf' pointer is set to
35  * point directly to 'raw_buf', so that CopyLoadRawBuf() loads the raw data
36  * directly into 'input_buf'. CopyConvertBuf() then merely validates that
37  * the data is valid in the current encoding.
38  *
39  * In binary mode, the pipeline is much simpler. Input is loaded into
40  * 'raw_buf', and encoding conversion is done in the datatype-specific
41  * receive functions, if required. 'input_buf' and 'line_buf' are not used,
42  * but 'attribute_buf' is used as a temporary buffer to hold one attribute's
43  * data when it's passed the receive function.
44  *
45  * 'raw_buf' is always 64 kB in size (RAW_BUF_SIZE). 'input_buf' is also
46  * 64 kB (INPUT_BUF_SIZE), if encoding conversion is required. 'line_buf'
47  * and 'attribute_buf' are expanded on demand, to hold the longest line
48  * encountered so far.
49  *
50  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
51  * Portions Copyright (c) 1994, Regents of the University of California
52  *
53  *
54  * IDENTIFICATION
55  * src/backend/commands/copyfromparse.c
56  *
57  *-------------------------------------------------------------------------
58  */
59 #include "postgres.h"
60 
61 #include <ctype.h>
62 #include <unistd.h>
63 #include <sys/stat.h>
64 
65 #include "commands/copy.h"
67 #include "commands/progress.h"
68 #include "executor/executor.h"
69 #include "libpq/libpq.h"
70 #include "libpq/pqformat.h"
71 #include "mb/pg_wchar.h"
72 #include "miscadmin.h"
73 #include "pgstat.h"
74 #include "port/pg_bswap.h"
75 #include "utils/builtins.h"
76 #include "utils/memutils.h"
77 #include "utils/rel.h"
78 
79 #define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
80 #define OCTVALUE(c) ((c) - '0')
81 
82 /*
83  * These macros centralize code used to process line_buf and input_buf buffers.
84  * They are macros because they often do continue/break control and to avoid
85  * function call overhead in tight COPY loops.
86  *
87  * We must use "if (1)" because the usual "do {...} while(0)" wrapper would
88  * prevent the continue/break processing from working. We end the "if (1)"
89  * with "else ((void) 0)" to ensure the "if" does not unintentionally match
90  * any "else" in the calling code, and to avoid any compiler warnings about
91  * empty statements. See http://www.cit.gu.edu.au/~anthony/info/C/C.macros.
92  */
93 
94 /*
95  * This keeps the character read at the top of the loop in the buffer
96  * even if there is more than one read-ahead.
97  */
98 #define IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(extralen) \
99 if (1) \
100 { \
101  if (input_buf_ptr + (extralen) >= copy_buf_len && !hit_eof) \
102  { \
103  input_buf_ptr = prev_raw_ptr; /* undo fetch */ \
104  need_data = true; \
105  continue; \
106  } \
107 } else ((void) 0)
108 
109 /* This consumes the remainder of the buffer and breaks */
110 #define IF_NEED_REFILL_AND_EOF_BREAK(extralen) \
111 if (1) \
112 { \
113  if (input_buf_ptr + (extralen) >= copy_buf_len && hit_eof) \
114  { \
115  if (extralen) \
116  input_buf_ptr = copy_buf_len; /* consume the partial character */ \
117  /* backslash just before EOF, treat as data char */ \
118  result = true; \
119  break; \
120  } \
121 } else ((void) 0)
122 
123 /*
124  * Transfer any approved data to line_buf; must do this to be sure
125  * there is some room in input_buf.
126  */
127 #define REFILL_LINEBUF \
128 if (1) \
129 { \
130  if (input_buf_ptr > cstate->input_buf_index) \
131  { \
132  appendBinaryStringInfo(&cstate->line_buf, \
133  cstate->input_buf + cstate->input_buf_index, \
134  input_buf_ptr - cstate->input_buf_index); \
135  cstate->input_buf_index = input_buf_ptr; \
136  } \
137 } else ((void) 0)
138 
139 /* Undo any read-ahead and jump out of the block. */
140 #define NO_END_OF_COPY_GOTO \
141 if (1) \
142 { \
143  input_buf_ptr = prev_raw_ptr + 1; \
144  goto not_end_of_copy; \
145 } else ((void) 0)
146 
147 /* NOTE: there's a copy of this in copyto.c */
148 static const char BinarySignature[11] = "PGCOPY\n\377\r\n\0";
149 
150 
151 /* non-export function prototypes */
152 static bool CopyReadLine(CopyFromState cstate);
153 static bool CopyReadLineText(CopyFromState cstate);
154 static int CopyReadAttributesText(CopyFromState cstate);
155 static int CopyReadAttributesCSV(CopyFromState cstate);
156 static Datum CopyReadBinaryAttribute(CopyFromState cstate, FmgrInfo *flinfo,
157  Oid typioparam, int32 typmod,
158  bool *isnull);
159 
160 
161 /* Low-level communications functions */
162 static int CopyGetData(CopyFromState cstate, void *databuf,
163  int minread, int maxread);
164 static inline bool CopyGetInt32(CopyFromState cstate, int32 *val);
165 static inline bool CopyGetInt16(CopyFromState cstate, int16 *val);
166 static void CopyLoadInputBuf(CopyFromState cstate);
167 static int CopyReadBinaryData(CopyFromState cstate, char *dest, int nbytes);
168 
169 void
171 {
173  int natts = list_length(cstate->attnumlist);
174  int16 format = (cstate->opts.binary ? 1 : 0);
175  int i;
176 
178  pq_sendbyte(&buf, format); /* overall format */
179  pq_sendint16(&buf, natts);
180  for (i = 0; i < natts; i++)
181  pq_sendint16(&buf, format); /* per-column formats */
182  pq_endmessage(&buf);
183  cstate->copy_src = COPY_FRONTEND;
184  cstate->fe_msgbuf = makeStringInfo();
185  /* We *must* flush here to ensure FE knows it can send. */
186  pq_flush();
187 }
188 
189 void
191 {
192  char readSig[11];
193  int32 tmp;
194 
195  /* Signature */
196  if (CopyReadBinaryData(cstate, readSig, 11) != 11 ||
197  memcmp(readSig, BinarySignature, 11) != 0)
198  ereport(ERROR,
199  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
200  errmsg("COPY file signature not recognized")));
201  /* Flags field */
202  if (!CopyGetInt32(cstate, &tmp))
203  ereport(ERROR,
204  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
205  errmsg("invalid COPY file header (missing flags)")));
206  if ((tmp & (1 << 16)) != 0)
207  ereport(ERROR,
208  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
209  errmsg("invalid COPY file header (WITH OIDS)")));
210  tmp &= ~(1 << 16);
211  if ((tmp >> 16) != 0)
212  ereport(ERROR,
213  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
214  errmsg("unrecognized critical flags in COPY file header")));
215  /* Header extension length */
216  if (!CopyGetInt32(cstate, &tmp) ||
217  tmp < 0)
218  ereport(ERROR,
219  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
220  errmsg("invalid COPY file header (missing length)")));
221  /* Skip extension header, if present */
222  while (tmp-- > 0)
223  {
224  if (CopyReadBinaryData(cstate, readSig, 1) != 1)
225  ereport(ERROR,
226  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
227  errmsg("invalid COPY file header (wrong length)")));
228  }
229 }
230 
231 /*
232  * CopyGetData reads data from the source (file or frontend)
233  *
234  * We attempt to read at least minread, and at most maxread, bytes from
235  * the source. The actual number of bytes read is returned; if this is
236  * less than minread, EOF was detected.
237  *
238  * Note: when copying from the frontend, we expect a proper EOF mark per
239  * protocol; if the frontend simply drops the connection, we raise error.
240  * It seems unwise to allow the COPY IN to complete normally in that case.
241  *
242  * NB: no data conversion is applied here.
243  */
244 static int
245 CopyGetData(CopyFromState cstate, void *databuf, int minread, int maxread)
246 {
247  int bytesread = 0;
248 
249  switch (cstate->copy_src)
250  {
251  case COPY_FILE:
252  bytesread = fread(databuf, 1, maxread, cstate->copy_file);
253  if (ferror(cstate->copy_file))
254  ereport(ERROR,
256  errmsg("could not read from COPY file: %m")));
257  if (bytesread == 0)
258  cstate->raw_reached_eof = true;
259  break;
260  case COPY_FRONTEND:
261  while (maxread > 0 && bytesread < minread && !cstate->raw_reached_eof)
262  {
263  int avail;
264 
265  while (cstate->fe_msgbuf->cursor >= cstate->fe_msgbuf->len)
266  {
267  /* Try to receive another message */
268  int mtype;
269  int maxmsglen;
270 
271  readmessage:
273  pq_startmsgread();
274  mtype = pq_getbyte();
275  if (mtype == EOF)
276  ereport(ERROR,
277  (errcode(ERRCODE_CONNECTION_FAILURE),
278  errmsg("unexpected EOF on client connection with an open transaction")));
279  /* Validate message type and set packet size limit */
280  switch (mtype)
281  {
282  case PqMsg_CopyData:
283  maxmsglen = PQ_LARGE_MESSAGE_LIMIT;
284  break;
285  case PqMsg_CopyDone:
286  case PqMsg_CopyFail:
287  case PqMsg_Flush:
288  case PqMsg_Sync:
289  maxmsglen = PQ_SMALL_MESSAGE_LIMIT;
290  break;
291  default:
292  ereport(ERROR,
293  (errcode(ERRCODE_PROTOCOL_VIOLATION),
294  errmsg("unexpected message type 0x%02X during COPY from stdin",
295  mtype)));
296  maxmsglen = 0; /* keep compiler quiet */
297  break;
298  }
299  /* Now collect the message body */
300  if (pq_getmessage(cstate->fe_msgbuf, maxmsglen))
301  ereport(ERROR,
302  (errcode(ERRCODE_CONNECTION_FAILURE),
303  errmsg("unexpected EOF on client connection with an open transaction")));
305  /* ... and process it */
306  switch (mtype)
307  {
308  case PqMsg_CopyData:
309  break;
310  case PqMsg_CopyDone:
311  /* COPY IN correctly terminated by frontend */
312  cstate->raw_reached_eof = true;
313  return bytesread;
314  case PqMsg_CopyFail:
315  ereport(ERROR,
316  (errcode(ERRCODE_QUERY_CANCELED),
317  errmsg("COPY from stdin failed: %s",
318  pq_getmsgstring(cstate->fe_msgbuf))));
319  break;
320  case PqMsg_Flush:
321  case PqMsg_Sync:
322 
323  /*
324  * Ignore Flush/Sync for the convenience of client
325  * libraries (such as libpq) that may send those
326  * without noticing that the command they just
327  * sent was COPY.
328  */
329  goto readmessage;
330  default:
331  Assert(false); /* NOT REACHED */
332  }
333  }
334  avail = cstate->fe_msgbuf->len - cstate->fe_msgbuf->cursor;
335  if (avail > maxread)
336  avail = maxread;
337  pq_copymsgbytes(cstate->fe_msgbuf, databuf, avail);
338  databuf = (void *) ((char *) databuf + avail);
339  maxread -= avail;
340  bytesread += avail;
341  }
342  break;
343  case COPY_CALLBACK:
344  bytesread = cstate->data_source_cb(databuf, minread, maxread);
345  break;
346  }
347 
348  return bytesread;
349 }
350 
351 
352 /*
353  * These functions do apply some data conversion
354  */
355 
356 /*
357  * CopyGetInt32 reads an int32 that appears in network byte order
358  *
359  * Returns true if OK, false if EOF
360  */
361 static inline bool
363 {
364  uint32 buf;
365 
366  if (CopyReadBinaryData(cstate, (char *) &buf, sizeof(buf)) != sizeof(buf))
367  {
368  *val = 0; /* suppress compiler warning */
369  return false;
370  }
371  *val = (int32) pg_ntoh32(buf);
372  return true;
373 }
374 
375 /*
376  * CopyGetInt16 reads an int16 that appears in network byte order
377  */
378 static inline bool
380 {
381  uint16 buf;
382 
383  if (CopyReadBinaryData(cstate, (char *) &buf, sizeof(buf)) != sizeof(buf))
384  {
385  *val = 0; /* suppress compiler warning */
386  return false;
387  }
388  *val = (int16) pg_ntoh16(buf);
389  return true;
390 }
391 
392 
393 /*
394  * Perform encoding conversion on data in 'raw_buf', writing the converted
395  * data into 'input_buf'.
396  *
397  * On entry, there must be some data to convert in 'raw_buf'.
398  */
399 static void
401 {
402  /*
403  * If the file and server encoding are the same, no encoding conversion is
404  * required. However, we still need to verify that the input is valid for
405  * the encoding.
406  */
407  if (!cstate->need_transcoding)
408  {
409  /*
410  * When conversion is not required, input_buf and raw_buf are the
411  * same. raw_buf_len is the total number of bytes in the buffer, and
412  * input_buf_len tracks how many of those bytes have already been
413  * verified.
414  */
415  int preverifiedlen = cstate->input_buf_len;
416  int unverifiedlen = cstate->raw_buf_len - cstate->input_buf_len;
417  int nverified;
418 
419  if (unverifiedlen == 0)
420  {
421  /*
422  * If no more raw data is coming, report the EOF to the caller.
423  */
424  if (cstate->raw_reached_eof)
425  cstate->input_reached_eof = true;
426  return;
427  }
428 
429  /*
430  * Verify the new data, including any residual unverified bytes from
431  * previous round.
432  */
433  nverified = pg_encoding_verifymbstr(cstate->file_encoding,
434  cstate->raw_buf + preverifiedlen,
435  unverifiedlen);
436  if (nverified == 0)
437  {
438  /*
439  * Could not verify anything.
440  *
441  * If there is no more raw input data coming, it means that there
442  * was an incomplete multi-byte sequence at the end. Also, if
443  * there's "enough" input left, we should be able to verify at
444  * least one character, and a failure to do so means that we've
445  * hit an invalid byte sequence.
446  */
447  if (cstate->raw_reached_eof || unverifiedlen >= pg_encoding_max_length(cstate->file_encoding))
448  cstate->input_reached_error = true;
449  return;
450  }
451  cstate->input_buf_len += nverified;
452  }
453  else
454  {
455  /*
456  * Encoding conversion is needed.
457  */
458  int nbytes;
459  unsigned char *src;
460  int srclen;
461  unsigned char *dst;
462  int dstlen;
463  int convertedlen;
464 
465  if (RAW_BUF_BYTES(cstate) == 0)
466  {
467  /*
468  * If no more raw data is coming, report the EOF to the caller.
469  */
470  if (cstate->raw_reached_eof)
471  cstate->input_reached_eof = true;
472  return;
473  }
474 
475  /*
476  * First, copy down any unprocessed data.
477  */
478  nbytes = INPUT_BUF_BYTES(cstate);
479  if (nbytes > 0 && cstate->input_buf_index > 0)
480  memmove(cstate->input_buf, cstate->input_buf + cstate->input_buf_index,
481  nbytes);
482  cstate->input_buf_index = 0;
483  cstate->input_buf_len = nbytes;
484  cstate->input_buf[nbytes] = '\0';
485 
486  src = (unsigned char *) cstate->raw_buf + cstate->raw_buf_index;
487  srclen = cstate->raw_buf_len - cstate->raw_buf_index;
488  dst = (unsigned char *) cstate->input_buf + cstate->input_buf_len;
489  dstlen = INPUT_BUF_SIZE - cstate->input_buf_len + 1;
490 
491  /*
492  * Do the conversion. This might stop short, if there is an invalid
493  * byte sequence in the input. We'll convert as much as we can in
494  * that case.
495  *
496  * Note: Even if we hit an invalid byte sequence, we don't report the
497  * error until all the valid bytes have been consumed. The input
498  * might contain an end-of-input marker (\.), and we don't want to
499  * report an error if the invalid byte sequence is after the
500  * end-of-input marker. We might unnecessarily convert some data
501  * after the end-of-input marker as long as it's valid for the
502  * encoding, but that's harmless.
503  */
504  convertedlen = pg_do_encoding_conversion_buf(cstate->conversion_proc,
505  cstate->file_encoding,
507  src, srclen,
508  dst, dstlen,
509  true);
510  if (convertedlen == 0)
511  {
512  /*
513  * Could not convert anything. If there is no more raw input data
514  * coming, it means that there was an incomplete multi-byte
515  * sequence at the end. Also, if there is plenty of input left,
516  * we should be able to convert at least one character, so a
517  * failure to do so must mean that we've hit a byte sequence
518  * that's invalid.
519  */
520  if (cstate->raw_reached_eof || srclen >= MAX_CONVERSION_INPUT_LENGTH)
521  cstate->input_reached_error = true;
522  return;
523  }
524  cstate->raw_buf_index += convertedlen;
525  cstate->input_buf_len += strlen((char *) dst);
526  }
527 }
528 
529 /*
530  * Report an encoding or conversion error.
531  */
532 static void
534 {
535  Assert(cstate->raw_buf_len > 0);
536  Assert(cstate->input_reached_error);
537 
538  if (!cstate->need_transcoding)
539  {
540  /*
541  * Everything up to input_buf_len was successfully verified, and
542  * input_buf_len points to the invalid or incomplete character.
543  */
545  cstate->raw_buf + cstate->input_buf_len,
546  cstate->raw_buf_len - cstate->input_buf_len);
547  }
548  else
549  {
550  /*
551  * raw_buf_index points to the invalid or untranslatable character. We
552  * let the conversion routine report the error, because it can provide
553  * a more specific error message than we could here. An earlier call
554  * to the conversion routine in CopyConvertBuf() detected that there
555  * is an error, now we call the conversion routine again with
556  * noError=false, to have it throw the error.
557  */
558  unsigned char *src;
559  int srclen;
560  unsigned char *dst;
561  int dstlen;
562 
563  src = (unsigned char *) cstate->raw_buf + cstate->raw_buf_index;
564  srclen = cstate->raw_buf_len - cstate->raw_buf_index;
565  dst = (unsigned char *) cstate->input_buf + cstate->input_buf_len;
566  dstlen = INPUT_BUF_SIZE - cstate->input_buf_len + 1;
567 
569  cstate->file_encoding,
571  src, srclen,
572  dst, dstlen,
573  false);
574 
575  /*
576  * The conversion routine should have reported an error, so this
577  * should not be reached.
578  */
579  elog(ERROR, "encoding conversion failed without error");
580  }
581 }
582 
583 /*
584  * Load more data from data source to raw_buf.
585  *
586  * If RAW_BUF_BYTES(cstate) > 0, the unprocessed bytes are moved to the
587  * beginning of the buffer, and we load new data after that.
588  */
589 static void
591 {
592  int nbytes;
593  int inbytes;
594 
595  /*
596  * In text mode, if encoding conversion is not required, raw_buf and
597  * input_buf point to the same buffer. Their len/index better agree, too.
598  */
599  if (cstate->raw_buf == cstate->input_buf)
600  {
601  Assert(!cstate->need_transcoding);
602  Assert(cstate->raw_buf_index == cstate->input_buf_index);
603  Assert(cstate->input_buf_len <= cstate->raw_buf_len);
604  }
605 
606  /*
607  * Copy down the unprocessed data if any.
608  */
609  nbytes = RAW_BUF_BYTES(cstate);
610  if (nbytes > 0 && cstate->raw_buf_index > 0)
611  memmove(cstate->raw_buf, cstate->raw_buf + cstate->raw_buf_index,
612  nbytes);
613  cstate->raw_buf_len -= cstate->raw_buf_index;
614  cstate->raw_buf_index = 0;
615 
616  /*
617  * If raw_buf and input_buf are in fact the same buffer, adjust the
618  * input_buf variables, too.
619  */
620  if (cstate->raw_buf == cstate->input_buf)
621  {
622  cstate->input_buf_len -= cstate->input_buf_index;
623  cstate->input_buf_index = 0;
624  }
625 
626  /* Load more data */
627  inbytes = CopyGetData(cstate, cstate->raw_buf + cstate->raw_buf_len,
628  1, RAW_BUF_SIZE - cstate->raw_buf_len);
629  nbytes += inbytes;
630  cstate->raw_buf[nbytes] = '\0';
631  cstate->raw_buf_len = nbytes;
632 
633  cstate->bytes_processed += inbytes;
635 
636  if (inbytes == 0)
637  cstate->raw_reached_eof = true;
638 }
639 
640 /*
641  * CopyLoadInputBuf loads some more data into input_buf
642  *
643  * On return, at least one more input character is loaded into
644  * input_buf, or input_reached_eof is set.
645  *
646  * If INPUT_BUF_BYTES(cstate) > 0, the unprocessed bytes are moved to the start
647  * of the buffer and then we load more data after that.
648  */
649 static void
651 {
652  int nbytes = INPUT_BUF_BYTES(cstate);
653 
654  /*
655  * The caller has updated input_buf_index to indicate how much of the
656  * input has been consumed and isn't needed anymore. If input_buf is the
657  * same physical area as raw_buf, update raw_buf_index accordingly.
658  */
659  if (cstate->raw_buf == cstate->input_buf)
660  {
661  Assert(!cstate->need_transcoding);
662  Assert(cstate->input_buf_index >= cstate->raw_buf_index);
663  cstate->raw_buf_index = cstate->input_buf_index;
664  }
665 
666  for (;;)
667  {
668  /* If we now have some unconverted data, try to convert it */
669  CopyConvertBuf(cstate);
670 
671  /* If we now have some more input bytes ready, return them */
672  if (INPUT_BUF_BYTES(cstate) > nbytes)
673  return;
674 
675  /*
676  * If we reached an invalid byte sequence, or we're at an incomplete
677  * multi-byte character but there is no more raw input data, report
678  * conversion error.
679  */
680  if (cstate->input_reached_error)
681  CopyConversionError(cstate);
682 
683  /* no more input, and everything has been converted */
684  if (cstate->input_reached_eof)
685  break;
686 
687  /* Try to load more raw data */
688  Assert(!cstate->raw_reached_eof);
689  CopyLoadRawBuf(cstate);
690  }
691 }
692 
693 /*
694  * CopyReadBinaryData
695  *
696  * Reads up to 'nbytes' bytes from cstate->copy_file via cstate->raw_buf
697  * and writes them to 'dest'. Returns the number of bytes read (which
698  * would be less than 'nbytes' only if we reach EOF).
699  */
700 static int
701 CopyReadBinaryData(CopyFromState cstate, char *dest, int nbytes)
702 {
703  int copied_bytes = 0;
704 
705  if (RAW_BUF_BYTES(cstate) >= nbytes)
706  {
707  /* Enough bytes are present in the buffer. */
708  memcpy(dest, cstate->raw_buf + cstate->raw_buf_index, nbytes);
709  cstate->raw_buf_index += nbytes;
710  copied_bytes = nbytes;
711  }
712  else
713  {
714  /*
715  * Not enough bytes in the buffer, so must read from the file. Need
716  * to loop since 'nbytes' could be larger than the buffer size.
717  */
718  do
719  {
720  int copy_bytes;
721 
722  /* Load more data if buffer is empty. */
723  if (RAW_BUF_BYTES(cstate) == 0)
724  {
725  CopyLoadRawBuf(cstate);
726  if (cstate->raw_reached_eof)
727  break; /* EOF */
728  }
729 
730  /* Transfer some bytes. */
731  copy_bytes = Min(nbytes - copied_bytes, RAW_BUF_BYTES(cstate));
732  memcpy(dest, cstate->raw_buf + cstate->raw_buf_index, copy_bytes);
733  cstate->raw_buf_index += copy_bytes;
734  dest += copy_bytes;
735  copied_bytes += copy_bytes;
736  } while (copied_bytes < nbytes);
737  }
738 
739  return copied_bytes;
740 }
741 
742 /*
743  * Read raw fields in the next line for COPY FROM in text or csv mode.
744  * Return false if no more lines.
745  *
746  * An internal temporary buffer is returned via 'fields'. It is valid until
747  * the next call of the function. Since the function returns all raw fields
748  * in the input file, 'nfields' could be different from the number of columns
749  * in the relation.
750  *
751  * NOTE: force_not_null option are not applied to the returned fields.
752  */
753 bool
754 NextCopyFromRawFields(CopyFromState cstate, char ***fields, int *nfields)
755 {
756  int fldct;
757  bool done;
758 
759  /* only available for text or csv input */
760  Assert(!cstate->opts.binary);
761 
762  /* on input check that the header line is correct if needed */
763  if (cstate->cur_lineno == 0 && cstate->opts.header_line)
764  {
765  ListCell *cur;
766  TupleDesc tupDesc;
767 
768  tupDesc = RelationGetDescr(cstate->rel);
769 
770  cstate->cur_lineno++;
771  done = CopyReadLine(cstate);
772 
773  if (cstate->opts.header_line == COPY_HEADER_MATCH)
774  {
775  int fldnum;
776 
777  if (cstate->opts.csv_mode)
778  fldct = CopyReadAttributesCSV(cstate);
779  else
780  fldct = CopyReadAttributesText(cstate);
781 
782  if (fldct != list_length(cstate->attnumlist))
783  ereport(ERROR,
784  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
785  errmsg("wrong number of fields in header line: got %d, expected %d",
786  fldct, list_length(cstate->attnumlist))));
787 
788  fldnum = 0;
789  foreach(cur, cstate->attnumlist)
790  {
791  int attnum = lfirst_int(cur);
792  char *colName;
793  Form_pg_attribute attr = TupleDescAttr(tupDesc, attnum - 1);
794 
795  Assert(fldnum < cstate->max_fields);
796 
797  colName = cstate->raw_fields[fldnum++];
798  if (colName == NULL)
799  ereport(ERROR,
800  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
801  errmsg("column name mismatch in header line field %d: got null value (\"%s\"), expected \"%s\"",
802  fldnum, cstate->opts.null_print, NameStr(attr->attname))));
803 
804  if (namestrcmp(&attr->attname, colName) != 0)
805  {
806  ereport(ERROR,
807  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
808  errmsg("column name mismatch in header line field %d: got \"%s\", expected \"%s\"",
809  fldnum, colName, NameStr(attr->attname))));
810  }
811  }
812  }
813 
814  if (done)
815  return false;
816  }
817 
818  cstate->cur_lineno++;
819 
820  /* Actually read the line into memory here */
821  done = CopyReadLine(cstate);
822 
823  /*
824  * EOF at start of line means we're done. If we see EOF after some
825  * characters, we act as though it was newline followed by EOF, ie,
826  * process the line and then exit loop on next iteration.
827  */
828  if (done && cstate->line_buf.len == 0)
829  return false;
830 
831  /* Parse the line into de-escaped field values */
832  if (cstate->opts.csv_mode)
833  fldct = CopyReadAttributesCSV(cstate);
834  else
835  fldct = CopyReadAttributesText(cstate);
836 
837  *fields = cstate->raw_fields;
838  *nfields = fldct;
839  return true;
840 }
841 
842 /*
843  * Read next tuple from file for COPY FROM. Return false if no more tuples.
844  *
845  * 'econtext' is used to evaluate default expression for each column that is
846  * either not read from the file or is using the DEFAULT option of COPY FROM.
847  * It can be NULL when no default values are used, i.e. when all columns are
848  * read from the file, and DEFAULT option is unset.
849  *
850  * 'values' and 'nulls' arrays must be the same length as columns of the
851  * relation passed to BeginCopyFrom. This function fills the arrays.
852  */
853 bool
855  Datum *values, bool *nulls)
856 {
857  TupleDesc tupDesc;
858  AttrNumber num_phys_attrs,
859  attr_count,
860  num_defaults = cstate->num_defaults;
861  FmgrInfo *in_functions = cstate->in_functions;
862  Oid *typioparams = cstate->typioparams;
863  int i;
864  int *defmap = cstate->defmap;
865  ExprState **defexprs = cstate->defexprs;
866 
867  tupDesc = RelationGetDescr(cstate->rel);
868  num_phys_attrs = tupDesc->natts;
869  attr_count = list_length(cstate->attnumlist);
870 
871  /* Initialize all values for row to NULL */
872  MemSet(values, 0, num_phys_attrs * sizeof(Datum));
873  MemSet(nulls, true, num_phys_attrs * sizeof(bool));
874  MemSet(cstate->defaults, false, num_phys_attrs * sizeof(bool));
875 
876  if (!cstate->opts.binary)
877  {
878  char **field_strings;
879  ListCell *cur;
880  int fldct;
881  int fieldno;
882  char *string;
883 
884  /* read raw fields in the next line */
885  if (!NextCopyFromRawFields(cstate, &field_strings, &fldct))
886  return false;
887 
888  /* check for overflowing fields */
889  if (attr_count > 0 && fldct > attr_count)
890  ereport(ERROR,
891  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
892  errmsg("extra data after last expected column")));
893 
894  fieldno = 0;
895 
896  /* Loop to read the user attributes on the line. */
897  foreach(cur, cstate->attnumlist)
898  {
899  int attnum = lfirst_int(cur);
900  int m = attnum - 1;
901  Form_pg_attribute att = TupleDescAttr(tupDesc, m);
902 
903  if (fieldno >= fldct)
904  ereport(ERROR,
905  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
906  errmsg("missing data for column \"%s\"",
907  NameStr(att->attname))));
908  string = field_strings[fieldno++];
909 
910  if (cstate->convert_select_flags &&
911  !cstate->convert_select_flags[m])
912  {
913  /* ignore input field, leaving column as NULL */
914  continue;
915  }
916 
917  if (cstate->opts.csv_mode)
918  {
919  if (string == NULL &&
920  cstate->opts.force_notnull_flags[m])
921  {
922  /*
923  * FORCE_NOT_NULL option is set and column is NULL -
924  * convert it to the NULL string.
925  */
926  string = cstate->opts.null_print;
927  }
928  else if (string != NULL && cstate->opts.force_null_flags[m]
929  && strcmp(string, cstate->opts.null_print) == 0)
930  {
931  /*
932  * FORCE_NULL option is set and column matches the NULL
933  * string. It must have been quoted, or otherwise the
934  * string would already have been set to NULL. Convert it
935  * to NULL as specified.
936  */
937  string = NULL;
938  }
939  }
940 
941  cstate->cur_attname = NameStr(att->attname);
942  cstate->cur_attval = string;
943 
944  if (string != NULL)
945  nulls[m] = false;
946 
947  if (cstate->defaults[m])
948  {
949  /*
950  * The caller must supply econtext and have switched into the
951  * per-tuple memory context in it.
952  */
953  Assert(econtext != NULL);
955 
956  values[m] = ExecEvalExpr(defexprs[m], econtext, &nulls[m]);
957  }
958  else
959  values[m] = InputFunctionCall(&in_functions[m],
960  string,
961  typioparams[m],
962  att->atttypmod);
963 
964  cstate->cur_attname = NULL;
965  cstate->cur_attval = NULL;
966  }
967 
968  Assert(fieldno == attr_count);
969  }
970  else
971  {
972  /* binary */
973  int16 fld_count;
974  ListCell *cur;
975 
976  cstate->cur_lineno++;
977 
978  if (!CopyGetInt16(cstate, &fld_count))
979  {
980  /* EOF detected (end of file, or protocol-level EOF) */
981  return false;
982  }
983 
984  if (fld_count == -1)
985  {
986  /*
987  * Received EOF marker. Wait for the protocol-level EOF, and
988  * complain if it doesn't come immediately. In COPY FROM STDIN,
989  * this ensures that we correctly handle CopyFail, if client
990  * chooses to send that now. When copying from file, we could
991  * ignore the rest of the file like in text mode, but we choose to
992  * be consistent with the COPY FROM STDIN case.
993  */
994  char dummy;
995 
996  if (CopyReadBinaryData(cstate, &dummy, 1) > 0)
997  ereport(ERROR,
998  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
999  errmsg("received copy data after EOF marker")));
1000  return false;
1001  }
1002 
1003  if (fld_count != attr_count)
1004  ereport(ERROR,
1005  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1006  errmsg("row field count is %d, expected %d",
1007  (int) fld_count, attr_count)));
1008 
1009  foreach(cur, cstate->attnumlist)
1010  {
1011  int attnum = lfirst_int(cur);
1012  int m = attnum - 1;
1013  Form_pg_attribute att = TupleDescAttr(tupDesc, m);
1014 
1015  cstate->cur_attname = NameStr(att->attname);
1016  values[m] = CopyReadBinaryAttribute(cstate,
1017  &in_functions[m],
1018  typioparams[m],
1019  att->atttypmod,
1020  &nulls[m]);
1021  cstate->cur_attname = NULL;
1022  }
1023  }
1024 
1025  /*
1026  * Now compute and insert any defaults available for the columns not
1027  * provided by the input data. Anything not processed here or above will
1028  * remain NULL.
1029  */
1030  for (i = 0; i < num_defaults; i++)
1031  {
1032  /*
1033  * The caller must supply econtext and have switched into the
1034  * per-tuple memory context in it.
1035  */
1036  Assert(econtext != NULL);
1038 
1039  values[defmap[i]] = ExecEvalExpr(defexprs[defmap[i]], econtext,
1040  &nulls[defmap[i]]);
1041  }
1042 
1043  return true;
1044 }
1045 
1046 /*
1047  * Read the next input line and stash it in line_buf.
1048  *
1049  * Result is true if read was terminated by EOF, false if terminated
1050  * by newline. The terminating newline or EOF marker is not included
1051  * in the final value of line_buf.
1052  */
1053 static bool
1055 {
1056  bool result;
1057 
1058  resetStringInfo(&cstate->line_buf);
1059  cstate->line_buf_valid = false;
1060 
1061  /* Parse data and transfer into line_buf */
1062  result = CopyReadLineText(cstate);
1063 
1064  if (result)
1065  {
1066  /*
1067  * Reached EOF. In protocol version 3, we should ignore anything
1068  * after \. up to the protocol end of copy data. (XXX maybe better
1069  * not to treat \. as special?)
1070  */
1071  if (cstate->copy_src == COPY_FRONTEND)
1072  {
1073  int inbytes;
1074 
1075  do
1076  {
1077  inbytes = CopyGetData(cstate, cstate->input_buf,
1078  1, INPUT_BUF_SIZE);
1079  } while (inbytes > 0);
1080  cstate->input_buf_index = 0;
1081  cstate->input_buf_len = 0;
1082  cstate->raw_buf_index = 0;
1083  cstate->raw_buf_len = 0;
1084  }
1085  }
1086  else
1087  {
1088  /*
1089  * If we didn't hit EOF, then we must have transferred the EOL marker
1090  * to line_buf along with the data. Get rid of it.
1091  */
1092  switch (cstate->eol_type)
1093  {
1094  case EOL_NL:
1095  Assert(cstate->line_buf.len >= 1);
1096  Assert(cstate->line_buf.data[cstate->line_buf.len - 1] == '\n');
1097  cstate->line_buf.len--;
1098  cstate->line_buf.data[cstate->line_buf.len] = '\0';
1099  break;
1100  case EOL_CR:
1101  Assert(cstate->line_buf.len >= 1);
1102  Assert(cstate->line_buf.data[cstate->line_buf.len - 1] == '\r');
1103  cstate->line_buf.len--;
1104  cstate->line_buf.data[cstate->line_buf.len] = '\0';
1105  break;
1106  case EOL_CRNL:
1107  Assert(cstate->line_buf.len >= 2);
1108  Assert(cstate->line_buf.data[cstate->line_buf.len - 2] == '\r');
1109  Assert(cstate->line_buf.data[cstate->line_buf.len - 1] == '\n');
1110  cstate->line_buf.len -= 2;
1111  cstate->line_buf.data[cstate->line_buf.len] = '\0';
1112  break;
1113  case EOL_UNKNOWN:
1114  /* shouldn't get here */
1115  Assert(false);
1116  break;
1117  }
1118  }
1119 
1120  /* Now it's safe to use the buffer in error messages */
1121  cstate->line_buf_valid = true;
1122 
1123  return result;
1124 }
1125 
1126 /*
1127  * CopyReadLineText - inner loop of CopyReadLine for text mode
1128  */
1129 static bool
1131 {
1132  char *copy_input_buf;
1133  int input_buf_ptr;
1134  int copy_buf_len;
1135  bool need_data = false;
1136  bool hit_eof = false;
1137  bool result = false;
1138 
1139  /* CSV variables */
1140  bool first_char_in_line = true;
1141  bool in_quote = false,
1142  last_was_esc = false;
1143  char quotec = '\0';
1144  char escapec = '\0';
1145 
1146  if (cstate->opts.csv_mode)
1147  {
1148  quotec = cstate->opts.quote[0];
1149  escapec = cstate->opts.escape[0];
1150  /* ignore special escape processing if it's the same as quotec */
1151  if (quotec == escapec)
1152  escapec = '\0';
1153  }
1154 
1155  /*
1156  * The objective of this loop is to transfer the entire next input line
1157  * into line_buf. Hence, we only care for detecting newlines (\r and/or
1158  * \n) and the end-of-copy marker (\.).
1159  *
1160  * In CSV mode, \r and \n inside a quoted field are just part of the data
1161  * value and are put in line_buf. We keep just enough state to know if we
1162  * are currently in a quoted field or not.
1163  *
1164  * These four characters, and the CSV escape and quote characters, are
1165  * assumed the same in frontend and backend encodings.
1166  *
1167  * The input has already been converted to the database encoding. All
1168  * supported server encodings have the property that all bytes in a
1169  * multi-byte sequence have the high bit set, so a multibyte character
1170  * cannot contain any newline or escape characters embedded in the
1171  * multibyte sequence. Therefore, we can process the input byte-by-byte,
1172  * regardless of the encoding.
1173  *
1174  * For speed, we try to move data from input_buf to line_buf in chunks
1175  * rather than one character at a time. input_buf_ptr points to the next
1176  * character to examine; any characters from input_buf_index to
1177  * input_buf_ptr have been determined to be part of the line, but not yet
1178  * transferred to line_buf.
1179  *
1180  * For a little extra speed within the loop, we copy input_buf and
1181  * input_buf_len into local variables.
1182  */
1183  copy_input_buf = cstate->input_buf;
1184  input_buf_ptr = cstate->input_buf_index;
1185  copy_buf_len = cstate->input_buf_len;
1186 
1187  for (;;)
1188  {
1189  int prev_raw_ptr;
1190  char c;
1191 
1192  /*
1193  * Load more data if needed.
1194  *
1195  * TODO: We could just force four bytes of read-ahead and avoid the
1196  * many calls to IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(). That was
1197  * unsafe with the old v2 COPY protocol, but we don't support that
1198  * anymore.
1199  */
1200  if (input_buf_ptr >= copy_buf_len || need_data)
1201  {
1203 
1204  CopyLoadInputBuf(cstate);
1205  /* update our local variables */
1206  hit_eof = cstate->input_reached_eof;
1207  input_buf_ptr = cstate->input_buf_index;
1208  copy_buf_len = cstate->input_buf_len;
1209 
1210  /*
1211  * If we are completely out of data, break out of the loop,
1212  * reporting EOF.
1213  */
1214  if (INPUT_BUF_BYTES(cstate) <= 0)
1215  {
1216  result = true;
1217  break;
1218  }
1219  need_data = false;
1220  }
1221 
1222  /* OK to fetch a character */
1223  prev_raw_ptr = input_buf_ptr;
1224  c = copy_input_buf[input_buf_ptr++];
1225 
1226  if (cstate->opts.csv_mode)
1227  {
1228  /*
1229  * If character is '\\' or '\r', we may need to look ahead below.
1230  * Force fetch of the next character if we don't already have it.
1231  * We need to do this before changing CSV state, in case one of
1232  * these characters is also the quote or escape character.
1233  */
1234  if (c == '\\' || c == '\r')
1235  {
1237  }
1238 
1239  /*
1240  * Dealing with quotes and escapes here is mildly tricky. If the
1241  * quote char is also the escape char, there's no problem - we
1242  * just use the char as a toggle. If they are different, we need
1243  * to ensure that we only take account of an escape inside a
1244  * quoted field and immediately preceding a quote char, and not
1245  * the second in an escape-escape sequence.
1246  */
1247  if (in_quote && c == escapec)
1248  last_was_esc = !last_was_esc;
1249  if (c == quotec && !last_was_esc)
1250  in_quote = !in_quote;
1251  if (c != escapec)
1252  last_was_esc = false;
1253 
1254  /*
1255  * Updating the line count for embedded CR and/or LF chars is
1256  * necessarily a little fragile - this test is probably about the
1257  * best we can do. (XXX it's arguable whether we should do this
1258  * at all --- is cur_lineno a physical or logical count?)
1259  */
1260  if (in_quote && c == (cstate->eol_type == EOL_NL ? '\n' : '\r'))
1261  cstate->cur_lineno++;
1262  }
1263 
1264  /* Process \r */
1265  if (c == '\r' && (!cstate->opts.csv_mode || !in_quote))
1266  {
1267  /* Check for \r\n on first line, _and_ handle \r\n. */
1268  if (cstate->eol_type == EOL_UNKNOWN ||
1269  cstate->eol_type == EOL_CRNL)
1270  {
1271  /*
1272  * If need more data, go back to loop top to load it.
1273  *
1274  * Note that if we are at EOF, c will wind up as '\0' because
1275  * of the guaranteed pad of input_buf.
1276  */
1278 
1279  /* get next char */
1280  c = copy_input_buf[input_buf_ptr];
1281 
1282  if (c == '\n')
1283  {
1284  input_buf_ptr++; /* eat newline */
1285  cstate->eol_type = EOL_CRNL; /* in case not set yet */
1286  }
1287  else
1288  {
1289  /* found \r, but no \n */
1290  if (cstate->eol_type == EOL_CRNL)
1291  ereport(ERROR,
1292  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1293  !cstate->opts.csv_mode ?
1294  errmsg("literal carriage return found in data") :
1295  errmsg("unquoted carriage return found in data"),
1296  !cstate->opts.csv_mode ?
1297  errhint("Use \"\\r\" to represent carriage return.") :
1298  errhint("Use quoted CSV field to represent carriage return.")));
1299 
1300  /*
1301  * if we got here, it is the first line and we didn't find
1302  * \n, so don't consume the peeked character
1303  */
1304  cstate->eol_type = EOL_CR;
1305  }
1306  }
1307  else if (cstate->eol_type == EOL_NL)
1308  ereport(ERROR,
1309  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1310  !cstate->opts.csv_mode ?
1311  errmsg("literal carriage return found in data") :
1312  errmsg("unquoted carriage return found in data"),
1313  !cstate->opts.csv_mode ?
1314  errhint("Use \"\\r\" to represent carriage return.") :
1315  errhint("Use quoted CSV field to represent carriage return.")));
1316  /* If reach here, we have found the line terminator */
1317  break;
1318  }
1319 
1320  /* Process \n */
1321  if (c == '\n' && (!cstate->opts.csv_mode || !in_quote))
1322  {
1323  if (cstate->eol_type == EOL_CR || cstate->eol_type == EOL_CRNL)
1324  ereport(ERROR,
1325  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1326  !cstate->opts.csv_mode ?
1327  errmsg("literal newline found in data") :
1328  errmsg("unquoted newline found in data"),
1329  !cstate->opts.csv_mode ?
1330  errhint("Use \"\\n\" to represent newline.") :
1331  errhint("Use quoted CSV field to represent newline.")));
1332  cstate->eol_type = EOL_NL; /* in case not set yet */
1333  /* If reach here, we have found the line terminator */
1334  break;
1335  }
1336 
1337  /*
1338  * In CSV mode, we only recognize \. alone on a line. This is because
1339  * \. is a valid CSV data value.
1340  */
1341  if (c == '\\' && (!cstate->opts.csv_mode || first_char_in_line))
1342  {
1343  char c2;
1344 
1347 
1348  /* -----
1349  * get next character
1350  * Note: we do not change c so if it isn't \., we can fall
1351  * through and continue processing.
1352  * -----
1353  */
1354  c2 = copy_input_buf[input_buf_ptr];
1355 
1356  if (c2 == '.')
1357  {
1358  input_buf_ptr++; /* consume the '.' */
1359 
1360  /*
1361  * Note: if we loop back for more data here, it does not
1362  * matter that the CSV state change checks are re-executed; we
1363  * will come back here with no important state changed.
1364  */
1365  if (cstate->eol_type == EOL_CRNL)
1366  {
1367  /* Get the next character */
1369  /* if hit_eof, c2 will become '\0' */
1370  c2 = copy_input_buf[input_buf_ptr++];
1371 
1372  if (c2 == '\n')
1373  {
1374  if (!cstate->opts.csv_mode)
1375  ereport(ERROR,
1376  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1377  errmsg("end-of-copy marker does not match previous newline style")));
1378  else
1380  }
1381  else if (c2 != '\r')
1382  {
1383  if (!cstate->opts.csv_mode)
1384  ereport(ERROR,
1385  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1386  errmsg("end-of-copy marker corrupt")));
1387  else
1389  }
1390  }
1391 
1392  /* Get the next character */
1394  /* if hit_eof, c2 will become '\0' */
1395  c2 = copy_input_buf[input_buf_ptr++];
1396 
1397  if (c2 != '\r' && c2 != '\n')
1398  {
1399  if (!cstate->opts.csv_mode)
1400  ereport(ERROR,
1401  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1402  errmsg("end-of-copy marker corrupt")));
1403  else
1405  }
1406 
1407  if ((cstate->eol_type == EOL_NL && c2 != '\n') ||
1408  (cstate->eol_type == EOL_CRNL && c2 != '\n') ||
1409  (cstate->eol_type == EOL_CR && c2 != '\r'))
1410  {
1411  ereport(ERROR,
1412  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1413  errmsg("end-of-copy marker does not match previous newline style")));
1414  }
1415 
1416  /*
1417  * Transfer only the data before the \. into line_buf, then
1418  * discard the data and the \. sequence.
1419  */
1420  if (prev_raw_ptr > cstate->input_buf_index)
1422  cstate->input_buf + cstate->input_buf_index,
1423  prev_raw_ptr - cstate->input_buf_index);
1424  cstate->input_buf_index = input_buf_ptr;
1425  result = true; /* report EOF */
1426  break;
1427  }
1428  else if (!cstate->opts.csv_mode)
1429  {
1430  /*
1431  * If we are here, it means we found a backslash followed by
1432  * something other than a period. In non-CSV mode, anything
1433  * after a backslash is special, so we skip over that second
1434  * character too. If we didn't do that \\. would be
1435  * considered an eof-of copy, while in non-CSV mode it is a
1436  * literal backslash followed by a period. In CSV mode,
1437  * backslashes are not special, so we want to process the
1438  * character after the backslash just like a normal character,
1439  * so we don't increment in those cases.
1440  */
1441  input_buf_ptr++;
1442  }
1443  }
1444 
1445  /*
1446  * This label is for CSV cases where \. appears at the start of a
1447  * line, but there is more text after it, meaning it was a data value.
1448  * We are more strict for \. in CSV mode because \. could be a data
1449  * value, while in non-CSV mode, \. cannot be a data value.
1450  */
1451 not_end_of_copy:
1452  first_char_in_line = false;
1453  } /* end of outer loop */
1454 
1455  /*
1456  * Transfer any still-uncopied data to line_buf.
1457  */
1459 
1460  return result;
1461 }
1462 
1463 /*
1464  * Return decimal value for a hexadecimal digit
1465  */
1466 static int
1468 {
1469  if (isdigit((unsigned char) hex))
1470  return hex - '0';
1471  else
1472  return tolower((unsigned char) hex) - 'a' + 10;
1473 }
1474 
1475 /*
1476  * Parse the current line into separate attributes (fields),
1477  * performing de-escaping as needed.
1478  *
1479  * The input is in line_buf. We use attribute_buf to hold the result
1480  * strings. cstate->raw_fields[k] is set to point to the k'th attribute
1481  * string, or NULL when the input matches the null marker string.
1482  * This array is expanded as necessary.
1483  *
1484  * (Note that the caller cannot check for nulls since the returned
1485  * string would be the post-de-escaping equivalent, which may look
1486  * the same as some valid data string.)
1487  *
1488  * delim is the column delimiter string (must be just one byte for now).
1489  * null_print is the null marker string. Note that this is compared to
1490  * the pre-de-escaped input string.
1491  *
1492  * The return value is the number of fields actually read.
1493  */
1494 static int
1496 {
1497  char delimc = cstate->opts.delim[0];
1498  int fieldno;
1499  char *output_ptr;
1500  char *cur_ptr;
1501  char *line_end_ptr;
1502 
1503  /*
1504  * We need a special case for zero-column tables: check that the input
1505  * line is empty, and return.
1506  */
1507  if (cstate->max_fields <= 0)
1508  {
1509  if (cstate->line_buf.len != 0)
1510  ereport(ERROR,
1511  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1512  errmsg("extra data after last expected column")));
1513  return 0;
1514  }
1515 
1516  resetStringInfo(&cstate->attribute_buf);
1517 
1518  /*
1519  * The de-escaped attributes will certainly not be longer than the input
1520  * data line, so we can just force attribute_buf to be large enough and
1521  * then transfer data without any checks for enough space. We need to do
1522  * it this way because enlarging attribute_buf mid-stream would invalidate
1523  * pointers already stored into cstate->raw_fields[].
1524  */
1525  if (cstate->attribute_buf.maxlen <= cstate->line_buf.len)
1526  enlargeStringInfo(&cstate->attribute_buf, cstate->line_buf.len);
1527  output_ptr = cstate->attribute_buf.data;
1528 
1529  /* set pointer variables for loop */
1530  cur_ptr = cstate->line_buf.data;
1531  line_end_ptr = cstate->line_buf.data + cstate->line_buf.len;
1532 
1533  /* Outer loop iterates over fields */
1534  fieldno = 0;
1535  for (;;)
1536  {
1537  bool found_delim = false;
1538  char *start_ptr;
1539  char *end_ptr;
1540  int input_len;
1541  bool saw_non_ascii = false;
1542 
1543  /* Make sure there is enough space for the next value */
1544  if (fieldno >= cstate->max_fields)
1545  {
1546  cstate->max_fields *= 2;
1547  cstate->raw_fields =
1548  repalloc(cstate->raw_fields, cstate->max_fields * sizeof(char *));
1549  }
1550 
1551  /* Remember start of field on both input and output sides */
1552  start_ptr = cur_ptr;
1553  cstate->raw_fields[fieldno] = output_ptr;
1554 
1555  /*
1556  * Scan data for field.
1557  *
1558  * Note that in this loop, we are scanning to locate the end of field
1559  * and also speculatively performing de-escaping. Once we find the
1560  * end-of-field, we can match the raw field contents against the null
1561  * marker string. Only after that comparison fails do we know that
1562  * de-escaping is actually the right thing to do; therefore we *must
1563  * not* throw any syntax errors before we've done the null-marker
1564  * check.
1565  */
1566  for (;;)
1567  {
1568  char c;
1569 
1570  end_ptr = cur_ptr;
1571  if (cur_ptr >= line_end_ptr)
1572  break;
1573  c = *cur_ptr++;
1574  if (c == delimc)
1575  {
1576  found_delim = true;
1577  break;
1578  }
1579  if (c == '\\')
1580  {
1581  if (cur_ptr >= line_end_ptr)
1582  break;
1583  c = *cur_ptr++;
1584  switch (c)
1585  {
1586  case '0':
1587  case '1':
1588  case '2':
1589  case '3':
1590  case '4':
1591  case '5':
1592  case '6':
1593  case '7':
1594  {
1595  /* handle \013 */
1596  int val;
1597 
1598  val = OCTVALUE(c);
1599  if (cur_ptr < line_end_ptr)
1600  {
1601  c = *cur_ptr;
1602  if (ISOCTAL(c))
1603  {
1604  cur_ptr++;
1605  val = (val << 3) + OCTVALUE(c);
1606  if (cur_ptr < line_end_ptr)
1607  {
1608  c = *cur_ptr;
1609  if (ISOCTAL(c))
1610  {
1611  cur_ptr++;
1612  val = (val << 3) + OCTVALUE(c);
1613  }
1614  }
1615  }
1616  }
1617  c = val & 0377;
1618  if (c == '\0' || IS_HIGHBIT_SET(c))
1619  saw_non_ascii = true;
1620  }
1621  break;
1622  case 'x':
1623  /* Handle \x3F */
1624  if (cur_ptr < line_end_ptr)
1625  {
1626  char hexchar = *cur_ptr;
1627 
1628  if (isxdigit((unsigned char) hexchar))
1629  {
1630  int val = GetDecimalFromHex(hexchar);
1631 
1632  cur_ptr++;
1633  if (cur_ptr < line_end_ptr)
1634  {
1635  hexchar = *cur_ptr;
1636  if (isxdigit((unsigned char) hexchar))
1637  {
1638  cur_ptr++;
1639  val = (val << 4) + GetDecimalFromHex(hexchar);
1640  }
1641  }
1642  c = val & 0xff;
1643  if (c == '\0' || IS_HIGHBIT_SET(c))
1644  saw_non_ascii = true;
1645  }
1646  }
1647  break;
1648  case 'b':
1649  c = '\b';
1650  break;
1651  case 'f':
1652  c = '\f';
1653  break;
1654  case 'n':
1655  c = '\n';
1656  break;
1657  case 'r':
1658  c = '\r';
1659  break;
1660  case 't':
1661  c = '\t';
1662  break;
1663  case 'v':
1664  c = '\v';
1665  break;
1666 
1667  /*
1668  * in all other cases, take the char after '\'
1669  * literally
1670  */
1671  }
1672  }
1673 
1674  /* Add c to output string */
1675  *output_ptr++ = c;
1676  }
1677 
1678  /* Check whether raw input matched null marker */
1679  input_len = end_ptr - start_ptr;
1680  if (input_len == cstate->opts.null_print_len &&
1681  strncmp(start_ptr, cstate->opts.null_print, input_len) == 0)
1682  cstate->raw_fields[fieldno] = NULL;
1683  /* Check whether raw input matched default marker */
1684  else if (fieldno < list_length(cstate->attnumlist) &&
1685  cstate->opts.default_print &&
1686  input_len == cstate->opts.default_print_len &&
1687  strncmp(start_ptr, cstate->opts.default_print, input_len) == 0)
1688  {
1689  /* fieldno is 0-indexed and attnum is 1-indexed */
1690  int m = list_nth_int(cstate->attnumlist, fieldno) - 1;
1691 
1692  if (cstate->defexprs[m] != NULL)
1693  {
1694  /* defaults contain entries for all physical attributes */
1695  cstate->defaults[m] = true;
1696  }
1697  else
1698  {
1699  TupleDesc tupDesc = RelationGetDescr(cstate->rel);
1700  Form_pg_attribute att = TupleDescAttr(tupDesc, m);
1701 
1702  ereport(ERROR,
1703  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1704  errmsg("unexpected default marker in COPY data"),
1705  errdetail("Column \"%s\" has no default value.",
1706  NameStr(att->attname))));
1707  }
1708  }
1709  else
1710  {
1711  /*
1712  * At this point we know the field is supposed to contain data.
1713  *
1714  * If we de-escaped any non-7-bit-ASCII chars, make sure the
1715  * resulting string is valid data for the db encoding.
1716  */
1717  if (saw_non_ascii)
1718  {
1719  char *fld = cstate->raw_fields[fieldno];
1720 
1721  pg_verifymbstr(fld, output_ptr - fld, false);
1722  }
1723  }
1724 
1725  /* Terminate attribute value in output area */
1726  *output_ptr++ = '\0';
1727 
1728  fieldno++;
1729  /* Done if we hit EOL instead of a delim */
1730  if (!found_delim)
1731  break;
1732  }
1733 
1734  /* Clean up state of attribute_buf */
1735  output_ptr--;
1736  Assert(*output_ptr == '\0');
1737  cstate->attribute_buf.len = (output_ptr - cstate->attribute_buf.data);
1738 
1739  return fieldno;
1740 }
1741 
1742 /*
1743  * Parse the current line into separate attributes (fields),
1744  * performing de-escaping as needed. This has exactly the same API as
1745  * CopyReadAttributesText, except we parse the fields according to
1746  * "standard" (i.e. common) CSV usage.
1747  */
1748 static int
1750 {
1751  char delimc = cstate->opts.delim[0];
1752  char quotec = cstate->opts.quote[0];
1753  char escapec = cstate->opts.escape[0];
1754  int fieldno;
1755  char *output_ptr;
1756  char *cur_ptr;
1757  char *line_end_ptr;
1758 
1759  /*
1760  * We need a special case for zero-column tables: check that the input
1761  * line is empty, and return.
1762  */
1763  if (cstate->max_fields <= 0)
1764  {
1765  if (cstate->line_buf.len != 0)
1766  ereport(ERROR,
1767  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1768  errmsg("extra data after last expected column")));
1769  return 0;
1770  }
1771 
1772  resetStringInfo(&cstate->attribute_buf);
1773 
1774  /*
1775  * The de-escaped attributes will certainly not be longer than the input
1776  * data line, so we can just force attribute_buf to be large enough and
1777  * then transfer data without any checks for enough space. We need to do
1778  * it this way because enlarging attribute_buf mid-stream would invalidate
1779  * pointers already stored into cstate->raw_fields[].
1780  */
1781  if (cstate->attribute_buf.maxlen <= cstate->line_buf.len)
1782  enlargeStringInfo(&cstate->attribute_buf, cstate->line_buf.len);
1783  output_ptr = cstate->attribute_buf.data;
1784 
1785  /* set pointer variables for loop */
1786  cur_ptr = cstate->line_buf.data;
1787  line_end_ptr = cstate->line_buf.data + cstate->line_buf.len;
1788 
1789  /* Outer loop iterates over fields */
1790  fieldno = 0;
1791  for (;;)
1792  {
1793  bool found_delim = false;
1794  bool saw_quote = false;
1795  char *start_ptr;
1796  char *end_ptr;
1797  int input_len;
1798 
1799  /* Make sure there is enough space for the next value */
1800  if (fieldno >= cstate->max_fields)
1801  {
1802  cstate->max_fields *= 2;
1803  cstate->raw_fields =
1804  repalloc(cstate->raw_fields, cstate->max_fields * sizeof(char *));
1805  }
1806 
1807  /* Remember start of field on both input and output sides */
1808  start_ptr = cur_ptr;
1809  cstate->raw_fields[fieldno] = output_ptr;
1810 
1811  /*
1812  * Scan data for field,
1813  *
1814  * The loop starts in "not quote" mode and then toggles between that
1815  * and "in quote" mode. The loop exits normally if it is in "not
1816  * quote" mode and a delimiter or line end is seen.
1817  */
1818  for (;;)
1819  {
1820  char c;
1821 
1822  /* Not in quote */
1823  for (;;)
1824  {
1825  end_ptr = cur_ptr;
1826  if (cur_ptr >= line_end_ptr)
1827  goto endfield;
1828  c = *cur_ptr++;
1829  /* unquoted field delimiter */
1830  if (c == delimc)
1831  {
1832  found_delim = true;
1833  goto endfield;
1834  }
1835  /* start of quoted field (or part of field) */
1836  if (c == quotec)
1837  {
1838  saw_quote = true;
1839  break;
1840  }
1841  /* Add c to output string */
1842  *output_ptr++ = c;
1843  }
1844 
1845  /* In quote */
1846  for (;;)
1847  {
1848  end_ptr = cur_ptr;
1849  if (cur_ptr >= line_end_ptr)
1850  ereport(ERROR,
1851  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1852  errmsg("unterminated CSV quoted field")));
1853 
1854  c = *cur_ptr++;
1855 
1856  /* escape within a quoted field */
1857  if (c == escapec)
1858  {
1859  /*
1860  * peek at the next char if available, and escape it if it
1861  * is an escape char or a quote char
1862  */
1863  if (cur_ptr < line_end_ptr)
1864  {
1865  char nextc = *cur_ptr;
1866 
1867  if (nextc == escapec || nextc == quotec)
1868  {
1869  *output_ptr++ = nextc;
1870  cur_ptr++;
1871  continue;
1872  }
1873  }
1874  }
1875 
1876  /*
1877  * end of quoted field. Must do this test after testing for
1878  * escape in case quote char and escape char are the same
1879  * (which is the common case).
1880  */
1881  if (c == quotec)
1882  break;
1883 
1884  /* Add c to output string */
1885  *output_ptr++ = c;
1886  }
1887  }
1888 endfield:
1889 
1890  /* Terminate attribute value in output area */
1891  *output_ptr++ = '\0';
1892 
1893  /* Check whether raw input matched null marker */
1894  input_len = end_ptr - start_ptr;
1895  if (!saw_quote && input_len == cstate->opts.null_print_len &&
1896  strncmp(start_ptr, cstate->opts.null_print, input_len) == 0)
1897  cstate->raw_fields[fieldno] = NULL;
1898  /* Check whether raw input matched default marker */
1899  else if (fieldno < list_length(cstate->attnumlist) &&
1900  cstate->opts.default_print &&
1901  input_len == cstate->opts.default_print_len &&
1902  strncmp(start_ptr, cstate->opts.default_print, input_len) == 0)
1903  {
1904  /* fieldno is 0-index and attnum is 1-index */
1905  int m = list_nth_int(cstate->attnumlist, fieldno) - 1;
1906 
1907  if (cstate->defexprs[m] != NULL)
1908  {
1909  /* defaults contain entries for all physical attributes */
1910  cstate->defaults[m] = true;
1911  }
1912  else
1913  {
1914  TupleDesc tupDesc = RelationGetDescr(cstate->rel);
1915  Form_pg_attribute att = TupleDescAttr(tupDesc, m);
1916 
1917  ereport(ERROR,
1918  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1919  errmsg("unexpected default marker in COPY data"),
1920  errdetail("Column \"%s\" has no default value.",
1921  NameStr(att->attname))));
1922  }
1923  }
1924 
1925  fieldno++;
1926  /* Done if we hit EOL instead of a delim */
1927  if (!found_delim)
1928  break;
1929  }
1930 
1931  /* Clean up state of attribute_buf */
1932  output_ptr--;
1933  Assert(*output_ptr == '\0');
1934  cstate->attribute_buf.len = (output_ptr - cstate->attribute_buf.data);
1935 
1936  return fieldno;
1937 }
1938 
1939 
1940 /*
1941  * Read a binary attribute
1942  */
1943 static Datum
1945  Oid typioparam, int32 typmod,
1946  bool *isnull)
1947 {
1948  int32 fld_size;
1949  Datum result;
1950 
1951  if (!CopyGetInt32(cstate, &fld_size))
1952  ereport(ERROR,
1953  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1954  errmsg("unexpected EOF in COPY data")));
1955  if (fld_size == -1)
1956  {
1957  *isnull = true;
1958  return ReceiveFunctionCall(flinfo, NULL, typioparam, typmod);
1959  }
1960  if (fld_size < 0)
1961  ereport(ERROR,
1962  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1963  errmsg("invalid field size")));
1964 
1965  /* reset attribute_buf to empty, and load raw data in it */
1966  resetStringInfo(&cstate->attribute_buf);
1967 
1968  enlargeStringInfo(&cstate->attribute_buf, fld_size);
1969  if (CopyReadBinaryData(cstate, cstate->attribute_buf.data,
1970  fld_size) != fld_size)
1971  ereport(ERROR,
1972  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1973  errmsg("unexpected EOF in COPY data")));
1974 
1975  cstate->attribute_buf.len = fld_size;
1976  cstate->attribute_buf.data[fld_size] = '\0';
1977 
1978  /* Call the column type's binary input converter */
1979  result = ReceiveFunctionCall(flinfo, &cstate->attribute_buf,
1980  typioparam, typmod);
1981 
1982  /* Trouble if it didn't eat the whole buffer */
1983  if (cstate->attribute_buf.cursor != cstate->attribute_buf.len)
1984  ereport(ERROR,
1985  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1986  errmsg("incorrect binary data format")));
1987 
1988  *isnull = false;
1989  return result;
1990 }
int16 AttrNumber
Definition: attnum.h:21
void pgstat_progress_update_param(int index, int64 val)
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define NameStr(name)
Definition: c.h:735
unsigned short uint16
Definition: c.h:494
unsigned int uint32
Definition: c.h:495
#define Min(x, y)
Definition: c.h:993
signed short int16
Definition: c.h:482
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1168
signed int int32
Definition: c.h:483
#define MemSet(start, val, len)
Definition: c.h:1009
#define RAW_BUF_BYTES(cstate)
#define INPUT_BUF_SIZE
@ EOL_CR
@ EOL_CRNL
@ EOL_UNKNOWN
@ EOL_NL
#define INPUT_BUF_BYTES(cstate)
#define RAW_BUF_SIZE
static int CopyReadAttributesCSV(CopyFromState cstate)
static bool CopyGetInt16(CopyFromState cstate, int16 *val)
static void CopyConversionError(CopyFromState cstate)
static bool CopyGetInt32(CopyFromState cstate, int32 *val)
static void CopyLoadRawBuf(CopyFromState cstate)
#define OCTVALUE(c)
Definition: copyfromparse.c:80
#define REFILL_LINEBUF
#define NO_END_OF_COPY_GOTO
static void CopyLoadInputBuf(CopyFromState cstate)
#define ISOCTAL(c)
Definition: copyfromparse.c:79
void ReceiveCopyBinaryHeader(CopyFromState cstate)
static int CopyGetData(CopyFromState cstate, void *databuf, int minread, int maxread)
static Datum CopyReadBinaryAttribute(CopyFromState cstate, FmgrInfo *flinfo, Oid typioparam, int32 typmod, bool *isnull)
static bool CopyReadLineText(CopyFromState cstate)
static int GetDecimalFromHex(char hex)
void ReceiveCopyBegin(CopyFromState cstate)
#define IF_NEED_REFILL_AND_EOF_BREAK(extralen)
static int CopyReadAttributesText(CopyFromState cstate)
static const char BinarySignature[11]
#define IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(extralen)
Definition: copyfromparse.c:98
static int CopyReadBinaryData(CopyFromState cstate, char *dest, int nbytes)
static bool CopyReadLine(CopyFromState cstate)
static void CopyConvertBuf(CopyFromState cstate)
bool NextCopyFrom(CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls)
bool NextCopyFromRawFields(CopyFromState cstate, char ***fields, int *nfields)
@ COPY_FILE
Definition: copyto.c:52
@ COPY_CALLBACK
Definition: copyto.c:54
@ COPY_FRONTEND
Definition: copyto.c:53
struct cursor * cur
Definition: ecpg.c:28
int errcode_for_file_access(void)
Definition: elog.c:881
int errdetail(const char *fmt,...)
Definition: elog.c:1202
int errhint(const char *fmt,...)
Definition: elog.c:1316
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:332
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1513
Datum ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf, Oid typioparam, int32 typmod)
Definition: fmgr.c:1680
@ COPY_HEADER_MATCH
Definition: copy.h:30
long val
Definition: informix.c:664
int i
Definition: isn.c:73
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
#define pq_flush()
Definition: libpq.h:46
#define PQ_SMALL_MESSAGE_LIMIT
Definition: libpq.h:30
#define PQ_LARGE_MESSAGE_LIMIT
Definition: libpq.h:31
Assert(fmt[strlen(fmt) - 1] !='\n')
int GetDatabaseEncoding(void)
Definition: mbutils.c:1268
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: mbutils.c:1563
int pg_do_encoding_conversion_buf(Oid proc, int src_encoding, int dest_encoding, unsigned char *src, int srclen, unsigned char *dest, int destlen, bool noError)
Definition: mbutils.c:470
void report_invalid_encoding(int encoding, const char *mbstr, int len)
Definition: mbutils.c:1705
MemoryContext CurrentMemoryContext
Definition: mcxt.c:135
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1476
#define HOLD_CANCEL_INTERRUPTS()
Definition: miscadmin.h:140
#define RESUME_CANCEL_INTERRUPTS()
Definition: miscadmin.h:142
int namestrcmp(Name name, const char *str)
Definition: name.c:247
int16 attnum
Definition: pg_attribute.h:74
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
static char format
#define pg_ntoh32(x)
Definition: pg_bswap.h:125
#define pg_ntoh16(x)
Definition: pg_bswap.h:124
static int list_length(const List *l)
Definition: pg_list.h:152
#define lfirst_int(lc)
Definition: pg_list.h:173
static int list_nth_int(const List *list, int n)
Definition: pg_list.h:310
static char * buf
Definition: pg_test_fsync.c:67
#define MAX_CONVERSION_INPUT_LENGTH
Definition: pg_wchar.h:320
uintptr_t Datum
Definition: postgres.h:64
unsigned int Oid
Definition: postgres_ext.h:31
int pq_getmessage(StringInfo s, int maxlen)
Definition: pqcomm.c:1212
int pq_getbyte(void)
Definition: pqcomm.c:980
void pq_startmsgread(void)
Definition: pqcomm.c:1150
const char * pq_getmsgstring(StringInfo msg)
Definition: pqformat.c:582
void pq_copymsgbytes(StringInfo msg, char *buf, int datalen)
Definition: pqformat.c:531
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:299
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:88
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:161
static void pq_sendint16(StringInfo buf, uint16 i)
Definition: pqformat.h:137
char * c
char string[11]
Definition: preproc-type.c:52
#define PROGRESS_COPY_BYTES_PROCESSED
Definition: progress.h:139
#define PqMsg_CopyDone
Definition: protocol.h:64
#define PqMsg_CopyData
Definition: protocol.h:65
#define PqMsg_CopyInResponse
Definition: protocol.h:45
#define PqMsg_Sync
Definition: protocol.h:27
#define PqMsg_CopyFail
Definition: protocol.h:29
#define PqMsg_Flush
Definition: protocol.h:24
#define RelationGetDescr(relation)
Definition: rel.h:530
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:75
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:283
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
Definition: stringinfo.c:227
int default_print_len
Definition: copy.h:51
bool binary
Definition: copy.h:43
int null_print_len
Definition: copy.h:48
char * quote
Definition: copy.h:53
CopyHeaderChoice header_line
Definition: copy.h:46
char * escape
Definition: copy.h:54
char * null_print
Definition: copy.h:47
char * delim
Definition: copy.h:52
bool * force_notnull_flags
Definition: copy.h:59
bool csv_mode
Definition: copy.h:45
bool * force_null_flags
Definition: copy.h:61
char * default_print
Definition: copy.h:50
ExprState ** defexprs
copy_data_source_cb data_source_cb
StringInfoData line_buf
CopyFormatOptions opts
StringInfoData attribute_buf
const char * cur_attval
const char * cur_attname
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:257
Definition: fmgr.h:57
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
int pg_encoding_verifymbstr(int encoding, const char *mbstr, int len)
Definition: wchar.c:2177
int pg_encoding_max_length(int encoding)
Definition: wchar.c:2188