]> granicus.if.org Git - postgresql/blob - src/backend/commands/copy.c
Add ENCODING option to COPY TO/FROM and file_fdw.
[postgresql] / src / backend / commands / copy.c
1 /*-------------------------------------------------------------------------
2  *
3  * copy.c
4  *              Implements the COPY utility command
5  *
6  * Portions Copyright (c) 1996-2011, 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 #include <netinet/in.h>
21 #include <arpa/inet.h>
22
23 #include "access/heapam.h"
24 #include "access/sysattr.h"
25 #include "access/xact.h"
26 #include "catalog/namespace.h"
27 #include "catalog/pg_type.h"
28 #include "commands/copy.h"
29 #include "commands/defrem.h"
30 #include "commands/trigger.h"
31 #include "executor/executor.h"
32 #include "libpq/libpq.h"
33 #include "libpq/pqformat.h"
34 #include "mb/pg_wchar.h"
35 #include "miscadmin.h"
36 #include "optimizer/planner.h"
37 #include "parser/parse_relation.h"
38 #include "rewrite/rewriteHandler.h"
39 #include "storage/fd.h"
40 #include "tcop/tcopprot.h"
41 #include "utils/acl.h"
42 #include "utils/builtins.h"
43 #include "utils/lsyscache.h"
44 #include "utils/memutils.h"
45 #include "utils/snapmgr.h"
46
47
48 #define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
49 #define OCTVALUE(c) ((c) - '0')
50
51 /*
52  * Represents the different source/dest cases we need to worry about at
53  * the bottom level
54  */
55 typedef enum CopyDest
56 {
57         COPY_FILE,                                      /* to/from file */
58         COPY_OLD_FE,                            /* to/from frontend (2.0 protocol) */
59         COPY_NEW_FE                                     /* to/from frontend (3.0 protocol) */
60 } CopyDest;
61
62 /*
63  *      Represents the end-of-line terminator type of the input
64  */
65 typedef enum EolType
66 {
67         EOL_UNKNOWN,
68         EOL_NL,
69         EOL_CR,
70         EOL_CRNL
71 } EolType;
72
73 /*
74  * This struct contains all the state variables used throughout a COPY
75  * operation. For simplicity, we use the same struct for all variants of COPY,
76  * even though some fields are used in only some cases.
77  *
78  * Multi-byte encodings: all supported client-side encodings encode multi-byte
79  * characters by having the first byte's high bit set. Subsequent bytes of the
80  * character can have the high bit not set. When scanning data in such an
81  * encoding to look for a match to a single-byte (ie ASCII) character, we must
82  * use the full pg_encoding_mblen() machinery to skip over multibyte
83  * characters, else we might find a false match to a trailing byte. In
84  * supported server encodings, there is no possibility of a false match, and
85  * it's faster to make useless comparisons to trailing bytes than it is to
86  * invoke pg_encoding_mblen() to skip over them. encoding_embeds_ascii is TRUE
87  * when we have to do it the hard way.
88  */
89 typedef struct CopyStateData
90 {
91         /* low-level state data */
92         CopyDest        copy_dest;              /* type of copy source/destination */
93         FILE       *copy_file;          /* used if copy_dest == COPY_FILE */
94         StringInfo      fe_msgbuf;              /* used for all dests during COPY TO, only for
95                                                                  * dest == COPY_NEW_FE in COPY FROM */
96         bool            fe_eof;                 /* true if detected end of copy data */
97         EolType         eol_type;               /* EOL type of input */
98         int                     file_encoding;  /* file or remote side's character encoding */
99         bool            need_transcoding;               /* file encoding diff from server? */
100         bool            encoding_embeds_ascii;  /* ASCII can be non-first byte? */
101
102         /* parameters from the COPY command */
103         Relation        rel;                    /* relation to copy to or from */
104         QueryDesc  *queryDesc;          /* executable query to copy from */
105         List       *attnumlist;         /* integer list of attnums to copy */
106         char       *filename;           /* filename, or NULL for STDIN/STDOUT */
107         bool            binary;                 /* binary format? */
108         bool            oids;                   /* include OIDs? */
109         bool            csv_mode;               /* Comma Separated Value format? */
110         bool            header_line;    /* CSV header line? */
111         char       *null_print;         /* NULL marker string (server encoding!) */
112         int                     null_print_len; /* length of same */
113         char       *null_print_client;          /* same converted to file encoding */
114         char       *delim;                      /* column delimiter (must be 1 byte) */
115         char       *quote;                      /* CSV quote char (must be 1 byte) */
116         char       *escape;                     /* CSV escape char (must be 1 byte) */
117         List       *force_quote;        /* list of column names */
118         bool            force_quote_all; /* FORCE QUOTE *? */
119         bool       *force_quote_flags;          /* per-column CSV FQ flags */
120         List       *force_notnull;      /* list of column names */
121         bool       *force_notnull_flags;        /* per-column CSV FNN flags */
122
123         /* these are just for error messages, see CopyFromErrorCallback */
124         const char *cur_relname;        /* table name for error messages */
125         int                     cur_lineno;             /* line number for error messages */
126         const char *cur_attname;        /* current att for error messages */
127         const char *cur_attval;         /* current att value for error messages */
128
129         /*
130          * Working state for COPY TO/FROM
131          */
132         MemoryContext copycontext;      /* per-copy execution context */
133
134         /*
135          * Working state for COPY TO
136          */
137         FmgrInfo   *out_functions;      /* lookup info for output functions */
138         MemoryContext rowcontext;       /* per-row evaluation context */
139
140         /*
141          * Working state for COPY FROM
142          */
143         AttrNumber      num_defaults;
144         bool            file_has_oids;
145         FmgrInfo        oid_in_function;
146         Oid                     oid_typioparam;
147         FmgrInfo   *in_functions;       /* array of input functions for each attrs */
148         Oid                *typioparams;        /* array of element types for in_functions */
149         int                *defmap;                     /* array of default att numbers */
150         ExprState **defexprs;           /* array of default att expressions */
151
152         /*
153          * These variables are used to reduce overhead in textual COPY FROM.
154          *
155          * attribute_buf holds the separated, de-escaped text for each field of
156          * the current line.  The CopyReadAttributes functions return arrays of
157          * pointers into this buffer.  We avoid palloc/pfree overhead by re-using
158          * the buffer on each cycle.
159          */
160         StringInfoData attribute_buf;
161
162         /* field raw data pointers found by COPY FROM */
163
164         int max_fields;
165         char ** raw_fields;
166
167         /*
168          * Similarly, line_buf holds the whole input line being processed. The
169          * input cycle is first to read the whole line into line_buf, convert it
170          * to server encoding there, and then extract the individual attribute
171          * fields into attribute_buf.  line_buf is preserved unmodified so that we
172          * can display it in error messages if appropriate.
173          */
174         StringInfoData line_buf;
175         bool            line_buf_converted;             /* converted to server encoding? */
176
177         /*
178          * Finally, raw_buf holds raw data read from the data source (file or
179          * client connection).  CopyReadLine parses this data sufficiently to
180          * locate line boundaries, then transfers the data to line_buf and
181          * converts it.  Note: we guarantee that there is a \0 at
182          * raw_buf[raw_buf_len].
183          */
184 #define RAW_BUF_SIZE 65536              /* we palloc RAW_BUF_SIZE+1 bytes */
185         char       *raw_buf;
186         int                     raw_buf_index;  /* next byte to process */
187         int                     raw_buf_len;    /* total # of bytes stored */
188 } CopyStateData;
189
190 /* DestReceiver for COPY (SELECT) TO */
191 typedef struct
192 {
193         DestReceiver pub;                       /* publicly-known function pointers */
194         CopyState       cstate;                 /* CopyStateData for the command */
195         uint64          processed;              /* # of tuples processed */
196 } DR_copy;
197
198
199 /*
200  * These macros centralize code used to process line_buf and raw_buf buffers.
201  * They are macros because they often do continue/break control and to avoid
202  * function call overhead in tight COPY loops.
203  *
204  * We must use "if (1)" because the usual "do {...} while(0)" wrapper would
205  * prevent the continue/break processing from working.  We end the "if (1)"
206  * with "else ((void) 0)" to ensure the "if" does not unintentionally match
207  * any "else" in the calling code, and to avoid any compiler warnings about
208  * empty statements.  See http://www.cit.gu.edu.au/~anthony/info/C/C.macros.
209  */
210
211 /*
212  * This keeps the character read at the top of the loop in the buffer
213  * even if there is more than one read-ahead.
214  */
215 #define IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(extralen) \
216 if (1) \
217 { \
218         if (raw_buf_ptr + (extralen) >= copy_buf_len && !hit_eof) \
219         { \
220                 raw_buf_ptr = prev_raw_ptr; /* undo fetch */ \
221                 need_data = true; \
222                 continue; \
223         } \
224 } else ((void) 0)
225
226 /* This consumes the remainder of the buffer and breaks */
227 #define IF_NEED_REFILL_AND_EOF_BREAK(extralen) \
228 if (1) \
229 { \
230         if (raw_buf_ptr + (extralen) >= copy_buf_len && hit_eof) \
231         { \
232                 if (extralen) \
233                         raw_buf_ptr = copy_buf_len; /* consume the partial character */ \
234                 /* backslash just before EOF, treat as data char */ \
235                 result = true; \
236                 break; \
237         } \
238 } else ((void) 0)
239
240 /*
241  * Transfer any approved data to line_buf; must do this to be sure
242  * there is some room in raw_buf.
243  */
244 #define REFILL_LINEBUF \
245 if (1) \
246 { \
247         if (raw_buf_ptr > cstate->raw_buf_index) \
248         { \
249                 appendBinaryStringInfo(&cstate->line_buf, \
250                                                          cstate->raw_buf + cstate->raw_buf_index, \
251                                                            raw_buf_ptr - cstate->raw_buf_index); \
252                 cstate->raw_buf_index = raw_buf_ptr; \
253         } \
254 } else ((void) 0)
255
256 /* Undo any read-ahead and jump out of the block. */
257 #define NO_END_OF_COPY_GOTO \
258 if (1) \
259 { \
260         raw_buf_ptr = prev_raw_ptr + 1; \
261         goto not_end_of_copy; \
262 } else ((void) 0)
263
264 static const char BinarySignature[11] = "PGCOPY\n\377\r\n\0";
265
266
267 /* non-export function prototypes */
268 static CopyState BeginCopy(bool is_from, Relation rel, Node *raw_query,
269                                 const char *queryString, List *attnamelist, List *options);
270 static void EndCopy(CopyState cstate);
271 static CopyState BeginCopyTo(Relation rel, Node *query, const char *queryString,
272                                 const char *filename, List *attnamelist, List *options);
273 static void EndCopyTo(CopyState cstate);
274 static uint64 DoCopyTo(CopyState cstate);
275 static uint64 CopyTo(CopyState cstate);
276 static void CopyOneRowTo(CopyState cstate, Oid tupleOid,
277                          Datum *values, bool *nulls);
278 static uint64 CopyFrom(CopyState cstate);
279 static bool CopyReadLine(CopyState cstate);
280 static bool CopyReadLineText(CopyState cstate);
281 static int CopyReadAttributesText(CopyState cstate);
282 static int CopyReadAttributesCSV(CopyState cstate);
283 static Datum CopyReadBinaryAttribute(CopyState cstate,
284                                                 int column_no, FmgrInfo *flinfo,
285                                                 Oid typioparam, int32 typmod,
286                                                 bool *isnull);
287 static void CopyAttributeOutText(CopyState cstate, char *string);
288 static void CopyAttributeOutCSV(CopyState cstate, char *string,
289                                         bool use_quote, bool single_attr);
290 static List *CopyGetAttnums(TupleDesc tupDesc, Relation rel,
291                            List *attnamelist);
292 static char *limit_printout_length(const char *str);
293
294 /* Low-level communications functions */
295 static void SendCopyBegin(CopyState cstate);
296 static void ReceiveCopyBegin(CopyState cstate);
297 static void SendCopyEnd(CopyState cstate);
298 static void CopySendData(CopyState cstate, void *databuf, int datasize);
299 static void CopySendString(CopyState cstate, const char *str);
300 static void CopySendChar(CopyState cstate, char c);
301 static void CopySendEndOfRow(CopyState cstate);
302 static int CopyGetData(CopyState cstate, void *databuf,
303                         int minread, int maxread);
304 static void CopySendInt32(CopyState cstate, int32 val);
305 static bool CopyGetInt32(CopyState cstate, int32 *val);
306 static void CopySendInt16(CopyState cstate, int16 val);
307 static bool CopyGetInt16(CopyState cstate, int16 *val);
308
309
310 /*
311  * Send copy start/stop messages for frontend copies.  These have changed
312  * in past protocol redesigns.
313  */
314 static void
315 SendCopyBegin(CopyState cstate)
316 {
317         if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
318         {
319                 /* new way */
320                 StringInfoData buf;
321                 int                     natts = list_length(cstate->attnumlist);
322                 int16           format = (cstate->binary ? 1 : 0);
323                 int                     i;
324
325                 pq_beginmessage(&buf, 'H');
326                 pq_sendbyte(&buf, format);              /* overall format */
327                 pq_sendint(&buf, natts, 2);
328                 for (i = 0; i < natts; i++)
329                         pq_sendint(&buf, format, 2);            /* per-column formats */
330                 pq_endmessage(&buf);
331                 cstate->copy_dest = COPY_NEW_FE;
332         }
333         else if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 2)
334         {
335                 /* old way */
336                 if (cstate->binary)
337                         ereport(ERROR,
338                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
339                         errmsg("COPY BINARY is not supported to stdout or from stdin")));
340                 pq_putemptymessage('H');
341                 /* grottiness needed for old COPY OUT protocol */
342                 pq_startcopyout();
343                 cstate->copy_dest = COPY_OLD_FE;
344         }
345         else
346         {
347                 /* very old way */
348                 if (cstate->binary)
349                         ereport(ERROR,
350                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
351                         errmsg("COPY BINARY is not supported to stdout or from stdin")));
352                 pq_putemptymessage('B');
353                 /* grottiness needed for old COPY OUT protocol */
354                 pq_startcopyout();
355                 cstate->copy_dest = COPY_OLD_FE;
356         }
357 }
358
359 static void
360 ReceiveCopyBegin(CopyState cstate)
361 {
362         if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
363         {
364                 /* new way */
365                 StringInfoData buf;
366                 int                     natts = list_length(cstate->attnumlist);
367                 int16           format = (cstate->binary ? 1 : 0);
368                 int                     i;
369
370                 pq_beginmessage(&buf, 'G');
371                 pq_sendbyte(&buf, format);              /* overall format */
372                 pq_sendint(&buf, natts, 2);
373                 for (i = 0; i < natts; i++)
374                         pq_sendint(&buf, format, 2);            /* per-column formats */
375                 pq_endmessage(&buf);
376                 cstate->copy_dest = COPY_NEW_FE;
377                 cstate->fe_msgbuf = makeStringInfo();
378         }
379         else if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 2)
380         {
381                 /* old way */
382                 if (cstate->binary)
383                         ereport(ERROR,
384                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
385                         errmsg("COPY BINARY is not supported to stdout or from stdin")));
386                 pq_putemptymessage('G');
387                 cstate->copy_dest = COPY_OLD_FE;
388         }
389         else
390         {
391                 /* very old way */
392                 if (cstate->binary)
393                         ereport(ERROR,
394                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
395                         errmsg("COPY BINARY is not supported to stdout or from stdin")));
396                 pq_putemptymessage('D');
397                 cstate->copy_dest = COPY_OLD_FE;
398         }
399         /* We *must* flush here to ensure FE knows it can send. */
400         pq_flush();
401 }
402
403 static void
404 SendCopyEnd(CopyState cstate)
405 {
406         if (cstate->copy_dest == COPY_NEW_FE)
407         {
408                 /* Shouldn't have any unsent data */
409                 Assert(cstate->fe_msgbuf->len == 0);
410                 /* Send Copy Done message */
411                 pq_putemptymessage('c');
412         }
413         else
414         {
415                 CopySendData(cstate, "\\.", 2);
416                 /* Need to flush out the trailer (this also appends a newline) */
417                 CopySendEndOfRow(cstate);
418                 pq_endcopyout(false);
419         }
420 }
421
422 /*----------
423  * CopySendData sends output data to the destination (file or frontend)
424  * CopySendString does the same for null-terminated strings
425  * CopySendChar does the same for single characters
426  * CopySendEndOfRow does the appropriate thing at end of each data row
427  *      (data is not actually flushed except by CopySendEndOfRow)
428  *
429  * NB: no data conversion is applied by these functions
430  *----------
431  */
432 static void
433 CopySendData(CopyState cstate, void *databuf, int datasize)
434 {
435         appendBinaryStringInfo(cstate->fe_msgbuf, (char *) databuf, datasize);
436 }
437
438 static void
439 CopySendString(CopyState cstate, const char *str)
440 {
441         appendBinaryStringInfo(cstate->fe_msgbuf, str, strlen(str));
442 }
443
444 static void
445 CopySendChar(CopyState cstate, char c)
446 {
447         appendStringInfoCharMacro(cstate->fe_msgbuf, c);
448 }
449
450 static void
451 CopySendEndOfRow(CopyState cstate)
452 {
453         StringInfo      fe_msgbuf = cstate->fe_msgbuf;
454
455         switch (cstate->copy_dest)
456         {
457                 case COPY_FILE:
458                         if (!cstate->binary)
459                         {
460                                 /* Default line termination depends on platform */
461 #ifndef WIN32
462                                 CopySendChar(cstate, '\n');
463 #else
464                                 CopySendString(cstate, "\r\n");
465 #endif
466                         }
467
468                         (void) fwrite(fe_msgbuf->data, fe_msgbuf->len,
469                                                   1, cstate->copy_file);
470                         if (ferror(cstate->copy_file))
471                                 ereport(ERROR,
472                                                 (errcode_for_file_access(),
473                                                  errmsg("could not write to COPY file: %m")));
474                         break;
475                 case COPY_OLD_FE:
476                         /* The FE/BE protocol uses \n as newline for all platforms */
477                         if (!cstate->binary)
478                                 CopySendChar(cstate, '\n');
479
480                         if (pq_putbytes(fe_msgbuf->data, fe_msgbuf->len))
481                         {
482                                 /* no hope of recovering connection sync, so FATAL */
483                                 ereport(FATAL,
484                                                 (errcode(ERRCODE_CONNECTION_FAILURE),
485                                                  errmsg("connection lost during COPY to stdout")));
486                         }
487                         break;
488                 case COPY_NEW_FE:
489                         /* The FE/BE protocol uses \n as newline for all platforms */
490                         if (!cstate->binary)
491                                 CopySendChar(cstate, '\n');
492
493                         /* Dump the accumulated row as one CopyData message */
494                         (void) pq_putmessage('d', fe_msgbuf->data, fe_msgbuf->len);
495                         break;
496         }
497
498         resetStringInfo(fe_msgbuf);
499 }
500
501 /*
502  * CopyGetData reads data from the source (file or frontend)
503  *
504  * We attempt to read at least minread, and at most maxread, bytes from
505  * the source.  The actual number of bytes read is returned; if this is
506  * less than minread, EOF was detected.
507  *
508  * Note: when copying from the frontend, we expect a proper EOF mark per
509  * protocol; if the frontend simply drops the connection, we raise error.
510  * It seems unwise to allow the COPY IN to complete normally in that case.
511  *
512  * NB: no data conversion is applied here.
513  */
514 static int
515 CopyGetData(CopyState cstate, void *databuf, int minread, int maxread)
516 {
517         int                     bytesread = 0;
518
519         switch (cstate->copy_dest)
520         {
521                 case COPY_FILE:
522                         bytesread = fread(databuf, 1, maxread, cstate->copy_file);
523                         if (ferror(cstate->copy_file))
524                                 ereport(ERROR,
525                                                 (errcode_for_file_access(),
526                                                  errmsg("could not read from COPY file: %m")));
527                         break;
528                 case COPY_OLD_FE:
529
530                         /*
531                          * We cannot read more than minread bytes (which in practice is 1)
532                          * because old protocol doesn't have any clear way of separating
533                          * the COPY stream from following data.  This is slow, but not any
534                          * slower than the code path was originally, and we don't care
535                          * much anymore about the performance of old protocol.
536                          */
537                         if (pq_getbytes((char *) databuf, minread))
538                         {
539                                 /* Only a \. terminator is legal EOF in old protocol */
540                                 ereport(ERROR,
541                                                 (errcode(ERRCODE_CONNECTION_FAILURE),
542                                                  errmsg("unexpected EOF on client connection")));
543                         }
544                         bytesread = minread;
545                         break;
546                 case COPY_NEW_FE:
547                         while (maxread > 0 && bytesread < minread && !cstate->fe_eof)
548                         {
549                                 int                     avail;
550
551                                 while (cstate->fe_msgbuf->cursor >= cstate->fe_msgbuf->len)
552                                 {
553                                         /* Try to receive another message */
554                                         int                     mtype;
555
556                         readmessage:
557                                         mtype = pq_getbyte();
558                                         if (mtype == EOF)
559                                                 ereport(ERROR,
560                                                                 (errcode(ERRCODE_CONNECTION_FAILURE),
561                                                          errmsg("unexpected EOF on client connection")));
562                                         if (pq_getmessage(cstate->fe_msgbuf, 0))
563                                                 ereport(ERROR,
564                                                                 (errcode(ERRCODE_CONNECTION_FAILURE),
565                                                          errmsg("unexpected EOF on client connection")));
566                                         switch (mtype)
567                                         {
568                                                 case 'd':               /* CopyData */
569                                                         break;
570                                                 case 'c':               /* CopyDone */
571                                                         /* COPY IN correctly terminated by frontend */
572                                                         cstate->fe_eof = true;
573                                                         return bytesread;
574                                                 case 'f':               /* CopyFail */
575                                                         ereport(ERROR,
576                                                                         (errcode(ERRCODE_QUERY_CANCELED),
577                                                                          errmsg("COPY from stdin failed: %s",
578                                                                            pq_getmsgstring(cstate->fe_msgbuf))));
579                                                         break;
580                                                 case 'H':               /* Flush */
581                                                 case 'S':               /* Sync */
582
583                                                         /*
584                                                          * Ignore Flush/Sync for the convenience of client
585                                                          * libraries (such as libpq) that may send those
586                                                          * without noticing that the command they just
587                                                          * sent was COPY.
588                                                          */
589                                                         goto readmessage;
590                                                 default:
591                                                         ereport(ERROR,
592                                                                         (errcode(ERRCODE_PROTOCOL_VIOLATION),
593                                                                          errmsg("unexpected message type 0x%02X during COPY from stdin",
594                                                                                         mtype)));
595                                                         break;
596                                         }
597                                 }
598                                 avail = cstate->fe_msgbuf->len - cstate->fe_msgbuf->cursor;
599                                 if (avail > maxread)
600                                         avail = maxread;
601                                 pq_copymsgbytes(cstate->fe_msgbuf, databuf, avail);
602                                 databuf = (void *) ((char *) databuf + avail);
603                                 maxread -= avail;
604                                 bytesread += avail;
605                         }
606                         break;
607         }
608
609         return bytesread;
610 }
611
612
613 /*
614  * These functions do apply some data conversion
615  */
616
617 /*
618  * CopySendInt32 sends an int32 in network byte order
619  */
620 static void
621 CopySendInt32(CopyState cstate, int32 val)
622 {
623         uint32          buf;
624
625         buf = htonl((uint32) val);
626         CopySendData(cstate, &buf, sizeof(buf));
627 }
628
629 /*
630  * CopyGetInt32 reads an int32 that appears in network byte order
631  *
632  * Returns true if OK, false if EOF
633  */
634 static bool
635 CopyGetInt32(CopyState cstate, int32 *val)
636 {
637         uint32          buf;
638
639         if (CopyGetData(cstate, &buf, sizeof(buf), sizeof(buf)) != sizeof(buf))
640         {
641                 *val = 0;                               /* suppress compiler warning */
642                 return false;
643         }
644         *val = (int32) ntohl(buf);
645         return true;
646 }
647
648 /*
649  * CopySendInt16 sends an int16 in network byte order
650  */
651 static void
652 CopySendInt16(CopyState cstate, int16 val)
653 {
654         uint16          buf;
655
656         buf = htons((uint16) val);
657         CopySendData(cstate, &buf, sizeof(buf));
658 }
659
660 /*
661  * CopyGetInt16 reads an int16 that appears in network byte order
662  */
663 static bool
664 CopyGetInt16(CopyState cstate, int16 *val)
665 {
666         uint16          buf;
667
668         if (CopyGetData(cstate, &buf, sizeof(buf), sizeof(buf)) != sizeof(buf))
669         {
670                 *val = 0;                               /* suppress compiler warning */
671                 return false;
672         }
673         *val = (int16) ntohs(buf);
674         return true;
675 }
676
677
678 /*
679  * CopyLoadRawBuf loads some more data into raw_buf
680  *
681  * Returns TRUE if able to obtain at least one more byte, else FALSE.
682  *
683  * If raw_buf_index < raw_buf_len, the unprocessed bytes are transferred
684  * down to the start of the buffer and then we load more data after that.
685  * This case is used only when a frontend multibyte character crosses a
686  * bufferload boundary.
687  */
688 static bool
689 CopyLoadRawBuf(CopyState cstate)
690 {
691         int                     nbytes;
692         int                     inbytes;
693
694         if (cstate->raw_buf_index < cstate->raw_buf_len)
695         {
696                 /* Copy down the unprocessed data */
697                 nbytes = cstate->raw_buf_len - cstate->raw_buf_index;
698                 memmove(cstate->raw_buf, cstate->raw_buf + cstate->raw_buf_index,
699                                 nbytes);
700         }
701         else
702                 nbytes = 0;                             /* no data need be saved */
703
704         inbytes = CopyGetData(cstate, cstate->raw_buf + nbytes,
705                                                   1, RAW_BUF_SIZE - nbytes);
706         nbytes += inbytes;
707         cstate->raw_buf[nbytes] = '\0';
708         cstate->raw_buf_index = 0;
709         cstate->raw_buf_len = nbytes;
710         return (inbytes > 0);
711 }
712
713
714 /*
715  *       DoCopy executes the SQL COPY statement
716  *
717  * Either unload or reload contents of table <relation>, depending on <from>.
718  * (<from> = TRUE means we are inserting into the table.)  In the "TO" case
719  * we also support copying the output of an arbitrary SELECT query.
720  *
721  * If <pipe> is false, transfer is between the table and the file named
722  * <filename>.  Otherwise, transfer is between the table and our regular
723  * input/output stream. The latter could be either stdin/stdout or a
724  * socket, depending on whether we're running under Postmaster control.
725  *
726  * Do not allow a Postgres user without superuser privilege to read from
727  * or write to a file.
728  *
729  * Do not allow the copy if user doesn't have proper permission to access
730  * the table or the specifically requested columns.
731  */
732 uint64
733 DoCopy(const CopyStmt *stmt, const char *queryString)
734 {
735         CopyState       cstate;
736         bool            is_from = stmt->is_from;
737         bool            pipe = (stmt->filename == NULL);
738         Relation        rel;
739         uint64          processed;
740
741         /* Disallow file COPY except to superusers. */
742         if (!pipe && !superuser())
743                 ereport(ERROR,
744                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
745                                  errmsg("must be superuser to COPY to or from a file"),
746                                  errhint("Anyone can COPY to stdout or from stdin. "
747                                                  "psql's \\copy command also works for anyone.")));
748
749         if (stmt->relation)
750         {
751                 TupleDesc               tupDesc;
752                 AclMode                 required_access = (is_from ? ACL_INSERT : ACL_SELECT);
753                 RangeTblEntry  *rte;
754                 List               *attnums;
755                 ListCell           *cur;
756
757                 Assert(!stmt->query);
758
759                 /* Open and lock the relation, using the appropriate lock type. */
760                 rel = heap_openrv(stmt->relation,
761                                                          (is_from ? RowExclusiveLock : AccessShareLock));
762
763                 rte = makeNode(RangeTblEntry);
764                 rte->rtekind = RTE_RELATION;
765                 rte->relid = RelationGetRelid(rel);
766                 rte->requiredPerms = required_access;
767
768                 tupDesc = RelationGetDescr(rel);
769                 attnums = CopyGetAttnums(tupDesc, rel, stmt->attlist);
770                 foreach(cur, attnums)
771                 {
772                         int             attno = lfirst_int(cur) -
773                                                         FirstLowInvalidHeapAttributeNumber;
774
775                         if (is_from)
776                                 rte->modifiedCols = bms_add_member(rte->modifiedCols, attno);
777                         else
778                                 rte->selectedCols = bms_add_member(rte->selectedCols, attno);
779                 }
780                 ExecCheckRTPerms(list_make1(rte), true);
781         }
782         else
783         {
784                 Assert(stmt->query);
785
786                 rel = NULL;
787         }
788
789         if (is_from)
790         {
791                 /* check read-only transaction */
792                 if (XactReadOnly && rel->rd_backend != MyBackendId)
793                         PreventCommandIfReadOnly("COPY FROM");
794
795                 cstate = BeginCopyFrom(rel, stmt->filename,
796                                                            stmt->attlist, stmt->options);
797                 processed = CopyFrom(cstate);   /* copy from file to database */
798                 EndCopyFrom(cstate);
799         }
800         else
801         {
802                 cstate = BeginCopyTo(rel, stmt->query, queryString, stmt->filename,
803                                                          stmt->attlist, stmt->options);
804                 processed = DoCopyTo(cstate);   /* copy from database to file */
805                 EndCopyTo(cstate);
806         }
807
808         /*
809          * Close the relation. If reading, we can release the AccessShareLock we
810          * got; if writing, we should hold the lock until end of transaction to
811          * ensure that updates will be committed before lock is released.
812          */
813         if (rel != NULL)
814                 heap_close(rel, (is_from ? NoLock : AccessShareLock));
815
816         return processed;
817 }
818
819 /*
820  * Process the statement option list for COPY.
821  *
822  * Scan the options list (a list of DefElem) and transpose the information
823  * into cstate, applying appropriate error checking.
824  *
825  * cstate is assumed to be filled with zeroes initially.
826  *
827  * This is exported so that external users of the COPY API can sanity-check
828  * a list of options.  In that usage, cstate should be passed as NULL
829  * (since external users don't know sizeof(CopyStateData)) and the collected
830  * data is just leaked until CurrentMemoryContext is reset.
831  *
832  * Note that additional checking, such as whether column names listed in FORCE
833  * QUOTE actually exist, has to be applied later.  This just checks for
834  * self-consistency of the options list.
835  */
836 void
837 ProcessCopyOptions(CopyState cstate,
838                                    bool is_from,
839                                    List *options)
840 {
841         bool            format_specified = false;
842         ListCell   *option;
843
844         /* Support external use for option sanity checking */
845         if (cstate == NULL)
846                 cstate = (CopyStateData *) palloc0(sizeof(CopyStateData));
847
848         cstate->file_encoding = -1;
849
850         /* Extract options from the statement node tree */
851         foreach(option, options)
852         {
853                 DefElem    *defel = (DefElem *) lfirst(option);
854
855                 if (strcmp(defel->defname, "format") == 0)
856                 {
857                         char       *fmt = defGetString(defel);
858
859                         if (format_specified)
860                                 ereport(ERROR,
861                                                 (errcode(ERRCODE_SYNTAX_ERROR),
862                                                  errmsg("conflicting or redundant options")));
863                         format_specified = true;
864                         if (strcmp(fmt, "text") == 0)
865                                  /* default format */ ;
866                         else if (strcmp(fmt, "csv") == 0)
867                                 cstate->csv_mode = true;
868                         else if (strcmp(fmt, "binary") == 0)
869                                 cstate->binary = true;
870                         else
871                                 ereport(ERROR,
872                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
873                                                  errmsg("COPY format \"%s\" not recognized", fmt)));
874                 }
875                 else if (strcmp(defel->defname, "oids") == 0)
876                 {
877                         if (cstate->oids)
878                                 ereport(ERROR,
879                                                 (errcode(ERRCODE_SYNTAX_ERROR),
880                                                  errmsg("conflicting or redundant options")));
881                         cstate->oids = defGetBoolean(defel);
882                 }
883                 else if (strcmp(defel->defname, "delimiter") == 0)
884                 {
885                         if (cstate->delim)
886                                 ereport(ERROR,
887                                                 (errcode(ERRCODE_SYNTAX_ERROR),
888                                                  errmsg("conflicting or redundant options")));
889                         cstate->delim = defGetString(defel);
890                 }
891                 else if (strcmp(defel->defname, "null") == 0)
892                 {
893                         if (cstate->null_print)
894                                 ereport(ERROR,
895                                                 (errcode(ERRCODE_SYNTAX_ERROR),
896                                                  errmsg("conflicting or redundant options")));
897                         cstate->null_print = defGetString(defel);
898                 }
899                 else if (strcmp(defel->defname, "header") == 0)
900                 {
901                         if (cstate->header_line)
902                                 ereport(ERROR,
903                                                 (errcode(ERRCODE_SYNTAX_ERROR),
904                                                  errmsg("conflicting or redundant options")));
905                         cstate->header_line = defGetBoolean(defel);
906                 }
907                 else if (strcmp(defel->defname, "quote") == 0)
908                 {
909                         if (cstate->quote)
910                                 ereport(ERROR,
911                                                 (errcode(ERRCODE_SYNTAX_ERROR),
912                                                  errmsg("conflicting or redundant options")));
913                         cstate->quote = defGetString(defel);
914                 }
915                 else if (strcmp(defel->defname, "escape") == 0)
916                 {
917                         if (cstate->escape)
918                                 ereport(ERROR,
919                                                 (errcode(ERRCODE_SYNTAX_ERROR),
920                                                  errmsg("conflicting or redundant options")));
921                         cstate->escape = defGetString(defel);
922                 }
923                 else if (strcmp(defel->defname, "force_quote") == 0)
924                 {
925                         if (cstate->force_quote || cstate->force_quote_all)
926                                 ereport(ERROR,
927                                                 (errcode(ERRCODE_SYNTAX_ERROR),
928                                                  errmsg("conflicting or redundant options")));
929                         if (defel->arg && IsA(defel->arg, A_Star))
930                                 cstate->force_quote_all = true;
931                         else if (defel->arg && IsA(defel->arg, List))
932                                 cstate->force_quote = (List *) defel->arg;
933                         else
934                                 ereport(ERROR,
935                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
936                                                  errmsg("argument to option \"%s\" must be a list of column names",
937                                                                 defel->defname)));
938                 }
939                 else if (strcmp(defel->defname, "force_not_null") == 0)
940                 {
941                         if (cstate->force_notnull)
942                                 ereport(ERROR,
943                                                 (errcode(ERRCODE_SYNTAX_ERROR),
944                                                  errmsg("conflicting or redundant options")));
945                         if (defel->arg && IsA(defel->arg, List))
946                                 cstate->force_notnull = (List *) defel->arg;
947                         else
948                                 ereport(ERROR,
949                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
950                                                  errmsg("argument to option \"%s\" must be a list of column names",
951                                                                 defel->defname)));
952                 }
953                 else if (strcmp(defel->defname, "encoding") == 0)
954                 {
955                         if (cstate->file_encoding >= 0)
956                                 ereport(ERROR,
957                                                 (errcode(ERRCODE_SYNTAX_ERROR),
958                                                  errmsg("conflicting or redundant options")));
959                         cstate->file_encoding = pg_char_to_encoding(defGetString(defel));
960                         if (cstate->file_encoding < 0)
961                                 ereport(ERROR,
962                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
963                                                  errmsg("argument to option \"%s\" must be a valid encoding name",
964                                                                 defel->defname)));
965                 }
966                 else
967                         ereport(ERROR,
968                                         (errcode(ERRCODE_SYNTAX_ERROR),
969                                          errmsg("option \"%s\" not recognized",
970                                                         defel->defname)));
971         }
972
973         /*
974          * Check for incompatible options (must do these two before inserting
975          * defaults)
976          */
977         if (cstate->binary && cstate->delim)
978                 ereport(ERROR,
979                                 (errcode(ERRCODE_SYNTAX_ERROR),
980                                  errmsg("cannot specify DELIMITER in BINARY mode")));
981
982         if (cstate->binary && cstate->null_print)
983                 ereport(ERROR,
984                                 (errcode(ERRCODE_SYNTAX_ERROR),
985                                  errmsg("cannot specify NULL in BINARY mode")));
986
987         /* Set defaults for omitted options */
988         if (!cstate->delim)
989                 cstate->delim = cstate->csv_mode ? "," : "\t";
990
991         if (!cstate->null_print)
992                 cstate->null_print = cstate->csv_mode ? "" : "\\N";
993         cstate->null_print_len = strlen(cstate->null_print);
994
995         if (cstate->csv_mode)
996         {
997                 if (!cstate->quote)
998                         cstate->quote = "\"";
999                 if (!cstate->escape)
1000                         cstate->escape = cstate->quote;
1001         }
1002
1003         /* Only single-byte delimiter strings are supported. */
1004         if (strlen(cstate->delim) != 1)
1005                 ereport(ERROR,
1006                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1007                           errmsg("COPY delimiter must be a single one-byte character")));
1008
1009         /* Disallow end-of-line characters */
1010         if (strchr(cstate->delim, '\r') != NULL ||
1011                 strchr(cstate->delim, '\n') != NULL)
1012                 ereport(ERROR,
1013                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1014                          errmsg("COPY delimiter cannot be newline or carriage return")));
1015
1016         if (strchr(cstate->null_print, '\r') != NULL ||
1017                 strchr(cstate->null_print, '\n') != NULL)
1018                 ereport(ERROR,
1019                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1020                                  errmsg("COPY null representation cannot use newline or carriage return")));
1021
1022         /*
1023          * Disallow unsafe delimiter characters in non-CSV mode.  We can't allow
1024          * backslash because it would be ambiguous.  We can't allow the other
1025          * cases because data characters matching the delimiter must be
1026          * backslashed, and certain backslash combinations are interpreted
1027          * non-literally by COPY IN.  Disallowing all lower case ASCII letters is
1028          * more than strictly necessary, but seems best for consistency and
1029          * future-proofing.  Likewise we disallow all digits though only octal
1030          * digits are actually dangerous.
1031          */
1032         if (!cstate->csv_mode &&
1033                 strchr("\\.abcdefghijklmnopqrstuvwxyz0123456789",
1034                            cstate->delim[0]) != NULL)
1035                 ereport(ERROR,
1036                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1037                                  errmsg("COPY delimiter cannot be \"%s\"", cstate->delim)));
1038
1039         /* Check header */
1040         if (!cstate->csv_mode && cstate->header_line)
1041                 ereport(ERROR,
1042                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1043                                  errmsg("COPY HEADER available only in CSV mode")));
1044
1045         /* Check quote */
1046         if (!cstate->csv_mode && cstate->quote != NULL)
1047                 ereport(ERROR,
1048                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1049                                  errmsg("COPY quote available only in CSV mode")));
1050
1051         if (cstate->csv_mode && strlen(cstate->quote) != 1)
1052                 ereport(ERROR,
1053                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1054                                  errmsg("COPY quote must be a single one-byte character")));
1055
1056         if (cstate->csv_mode && cstate->delim[0] == cstate->quote[0])
1057                 ereport(ERROR,
1058                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1059                                  errmsg("COPY delimiter and quote must be different")));
1060
1061         /* Check escape */
1062         if (!cstate->csv_mode && cstate->escape != NULL)
1063                 ereport(ERROR,
1064                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1065                                  errmsg("COPY escape available only in CSV mode")));
1066
1067         if (cstate->csv_mode && strlen(cstate->escape) != 1)
1068                 ereport(ERROR,
1069                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1070                                  errmsg("COPY escape must be a single one-byte character")));
1071
1072         /* Check force_quote */
1073         if (!cstate->csv_mode && (cstate->force_quote || cstate->force_quote_all))
1074                 ereport(ERROR,
1075                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1076                                  errmsg("COPY force quote available only in CSV mode")));
1077         if ((cstate->force_quote || cstate->force_quote_all) && is_from)
1078                 ereport(ERROR,
1079                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1080                                  errmsg("COPY force quote only available using COPY TO")));
1081
1082         /* Check force_notnull */
1083         if (!cstate->csv_mode && cstate->force_notnull != NIL)
1084                 ereport(ERROR,
1085                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1086                                  errmsg("COPY force not null available only in CSV mode")));
1087         if (cstate->force_notnull != NIL && !is_from)
1088                 ereport(ERROR,
1089                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1090                           errmsg("COPY force not null only available using COPY FROM")));
1091
1092         /* Don't allow the delimiter to appear in the null string. */
1093         if (strchr(cstate->null_print, cstate->delim[0]) != NULL)
1094                 ereport(ERROR,
1095                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1096                 errmsg("COPY delimiter must not appear in the NULL specification")));
1097
1098         /* Don't allow the CSV quote char to appear in the null string. */
1099         if (cstate->csv_mode &&
1100                 strchr(cstate->null_print, cstate->quote[0]) != NULL)
1101                 ereport(ERROR,
1102                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1103                                  errmsg("CSV quote character must not appear in the NULL specification")));
1104 }
1105
1106 /*
1107  * Common setup routines used by BeginCopyFrom and BeginCopyTo.
1108  *
1109  * Iff <binary>, unload or reload in the binary format, as opposed to the
1110  * more wasteful but more robust and portable text format.
1111  *
1112  * Iff <oids>, unload or reload the format that includes OID information.
1113  * On input, we accept OIDs whether or not the table has an OID column,
1114  * but silently drop them if it does not.  On output, we report an error
1115  * if the user asks for OIDs in a table that has none (not providing an
1116  * OID column might seem friendlier, but could seriously confuse programs).
1117  *
1118  * If in the text format, delimit columns with delimiter <delim> and print
1119  * NULL values as <null_print>.
1120  */
1121 static CopyState
1122 BeginCopy(bool is_from,
1123                   Relation rel,
1124                   Node *raw_query,
1125                   const char *queryString,
1126                   List *attnamelist,
1127                   List *options)
1128 {
1129         CopyState       cstate;
1130         TupleDesc       tupDesc;
1131         int                     num_phys_attrs;
1132         MemoryContext oldcontext;
1133
1134         /* Allocate workspace and zero all fields */
1135         cstate = (CopyStateData *) palloc0(sizeof(CopyStateData));
1136
1137         /*
1138          * We allocate everything used by a cstate in a new memory context.
1139          * This avoids memory leaks during repeated use of COPY in a query.
1140          */
1141         cstate->copycontext = AllocSetContextCreate(CurrentMemoryContext,
1142                                                                                                 "COPY",
1143                                                                                                 ALLOCSET_DEFAULT_MINSIZE,
1144                                                                                                 ALLOCSET_DEFAULT_INITSIZE,
1145                                                                                                 ALLOCSET_DEFAULT_MAXSIZE);
1146
1147         oldcontext = MemoryContextSwitchTo(cstate->copycontext);
1148
1149         /* Extract options from the statement node tree */
1150         ProcessCopyOptions(cstate, is_from, options);
1151
1152         /* Process the source/target relation or query */
1153         if (rel)
1154         {
1155                 Assert(!raw_query);
1156
1157                 cstate->rel = rel;
1158
1159                 tupDesc = RelationGetDescr(cstate->rel);
1160
1161                 /* Don't allow COPY w/ OIDs to or from a table without them */
1162                 if (cstate->oids && !cstate->rel->rd_rel->relhasoids)
1163                         ereport(ERROR,
1164                                         (errcode(ERRCODE_UNDEFINED_COLUMN),
1165                                          errmsg("table \"%s\" does not have OIDs",
1166                                                         RelationGetRelationName(cstate->rel))));
1167         }
1168         else
1169         {
1170                 List       *rewritten;
1171                 Query      *query;
1172                 PlannedStmt *plan;
1173                 DestReceiver *dest;
1174
1175                 Assert(!is_from);
1176                 cstate->rel = NULL;
1177
1178                 /* Don't allow COPY w/ OIDs from a select */
1179                 if (cstate->oids)
1180                         ereport(ERROR,
1181                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1182                                          errmsg("COPY (SELECT) WITH OIDS is not supported")));
1183
1184                 /*
1185                  * Run parse analysis and rewrite.      Note this also acquires sufficient
1186                  * locks on the source table(s).
1187                  *
1188                  * Because the parser and planner tend to scribble on their input, we
1189                  * make a preliminary copy of the source querytree.  This prevents
1190                  * problems in the case that the COPY is in a portal or plpgsql
1191                  * function and is executed repeatedly.  (See also the same hack in
1192                  * DECLARE CURSOR and PREPARE.)  XXX FIXME someday.
1193                  */
1194                 rewritten = pg_analyze_and_rewrite((Node *) copyObject(raw_query),
1195                                                                                    queryString, NULL, 0);
1196
1197                 /* We don't expect more or less than one result query */
1198                 if (list_length(rewritten) != 1)
1199                         elog(ERROR, "unexpected rewrite result");
1200
1201                 query = (Query *) linitial(rewritten);
1202                 Assert(query->commandType == CMD_SELECT);
1203                 Assert(query->utilityStmt == NULL);
1204
1205                 /* Query mustn't use INTO, either */
1206                 if (query->intoClause)
1207                         ereport(ERROR,
1208                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1209                                          errmsg("COPY (SELECT INTO) is not supported")));
1210
1211                 /* plan the query */
1212                 plan = planner(query, 0, NULL);
1213
1214                 /*
1215                  * Use a snapshot with an updated command ID to ensure this query sees
1216                  * results of any previously executed queries.
1217                  */
1218                 PushUpdatedSnapshot(GetActiveSnapshot());
1219
1220                 /* Create dest receiver for COPY OUT */
1221                 dest = CreateDestReceiver(DestCopyOut);
1222                 ((DR_copy *) dest)->cstate = cstate;
1223
1224                 /* Create a QueryDesc requesting no output */
1225                 cstate->queryDesc = CreateQueryDesc(plan, queryString,
1226                                                                                         GetActiveSnapshot(),
1227                                                                                         InvalidSnapshot,
1228                                                                                         dest, NULL, 0);
1229
1230                 /*
1231                  * Call ExecutorStart to prepare the plan for execution.
1232                  *
1233                  * ExecutorStart computes a result tupdesc for us
1234                  */
1235                 ExecutorStart(cstate->queryDesc, 0);
1236
1237                 tupDesc = cstate->queryDesc->tupDesc;
1238         }
1239
1240         /* Generate or convert list of attributes to process */
1241         cstate->attnumlist = CopyGetAttnums(tupDesc, cstate->rel, attnamelist);
1242
1243         num_phys_attrs = tupDesc->natts;
1244
1245         /* Convert FORCE QUOTE name list to per-column flags, check validity */
1246         cstate->force_quote_flags = (bool *) palloc0(num_phys_attrs * sizeof(bool));
1247         if (cstate->force_quote_all)
1248         {
1249                 int                     i;
1250
1251                 for (i = 0; i < num_phys_attrs; i++)
1252                         cstate->force_quote_flags[i] = true;
1253         }
1254         else if (cstate->force_quote)
1255         {
1256                 List       *attnums;
1257                 ListCell   *cur;
1258
1259                 attnums = CopyGetAttnums(tupDesc, cstate->rel, cstate->force_quote);
1260
1261                 foreach(cur, attnums)
1262                 {
1263                         int                     attnum = lfirst_int(cur);
1264
1265                         if (!list_member_int(cstate->attnumlist, attnum))
1266                                 ereport(ERROR,
1267                                                 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1268                                    errmsg("FORCE QUOTE column \"%s\" not referenced by COPY",
1269                                                   NameStr(tupDesc->attrs[attnum - 1]->attname))));
1270                         cstate->force_quote_flags[attnum - 1] = true;
1271                 }
1272         }
1273
1274         /* Convert FORCE NOT NULL name list to per-column flags, check validity */
1275         cstate->force_notnull_flags = (bool *) palloc0(num_phys_attrs * sizeof(bool));
1276         if (cstate->force_notnull)
1277         {
1278                 List       *attnums;
1279                 ListCell   *cur;
1280
1281                 attnums = CopyGetAttnums(tupDesc, cstate->rel, cstate->force_notnull);
1282
1283                 foreach(cur, attnums)
1284                 {
1285                         int                     attnum = lfirst_int(cur);
1286
1287                         if (!list_member_int(cstate->attnumlist, attnum))
1288                                 ereport(ERROR,
1289                                                 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1290                                 errmsg("FORCE NOT NULL column \"%s\" not referenced by COPY",
1291                                            NameStr(tupDesc->attrs[attnum - 1]->attname))));
1292                         cstate->force_notnull_flags[attnum - 1] = true;
1293                 }
1294         }
1295
1296         /* Use client encoding when ENCODING option is not specified. */
1297         if (cstate->file_encoding < 0)
1298                 cstate->file_encoding = pg_get_client_encoding();
1299
1300         /*
1301          * Set up encoding conversion info.  Even if the file and server
1302          * encodings are the same, we must apply pg_any_to_server() to validate
1303          * data in multibyte encodings.
1304          */
1305         cstate->need_transcoding =
1306                 (cstate->file_encoding != GetDatabaseEncoding() ||
1307                  pg_database_encoding_max_length() > 1);
1308         /* See Multibyte encoding comment above */
1309         cstate->encoding_embeds_ascii = PG_ENCODING_IS_CLIENT_ONLY(cstate->file_encoding);
1310
1311         cstate->copy_dest = COPY_FILE;          /* default */
1312
1313         MemoryContextSwitchTo(oldcontext);
1314
1315         return cstate;
1316 }
1317
1318 /*
1319  * Release resources allocated in a cstate for COPY TO/FROM.
1320  */
1321 static void
1322 EndCopy(CopyState cstate)
1323 {
1324         if (cstate->filename != NULL && FreeFile(cstate->copy_file))
1325                 ereport(ERROR,
1326                                 (errcode_for_file_access(),
1327                                  errmsg("could not close file \"%s\": %m",
1328                                                 cstate->filename)));
1329
1330         MemoryContextDelete(cstate->copycontext);
1331         pfree(cstate);
1332 }
1333
1334 /*
1335  * Setup CopyState to read tuples from a table or a query for COPY TO.
1336  */
1337 static CopyState
1338 BeginCopyTo(Relation rel,
1339                         Node *query,
1340                         const char *queryString,
1341                         const char *filename,
1342                         List *attnamelist,
1343                         List *options)
1344 {
1345         CopyState       cstate;
1346         bool            pipe = (filename == NULL);
1347         MemoryContext oldcontext;
1348
1349         if (rel != NULL && rel->rd_rel->relkind != RELKIND_RELATION)
1350         {
1351                 if (rel->rd_rel->relkind == RELKIND_VIEW)
1352                         ereport(ERROR,
1353                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1354                                          errmsg("cannot copy from view \"%s\"",
1355                                                         RelationGetRelationName(rel)),
1356                                          errhint("Try the COPY (SELECT ...) TO variant.")));
1357                 else if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1358                         ereport(ERROR,
1359                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1360                                          errmsg("cannot copy from foreign table \"%s\"",
1361                                                         RelationGetRelationName(rel)),
1362                                          errhint("Try the COPY (SELECT ...) TO variant.")));
1363                 else if (rel->rd_rel->relkind == RELKIND_SEQUENCE)
1364                         ereport(ERROR,
1365                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1366                                          errmsg("cannot copy from sequence \"%s\"",
1367                                                         RelationGetRelationName(rel))));
1368                 else
1369                         ereport(ERROR,
1370                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1371                                          errmsg("cannot copy from non-table relation \"%s\"",
1372                                                         RelationGetRelationName(rel))));
1373         }
1374
1375         cstate = BeginCopy(false, rel, query, queryString, attnamelist, options);
1376         oldcontext = MemoryContextSwitchTo(cstate->copycontext);
1377
1378         if (pipe)
1379         {
1380                 if (whereToSendOutput != DestRemote)
1381                         cstate->copy_file = stdout;
1382         }
1383         else
1384         {
1385                 mode_t          oumask;         /* Pre-existing umask value */
1386                 struct stat st;
1387
1388                 /*
1389                  * Prevent write to relative path ... too easy to shoot oneself in the
1390                  * foot by overwriting a database file ...
1391                  */
1392                 if (!is_absolute_path(filename))
1393                         ereport(ERROR,
1394                                         (errcode(ERRCODE_INVALID_NAME),
1395                                          errmsg("relative path not allowed for COPY to file")));
1396
1397                 cstate->filename = pstrdup(filename);
1398                 oumask = umask(S_IWGRP | S_IWOTH);
1399                 cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_W);
1400                 umask(oumask);
1401
1402                 if (cstate->copy_file == NULL)
1403                         ereport(ERROR,
1404                                         (errcode_for_file_access(),
1405                                          errmsg("could not open file \"%s\" for writing: %m",
1406                                                         cstate->filename)));
1407
1408                 fstat(fileno(cstate->copy_file), &st);
1409                 if (S_ISDIR(st.st_mode))
1410                         ereport(ERROR,
1411                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1412                                          errmsg("\"%s\" is a directory", cstate->filename)));
1413         }
1414
1415         MemoryContextSwitchTo(oldcontext);
1416
1417         return cstate;
1418 }
1419
1420 /*
1421  * This intermediate routine exists mainly to localize the effects of setjmp
1422  * so we don't need to plaster a lot of variables with "volatile".
1423  */
1424 static uint64
1425 DoCopyTo(CopyState cstate)
1426 {
1427         bool            pipe = (cstate->filename == NULL);
1428         bool            fe_copy = (pipe && whereToSendOutput == DestRemote);
1429         uint64          processed;
1430
1431         PG_TRY();
1432         {
1433                 if (fe_copy)
1434                         SendCopyBegin(cstate);
1435
1436                 processed = CopyTo(cstate);
1437
1438                 if (fe_copy)
1439                         SendCopyEnd(cstate);
1440         }
1441         PG_CATCH();
1442         {
1443                 /*
1444                  * Make sure we turn off old-style COPY OUT mode upon error. It is
1445                  * okay to do this in all cases, since it does nothing if the mode is
1446                  * not on.
1447                  */
1448                 pq_endcopyout(true);
1449                 PG_RE_THROW();
1450         }
1451         PG_END_TRY();
1452
1453         return processed;
1454 }
1455
1456 /*
1457  * Clean up storage and release resources for COPY TO.
1458  */
1459 static void
1460 EndCopyTo(CopyState cstate)
1461 {
1462         if (cstate->queryDesc != NULL)
1463         {
1464                 /* Close down the query and free resources. */
1465                 ExecutorEnd(cstate->queryDesc);
1466                 FreeQueryDesc(cstate->queryDesc);
1467                 PopActiveSnapshot();
1468         }
1469
1470         /* Clean up storage */
1471         EndCopy(cstate);
1472 }
1473
1474 /*
1475  * Copy from relation or query TO file.
1476  */
1477 static uint64
1478 CopyTo(CopyState cstate)
1479 {
1480         TupleDesc       tupDesc;
1481         int                     num_phys_attrs;
1482         Form_pg_attribute *attr;
1483         ListCell   *cur;
1484         uint64          processed;
1485
1486         if (cstate->rel)
1487                 tupDesc = RelationGetDescr(cstate->rel);
1488         else
1489                 tupDesc = cstate->queryDesc->tupDesc;
1490         attr = tupDesc->attrs;
1491         num_phys_attrs = tupDesc->natts;
1492         cstate->null_print_client = cstate->null_print;         /* default */
1493
1494         /* We use fe_msgbuf as a per-row buffer regardless of copy_dest */
1495         cstate->fe_msgbuf = makeStringInfo();
1496
1497         /* Get info about the columns we need to process. */
1498         cstate->out_functions = (FmgrInfo *) palloc(num_phys_attrs * sizeof(FmgrInfo));
1499         foreach(cur, cstate->attnumlist)
1500         {
1501                 int                     attnum = lfirst_int(cur);
1502                 Oid                     out_func_oid;
1503                 bool            isvarlena;
1504
1505                 if (cstate->binary)
1506                         getTypeBinaryOutputInfo(attr[attnum - 1]->atttypid,
1507                                                                         &out_func_oid,
1508                                                                         &isvarlena);
1509                 else
1510                         getTypeOutputInfo(attr[attnum - 1]->atttypid,
1511                                                           &out_func_oid,
1512                                                           &isvarlena);
1513                 fmgr_info(out_func_oid, &cstate->out_functions[attnum - 1]);
1514         }
1515
1516         /*
1517          * Create a temporary memory context that we can reset once per row to
1518          * recover palloc'd memory.  This avoids any problems with leaks inside
1519          * datatype output routines, and should be faster than retail pfree's
1520          * anyway.      (We don't need a whole econtext as CopyFrom does.)
1521          */
1522         cstate->rowcontext = AllocSetContextCreate(CurrentMemoryContext,
1523                                                                                            "COPY TO",
1524                                                                                            ALLOCSET_DEFAULT_MINSIZE,
1525                                                                                            ALLOCSET_DEFAULT_INITSIZE,
1526                                                                                            ALLOCSET_DEFAULT_MAXSIZE);
1527
1528         if (cstate->binary)
1529         {
1530                 /* Generate header for a binary copy */
1531                 int32           tmp;
1532
1533                 /* Signature */
1534                 CopySendData(cstate, (char *) BinarySignature, 11);
1535                 /* Flags field */
1536                 tmp = 0;
1537                 if (cstate->oids)
1538                         tmp |= (1 << 16);
1539                 CopySendInt32(cstate, tmp);
1540                 /* No header extension */
1541                 tmp = 0;
1542                 CopySendInt32(cstate, tmp);
1543         }
1544         else
1545         {
1546                 /*
1547                  * For non-binary copy, we need to convert null_print to file
1548                  * encoding, because it will be sent directly with CopySendString.
1549                  */
1550                 if (cstate->need_transcoding)
1551                         cstate->null_print_client = pg_server_to_any(cstate->null_print,
1552                                                                                                                  cstate->null_print_len,
1553                                                                                                                  cstate->file_encoding);
1554
1555                 /* if a header has been requested send the line */
1556                 if (cstate->header_line)
1557                 {
1558                         bool            hdr_delim = false;
1559
1560                         foreach(cur, cstate->attnumlist)
1561                         {
1562                                 int                     attnum = lfirst_int(cur);
1563                                 char       *colname;
1564
1565                                 if (hdr_delim)
1566                                         CopySendChar(cstate, cstate->delim[0]);
1567                                 hdr_delim = true;
1568
1569                                 colname = NameStr(attr[attnum - 1]->attname);
1570
1571                                 CopyAttributeOutCSV(cstate, colname, false,
1572                                                                         list_length(cstate->attnumlist) == 1);
1573                         }
1574
1575                         CopySendEndOfRow(cstate);
1576                 }
1577         }
1578
1579         if (cstate->rel)
1580         {
1581                 Datum      *values;
1582                 bool       *nulls;
1583                 HeapScanDesc scandesc;
1584                 HeapTuple       tuple;
1585
1586                 values = (Datum *) palloc(num_phys_attrs * sizeof(Datum));
1587                 nulls = (bool *) palloc(num_phys_attrs * sizeof(bool));
1588
1589                 scandesc = heap_beginscan(cstate->rel, GetActiveSnapshot(), 0, NULL);
1590
1591                 processed = 0;
1592                 while ((tuple = heap_getnext(scandesc, ForwardScanDirection)) != NULL)
1593                 {
1594                         CHECK_FOR_INTERRUPTS();
1595
1596                         /* Deconstruct the tuple ... faster than repeated heap_getattr */
1597                         heap_deform_tuple(tuple, tupDesc, values, nulls);
1598
1599                         /* Format and send the data */
1600                         CopyOneRowTo(cstate, HeapTupleGetOid(tuple), values, nulls);
1601                         processed++;
1602                 }
1603
1604                 heap_endscan(scandesc);
1605
1606                 pfree(values);
1607                 pfree(nulls);
1608         }
1609         else
1610         {
1611                 /* run the plan --- the dest receiver will send tuples */
1612                 ExecutorRun(cstate->queryDesc, ForwardScanDirection, 0L);
1613                 processed = ((DR_copy *) cstate->queryDesc->dest)->processed;
1614         }
1615
1616         if (cstate->binary)
1617         {
1618                 /* Generate trailer for a binary copy */
1619                 CopySendInt16(cstate, -1);
1620                 /* Need to flush out the trailer */
1621                 CopySendEndOfRow(cstate);
1622         }
1623
1624         MemoryContextDelete(cstate->rowcontext);
1625
1626         return processed;
1627 }
1628
1629 /*
1630  * Emit one row during CopyTo().
1631  */
1632 static void
1633 CopyOneRowTo(CopyState cstate, Oid tupleOid, Datum *values, bool *nulls)
1634 {
1635         bool            need_delim = false;
1636         FmgrInfo   *out_functions = cstate->out_functions;
1637         MemoryContext oldcontext;
1638         ListCell   *cur;
1639         char       *string;
1640
1641         MemoryContextReset(cstate->rowcontext);
1642         oldcontext = MemoryContextSwitchTo(cstate->rowcontext);
1643
1644         if (cstate->binary)
1645         {
1646                 /* Binary per-tuple header */
1647                 CopySendInt16(cstate, list_length(cstate->attnumlist));
1648                 /* Send OID if wanted --- note attnumlist doesn't include it */
1649                 if (cstate->oids)
1650                 {
1651                         /* Hack --- assume Oid is same size as int32 */
1652                         CopySendInt32(cstate, sizeof(int32));
1653                         CopySendInt32(cstate, tupleOid);
1654                 }
1655         }
1656         else
1657         {
1658                 /* Text format has no per-tuple header, but send OID if wanted */
1659                 /* Assume digits don't need any quoting or encoding conversion */
1660                 if (cstate->oids)
1661                 {
1662                         string = DatumGetCString(DirectFunctionCall1(oidout,
1663                                                                                                 ObjectIdGetDatum(tupleOid)));
1664                         CopySendString(cstate, string);
1665                         need_delim = true;
1666                 }
1667         }
1668
1669         foreach(cur, cstate->attnumlist)
1670         {
1671                 int                     attnum = lfirst_int(cur);
1672                 Datum           value = values[attnum - 1];
1673                 bool            isnull = nulls[attnum - 1];
1674
1675                 if (!cstate->binary)
1676                 {
1677                         if (need_delim)
1678                                 CopySendChar(cstate, cstate->delim[0]);
1679                         need_delim = true;
1680                 }
1681
1682                 if (isnull)
1683                 {
1684                         if (!cstate->binary)
1685                                 CopySendString(cstate, cstate->null_print_client);
1686                         else
1687                                 CopySendInt32(cstate, -1);
1688                 }
1689                 else
1690                 {
1691                         if (!cstate->binary)
1692                         {
1693                                 string = OutputFunctionCall(&out_functions[attnum - 1],
1694                                                                                         value);
1695                                 if (cstate->csv_mode)
1696                                         CopyAttributeOutCSV(cstate, string,
1697                                                                                 cstate->force_quote_flags[attnum - 1],
1698                                                                                 list_length(cstate->attnumlist) == 1);
1699                                 else
1700                                         CopyAttributeOutText(cstate, string);
1701                         }
1702                         else
1703                         {
1704                                 bytea      *outputbytes;
1705
1706                                 outputbytes = SendFunctionCall(&out_functions[attnum - 1],
1707                                                                                            value);
1708                                 CopySendInt32(cstate, VARSIZE(outputbytes) - VARHDRSZ);
1709                                 CopySendData(cstate, VARDATA(outputbytes),
1710                                                          VARSIZE(outputbytes) - VARHDRSZ);
1711                         }
1712                 }
1713         }
1714
1715         CopySendEndOfRow(cstate);
1716
1717         MemoryContextSwitchTo(oldcontext);
1718 }
1719
1720
1721 /*
1722  * error context callback for COPY FROM
1723  *
1724  * The argument for the error context must be CopyState.
1725  */
1726 void
1727 CopyFromErrorCallback(void *arg)
1728 {
1729         CopyState       cstate = (CopyState) arg;
1730
1731         if (cstate->binary)
1732         {
1733                 /* can't usefully display the data */
1734                 if (cstate->cur_attname)
1735                         errcontext("COPY %s, line %d, column %s",
1736                                            cstate->cur_relname, cstate->cur_lineno,
1737                                            cstate->cur_attname);
1738                 else
1739                         errcontext("COPY %s, line %d",
1740                                            cstate->cur_relname, cstate->cur_lineno);
1741         }
1742         else
1743         {
1744                 if (cstate->cur_attname && cstate->cur_attval)
1745                 {
1746                         /* error is relevant to a particular column */
1747                         char       *attval;
1748
1749                         attval = limit_printout_length(cstate->cur_attval);
1750                         errcontext("COPY %s, line %d, column %s: \"%s\"",
1751                                            cstate->cur_relname, cstate->cur_lineno,
1752                                            cstate->cur_attname, attval);
1753                         pfree(attval);
1754                 }
1755                 else if (cstate->cur_attname)
1756                 {
1757                         /* error is relevant to a particular column, value is NULL */
1758                         errcontext("COPY %s, line %d, column %s: null input",
1759                                            cstate->cur_relname, cstate->cur_lineno,
1760                                            cstate->cur_attname);
1761                 }
1762                 else
1763                 {
1764                         /* error is relevant to a particular line */
1765                         if (cstate->line_buf_converted || !cstate->need_transcoding)
1766                         {
1767                                 char       *lineval;
1768
1769                                 lineval = limit_printout_length(cstate->line_buf.data);
1770                                 errcontext("COPY %s, line %d: \"%s\"",
1771                                                    cstate->cur_relname, cstate->cur_lineno, lineval);
1772                                 pfree(lineval);
1773                         }
1774                         else
1775                         {
1776                                 /*
1777                                  * Here, the line buffer is still in a foreign encoding, and
1778                                  * indeed it's quite likely that the error is precisely a
1779                                  * failure to do encoding conversion (ie, bad data).  We dare
1780                                  * not try to convert it, and at present there's no way to
1781                                  * regurgitate it without conversion.  So we have to punt and
1782                                  * just report the line number.
1783                                  */
1784                                 errcontext("COPY %s, line %d",
1785                                                    cstate->cur_relname, cstate->cur_lineno);
1786                         }
1787                 }
1788         }
1789 }
1790
1791 /*
1792  * Make sure we don't print an unreasonable amount of COPY data in a message.
1793  *
1794  * It would seem a lot easier to just use the sprintf "precision" limit to
1795  * truncate the string.  However, some versions of glibc have a bug/misfeature
1796  * that vsnprintf will always fail (return -1) if it is asked to truncate
1797  * a string that contains invalid byte sequences for the current encoding.
1798  * So, do our own truncation.  We return a pstrdup'd copy of the input.
1799  */
1800 static char *
1801 limit_printout_length(const char *str)
1802 {
1803 #define MAX_COPY_DATA_DISPLAY 100
1804
1805         int                     slen = strlen(str);
1806         int                     len;
1807         char       *res;
1808
1809         /* Fast path if definitely okay */
1810         if (slen <= MAX_COPY_DATA_DISPLAY)
1811                 return pstrdup(str);
1812
1813         /* Apply encoding-dependent truncation */
1814         len = pg_mbcliplen(str, slen, MAX_COPY_DATA_DISPLAY);
1815
1816         /*
1817          * Truncate, and add "..." to show we truncated the input.
1818          */
1819         res = (char *) palloc(len + 4);
1820         memcpy(res, str, len);
1821         strcpy(res + len, "...");
1822
1823         return res;
1824 }
1825
1826 /*
1827  * Copy FROM file to relation.
1828  */
1829 static uint64
1830 CopyFrom(CopyState cstate)
1831 {
1832         HeapTuple       tuple;
1833         TupleDesc       tupDesc;
1834         Datum      *values;
1835         bool       *nulls;
1836         ResultRelInfo *resultRelInfo;
1837         EState     *estate = CreateExecutorState(); /* for ExecConstraints() */
1838         ExprContext *econtext;
1839         TupleTableSlot *slot;
1840         MemoryContext oldcontext = CurrentMemoryContext;
1841         ErrorContextCallback errcontext;
1842         CommandId       mycid = GetCurrentCommandId(true);
1843         int                     hi_options = 0; /* start with default heap_insert options */
1844         BulkInsertState bistate;
1845         uint64          processed = 0;
1846
1847         Assert(cstate->rel);
1848
1849         if (cstate->rel->rd_rel->relkind != RELKIND_RELATION)
1850         {
1851                 if (cstate->rel->rd_rel->relkind == RELKIND_VIEW)
1852                         ereport(ERROR,
1853                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1854                                          errmsg("cannot copy to view \"%s\"",
1855                                                         RelationGetRelationName(cstate->rel))));
1856                 else if (cstate->rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1857                         ereport(ERROR,
1858                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1859                                          errmsg("cannot copy to foreign table \"%s\"",
1860                                                         RelationGetRelationName(cstate->rel))));
1861                 else if (cstate->rel->rd_rel->relkind == RELKIND_SEQUENCE)
1862                         ereport(ERROR,
1863                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1864                                          errmsg("cannot copy to sequence \"%s\"",
1865                                                         RelationGetRelationName(cstate->rel))));
1866                 else
1867                         ereport(ERROR,
1868                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1869                                          errmsg("cannot copy to non-table relation \"%s\"",
1870                                                         RelationGetRelationName(cstate->rel))));
1871         }
1872
1873         tupDesc = RelationGetDescr(cstate->rel);
1874
1875         /*----------
1876          * Check to see if we can avoid writing WAL
1877          *
1878          * If archive logging/streaming is not enabled *and* either
1879          *      - table was created in same transaction as this COPY
1880          *      - data is being written to relfilenode created in this transaction
1881          * then we can skip writing WAL.  It's safe because if the transaction
1882          * doesn't commit, we'll discard the table (or the new relfilenode file).
1883          * If it does commit, we'll have done the heap_sync at the bottom of this
1884          * routine first.
1885          *
1886          * As mentioned in comments in utils/rel.h, the in-same-transaction test
1887          * is not completely reliable, since in rare cases rd_createSubid or
1888          * rd_newRelfilenodeSubid can be cleared before the end of the transaction.
1889          * However this is OK since at worst we will fail to make the optimization.
1890          *
1891          * Also, if the target file is new-in-transaction, we assume that checking
1892          * FSM for free space is a waste of time, even if we must use WAL because
1893          * of archiving.  This could possibly be wrong, but it's unlikely.
1894          *
1895          * The comments for heap_insert and RelationGetBufferForTuple specify that
1896          * skipping WAL logging is only safe if we ensure that our tuples do not
1897          * go into pages containing tuples from any other transactions --- but this
1898          * must be the case if we have a new table or new relfilenode, so we need
1899          * no additional work to enforce that.
1900          *----------
1901          */
1902         if (cstate->rel->rd_createSubid != InvalidSubTransactionId ||
1903                 cstate->rel->rd_newRelfilenodeSubid != InvalidSubTransactionId)
1904         {
1905                 hi_options |= HEAP_INSERT_SKIP_FSM;
1906                 if (!XLogIsNeeded())
1907                         hi_options |= HEAP_INSERT_SKIP_WAL;
1908         }
1909
1910         /*
1911          * We need a ResultRelInfo so we can use the regular executor's
1912          * index-entry-making machinery.  (There used to be a huge amount of code
1913          * here that basically duplicated execUtils.c ...)
1914          */
1915         resultRelInfo = makeNode(ResultRelInfo);
1916         resultRelInfo->ri_RangeTableIndex = 1;          /* dummy */
1917         resultRelInfo->ri_RelationDesc = cstate->rel;
1918         resultRelInfo->ri_TrigDesc = CopyTriggerDesc(cstate->rel->trigdesc);
1919         if (resultRelInfo->ri_TrigDesc)
1920         {
1921                 resultRelInfo->ri_TrigFunctions = (FmgrInfo *)
1922                         palloc0(resultRelInfo->ri_TrigDesc->numtriggers * sizeof(FmgrInfo));
1923                 resultRelInfo->ri_TrigWhenExprs = (List **)
1924                         palloc0(resultRelInfo->ri_TrigDesc->numtriggers * sizeof(List *));
1925         }
1926         resultRelInfo->ri_TrigInstrument = NULL;
1927
1928         ExecOpenIndices(resultRelInfo);
1929
1930         estate->es_result_relations = resultRelInfo;
1931         estate->es_num_result_relations = 1;
1932         estate->es_result_relation_info = resultRelInfo;
1933
1934         /* Set up a tuple slot too */
1935         slot = ExecInitExtraTupleSlot(estate);
1936         ExecSetSlotDescriptor(slot, tupDesc);
1937
1938         /* Prepare to catch AFTER triggers. */
1939         AfterTriggerBeginQuery();
1940
1941         /*
1942          * Check BEFORE STATEMENT insertion triggers. It's debateable whether we
1943          * should do this for COPY, since it's not really an "INSERT" statement as
1944          * such. However, executing these triggers maintains consistency with the
1945          * EACH ROW triggers that we already fire on COPY.
1946          */
1947         ExecBSInsertTriggers(estate, resultRelInfo);
1948
1949         values = (Datum *) palloc(tupDesc->natts * sizeof(Datum));
1950         nulls = (bool *) palloc(tupDesc->natts * sizeof(bool));
1951
1952         bistate = GetBulkInsertState();
1953         econtext = GetPerTupleExprContext(estate);
1954
1955         /* Set up callback to identify error line number */
1956         errcontext.callback = CopyFromErrorCallback;
1957         errcontext.arg = (void *) cstate;
1958         errcontext.previous = error_context_stack;
1959         error_context_stack = &errcontext;
1960
1961         for (;;)
1962         {
1963                 bool            skip_tuple;
1964                 Oid                     loaded_oid = InvalidOid;
1965
1966                 CHECK_FOR_INTERRUPTS();
1967
1968                 /* Reset the per-tuple exprcontext */
1969                 ResetPerTupleExprContext(estate);
1970
1971                 /* Switch into its memory context */
1972                 MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
1973
1974                 if (!NextCopyFrom(cstate, econtext, values, nulls, &loaded_oid))
1975                         break;
1976
1977                 /* And now we can form the input tuple. */
1978                 tuple = heap_form_tuple(tupDesc, values, nulls);
1979
1980                 if (loaded_oid != InvalidOid)
1981                         HeapTupleSetOid(tuple, loaded_oid);
1982
1983                 /* Triggers and stuff need to be invoked in query context. */
1984                 MemoryContextSwitchTo(oldcontext);
1985
1986                 skip_tuple = false;
1987
1988                 /* BEFORE ROW INSERT Triggers */
1989                 if (resultRelInfo->ri_TrigDesc &&
1990                         resultRelInfo->ri_TrigDesc->trig_insert_before_row)
1991                 {
1992                         HeapTuple       newtuple;
1993
1994                         newtuple = ExecBRInsertTriggers(estate, resultRelInfo, tuple);
1995
1996                         if (newtuple == NULL)           /* "do nothing" */
1997                                 skip_tuple = true;
1998                         else if (newtuple != tuple) /* modified by Trigger(s) */
1999                         {
2000                                 heap_freetuple(tuple);
2001                                 tuple = newtuple;
2002                         }
2003                 }
2004
2005                 if (!skip_tuple)
2006                 {
2007                         List       *recheckIndexes = NIL;
2008
2009                         /* Place tuple in tuple slot */
2010                         ExecStoreTuple(tuple, slot, InvalidBuffer, false);
2011
2012                         /* Check the constraints of the tuple */
2013                         if (cstate->rel->rd_att->constr)
2014                                 ExecConstraints(resultRelInfo, slot, estate);
2015
2016                         /* OK, store the tuple and create index entries for it */
2017                         heap_insert(cstate->rel, tuple, mycid, hi_options, bistate);
2018
2019                         if (resultRelInfo->ri_NumIndices > 0)
2020                                 recheckIndexes = ExecInsertIndexTuples(slot, &(tuple->t_self),
2021                                                                                                            estate);
2022
2023                         /* AFTER ROW INSERT Triggers */
2024                         ExecARInsertTriggers(estate, resultRelInfo, tuple,
2025                                                                  recheckIndexes);
2026
2027                         list_free(recheckIndexes);
2028
2029                         /*
2030                          * We count only tuples not suppressed by a BEFORE INSERT trigger;
2031                          * this is the same definition used by execMain.c for counting
2032                          * tuples inserted by an INSERT command.
2033                          */
2034                         processed++;
2035                 }
2036         }
2037
2038         /* Done, clean up */
2039         error_context_stack = errcontext.previous;
2040
2041         FreeBulkInsertState(bistate);
2042
2043         MemoryContextSwitchTo(oldcontext);
2044
2045         /* Execute AFTER STATEMENT insertion triggers */
2046         ExecASInsertTriggers(estate, resultRelInfo);
2047
2048         /* Handle queued AFTER triggers */
2049         AfterTriggerEndQuery(estate);
2050
2051         pfree(values);
2052         pfree(nulls);
2053
2054         ExecResetTupleTable(estate->es_tupleTable, false);
2055
2056         ExecCloseIndices(resultRelInfo);
2057
2058         FreeExecutorState(estate);
2059
2060         /*
2061          * If we skipped writing WAL, then we need to sync the heap (but not
2062          * indexes since those use WAL anyway)
2063          */
2064         if (hi_options & HEAP_INSERT_SKIP_WAL)
2065                 heap_sync(cstate->rel);
2066
2067         return processed;
2068 }
2069
2070 /*
2071  * Setup to read tuples from a file for COPY FROM.
2072  *
2073  * 'rel': Used as a template for the tuples
2074  * 'filename': Name of server-local file to read
2075  * 'attnamelist': List of char *, columns to include. NIL selects all cols.
2076  * 'options': List of DefElem. See copy_opt_item in gram.y for selections.
2077  *
2078  * Returns a CopyState, to be passed to NextCopyFrom and related functions.
2079  */
2080 CopyState
2081 BeginCopyFrom(Relation rel,
2082                           const char *filename,
2083                           List *attnamelist,
2084                           List *options)
2085 {
2086         CopyState       cstate;
2087         bool            pipe = (filename == NULL);
2088         TupleDesc       tupDesc;
2089         Form_pg_attribute *attr;
2090         AttrNumber      num_phys_attrs,
2091                                 num_defaults;
2092         FmgrInfo   *in_functions;
2093         Oid                *typioparams;
2094         int                     attnum;
2095         Oid                     in_func_oid;
2096         int                *defmap;
2097         ExprState **defexprs;
2098         MemoryContext oldcontext;
2099
2100         cstate = BeginCopy(true, rel, NULL, NULL, attnamelist, options);
2101         oldcontext = MemoryContextSwitchTo(cstate->copycontext);
2102
2103         /* Initialize state variables */
2104         cstate->fe_eof = false;
2105         cstate->eol_type = EOL_UNKNOWN;
2106         cstate->cur_relname = RelationGetRelationName(cstate->rel);
2107         cstate->cur_lineno = 0;
2108         cstate->cur_attname = NULL;
2109         cstate->cur_attval = NULL;
2110
2111         /* Set up variables to avoid per-attribute overhead. */
2112         initStringInfo(&cstate->attribute_buf);
2113         initStringInfo(&cstate->line_buf);
2114         cstate->line_buf_converted = false;
2115         cstate->raw_buf = (char *) palloc(RAW_BUF_SIZE + 1);
2116         cstate->raw_buf_index = cstate->raw_buf_len = 0;
2117
2118         tupDesc = RelationGetDescr(cstate->rel);
2119         attr = tupDesc->attrs;
2120         num_phys_attrs = tupDesc->natts;
2121         num_defaults = 0;
2122
2123         /*
2124          * Pick up the required catalog information for each attribute in the
2125          * relation, including the input function, the element type (to pass to
2126          * the input function), and info about defaults and constraints. (Which
2127          * input function we use depends on text/binary format choice.)
2128          */
2129         in_functions = (FmgrInfo *) palloc(num_phys_attrs * sizeof(FmgrInfo));
2130         typioparams = (Oid *) palloc(num_phys_attrs * sizeof(Oid));
2131         defmap = (int *) palloc(num_phys_attrs * sizeof(int));
2132         defexprs = (ExprState **) palloc(num_phys_attrs * sizeof(ExprState *));
2133
2134         for (attnum = 1; attnum <= num_phys_attrs; attnum++)
2135         {
2136                 /* We don't need info for dropped attributes */
2137                 if (attr[attnum - 1]->attisdropped)
2138                         continue;
2139
2140                 /* Fetch the input function and typioparam info */
2141                 if (cstate->binary)
2142                         getTypeBinaryInputInfo(attr[attnum - 1]->atttypid,
2143                                                                    &in_func_oid, &typioparams[attnum - 1]);
2144                 else
2145                         getTypeInputInfo(attr[attnum - 1]->atttypid,
2146                                                          &in_func_oid, &typioparams[attnum - 1]);
2147                 fmgr_info(in_func_oid, &in_functions[attnum - 1]);
2148
2149                 /* Get default info if needed */
2150                 if (!list_member_int(cstate->attnumlist, attnum))
2151                 {
2152                         /* attribute is NOT to be copied from input */
2153                         /* use default value if one exists */
2154                         Node       *defexpr = build_column_default(cstate->rel, attnum);
2155
2156                         if (defexpr != NULL)
2157                         {
2158                                 /* Initialize expressions in copycontext. */
2159                                 defexprs[num_defaults] = ExecInitExpr(
2160                                                                 expression_planner((Expr *) defexpr), NULL);
2161                                 defmap[num_defaults] = attnum - 1;
2162                                 num_defaults++;
2163                         }
2164                 }
2165         }
2166
2167         /* We keep those variables in cstate. */
2168         cstate->in_functions = in_functions;
2169         cstate->typioparams = typioparams;
2170         cstate->defmap = defmap;
2171         cstate->defexprs = defexprs;
2172         cstate->num_defaults = num_defaults;
2173
2174         if (pipe)
2175         {
2176                 if (whereToSendOutput == DestRemote)
2177                         ReceiveCopyBegin(cstate);
2178                 else
2179                         cstate->copy_file = stdin;
2180         }
2181         else
2182         {
2183                 struct stat st;
2184
2185                 cstate->filename = pstrdup(filename);
2186                 cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_R);
2187
2188                 if (cstate->copy_file == NULL)
2189                         ereport(ERROR,
2190                                         (errcode_for_file_access(),
2191                                          errmsg("could not open file \"%s\" for reading: %m",
2192                                                         cstate->filename)));
2193
2194                 fstat(fileno(cstate->copy_file), &st);
2195                 if (S_ISDIR(st.st_mode))
2196                         ereport(ERROR,
2197                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2198                                          errmsg("\"%s\" is a directory", cstate->filename)));
2199         }
2200
2201         if (!cstate->binary)
2202         {
2203                 /* must rely on user to tell us... */
2204                 cstate->file_has_oids = cstate->oids;
2205         }
2206         else
2207         {
2208                 /* Read and verify binary header */
2209                 char            readSig[11];
2210                 int32           tmp;
2211
2212                 /* Signature */
2213                 if (CopyGetData(cstate, readSig, 11, 11) != 11 ||
2214                         memcmp(readSig, BinarySignature, 11) != 0)
2215                         ereport(ERROR,
2216                                         (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2217                                          errmsg("COPY file signature not recognized")));
2218                 /* Flags field */
2219                 if (!CopyGetInt32(cstate, &tmp))
2220                         ereport(ERROR,
2221                                         (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2222                                          errmsg("invalid COPY file header (missing flags)")));
2223                 cstate->file_has_oids = (tmp & (1 << 16)) != 0;
2224                 tmp &= ~(1 << 16);
2225                 if ((tmp >> 16) != 0)
2226                         ereport(ERROR,
2227                                         (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2228                                  errmsg("unrecognized critical flags in COPY file header")));
2229                 /* Header extension length */
2230                 if (!CopyGetInt32(cstate, &tmp) ||
2231                         tmp < 0)
2232                         ereport(ERROR,
2233                                         (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2234                                          errmsg("invalid COPY file header (missing length)")));
2235                 /* Skip extension header, if present */
2236                 while (tmp-- > 0)
2237                 {
2238                         if (CopyGetData(cstate, readSig, 1, 1) != 1)
2239                                 ereport(ERROR,
2240                                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2241                                                  errmsg("invalid COPY file header (wrong length)")));
2242                 }
2243         }
2244
2245         if (cstate->file_has_oids && cstate->binary)
2246         {
2247                 getTypeBinaryInputInfo(OIDOID,
2248                                                            &in_func_oid, &cstate->oid_typioparam);
2249                 fmgr_info(in_func_oid, &cstate->oid_in_function);
2250         }
2251
2252         /* create workspace for CopyReadAttributes results */
2253         if (!cstate->binary)
2254         {
2255                 AttrNumber      attr_count = list_length(cstate->attnumlist);
2256                 int     nfields = cstate->file_has_oids ? (attr_count + 1) : attr_count;
2257
2258                 cstate->max_fields = nfields;
2259                 cstate->raw_fields = (char **) palloc(nfields * sizeof(char *));
2260         }
2261
2262         MemoryContextSwitchTo(oldcontext);
2263
2264         return cstate;
2265 }
2266
2267 /*
2268  * Read raw fields in the next line for COPY FROM in text or csv mode.
2269  * Return false if no more lines.
2270  *
2271  * An internal temporary buffer is returned via 'fields'. It is valid until
2272  * the next call of the function. Since the function returns all raw fields
2273  * in the input file, 'nfields' could be different from the number of columns
2274  * in the relation.
2275  *
2276  * NOTE: force_not_null option are not applied to the returned fields.
2277  */
2278 bool
2279 NextCopyFromRawFields(CopyState cstate, char ***fields, int *nfields)
2280 {
2281         int                     fldct;
2282         bool            done;
2283
2284         /* only available for text or csv input */
2285         Assert(!cstate->binary);
2286
2287         /* on input just throw the header line away */
2288         if (cstate->cur_lineno == 0 && cstate->header_line)
2289         {
2290                 cstate->cur_lineno++;
2291                 if (CopyReadLine(cstate))
2292                         return false;   /* done */
2293         }
2294
2295         cstate->cur_lineno++;
2296
2297         /* Actually read the line into memory here */
2298         done = CopyReadLine(cstate);
2299
2300         /*
2301          * EOF at start of line means we're done.  If we see EOF after
2302          * some characters, we act as though it was newline followed by
2303          * EOF, ie, process the line and then exit loop on next iteration.
2304          */
2305         if (done && cstate->line_buf.len == 0)
2306                 return false;
2307
2308         /* Parse the line into de-escaped field values */
2309         if (cstate->csv_mode)
2310                 fldct = CopyReadAttributesCSV(cstate);
2311         else
2312                 fldct = CopyReadAttributesText(cstate);
2313
2314         *fields = cstate->raw_fields;
2315         *nfields = fldct;
2316         return true;
2317 }
2318
2319 /*
2320  * Read next tuple from file for COPY FROM. Return false if no more tuples.
2321  *
2322  * 'econtext' is used to evaluate default expression for each columns not
2323  * read from the file. It can be NULL when no default values are used, i.e.
2324  * when all columns are read from the file.
2325  *
2326  * 'values' and 'nulls' arrays must be the same length as columns of the
2327  * relation passed to BeginCopyFrom. This function fills the arrays.
2328  * Oid of the tuple is returned with 'tupleOid' separately.
2329  */
2330 bool
2331 NextCopyFrom(CopyState cstate, ExprContext *econtext,
2332                          Datum *values, bool *nulls, Oid *tupleOid)
2333 {
2334         TupleDesc       tupDesc;
2335         Form_pg_attribute *attr;
2336         AttrNumber      num_phys_attrs,
2337                                 attr_count,
2338                                 num_defaults = cstate->num_defaults;
2339         FmgrInfo   *in_functions = cstate->in_functions;
2340         Oid                *typioparams = cstate->typioparams;
2341         int                     i;
2342         int         nfields;
2343         bool            isnull;
2344         bool            file_has_oids = cstate->file_has_oids;
2345         int                *defmap = cstate->defmap;
2346         ExprState **defexprs = cstate->defexprs;
2347
2348         tupDesc = RelationGetDescr(cstate->rel);
2349         attr = tupDesc->attrs;
2350         num_phys_attrs = tupDesc->natts;
2351         attr_count = list_length(cstate->attnumlist);
2352         nfields = file_has_oids ? (attr_count + 1) : attr_count;
2353
2354         /* Initialize all values for row to NULL */
2355         MemSet(values, 0, num_phys_attrs * sizeof(Datum));
2356         MemSet(nulls, true, num_phys_attrs * sizeof(bool));
2357
2358         if (!cstate->binary)
2359         {
2360                 char      **field_strings;
2361                 ListCell   *cur;
2362                 int                     fldct;
2363                 int                     fieldno;
2364                 char       *string;
2365
2366                 /* read raw fields in the next line */
2367                 if (!NextCopyFromRawFields(cstate, &field_strings, &fldct))
2368                         return false;
2369
2370                 /* check for overflowing fields */
2371                 if (nfields > 0 && fldct > nfields)
2372                         ereport(ERROR,
2373                                         (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2374                                          errmsg("extra data after last expected column")));
2375
2376                 fieldno = 0;
2377
2378                 /* Read the OID field if present */
2379                 if (file_has_oids)
2380                 {
2381                         if (fieldno >= fldct)
2382                                 ereport(ERROR,
2383                                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2384                                                  errmsg("missing data for OID column")));
2385                         string = field_strings[fieldno++];
2386
2387                         if (string == NULL)
2388                                 ereport(ERROR,
2389                                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2390                                                  errmsg("null OID in COPY data")));
2391                         else if (cstate->oids && tupleOid != NULL)
2392                         {
2393                                 cstate->cur_attname = "oid";
2394                                 cstate->cur_attval = string;
2395                                 *tupleOid = DatumGetObjectId(DirectFunctionCall1(oidin,
2396                                                                                                    CStringGetDatum(string)));
2397                                 if (*tupleOid == InvalidOid)
2398                                         ereport(ERROR,
2399                                                         (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2400                                                          errmsg("invalid OID in COPY data")));
2401                                 cstate->cur_attname = NULL;
2402                                 cstate->cur_attval = NULL;
2403                         }
2404                 }
2405
2406                 /* Loop to read the user attributes on the line. */
2407                 foreach(cur, cstate->attnumlist)
2408                 {
2409                         int                     attnum = lfirst_int(cur);
2410                         int                     m = attnum - 1;
2411
2412                         if (fieldno >= fldct)
2413                                 ereport(ERROR,
2414                                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2415                                                  errmsg("missing data for column \"%s\"",
2416                                                                 NameStr(attr[m]->attname))));
2417                         string = field_strings[fieldno++];
2418
2419                         if (cstate->csv_mode && string == NULL &&
2420                                 cstate->force_notnull_flags[m])
2421                         {
2422                                 /* Go ahead and read the NULL string */
2423                                 string = cstate->null_print;
2424                         }
2425
2426                         cstate->cur_attname = NameStr(attr[m]->attname);
2427                         cstate->cur_attval = string;
2428                         values[m] = InputFunctionCall(&in_functions[m],
2429                                                                                   string,
2430                                                                                   typioparams[m],
2431                                                                                   attr[m]->atttypmod);
2432                         if (string != NULL)
2433                                 nulls[m] = false;
2434                         cstate->cur_attname = NULL;
2435                         cstate->cur_attval = NULL;
2436                 }
2437
2438                 Assert(fieldno == nfields);
2439         }
2440         else
2441         {
2442                 /* binary */
2443                 int16           fld_count;
2444                 ListCell   *cur;
2445
2446                 cstate->cur_lineno++;
2447
2448                 if (!CopyGetInt16(cstate, &fld_count))
2449                 {
2450                         /* EOF detected (end of file, or protocol-level EOF) */
2451                         return false;
2452                 }
2453
2454                 if (fld_count == -1)
2455                 {
2456                         /*
2457                          * Received EOF marker.  In a V3-protocol copy, wait for
2458                          * the protocol-level EOF, and complain if it doesn't come
2459                          * immediately.  This ensures that we correctly handle
2460                          * CopyFail, if client chooses to send that now.
2461                          *
2462                          * Note that we MUST NOT try to read more data in an
2463                          * old-protocol copy, since there is no protocol-level EOF
2464                          * marker then.  We could go either way for copy from file,
2465                          * but choose to throw error if there's data after the EOF
2466                          * marker, for consistency with the new-protocol case.
2467                          */
2468                         char    dummy;
2469
2470                         if (cstate->copy_dest != COPY_OLD_FE &&
2471                                 CopyGetData(cstate, &dummy, 1, 1) > 0)
2472                                 ereport(ERROR,
2473                                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2474                                                  errmsg("received copy data after EOF marker")));
2475                         return false;
2476                 }
2477
2478                 if (fld_count != attr_count)
2479                         ereport(ERROR,
2480                                         (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2481                                          errmsg("row field count is %d, expected %d",
2482                                                         (int) fld_count, attr_count)));
2483
2484                 if (file_has_oids)
2485                 {
2486                         Oid             loaded_oid;
2487
2488                         cstate->cur_attname = "oid";
2489                         loaded_oid =
2490                                 DatumGetObjectId(CopyReadBinaryAttribute(cstate,
2491                                                                                                                  0,
2492                                                                                                                  &cstate->oid_in_function,
2493                                                                                                                  cstate->oid_typioparam,
2494                                                                                                                  -1,
2495                                                                                                                  &isnull));
2496                         if (isnull || loaded_oid == InvalidOid)
2497                                 ereport(ERROR,
2498                                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2499                                                  errmsg("invalid OID in COPY data")));
2500                         cstate->cur_attname = NULL;
2501                         if (cstate->oids && tupleOid != NULL)
2502                                 *tupleOid = loaded_oid;
2503                 }
2504
2505                 i = 0;
2506                 foreach(cur, cstate->attnumlist)
2507                 {
2508                         int                     attnum = lfirst_int(cur);
2509                         int                     m = attnum - 1;
2510
2511                         cstate->cur_attname = NameStr(attr[m]->attname);
2512                         i++;
2513                         values[m] = CopyReadBinaryAttribute(cstate,
2514                                                                                                 i,
2515                                                                                                 &in_functions[m],
2516                                                                                                 typioparams[m],
2517                                                                                                 attr[m]->atttypmod,
2518                                                                                                 &nulls[m]);
2519                         cstate->cur_attname = NULL;
2520                 }
2521         }
2522
2523         /*
2524          * Now compute and insert any defaults available for the columns not
2525          * provided by the input data.  Anything not processed here or above
2526          * will remain NULL.
2527          */
2528         for (i = 0; i < num_defaults; i++)
2529         {
2530                 /*
2531                  * The caller must supply econtext and have switched into the
2532                  * per-tuple memory context in it.
2533                  */
2534                 Assert(econtext != NULL);
2535                 Assert(CurrentMemoryContext == econtext->ecxt_per_tuple_memory);
2536
2537                 values[defmap[i]] = ExecEvalExpr(defexprs[i], econtext,
2538                                                                                  &nulls[defmap[i]], NULL);
2539         }
2540
2541         return true;
2542 }
2543
2544 /*
2545  * Clean up storage and release resources for COPY FROM.
2546  */
2547 void
2548 EndCopyFrom(CopyState cstate)
2549 {
2550         /* No COPY FROM related resources except memory. */
2551
2552         EndCopy(cstate);
2553 }
2554
2555 /*
2556  * Read the next input line and stash it in line_buf, with conversion to
2557  * server encoding.
2558  *
2559  * Result is true if read was terminated by EOF, false if terminated
2560  * by newline.  The terminating newline or EOF marker is not included
2561  * in the final value of line_buf.
2562  */
2563 static bool
2564 CopyReadLine(CopyState cstate)
2565 {
2566         bool            result;
2567
2568         resetStringInfo(&cstate->line_buf);
2569
2570         /* Mark that encoding conversion hasn't occurred yet */
2571         cstate->line_buf_converted = false;
2572
2573         /* Parse data and transfer into line_buf */
2574         result = CopyReadLineText(cstate);
2575
2576         if (result)
2577         {
2578                 /*
2579                  * Reached EOF.  In protocol version 3, we should ignore anything
2580                  * after \. up to the protocol end of copy data.  (XXX maybe better
2581                  * not to treat \. as special?)
2582                  */
2583                 if (cstate->copy_dest == COPY_NEW_FE)
2584                 {
2585                         do
2586                         {
2587                                 cstate->raw_buf_index = cstate->raw_buf_len;
2588                         } while (CopyLoadRawBuf(cstate));
2589                 }
2590         }
2591         else
2592         {
2593                 /*
2594                  * If we didn't hit EOF, then we must have transferred the EOL marker
2595                  * to line_buf along with the data.  Get rid of it.
2596                  */
2597                 switch (cstate->eol_type)
2598                 {
2599                         case EOL_NL:
2600                                 Assert(cstate->line_buf.len >= 1);
2601                                 Assert(cstate->line_buf.data[cstate->line_buf.len - 1] == '\n');
2602                                 cstate->line_buf.len--;
2603                                 cstate->line_buf.data[cstate->line_buf.len] = '\0';
2604                                 break;
2605                         case EOL_CR:
2606                                 Assert(cstate->line_buf.len >= 1);
2607                                 Assert(cstate->line_buf.data[cstate->line_buf.len - 1] == '\r');
2608                                 cstate->line_buf.len--;
2609                                 cstate->line_buf.data[cstate->line_buf.len] = '\0';
2610                                 break;
2611                         case EOL_CRNL:
2612                                 Assert(cstate->line_buf.len >= 2);
2613                                 Assert(cstate->line_buf.data[cstate->line_buf.len - 2] == '\r');
2614                                 Assert(cstate->line_buf.data[cstate->line_buf.len - 1] == '\n');
2615                                 cstate->line_buf.len -= 2;
2616                                 cstate->line_buf.data[cstate->line_buf.len] = '\0';
2617                                 break;
2618                         case EOL_UNKNOWN:
2619                                 /* shouldn't get here */
2620                                 Assert(false);
2621                                 break;
2622                 }
2623         }
2624
2625         /* Done reading the line.  Convert it to server encoding. */
2626         if (cstate->need_transcoding)
2627         {
2628                 char       *cvt;
2629
2630                 cvt = pg_any_to_server(cstate->line_buf.data,
2631                                                            cstate->line_buf.len,
2632                                                            cstate->file_encoding);
2633                 if (cvt != cstate->line_buf.data)
2634                 {
2635                         /* transfer converted data back to line_buf */
2636                         resetStringInfo(&cstate->line_buf);
2637                         appendBinaryStringInfo(&cstate->line_buf, cvt, strlen(cvt));
2638                         pfree(cvt);
2639                 }
2640         }
2641
2642         /* Now it's safe to use the buffer in error messages */
2643         cstate->line_buf_converted = true;
2644
2645         return result;
2646 }
2647
2648 /*
2649  * CopyReadLineText - inner loop of CopyReadLine for text mode
2650  */
2651 static bool
2652 CopyReadLineText(CopyState cstate)
2653 {
2654         char       *copy_raw_buf;
2655         int                     raw_buf_ptr;
2656         int                     copy_buf_len;
2657         bool            need_data = false;
2658         bool            hit_eof = false;
2659         bool            result = false;
2660         char            mblen_str[2];
2661
2662         /* CSV variables */
2663         bool            first_char_in_line = true;
2664         bool            in_quote = false,
2665                                 last_was_esc = false;
2666         char            quotec = '\0';
2667         char            escapec = '\0';
2668
2669         if (cstate->csv_mode)
2670         {
2671                 quotec = cstate->quote[0];
2672                 escapec = cstate->escape[0];
2673                 /* ignore special escape processing if it's the same as quotec */
2674                 if (quotec == escapec)
2675                         escapec = '\0';
2676         }
2677
2678         mblen_str[1] = '\0';
2679
2680         /*
2681          * The objective of this loop is to transfer the entire next input line
2682          * into line_buf.  Hence, we only care for detecting newlines (\r and/or
2683          * \n) and the end-of-copy marker (\.).
2684          *
2685          * In CSV mode, \r and \n inside a quoted field are just part of the data
2686          * value and are put in line_buf.  We keep just enough state to know if we
2687          * are currently in a quoted field or not.
2688          *
2689          * These four characters, and the CSV escape and quote characters, are
2690          * assumed the same in frontend and backend encodings.
2691          *
2692          * For speed, we try to move data from raw_buf to line_buf in chunks
2693          * rather than one character at a time.  raw_buf_ptr points to the next
2694          * character to examine; any characters from raw_buf_index to raw_buf_ptr
2695          * have been determined to be part of the line, but not yet transferred to
2696          * line_buf.
2697          *
2698          * For a little extra speed within the loop, we copy raw_buf and
2699          * raw_buf_len into local variables.
2700          */
2701         copy_raw_buf = cstate->raw_buf;
2702         raw_buf_ptr = cstate->raw_buf_index;
2703         copy_buf_len = cstate->raw_buf_len;
2704
2705         for (;;)
2706         {
2707                 int                     prev_raw_ptr;
2708                 char            c;
2709
2710                 /*
2711                  * Load more data if needed.  Ideally we would just force four bytes
2712                  * of read-ahead and avoid the many calls to
2713                  * IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(), but the COPY_OLD_FE protocol
2714                  * does not allow us to read too far ahead or we might read into the
2715                  * next data, so we read-ahead only as far we know we can.      One
2716                  * optimization would be to read-ahead four byte here if
2717                  * cstate->copy_dest != COPY_OLD_FE, but it hardly seems worth it,
2718                  * considering the size of the buffer.
2719                  */
2720                 if (raw_buf_ptr >= copy_buf_len || need_data)
2721                 {
2722                         REFILL_LINEBUF;
2723
2724                         /*
2725                          * Try to read some more data.  This will certainly reset
2726                          * raw_buf_index to zero, and raw_buf_ptr must go with it.
2727                          */
2728                         if (!CopyLoadRawBuf(cstate))
2729                                 hit_eof = true;
2730                         raw_buf_ptr = 0;
2731                         copy_buf_len = cstate->raw_buf_len;
2732
2733                         /*
2734                          * If we are completely out of data, break out of the loop,
2735                          * reporting EOF.
2736                          */
2737                         if (copy_buf_len <= 0)
2738                         {
2739                                 result = true;
2740                                 break;
2741                         }
2742                         need_data = false;
2743                 }
2744
2745                 /* OK to fetch a character */
2746                 prev_raw_ptr = raw_buf_ptr;
2747                 c = copy_raw_buf[raw_buf_ptr++];
2748
2749                 if (cstate->csv_mode)
2750                 {
2751                         /*
2752                          * If character is '\\' or '\r', we may need to look ahead below.
2753                          * Force fetch of the next character if we don't already have it.
2754                          * We need to do this before changing CSV state, in case one of
2755                          * these characters is also the quote or escape character.
2756                          *
2757                          * Note: old-protocol does not like forced prefetch, but it's OK
2758                          * here since we cannot validly be at EOF.
2759                          */
2760                         if (c == '\\' || c == '\r')
2761                         {
2762                                 IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(0);
2763                         }
2764
2765                         /*
2766                          * Dealing with quotes and escapes here is mildly tricky. If the
2767                          * quote char is also the escape char, there's no problem - we
2768                          * just use the char as a toggle. If they are different, we need
2769                          * to ensure that we only take account of an escape inside a
2770                          * quoted field and immediately preceding a quote char, and not
2771                          * the second in a escape-escape sequence.
2772                          */
2773                         if (in_quote && c == escapec)
2774                                 last_was_esc = !last_was_esc;
2775                         if (c == quotec && !last_was_esc)
2776                                 in_quote = !in_quote;
2777                         if (c != escapec)
2778                                 last_was_esc = false;
2779
2780                         /*
2781                          * Updating the line count for embedded CR and/or LF chars is
2782                          * necessarily a little fragile - this test is probably about the
2783                          * best we can do.      (XXX it's arguable whether we should do this
2784                          * at all --- is cur_lineno a physical or logical count?)
2785                          */
2786                         if (in_quote && c == (cstate->eol_type == EOL_NL ? '\n' : '\r'))
2787                                 cstate->cur_lineno++;
2788                 }
2789
2790                 /* Process \r */
2791                 if (c == '\r' && (!cstate->csv_mode || !in_quote))
2792                 {
2793                         /* Check for \r\n on first line, _and_ handle \r\n. */
2794                         if (cstate->eol_type == EOL_UNKNOWN ||
2795                                 cstate->eol_type == EOL_CRNL)
2796                         {
2797                                 /*
2798                                  * If need more data, go back to loop top to load it.
2799                                  *
2800                                  * Note that if we are at EOF, c will wind up as '\0' because
2801                                  * of the guaranteed pad of raw_buf.
2802                                  */
2803                                 IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(0);
2804
2805                                 /* get next char */
2806                                 c = copy_raw_buf[raw_buf_ptr];
2807
2808                                 if (c == '\n')
2809                                 {
2810                                         raw_buf_ptr++;          /* eat newline */
2811                                         cstate->eol_type = EOL_CRNL;            /* in case not set yet */
2812                                 }
2813                                 else
2814                                 {
2815                                         /* found \r, but no \n */
2816                                         if (cstate->eol_type == EOL_CRNL)
2817                                                 ereport(ERROR,
2818                                                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2819                                                                  !cstate->csv_mode ?
2820                                                         errmsg("literal carriage return found in data") :
2821                                                         errmsg("unquoted carriage return found in data"),
2822                                                                  !cstate->csv_mode ?
2823                                                 errhint("Use \"\\r\" to represent carriage return.") :
2824                                                                  errhint("Use quoted CSV field to represent carriage return.")));
2825
2826                                         /*
2827                                          * if we got here, it is the first line and we didn't find
2828                                          * \n, so don't consume the peeked character
2829                                          */
2830                                         cstate->eol_type = EOL_CR;
2831                                 }
2832                         }
2833                         else if (cstate->eol_type == EOL_NL)
2834                                 ereport(ERROR,
2835                                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2836                                                  !cstate->csv_mode ?
2837                                                  errmsg("literal carriage return found in data") :
2838                                                  errmsg("unquoted carriage return found in data"),
2839                                                  !cstate->csv_mode ?
2840                                            errhint("Use \"\\r\" to represent carriage return.") :
2841                                                  errhint("Use quoted CSV field to represent carriage return.")));
2842                         /* If reach here, we have found the line terminator */
2843                         break;
2844                 }
2845
2846                 /* Process \n */
2847                 if (c == '\n' && (!cstate->csv_mode || !in_quote))
2848                 {
2849                         if (cstate->eol_type == EOL_CR || cstate->eol_type == EOL_CRNL)
2850                                 ereport(ERROR,
2851                                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2852                                                  !cstate->csv_mode ?
2853                                                  errmsg("literal newline found in data") :
2854                                                  errmsg("unquoted newline found in data"),
2855                                                  !cstate->csv_mode ?
2856                                                  errhint("Use \"\\n\" to represent newline.") :
2857                                          errhint("Use quoted CSV field to represent newline.")));
2858                         cstate->eol_type = EOL_NL;      /* in case not set yet */
2859                         /* If reach here, we have found the line terminator */
2860                         break;
2861                 }
2862
2863                 /*
2864                  * In CSV mode, we only recognize \. alone on a line.  This is because
2865                  * \. is a valid CSV data value.
2866                  */
2867                 if (c == '\\' && (!cstate->csv_mode || first_char_in_line))
2868                 {
2869                         char            c2;
2870
2871                         IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(0);
2872                         IF_NEED_REFILL_AND_EOF_BREAK(0);
2873
2874                         /* -----
2875                          * get next character
2876                          * Note: we do not change c so if it isn't \., we can fall
2877                          * through and continue processing for file encoding.
2878                          * -----
2879                          */
2880                         c2 = copy_raw_buf[raw_buf_ptr];
2881
2882                         if (c2 == '.')
2883                         {
2884                                 raw_buf_ptr++;  /* consume the '.' */
2885
2886                                 /*
2887                                  * Note: if we loop back for more data here, it does not
2888                                  * matter that the CSV state change checks are re-executed; we
2889                                  * will come back here with no important state changed.
2890                                  */
2891                                 if (cstate->eol_type == EOL_CRNL)
2892                                 {
2893                                         /* Get the next character */
2894                                         IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(0);
2895                                         /* if hit_eof, c2 will become '\0' */
2896                                         c2 = copy_raw_buf[raw_buf_ptr++];
2897
2898                                         if (c2 == '\n')
2899                                         {
2900                                                 if (!cstate->csv_mode)
2901                                                         ereport(ERROR,
2902                                                                         (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2903                                                                          errmsg("end-of-copy marker does not match previous newline style")));
2904                                                 else
2905                                                         NO_END_OF_COPY_GOTO;
2906                                         }
2907                                         else if (c2 != '\r')
2908                                         {
2909                                                 if (!cstate->csv_mode)
2910                                                         ereport(ERROR,
2911                                                                         (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2912                                                                          errmsg("end-of-copy marker corrupt")));
2913                                                 else
2914                                                         NO_END_OF_COPY_GOTO;
2915                                         }
2916                                 }
2917
2918                                 /* Get the next character */
2919                                 IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(0);
2920                                 /* if hit_eof, c2 will become '\0' */
2921                                 c2 = copy_raw_buf[raw_buf_ptr++];
2922
2923                                 if (c2 != '\r' && c2 != '\n')
2924                                 {
2925                                         if (!cstate->csv_mode)
2926                                                 ereport(ERROR,
2927                                                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2928                                                                  errmsg("end-of-copy marker corrupt")));
2929                                         else
2930                                                 NO_END_OF_COPY_GOTO;
2931                                 }
2932
2933                                 if ((cstate->eol_type == EOL_NL && c2 != '\n') ||
2934                                         (cstate->eol_type == EOL_CRNL && c2 != '\n') ||
2935                                         (cstate->eol_type == EOL_CR && c2 != '\r'))
2936                                 {
2937                                         ereport(ERROR,
2938                                                         (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2939                                                          errmsg("end-of-copy marker does not match previous newline style")));
2940                                 }
2941
2942                                 /*
2943                                  * Transfer only the data before the \. into line_buf, then
2944                                  * discard the data and the \. sequence.
2945                                  */
2946                                 if (prev_raw_ptr > cstate->raw_buf_index)
2947                                         appendBinaryStringInfo(&cstate->line_buf,
2948                                                                          cstate->raw_buf + cstate->raw_buf_index,
2949                                                                            prev_raw_ptr - cstate->raw_buf_index);
2950                                 cstate->raw_buf_index = raw_buf_ptr;
2951                                 result = true;  /* report EOF */
2952                                 break;
2953                         }
2954                         else if (!cstate->csv_mode)
2955
2956                                 /*
2957                                  * If we are here, it means we found a backslash followed by
2958                                  * something other than a period.  In non-CSV mode, anything
2959                                  * after a backslash is special, so we skip over that second
2960                                  * character too.  If we didn't do that \\. would be
2961                                  * considered an eof-of copy, while in non-CSV mode it is a
2962                                  * literal backslash followed by a period.      In CSV mode,
2963                                  * backslashes are not special, so we want to process the
2964                                  * character after the backslash just like a normal character,
2965                                  * so we don't increment in those cases.
2966                                  */
2967                                 raw_buf_ptr++;
2968                 }
2969
2970                 /*
2971                  * This label is for CSV cases where \. appears at the start of a
2972                  * line, but there is more text after it, meaning it was a data value.
2973                  * We are more strict for \. in CSV mode because \. could be a data
2974                  * value, while in non-CSV mode, \. cannot be a data value.
2975                  */
2976 not_end_of_copy:
2977
2978                 /*
2979                  * Process all bytes of a multi-byte character as a group.
2980                  *
2981                  * We only support multi-byte sequences where the first byte has the
2982                  * high-bit set, so as an optimization we can avoid this block
2983                  * entirely if it is not set.
2984                  */
2985                 if (cstate->encoding_embeds_ascii && IS_HIGHBIT_SET(c))
2986                 {
2987                         int                     mblen;
2988
2989                         mblen_str[0] = c;
2990                         /* All our encodings only read the first byte to get the length */
2991                         mblen = pg_encoding_mblen(cstate->file_encoding, mblen_str);
2992                         IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(mblen - 1);
2993                         IF_NEED_REFILL_AND_EOF_BREAK(mblen - 1);
2994                         raw_buf_ptr += mblen - 1;
2995                 }
2996                 first_char_in_line = false;
2997         }                                                       /* end of outer loop */
2998
2999         /*
3000          * Transfer any still-uncopied data to line_buf.
3001          */
3002         REFILL_LINEBUF;
3003
3004         return result;
3005 }
3006
3007 /*
3008  *      Return decimal value for a hexadecimal digit
3009  */
3010 static int
3011 GetDecimalFromHex(char hex)
3012 {
3013         if (isdigit((unsigned char) hex))
3014                 return hex - '0';
3015         else
3016                 return tolower((unsigned char) hex) - 'a' + 10;
3017 }
3018
3019 /*
3020  * Parse the current line into separate attributes (fields),
3021  * performing de-escaping as needed.
3022  *
3023  * The input is in line_buf.  We use attribute_buf to hold the result
3024  * strings.  cstate->raw_fields[k] is set to point to the k'th attribute 
3025  * string, or NULL when the input matches the null marker string.  
3026  * This array is expanded as necessary.
3027  *
3028  * (Note that the caller cannot check for nulls since the returned 
3029  * string would be the post-de-escaping equivalent, which may look 
3030  * the same as some valid data string.)
3031  *
3032  * delim is the column delimiter string (must be just one byte for now).
3033  * null_print is the null marker string.  Note that this is compared to
3034  * the pre-de-escaped input string.
3035  *
3036  * The return value is the number of fields actually read.
3037  */
3038 static int
3039 CopyReadAttributesText(CopyState cstate)
3040 {
3041         char            delimc = cstate->delim[0];
3042         int                     fieldno;
3043         char       *output_ptr;
3044         char       *cur_ptr;
3045         char       *line_end_ptr;
3046
3047         /*
3048          * We need a special case for zero-column tables: check that the input
3049          * line is empty, and return.
3050          */
3051         if (cstate->max_fields <= 0)
3052         {
3053                 if (cstate->line_buf.len != 0)
3054                         ereport(ERROR,
3055                                         (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3056                                          errmsg("extra data after last expected column")));
3057                 return 0;
3058         }
3059
3060         resetStringInfo(&cstate->attribute_buf);
3061
3062         /*
3063          * The de-escaped attributes will certainly not be longer than the input
3064          * data line, so we can just force attribute_buf to be large enough and
3065          * then transfer data without any checks for enough space.      We need to do
3066          * it this way because enlarging attribute_buf mid-stream would invalidate
3067          * pointers already stored into cstate->raw_fields[].
3068          */
3069         if (cstate->attribute_buf.maxlen <= cstate->line_buf.len)
3070                 enlargeStringInfo(&cstate->attribute_buf, cstate->line_buf.len);
3071         output_ptr = cstate->attribute_buf.data;
3072
3073         /* set pointer variables for loop */
3074         cur_ptr = cstate->line_buf.data;
3075         line_end_ptr = cstate->line_buf.data + cstate->line_buf.len;
3076
3077         /* Outer loop iterates over fields */
3078         fieldno = 0;
3079         for (;;)
3080         {
3081                 bool            found_delim = false;
3082                 char       *start_ptr;
3083                 char       *end_ptr;
3084                 int                     input_len;
3085                 bool            saw_non_ascii = false;
3086
3087                 /* Make sure there is enough space for the next value */
3088                 if (fieldno >= cstate->max_fields)
3089                 {
3090                         cstate->max_fields *= 2;
3091                         cstate->raw_fields = 
3092                                 repalloc(cstate->raw_fields, cstate->max_fields*sizeof(char *));
3093                 }
3094
3095                 /* Remember start of field on both input and output sides */
3096                 start_ptr = cur_ptr;
3097                 cstate->raw_fields[fieldno] = output_ptr;
3098
3099                 /* Scan data for field */
3100                 for (;;)
3101                 {
3102                         char            c;
3103
3104                         end_ptr = cur_ptr;
3105                         if (cur_ptr >= line_end_ptr)
3106                                 break;
3107                         c = *cur_ptr++;
3108                         if (c == delimc)
3109                         {
3110                                 found_delim = true;
3111                                 break;
3112                         }
3113                         if (c == '\\')
3114                         {
3115                                 if (cur_ptr >= line_end_ptr)
3116                                         break;
3117                                 c = *cur_ptr++;
3118                                 switch (c)
3119                                 {
3120                                         case '0':
3121                                         case '1':
3122                                         case '2':
3123                                         case '3':
3124                                         case '4':
3125                                         case '5':
3126                                         case '6':
3127                                         case '7':
3128                                                 {
3129                                                         /* handle \013 */
3130                                                         int                     val;
3131
3132                                                         val = OCTVALUE(c);
3133                                                         if (cur_ptr < line_end_ptr)
3134                                                         {
3135                                                                 c = *cur_ptr;
3136                                                                 if (ISOCTAL(c))
3137                                                                 {
3138                                                                         cur_ptr++;
3139                                                                         val = (val << 3) + OCTVALUE(c);
3140                                                                         if (cur_ptr < line_end_ptr)
3141                                                                         {
3142                                                                                 c = *cur_ptr;
3143                                                                                 if (ISOCTAL(c))
3144                                                                                 {
3145                                                                                         cur_ptr++;
3146                                                                                         val = (val << 3) + OCTVALUE(c);
3147                                                                                 }
3148                                                                         }
3149                                                                 }
3150                                                         }
3151                                                         c = val & 0377;
3152                                                         if (c == '\0' || IS_HIGHBIT_SET(c))
3153                                                                 saw_non_ascii = true;
3154                                                 }
3155                                                 break;
3156                                         case 'x':
3157                                                 /* Handle \x3F */
3158                                                 if (cur_ptr < line_end_ptr)
3159                                                 {
3160                                                         char            hexchar = *cur_ptr;
3161
3162                                                         if (isxdigit((unsigned char) hexchar))
3163                                                         {
3164                                                                 int                     val = GetDecimalFromHex(hexchar);
3165
3166                                                                 cur_ptr++;
3167                                                                 if (cur_ptr < line_end_ptr)
3168                                                                 {
3169                                                                         hexchar = *cur_ptr;
3170                                                                         if (isxdigit((unsigned char) hexchar))
3171                                                                         {
3172                                                                                 cur_ptr++;
3173                                                                                 val = (val << 4) + GetDecimalFromHex(hexchar);
3174                                                                         }
3175                                                                 }
3176                                                                 c = val & 0xff;
3177                                                                 if (c == '\0' || IS_HIGHBIT_SET(c))
3178                                                                         saw_non_ascii = true;
3179                                                         }
3180                                                 }
3181                                                 break;
3182                                         case 'b':
3183                                                 c = '\b';
3184                                                 break;
3185                                         case 'f':
3186                                                 c = '\f';
3187                                                 break;
3188                                         case 'n':
3189                                                 c = '\n';
3190                                                 break;
3191                                         case 'r':
3192                                                 c = '\r';
3193                                                 break;
3194                                         case 't':
3195                                                 c = '\t';
3196                                                 break;
3197                                         case 'v':
3198                                                 c = '\v';
3199                                                 break;
3200
3201                                                 /*
3202                                                  * in all other cases, take the char after '\'
3203                                                  * literally
3204                                                  */
3205                                 }
3206                         }
3207
3208                         /* Add c to output string */
3209                         *output_ptr++ = c;
3210                 }
3211
3212                 /* Terminate attribute value in output area */
3213                 *output_ptr++ = '\0';
3214
3215                 /*
3216                  * If we de-escaped a non-7-bit-ASCII char, make sure we still have
3217                  * valid data for the db encoding. Avoid calling strlen here for the
3218                  * sake of efficiency.
3219                  */
3220                 if (saw_non_ascii)
3221                 {
3222                         char       *fld = cstate->raw_fields[fieldno];
3223
3224                         pg_verifymbstr(fld, output_ptr - (fld + 1), false);
3225                 }
3226
3227                 /* Check whether raw input matched null marker */
3228                 input_len = end_ptr - start_ptr;
3229                 if (input_len == cstate->null_print_len &&
3230                         strncmp(start_ptr, cstate->null_print, input_len) == 0)
3231                         cstate->raw_fields[fieldno] = NULL;
3232
3233                 fieldno++;
3234                 /* Done if we hit EOL instead of a delim */
3235                 if (!found_delim)
3236                         break;
3237         }
3238
3239         /* Clean up state of attribute_buf */
3240         output_ptr--;
3241         Assert(*output_ptr == '\0');
3242         cstate->attribute_buf.len = (output_ptr - cstate->attribute_buf.data);
3243
3244         return fieldno;
3245 }
3246
3247 /*
3248  * Parse the current line into separate attributes (fields),
3249  * performing de-escaping as needed.  This has exactly the same API as
3250  * CopyReadAttributesText, except we parse the fields according to
3251  * "standard" (i.e. common) CSV usage.
3252  */
3253 static int
3254 CopyReadAttributesCSV(CopyState cstate)
3255 {
3256         char            delimc = cstate->delim[0];
3257         char            quotec = cstate->quote[0];
3258         char            escapec = cstate->escape[0];
3259         int                     fieldno;
3260         char       *output_ptr;
3261         char       *cur_ptr;
3262         char       *line_end_ptr;
3263
3264         /*
3265          * We need a special case for zero-column tables: check that the input
3266          * line is empty, and return.
3267          */
3268         if (cstate->max_fields <= 0)
3269         {
3270                 if (cstate->line_buf.len != 0)
3271                         ereport(ERROR,
3272                                         (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3273                                          errmsg("extra data after last expected column")));
3274                 return 0;
3275         }
3276
3277         resetStringInfo(&cstate->attribute_buf);
3278
3279         /*
3280          * The de-escaped attributes will certainly not be longer than the input
3281          * data line, so we can just force attribute_buf to be large enough and
3282          * then transfer data without any checks for enough space.      We need to do
3283          * it this way because enlarging attribute_buf mid-stream would invalidate
3284          * pointers already stored into cstate->raw_fields[].
3285          */
3286         if (cstate->attribute_buf.maxlen <= cstate->line_buf.len)
3287                 enlargeStringInfo(&cstate->attribute_buf, cstate->line_buf.len);
3288         output_ptr = cstate->attribute_buf.data;
3289
3290         /* set pointer variables for loop */
3291         cur_ptr = cstate->line_buf.data;
3292         line_end_ptr = cstate->line_buf.data + cstate->line_buf.len;
3293
3294         /* Outer loop iterates over fields */
3295         fieldno = 0;
3296         for (;;)
3297         {
3298                 bool            found_delim = false;
3299                 bool            saw_quote = false;
3300                 char       *start_ptr;
3301                 char       *end_ptr;
3302                 int                     input_len;
3303
3304                 /* Make sure there is enough space for the next value */
3305                 if (fieldno >= cstate->max_fields)
3306                 {
3307                         cstate->max_fields *= 2;
3308                         cstate->raw_fields = 
3309                                 repalloc(cstate->raw_fields, cstate->max_fields*sizeof(char *));
3310                 }
3311
3312                 /* Remember start of field on both input and output sides */
3313                 start_ptr = cur_ptr;
3314                 cstate->raw_fields[fieldno] = output_ptr;
3315
3316                 /*
3317                  * Scan data for field,
3318                  *
3319                  * The loop starts in "not quote" mode and then toggles between that
3320                  * and "in quote" mode. The loop exits normally if it is in "not
3321                  * quote" mode and a delimiter or line end is seen.
3322                  */
3323                 for (;;)
3324                 {
3325                         char            c;
3326
3327                         /* Not in quote */
3328                         for (;;)
3329                         {
3330                                 end_ptr = cur_ptr;
3331                                 if (cur_ptr >= line_end_ptr)
3332                                         goto endfield;
3333                                 c = *cur_ptr++;
3334                                 /* unquoted field delimiter */
3335                                 if (c == delimc)
3336                                 {
3337                                         found_delim = true;
3338                                         goto endfield;
3339                                 }
3340                                 /* start of quoted field (or part of field) */
3341                                 if (c == quotec)
3342                                 {
3343                                         saw_quote = true;
3344                                         break;
3345                                 }
3346                                 /* Add c to output string */
3347                                 *output_ptr++ = c;
3348                         }
3349
3350                         /* In quote */
3351                         for (;;)
3352                         {
3353                                 end_ptr = cur_ptr;
3354                                 if (cur_ptr >= line_end_ptr)
3355                                         ereport(ERROR,
3356                                                         (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3357                                                          errmsg("unterminated CSV quoted field")));
3358
3359                                 c = *cur_ptr++;
3360
3361                                 /* escape within a quoted field */
3362                                 if (c == escapec)
3363                                 {
3364                                         /*
3365                                          * peek at the next char if available, and escape it if it
3366                                          * is an escape char or a quote char
3367                                          */
3368                                         if (cur_ptr < line_end_ptr)
3369                                         {
3370                                                 char            nextc = *cur_ptr;
3371
3372                                                 if (nextc == escapec || nextc == quotec)
3373                                                 {
3374                                                         *output_ptr++ = nextc;
3375                                                         cur_ptr++;
3376                                                         continue;
3377                                                 }
3378                                         }
3379                                 }
3380
3381                                 /*
3382                                  * end of quoted field. Must do this test after testing for
3383                                  * escape in case quote char and escape char are the same
3384                                  * (which is the common case).
3385                                  */
3386                                 if (c == quotec)
3387                                         break;
3388
3389                                 /* Add c to output string */
3390                                 *output_ptr++ = c;
3391                         }
3392                 }
3393 endfield:
3394
3395                 /* Terminate attribute value in output area */
3396                 *output_ptr++ = '\0';
3397
3398                 /* Check whether raw input matched null marker */
3399                 input_len = end_ptr - start_ptr;
3400                 if (!saw_quote && input_len == cstate->null_print_len &&
3401                         strncmp(start_ptr, cstate->null_print, input_len) == 0)
3402                         cstate->raw_fields[fieldno] = NULL;
3403
3404                 fieldno++;
3405                 /* Done if we hit EOL instead of a delim */
3406                 if (!found_delim)
3407                         break;
3408         }
3409
3410         /* Clean up state of attribute_buf */
3411         output_ptr--;
3412         Assert(*output_ptr == '\0');
3413         cstate->attribute_buf.len = (output_ptr - cstate->attribute_buf.data);
3414
3415         return fieldno;
3416 }
3417
3418
3419 /*
3420  * Read a binary attribute
3421  */
3422 static Datum
3423 CopyReadBinaryAttribute(CopyState cstate,
3424                                                 int column_no, FmgrInfo *flinfo,
3425                                                 Oid typioparam, int32 typmod,
3426                                                 bool *isnull)
3427 {
3428         int32           fld_size;
3429         Datum           result;
3430
3431         if (!CopyGetInt32(cstate, &fld_size))
3432                 ereport(ERROR,
3433                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3434                                  errmsg("unexpected EOF in COPY data")));
3435         if (fld_size == -1)
3436         {
3437                 *isnull = true;
3438                 return ReceiveFunctionCall(flinfo, NULL, typioparam, typmod);
3439         }
3440         if (fld_size < 0)
3441                 ereport(ERROR,
3442                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3443                                  errmsg("invalid field size")));
3444
3445         /* reset attribute_buf to empty, and load raw data in it */
3446         resetStringInfo(&cstate->attribute_buf);
3447
3448         enlargeStringInfo(&cstate->attribute_buf, fld_size);
3449         if (CopyGetData(cstate, cstate->attribute_buf.data,
3450                                         fld_size, fld_size) != fld_size)
3451                 ereport(ERROR,
3452                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3453                                  errmsg("unexpected EOF in COPY data")));
3454
3455         cstate->attribute_buf.len = fld_size;
3456         cstate->attribute_buf.data[fld_size] = '\0';
3457
3458         /* Call the column type's binary input converter */
3459         result = ReceiveFunctionCall(flinfo, &cstate->attribute_buf,
3460                                                                  typioparam, typmod);
3461
3462         /* Trouble if it didn't eat the whole buffer */
3463         if (cstate->attribute_buf.cursor != cstate->attribute_buf.len)
3464                 ereport(ERROR,
3465                                 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
3466                                  errmsg("incorrect binary data format")));
3467
3468         *isnull = false;
3469         return result;
3470 }
3471
3472 /*
3473  * Send text representation of one attribute, with conversion and escaping
3474  */
3475 #define DUMPSOFAR() \
3476         do { \
3477                 if (ptr > start) \
3478                         CopySendData(cstate, start, ptr - start); \
3479         } while (0)
3480
3481 static void
3482 CopyAttributeOutText(CopyState cstate, char *string)
3483 {
3484         char       *ptr;
3485         char       *start;
3486         char            c;
3487         char            delimc = cstate->delim[0];
3488
3489         if (cstate->need_transcoding)
3490                 ptr = pg_server_to_any(string, strlen(string), cstate->file_encoding);
3491         else
3492                 ptr = string;
3493
3494         /*
3495          * We have to grovel through the string searching for control characters
3496          * and instances of the delimiter character.  In most cases, though, these
3497          * are infrequent.      To avoid overhead from calling CopySendData once per
3498          * character, we dump out all characters between escaped characters in a
3499          * single call.  The loop invariant is that the data from "start" to "ptr"
3500          * can be sent literally, but hasn't yet been.
3501          *
3502          * We can skip pg_encoding_mblen() overhead when encoding is safe, because
3503          * in valid backend encodings, extra bytes of a multibyte character never
3504          * look like ASCII.  This loop is sufficiently performance-critical that
3505          * it's worth making two copies of it to get the IS_HIGHBIT_SET() test out
3506          * of the normal safe-encoding path.
3507          */
3508         if (cstate->encoding_embeds_ascii)
3509         {
3510                 start = ptr;
3511                 while ((c = *ptr) != '\0')
3512                 {
3513                         if ((unsigned char) c < (unsigned char) 0x20)
3514                         {
3515                                 /*
3516                                  * \r and \n must be escaped, the others are traditional. We
3517                                  * prefer to dump these using the C-like notation, rather than
3518                                  * a backslash and the literal character, because it makes the
3519                                  * dump file a bit more proof against Microsoftish data
3520                                  * mangling.
3521                                  */
3522                                 switch (c)
3523                                 {
3524                                         case '\b':
3525                                                 c = 'b';
3526                                                 break;
3527                                         case '\f':
3528                                                 c = 'f';
3529                                                 break;
3530                                         case '\n':
3531                                                 c = 'n';
3532                                                 break;
3533                                         case '\r':
3534                                                 c = 'r';
3535                                                 break;
3536                                         case '\t':
3537                                                 c = 't';
3538                                                 break;
3539                                         case '\v':
3540                                                 c = 'v';
3541                                                 break;
3542                                         default:
3543                                                 /* If it's the delimiter, must backslash it */
3544                                                 if (c == delimc)
3545                                                         break;
3546                                                 /* All ASCII control chars are length 1 */
3547                                                 ptr++;
3548                                                 continue;               /* fall to end of loop */
3549                                 }
3550                                 /* if we get here, we need to convert the control char */
3551                                 DUMPSOFAR();
3552                                 CopySendChar(cstate, '\\');
3553                                 CopySendChar(cstate, c);
3554                                 start = ++ptr;  /* do not include char in next run */
3555                         }
3556                         else if (c == '\\' || c == delimc)
3557                         {
3558                                 DUMPSOFAR();
3559                                 CopySendChar(cstate, '\\');
3560                                 start = ptr++;  /* we include char in next run */
3561                         }
3562                         else if (IS_HIGHBIT_SET(c))
3563                                 ptr += pg_encoding_mblen(cstate->file_encoding, ptr);
3564                         else
3565                                 ptr++;
3566                 }
3567         }
3568         else
3569         {
3570                 start = ptr;
3571                 while ((c = *ptr) != '\0')
3572                 {
3573                         if ((unsigned char) c < (unsigned char) 0x20)
3574                         {
3575                                 /*
3576                                  * \r and \n must be escaped, the others are traditional. We
3577                                  * prefer to dump these using the C-like notation, rather than
3578                                  * a backslash and the literal character, because it makes the
3579                                  * dump file a bit more proof against Microsoftish data
3580                                  * mangling.
3581                                  */
3582                                 switch (c)
3583                                 {
3584                                         case '\b':
3585                                                 c = 'b';
3586                                                 break;
3587                                         case '\f':
3588                                                 c = 'f';
3589                                                 break;
3590                                         case '\n':
3591                                                 c = 'n';
3592                                                 break;
3593                                         case '\r':
3594                                                 c = 'r';
3595                                                 break;
3596                                         case '\t':
3597                                                 c = 't';
3598                                                 break;
3599                                         case '\v':
3600                                                 c = 'v';
3601                                                 break;
3602                                         default:
3603                                                 /* If it's the delimiter, must backslash it */
3604                                                 if (c == delimc)
3605                                                         break;
3606                                                 /* All ASCII control chars are length 1 */
3607                                                 ptr++;
3608                                                 continue;               /* fall to end of loop */
3609                                 }
3610                                 /* if we get here, we need to convert the control char */
3611                                 DUMPSOFAR();
3612                                 CopySendChar(cstate, '\\');
3613                                 CopySendChar(cstate, c);
3614                                 start = ++ptr;  /* do not include char in next run */
3615                         }
3616                         else if (c == '\\' || c == delimc)
3617                         {
3618                                 DUMPSOFAR();
3619                                 CopySendChar(cstate, '\\');
3620                                 start = ptr++;  /* we include char in next run */
3621                         }
3622                         else
3623                                 ptr++;
3624                 }
3625         }
3626
3627         DUMPSOFAR();
3628 }
3629
3630 /*
3631  * Send text representation of one attribute, with conversion and
3632  * CSV-style escaping
3633  */
3634 static void
3635 CopyAttributeOutCSV(CopyState cstate, char *string,
3636                                         bool use_quote, bool single_attr)
3637 {
3638         char       *ptr;
3639         char       *start;
3640         char            c;
3641         char            delimc = cstate->delim[0];
3642         char            quotec = cstate->quote[0];
3643         char            escapec = cstate->escape[0];
3644
3645         /* force quoting if it matches null_print (before conversion!) */
3646         if (!use_quote && strcmp(string, cstate->null_print) == 0)
3647                 use_quote = true;
3648
3649         if (cstate->need_transcoding)
3650                 ptr = pg_server_to_any(string, strlen(string), cstate->file_encoding);
3651         else
3652                 ptr = string;
3653
3654         /*
3655          * Make a preliminary pass to discover if it needs quoting
3656          */
3657         if (!use_quote)
3658         {
3659                 /*
3660                  * Because '\.' can be a data value, quote it if it appears alone on a
3661                  * line so it is not interpreted as the end-of-data marker.
3662                  */
3663                 if (single_attr && strcmp(ptr, "\\.") == 0)
3664                         use_quote = true;
3665                 else
3666                 {
3667                         char       *tptr = ptr;
3668
3669                         while ((c = *tptr) != '\0')
3670                         {
3671                                 if (c == delimc || c == quotec || c == '\n' || c == '\r')
3672                                 {
3673                                         use_quote = true;
3674                                         break;
3675                                 }
3676                                 if (IS_HIGHBIT_SET(c) && cstate->encoding_embeds_ascii)
3677                                         tptr += pg_encoding_mblen(cstate->file_encoding, tptr);
3678                                 else
3679                                         tptr++;
3680                         }
3681                 }
3682         }
3683
3684         if (use_quote)
3685         {
3686                 CopySendChar(cstate, quotec);
3687
3688                 /*
3689                  * We adopt the same optimization strategy as in CopyAttributeOutText
3690                  */
3691                 start = ptr;
3692                 while ((c = *ptr) != '\0')
3693                 {
3694                         if (c == quotec || c == escapec)
3695                         {
3696                                 DUMPSOFAR();
3697                                 CopySendChar(cstate, escapec);
3698                                 start = ptr;    /* we include char in next run */
3699                         }
3700                         if (IS_HIGHBIT_SET(c) && cstate->encoding_embeds_ascii)
3701                                 ptr += pg_encoding_mblen(cstate->file_encoding, ptr);
3702                         else
3703                                 ptr++;
3704                 }
3705                 DUMPSOFAR();
3706
3707                 CopySendChar(cstate, quotec);
3708         }
3709         else
3710         {
3711                 /* If it doesn't need quoting, we can just dump it as-is */
3712                 CopySendString(cstate, ptr);
3713         }
3714 }
3715
3716 /*
3717  * CopyGetAttnums - build an integer list of attnums to be copied
3718  *
3719  * The input attnamelist is either the user-specified column list,
3720  * or NIL if there was none (in which case we want all the non-dropped
3721  * columns).
3722  *
3723  * rel can be NULL ... it's only used for error reports.
3724  */
3725 static List *
3726 CopyGetAttnums(TupleDesc tupDesc, Relation rel, List *attnamelist)
3727 {
3728         List       *attnums = NIL;
3729
3730         if (attnamelist == NIL)
3731         {
3732                 /* Generate default column list */
3733                 Form_pg_attribute *attr = tupDesc->attrs;
3734                 int                     attr_count = tupDesc->natts;
3735                 int                     i;
3736
3737                 for (i = 0; i < attr_count; i++)
3738                 {
3739                         if (attr[i]->attisdropped)
3740                                 continue;
3741                         attnums = lappend_int(attnums, i + 1);
3742                 }
3743         }
3744         else
3745         {
3746                 /* Validate the user-supplied list and extract attnums */
3747                 ListCell   *l;
3748
3749                 foreach(l, attnamelist)
3750                 {
3751                         char       *name = strVal(lfirst(l));
3752                         int                     attnum;
3753                         int                     i;
3754
3755                         /* Lookup column name */
3756                         attnum = InvalidAttrNumber;
3757                         for (i = 0; i < tupDesc->natts; i++)
3758                         {
3759                                 if (tupDesc->attrs[i]->attisdropped)
3760                                         continue;
3761                                 if (namestrcmp(&(tupDesc->attrs[i]->attname), name) == 0)
3762                                 {
3763                                         attnum = tupDesc->attrs[i]->attnum;
3764                                         break;
3765                                 }
3766                         }
3767                         if (attnum == InvalidAttrNumber)
3768                         {
3769                                 if (rel != NULL)
3770                                         ereport(ERROR,
3771                                                         (errcode(ERRCODE_UNDEFINED_COLUMN),
3772                                         errmsg("column \"%s\" of relation \"%s\" does not exist",
3773                                                    name, RelationGetRelationName(rel))));
3774                                 else
3775                                         ereport(ERROR,
3776                                                         (errcode(ERRCODE_UNDEFINED_COLUMN),
3777                                                          errmsg("column \"%s\" does not exist",
3778                                                                         name)));
3779                         }
3780                         /* Check for duplicates */
3781                         if (list_member_int(attnums, attnum))
3782                                 ereport(ERROR,
3783                                                 (errcode(ERRCODE_DUPLICATE_COLUMN),
3784                                                  errmsg("column \"%s\" specified more than once",
3785                                                                 name)));
3786                         attnums = lappend_int(attnums, attnum);
3787                 }
3788         }
3789
3790         return attnums;
3791 }
3792
3793
3794 /*
3795  * copy_dest_startup --- executor startup
3796  */
3797 static void
3798 copy_dest_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
3799 {
3800         /* no-op */
3801 }
3802
3803 /*
3804  * copy_dest_receive --- receive one tuple
3805  */
3806 static void
3807 copy_dest_receive(TupleTableSlot *slot, DestReceiver *self)
3808 {
3809         DR_copy    *myState = (DR_copy *) self;
3810         CopyState       cstate = myState->cstate;
3811
3812         /* Make sure the tuple is fully deconstructed */
3813         slot_getallattrs(slot);
3814
3815         /* And send the data */
3816         CopyOneRowTo(cstate, InvalidOid, slot->tts_values, slot->tts_isnull);
3817         myState->processed++;
3818 }
3819
3820 /*
3821  * copy_dest_shutdown --- executor end
3822  */
3823 static void
3824 copy_dest_shutdown(DestReceiver *self)
3825 {
3826         /* no-op */
3827 }
3828
3829 /*
3830  * copy_dest_destroy --- release DestReceiver object
3831  */
3832 static void
3833 copy_dest_destroy(DestReceiver *self)
3834 {
3835         pfree(self);
3836 }
3837
3838 /*
3839  * CreateCopyDestReceiver -- create a suitable DestReceiver object
3840  */
3841 DestReceiver *
3842 CreateCopyDestReceiver(void)
3843 {
3844         DR_copy    *self = (DR_copy *) palloc(sizeof(DR_copy));
3845
3846         self->pub.receiveSlot = copy_dest_receive;
3847         self->pub.rStartup = copy_dest_startup;
3848         self->pub.rShutdown = copy_dest_shutdown;
3849         self->pub.rDestroy = copy_dest_destroy;
3850         self->pub.mydest = DestCopyOut;
3851
3852         self->cstate = NULL;            /* will be set later */
3853         self->processed = 0;
3854
3855         return (DestReceiver *) self;
3856 }