]> granicus.if.org Git - postgresql/blob - src/backend/commands/copy.c
The attached applied patch throws an error if the delimiter appears in
[postgresql] / src / backend / commands / copy.c
1 /*-------------------------------------------------------------------------
2  *
3  * copy.c
4  *              Implements the COPY utility command.
5  *
6  * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.219 2004/04/06 13:21:33 momjian Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include <errno.h>
18 #include <unistd.h>
19 #include <sys/stat.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22
23 #include "access/genam.h"
24 #include "access/heapam.h"
25 #include "access/printtup.h"
26 #include "catalog/catname.h"
27 #include "catalog/index.h"
28 #include "catalog/namespace.h"
29 #include "catalog/pg_index.h"
30 #include "catalog/pg_shadow.h"
31 #include "catalog/pg_type.h"
32 #include "commands/copy.h"
33 #include "commands/trigger.h"
34 #include "executor/executor.h"
35 #include "libpq/libpq.h"
36 #include "libpq/pqformat.h"
37 #include "mb/pg_wchar.h"
38 #include "miscadmin.h"
39 #include "nodes/makefuncs.h"
40 #include "parser/parse_coerce.h"
41 #include "parser/parse_relation.h"
42 #include "rewrite/rewriteHandler.h"
43 #include "storage/fd.h"
44 #include "tcop/pquery.h"
45 #include "tcop/tcopprot.h"
46 #include "utils/acl.h"
47 #include "utils/builtins.h"
48 #include "utils/relcache.h"
49 #include "utils/lsyscache.h"
50 #include "utils/syscache.h"
51
52
53 #define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
54 #define OCTVALUE(c) ((c) - '0')
55
56 /*
57  * Represents the different source/dest cases we need to worry about at
58  * the bottom level
59  */
60 typedef enum CopyDest
61 {
62         COPY_FILE,                                      /* to/from file */
63         COPY_OLD_FE,                            /* to/from frontend (2.0 protocol) */
64         COPY_NEW_FE                                     /* to/from frontend (3.0 protocol) */
65 } CopyDest;
66
67 /*
68  * State indicator showing what stopped CopyReadAttribute()
69  */
70 typedef enum CopyReadResult
71 {
72         NORMAL_ATTR,
73         END_OF_LINE
74 } CopyReadResult;
75
76 /*
77  *      Represents the end-of-line terminator type of the input
78  */
79 typedef enum EolType
80 {
81         EOL_UNKNOWN,
82         EOL_NL,
83         EOL_CR,
84         EOL_CRNL
85 } EolType;
86
87
88 static const char BinarySignature[11] = "PGCOPY\n\377\r\n\0";
89
90 /*
91  * Static communication variables ... pretty grotty, but COPY has
92  * never been reentrant...
93  */
94 static CopyDest copy_dest;
95 static FILE *copy_file;                 /* used if copy_dest == COPY_FILE */
96 static StringInfo copy_msgbuf;  /* used if copy_dest == COPY_NEW_FE */
97 static bool fe_eof;                             /* true if detected end of copy data */
98 static EolType eol_type;                /* EOL type of input */
99 static int      client_encoding;        /* remote side's character encoding */
100 static int      server_encoding;        /* local encoding */
101
102 /* these are just for error messages, see copy_in_error_callback */
103 static bool copy_binary;                /* is it a binary copy? */
104 static const char *copy_relname;        /* table name for error messages */
105 static int      copy_lineno;            /* line number for error messages */
106 static const char *copy_attname;        /* current att for error messages */
107
108
109 /*
110  * These static variables are used to avoid incurring overhead for each
111  * attribute processed.  attribute_buf is reused on each CopyReadAttribute
112  * call to hold the string being read in.  Under normal use it will soon
113  * grow to a suitable size, and then we will avoid palloc/pfree overhead
114  * for subsequent attributes.  Note that CopyReadAttribute returns a pointer
115  * to attribute_buf's data buffer!
116  */
117 static StringInfoData attribute_buf;
118
119 /*
120  * Similarly, line_buf holds the whole input line being processed (its
121  * cursor field points to the next character to be read by CopyReadAttribute).
122  * The input cycle is first to read the whole line into line_buf, convert it
123  * to server encoding, and then extract individual attribute fields into
124  * attribute_buf.  (We used to have CopyReadAttribute read the input source
125  * directly, but that caused a lot of encoding issues and unnecessary logic
126  * complexity.)
127  */
128 static StringInfoData line_buf;
129 static bool line_buf_converted;
130
131 /* non-export function prototypes */
132 static void CopyTo(Relation rel, List *attnumlist, bool binary, bool oids,
133            char *delim, char *null_print);
134 static void CopyFrom(Relation rel, List *attnumlist, bool binary, bool oids,
135                  char *delim, char *null_print);
136 static bool CopyReadLine(void);
137 static char *CopyReadAttribute(const char *delim, const char *null_print,
138                                                            CopyReadResult *result, bool *isnull);
139 static Datum CopyReadBinaryAttribute(int column_no, FmgrInfo *flinfo,
140                                                 Oid typelem, bool *isnull);
141 static void CopyAttributeOut(char *string, char *delim);
142 static List *CopyGetAttnums(Relation rel, List *attnamelist);
143 static void limit_printout_length(StringInfo buf);
144
145 /* Internal communications functions */
146 static void SendCopyBegin(bool binary, int natts);
147 static void ReceiveCopyBegin(bool binary, int natts);
148 static void SendCopyEnd(bool binary);
149 static void CopySendData(void *databuf, int datasize);
150 static void CopySendString(const char *str);
151 static void CopySendChar(char c);
152 static void CopySendEndOfRow(bool binary);
153 static void CopyGetData(void *databuf, int datasize);
154 static int      CopyGetChar(void);
155
156 #define CopyGetEof()  (fe_eof)
157 static int      CopyPeekChar(void);
158 static void CopyDonePeek(int c, bool pickup);
159 static void CopySendInt32(int32 val);
160 static int32 CopyGetInt32(void);
161 static void CopySendInt16(int16 val);
162 static int16 CopyGetInt16(void);
163
164
165 /*
166  * Send copy start/stop messages for frontend copies.  These have changed
167  * in past protocol redesigns.
168  */
169 static void
170 SendCopyBegin(bool binary, int natts)
171 {
172         if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
173         {
174                 /* new way */
175                 StringInfoData buf;
176                 int16           format = (binary ? 1 : 0);
177                 int                     i;
178
179                 pq_beginmessage(&buf, 'H');
180                 pq_sendbyte(&buf, format);              /* overall format */
181                 pq_sendint(&buf, natts, 2);
182                 for (i = 0; i < natts; i++)
183                         pq_sendint(&buf, format, 2);            /* per-column formats */
184                 pq_endmessage(&buf);
185                 copy_dest = COPY_NEW_FE;
186                 copy_msgbuf = makeStringInfo();
187         }
188         else if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 2)
189         {
190                 /* old way */
191                 if (binary)
192                         ereport(ERROR,
193                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
194                                          errmsg("COPY BINARY is not supported to stdout or from stdin")));
195                 pq_putemptymessage('H');
196                 /* grottiness needed for old COPY OUT protocol */
197                 pq_startcopyout();
198                 copy_dest = COPY_OLD_FE;
199         }
200         else
201         {
202                 /* very old way */
203                 if (binary)
204                         ereport(ERROR,
205                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
206                                          errmsg("COPY BINARY is not supported to stdout or from stdin")));
207                 pq_putemptymessage('B');
208                 /* grottiness needed for old COPY OUT protocol */
209                 pq_startcopyout();
210                 copy_dest = COPY_OLD_FE;
211         }
212 }
213
214 static void
215 ReceiveCopyBegin(bool binary, int natts)
216 {
217         if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
218         {
219                 /* new way */
220                 StringInfoData buf;
221                 int16           format = (binary ? 1 : 0);
222                 int                     i;
223
224                 pq_beginmessage(&buf, 'G');
225                 pq_sendbyte(&buf, format);              /* overall format */
226                 pq_sendint(&buf, natts, 2);
227                 for (i = 0; i < natts; i++)
228                         pq_sendint(&buf, format, 2);            /* per-column formats */
229                 pq_endmessage(&buf);
230                 copy_dest = COPY_NEW_FE;
231                 copy_msgbuf = makeStringInfo();
232         }
233         else if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 2)
234         {
235                 /* old way */
236                 if (binary)
237                         ereport(ERROR,
238                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
239                                          errmsg("COPY BINARY is not supported to stdout or from stdin")));
240                 pq_putemptymessage('G');
241                 copy_dest = COPY_OLD_FE;
242         }
243         else
244         {
245                 /* very old way */
246                 if (binary)
247                         ereport(ERROR,
248                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
249                                          errmsg("COPY BINARY is not supported to stdout or from stdin")));
250                 pq_putemptymessage('D');
251                 copy_dest = COPY_OLD_FE;
252         }
253         /* We *must* flush here to ensure FE knows it can send. */
254         pq_flush();
255 }
256
257 static void
258 SendCopyEnd(bool binary)
259 {
260         if (copy_dest == COPY_NEW_FE)
261         {
262                 if (binary)
263                 {
264                         /* Need to flush out file trailer word */
265                         CopySendEndOfRow(true);
266                 }
267                 else
268                 {
269                         /* Shouldn't have any unsent data */
270                         Assert(copy_msgbuf->len == 0);
271                 }
272                 /* Send Copy Done message */
273                 pq_putemptymessage('c');
274         }
275         else
276         {
277                 /* The FE/BE protocol uses \n as newline for all platforms */
278                 CopySendData("\\.\n", 3);
279                 pq_endcopyout(false);
280         }
281 }
282
283 /*----------
284  * CopySendData sends output data to the destination (file or frontend)
285  * CopySendString does the same for null-terminated strings
286  * CopySendChar does the same for single characters
287  * CopySendEndOfRow does the appropriate thing at end of each data row
288  *
289  * NB: no data conversion is applied by these functions
290  *----------
291  */
292 static void
293 CopySendData(void *databuf, int datasize)
294 {
295         switch (copy_dest)
296         {
297                 case COPY_FILE:
298                         fwrite(databuf, datasize, 1, copy_file);
299                         if (ferror(copy_file))
300                                 ereport(ERROR,
301                                                 (errcode_for_file_access(),
302                                                  errmsg("could not write to COPY file: %m")));
303                         break;
304                 case COPY_OLD_FE:
305                         if (pq_putbytes((char *) databuf, datasize))
306                         {
307                                 /* no hope of recovering connection sync, so FATAL */
308                                 ereport(FATAL,
309                                                 (errcode(ERRCODE_CONNECTION_FAILURE),
310                                            errmsg("connection lost during COPY to stdout")));
311                         }
312                         break;
313                 case COPY_NEW_FE:
314                         appendBinaryStringInfo(copy_msgbuf, (char *) databuf, datasize);
315                         break;
316         }
317 }
318
319 static void
320 CopySendString(const char *str)
321 {
322         CopySendData((void *) str, strlen(str));
323 }
324
325 static void
326 CopySendChar(char c)
327 {
328         CopySendData(&c, 1);
329 }
330
331 static void
332 CopySendEndOfRow(bool binary)
333 {
334         switch (copy_dest)
335         {
336                 case COPY_FILE:
337                         if (!binary)
338                         {
339                                 /* Default line termination depends on platform */
340 #ifndef WIN32
341                                 CopySendChar('\n');
342 #else
343                                 CopySendString("\r\n");
344 #endif
345                         }
346                         break;
347                 case COPY_OLD_FE:
348                         /* The FE/BE protocol uses \n as newline for all platforms */
349                         if (!binary)
350                                 CopySendChar('\n');
351                         break;
352                 case COPY_NEW_FE:
353                         /* The FE/BE protocol uses \n as newline for all platforms */
354                         if (!binary)
355                                 CopySendChar('\n');
356                         /* Dump the accumulated row as one CopyData message */
357                         (void) pq_putmessage('d', copy_msgbuf->data, copy_msgbuf->len);
358                         /* Reset copy_msgbuf to empty */
359                         copy_msgbuf->len = 0;
360                         copy_msgbuf->data[0] = '\0';
361                         break;
362         }
363 }
364
365 /*
366  * CopyGetData reads data from the source (file or frontend)
367  * CopyGetChar does the same for single characters
368  *
369  * CopyGetEof checks if EOF was detected by previous Get operation.
370  *
371  * Note: when copying from the frontend, we expect a proper EOF mark per
372  * protocol; if the frontend simply drops the connection, we raise error.
373  * It seems unwise to allow the COPY IN to complete normally in that case.
374  *
375  * NB: no data conversion is applied by these functions
376  */
377 static void
378 CopyGetData(void *databuf, int datasize)
379 {
380         switch (copy_dest)
381         {
382                 case COPY_FILE:
383                         fread(databuf, datasize, 1, copy_file);
384                         if (feof(copy_file))
385                                 fe_eof = true;
386                         break;
387                 case COPY_OLD_FE:
388                         if (pq_getbytes((char *) databuf, datasize))
389                         {
390                                 /* Only a \. terminator is legal EOF in old protocol */
391                                 ereport(ERROR,
392                                                 (errcode(ERRCODE_CONNECTION_FAILURE),
393                                                  errmsg("unexpected EOF on client connection")));
394                         }
395                         break;
396                 case COPY_NEW_FE:
397                         while (datasize > 0 && !fe_eof)
398                         {
399                                 int                     avail;
400
401                                 while (copy_msgbuf->cursor >= copy_msgbuf->len)
402                                 {
403                                         /* Try to receive another message */
404                                         int                     mtype;
405
406                                 readmessage:
407                                         mtype = pq_getbyte();
408                                         if (mtype == EOF)
409                                                 ereport(ERROR,
410                                                                 (errcode(ERRCODE_CONNECTION_FAILURE),
411                                                  errmsg("unexpected EOF on client connection")));
412                                         if (pq_getmessage(copy_msgbuf, 0))
413                                                 ereport(ERROR,
414                                                                 (errcode(ERRCODE_CONNECTION_FAILURE),
415                                                  errmsg("unexpected EOF on client connection")));
416                                         switch (mtype)
417                                         {
418                                                 case 'd':               /* CopyData */
419                                                         break;
420                                                 case 'c':               /* CopyDone */
421                                                         /* COPY IN correctly terminated by frontend */
422                                                         fe_eof = true;
423                                                         return;
424                                                 case 'f':               /* CopyFail */
425                                                         ereport(ERROR,
426                                                                         (errcode(ERRCODE_QUERY_CANCELED),
427                                                                          errmsg("COPY from stdin failed: %s",
428                                                                                  pq_getmsgstring(copy_msgbuf))));
429                                                         break;
430                                                 case 'H':               /* Flush */
431                                                 case 'S':               /* Sync */
432                                                         /*
433                                                          * Ignore Flush/Sync for the convenience of
434                                                          * client libraries (such as libpq) that may
435                                                          * send those without noticing that the command
436                                                          * they just sent was COPY.
437                                                          */
438                                                         goto readmessage;
439                                                 default:
440                                                         ereport(ERROR,
441                                                                         (errcode(ERRCODE_PROTOCOL_VIOLATION),
442                                                                          errmsg("unexpected message type 0x%02X during COPY from stdin",
443                                                                                         mtype)));
444                                                         break;
445                                         }
446                                 }
447                                 avail = copy_msgbuf->len - copy_msgbuf->cursor;
448                                 if (avail > datasize)
449                                         avail = datasize;
450                                 pq_copymsgbytes(copy_msgbuf, databuf, avail);
451                                 databuf = (void *) ((char *) databuf + avail);
452                                 datasize -= avail;
453                         }
454                         break;
455         }
456 }
457
458 static int
459 CopyGetChar(void)
460 {
461         int                     ch;
462
463         switch (copy_dest)
464         {
465                 case COPY_FILE:
466                         ch = getc(copy_file);
467                         break;
468                 case COPY_OLD_FE:
469                         ch = pq_getbyte();
470                         if (ch == EOF)
471                         {
472                                 /* Only a \. terminator is legal EOF in old protocol */
473                                 ereport(ERROR,
474                                                 (errcode(ERRCODE_CONNECTION_FAILURE),
475                                                  errmsg("unexpected EOF on client connection")));
476                         }
477                         break;
478                 case COPY_NEW_FE:
479                         {
480                                 unsigned char cc;
481
482                                 CopyGetData(&cc, 1);
483                                 if (fe_eof)
484                                         ch = EOF;
485                                 else
486                                         ch = cc;
487                                 break;
488                         }
489                 default:
490                         ch = EOF;
491                         break;
492         }
493         if (ch == EOF)
494                 fe_eof = true;
495         return ch;
496 }
497
498 /*
499  * CopyPeekChar reads a byte in "peekable" mode.
500  *
501  * after each call to CopyPeekChar, a call to CopyDonePeek _must_
502  * follow, unless EOF was returned.
503  *
504  * CopyDonePeek will either take the peeked char off the stream
505  * (if pickup is true) or leave it on the stream (if pickup is false).
506  */
507 static int
508 CopyPeekChar(void)
509 {
510         int                     ch;
511
512         switch (copy_dest)
513         {
514                 case COPY_FILE:
515                         ch = getc(copy_file);
516                         break;
517                 case COPY_OLD_FE:
518                         ch = pq_peekbyte();
519                         if (ch == EOF)
520                         {
521                                 /* Only a \. terminator is legal EOF in old protocol */
522                                 ereport(ERROR,
523                                                 (errcode(ERRCODE_CONNECTION_FAILURE),
524                                                  errmsg("unexpected EOF on client connection")));
525                         }
526                         break;
527                 case COPY_NEW_FE:
528                         {
529                                 unsigned char cc;
530
531                                 CopyGetData(&cc, 1);
532                                 if (fe_eof)
533                                         ch = EOF;
534                                 else
535                                         ch = cc;
536                                 break;
537                         }
538                 default:
539                         ch = EOF;
540                         break;
541         }
542         if (ch == EOF)
543                 fe_eof = true;
544         return ch;
545 }
546
547 static void
548 CopyDonePeek(int c, bool pickup)
549 {
550         if (fe_eof)
551                 return;                                 /* can't unget an EOF */
552         switch (copy_dest)
553         {
554                 case COPY_FILE:
555                         if (!pickup)
556                         {
557                                 /* We don't want to pick it up - so put it back in there */
558                                 ungetc(c, copy_file);
559                         }
560                         /* If we wanted to pick it up, it's already done */
561                         break;
562                 case COPY_OLD_FE:
563                         if (pickup)
564                         {
565                                 /* We want to pick it up */
566                                 (void) pq_getbyte();
567                         }
568
569                         /*
570                          * If we didn't want to pick it up, just leave it where it
571                          * sits
572                          */
573                         break;
574                 case COPY_NEW_FE:
575                         if (!pickup)
576                         {
577                                 /* We don't want to pick it up - so put it back in there */
578                                 copy_msgbuf->cursor--;
579                         }
580                         /* If we wanted to pick it up, it's already done */
581                         break;
582         }
583 }
584
585
586 /*
587  * These functions do apply some data conversion
588  */
589
590 /*
591  * CopySendInt32 sends an int32 in network byte order
592  */
593 static void
594 CopySendInt32(int32 val)
595 {
596         uint32          buf;
597
598         buf = htonl((uint32) val);
599         CopySendData(&buf, sizeof(buf));
600 }
601
602 /*
603  * CopyGetInt32 reads an int32 that appears in network byte order
604  */
605 static int32
606 CopyGetInt32(void)
607 {
608         uint32          buf;
609
610         CopyGetData(&buf, sizeof(buf));
611         return (int32) ntohl(buf);
612 }
613
614 /*
615  * CopySendInt16 sends an int16 in network byte order
616  */
617 static void
618 CopySendInt16(int16 val)
619 {
620         uint16          buf;
621
622         buf = htons((uint16) val);
623         CopySendData(&buf, sizeof(buf));
624 }
625
626 /*
627  * CopyGetInt16 reads an int16 that appears in network byte order
628  */
629 static int16
630 CopyGetInt16(void)
631 {
632         uint16          buf;
633
634         CopyGetData(&buf, sizeof(buf));
635         return (int16) ntohs(buf);
636 }
637
638
639 /*
640  *       DoCopy executes the SQL COPY statement.
641  *
642  * Either unload or reload contents of table <relation>, depending on <from>.
643  * (<from> = TRUE means we are inserting into the table.)
644  *
645  * If <pipe> is false, transfer is between the table and the file named
646  * <filename>.  Otherwise, transfer is between the table and our regular
647  * input/output stream. The latter could be either stdin/stdout or a
648  * socket, depending on whether we're running under Postmaster control.
649  *
650  * Iff <binary>, unload or reload in the binary format, as opposed to the
651  * more wasteful but more robust and portable text format.
652  *
653  * Iff <oids>, unload or reload the format that includes OID information.
654  * On input, we accept OIDs whether or not the table has an OID column,
655  * but silently drop them if it does not.  On output, we report an error
656  * if the user asks for OIDs in a table that has none (not providing an
657  * OID column might seem friendlier, but could seriously confuse programs).
658  *
659  * If in the text format, delimit columns with delimiter <delim> and print
660  * NULL values as <null_print>.
661  *
662  * When loading in the text format from an input stream (as opposed to
663  * a file), recognize a "." on a line by itself as EOF. Also recognize
664  * a stream EOF.  When unloading in the text format to an output stream,
665  * write a "." on a line by itself at the end of the data.
666  *
667  * Do not allow a Postgres user without superuser privilege to read from
668  * or write to a file.
669  *
670  * Do not allow the copy if user doesn't have proper permission to access
671  * the table.
672  */
673 void
674 DoCopy(const CopyStmt *stmt)
675 {
676         RangeVar   *relation = stmt->relation;
677         char       *filename = stmt->filename;
678         bool            is_from = stmt->is_from;
679         bool            pipe = (stmt->filename == NULL);
680         List       *option;
681         List       *attnamelist = stmt->attlist;
682         List       *attnumlist;
683         bool            binary = false;
684         bool            oids = false;
685         char       *delim = NULL;
686         char       *null_print = NULL;
687         Relation        rel;
688         AclMode         required_access = (is_from ? ACL_INSERT : ACL_SELECT);
689         AclResult       aclresult;
690
691         /* Extract options from the statement node tree */
692         foreach(option, stmt->options)
693         {
694                 DefElem    *defel = (DefElem *) lfirst(option);
695
696                 if (strcmp(defel->defname, "binary") == 0)
697                 {
698                         if (binary)
699                                 ereport(ERROR,
700                                                 (errcode(ERRCODE_SYNTAX_ERROR),
701                                                  errmsg("conflicting or redundant options")));
702                         binary = intVal(defel->arg);
703                 }
704                 else if (strcmp(defel->defname, "oids") == 0)
705                 {
706                         if (oids)
707                                 ereport(ERROR,
708                                                 (errcode(ERRCODE_SYNTAX_ERROR),
709                                                  errmsg("conflicting or redundant options")));
710                         oids = intVal(defel->arg);
711                 }
712                 else if (strcmp(defel->defname, "delimiter") == 0)
713                 {
714                         if (delim)
715                                 ereport(ERROR,
716                                                 (errcode(ERRCODE_SYNTAX_ERROR),
717                                                  errmsg("conflicting or redundant options")));
718                         delim = strVal(defel->arg);
719                 }
720                 else if (strcmp(defel->defname, "null") == 0)
721                 {
722                         if (null_print)
723                                 ereport(ERROR,
724                                                 (errcode(ERRCODE_SYNTAX_ERROR),
725                                                  errmsg("conflicting or redundant options")));
726                         null_print = strVal(defel->arg);
727                 }
728                 else
729                         elog(ERROR, "option \"%s\" not recognized",
730                                  defel->defname);
731         }
732
733         if (binary && delim)
734                 ereport(ERROR,
735                                 (errcode(ERRCODE_SYNTAX_ERROR),
736                                  errmsg("cannot specify DELIMITER in BINARY mode")));
737
738         if (binary && null_print)
739                 ereport(ERROR,
740                                 (errcode(ERRCODE_SYNTAX_ERROR),
741                                  errmsg("cannot specify NULL in BINARY mode")));
742
743         /* Set defaults */
744         if (!delim)
745                 delim = "\t";
746
747         if (!null_print)
748                 null_print = "\\N";
749
750         /*
751          * Open and lock the relation, using the appropriate lock type.
752          */
753         rel = heap_openrv(relation, (is_from ? RowExclusiveLock : AccessShareLock));
754
755         /* check read-only transaction */
756         if (XactReadOnly && !is_from && !isTempNamespace(RelationGetNamespace(rel)))
757                 ereport(ERROR,
758                                 (errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
759                                  errmsg("transaction is read-only")));
760
761         /* Check permissions. */
762         aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(),
763                                                                   required_access);
764         if (aclresult != ACLCHECK_OK)
765                 aclcheck_error(aclresult, ACL_KIND_CLASS,
766                                            RelationGetRelationName(rel));
767         if (!pipe && !superuser())
768                 ereport(ERROR,
769                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
770                                  errmsg("must be superuser to COPY to or from a file"),
771                                  errhint("Anyone can COPY to stdout or from stdin. "
772                                            "psql's \\copy command also works for anyone.")));
773
774         /*
775          * Presently, only single-character delimiter strings are supported.
776          */
777         if (strlen(delim) != 1)
778                 ereport(ERROR,
779                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
780                                  errmsg("COPY delimiter must be a single character")));
781
782         /*
783          * Don't allow the delimiter to appear in the null string.
784          */
785         if (strchr(null_print, delim[0]) != NULL)
786                 ereport(ERROR,
787                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
788                                  errmsg("COPY delimiter must not appear in the NULL specification")));
789
790         /*
791          * Don't allow COPY w/ OIDs to or from a table without them
792          */
793         if (oids && !rel->rd_rel->relhasoids)
794                 ereport(ERROR,
795                                 (errcode(ERRCODE_UNDEFINED_COLUMN),
796                                  errmsg("table \"%s\" does not have OIDs",
797                                                 RelationGetRelationName(rel))));
798
799         /*
800          * Generate or convert list of attributes to process
801          */
802         attnumlist = CopyGetAttnums(rel, attnamelist);
803
804         /*
805          * Set up variables to avoid per-attribute overhead.
806          */
807         initStringInfo(&attribute_buf);
808         initStringInfo(&line_buf);
809         line_buf_converted = false;
810
811         client_encoding = pg_get_client_encoding();
812         server_encoding = GetDatabaseEncoding();
813
814         copy_dest = COPY_FILE;          /* default */
815         copy_file = NULL;
816         copy_msgbuf = NULL;
817         fe_eof = false;
818
819         if (is_from)
820         {                                                       /* copy from file to database */
821                 if (rel->rd_rel->relkind != RELKIND_RELATION)
822                 {
823                         if (rel->rd_rel->relkind == RELKIND_VIEW)
824                                 ereport(ERROR,
825                                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
826                                                  errmsg("cannot copy to view \"%s\"",
827                                                                 RelationGetRelationName(rel))));
828                         else if (rel->rd_rel->relkind == RELKIND_SEQUENCE)
829                                 ereport(ERROR,
830                                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
831                                                  errmsg("cannot copy to sequence \"%s\"",
832                                                                 RelationGetRelationName(rel))));
833                         else
834                                 ereport(ERROR,
835                                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
836                                            errmsg("cannot copy to non-table relation \"%s\"",
837                                                           RelationGetRelationName(rel))));
838                 }
839                 if (pipe)
840                 {
841                         if (whereToSendOutput == Remote)
842                                 ReceiveCopyBegin(binary, length(attnumlist));
843                         else
844                                 copy_file = stdin;
845                 }
846                 else
847                 {
848                         struct stat st;
849
850                         copy_file = AllocateFile(filename, PG_BINARY_R);
851
852                         if (copy_file == NULL)
853                                 ereport(ERROR,
854                                                 (errcode_for_file_access(),
855                                          errmsg("could not open file \"%s\" for reading: %m",
856                                                         filename)));
857
858                         fstat(fileno(copy_file), &st);
859                         if (S_ISDIR(st.st_mode))
860                         {
861                                 FreeFile(copy_file);
862                                 ereport(ERROR,
863                                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
864                                                  errmsg("\"%s\" is a directory", filename)));
865                         }
866                 }
867                 CopyFrom(rel, attnumlist, binary, oids, delim, null_print);
868         }
869         else
870         {                                                       /* copy from database to file */
871                 if (rel->rd_rel->relkind != RELKIND_RELATION)
872                 {
873                         if (rel->rd_rel->relkind == RELKIND_VIEW)
874                                 ereport(ERROR,
875                                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
876                                                  errmsg("cannot copy from view \"%s\"",
877                                                                 RelationGetRelationName(rel))));
878                         else if (rel->rd_rel->relkind == RELKIND_SEQUENCE)
879                                 ereport(ERROR,
880                                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
881                                                  errmsg("cannot copy from sequence \"%s\"",
882                                                                 RelationGetRelationName(rel))));
883                         else
884                                 ereport(ERROR,
885                                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
886                                          errmsg("cannot copy from non-table relation \"%s\"",
887                                                         RelationGetRelationName(rel))));
888                 }
889                 if (pipe)
890                 {
891                         if (whereToSendOutput == Remote)
892                                 SendCopyBegin(binary, length(attnumlist));
893                         else
894                                 copy_file = stdout;
895                 }
896                 else
897                 {
898                         mode_t          oumask; /* Pre-existing umask value */
899                         struct stat st;
900
901                         /*
902                          * Prevent write to relative path ... too easy to shoot
903                          * oneself in the foot by overwriting a database file ...
904                          */
905                         if (!is_absolute_path(filename))
906                                 ereport(ERROR,
907                                                 (errcode(ERRCODE_INVALID_NAME),
908                                   errmsg("relative path not allowed for COPY to file")));
909
910                         oumask = umask((mode_t) 022);
911                         copy_file = AllocateFile(filename, PG_BINARY_W);
912                         umask(oumask);
913
914                         if (copy_file == NULL)
915                                 ereport(ERROR,
916                                                 (errcode_for_file_access(),
917                                          errmsg("could not open file \"%s\" for writing: %m",
918                                                         filename)));
919
920                         fstat(fileno(copy_file), &st);
921                         if (S_ISDIR(st.st_mode))
922                         {
923                                 FreeFile(copy_file);
924                                 ereport(ERROR,
925                                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
926                                                  errmsg("\"%s\" is a directory", filename)));
927                         }
928                 }
929                 CopyTo(rel, attnumlist, binary, oids, delim, null_print);
930         }
931
932         if (!pipe)
933         {
934                 /* we assume only the write case could fail here */
935                 if (FreeFile(copy_file))
936                         ereport(ERROR,
937                                         (errcode_for_file_access(),
938                                          errmsg("could not write to file \"%s\": %m",
939                                                         filename)));
940         }
941         else if (whereToSendOutput == Remote && !is_from)
942                 SendCopyEnd(binary);
943         pfree(attribute_buf.data);
944         pfree(line_buf.data);
945
946         /*
947          * Close the relation.  If reading, we can release the AccessShareLock
948          * we got; if writing, we should hold the lock until end of
949          * transaction to ensure that updates will be committed before lock is
950          * released.
951          */
952         heap_close(rel, (is_from ? NoLock : AccessShareLock));
953 }
954
955
956 /*
957  * Copy from relation TO file.
958  */
959 static void
960 CopyTo(Relation rel, List *attnumlist, bool binary, bool oids,
961            char *delim, char *null_print)
962 {
963         HeapTuple       tuple;
964         TupleDesc       tupDesc;
965         HeapScanDesc scandesc;
966         int                     num_phys_attrs;
967         int                     attr_count;
968         Form_pg_attribute *attr;
969         FmgrInfo   *out_functions;
970         Oid                *elements;
971         bool       *isvarlena;
972         char       *string;
973         Snapshot        mySnapshot;
974         List       *cur;
975         MemoryContext oldcontext;
976         MemoryContext mycontext;
977
978         tupDesc = rel->rd_att;
979         attr = tupDesc->attrs;
980         num_phys_attrs = tupDesc->natts;
981         attr_count = length(attnumlist);
982
983         /*
984          * Get info about the columns we need to process.
985          *
986          * +1's here are to avoid palloc(0) in a zero-column table.
987          */
988         out_functions = (FmgrInfo *) palloc((num_phys_attrs + 1) * sizeof(FmgrInfo));
989         elements = (Oid *) palloc((num_phys_attrs + 1) * sizeof(Oid));
990         isvarlena = (bool *) palloc((num_phys_attrs + 1) * sizeof(bool));
991         foreach(cur, attnumlist)
992         {
993                 int                     attnum = lfirsti(cur);
994                 Oid                     out_func_oid;
995
996                 if (binary)
997                         getTypeBinaryOutputInfo(attr[attnum - 1]->atttypid,
998                                                                         &out_func_oid, &elements[attnum - 1],
999                                                                         &isvarlena[attnum - 1]);
1000                 else
1001                         getTypeOutputInfo(attr[attnum - 1]->atttypid,
1002                                                           &out_func_oid, &elements[attnum - 1],
1003                                                           &isvarlena[attnum - 1]);
1004                 fmgr_info(out_func_oid, &out_functions[attnum - 1]);
1005         }
1006
1007         /*
1008          * Create a temporary memory context that we can reset once per row to
1009          * recover palloc'd memory.  This avoids any problems with leaks
1010          * inside datatype output routines, and should be faster than retail
1011          * pfree's anyway.  (We don't need a whole econtext as CopyFrom does.)
1012          */
1013         mycontext = AllocSetContextCreate(CurrentMemoryContext,
1014                                                                           "COPY TO",
1015                                                                           ALLOCSET_DEFAULT_MINSIZE,
1016                                                                           ALLOCSET_DEFAULT_INITSIZE,
1017                                                                           ALLOCSET_DEFAULT_MAXSIZE);
1018
1019         if (binary)
1020         {
1021                 /* Generate header for a binary copy */
1022                 int32           tmp;
1023
1024                 /* Signature */
1025                 CopySendData((char *) BinarySignature, 11);
1026                 /* Flags field */
1027                 tmp = 0;
1028                 if (oids)
1029                         tmp |= (1 << 16);
1030                 CopySendInt32(tmp);
1031                 /* No header extension */
1032                 tmp = 0;
1033                 CopySendInt32(tmp);
1034         }
1035         else
1036         {
1037                 /*
1038                  * For non-binary copy, we need to convert null_print to client
1039                  * encoding, because it will be sent directly with CopySendString.
1040                  */
1041                 if (server_encoding != client_encoding)
1042                         null_print = (char *)
1043                                 pg_server_to_client((unsigned char *) null_print,
1044                                                                         strlen(null_print));
1045         }
1046
1047         mySnapshot = CopyQuerySnapshot();
1048
1049         scandesc = heap_beginscan(rel, mySnapshot, 0, NULL);
1050
1051         while ((tuple = heap_getnext(scandesc, ForwardScanDirection)) != NULL)
1052         {
1053                 bool            need_delim = false;
1054
1055                 CHECK_FOR_INTERRUPTS();
1056
1057                 MemoryContextReset(mycontext);
1058                 oldcontext = MemoryContextSwitchTo(mycontext);
1059
1060                 if (binary)
1061                 {
1062                         /* Binary per-tuple header */
1063                         CopySendInt16(attr_count);
1064                         /* Send OID if wanted --- note attr_count doesn't include it */
1065                         if (oids)
1066                         {
1067                                 Oid                     oid = HeapTupleGetOid(tuple);
1068
1069                                 /* Hack --- assume Oid is same size as int32 */
1070                                 CopySendInt32(sizeof(int32));
1071                                 CopySendInt32(oid);
1072                         }
1073                 }
1074                 else
1075                 {
1076                         /* Text format has no per-tuple header, but send OID if wanted */
1077                         if (oids)
1078                         {
1079                                 string = DatumGetCString(DirectFunctionCall1(oidout,
1080                                                           ObjectIdGetDatum(HeapTupleGetOid(tuple))));
1081                                 CopySendString(string);
1082                                 need_delim = true;
1083                         }
1084                 }
1085
1086                 foreach(cur, attnumlist)
1087                 {
1088                         int                     attnum = lfirsti(cur);
1089                         Datum           value;
1090                         bool            isnull;
1091
1092                         value = heap_getattr(tuple, attnum, tupDesc, &isnull);
1093
1094                         if (!binary)
1095                         {
1096                                 if (need_delim)
1097                                         CopySendChar(delim[0]);
1098                                 need_delim = true;
1099                         }
1100
1101                         if (isnull)
1102                         {
1103                                 if (!binary)
1104                                         CopySendString(null_print); /* null indicator */
1105                                 else
1106                                         CopySendInt32(-1);      /* null marker */
1107                         }
1108                         else
1109                         {
1110                                 if (!binary)
1111                                 {
1112                                         string = DatumGetCString(FunctionCall3(&out_functions[attnum - 1],
1113                                                                                                                    value,
1114                                                                   ObjectIdGetDatum(elements[attnum - 1]),
1115                                                         Int32GetDatum(attr[attnum - 1]->atttypmod)));
1116                                         CopyAttributeOut(string, delim);
1117                                 }
1118                                 else
1119                                 {
1120                                         bytea      *outputbytes;
1121
1122                                         outputbytes = DatumGetByteaP(FunctionCall2(&out_functions[attnum - 1],
1123                                                                                                                            value,
1124                                                                 ObjectIdGetDatum(elements[attnum - 1])));
1125                                         /* We assume the result will not have been toasted */
1126                                         CopySendInt32(VARSIZE(outputbytes) - VARHDRSZ);
1127                                         CopySendData(VARDATA(outputbytes),
1128                                                                  VARSIZE(outputbytes) - VARHDRSZ);
1129                                 }
1130                         }
1131                 }
1132
1133                 CopySendEndOfRow(binary);
1134
1135                 MemoryContextSwitchTo(oldcontext);
1136         }
1137
1138         heap_endscan(scandesc);
1139
1140         if (binary)
1141         {
1142                 /* Generate trailer for a binary copy */
1143                 CopySendInt16(-1);
1144         }
1145
1146         MemoryContextDelete(mycontext);
1147
1148         pfree(out_functions);
1149         pfree(elements);
1150         pfree(isvarlena);
1151 }
1152
1153
1154 /*
1155  * error context callback for COPY FROM
1156  */
1157 static void
1158 copy_in_error_callback(void *arg)
1159 {
1160         if (copy_binary)
1161         {
1162                 /* can't usefully display the data */
1163                 if (copy_attname)
1164                         errcontext("COPY %s, line %d, column %s",
1165                                            copy_relname, copy_lineno, copy_attname);
1166                 else
1167                         errcontext("COPY %s, line %d", copy_relname, copy_lineno);
1168         }
1169         else
1170         {
1171                 if (copy_attname)
1172                 {
1173                         /* error is relevant to a particular column */
1174                         limit_printout_length(&attribute_buf);
1175                         errcontext("COPY %s, line %d, column %s: \"%s\"",
1176                                            copy_relname, copy_lineno, copy_attname,
1177                                            attribute_buf.data);
1178                 }
1179                 else
1180                 {
1181                         /* error is relevant to a particular line */
1182                         if (!line_buf_converted)
1183                         {
1184                                 /* didn't convert the encoding yet... */
1185                                 line_buf_converted = true;
1186                                 if (client_encoding != server_encoding)
1187                                 {
1188                                         char       *cvt;
1189
1190                                         cvt = (char *) pg_client_to_server((unsigned char *) line_buf.data,
1191                                                                                                            line_buf.len);
1192                                         if (cvt != line_buf.data)
1193                                         {
1194                                                 /* transfer converted data back to line_buf */
1195                                                 line_buf.len = 0;
1196                                                 line_buf.data[0] = '\0';
1197                                                 appendBinaryStringInfo(&line_buf, cvt, strlen(cvt));
1198                                         }
1199                                 }
1200                         }
1201                         limit_printout_length(&line_buf);
1202                         errcontext("COPY %s, line %d: \"%s\"",
1203                                            copy_relname, copy_lineno,
1204                                            line_buf.data);
1205                 }
1206         }
1207 }
1208
1209 /*
1210  * Make sure we don't print an unreasonable amount of COPY data in a message.
1211  *
1212  * It would seem a lot easier to just use the sprintf "precision" limit to
1213  * truncate the string.  However, some versions of glibc have a bug/misfeature
1214  * that vsnprintf will always fail (return -1) if it is asked to truncate
1215  * a string that contains invalid byte sequences for the current encoding.
1216  * So, do our own truncation.  We assume we can alter the StringInfo buffer
1217  * holding the input data.
1218  */
1219 static void
1220 limit_printout_length(StringInfo buf)
1221 {
1222 #define MAX_COPY_DATA_DISPLAY 100
1223
1224         int len;
1225
1226         /* Fast path if definitely okay */
1227         if (buf->len <= MAX_COPY_DATA_DISPLAY)
1228                 return;
1229
1230         /* Apply encoding-dependent truncation */
1231         len = pg_mbcliplen(buf->data, buf->len, MAX_COPY_DATA_DISPLAY);
1232         if (buf->len <= len)
1233                 return;                                 /* no need to truncate */
1234         buf->len = len;
1235         buf->data[len] = '\0';
1236
1237         /* Add "..." to show we truncated the input */
1238         appendStringInfoString(buf, "...");
1239 }
1240
1241 /*
1242  * Copy FROM file to relation.
1243  */
1244 static void
1245 CopyFrom(Relation rel, List *attnumlist, bool binary, bool oids,
1246                  char *delim, char *null_print)
1247 {
1248         HeapTuple       tuple;
1249         TupleDesc       tupDesc;
1250         Form_pg_attribute *attr;
1251         AttrNumber      num_phys_attrs,
1252                                 attr_count,
1253                                 num_defaults;
1254         FmgrInfo   *in_functions;
1255         FmgrInfo        oid_in_function;
1256         Oid                *elements;
1257         Oid                     oid_in_element;
1258         ExprState **constraintexprs;
1259         bool            hasConstraints = false;
1260         int                     i;
1261         List       *cur;
1262         Oid                     in_func_oid;
1263         Datum      *values;
1264         char       *nulls;
1265         bool            done = false;
1266         bool            isnull;
1267         ResultRelInfo *resultRelInfo;
1268         EState     *estate = CreateExecutorState(); /* for ExecConstraints() */
1269         TupleTable      tupleTable;
1270         TupleTableSlot *slot;
1271         bool            file_has_oids;
1272         int                *defmap;
1273         ExprState **defexprs;           /* array of default att expressions */
1274         ExprContext *econtext;          /* used for ExecEvalExpr for default atts */
1275         MemoryContext oldcontext = CurrentMemoryContext;
1276         ErrorContextCallback errcontext;
1277
1278         tupDesc = RelationGetDescr(rel);
1279         attr = tupDesc->attrs;
1280         num_phys_attrs = tupDesc->natts;
1281         attr_count = length(attnumlist);
1282         num_defaults = 0;
1283
1284         /*
1285          * We need a ResultRelInfo so we can use the regular executor's
1286          * index-entry-making machinery.  (There used to be a huge amount of
1287          * code here that basically duplicated execUtils.c ...)
1288          */
1289         resultRelInfo = makeNode(ResultRelInfo);
1290         resultRelInfo->ri_RangeTableIndex = 1;          /* dummy */
1291         resultRelInfo->ri_RelationDesc = rel;
1292         resultRelInfo->ri_TrigDesc = CopyTriggerDesc(rel->trigdesc);
1293
1294         ExecOpenIndices(resultRelInfo);
1295
1296         estate->es_result_relations = resultRelInfo;
1297         estate->es_num_result_relations = 1;
1298         estate->es_result_relation_info = resultRelInfo;
1299
1300         /* Set up a dummy tuple table too */
1301         tupleTable = ExecCreateTupleTable(1);
1302         slot = ExecAllocTableSlot(tupleTable);
1303         ExecSetSlotDescriptor(slot, tupDesc, false);
1304
1305         econtext = GetPerTupleExprContext(estate);
1306
1307         /*
1308          * Pick up the required catalog information for each attribute in the
1309          * relation, including the input function, the element type (to pass
1310          * to the input function), and info about defaults and constraints.
1311          * (Which input function we use depends on text/binary format choice.)
1312          * +1's here are to avoid palloc(0) in a zero-column table.
1313          */
1314         in_functions = (FmgrInfo *) palloc((num_phys_attrs + 1) * sizeof(FmgrInfo));
1315         elements = (Oid *) palloc((num_phys_attrs + 1) * sizeof(Oid));
1316         defmap = (int *) palloc((num_phys_attrs + 1) * sizeof(int));
1317         defexprs = (ExprState **) palloc((num_phys_attrs + 1) * sizeof(ExprState *));
1318         constraintexprs = (ExprState **) palloc0((num_phys_attrs + 1) * sizeof(ExprState *));
1319
1320         for (i = 0; i < num_phys_attrs; i++)
1321         {
1322                 /* We don't need info for dropped attributes */
1323                 if (attr[i]->attisdropped)
1324                         continue;
1325
1326                 /* Fetch the input function and typelem info */
1327                 if (binary)
1328                         getTypeBinaryInputInfo(attr[i]->atttypid,
1329                                                                    &in_func_oid, &elements[i]);
1330                 else
1331                         getTypeInputInfo(attr[i]->atttypid,
1332                                                          &in_func_oid, &elements[i]);
1333                 fmgr_info(in_func_oid, &in_functions[i]);
1334
1335                 /* Get default info if needed */
1336                 if (!intMember(i + 1, attnumlist))
1337                 {
1338                         /* attribute is NOT to be copied from input */
1339                         /* use default value if one exists */
1340                         Node       *defexpr = build_column_default(rel, i + 1);
1341
1342                         if (defexpr != NULL)
1343                         {
1344                                 defexprs[num_defaults] = ExecPrepareExpr((Expr *) defexpr,
1345                                                                                                                  estate);
1346                                 defmap[num_defaults] = i;
1347                                 num_defaults++;
1348                         }
1349                 }
1350
1351                 /* If it's a domain type, set up to check domain constraints */
1352                 if (get_typtype(attr[i]->atttypid) == 'd')
1353                 {
1354                         Param      *prm;
1355                         Node       *node;
1356
1357                         /*
1358                          * Easiest way to do this is to use parse_coerce.c to set up
1359                          * an expression that checks the constraints.  (At present,
1360                          * the expression might contain a length-coercion-function
1361                          * call and/or CoerceToDomain nodes.)  The bottom of the
1362                          * expression is a Param node so that we can fill in the
1363                          * actual datum during the data input loop.
1364                          */
1365                         prm = makeNode(Param);
1366                         prm->paramkind = PARAM_EXEC;
1367                         prm->paramid = 0;
1368                         prm->paramtype = getBaseType(attr[i]->atttypid);
1369
1370                         node = coerce_to_domain((Node *) prm,
1371                                                                         prm->paramtype,
1372                                                                         attr[i]->atttypid,
1373                                                                         COERCE_IMPLICIT_CAST);
1374
1375                         constraintexprs[i] = ExecPrepareExpr((Expr *) node,
1376                                                                                                  estate);
1377                         hasConstraints = true;
1378                 }
1379         }
1380
1381         /*
1382          * Check BEFORE STATEMENT insertion triggers. It's debateable whether
1383          * we should do this for COPY, since it's not really an "INSERT"
1384          * statement as such. However, executing these triggers maintains
1385          * consistency with the EACH ROW triggers that we already fire on
1386          * COPY.
1387          */
1388         ExecBSInsertTriggers(estate, resultRelInfo);
1389
1390         if (!binary)
1391         {
1392                 file_has_oids = oids;   /* must rely on user to tell us this... */
1393         }
1394         else
1395         {
1396                 /* Read and verify binary header */
1397                 char            readSig[11];
1398                 int32           tmp;
1399
1400                 /* Signature */
1401                 CopyGetData(readSig, 11);
1402                 if (CopyGetEof() || memcmp(readSig, BinarySignature, 11) != 0)
1403                         ereport(ERROR,
1404                                         (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1405                                          errmsg("COPY file signature not recognized")));
1406                 /* Flags field */
1407                 tmp = CopyGetInt32();
1408                 if (CopyGetEof())
1409                         ereport(ERROR,
1410                                         (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1411                                          errmsg("invalid COPY file header (missing flags)")));
1412                 file_has_oids = (tmp & (1 << 16)) != 0;
1413                 tmp &= ~(1 << 16);
1414                 if ((tmp >> 16) != 0)
1415                         ereport(ERROR,
1416                                         (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1417                          errmsg("unrecognized critical flags in COPY file header")));
1418                 /* Header extension length */
1419                 tmp = CopyGetInt32();
1420                 if (CopyGetEof() || tmp < 0)
1421                         ereport(ERROR,
1422                                         (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1423                                    errmsg("invalid COPY file header (missing length)")));
1424                 /* Skip extension header, if present */
1425                 while (tmp-- > 0)
1426                 {
1427                         CopyGetData(readSig, 1);
1428                         if (CopyGetEof())
1429                                 ereport(ERROR,
1430                                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1431                                          errmsg("invalid COPY file header (wrong length)")));
1432                 }
1433         }
1434
1435         if (file_has_oids && binary)
1436         {
1437                 getTypeBinaryInputInfo(OIDOID,
1438                                                            &in_func_oid, &oid_in_element);
1439                 fmgr_info(in_func_oid, &oid_in_function);
1440         }
1441
1442         values = (Datum *) palloc((num_phys_attrs + 1) * sizeof(Datum));
1443         nulls = (char *) palloc((num_phys_attrs + 1) * sizeof(char));
1444
1445         /* Make room for a PARAM_EXEC value for domain constraint checks */
1446         if (hasConstraints)
1447                 econtext->ecxt_param_exec_vals = (ParamExecData *)
1448                         palloc0(sizeof(ParamExecData));
1449
1450         /* Initialize static variables */
1451         fe_eof = false;
1452         eol_type = EOL_UNKNOWN;
1453         copy_binary = binary;
1454         copy_relname = RelationGetRelationName(rel);
1455         copy_lineno = 0;
1456         copy_attname = NULL;
1457
1458         /* Set up callback to identify error line number */
1459         errcontext.callback = copy_in_error_callback;
1460         errcontext.arg = NULL;
1461         errcontext.previous = error_context_stack;
1462         error_context_stack = &errcontext;
1463
1464         while (!done)
1465         {
1466                 bool            skip_tuple;
1467                 Oid                     loaded_oid = InvalidOid;
1468
1469                 CHECK_FOR_INTERRUPTS();
1470
1471                 copy_lineno++;
1472
1473                 /* Reset the per-tuple exprcontext */
1474                 ResetPerTupleExprContext(estate);
1475
1476                 /* Switch into its memory context */
1477                 MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
1478
1479                 /* Initialize all values for row to NULL */
1480                 MemSet(values, 0, num_phys_attrs * sizeof(Datum));
1481                 MemSet(nulls, 'n', num_phys_attrs * sizeof(char));
1482
1483                 if (!binary)
1484                 {
1485                         CopyReadResult result = NORMAL_ATTR;
1486                         char       *string;
1487
1488                         /* Actually read the line into memory here */
1489                         done = CopyReadLine();
1490
1491                         /*
1492                          * EOF at start of line means we're done.  If we see EOF
1493                          * after some characters, we act as though it was newline
1494                          * followed by EOF, ie, process the line and then exit loop
1495                          * on next iteration.
1496                          */
1497                         if (done && line_buf.len == 0)
1498                                 break;
1499
1500                         if (file_has_oids)
1501                         {
1502                                 string = CopyReadAttribute(delim, null_print,
1503                                                                                    &result, &isnull);
1504
1505                                 if (isnull)
1506                                         ereport(ERROR,
1507                                                         (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1508                                                          errmsg("null OID in COPY data")));
1509                                 else
1510                                 {
1511                                         copy_attname = "oid";
1512                                         loaded_oid = DatumGetObjectId(DirectFunctionCall1(oidin,
1513                                                                                            CStringGetDatum(string)));
1514                                         if (loaded_oid == InvalidOid)
1515                                                 ereport(ERROR,
1516                                                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1517                                                                  errmsg("invalid OID in COPY data")));
1518                                         copy_attname = NULL;
1519                                 }
1520                         }
1521
1522                         /*
1523                          * Loop to read the user attributes on the line.
1524                          */
1525                         foreach(cur, attnumlist)
1526                         {
1527                                 int                     attnum = lfirsti(cur);
1528                                 int                     m = attnum - 1;
1529
1530                                 /*
1531                                  * If prior attr on this line was ended by newline,
1532                                  * complain.
1533                                  */
1534                                 if (result != NORMAL_ATTR)
1535                                         ereport(ERROR,
1536                                                         (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1537                                                          errmsg("missing data for column \"%s\"",
1538                                                                         NameStr(attr[m]->attname))));
1539
1540                                 string = CopyReadAttribute(delim, null_print,
1541                                                                                    &result, &isnull);
1542
1543                                 if (isnull)
1544                                 {
1545                                         /* we read an SQL NULL, no need to do anything */
1546                                 }
1547                                 else
1548                                 {
1549                                         copy_attname = NameStr(attr[m]->attname);
1550                                         values[m] = FunctionCall3(&in_functions[m],
1551                                                                                           CStringGetDatum(string),
1552                                                                                    ObjectIdGetDatum(elements[m]),
1553                                                                           Int32GetDatum(attr[m]->atttypmod));
1554                                         nulls[m] = ' ';
1555                                         copy_attname = NULL;
1556                                 }
1557                         }
1558
1559                         /*
1560                          * Complain if there are more fields on the input line.
1561                          *
1562                          * Special case: if we're reading a zero-column table, we won't
1563                          * yet have called CopyReadAttribute() at all; so no error if
1564                          * line is empty.
1565                          */
1566                         if (result == NORMAL_ATTR && line_buf.len != 0)
1567                                 ereport(ERROR,
1568                                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1569                                                  errmsg("extra data after last expected column")));
1570                 }
1571                 else
1572                 {
1573                         /* binary */
1574                         int16           fld_count;
1575
1576                         fld_count = CopyGetInt16();
1577                         if (CopyGetEof() || fld_count == -1)
1578                         {
1579                                 done = true;
1580                                 break;
1581                         }
1582
1583                         if (fld_count != attr_count)
1584                                 ereport(ERROR,
1585                                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1586                                                  errmsg("row field count is %d, expected %d",
1587                                                                 (int) fld_count, attr_count)));
1588
1589                         if (file_has_oids)
1590                         {
1591                                 copy_attname = "oid";
1592                                 loaded_oid =
1593                                         DatumGetObjectId(CopyReadBinaryAttribute(0,
1594                                                                                                                 &oid_in_function,
1595                                                                                                                   oid_in_element,
1596                                                                                                                          &isnull));
1597                                 if (isnull || loaded_oid == InvalidOid)
1598                                         ereport(ERROR,
1599                                                         (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1600                                                          errmsg("invalid OID in COPY data")));
1601                                 copy_attname = NULL;
1602                         }
1603
1604                         i = 0;
1605                         foreach(cur, attnumlist)
1606                         {
1607                                 int                     attnum = lfirsti(cur);
1608                                 int                     m = attnum - 1;
1609
1610                                 copy_attname = NameStr(attr[m]->attname);
1611                                 i++;
1612                                 values[m] = CopyReadBinaryAttribute(i,
1613                                                                                                         &in_functions[m],
1614                                                                                                         elements[m],
1615                                                                                                         &isnull);
1616                                 nulls[m] = isnull ? 'n' : ' ';
1617                                 copy_attname = NULL;
1618                         }
1619                 }
1620
1621                 /*
1622                  * Now compute and insert any defaults available for the columns
1623                  * not provided by the input data.      Anything not processed here or
1624                  * above will remain NULL.
1625                  */
1626                 for (i = 0; i < num_defaults; i++)
1627                 {
1628                         values[defmap[i]] = ExecEvalExpr(defexprs[i], econtext,
1629                                                                                          &isnull, NULL);
1630                         if (!isnull)
1631                                 nulls[defmap[i]] = ' ';
1632                 }
1633
1634                 /*
1635                  * Next apply any domain constraints
1636                  */
1637                 if (hasConstraints)
1638                 {
1639                         ParamExecData *prmdata = &econtext->ecxt_param_exec_vals[0];
1640
1641                         for (i = 0; i < num_phys_attrs; i++)
1642                         {
1643                                 ExprState  *exprstate = constraintexprs[i];
1644
1645                                 if (exprstate == NULL)
1646                                         continue;       /* no constraint for this attr */
1647
1648                                 /* Insert current row's value into the Param value */
1649                                 prmdata->value = values[i];
1650                                 prmdata->isnull = (nulls[i] == 'n');
1651
1652                                 /*
1653                                  * Execute the constraint expression.  Allow the
1654                                  * expression to replace the value (consider e.g. a
1655                                  * timestamp precision restriction).
1656                                  */
1657                                 values[i] = ExecEvalExpr(exprstate, econtext,
1658                                                                                  &isnull, NULL);
1659                                 nulls[i] = isnull ? 'n' : ' ';
1660                         }
1661                 }
1662
1663                 /*
1664                  * And now we can form the input tuple.
1665                  */
1666                 tuple = heap_formtuple(tupDesc, values, nulls);
1667
1668                 if (oids && file_has_oids)
1669                         HeapTupleSetOid(tuple, loaded_oid);
1670
1671                 /*
1672                  * Triggers and stuff need to be invoked in query context.
1673                  */
1674                 MemoryContextSwitchTo(oldcontext);
1675
1676                 skip_tuple = false;
1677
1678                 /* BEFORE ROW INSERT Triggers */
1679                 if (resultRelInfo->ri_TrigDesc &&
1680                         resultRelInfo->ri_TrigDesc->n_before_row[TRIGGER_EVENT_INSERT] > 0)
1681                 {
1682                         HeapTuple       newtuple;
1683
1684                         newtuple = ExecBRInsertTriggers(estate, resultRelInfo, tuple);
1685
1686                         if (newtuple == NULL)           /* "do nothing" */
1687                                 skip_tuple = true;
1688                         else if (newtuple != tuple) /* modified by Trigger(s) */
1689                         {
1690                                 heap_freetuple(tuple);
1691                                 tuple = newtuple;
1692                         }
1693                 }
1694
1695                 if (!skip_tuple)
1696                 {
1697                         /* Place tuple in tuple slot */
1698                         ExecStoreTuple(tuple, slot, InvalidBuffer, false);
1699
1700                         /*
1701                          * Check the constraints of the tuple
1702                          */
1703                         if (rel->rd_att->constr)
1704                                 ExecConstraints(resultRelInfo, slot, estate);
1705
1706                         /*
1707                          * OK, store the tuple and create index entries for it
1708                          */
1709                         simple_heap_insert(rel, tuple);
1710
1711                         if (resultRelInfo->ri_NumIndices > 0)
1712                                 ExecInsertIndexTuples(slot, &(tuple->t_self), estate, false);
1713
1714                         /* AFTER ROW INSERT Triggers */
1715                         ExecARInsertTriggers(estate, resultRelInfo, tuple);
1716                 }
1717         }
1718
1719         /*
1720          * Done, clean up
1721          */
1722         error_context_stack = errcontext.previous;
1723
1724         MemoryContextSwitchTo(oldcontext);
1725
1726         /*
1727          * Execute AFTER STATEMENT insertion triggers
1728          */
1729         ExecASInsertTriggers(estate, resultRelInfo);
1730
1731         pfree(values);
1732         pfree(nulls);
1733
1734         if (!binary)
1735         {
1736                 pfree(in_functions);
1737                 pfree(elements);
1738         }
1739
1740         ExecDropTupleTable(tupleTable, true);
1741
1742         ExecCloseIndices(resultRelInfo);
1743
1744         FreeExecutorState(estate);
1745 }
1746
1747
1748 /*
1749  * Read the next input line and stash it in line_buf, with conversion to
1750  * server encoding.
1751  *
1752  * Result is true if read was terminated by EOF, false if terminated
1753  * by newline.
1754  */
1755 static bool
1756 CopyReadLine(void)
1757 {
1758         bool            result;
1759         bool            change_encoding = (client_encoding != server_encoding);
1760         int                     c;
1761         int                     mblen;
1762         int                     j;
1763         unsigned char s[2];
1764         char       *cvt;
1765
1766         s[1] = 0;
1767
1768         /* reset line_buf to empty */
1769         line_buf.len = 0;
1770         line_buf.data[0] = '\0';
1771         line_buf.cursor = 0;
1772
1773         /* mark that encoding conversion hasn't occurred yet */
1774         line_buf_converted = false;
1775
1776         /* set default status */
1777         result = false;
1778
1779         /*
1780          * In this loop we only care for detecting newlines (\r and/or \n)
1781          * and the end-of-copy marker (\.).  For backwards compatibility
1782          * we allow backslashes to escape newline characters.  Backslashes
1783          * other than the end marker get put into the line_buf, since
1784          * CopyReadAttribute does its own escape processing.  These four
1785          * characters, and only these four, are assumed the same in frontend
1786          * and backend encodings.  We do not assume that second and later bytes
1787          * of a frontend multibyte character couldn't look like ASCII characters.
1788          */
1789         for (;;)
1790         {
1791                 c = CopyGetChar();
1792                 if (c == EOF)
1793                 {
1794                         result = true;
1795                         break;
1796                 }
1797                 if (c == '\r')
1798                 {
1799                         if (eol_type == EOL_NL)
1800                                 ereport(ERROR,
1801                                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1802                                                  errmsg("literal carriage return found in data"),
1803                                   errhint("Use \"\\r\" to represent carriage return.")));
1804                         /* Check for \r\n on first line, _and_ handle \r\n. */
1805                         if (eol_type == EOL_UNKNOWN || eol_type == EOL_CRNL)
1806                         {
1807                                 int                     c2 = CopyPeekChar();
1808
1809                                 if (c2 == '\n')
1810                                 {
1811                                         CopyDonePeek(c2, true);         /* eat newline */
1812                                         eol_type = EOL_CRNL;
1813                                 }
1814                                 else
1815                                 {
1816                                         /* found \r, but no \n */
1817                                         if (eol_type == EOL_CRNL)
1818                                                 ereport(ERROR,
1819                                                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1820                                                  errmsg("literal carriage return found in data"),
1821                                                                  errhint("Use \"\\r\" to represent carriage return.")));
1822
1823                                         /*
1824                                          * if we got here, it is the first line and we didn't
1825                                          * get \n, so put it back
1826                                          */
1827                                         CopyDonePeek(c2, false);
1828                                         eol_type = EOL_CR;
1829                                 }
1830                         }
1831                         break;
1832                 }
1833                 if (c == '\n')
1834                 {
1835                         if (eol_type == EOL_CR || eol_type == EOL_CRNL)
1836                                 ereport(ERROR,
1837                                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1838                                                  errmsg("literal newline found in data"),
1839                                                  errhint("Use \"\\n\" to represent newline.")));
1840                         eol_type = EOL_NL;
1841                         break;
1842                 }
1843                 if (c == '\\')
1844                 {
1845                         c = CopyGetChar();
1846                         if (c == EOF)
1847                         {
1848                                 result = true;
1849                                 break;
1850                         }
1851                         if (c == '.')
1852                         {
1853                                 if (eol_type == EOL_CRNL)
1854                                 {
1855                                         c = CopyGetChar();
1856                                         if (c == '\n')
1857                                                 ereport(ERROR,
1858                                                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1859                                                                  errmsg("end-of-copy marker does not match previous newline style")));
1860                                         if (c != '\r')
1861                                                 ereport(ERROR,
1862                                                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1863                                                                  errmsg("end-of-copy marker corrupt")));
1864                                 }
1865                                 c = CopyGetChar();
1866                                 if (c != '\r' && c != '\n')
1867                                         ereport(ERROR,
1868                                                         (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1869                                                          errmsg("end-of-copy marker corrupt")));
1870                                 if ((eol_type == EOL_NL && c != '\n') ||
1871                                         (eol_type == EOL_CRNL && c != '\n') ||
1872                                         (eol_type == EOL_CR && c != '\r'))
1873                                         ereport(ERROR,
1874                                                         (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1875                                                          errmsg("end-of-copy marker does not match previous newline style")));
1876
1877                                 /*
1878                                  * In protocol version 3, we should ignore anything
1879                                  * after \. up to the protocol end of copy data.  (XXX
1880                                  * maybe better not to treat \. as special?)
1881                                  */
1882                                 if (copy_dest == COPY_NEW_FE)
1883                                 {
1884                                         while (c != EOF)
1885                                                 c = CopyGetChar();
1886                                 }
1887                                 result = true;  /* report EOF */
1888                                 break;
1889                         }
1890                         /* not EOF mark, so emit \ and following char literally */
1891                         appendStringInfoCharMacro(&line_buf, '\\');
1892                 }
1893
1894                 appendStringInfoCharMacro(&line_buf, c);
1895
1896                 /*
1897                  * When client encoding != server, must be careful to read the
1898                  * extra bytes of a multibyte character exactly, since the encoding
1899                  * might not ensure they don't look like ASCII.  When the encodings
1900                  * are the same, we need not do this, since no server encoding we
1901                  * use has ASCII-like following bytes.
1902                  */
1903                 if (change_encoding)
1904                 {
1905                         s[0] = c;
1906                         mblen = pg_encoding_mblen(client_encoding, s);
1907                         for (j = 1; j < mblen; j++)
1908                         {
1909                                 c = CopyGetChar();
1910                                 if (c == EOF)
1911                                 {
1912                                         result = true;
1913                                         break;
1914                                 }
1915                                 appendStringInfoCharMacro(&line_buf, c);
1916                         }
1917                         if (result)
1918                                 break;                  /* out of outer loop */
1919                 }
1920         } /* end of outer loop */
1921
1922         /*
1923          * Done reading the line.  Convert it to server encoding.
1924          *
1925          * Note: set line_buf_converted to true *before* attempting conversion;
1926          * this prevents infinite recursion during error reporting should
1927          * pg_client_to_server() issue an error, due to copy_in_error_callback
1928          * again attempting the same conversion.  We'll end up issuing the message
1929          * without conversion, which is bad but better than nothing ...
1930          */
1931         line_buf_converted = true;
1932
1933         if (change_encoding)
1934         {
1935                 cvt = (char *) pg_client_to_server((unsigned char *) line_buf.data,
1936                                                                                    line_buf.len);
1937                 if (cvt != line_buf.data)
1938                 {
1939                         /* transfer converted data back to line_buf */
1940                         line_buf.len = 0;
1941                         line_buf.data[0] = '\0';
1942                         appendBinaryStringInfo(&line_buf, cvt, strlen(cvt));
1943                 }
1944         }
1945
1946         return result;
1947 }
1948
1949 /*----------
1950  * Read the value of a single attribute, performing de-escaping as needed.
1951  *
1952  * delim is the column delimiter string (must be just one byte for now).
1953  * null_print is the null marker string.  Note that this is compared to
1954  * the pre-de-escaped input string.
1955  *
1956  * *result is set to indicate what terminated the read:
1957  *              NORMAL_ATTR:    column delimiter
1958  *              END_OF_LINE:    end of line
1959  * In either case, the string read up to the terminator is returned.
1960  *
1961  * *isnull is set true or false depending on whether the input matched
1962  * the null marker.  Note that the caller cannot check this since the
1963  * returned string will be the post-de-escaping equivalent, which may
1964  * look the same as some valid data string.
1965  *----------
1966  */
1967 static char *
1968 CopyReadAttribute(const char *delim, const char *null_print,
1969                                   CopyReadResult *result, bool *isnull)
1970 {
1971         char            c;
1972         char            delimc = delim[0];
1973         int                     start_cursor = line_buf.cursor;
1974         int                     end_cursor;
1975         int                     input_len;
1976
1977         /* reset attribute_buf to empty */
1978         attribute_buf.len = 0;
1979         attribute_buf.data[0] = '\0';
1980
1981         /* set default status */
1982         *result = END_OF_LINE;
1983
1984         for (;;)
1985         {
1986                 end_cursor = line_buf.cursor;
1987                 if (line_buf.cursor >= line_buf.len)
1988                         break;
1989                 c = line_buf.data[line_buf.cursor++];
1990                 if (c == delimc)
1991                 {
1992                         *result = NORMAL_ATTR;
1993                         break;
1994                 }
1995                 if (c == '\\')
1996                 {
1997                         if (line_buf.cursor >= line_buf.len)
1998                                 break;
1999                         c = line_buf.data[line_buf.cursor++];
2000                         switch (c)
2001                         {
2002                                 case '0':
2003                                 case '1':
2004                                 case '2':
2005                                 case '3':
2006                                 case '4':
2007                                 case '5':
2008                                 case '6':
2009                                 case '7':
2010                                         {
2011                                                 int                     val;
2012
2013                                                 val = OCTVALUE(c);
2014                                                 if (line_buf.cursor < line_buf.len)
2015                                                 {
2016                                                         c = line_buf.data[line_buf.cursor];
2017                                                         if (ISOCTAL(c))
2018                                                         {
2019                                                                 line_buf.cursor++;
2020                                                                 val = (val << 3) + OCTVALUE(c);
2021                                                                 if (line_buf.cursor < line_buf.len)
2022                                                                 {
2023                                                                         c = line_buf.data[line_buf.cursor];
2024                                                                         if (ISOCTAL(c))
2025                                                                         {
2026                                                                                 line_buf.cursor++;
2027                                                                                 val = (val << 3) + OCTVALUE(c);
2028                                                                         }
2029                                                                 }
2030                                                         }
2031                                                 }
2032                                                 c = val & 0377;
2033                                         }
2034                                         break;
2035                                 case 'b':
2036                                         c = '\b';
2037                                         break;
2038                                 case 'f':
2039                                         c = '\f';
2040                                         break;
2041                                 case 'n':
2042                                         c = '\n';
2043                                         break;
2044                                 case 'r':
2045                                         c = '\r';
2046                                         break;
2047                                 case 't':
2048                                         c = '\t';
2049                                         break;
2050                                 case 'v':
2051                                         c = '\v';
2052                                         break;
2053                                 /*
2054                                  * in all other cases, take the char after '\' literally
2055                                  */
2056                         }
2057                 }
2058                 appendStringInfoCharMacro(&attribute_buf, c);
2059         }
2060
2061         /* check whether raw input matched null marker */
2062         input_len = end_cursor - start_cursor;
2063         if (input_len == strlen(null_print) &&
2064                 strncmp(&line_buf.data[start_cursor], null_print, input_len) == 0)
2065                 *isnull = true;
2066         else
2067                 *isnull = false;
2068
2069         return attribute_buf.data;
2070 }
2071
2072 /*
2073  * Read a binary attribute
2074  */
2075 static Datum
2076 CopyReadBinaryAttribute(int column_no, FmgrInfo *flinfo, Oid typelem,
2077                                                 bool *isnull)
2078 {
2079         int32           fld_size;
2080         Datum           result;
2081
2082         fld_size = CopyGetInt32();
2083         if (CopyGetEof())
2084                 ereport(ERROR,
2085                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2086                                  errmsg("unexpected EOF in COPY data")));
2087         if (fld_size == -1)
2088         {
2089                 *isnull = true;
2090                 return (Datum) 0;
2091         }
2092         if (fld_size < 0)
2093                 ereport(ERROR,
2094                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2095                                  errmsg("invalid field size")));
2096
2097         /* reset attribute_buf to empty, and load raw data in it */
2098         attribute_buf.len = 0;
2099         attribute_buf.data[0] = '\0';
2100         attribute_buf.cursor = 0;
2101
2102         enlargeStringInfo(&attribute_buf, fld_size);
2103
2104         CopyGetData(attribute_buf.data, fld_size);
2105         if (CopyGetEof())
2106                 ereport(ERROR,
2107                                 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2108                                  errmsg("unexpected EOF in COPY data")));
2109
2110         attribute_buf.len = fld_size;
2111         attribute_buf.data[fld_size] = '\0';
2112
2113         /* Call the column type's binary input converter */
2114         result = FunctionCall2(flinfo,
2115                                                    PointerGetDatum(&attribute_buf),
2116                                                    ObjectIdGetDatum(typelem));
2117
2118         /* Trouble if it didn't eat the whole buffer */
2119         if (attribute_buf.cursor != attribute_buf.len)
2120                 ereport(ERROR,
2121                                 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
2122                                  errmsg("incorrect binary data format")));
2123
2124         *isnull = false;
2125         return result;
2126 }
2127
2128 /*
2129  * Send text representation of one attribute, with conversion and escaping
2130  */
2131 static void
2132 CopyAttributeOut(char *server_string, char *delim)
2133 {
2134         char       *string;
2135         char            c;
2136         char            delimc = delim[0];
2137         bool            same_encoding;
2138         int                     mblen;
2139         int                     i;
2140
2141         same_encoding = (server_encoding == client_encoding);
2142         if (!same_encoding)
2143                 string = (char *) pg_server_to_client((unsigned char *) server_string,
2144                                                                                           strlen(server_string));
2145         else
2146                 string = server_string;
2147
2148         for (; (c = *string) != '\0'; string += mblen)
2149         {
2150                 mblen = 1;
2151
2152                 switch (c)
2153                 {
2154                         case '\b':
2155                                 CopySendString("\\b");
2156                                 break;
2157                         case '\f':
2158                                 CopySendString("\\f");
2159                                 break;
2160                         case '\n':
2161                                 CopySendString("\\n");
2162                                 break;
2163                         case '\r':
2164                                 CopySendString("\\r");
2165                                 break;
2166                         case '\t':
2167                                 CopySendString("\\t");
2168                                 break;
2169                         case '\v':
2170                                 CopySendString("\\v");
2171                                 break;
2172                         case '\\':
2173                                 CopySendString("\\\\");
2174                                 break;
2175                         default:
2176                                 if (c == delimc)
2177                                         CopySendChar('\\');
2178                                 CopySendChar(c);
2179
2180                                 /*
2181                                  * We can skip pg_encoding_mblen() overhead when encoding
2182                                  * is same, because in valid backend encodings, extra
2183                                  * bytes of a multibyte character never look like ASCII.
2184                                  */
2185                                 if (!same_encoding)
2186                                 {
2187                                         /* send additional bytes of the char, if any */
2188                                         mblen = pg_encoding_mblen(client_encoding, string);
2189                                         for (i = 1; i < mblen; i++)
2190                                                 CopySendChar(string[i]);
2191                                 }
2192                                 break;
2193                 }
2194         }
2195 }
2196
2197 /*
2198  * CopyGetAttnums - build an integer list of attnums to be copied
2199  *
2200  * The input attnamelist is either the user-specified column list,
2201  * or NIL if there was none (in which case we want all the non-dropped
2202  * columns).
2203  */
2204 static List *
2205 CopyGetAttnums(Relation rel, List *attnamelist)
2206 {
2207         List       *attnums = NIL;
2208
2209         if (attnamelist == NIL)
2210         {
2211                 /* Generate default column list */
2212                 TupleDesc       tupDesc = RelationGetDescr(rel);
2213                 Form_pg_attribute *attr = tupDesc->attrs;
2214                 int                     attr_count = tupDesc->natts;
2215                 int                     i;
2216
2217                 for (i = 0; i < attr_count; i++)
2218                 {
2219                         if (attr[i]->attisdropped)
2220                                 continue;
2221                         attnums = lappendi(attnums, i + 1);
2222                 }
2223         }
2224         else
2225         {
2226                 /* Validate the user-supplied list and extract attnums */
2227                 List       *l;
2228
2229                 foreach(l, attnamelist)
2230                 {
2231                         char       *name = strVal(lfirst(l));
2232                         int                     attnum;
2233
2234                         /* Lookup column name, ereport on failure */
2235                         /* Note we disallow system columns here */
2236                         attnum = attnameAttNum(rel, name, false);
2237                         /* Check for duplicates */
2238                         if (intMember(attnum, attnums))
2239                                 ereport(ERROR,
2240                                                 (errcode(ERRCODE_DUPLICATE_COLUMN),
2241                                           errmsg("column \"%s\" specified more than once",
2242                                                          name)));
2243                         attnums = lappendi(attnums, attnum);
2244                 }
2245         }
2246
2247         return attnums;
2248 }