OSDN Git Service

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