1 /*-------------------------------------------------------------------------
4 * Implements the COPY utility command.
6 * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.229 2004/08/29 04:12:30 momjian Exp $
13 *-------------------------------------------------------------------------
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
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"
52 #define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
53 #define OCTVALUE(c) ((c) - '0')
56 * Represents the different source/dest cases we need to worry about at
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) */
67 * State indicator showing what stopped CopyReadAttribute()
69 typedef enum CopyReadResult
77 * Represents the end-of-line terminator type of the input
88 static const char BinarySignature[11] = "PGCOPY\n\377\r\n\0";
91 * Static communication variables ... pretty grotty, but COPY has
92 * never been reentrant...
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 */
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 */
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!
117 static StringInfoData attribute_buf;
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
128 static StringInfoData line_buf;
129 static bool line_buf_converted;
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);
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);
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);
176 * Send copy start/stop messages for frontend copies. These have changed
177 * in past protocol redesigns.
180 SendCopyBegin(bool binary, int natts)
182 if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
186 int16 format = (binary ? 1 : 0);
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 */
195 copy_dest = COPY_NEW_FE;
196 copy_msgbuf = makeStringInfo();
198 else if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 2)
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 */
208 copy_dest = COPY_OLD_FE;
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 */
220 copy_dest = COPY_OLD_FE;
225 ReceiveCopyBegin(bool binary, int natts)
227 if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
231 int16 format = (binary ? 1 : 0);
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 */
240 copy_dest = COPY_NEW_FE;
241 copy_msgbuf = makeStringInfo();
243 else if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 2)
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;
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;
263 /* We *must* flush here to ensure FE knows it can send. */
268 SendCopyEnd(bool binary)
270 if (copy_dest == COPY_NEW_FE)
274 /* Need to flush out file trailer word */
275 CopySendEndOfRow(true);
279 /* Shouldn't have any unsent data */
280 Assert(copy_msgbuf->len == 0);
282 /* Send Copy Done message */
283 pq_putemptymessage('c');
287 /* The FE/BE protocol uses \n as newline for all platforms */
288 CopySendData("\\.\n", 3);
289 pq_endcopyout(false);
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
299 * NB: no data conversion is applied by these functions
303 CopySendData(void *databuf, int datasize)
308 fwrite(databuf, datasize, 1, copy_file);
309 if (ferror(copy_file))
311 (errcode_for_file_access(),
312 errmsg("could not write to COPY file: %m")));
315 if (pq_putbytes((char *) databuf, datasize))
317 /* no hope of recovering connection sync, so FATAL */
319 (errcode(ERRCODE_CONNECTION_FAILURE),
320 errmsg("connection lost during COPY to stdout")));
324 appendBinaryStringInfo(copy_msgbuf, (char *) databuf, datasize);
330 CopySendString(const char *str)
332 CopySendData((void *) str, strlen(str));
342 CopySendEndOfRow(bool binary)
349 /* Default line termination depends on platform */
353 CopySendString("\r\n");
358 /* The FE/BE protocol uses \n as newline for all platforms */
363 /* The FE/BE protocol uses \n as newline for all platforms */
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';
376 * CopyGetData reads data from the source (file or frontend)
377 * CopyGetChar does the same for single characters
379 * CopyGetEof checks if EOF was detected by previous Get operation.
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.
385 * NB: no data conversion is applied by these functions
388 CopyGetData(void *databuf, int datasize)
393 fread(databuf, datasize, 1, copy_file);
398 if (pq_getbytes((char *) databuf, datasize))
400 /* Only a \. terminator is legal EOF in old protocol */
402 (errcode(ERRCODE_CONNECTION_FAILURE),
403 errmsg("unexpected EOF on client connection")));
407 while (datasize > 0 && !fe_eof)
411 while (copy_msgbuf->cursor >= copy_msgbuf->len)
413 /* Try to receive another message */
417 mtype = pq_getbyte();
420 (errcode(ERRCODE_CONNECTION_FAILURE),
421 errmsg("unexpected EOF on client connection")));
422 if (pq_getmessage(copy_msgbuf, 0))
424 (errcode(ERRCODE_CONNECTION_FAILURE),
425 errmsg("unexpected EOF on client connection")));
428 case 'd': /* CopyData */
430 case 'c': /* CopyDone */
431 /* COPY IN correctly terminated by frontend */
434 case 'f': /* CopyFail */
436 (errcode(ERRCODE_QUERY_CANCELED),
437 errmsg("COPY from stdin failed: %s",
438 pq_getmsgstring(copy_msgbuf))));
440 case 'H': /* Flush */
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.
451 (errcode(ERRCODE_PROTOCOL_VIOLATION),
452 errmsg("unexpected message type 0x%02X during COPY from stdin",
457 avail = copy_msgbuf->len - copy_msgbuf->cursor;
458 if (avail > datasize)
460 pq_copymsgbytes(copy_msgbuf, databuf, avail);
461 databuf = (void *) ((char *) databuf + avail);
476 ch = getc(copy_file);
482 /* Only a \. terminator is legal EOF in old protocol */
484 (errcode(ERRCODE_CONNECTION_FAILURE),
485 errmsg("unexpected EOF on client connection")));
509 * CopyPeekChar reads a byte in "peekable" mode.
511 * after each call to CopyPeekChar, a call to CopyDonePeek _must_
512 * follow, unless EOF was returned.
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).
525 ch = getc(copy_file);
531 /* Only a \. terminator is legal EOF in old protocol */
533 (errcode(ERRCODE_CONNECTION_FAILURE),
534 errmsg("unexpected EOF on client connection")));
558 CopyDonePeek(int c, bool pickup)
561 return; /* can't unget an EOF */
567 /* We don't want to pick it up - so put it back in there */
568 ungetc(c, copy_file);
570 /* If we wanted to pick it up, it's already done */
575 /* We want to pick it up */
580 * If we didn't want to pick it up, just leave it where it
587 /* We don't want to pick it up - so put it back in there */
588 copy_msgbuf->cursor--;
590 /* If we wanted to pick it up, it's already done */
597 * These functions do apply some data conversion
601 * CopySendInt32 sends an int32 in network byte order
604 CopySendInt32(int32 val)
608 buf = htonl((uint32) val);
609 CopySendData(&buf, sizeof(buf));
613 * CopyGetInt32 reads an int32 that appears in network byte order
620 CopyGetData(&buf, sizeof(buf));
621 return (int32) ntohl(buf);
625 * CopySendInt16 sends an int16 in network byte order
628 CopySendInt16(int16 val)
632 buf = htons((uint16) val);
633 CopySendData(&buf, sizeof(buf));
637 * CopyGetInt16 reads an int16 that appears in network byte order
644 CopyGetData(&buf, sizeof(buf));
645 return (int16) ntohs(buf);
650 * DoCopy executes the SQL COPY statement.
652 * Either unload or reload contents of table <relation>, depending on <from>.
653 * (<from> = TRUE means we are inserting into the table.)
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.
660 * Iff <binary>, unload or reload in the binary format, as opposed to the
661 * more wasteful but more robust and portable text format.
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).
669 * If in the text format, delimit columns with delimiter <delim> and print
670 * NULL values as <null_print>.
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.
677 * Do not allow a Postgres user without superuser privilege to read from
678 * or write to a file.
680 * Do not allow the copy if user doesn't have proper permission to access
684 DoCopy(const CopyStmt *stmt)
686 RangeVar *relation = stmt->relation;
687 char *filename = stmt->filename;
688 bool is_from = stmt->is_from;
689 bool pipe = (stmt->filename == NULL);
691 List *attnamelist = stmt->attlist;
693 bool fe_copy = false;
696 bool csv_mode = false;
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;
706 AclMode required_access = (is_from ? ACL_INSERT : ACL_SELECT);
709 /* Extract options from the statement node tree */
710 foreach(option, stmt->options)
712 DefElem *defel = (DefElem *) lfirst(option);
714 if (strcmp(defel->defname, "binary") == 0)
718 (errcode(ERRCODE_SYNTAX_ERROR),
719 errmsg("conflicting or redundant options")));
720 binary = intVal(defel->arg);
722 else if (strcmp(defel->defname, "oids") == 0)
726 (errcode(ERRCODE_SYNTAX_ERROR),
727 errmsg("conflicting or redundant options")));
728 oids = intVal(defel->arg);
730 else if (strcmp(defel->defname, "delimiter") == 0)
734 (errcode(ERRCODE_SYNTAX_ERROR),
735 errmsg("conflicting or redundant options")));
736 delim = strVal(defel->arg);
738 else if (strcmp(defel->defname, "null") == 0)
742 (errcode(ERRCODE_SYNTAX_ERROR),
743 errmsg("conflicting or redundant options")));
744 null_print = strVal(defel->arg);
746 else if (strcmp(defel->defname, "csv") == 0)
750 (errcode(ERRCODE_SYNTAX_ERROR),
751 errmsg("conflicting or redundant options")));
752 csv_mode = intVal(defel->arg);
754 else if (strcmp(defel->defname, "quote") == 0)
758 (errcode(ERRCODE_SYNTAX_ERROR),
759 errmsg("conflicting or redundant options")));
760 quote = strVal(defel->arg);
762 else if (strcmp(defel->defname, "escape") == 0)
766 (errcode(ERRCODE_SYNTAX_ERROR),
767 errmsg("conflicting or redundant options")));
768 escape = strVal(defel->arg);
770 else if (strcmp(defel->defname, "force_quote") == 0)
774 (errcode(ERRCODE_SYNTAX_ERROR),
775 errmsg("conflicting or redundant options")));
776 force_quote = (List *)defel->arg;
778 else if (strcmp(defel->defname, "force_notnull") == 0)
782 (errcode(ERRCODE_SYNTAX_ERROR),
783 errmsg("conflicting or redundant options")));
784 force_notnull = (List *)defel->arg;
787 elog(ERROR, "option \"%s\" not recognized",
793 (errcode(ERRCODE_SYNTAX_ERROR),
794 errmsg("cannot specify DELIMITER in BINARY mode")));
796 if (binary && csv_mode)
798 (errcode(ERRCODE_SYNTAX_ERROR),
799 errmsg("cannot specify CSV in BINARY mode")));
801 if (binary && null_print)
803 (errcode(ERRCODE_SYNTAX_ERROR),
804 errmsg("cannot specify NULL in BINARY mode")));
808 delim = csv_mode ? "," : "\t";
811 null_print = csv_mode ? "" : "\\N";
822 * Only single-character delimiter strings are supported.
824 if (strlen(delim) != 1)
826 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
827 errmsg("COPY delimiter must be a single character")));
832 if (!csv_mode && quote != NULL)
834 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
835 errmsg("COPY quote available only in CSV mode")));
837 if (csv_mode && strlen(quote) != 1)
839 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
840 errmsg("COPY quote must be a single character")));
845 if (!csv_mode && escape != NULL)
847 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
848 errmsg("COPY escape available only in CSV mode")));
850 if (csv_mode && strlen(escape) != 1)
852 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
853 errmsg("COPY escape must be a single character")));
858 if (!csv_mode && force_quote != NIL)
860 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
861 errmsg("COPY force quote available only in CSV mode")));
862 if (force_quote != NIL && is_from)
864 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
865 errmsg("COPY force quote only available using COPY TO")));
868 * Check force_notnull
870 if (!csv_mode && force_notnull != NIL)
872 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
873 errmsg("COPY force not null available only in CSV mode")));
874 if (force_notnull != NIL && !is_from)
876 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
877 errmsg("COPY force not null only available using COPY FROM")));
880 * Don't allow the delimiter to appear in the null string.
882 if (strchr(null_print, delim[0]) != NULL)
884 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
885 errmsg("COPY delimiter must not appear in the NULL specification")));
888 * Don't allow the csv quote char to appear in the null string.
890 if (csv_mode && strchr(null_print, quote[0]) != NULL)
892 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
893 errmsg("CSV quote character must not appear in the NULL specification")));
896 * Open and lock the relation, using the appropriate lock type.
898 rel = heap_openrv(relation, (is_from ? RowExclusiveLock : AccessShareLock));
900 /* check read-only transaction */
901 if (XactReadOnly && !is_from && !isTempNamespace(RelationGetNamespace(rel)))
903 (errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
904 errmsg("transaction is read-only")));
906 /* Check permissions. */
907 aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(),
909 if (aclresult != ACLCHECK_OK)
910 aclcheck_error(aclresult, ACL_KIND_CLASS,
911 RelationGetRelationName(rel));
912 if (!pipe && !superuser())
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.")));
920 * Don't allow COPY w/ OIDs to or from a table without them
922 if (oids && !rel->rd_rel->relhasoids)
924 (errcode(ERRCODE_UNDEFINED_COLUMN),
925 errmsg("table \"%s\" does not have OIDs",
926 RelationGetRelationName(rel))));
929 * Generate or convert list of attributes to process
931 attnumlist = CopyGetAttnums(rel, attnamelist);
934 * Check that FORCE QUOTE references valid COPY columns
938 TupleDesc tupDesc = RelationGetDescr(rel);
939 Form_pg_attribute *attr = tupDesc->attrs;
942 force_quote_atts = CopyGetAttnums(rel, force_quote);
944 foreach(cur, force_quote_atts)
946 int attnum = lfirst_int(cur);
948 if (!list_member_int(attnumlist, attnum))
950 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
951 errmsg("FORCE QUOTE column \"%s\" not referenced by COPY",
952 NameStr(attr[attnum - 1]->attname))));
957 * Check that FORCE NOT NULL references valid COPY columns
962 TupleDesc tupDesc = RelationGetDescr(rel);
963 Form_pg_attribute *attr = tupDesc->attrs;
965 force_notnull_atts = CopyGetAttnums(rel, force_notnull);
967 foreach(cur, force_notnull_atts)
969 int attnum = lfirst_int(cur);
971 if (!list_member_int(attnumlist, attnum))
973 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
974 errmsg("FORCE NOT NULL column \"%s\" not referenced by COPY",
975 NameStr(attr[attnum - 1]->attname))));
980 * Set up variables to avoid per-attribute overhead.
982 initStringInfo(&attribute_buf);
983 initStringInfo(&line_buf);
984 line_buf_converted = false;
986 client_encoding = pg_get_client_encoding();
987 server_encoding = GetDatabaseEncoding();
989 copy_dest = COPY_FILE; /* default */
995 { /* copy from file to database */
996 if (rel->rd_rel->relkind != RELKIND_RELATION)
998 if (rel->rd_rel->relkind == RELKIND_VIEW)
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)
1005 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1006 errmsg("cannot copy to sequence \"%s\"",
1007 RelationGetRelationName(rel))));
1010 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1011 errmsg("cannot copy to non-table relation \"%s\"",
1012 RelationGetRelationName(rel))));
1016 if (whereToSendOutput == Remote)
1017 ReceiveCopyBegin(binary, list_length(attnumlist));
1025 copy_file = AllocateFile(filename, PG_BINARY_R);
1027 if (copy_file == NULL)
1029 (errcode_for_file_access(),
1030 errmsg("could not open file \"%s\" for reading: %m",
1033 fstat(fileno(copy_file), &st);
1034 if (S_ISDIR(st.st_mode))
1036 FreeFile(copy_file);
1038 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1039 errmsg("\"%s\" is a directory", filename)));
1042 CopyFrom(rel, attnumlist, binary, oids, delim, null_print, csv_mode,
1043 quote, escape, force_notnull_atts);
1046 { /* copy from database to file */
1047 if (rel->rd_rel->relkind != RELKIND_RELATION)
1049 if (rel->rd_rel->relkind == RELKIND_VIEW)
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)
1056 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1057 errmsg("cannot copy from sequence \"%s\"",
1058 RelationGetRelationName(rel))));
1061 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1062 errmsg("cannot copy from non-table relation \"%s\"",
1063 RelationGetRelationName(rel))));
1067 if (whereToSendOutput == Remote)
1074 mode_t oumask; /* Pre-existing umask value */
1078 * Prevent write to relative path ... too easy to shoot
1079 * oneself in the foot by overwriting a database file ...
1081 if (!is_absolute_path(filename))
1083 (errcode(ERRCODE_INVALID_NAME),
1084 errmsg("relative path not allowed for COPY to file")));
1086 oumask = umask((mode_t) 022);
1087 copy_file = AllocateFile(filename, PG_BINARY_W);
1090 if (copy_file == NULL)
1092 (errcode_for_file_access(),
1093 errmsg("could not open file \"%s\" for writing: %m",
1096 fstat(fileno(copy_file), &st);
1097 if (S_ISDIR(st.st_mode))
1099 FreeFile(copy_file);
1101 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1102 errmsg("\"%s\" is a directory", filename)));
1106 DoCopyTo(rel, attnumlist, binary, oids, delim, null_print, csv_mode,
1107 quote, escape, force_quote_atts, fe_copy);
1112 /* we assume only the write case could fail here */
1113 if (FreeFile(copy_file))
1115 (errcode_for_file_access(),
1116 errmsg("could not write to file \"%s\": %m",
1119 pfree(attribute_buf.data);
1120 pfree(line_buf.data);
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
1128 heap_close(rel, (is_from ? NoLock : AccessShareLock));
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".
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)
1144 SendCopyBegin(binary, list_length(attnumlist));
1146 CopyTo(rel, attnumlist, binary, oids, delim, null_print, csv_mode,
1147 quote, escape, force_quote_atts);
1150 SendCopyEnd(binary);
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.
1159 pq_endcopyout(true);
1166 * Copy from relation TO file.
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)
1175 HeapScanDesc scandesc;
1178 Form_pg_attribute *attr;
1179 FmgrInfo *out_functions;
1184 Snapshot mySnapshot;
1186 MemoryContext oldcontext;
1187 MemoryContext mycontext;
1189 tupDesc = rel->rd_att;
1190 attr = tupDesc->attrs;
1191 num_phys_attrs = tupDesc->natts;
1192 attr_count = list_length(attnumlist);
1195 * Get info about the columns we need to process.
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)
1203 int attnum = lfirst_int(cur);
1207 getTypeBinaryOutputInfo(attr[attnum - 1]->atttypid,
1208 &out_func_oid, &typioparams[attnum - 1],
1209 &isvarlena[attnum - 1]);
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]);
1216 if (list_member_int(force_quote_atts, attnum))
1217 force_quote[attnum - 1] = true;
1219 force_quote[attnum - 1] = false;
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.)
1228 mycontext = AllocSetContextCreate(CurrentMemoryContext,
1230 ALLOCSET_DEFAULT_MINSIZE,
1231 ALLOCSET_DEFAULT_INITSIZE,
1232 ALLOCSET_DEFAULT_MAXSIZE);
1236 /* Generate header for a binary copy */
1240 CopySendData((char *) BinarySignature, 11);
1246 /* No header extension */
1253 * For non-binary copy, we need to convert null_print to client
1254 * encoding, because it will be sent directly with CopySendString.
1256 if (server_encoding != client_encoding)
1257 null_print = (char *)
1258 pg_server_to_client((unsigned char *) null_print,
1259 strlen(null_print));
1262 mySnapshot = CopyQuerySnapshot();
1264 scandesc = heap_beginscan(rel, mySnapshot, 0, NULL);
1266 while ((tuple = heap_getnext(scandesc, ForwardScanDirection)) != NULL)
1268 bool need_delim = false;
1269 CHECK_FOR_INTERRUPTS();
1271 MemoryContextReset(mycontext);
1272 oldcontext = MemoryContextSwitchTo(mycontext);
1276 /* Binary per-tuple header */
1277 CopySendInt16(attr_count);
1278 /* Send OID if wanted --- note attr_count doesn't include it */
1281 Oid oid = HeapTupleGetOid(tuple);
1283 /* Hack --- assume Oid is same size as int32 */
1284 CopySendInt32(sizeof(int32));
1290 /* Text format has no per-tuple header, but send OID if wanted */
1293 string = DatumGetCString(DirectFunctionCall1(oidout,
1294 ObjectIdGetDatum(HeapTupleGetOid(tuple))));
1295 CopySendString(string);
1300 foreach(cur, attnumlist)
1302 int attnum = lfirst_int(cur);
1306 value = heap_getattr(tuple, attnum, tupDesc, &isnull);
1311 CopySendChar(delim[0]);
1318 CopySendString(null_print); /* null indicator */
1320 CopySendInt32(-1); /* null marker */
1326 string = DatumGetCString(FunctionCall3(&out_functions[attnum - 1],
1328 ObjectIdGetDatum(typioparams[attnum - 1]),
1329 Int32GetDatum(attr[attnum - 1]->atttypmod)));
1332 CopyAttributeOutCSV(string, delim, quote, escape,
1333 (strcmp(string, null_print) == 0 ||
1334 force_quote[attnum - 1]));
1337 CopyAttributeOut(string, delim);
1344 outputbytes = DatumGetByteaP(FunctionCall2(&out_functions[attnum - 1],
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);
1355 CopySendEndOfRow(binary);
1357 MemoryContextSwitchTo(oldcontext);
1360 heap_endscan(scandesc);
1364 /* Generate trailer for a binary copy */
1368 MemoryContextDelete(mycontext);
1370 pfree(out_functions);
1378 * error context callback for COPY FROM
1381 copy_in_error_callback(void *arg)
1385 /* can't usefully display the data */
1387 errcontext("COPY %s, line %d, column %s",
1388 copy_relname, copy_lineno, copy_attname);
1390 errcontext("COPY %s, line %d", copy_relname, copy_lineno);
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);
1404 /* error is relevant to a particular line */
1405 if (!line_buf_converted)
1407 /* didn't convert the encoding yet... */
1408 line_buf_converted = true;
1409 if (client_encoding != server_encoding)
1413 cvt = (char *) pg_client_to_server((unsigned char *) line_buf.data,
1415 if (cvt != line_buf.data)
1417 /* transfer converted data back to line_buf */
1419 line_buf.data[0] = '\0';
1420 appendBinaryStringInfo(&line_buf, cvt, strlen(cvt));
1424 limit_printout_length(&line_buf);
1425 errcontext("COPY %s, line %d: \"%s\"",
1426 copy_relname, copy_lineno,
1433 * Make sure we don't print an unreasonable amount of COPY data in a message.
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.
1443 limit_printout_length(StringInfo buf)
1445 #define MAX_COPY_DATA_DISPLAY 100
1449 /* Fast path if definitely okay */
1450 if (buf->len <= MAX_COPY_DATA_DISPLAY)
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 */
1458 buf->data[len] = '\0';
1460 /* Add "..." to show we truncated the input */
1461 appendStringInfoString(buf, "...");
1465 * Copy FROM file to relation.
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)
1474 Form_pg_attribute *attr;
1475 AttrNumber num_phys_attrs,
1478 FmgrInfo *in_functions;
1479 FmgrInfo oid_in_function;
1482 ExprState **constraintexprs;
1483 bool *force_notnull;
1484 bool hasConstraints = false;
1492 ResultRelInfo *resultRelInfo;
1493 EState *estate = CreateExecutorState(); /* for ExecConstraints() */
1494 TupleTable tupleTable;
1495 TupleTableSlot *slot;
1498 ExprState **defexprs; /* array of default att expressions */
1499 ExprContext *econtext; /* used for ExecEvalExpr for default atts */
1500 MemoryContext oldcontext = CurrentMemoryContext;
1501 ErrorContextCallback errcontext;
1503 tupDesc = RelationGetDescr(rel);
1504 attr = tupDesc->attrs;
1505 num_phys_attrs = tupDesc->natts;
1506 attr_count = list_length(attnumlist);
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 ...)
1514 resultRelInfo = makeNode(ResultRelInfo);
1515 resultRelInfo->ri_RangeTableIndex = 1; /* dummy */
1516 resultRelInfo->ri_RelationDesc = rel;
1517 resultRelInfo->ri_TrigDesc = CopyTriggerDesc(rel->trigdesc);
1519 ExecOpenIndices(resultRelInfo);
1521 estate->es_result_relations = resultRelInfo;
1522 estate->es_num_result_relations = 1;
1523 estate->es_result_relation_info = resultRelInfo;
1525 /* Set up a dummy tuple table too */
1526 tupleTable = ExecCreateTupleTable(1);
1527 slot = ExecAllocTableSlot(tupleTable);
1528 ExecSetSlotDescriptor(slot, tupDesc, false);
1530 econtext = GetPerTupleExprContext(estate);
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.)
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));
1545 for (attnum = 1; attnum <= num_phys_attrs; attnum++)
1547 /* We don't need info for dropped attributes */
1548 if (attr[attnum - 1]->attisdropped)
1551 /* Fetch the input function and typioparam info */
1553 getTypeBinaryInputInfo(attr[attnum - 1]->atttypid,
1554 &in_func_oid, &typioparams[attnum - 1]);
1556 getTypeInputInfo(attr[attnum - 1]->atttypid,
1557 &in_func_oid, &typioparams[attnum - 1]);
1558 fmgr_info(in_func_oid, &in_functions[attnum - 1]);
1560 if (list_member_int(force_notnull_atts, attnum))
1561 force_notnull[attnum - 1] = true;
1563 force_notnull[attnum - 1] = false;
1565 /* Get default info if needed */
1566 if (!list_member_int(attnumlist, attnum))
1568 /* attribute is NOT to be copied from input */
1569 /* use default value if one exists */
1570 Node *defexpr = build_column_default(rel, attnum);
1572 if (defexpr != NULL)
1574 defexprs[num_defaults] = ExecPrepareExpr((Expr *) defexpr,
1576 defmap[num_defaults] = attnum - 1;
1581 /* If it's a domain type, set up to check domain constraints */
1582 if (get_typtype(attr[attnum - 1]->atttypid) == 'd')
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.
1595 prm = makeNode(Param);
1596 prm->paramkind = PARAM_EXEC;
1598 prm->paramtype = getBaseType(attr[attnum - 1]->atttypid);
1600 node = coerce_to_domain((Node *) prm,
1602 attr[attnum - 1]->atttypid,
1603 COERCE_IMPLICIT_CAST, false);
1605 constraintexprs[attnum - 1] = ExecPrepareExpr((Expr *) node,
1607 hasConstraints = true;
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
1618 ExecBSInsertTriggers(estate, resultRelInfo);
1621 file_has_oids = oids; /* must rely on user to tell us this... */
1624 /* Read and verify binary header */
1629 CopyGetData(readSig, 11);
1630 if (CopyGetEof() || memcmp(readSig, BinarySignature, 11) != 0)
1632 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1633 errmsg("COPY file signature not recognized")));
1635 tmp = CopyGetInt32();
1638 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1639 errmsg("invalid COPY file header (missing flags)")));
1640 file_has_oids = (tmp & (1 << 16)) != 0;
1642 if ((tmp >> 16) != 0)
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)
1650 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1651 errmsg("invalid COPY file header (missing length)")));
1652 /* Skip extension header, if present */
1655 CopyGetData(readSig, 1);
1658 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1659 errmsg("invalid COPY file header (wrong length)")));
1663 if (file_has_oids && binary)
1665 getTypeBinaryInputInfo(OIDOID,
1666 &in_func_oid, &oid_typioparam);
1667 fmgr_info(in_func_oid, &oid_in_function);
1670 values = (Datum *) palloc(num_phys_attrs * sizeof(Datum));
1671 nulls = (char *) palloc(num_phys_attrs * sizeof(char));
1673 /* Make room for a PARAM_EXEC value for domain constraint checks */
1675 econtext->ecxt_param_exec_vals = (ParamExecData *)
1676 palloc0(sizeof(ParamExecData));
1678 /* Initialize static variables */
1680 eol_type = EOL_UNKNOWN;
1681 copy_binary = binary;
1682 copy_relname = RelationGetRelationName(rel);
1684 copy_attname = NULL;
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;
1695 Oid loaded_oid = InvalidOid;
1697 CHECK_FOR_INTERRUPTS();
1701 /* Reset the per-tuple exprcontext */
1702 ResetPerTupleExprContext(estate);
1704 /* Switch into its memory context */
1705 MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
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));
1713 CopyReadResult result = NORMAL_ATTR;
1717 /* Actually read the line into memory here */
1718 done = CopyReadLine();
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.
1726 if (done && line_buf.len == 0)
1731 /* can't be in CSV mode here */
1732 string = CopyReadAttribute(delim, null_print,
1737 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1738 errmsg("null OID in COPY data")));
1741 copy_attname = "oid";
1742 loaded_oid = DatumGetObjectId(DirectFunctionCall1(oidin,
1743 CStringGetDatum(string)));
1744 if (loaded_oid == InvalidOid)
1746 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1747 errmsg("invalid OID in COPY data")));
1748 copy_attname = NULL;
1753 * Loop to read the user attributes on the line.
1755 foreach(cur, attnumlist)
1757 int attnum = lfirst_int(cur);
1761 * If prior attr on this line was ended by newline,
1764 if (result != NORMAL_ATTR)
1766 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1767 errmsg("missing data for column \"%s\"",
1768 NameStr(attr[m]->attname))));
1772 string = CopyReadAttributeCSV(delim, null_print, quote,
1773 escape, &result, &isnull);
1774 if (result == UNTERMINATED_FIELD)
1776 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1777 errmsg("unterminated CSV quoted field")));
1780 string = CopyReadAttribute(delim, null_print,
1783 if (csv_mode && isnull && force_notnull[m])
1785 string = null_print; /* set to NULL string */
1789 /* we read an SQL NULL, no need to do anything */
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));
1798 copy_attname = NULL;
1803 * Complain if there are more fields on the input line.
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
1809 if (result == NORMAL_ATTR && line_buf.len != 0)
1811 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1812 errmsg("extra data after last expected column")));
1820 fld_count = CopyGetInt16();
1821 if (CopyGetEof() || fld_count == -1)
1827 if (fld_count != attr_count)
1829 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1830 errmsg("row field count is %d, expected %d",
1831 (int) fld_count, attr_count)));
1835 copy_attname = "oid";
1837 DatumGetObjectId(CopyReadBinaryAttribute(0,
1841 if (isnull || loaded_oid == InvalidOid)
1843 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1844 errmsg("invalid OID in COPY data")));
1845 copy_attname = NULL;
1849 foreach(cur, attnumlist)
1851 int attnum = lfirst_int(cur);
1854 copy_attname = NameStr(attr[m]->attname);
1856 values[m] = CopyReadBinaryAttribute(i,
1860 nulls[m] = isnull ? 'n' : ' ';
1861 copy_attname = NULL;
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.
1870 for (i = 0; i < num_defaults; i++)
1872 values[defmap[i]] = ExecEvalExpr(defexprs[i], econtext,
1875 nulls[defmap[i]] = ' ';
1879 * Next apply any domain constraints
1883 ParamExecData *prmdata = &econtext->ecxt_param_exec_vals[0];
1885 for (i = 0; i < num_phys_attrs; i++)
1887 ExprState *exprstate = constraintexprs[i];
1889 if (exprstate == NULL)
1890 continue; /* no constraint for this attr */
1892 /* Insert current row's value into the Param value */
1893 prmdata->value = values[i];
1894 prmdata->isnull = (nulls[i] == 'n');
1897 * Execute the constraint expression. Allow the
1898 * expression to replace the value (consider e.g. a
1899 * timestamp precision restriction).
1901 values[i] = ExecEvalExpr(exprstate, econtext,
1903 nulls[i] = isnull ? 'n' : ' ';
1908 * And now we can form the input tuple.
1910 tuple = heap_formtuple(tupDesc, values, nulls);
1912 if (oids && file_has_oids)
1913 HeapTupleSetOid(tuple, loaded_oid);
1916 * Triggers and stuff need to be invoked in query context.
1918 MemoryContextSwitchTo(oldcontext);
1922 /* BEFORE ROW INSERT Triggers */
1923 if (resultRelInfo->ri_TrigDesc &&
1924 resultRelInfo->ri_TrigDesc->n_before_row[TRIGGER_EVENT_INSERT] > 0)
1928 newtuple = ExecBRInsertTriggers(estate, resultRelInfo, tuple);
1930 if (newtuple == NULL) /* "do nothing" */
1932 else if (newtuple != tuple) /* modified by Trigger(s) */
1934 heap_freetuple(tuple);
1941 /* Place tuple in tuple slot */
1942 ExecStoreTuple(tuple, slot, InvalidBuffer, false);
1945 * Check the constraints of the tuple
1947 if (rel->rd_att->constr)
1948 ExecConstraints(resultRelInfo, slot, estate);
1951 * OK, store the tuple and create index entries for it
1953 simple_heap_insert(rel, tuple);
1955 if (resultRelInfo->ri_NumIndices > 0)
1956 ExecInsertIndexTuples(slot, &(tuple->t_self), estate, false);
1958 /* AFTER ROW INSERT Triggers */
1959 ExecARInsertTriggers(estate, resultRelInfo, tuple);
1966 error_context_stack = errcontext.previous;
1968 MemoryContextSwitchTo(oldcontext);
1971 * Execute AFTER STATEMENT insertion triggers
1973 ExecASInsertTriggers(estate, resultRelInfo);
1978 pfree(in_functions);
1982 pfree(constraintexprs);
1983 pfree(force_notnull);
1985 ExecDropTupleTable(tupleTable, true);
1987 ExecCloseIndices(resultRelInfo);
1989 FreeExecutorState(estate);
1994 * Read the next input line and stash it in line_buf, with conversion to
1997 * Result is true if read was terminated by EOF, false if terminated
2004 bool change_encoding = (client_encoding != server_encoding);
2013 /* reset line_buf to empty */
2015 line_buf.data[0] = '\0';
2016 line_buf.cursor = 0;
2018 /* mark that encoding conversion hasn't occurred yet */
2019 line_buf_converted = false;
2021 /* set default status */
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.
2044 if (eol_type == EOL_NL)
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)
2052 int c2 = CopyPeekChar();
2056 CopyDonePeek(c2, true); /* eat newline */
2057 eol_type = EOL_CRNL;
2061 /* found \r, but no \n */
2062 if (eol_type == EOL_CRNL)
2064 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2065 errmsg("literal carriage return found in data"),
2066 errhint("Use \"\\r\" to represent carriage return.")));
2069 * if we got here, it is the first line and we didn't
2070 * get \n, so put it back
2072 CopyDonePeek(c2, false);
2080 if (eol_type == EOL_CR || eol_type == EOL_CRNL)
2082 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2083 errmsg("literal newline found in data"),
2084 errhint("Use \"\\n\" to represent newline.")));
2098 if (eol_type == EOL_CRNL)
2103 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2104 errmsg("end-of-copy marker does not match previous newline style")));
2107 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2108 errmsg("end-of-copy marker corrupt")));
2111 if (c != '\r' && c != '\n')
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'))
2119 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2120 errmsg("end-of-copy marker does not match previous newline style")));
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?)
2127 if (copy_dest == COPY_NEW_FE)
2132 result = true; /* report EOF */
2135 /* not EOF mark, so emit \ and following char literally */
2136 appendStringInfoCharMacro(&line_buf, '\\');
2139 appendStringInfoCharMacro(&line_buf, c);
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.
2148 if (change_encoding)
2151 mblen = pg_encoding_mblen(client_encoding, s);
2152 for (j = 1; j < mblen; j++)
2160 appendStringInfoCharMacro(&line_buf, c);
2163 break; /* out of outer loop */
2165 } /* end of outer loop */
2168 * Done reading the line. Convert it to server encoding.
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 ...
2176 line_buf_converted = true;
2178 if (change_encoding)
2180 cvt = (char *) pg_client_to_server((unsigned char *) line_buf.data,
2182 if (cvt != line_buf.data)
2184 /* transfer converted data back to line_buf */
2186 line_buf.data[0] = '\0';
2187 appendBinaryStringInfo(&line_buf, cvt, strlen(cvt));
2195 * Read the value of a single attribute, performing de-escaping as needed.
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.
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.
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.
2213 CopyReadAttribute(const char *delim, const char *null_print,
2214 CopyReadResult *result, bool *isnull)
2217 char delimc = delim[0];
2218 int start_cursor = line_buf.cursor;
2222 /* reset attribute_buf to empty */
2223 attribute_buf.len = 0;
2224 attribute_buf.data[0] = '\0';
2226 /* set default status */
2227 *result = END_OF_LINE;
2231 end_cursor = line_buf.cursor;
2232 if (line_buf.cursor >= line_buf.len)
2234 c = line_buf.data[line_buf.cursor++];
2237 *result = NORMAL_ATTR;
2242 if (line_buf.cursor >= line_buf.len)
2244 c = line_buf.data[line_buf.cursor++];
2259 if (line_buf.cursor < line_buf.len)
2261 c = line_buf.data[line_buf.cursor];
2265 val = (val << 3) + OCTVALUE(c);
2266 if (line_buf.cursor < line_buf.len)
2268 c = line_buf.data[line_buf.cursor];
2272 val = (val << 3) + OCTVALUE(c);
2299 * in all other cases, take the char after '\' literally
2303 appendStringInfoCharMacro(&attribute_buf, c);
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)
2314 return attribute_buf.data;
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.
2323 * Quoted fields can span lines, in which case the line end is embedded
2324 * in the returned string.
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).
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
2334 * In any case, the string read up to the terminator (or end of file)
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.
2345 CopyReadAttributeCSV(const char *delim, const char *null_print, char *quote,
2346 char *escape, CopyReadResult *result, bool *isnull)
2348 char delimc = delim[0];
2349 char quotec = quote[0];
2350 char escapec = escape[0];
2352 int start_cursor = line_buf.cursor;
2353 int end_cursor = start_cursor;
2355 bool in_quote = false;
2356 bool saw_quote = false;
2358 /* reset attribute_buf to empty */
2359 attribute_buf.len = 0;
2360 attribute_buf.data[0] = '\0';
2362 /* set default status */
2363 *result = END_OF_LINE;
2367 /* handle multiline quoted fields */
2368 if (in_quote && line_buf.cursor >= line_buf.len)
2375 appendStringInfoString(&attribute_buf,"\n");
2378 appendStringInfoString(&attribute_buf,"\r");
2381 appendStringInfoString(&attribute_buf,"\r\n");
2384 /* shouldn't happen - just keep going */
2389 done = CopyReadLine();
2390 if (done && line_buf.len == 0)
2392 start_cursor = line_buf.cursor;
2395 end_cursor = line_buf.cursor;
2396 if (line_buf.cursor >= line_buf.len)
2398 c = line_buf.data[line_buf.cursor++];
2400 * unquoted field delimiter
2402 if (!in_quote && c == delimc)
2404 *result = NORMAL_ATTR;
2408 * start of quoted field (or part of field)
2410 if (!in_quote && c == quotec)
2417 * escape within a quoted field
2419 if (in_quote && c == escapec)
2422 * peek at the next char if available, and escape it if it
2423 * is an escape char or a quote char
2425 if (line_buf.cursor <= line_buf.len)
2427 char nextc = line_buf.data[line_buf.cursor];
2428 if (nextc == escapec || nextc == quotec)
2430 appendStringInfoCharMacro(&attribute_buf, nextc);
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).
2441 if (in_quote && c == quotec)
2446 appendStringInfoCharMacro(&attribute_buf, c);
2450 *result = UNTERMINATED_FIELD;
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)
2460 return attribute_buf.data;
2464 * Read a binary attribute
2467 CopyReadBinaryAttribute(int column_no, FmgrInfo *flinfo, Oid typioparam,
2473 fld_size = CopyGetInt32();
2476 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2477 errmsg("unexpected EOF in COPY data")));
2485 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2486 errmsg("invalid field size")));
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;
2493 enlargeStringInfo(&attribute_buf, fld_size);
2495 CopyGetData(attribute_buf.data, fld_size);
2498 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2499 errmsg("unexpected EOF in COPY data")));
2501 attribute_buf.len = fld_size;
2502 attribute_buf.data[fld_size] = '\0';
2504 /* Call the column type's binary input converter */
2505 result = FunctionCall2(flinfo,
2506 PointerGetDatum(&attribute_buf),
2507 ObjectIdGetDatum(typioparam));
2509 /* Trouble if it didn't eat the whole buffer */
2510 if (attribute_buf.cursor != attribute_buf.len)
2512 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
2513 errmsg("incorrect binary data format")));
2520 * Send text representation of one attribute, with conversion and escaping
2523 CopyAttributeOut(char *server_string, char *delim)
2527 char delimc = delim[0];
2532 same_encoding = (server_encoding == client_encoding);
2534 string = (char *) pg_server_to_client((unsigned char *) server_string,
2535 strlen(server_string));
2537 string = server_string;
2539 for (; (c = *string) != '\0'; string += mblen)
2546 CopySendString("\\b");
2549 CopySendString("\\f");
2552 CopySendString("\\n");
2555 CopySendString("\\r");
2558 CopySendString("\\t");
2561 CopySendString("\\v");
2564 CopySendString("\\\\");
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.
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]);
2589 * Send CSV representation of one attribute, with conversion and
2593 CopyAttributeOutCSV(char *server_string, char *delim, char *quote,
2594 char *escape, bool use_quote)
2598 char delimc = delim[0];
2599 char quotec = quote[0];
2600 char escapec = escape[0];
2606 same_encoding = (server_encoding == client_encoding);
2608 string = (char *) pg_server_to_client((unsigned char *) server_string,
2609 strlen(server_string));
2611 string = server_string;
2613 /* have to run through the string twice,
2614 * first time to see if it needs quoting, second to actually send it
2617 for(test_string = string;
2618 !use_quote && (c = *test_string) != '\0';
2619 test_string += mblen)
2621 if (c == delimc || c == quotec || c == '\n' || c == '\r')
2624 mblen = pg_encoding_mblen(client_encoding, test_string);
2630 CopySendChar(quotec);
2632 for (; (c = *string) != '\0'; string += mblen)
2634 if (use_quote && (c == quotec || c == escapec))
2635 CopySendChar(escapec);
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]);
2651 CopySendChar(quotec);
2655 * CopyGetAttnums - build an integer list of attnums to be copied
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
2662 CopyGetAttnums(Relation rel, List *attnamelist)
2664 List *attnums = NIL;
2666 if (attnamelist == NIL)
2668 /* Generate default column list */
2669 TupleDesc tupDesc = RelationGetDescr(rel);
2670 Form_pg_attribute *attr = tupDesc->attrs;
2671 int attr_count = tupDesc->natts;
2674 for (i = 0; i < attr_count; i++)
2676 if (attr[i]->attisdropped)
2678 attnums = lappend_int(attnums, i + 1);
2683 /* Validate the user-supplied list and extract attnums */
2686 foreach(l, attnamelist)
2688 char *name = strVal(lfirst(l));
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))
2697 (errcode(ERRCODE_DUPLICATE_COLUMN),
2698 errmsg("column \"%s\" specified more than once",
2700 attnums = lappend_int(attnums, attnum);