1 /*-------------------------------------------------------------------------
4 * Implements the COPY utility command
6 * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/commands/copy.c
13 *-------------------------------------------------------------------------
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
23 #include "access/heapam.h"
24 #include "access/htup_details.h"
25 #include "access/sysattr.h"
26 #include "access/xact.h"
27 #include "catalog/namespace.h"
28 #include "catalog/pg_type.h"
29 #include "commands/copy.h"
30 #include "commands/defrem.h"
31 #include "commands/trigger.h"
32 #include "executor/executor.h"
33 #include "libpq/libpq.h"
34 #include "libpq/pqformat.h"
35 #include "mb/pg_wchar.h"
36 #include "miscadmin.h"
37 #include "optimizer/clauses.h"
38 #include "optimizer/planner.h"
39 #include "parser/parse_relation.h"
40 #include "rewrite/rewriteHandler.h"
41 #include "storage/fd.h"
42 #include "tcop/tcopprot.h"
43 #include "utils/acl.h"
44 #include "utils/builtins.h"
45 #include "utils/lsyscache.h"
46 #include "utils/memutils.h"
47 #include "utils/portal.h"
48 #include "utils/rel.h"
49 #include "utils/snapmgr.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 (or a piped program) */
62 COPY_OLD_FE, /* to/from frontend (2.0 protocol) */
63 COPY_NEW_FE /* to/from frontend (3.0 protocol) */
67 * Represents the end-of-line terminator type of the input
78 * This struct contains all the state variables used throughout a COPY
79 * operation. For simplicity, we use the same struct for all variants of COPY,
80 * even though some fields are used in only some cases.
82 * Multi-byte encodings: all supported client-side encodings encode multi-byte
83 * characters by having the first byte's high bit set. Subsequent bytes of the
84 * character can have the high bit not set. When scanning data in such an
85 * encoding to look for a match to a single-byte (ie ASCII) character, we must
86 * use the full pg_encoding_mblen() machinery to skip over multibyte
87 * characters, else we might find a false match to a trailing byte. In
88 * supported server encodings, there is no possibility of a false match, and
89 * it's faster to make useless comparisons to trailing bytes than it is to
90 * invoke pg_encoding_mblen() to skip over them. encoding_embeds_ascii is TRUE
91 * when we have to do it the hard way.
93 typedef struct CopyStateData
95 /* low-level state data */
96 CopyDest copy_dest; /* type of copy source/destination */
97 FILE *copy_file; /* used if copy_dest == COPY_FILE */
98 StringInfo fe_msgbuf; /* used for all dests during COPY TO, only for
99 * dest == COPY_NEW_FE in COPY FROM */
100 bool fe_eof; /* true if detected end of copy data */
101 EolType eol_type; /* EOL type of input */
102 int file_encoding; /* file or remote side's character encoding */
103 bool need_transcoding; /* file encoding diff from server? */
104 bool encoding_embeds_ascii; /* ASCII can be non-first byte? */
106 /* parameters from the COPY command */
107 Relation rel; /* relation to copy to or from */
108 QueryDesc *queryDesc; /* executable query to copy from */
109 List *attnumlist; /* integer list of attnums to copy */
110 char *filename; /* filename, or NULL for STDIN/STDOUT */
111 bool is_program; /* is 'filename' a program to popen? */
112 bool binary; /* binary format? */
113 bool oids; /* include OIDs? */
114 bool freeze; /* freeze rows on loading? */
115 bool csv_mode; /* Comma Separated Value format? */
116 bool header_line; /* CSV header line? */
117 char *null_print; /* NULL marker string (server encoding!) */
118 int null_print_len; /* length of same */
119 char *null_print_client; /* same converted to file encoding */
120 char *delim; /* column delimiter (must be 1 byte) */
121 char *quote; /* CSV quote char (must be 1 byte) */
122 char *escape; /* CSV escape char (must be 1 byte) */
123 List *force_quote; /* list of column names */
124 bool force_quote_all; /* FORCE QUOTE *? */
125 bool *force_quote_flags; /* per-column CSV FQ flags */
126 List *force_notnull; /* list of column names */
127 bool *force_notnull_flags; /* per-column CSV FNN flags */
128 bool convert_selectively; /* do selective binary conversion? */
129 List *convert_select; /* list of column names (can be NIL) */
130 bool *convert_select_flags; /* per-column CSV/TEXT CS flags */
132 /* these are just for error messages, see CopyFromErrorCallback */
133 const char *cur_relname; /* table name for error messages */
134 int cur_lineno; /* line number for error messages */
135 const char *cur_attname; /* current att for error messages */
136 const char *cur_attval; /* current att value for error messages */
139 * Working state for COPY TO/FROM
141 MemoryContext copycontext; /* per-copy execution context */
144 * Working state for COPY TO
146 FmgrInfo *out_functions; /* lookup info for output functions */
147 MemoryContext rowcontext; /* per-row evaluation context */
150 * Working state for COPY FROM
152 AttrNumber num_defaults;
154 FmgrInfo oid_in_function;
156 FmgrInfo *in_functions; /* array of input functions for each attrs */
157 Oid *typioparams; /* array of element types for in_functions */
158 int *defmap; /* array of default att numbers */
159 ExprState **defexprs; /* array of default att expressions */
160 bool volatile_defexprs; /* is any of defexprs volatile? */
163 * These variables are used to reduce overhead in textual COPY FROM.
165 * attribute_buf holds the separated, de-escaped text for each field of
166 * the current line. The CopyReadAttributes functions return arrays of
167 * pointers into this buffer. We avoid palloc/pfree overhead by re-using
168 * the buffer on each cycle.
170 StringInfoData attribute_buf;
172 /* field raw data pointers found by COPY FROM */
178 * Similarly, line_buf holds the whole input line being processed. The
179 * input cycle is first to read the whole line into line_buf, convert it
180 * to server encoding there, and then extract the individual attribute
181 * fields into attribute_buf. line_buf is preserved unmodified so that we
182 * can display it in error messages if appropriate.
184 StringInfoData line_buf;
185 bool line_buf_converted; /* converted to server encoding? */
186 bool line_buf_valid; /* contains the row being processed? */
189 * Finally, raw_buf holds raw data read from the data source (file or
190 * client connection). CopyReadLine parses this data sufficiently to
191 * locate line boundaries, then transfers the data to line_buf and
192 * converts it. Note: we guarantee that there is a \0 at
193 * raw_buf[raw_buf_len].
195 #define RAW_BUF_SIZE 65536 /* we palloc RAW_BUF_SIZE+1 bytes */
197 int raw_buf_index; /* next byte to process */
198 int raw_buf_len; /* total # of bytes stored */
201 /* DestReceiver for COPY (SELECT) TO */
204 DestReceiver pub; /* publicly-known function pointers */
205 CopyState cstate; /* CopyStateData for the command */
206 uint64 processed; /* # of tuples processed */
211 * These macros centralize code used to process line_buf and raw_buf buffers.
212 * They are macros because they often do continue/break control and to avoid
213 * function call overhead in tight COPY loops.
215 * We must use "if (1)" because the usual "do {...} while(0)" wrapper would
216 * prevent the continue/break processing from working. We end the "if (1)"
217 * with "else ((void) 0)" to ensure the "if" does not unintentionally match
218 * any "else" in the calling code, and to avoid any compiler warnings about
219 * empty statements. See http://www.cit.gu.edu.au/~anthony/info/C/C.macros.
223 * This keeps the character read at the top of the loop in the buffer
224 * even if there is more than one read-ahead.
226 #define IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(extralen) \
229 if (raw_buf_ptr + (extralen) >= copy_buf_len && !hit_eof) \
231 raw_buf_ptr = prev_raw_ptr; /* undo fetch */ \
237 /* This consumes the remainder of the buffer and breaks */
238 #define IF_NEED_REFILL_AND_EOF_BREAK(extralen) \
241 if (raw_buf_ptr + (extralen) >= copy_buf_len && hit_eof) \
244 raw_buf_ptr = copy_buf_len; /* consume the partial character */ \
245 /* backslash just before EOF, treat as data char */ \
252 * Transfer any approved data to line_buf; must do this to be sure
253 * there is some room in raw_buf.
255 #define REFILL_LINEBUF \
258 if (raw_buf_ptr > cstate->raw_buf_index) \
260 appendBinaryStringInfo(&cstate->line_buf, \
261 cstate->raw_buf + cstate->raw_buf_index, \
262 raw_buf_ptr - cstate->raw_buf_index); \
263 cstate->raw_buf_index = raw_buf_ptr; \
267 /* Undo any read-ahead and jump out of the block. */
268 #define NO_END_OF_COPY_GOTO \
271 raw_buf_ptr = prev_raw_ptr + 1; \
272 goto not_end_of_copy; \
275 static const char BinarySignature[11] = "PGCOPY\n\377\r\n\0";
278 /* non-export function prototypes */
279 static CopyState BeginCopy(bool is_from, Relation rel, Node *raw_query,
280 const char *queryString, List *attnamelist, List *options);
281 static void EndCopy(CopyState cstate);
282 static void ClosePipeToProgram(CopyState cstate);
283 static CopyState BeginCopyTo(Relation rel, Node *query, const char *queryString,
284 const char *filename, bool is_program, List *attnamelist,
286 static void EndCopyTo(CopyState cstate);
287 static uint64 DoCopyTo(CopyState cstate);
288 static uint64 CopyTo(CopyState cstate);
289 static void CopyOneRowTo(CopyState cstate, Oid tupleOid,
290 Datum *values, bool *nulls);
291 static uint64 CopyFrom(CopyState cstate);
292 static void CopyFromInsertBatch(CopyState cstate, EState *estate,
293 CommandId mycid, int hi_options,
294 ResultRelInfo *resultRelInfo, TupleTableSlot *myslot,
295 BulkInsertState bistate,
296 int nBufferedTuples, HeapTuple *bufferedTuples,
297 int firstBufferedLineNo);
298 static bool CopyReadLine(CopyState cstate);
299 static bool CopyReadLineText(CopyState cstate);
300 static int CopyReadAttributesText(CopyState cstate);
301 static int CopyReadAttributesCSV(CopyState cstate);
302 static Datum CopyReadBinaryAttribute(CopyState cstate,
303 int column_no, FmgrInfo *flinfo,
304 Oid typioparam, int32 typmod,
306 static void CopyAttributeOutText(CopyState cstate, char *string);
307 static void CopyAttributeOutCSV(CopyState cstate, char *string,
308 bool use_quote, bool single_attr);
309 static List *CopyGetAttnums(TupleDesc tupDesc, Relation rel,
311 static char *limit_printout_length(const char *str);
313 /* Low-level communications functions */
314 static void SendCopyBegin(CopyState cstate);
315 static void ReceiveCopyBegin(CopyState cstate);
316 static void SendCopyEnd(CopyState cstate);
317 static void CopySendData(CopyState cstate, const void *databuf, int datasize);
318 static void CopySendString(CopyState cstate, const char *str);
319 static void CopySendChar(CopyState cstate, char c);
320 static void CopySendEndOfRow(CopyState cstate);
321 static int CopyGetData(CopyState cstate, void *databuf,
322 int minread, int maxread);
323 static void CopySendInt32(CopyState cstate, int32 val);
324 static bool CopyGetInt32(CopyState cstate, int32 *val);
325 static void CopySendInt16(CopyState cstate, int16 val);
326 static bool CopyGetInt16(CopyState cstate, int16 *val);
330 * Send copy start/stop messages for frontend copies. These have changed
331 * in past protocol redesigns.
334 SendCopyBegin(CopyState cstate)
336 if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
340 int natts = list_length(cstate->attnumlist);
341 int16 format = (cstate->binary ? 1 : 0);
344 pq_beginmessage(&buf, 'H');
345 pq_sendbyte(&buf, format); /* overall format */
346 pq_sendint(&buf, natts, 2);
347 for (i = 0; i < natts; i++)
348 pq_sendint(&buf, format, 2); /* per-column formats */
350 cstate->copy_dest = COPY_NEW_FE;
352 else if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 2)
357 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
358 errmsg("COPY BINARY is not supported to stdout or from stdin")));
359 pq_putemptymessage('H');
360 /* grottiness needed for old COPY OUT protocol */
362 cstate->copy_dest = COPY_OLD_FE;
369 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
370 errmsg("COPY BINARY is not supported to stdout or from stdin")));
371 pq_putemptymessage('B');
372 /* grottiness needed for old COPY OUT protocol */
374 cstate->copy_dest = COPY_OLD_FE;
379 ReceiveCopyBegin(CopyState cstate)
381 if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
385 int natts = list_length(cstate->attnumlist);
386 int16 format = (cstate->binary ? 1 : 0);
389 pq_beginmessage(&buf, 'G');
390 pq_sendbyte(&buf, format); /* overall format */
391 pq_sendint(&buf, natts, 2);
392 for (i = 0; i < natts; i++)
393 pq_sendint(&buf, format, 2); /* per-column formats */
395 cstate->copy_dest = COPY_NEW_FE;
396 cstate->fe_msgbuf = makeStringInfo();
398 else if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 2)
403 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
404 errmsg("COPY BINARY is not supported to stdout or from stdin")));
405 pq_putemptymessage('G');
406 cstate->copy_dest = COPY_OLD_FE;
413 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
414 errmsg("COPY BINARY is not supported to stdout or from stdin")));
415 pq_putemptymessage('D');
416 cstate->copy_dest = COPY_OLD_FE;
418 /* We *must* flush here to ensure FE knows it can send. */
423 SendCopyEnd(CopyState cstate)
425 if (cstate->copy_dest == COPY_NEW_FE)
427 /* Shouldn't have any unsent data */
428 Assert(cstate->fe_msgbuf->len == 0);
429 /* Send Copy Done message */
430 pq_putemptymessage('c');
434 CopySendData(cstate, "\\.", 2);
435 /* Need to flush out the trailer (this also appends a newline) */
436 CopySendEndOfRow(cstate);
437 pq_endcopyout(false);
442 * CopySendData sends output data to the destination (file or frontend)
443 * CopySendString does the same for null-terminated strings
444 * CopySendChar does the same for single characters
445 * CopySendEndOfRow does the appropriate thing at end of each data row
446 * (data is not actually flushed except by CopySendEndOfRow)
448 * NB: no data conversion is applied by these functions
452 CopySendData(CopyState cstate, const void *databuf, int datasize)
454 appendBinaryStringInfo(cstate->fe_msgbuf, databuf, datasize);
458 CopySendString(CopyState cstate, const char *str)
460 appendBinaryStringInfo(cstate->fe_msgbuf, str, strlen(str));
464 CopySendChar(CopyState cstate, char c)
466 appendStringInfoCharMacro(cstate->fe_msgbuf, c);
470 CopySendEndOfRow(CopyState cstate)
472 StringInfo fe_msgbuf = cstate->fe_msgbuf;
474 switch (cstate->copy_dest)
479 /* Default line termination depends on platform */
481 CopySendChar(cstate, '\n');
483 CopySendString(cstate, "\r\n");
487 if (fwrite(fe_msgbuf->data, fe_msgbuf->len, 1,
488 cstate->copy_file) != 1 ||
489 ferror(cstate->copy_file))
491 if (cstate->is_program)
496 * The pipe will be closed automatically on error at
497 * the end of transaction, but we might get a better
498 * error message from the subprocess' exit code than
501 ClosePipeToProgram(cstate);
504 * If ClosePipeToProgram() didn't throw an error, the
505 * program terminated normally, but closed the pipe
506 * first. Restore errno, and throw an error.
511 (errcode_for_file_access(),
512 errmsg("could not write to COPY program: %m")));
516 (errcode_for_file_access(),
517 errmsg("could not write to COPY file: %m")));
521 /* The FE/BE protocol uses \n as newline for all platforms */
523 CopySendChar(cstate, '\n');
525 if (pq_putbytes(fe_msgbuf->data, fe_msgbuf->len))
527 /* no hope of recovering connection sync, so FATAL */
529 (errcode(ERRCODE_CONNECTION_FAILURE),
530 errmsg("connection lost during COPY to stdout")));
534 /* The FE/BE protocol uses \n as newline for all platforms */
536 CopySendChar(cstate, '\n');
538 /* Dump the accumulated row as one CopyData message */
539 (void) pq_putmessage('d', fe_msgbuf->data, fe_msgbuf->len);
543 resetStringInfo(fe_msgbuf);
547 * CopyGetData reads data from the source (file or frontend)
549 * We attempt to read at least minread, and at most maxread, bytes from
550 * the source. The actual number of bytes read is returned; if this is
551 * less than minread, EOF was detected.
553 * Note: when copying from the frontend, we expect a proper EOF mark per
554 * protocol; if the frontend simply drops the connection, we raise error.
555 * It seems unwise to allow the COPY IN to complete normally in that case.
557 * NB: no data conversion is applied here.
560 CopyGetData(CopyState cstate, void *databuf, int minread, int maxread)
564 switch (cstate->copy_dest)
567 bytesread = fread(databuf, 1, maxread, cstate->copy_file);
568 if (ferror(cstate->copy_file))
570 (errcode_for_file_access(),
571 errmsg("could not read from COPY file: %m")));
576 * We cannot read more than minread bytes (which in practice is 1)
577 * because old protocol doesn't have any clear way of separating
578 * the COPY stream from following data. This is slow, but not any
579 * slower than the code path was originally, and we don't care
580 * much anymore about the performance of old protocol.
582 if (pq_getbytes((char *) databuf, minread))
584 /* Only a \. terminator is legal EOF in old protocol */
586 (errcode(ERRCODE_CONNECTION_FAILURE),
587 errmsg("unexpected EOF on client connection with an open transaction")));
592 while (maxread > 0 && bytesread < minread && !cstate->fe_eof)
596 while (cstate->fe_msgbuf->cursor >= cstate->fe_msgbuf->len)
598 /* Try to receive another message */
602 mtype = pq_getbyte();
605 (errcode(ERRCODE_CONNECTION_FAILURE),
606 errmsg("unexpected EOF on client connection with an open transaction")));
607 if (pq_getmessage(cstate->fe_msgbuf, 0))
609 (errcode(ERRCODE_CONNECTION_FAILURE),
610 errmsg("unexpected EOF on client connection with an open transaction")));
613 case 'd': /* CopyData */
615 case 'c': /* CopyDone */
616 /* COPY IN correctly terminated by frontend */
617 cstate->fe_eof = true;
619 case 'f': /* CopyFail */
621 (errcode(ERRCODE_QUERY_CANCELED),
622 errmsg("COPY from stdin failed: %s",
623 pq_getmsgstring(cstate->fe_msgbuf))));
625 case 'H': /* Flush */
629 * Ignore Flush/Sync for the convenience of client
630 * libraries (such as libpq) that may send those
631 * without noticing that the command they just
637 (errcode(ERRCODE_PROTOCOL_VIOLATION),
638 errmsg("unexpected message type 0x%02X during COPY from stdin",
643 avail = cstate->fe_msgbuf->len - cstate->fe_msgbuf->cursor;
646 pq_copymsgbytes(cstate->fe_msgbuf, databuf, avail);
647 databuf = (void *) ((char *) databuf + avail);
659 * These functions do apply some data conversion
663 * CopySendInt32 sends an int32 in network byte order
666 CopySendInt32(CopyState cstate, int32 val)
670 buf = htonl((uint32) val);
671 CopySendData(cstate, &buf, sizeof(buf));
675 * CopyGetInt32 reads an int32 that appears in network byte order
677 * Returns true if OK, false if EOF
680 CopyGetInt32(CopyState cstate, int32 *val)
684 if (CopyGetData(cstate, &buf, sizeof(buf), sizeof(buf)) != sizeof(buf))
686 *val = 0; /* suppress compiler warning */
689 *val = (int32) ntohl(buf);
694 * CopySendInt16 sends an int16 in network byte order
697 CopySendInt16(CopyState cstate, int16 val)
701 buf = htons((uint16) val);
702 CopySendData(cstate, &buf, sizeof(buf));
706 * CopyGetInt16 reads an int16 that appears in network byte order
709 CopyGetInt16(CopyState cstate, int16 *val)
713 if (CopyGetData(cstate, &buf, sizeof(buf), sizeof(buf)) != sizeof(buf))
715 *val = 0; /* suppress compiler warning */
718 *val = (int16) ntohs(buf);
724 * CopyLoadRawBuf loads some more data into raw_buf
726 * Returns TRUE if able to obtain at least one more byte, else FALSE.
728 * If raw_buf_index < raw_buf_len, the unprocessed bytes are transferred
729 * down to the start of the buffer and then we load more data after that.
730 * This case is used only when a frontend multibyte character crosses a
731 * bufferload boundary.
734 CopyLoadRawBuf(CopyState cstate)
739 if (cstate->raw_buf_index < cstate->raw_buf_len)
741 /* Copy down the unprocessed data */
742 nbytes = cstate->raw_buf_len - cstate->raw_buf_index;
743 memmove(cstate->raw_buf, cstate->raw_buf + cstate->raw_buf_index,
747 nbytes = 0; /* no data need be saved */
749 inbytes = CopyGetData(cstate, cstate->raw_buf + nbytes,
750 1, RAW_BUF_SIZE - nbytes);
752 cstate->raw_buf[nbytes] = '\0';
753 cstate->raw_buf_index = 0;
754 cstate->raw_buf_len = nbytes;
755 return (inbytes > 0);
760 * DoCopy executes the SQL COPY statement
762 * Either unload or reload contents of table <relation>, depending on <from>.
763 * (<from> = TRUE means we are inserting into the table.) In the "TO" case
764 * we also support copying the output of an arbitrary SELECT query.
766 * If <pipe> is false, transfer is between the table and the file named
767 * <filename>. Otherwise, transfer is between the table and our regular
768 * input/output stream. The latter could be either stdin/stdout or a
769 * socket, depending on whether we're running under Postmaster control.
771 * Do not allow a Postgres user without superuser privilege to read from
772 * or write to a file.
774 * Do not allow the copy if user doesn't have proper permission to access
775 * the table or the specifically requested columns.
778 DoCopy(const CopyStmt *stmt, const char *queryString, uint64 *processed)
781 bool is_from = stmt->is_from;
782 bool pipe = (stmt->filename == NULL);
786 /* Disallow COPY to/from file or program except to superusers. */
787 if (!pipe && !superuser())
789 if (stmt->is_program)
791 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
792 errmsg("must be superuser to COPY to or from an external program"),
793 errhint("Anyone can COPY to stdout or from stdin. "
794 "psql's \\copy command also works for anyone.")));
797 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
798 errmsg("must be superuser to COPY to or from a file"),
799 errhint("Anyone can COPY to stdout or from stdin. "
800 "psql's \\copy command also works for anyone.")));
806 AclMode required_access = (is_from ? ACL_INSERT : ACL_SELECT);
811 Assert(!stmt->query);
813 /* Open and lock the relation, using the appropriate lock type. */
814 rel = heap_openrv(stmt->relation,
815 (is_from ? RowExclusiveLock : AccessShareLock));
817 relid = RelationGetRelid(rel);
819 rte = makeNode(RangeTblEntry);
820 rte->rtekind = RTE_RELATION;
821 rte->relid = RelationGetRelid(rel);
822 rte->relkind = rel->rd_rel->relkind;
823 rte->requiredPerms = required_access;
825 tupDesc = RelationGetDescr(rel);
826 attnums = CopyGetAttnums(tupDesc, rel, stmt->attlist);
827 foreach(cur, attnums)
829 int attno = lfirst_int(cur) -
830 FirstLowInvalidHeapAttributeNumber;
833 rte->modifiedCols = bms_add_member(rte->modifiedCols, attno);
835 rte->selectedCols = bms_add_member(rte->selectedCols, attno);
837 ExecCheckRTPerms(list_make1(rte), true);
851 /* check read-only transaction */
852 if (XactReadOnly && !rel->rd_islocaltemp)
853 PreventCommandIfReadOnly("COPY FROM");
855 cstate = BeginCopyFrom(rel, stmt->filename, stmt->is_program,
856 stmt->attlist, stmt->options);
857 *processed = CopyFrom(cstate); /* copy from file to database */
862 cstate = BeginCopyTo(rel, stmt->query, queryString,
863 stmt->filename, stmt->is_program,
864 stmt->attlist, stmt->options);
865 *processed = DoCopyTo(cstate); /* copy from database to file */
870 * Close the relation. If reading, we can release the AccessShareLock we
871 * got; if writing, we should hold the lock until end of transaction to
872 * ensure that updates will be committed before lock is released.
875 heap_close(rel, (is_from ? NoLock : AccessShareLock));
881 * Process the statement option list for COPY.
883 * Scan the options list (a list of DefElem) and transpose the information
884 * into cstate, applying appropriate error checking.
886 * cstate is assumed to be filled with zeroes initially.
888 * This is exported so that external users of the COPY API can sanity-check
889 * a list of options. In that usage, cstate should be passed as NULL
890 * (since external users don't know sizeof(CopyStateData)) and the collected
891 * data is just leaked until CurrentMemoryContext is reset.
893 * Note that additional checking, such as whether column names listed in FORCE
894 * QUOTE actually exist, has to be applied later. This just checks for
895 * self-consistency of the options list.
898 ProcessCopyOptions(CopyState cstate,
902 bool format_specified = false;
905 /* Support external use for option sanity checking */
907 cstate = (CopyStateData *) palloc0(sizeof(CopyStateData));
909 cstate->file_encoding = -1;
911 /* Extract options from the statement node tree */
912 foreach(option, options)
914 DefElem *defel = (DefElem *) lfirst(option);
916 if (strcmp(defel->defname, "format") == 0)
918 char *fmt = defGetString(defel);
920 if (format_specified)
922 (errcode(ERRCODE_SYNTAX_ERROR),
923 errmsg("conflicting or redundant options")));
924 format_specified = true;
925 if (strcmp(fmt, "text") == 0)
926 /* default format */ ;
927 else if (strcmp(fmt, "csv") == 0)
928 cstate->csv_mode = true;
929 else if (strcmp(fmt, "binary") == 0)
930 cstate->binary = true;
933 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
934 errmsg("COPY format \"%s\" not recognized", fmt)));
936 else if (strcmp(defel->defname, "oids") == 0)
940 (errcode(ERRCODE_SYNTAX_ERROR),
941 errmsg("conflicting or redundant options")));
942 cstate->oids = defGetBoolean(defel);
944 else if (strcmp(defel->defname, "freeze") == 0)
948 (errcode(ERRCODE_SYNTAX_ERROR),
949 errmsg("conflicting or redundant options")));
950 cstate->freeze = defGetBoolean(defel);
952 else if (strcmp(defel->defname, "delimiter") == 0)
956 (errcode(ERRCODE_SYNTAX_ERROR),
957 errmsg("conflicting or redundant options")));
958 cstate->delim = defGetString(defel);
960 else if (strcmp(defel->defname, "null") == 0)
962 if (cstate->null_print)
964 (errcode(ERRCODE_SYNTAX_ERROR),
965 errmsg("conflicting or redundant options")));
966 cstate->null_print = defGetString(defel);
968 else if (strcmp(defel->defname, "header") == 0)
970 if (cstate->header_line)
972 (errcode(ERRCODE_SYNTAX_ERROR),
973 errmsg("conflicting or redundant options")));
974 cstate->header_line = defGetBoolean(defel);
976 else if (strcmp(defel->defname, "quote") == 0)
980 (errcode(ERRCODE_SYNTAX_ERROR),
981 errmsg("conflicting or redundant options")));
982 cstate->quote = defGetString(defel);
984 else if (strcmp(defel->defname, "escape") == 0)
988 (errcode(ERRCODE_SYNTAX_ERROR),
989 errmsg("conflicting or redundant options")));
990 cstate->escape = defGetString(defel);
992 else if (strcmp(defel->defname, "force_quote") == 0)
994 if (cstate->force_quote || cstate->force_quote_all)
996 (errcode(ERRCODE_SYNTAX_ERROR),
997 errmsg("conflicting or redundant options")));
998 if (defel->arg && IsA(defel->arg, A_Star))
999 cstate->force_quote_all = true;
1000 else if (defel->arg && IsA(defel->arg, List))
1001 cstate->force_quote = (List *) defel->arg;
1004 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1005 errmsg("argument to option \"%s\" must be a list of column names",
1008 else if (strcmp(defel->defname, "force_not_null") == 0)
1010 if (cstate->force_notnull)
1012 (errcode(ERRCODE_SYNTAX_ERROR),
1013 errmsg("conflicting or redundant options")));
1014 if (defel->arg && IsA(defel->arg, List))
1015 cstate->force_notnull = (List *) defel->arg;
1018 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1019 errmsg("argument to option \"%s\" must be a list of column names",
1022 else if (strcmp(defel->defname, "convert_selectively") == 0)
1025 * Undocumented, not-accessible-from-SQL option: convert only the
1026 * named columns to binary form, storing the rest as NULLs. It's
1027 * allowed for the column list to be NIL.
1029 if (cstate->convert_selectively)
1031 (errcode(ERRCODE_SYNTAX_ERROR),
1032 errmsg("conflicting or redundant options")));
1033 cstate->convert_selectively = true;
1034 if (defel->arg == NULL || IsA(defel->arg, List))
1035 cstate->convert_select = (List *) defel->arg;
1038 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1039 errmsg("argument to option \"%s\" must be a list of column names",
1042 else if (strcmp(defel->defname, "encoding") == 0)
1044 if (cstate->file_encoding >= 0)
1046 (errcode(ERRCODE_SYNTAX_ERROR),
1047 errmsg("conflicting or redundant options")));
1048 cstate->file_encoding = pg_char_to_encoding(defGetString(defel));
1049 if (cstate->file_encoding < 0)
1051 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1052 errmsg("argument to option \"%s\" must be a valid encoding name",
1057 (errcode(ERRCODE_SYNTAX_ERROR),
1058 errmsg("option \"%s\" not recognized",
1063 * Check for incompatible options (must do these two before inserting
1066 if (cstate->binary && cstate->delim)
1068 (errcode(ERRCODE_SYNTAX_ERROR),
1069 errmsg("cannot specify DELIMITER in BINARY mode")));
1071 if (cstate->binary && cstate->null_print)
1073 (errcode(ERRCODE_SYNTAX_ERROR),
1074 errmsg("cannot specify NULL in BINARY mode")));
1076 /* Set defaults for omitted options */
1078 cstate->delim = cstate->csv_mode ? "," : "\t";
1080 if (!cstate->null_print)
1081 cstate->null_print = cstate->csv_mode ? "" : "\\N";
1082 cstate->null_print_len = strlen(cstate->null_print);
1084 if (cstate->csv_mode)
1087 cstate->quote = "\"";
1088 if (!cstate->escape)
1089 cstate->escape = cstate->quote;
1092 /* Only single-byte delimiter strings are supported. */
1093 if (strlen(cstate->delim) != 1)
1095 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1096 errmsg("COPY delimiter must be a single one-byte character")));
1098 /* Disallow end-of-line characters */
1099 if (strchr(cstate->delim, '\r') != NULL ||
1100 strchr(cstate->delim, '\n') != NULL)
1102 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1103 errmsg("COPY delimiter cannot be newline or carriage return")));
1105 if (strchr(cstate->null_print, '\r') != NULL ||
1106 strchr(cstate->null_print, '\n') != NULL)
1108 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1109 errmsg("COPY null representation cannot use newline or carriage return")));
1112 * Disallow unsafe delimiter characters in non-CSV mode. We can't allow
1113 * backslash because it would be ambiguous. We can't allow the other
1114 * cases because data characters matching the delimiter must be
1115 * backslashed, and certain backslash combinations are interpreted
1116 * non-literally by COPY IN. Disallowing all lower case ASCII letters is
1117 * more than strictly necessary, but seems best for consistency and
1118 * future-proofing. Likewise we disallow all digits though only octal
1119 * digits are actually dangerous.
1121 if (!cstate->csv_mode &&
1122 strchr("\\.abcdefghijklmnopqrstuvwxyz0123456789",
1123 cstate->delim[0]) != NULL)
1125 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1126 errmsg("COPY delimiter cannot be \"%s\"", cstate->delim)));
1129 if (!cstate->csv_mode && cstate->header_line)
1131 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1132 errmsg("COPY HEADER available only in CSV mode")));
1135 if (!cstate->csv_mode && cstate->quote != NULL)
1137 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1138 errmsg("COPY quote available only in CSV mode")));
1140 if (cstate->csv_mode && strlen(cstate->quote) != 1)
1142 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1143 errmsg("COPY quote must be a single one-byte character")));
1145 if (cstate->csv_mode && cstate->delim[0] == cstate->quote[0])
1147 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1148 errmsg("COPY delimiter and quote must be different")));
1151 if (!cstate->csv_mode && cstate->escape != NULL)
1153 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1154 errmsg("COPY escape available only in CSV mode")));
1156 if (cstate->csv_mode && strlen(cstate->escape) != 1)
1158 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1159 errmsg("COPY escape must be a single one-byte character")));
1161 /* Check force_quote */
1162 if (!cstate->csv_mode && (cstate->force_quote || cstate->force_quote_all))
1164 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1165 errmsg("COPY force quote available only in CSV mode")));
1166 if ((cstate->force_quote || cstate->force_quote_all) && is_from)
1168 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1169 errmsg("COPY force quote only available using COPY TO")));
1171 /* Check force_notnull */
1172 if (!cstate->csv_mode && cstate->force_notnull != NIL)
1174 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1175 errmsg("COPY force not null available only in CSV mode")));
1176 if (cstate->force_notnull != NIL && !is_from)
1178 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1179 errmsg("COPY force not null only available using COPY FROM")));
1181 /* Don't allow the delimiter to appear in the null string. */
1182 if (strchr(cstate->null_print, cstate->delim[0]) != NULL)
1184 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1185 errmsg("COPY delimiter must not appear in the NULL specification")));
1187 /* Don't allow the CSV quote char to appear in the null string. */
1188 if (cstate->csv_mode &&
1189 strchr(cstate->null_print, cstate->quote[0]) != NULL)
1191 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1192 errmsg("CSV quote character must not appear in the NULL specification")));
1196 * Common setup routines used by BeginCopyFrom and BeginCopyTo.
1198 * Iff <binary>, unload or reload in the binary format, as opposed to the
1199 * more wasteful but more robust and portable text format.
1201 * Iff <oids>, unload or reload the format that includes OID information.
1202 * On input, we accept OIDs whether or not the table has an OID column,
1203 * but silently drop them if it does not. On output, we report an error
1204 * if the user asks for OIDs in a table that has none (not providing an
1205 * OID column might seem friendlier, but could seriously confuse programs).
1207 * If in the text format, delimit columns with delimiter <delim> and print
1208 * NULL values as <null_print>.
1211 BeginCopy(bool is_from,
1214 const char *queryString,
1221 MemoryContext oldcontext;
1223 /* Allocate workspace and zero all fields */
1224 cstate = (CopyStateData *) palloc0(sizeof(CopyStateData));
1227 * We allocate everything used by a cstate in a new memory context. This
1228 * avoids memory leaks during repeated use of COPY in a query.
1230 cstate->copycontext = AllocSetContextCreate(CurrentMemoryContext,
1232 ALLOCSET_DEFAULT_MINSIZE,
1233 ALLOCSET_DEFAULT_INITSIZE,
1234 ALLOCSET_DEFAULT_MAXSIZE);
1236 oldcontext = MemoryContextSwitchTo(cstate->copycontext);
1238 /* Extract options from the statement node tree */
1239 ProcessCopyOptions(cstate, is_from, options);
1241 /* Process the source/target relation or query */
1248 tupDesc = RelationGetDescr(cstate->rel);
1250 /* Don't allow COPY w/ OIDs to or from a table without them */
1251 if (cstate->oids && !cstate->rel->rd_rel->relhasoids)
1253 (errcode(ERRCODE_UNDEFINED_COLUMN),
1254 errmsg("table \"%s\" does not have OIDs",
1255 RelationGetRelationName(cstate->rel))));
1267 /* Don't allow COPY w/ OIDs from a select */
1270 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1271 errmsg("COPY (SELECT) WITH OIDS is not supported")));
1274 * Run parse analysis and rewrite. Note this also acquires sufficient
1275 * locks on the source table(s).
1277 * Because the parser and planner tend to scribble on their input, we
1278 * make a preliminary copy of the source querytree. This prevents
1279 * problems in the case that the COPY is in a portal or plpgsql
1280 * function and is executed repeatedly. (See also the same hack in
1281 * DECLARE CURSOR and PREPARE.) XXX FIXME someday.
1283 rewritten = pg_analyze_and_rewrite((Node *) copyObject(raw_query),
1284 queryString, NULL, 0);
1286 /* We don't expect more or less than one result query */
1287 if (list_length(rewritten) != 1)
1288 elog(ERROR, "unexpected rewrite result");
1290 query = (Query *) linitial(rewritten);
1292 /* The grammar allows SELECT INTO, but we don't support that */
1293 if (query->utilityStmt != NULL &&
1294 IsA(query->utilityStmt, CreateTableAsStmt))
1296 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1297 errmsg("COPY (SELECT INTO) is not supported")));
1299 Assert(query->commandType == CMD_SELECT);
1300 Assert(query->utilityStmt == NULL);
1302 /* plan the query */
1303 plan = planner(query, 0, NULL);
1306 * Use a snapshot with an updated command ID to ensure this query sees
1307 * results of any previously executed queries.
1309 PushCopiedSnapshot(GetActiveSnapshot());
1310 UpdateActiveSnapshotCommandId();
1312 /* Create dest receiver for COPY OUT */
1313 dest = CreateDestReceiver(DestCopyOut);
1314 ((DR_copy *) dest)->cstate = cstate;
1316 /* Create a QueryDesc requesting no output */
1317 cstate->queryDesc = CreateQueryDesc(plan, queryString,
1318 GetActiveSnapshot(),
1323 * Call ExecutorStart to prepare the plan for execution.
1325 * ExecutorStart computes a result tupdesc for us
1327 ExecutorStart(cstate->queryDesc, 0);
1329 tupDesc = cstate->queryDesc->tupDesc;
1332 /* Generate or convert list of attributes to process */
1333 cstate->attnumlist = CopyGetAttnums(tupDesc, cstate->rel, attnamelist);
1335 num_phys_attrs = tupDesc->natts;
1337 /* Convert FORCE QUOTE name list to per-column flags, check validity */
1338 cstate->force_quote_flags = (bool *) palloc0(num_phys_attrs * sizeof(bool));
1339 if (cstate->force_quote_all)
1343 for (i = 0; i < num_phys_attrs; i++)
1344 cstate->force_quote_flags[i] = true;
1346 else if (cstate->force_quote)
1351 attnums = CopyGetAttnums(tupDesc, cstate->rel, cstate->force_quote);
1353 foreach(cur, attnums)
1355 int attnum = lfirst_int(cur);
1357 if (!list_member_int(cstate->attnumlist, attnum))
1359 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1360 errmsg("FORCE QUOTE column \"%s\" not referenced by COPY",
1361 NameStr(tupDesc->attrs[attnum - 1]->attname))));
1362 cstate->force_quote_flags[attnum - 1] = true;
1366 /* Convert FORCE NOT NULL name list to per-column flags, check validity */
1367 cstate->force_notnull_flags = (bool *) palloc0(num_phys_attrs * sizeof(bool));
1368 if (cstate->force_notnull)
1373 attnums = CopyGetAttnums(tupDesc, cstate->rel, cstate->force_notnull);
1375 foreach(cur, attnums)
1377 int attnum = lfirst_int(cur);
1379 if (!list_member_int(cstate->attnumlist, attnum))
1381 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1382 errmsg("FORCE NOT NULL column \"%s\" not referenced by COPY",
1383 NameStr(tupDesc->attrs[attnum - 1]->attname))));
1384 cstate->force_notnull_flags[attnum - 1] = true;
1388 /* Convert convert_selectively name list to per-column flags */
1389 if (cstate->convert_selectively)
1394 cstate->convert_select_flags = (bool *) palloc0(num_phys_attrs * sizeof(bool));
1396 attnums = CopyGetAttnums(tupDesc, cstate->rel, cstate->convert_select);
1398 foreach(cur, attnums)
1400 int attnum = lfirst_int(cur);
1402 if (!list_member_int(cstate->attnumlist, attnum))
1404 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1405 errmsg_internal("selected column \"%s\" not referenced by COPY",
1406 NameStr(tupDesc->attrs[attnum - 1]->attname))));
1407 cstate->convert_select_flags[attnum - 1] = true;
1411 /* Use client encoding when ENCODING option is not specified. */
1412 if (cstate->file_encoding < 0)
1413 cstate->file_encoding = pg_get_client_encoding();
1416 * Set up encoding conversion info. Even if the file and server encodings
1417 * are the same, we must apply pg_any_to_server() to validate data in
1418 * multibyte encodings.
1420 cstate->need_transcoding =
1421 (cstate->file_encoding != GetDatabaseEncoding() ||
1422 pg_database_encoding_max_length() > 1);
1423 /* See Multibyte encoding comment above */
1424 cstate->encoding_embeds_ascii = PG_ENCODING_IS_CLIENT_ONLY(cstate->file_encoding);
1426 cstate->copy_dest = COPY_FILE; /* default */
1428 MemoryContextSwitchTo(oldcontext);
1434 * Closes the pipe to an external program, checking the pclose() return code.
1437 ClosePipeToProgram(CopyState cstate)
1441 Assert(cstate->is_program);
1443 pclose_rc = ClosePipeStream(cstate->copy_file);
1444 if (pclose_rc == -1)
1446 (errmsg("could not close pipe to external command: %m")));
1447 else if (pclose_rc != 0)
1449 (errmsg("program \"%s\" failed",
1451 errdetail_internal("%s", wait_result_to_str(pclose_rc))));
1455 * Release resources allocated in a cstate for COPY TO/FROM.
1458 EndCopy(CopyState cstate)
1460 if (cstate->is_program)
1462 ClosePipeToProgram(cstate);
1466 if (cstate->filename != NULL && FreeFile(cstate->copy_file))
1468 (errcode_for_file_access(),
1469 errmsg("could not close file \"%s\": %m",
1470 cstate->filename)));
1473 MemoryContextDelete(cstate->copycontext);
1478 * Setup CopyState to read tuples from a table or a query for COPY TO.
1481 BeginCopyTo(Relation rel,
1483 const char *queryString,
1484 const char *filename,
1490 bool pipe = (filename == NULL);
1491 MemoryContext oldcontext;
1493 if (rel != NULL && rel->rd_rel->relkind != RELKIND_RELATION)
1495 if (rel->rd_rel->relkind == RELKIND_VIEW)
1497 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1498 errmsg("cannot copy from view \"%s\"",
1499 RelationGetRelationName(rel)),
1500 errhint("Try the COPY (SELECT ...) TO variant.")));
1501 else if (rel->rd_rel->relkind == RELKIND_MATVIEW)
1503 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1504 errmsg("cannot copy from materialized view \"%s\"",
1505 RelationGetRelationName(rel)),
1506 errhint("Try the COPY (SELECT ...) TO variant.")));
1507 else if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1509 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1510 errmsg("cannot copy from foreign table \"%s\"",
1511 RelationGetRelationName(rel)),
1512 errhint("Try the COPY (SELECT ...) TO variant.")));
1513 else if (rel->rd_rel->relkind == RELKIND_SEQUENCE)
1515 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1516 errmsg("cannot copy from sequence \"%s\"",
1517 RelationGetRelationName(rel))));
1520 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1521 errmsg("cannot copy from non-table relation \"%s\"",
1522 RelationGetRelationName(rel))));
1525 cstate = BeginCopy(false, rel, query, queryString, attnamelist, options);
1526 oldcontext = MemoryContextSwitchTo(cstate->copycontext);
1530 Assert(!is_program); /* the grammar does not allow this */
1531 if (whereToSendOutput != DestRemote)
1532 cstate->copy_file = stdout;
1536 cstate->filename = pstrdup(filename);
1537 cstate->is_program = is_program;
1541 cstate->copy_file = OpenPipeStream(cstate->filename, PG_BINARY_W);
1542 if (cstate->copy_file == NULL)
1544 (errmsg("could not execute command \"%s\": %m",
1545 cstate->filename)));
1549 mode_t oumask; /* Pre-existing umask value */
1553 * Prevent write to relative path ... too easy to shoot oneself in
1554 * the foot by overwriting a database file ...
1556 if (!is_absolute_path(filename))
1558 (errcode(ERRCODE_INVALID_NAME),
1559 errmsg("relative path not allowed for COPY to file")));
1561 oumask = umask(S_IWGRP | S_IWOTH);
1562 cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_W);
1564 if (cstate->copy_file == NULL)
1566 (errcode_for_file_access(),
1567 errmsg("could not open file \"%s\" for writing: %m",
1568 cstate->filename)));
1570 fstat(fileno(cstate->copy_file), &st);
1571 if (S_ISDIR(st.st_mode))
1573 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1574 errmsg("\"%s\" is a directory", cstate->filename)));
1578 MemoryContextSwitchTo(oldcontext);
1584 * This intermediate routine exists mainly to localize the effects of setjmp
1585 * so we don't need to plaster a lot of variables with "volatile".
1588 DoCopyTo(CopyState cstate)
1590 bool pipe = (cstate->filename == NULL);
1591 bool fe_copy = (pipe && whereToSendOutput == DestRemote);
1597 SendCopyBegin(cstate);
1599 processed = CopyTo(cstate);
1602 SendCopyEnd(cstate);
1607 * Make sure we turn off old-style COPY OUT mode upon error. It is
1608 * okay to do this in all cases, since it does nothing if the mode is
1611 pq_endcopyout(true);
1620 * Clean up storage and release resources for COPY TO.
1623 EndCopyTo(CopyState cstate)
1625 if (cstate->queryDesc != NULL)
1627 /* Close down the query and free resources. */
1628 ExecutorFinish(cstate->queryDesc);
1629 ExecutorEnd(cstate->queryDesc);
1630 FreeQueryDesc(cstate->queryDesc);
1631 PopActiveSnapshot();
1634 /* Clean up storage */
1639 * Copy from relation or query TO file.
1642 CopyTo(CopyState cstate)
1646 Form_pg_attribute *attr;
1651 tupDesc = RelationGetDescr(cstate->rel);
1653 tupDesc = cstate->queryDesc->tupDesc;
1654 attr = tupDesc->attrs;
1655 num_phys_attrs = tupDesc->natts;
1656 cstate->null_print_client = cstate->null_print; /* default */
1658 /* We use fe_msgbuf as a per-row buffer regardless of copy_dest */
1659 cstate->fe_msgbuf = makeStringInfo();
1661 /* Get info about the columns we need to process. */
1662 cstate->out_functions = (FmgrInfo *) palloc(num_phys_attrs * sizeof(FmgrInfo));
1663 foreach(cur, cstate->attnumlist)
1665 int attnum = lfirst_int(cur);
1670 getTypeBinaryOutputInfo(attr[attnum - 1]->atttypid,
1674 getTypeOutputInfo(attr[attnum - 1]->atttypid,
1677 fmgr_info(out_func_oid, &cstate->out_functions[attnum - 1]);
1681 * Create a temporary memory context that we can reset once per row to
1682 * recover palloc'd memory. This avoids any problems with leaks inside
1683 * datatype output routines, and should be faster than retail pfree's
1684 * anyway. (We don't need a whole econtext as CopyFrom does.)
1686 cstate->rowcontext = AllocSetContextCreate(CurrentMemoryContext,
1688 ALLOCSET_DEFAULT_MINSIZE,
1689 ALLOCSET_DEFAULT_INITSIZE,
1690 ALLOCSET_DEFAULT_MAXSIZE);
1694 /* Generate header for a binary copy */
1698 CopySendData(cstate, BinarySignature, 11);
1703 CopySendInt32(cstate, tmp);
1704 /* No header extension */
1706 CopySendInt32(cstate, tmp);
1711 * For non-binary copy, we need to convert null_print to file
1712 * encoding, because it will be sent directly with CopySendString.
1714 if (cstate->need_transcoding)
1715 cstate->null_print_client = pg_server_to_any(cstate->null_print,
1716 cstate->null_print_len,
1717 cstate->file_encoding);
1719 /* if a header has been requested send the line */
1720 if (cstate->header_line)
1722 bool hdr_delim = false;
1724 foreach(cur, cstate->attnumlist)
1726 int attnum = lfirst_int(cur);
1730 CopySendChar(cstate, cstate->delim[0]);
1733 colname = NameStr(attr[attnum - 1]->attname);
1735 CopyAttributeOutCSV(cstate, colname, false,
1736 list_length(cstate->attnumlist) == 1);
1739 CopySendEndOfRow(cstate);
1747 HeapScanDesc scandesc;
1750 values = (Datum *) palloc(num_phys_attrs * sizeof(Datum));
1751 nulls = (bool *) palloc(num_phys_attrs * sizeof(bool));
1753 scandesc = heap_beginscan(cstate->rel, GetActiveSnapshot(), 0, NULL);
1756 while ((tuple = heap_getnext(scandesc, ForwardScanDirection)) != NULL)
1758 CHECK_FOR_INTERRUPTS();
1760 /* Deconstruct the tuple ... faster than repeated heap_getattr */
1761 heap_deform_tuple(tuple, tupDesc, values, nulls);
1763 /* Format and send the data */
1764 CopyOneRowTo(cstate, HeapTupleGetOid(tuple), values, nulls);
1768 heap_endscan(scandesc);
1775 /* run the plan --- the dest receiver will send tuples */
1776 ExecutorRun(cstate->queryDesc, ForwardScanDirection, 0L);
1777 processed = ((DR_copy *) cstate->queryDesc->dest)->processed;
1782 /* Generate trailer for a binary copy */
1783 CopySendInt16(cstate, -1);
1784 /* Need to flush out the trailer */
1785 CopySendEndOfRow(cstate);
1788 MemoryContextDelete(cstate->rowcontext);
1794 * Emit one row during CopyTo().
1797 CopyOneRowTo(CopyState cstate, Oid tupleOid, Datum *values, bool *nulls)
1799 bool need_delim = false;
1800 FmgrInfo *out_functions = cstate->out_functions;
1801 MemoryContext oldcontext;
1805 MemoryContextReset(cstate->rowcontext);
1806 oldcontext = MemoryContextSwitchTo(cstate->rowcontext);
1810 /* Binary per-tuple header */
1811 CopySendInt16(cstate, list_length(cstate->attnumlist));
1812 /* Send OID if wanted --- note attnumlist doesn't include it */
1815 /* Hack --- assume Oid is same size as int32 */
1816 CopySendInt32(cstate, sizeof(int32));
1817 CopySendInt32(cstate, tupleOid);
1822 /* Text format has no per-tuple header, but send OID if wanted */
1823 /* Assume digits don't need any quoting or encoding conversion */
1826 string = DatumGetCString(DirectFunctionCall1(oidout,
1827 ObjectIdGetDatum(tupleOid)));
1828 CopySendString(cstate, string);
1833 foreach(cur, cstate->attnumlist)
1835 int attnum = lfirst_int(cur);
1836 Datum value = values[attnum - 1];
1837 bool isnull = nulls[attnum - 1];
1839 if (!cstate->binary)
1842 CopySendChar(cstate, cstate->delim[0]);
1848 if (!cstate->binary)
1849 CopySendString(cstate, cstate->null_print_client);
1851 CopySendInt32(cstate, -1);
1855 if (!cstate->binary)
1857 string = OutputFunctionCall(&out_functions[attnum - 1],
1859 if (cstate->csv_mode)
1860 CopyAttributeOutCSV(cstate, string,
1861 cstate->force_quote_flags[attnum - 1],
1862 list_length(cstate->attnumlist) == 1);
1864 CopyAttributeOutText(cstate, string);
1870 outputbytes = SendFunctionCall(&out_functions[attnum - 1],
1872 CopySendInt32(cstate, VARSIZE(outputbytes) - VARHDRSZ);
1873 CopySendData(cstate, VARDATA(outputbytes),
1874 VARSIZE(outputbytes) - VARHDRSZ);
1879 CopySendEndOfRow(cstate);
1881 MemoryContextSwitchTo(oldcontext);
1886 * error context callback for COPY FROM
1888 * The argument for the error context must be CopyState.
1891 CopyFromErrorCallback(void *arg)
1893 CopyState cstate = (CopyState) arg;
1897 /* can't usefully display the data */
1898 if (cstate->cur_attname)
1899 errcontext("COPY %s, line %d, column %s",
1900 cstate->cur_relname, cstate->cur_lineno,
1901 cstate->cur_attname);
1903 errcontext("COPY %s, line %d",
1904 cstate->cur_relname, cstate->cur_lineno);
1908 if (cstate->cur_attname && cstate->cur_attval)
1910 /* error is relevant to a particular column */
1913 attval = limit_printout_length(cstate->cur_attval);
1914 errcontext("COPY %s, line %d, column %s: \"%s\"",
1915 cstate->cur_relname, cstate->cur_lineno,
1916 cstate->cur_attname, attval);
1919 else if (cstate->cur_attname)
1921 /* error is relevant to a particular column, value is NULL */
1922 errcontext("COPY %s, line %d, column %s: null input",
1923 cstate->cur_relname, cstate->cur_lineno,
1924 cstate->cur_attname);
1929 * Error is relevant to a particular line.
1931 * If line_buf still contains the correct line, and it's already
1932 * transcoded, print it. If it's still in a foreign encoding, it's
1933 * quite likely that the error is precisely a failure to do
1934 * encoding conversion (ie, bad data). We dare not try to convert
1935 * it, and at present there's no way to regurgitate it without
1936 * conversion. So we have to punt and just report the line number.
1938 if (cstate->line_buf_valid &&
1939 (cstate->line_buf_converted || !cstate->need_transcoding))
1943 lineval = limit_printout_length(cstate->line_buf.data);
1944 errcontext("COPY %s, line %d: \"%s\"",
1945 cstate->cur_relname, cstate->cur_lineno, lineval);
1950 errcontext("COPY %s, line %d",
1951 cstate->cur_relname, cstate->cur_lineno);
1958 * Make sure we don't print an unreasonable amount of COPY data in a message.
1960 * It would seem a lot easier to just use the sprintf "precision" limit to
1961 * truncate the string. However, some versions of glibc have a bug/misfeature
1962 * that vsnprintf will always fail (return -1) if it is asked to truncate
1963 * a string that contains invalid byte sequences for the current encoding.
1964 * So, do our own truncation. We return a pstrdup'd copy of the input.
1967 limit_printout_length(const char *str)
1969 #define MAX_COPY_DATA_DISPLAY 100
1971 int slen = strlen(str);
1975 /* Fast path if definitely okay */
1976 if (slen <= MAX_COPY_DATA_DISPLAY)
1977 return pstrdup(str);
1979 /* Apply encoding-dependent truncation */
1980 len = pg_mbcliplen(str, slen, MAX_COPY_DATA_DISPLAY);
1983 * Truncate, and add "..." to show we truncated the input.
1985 res = (char *) palloc(len + 4);
1986 memcpy(res, str, len);
1987 strcpy(res + len, "...");
1993 * Copy FROM file to relation.
1996 CopyFrom(CopyState cstate)
2002 ResultRelInfo *resultRelInfo;
2003 EState *estate = CreateExecutorState(); /* for ExecConstraints() */
2004 ExprContext *econtext;
2005 TupleTableSlot *myslot;
2006 MemoryContext oldcontext = CurrentMemoryContext;
2008 ErrorContextCallback errcallback;
2009 CommandId mycid = GetCurrentCommandId(true);
2010 int hi_options = 0; /* start with default heap_insert options */
2011 BulkInsertState bistate;
2012 uint64 processed = 0;
2013 bool useHeapMultiInsert;
2014 int nBufferedTuples = 0;
2016 #define MAX_BUFFERED_TUPLES 1000
2017 HeapTuple *bufferedTuples = NULL; /* initialize to silence warning */
2018 Size bufferedTuplesSize = 0;
2019 int firstBufferedLineNo = 0;
2021 Assert(cstate->rel);
2023 if (cstate->rel->rd_rel->relkind != RELKIND_RELATION)
2025 if (cstate->rel->rd_rel->relkind == RELKIND_VIEW)
2027 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2028 errmsg("cannot copy to view \"%s\"",
2029 RelationGetRelationName(cstate->rel))));
2030 else if (cstate->rel->rd_rel->relkind == RELKIND_MATVIEW)
2032 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2033 errmsg("cannot copy to materialized view \"%s\"",
2034 RelationGetRelationName(cstate->rel))));
2035 else if (cstate->rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
2037 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2038 errmsg("cannot copy to foreign table \"%s\"",
2039 RelationGetRelationName(cstate->rel))));
2040 else if (cstate->rel->rd_rel->relkind == RELKIND_SEQUENCE)
2042 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2043 errmsg("cannot copy to sequence \"%s\"",
2044 RelationGetRelationName(cstate->rel))));
2047 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2048 errmsg("cannot copy to non-table relation \"%s\"",
2049 RelationGetRelationName(cstate->rel))));
2052 tupDesc = RelationGetDescr(cstate->rel);
2055 * Check to see if we can avoid writing WAL
2057 * If archive logging/streaming is not enabled *and* either
2058 * - table was created in same transaction as this COPY
2059 * - data is being written to relfilenode created in this transaction
2060 * then we can skip writing WAL. It's safe because if the transaction
2061 * doesn't commit, we'll discard the table (or the new relfilenode file).
2062 * If it does commit, we'll have done the heap_sync at the bottom of this
2065 * As mentioned in comments in utils/rel.h, the in-same-transaction test
2066 * is not always set correctly, since in rare cases rd_newRelfilenodeSubid
2067 * can be cleared before the end of the transaction. The exact case is
2068 * when a relation sets a new relfilenode twice in same transaction, yet
2069 * the second one fails in an aborted subtransaction, e.g.
2078 * Also, if the target file is new-in-transaction, we assume that checking
2079 * FSM for free space is a waste of time, even if we must use WAL because
2080 * of archiving. This could possibly be wrong, but it's unlikely.
2082 * The comments for heap_insert and RelationGetBufferForTuple specify that
2083 * skipping WAL logging is only safe if we ensure that our tuples do not
2084 * go into pages containing tuples from any other transactions --- but this
2085 * must be the case if we have a new table or new relfilenode, so we need
2086 * no additional work to enforce that.
2089 /* createSubid is creation check, newRelfilenodeSubid is truncation check */
2090 if (cstate->rel->rd_createSubid != InvalidSubTransactionId ||
2091 cstate->rel->rd_newRelfilenodeSubid != InvalidSubTransactionId)
2093 hi_options |= HEAP_INSERT_SKIP_FSM;
2094 if (!XLogIsNeeded())
2095 hi_options |= HEAP_INSERT_SKIP_WAL;
2099 * Optimize if new relfilenode was created in this subxact or one of its
2100 * committed children and we won't see those rows later as part of an
2101 * earlier scan or command. This ensures that if this subtransaction
2102 * aborts then the frozen rows won't be visible after xact cleanup. Note
2103 * that the stronger test of exactly which subtransaction created it is
2104 * crucial for correctness of this optimisation.
2108 if (!ThereAreNoPriorRegisteredSnapshots() || !ThereAreNoReadyPortals())
2110 (ERRCODE_INVALID_TRANSACTION_STATE,
2111 errmsg("cannot perform FREEZE because of prior transaction activity")));
2113 if (cstate->rel->rd_createSubid != GetCurrentSubTransactionId() &&
2114 cstate->rel->rd_newRelfilenodeSubid != GetCurrentSubTransactionId())
2116 (ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE,
2117 errmsg("cannot perform FREEZE because the table was not created or truncated in the current subtransaction")));
2119 hi_options |= HEAP_INSERT_FROZEN;
2123 * We need a ResultRelInfo so we can use the regular executor's
2124 * index-entry-making machinery. (There used to be a huge amount of code
2125 * here that basically duplicated execUtils.c ...)
2127 resultRelInfo = makeNode(ResultRelInfo);
2128 InitResultRelInfo(resultRelInfo,
2130 1, /* dummy rangetable index */
2133 ExecOpenIndices(resultRelInfo);
2135 estate->es_result_relations = resultRelInfo;
2136 estate->es_num_result_relations = 1;
2137 estate->es_result_relation_info = resultRelInfo;
2139 /* Set up a tuple slot too */
2140 myslot = ExecInitExtraTupleSlot(estate);
2141 ExecSetSlotDescriptor(myslot, tupDesc);
2142 /* Triggers might need a slot as well */
2143 estate->es_trig_tuple_slot = ExecInitExtraTupleSlot(estate);
2146 * It's more efficient to prepare a bunch of tuples for insertion, and
2147 * insert them in one heap_multi_insert() call, than call heap_insert()
2148 * separately for every tuple. However, we can't do that if there are
2149 * BEFORE/INSTEAD OF triggers, or we need to evaluate volatile default
2150 * expressions. Such triggers or expressions might query the table we're
2151 * inserting to, and act differently if the tuples that have already been
2152 * processed and prepared for insertion are not there.
2154 if ((resultRelInfo->ri_TrigDesc != NULL &&
2155 (resultRelInfo->ri_TrigDesc->trig_insert_before_row ||
2156 resultRelInfo->ri_TrigDesc->trig_insert_instead_row)) ||
2157 cstate->volatile_defexprs)
2159 useHeapMultiInsert = false;
2163 useHeapMultiInsert = true;
2164 bufferedTuples = palloc(MAX_BUFFERED_TUPLES * sizeof(HeapTuple));
2167 /* Prepare to catch AFTER triggers. */
2168 AfterTriggerBeginQuery();
2171 * Check BEFORE STATEMENT insertion triggers. It's debatable whether we
2172 * should do this for COPY, since it's not really an "INSERT" statement as
2173 * such. However, executing these triggers maintains consistency with the
2174 * EACH ROW triggers that we already fire on COPY.
2176 ExecBSInsertTriggers(estate, resultRelInfo);
2178 values = (Datum *) palloc(tupDesc->natts * sizeof(Datum));
2179 nulls = (bool *) palloc(tupDesc->natts * sizeof(bool));
2181 bistate = GetBulkInsertState();
2182 econtext = GetPerTupleExprContext(estate);
2184 /* Set up callback to identify error line number */
2185 errcallback.callback = CopyFromErrorCallback;
2186 errcallback.arg = (void *) cstate;
2187 errcallback.previous = error_context_stack;
2188 error_context_stack = &errcallback;
2192 TupleTableSlot *slot;
2194 Oid loaded_oid = InvalidOid;
2196 CHECK_FOR_INTERRUPTS();
2198 if (nBufferedTuples == 0)
2201 * Reset the per-tuple exprcontext. We can only do this if the
2202 * tuple buffer is empty (calling the context the per-tuple memory
2203 * context is a bit of a misnomer now
2205 ResetPerTupleExprContext(estate);
2208 /* Switch into its memory context */
2209 MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
2211 if (!NextCopyFrom(cstate, econtext, values, nulls, &loaded_oid))
2214 /* And now we can form the input tuple. */
2215 tuple = heap_form_tuple(tupDesc, values, nulls);
2217 if (loaded_oid != InvalidOid)
2218 HeapTupleSetOid(tuple, loaded_oid);
2220 /* Triggers and stuff need to be invoked in query context. */
2221 MemoryContextSwitchTo(oldcontext);
2223 /* Place tuple in tuple slot --- but slot shouldn't free it */
2225 ExecStoreTuple(tuple, slot, InvalidBuffer, false);
2229 /* BEFORE ROW INSERT Triggers */
2230 if (resultRelInfo->ri_TrigDesc &&
2231 resultRelInfo->ri_TrigDesc->trig_insert_before_row)
2233 slot = ExecBRInsertTriggers(estate, resultRelInfo, slot);
2235 if (slot == NULL) /* "do nothing" */
2237 else /* trigger might have changed tuple */
2238 tuple = ExecMaterializeSlot(slot);
2243 /* Check the constraints of the tuple */
2244 if (cstate->rel->rd_att->constr)
2245 ExecConstraints(resultRelInfo, slot, estate);
2247 if (useHeapMultiInsert)
2249 /* Add this tuple to the tuple buffer */
2250 if (nBufferedTuples == 0)
2251 firstBufferedLineNo = cstate->cur_lineno;
2252 bufferedTuples[nBufferedTuples++] = tuple;
2253 bufferedTuplesSize += tuple->t_len;
2256 * If the buffer filled up, flush it. Also flush if the total
2257 * size of all the tuples in the buffer becomes large, to
2258 * avoid using large amounts of memory for the buffers when
2259 * the tuples are exceptionally wide.
2261 if (nBufferedTuples == MAX_BUFFERED_TUPLES ||
2262 bufferedTuplesSize > 65535)
2264 CopyFromInsertBatch(cstate, estate, mycid, hi_options,
2265 resultRelInfo, myslot, bistate,
2266 nBufferedTuples, bufferedTuples,
2267 firstBufferedLineNo);
2268 nBufferedTuples = 0;
2269 bufferedTuplesSize = 0;
2274 List *recheckIndexes = NIL;
2276 /* OK, store the tuple and create index entries for it */
2277 heap_insert(cstate->rel, tuple, mycid, hi_options, bistate);
2279 if (resultRelInfo->ri_NumIndices > 0)
2280 recheckIndexes = ExecInsertIndexTuples(slot, &(tuple->t_self),
2283 /* AFTER ROW INSERT Triggers */
2284 ExecARInsertTriggers(estate, resultRelInfo, tuple,
2287 list_free(recheckIndexes);
2291 * We count only tuples not suppressed by a BEFORE INSERT trigger;
2292 * this is the same definition used by execMain.c for counting
2293 * tuples inserted by an INSERT command.
2299 /* Flush any remaining buffered tuples */
2300 if (nBufferedTuples > 0)
2301 CopyFromInsertBatch(cstate, estate, mycid, hi_options,
2302 resultRelInfo, myslot, bistate,
2303 nBufferedTuples, bufferedTuples,
2304 firstBufferedLineNo);
2306 /* Done, clean up */
2307 error_context_stack = errcallback.previous;
2309 FreeBulkInsertState(bistate);
2311 MemoryContextSwitchTo(oldcontext);
2313 /* Execute AFTER STATEMENT insertion triggers */
2314 ExecASInsertTriggers(estate, resultRelInfo);
2316 /* Handle queued AFTER triggers */
2317 AfterTriggerEndQuery(estate);
2322 ExecResetTupleTable(estate->es_tupleTable, false);
2324 ExecCloseIndices(resultRelInfo);
2326 FreeExecutorState(estate);
2329 * If we skipped writing WAL, then we need to sync the heap (but not
2330 * indexes since those use WAL anyway)
2332 if (hi_options & HEAP_INSERT_SKIP_WAL)
2333 heap_sync(cstate->rel);
2339 * A subroutine of CopyFrom, to write the current batch of buffered heap
2340 * tuples to the heap. Also updates indexes and runs AFTER ROW INSERT
2344 CopyFromInsertBatch(CopyState cstate, EState *estate, CommandId mycid,
2345 int hi_options, ResultRelInfo *resultRelInfo,
2346 TupleTableSlot *myslot, BulkInsertState bistate,
2347 int nBufferedTuples, HeapTuple *bufferedTuples,
2348 int firstBufferedLineNo)
2350 MemoryContext oldcontext;
2352 int save_cur_lineno;
2355 * Print error context information correctly, if one of the operations
2358 cstate->line_buf_valid = false;
2359 save_cur_lineno = cstate->cur_lineno;
2362 * heap_multi_insert leaks memory, so switch to short-lived memory context
2363 * before calling it.
2365 oldcontext = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
2366 heap_multi_insert(cstate->rel,
2372 MemoryContextSwitchTo(oldcontext);
2375 * If there are any indexes, update them for all the inserted tuples, and
2376 * run AFTER ROW INSERT triggers.
2378 if (resultRelInfo->ri_NumIndices > 0)
2380 for (i = 0; i < nBufferedTuples; i++)
2382 List *recheckIndexes;
2384 cstate->cur_lineno = firstBufferedLineNo + i;
2385 ExecStoreTuple(bufferedTuples[i], myslot, InvalidBuffer, false);
2387 ExecInsertIndexTuples(myslot, &(bufferedTuples[i]->t_self),
2389 ExecARInsertTriggers(estate, resultRelInfo,
2392 list_free(recheckIndexes);
2397 * There's no indexes, but see if we need to run AFTER ROW INSERT triggers
2400 else if (resultRelInfo->ri_TrigDesc != NULL &&
2401 resultRelInfo->ri_TrigDesc->trig_insert_after_row)
2403 for (i = 0; i < nBufferedTuples; i++)
2405 cstate->cur_lineno = firstBufferedLineNo + i;
2406 ExecARInsertTriggers(estate, resultRelInfo,
2412 /* reset cur_lineno to where we were */
2413 cstate->cur_lineno = save_cur_lineno;
2417 * Setup to read tuples from a file for COPY FROM.
2419 * 'rel': Used as a template for the tuples
2420 * 'filename': Name of server-local file to read
2421 * 'attnamelist': List of char *, columns to include. NIL selects all cols.
2422 * 'options': List of DefElem. See copy_opt_item in gram.y for selections.
2424 * Returns a CopyState, to be passed to NextCopyFrom and related functions.
2427 BeginCopyFrom(Relation rel,
2428 const char *filename,
2434 bool pipe = (filename == NULL);
2436 Form_pg_attribute *attr;
2437 AttrNumber num_phys_attrs,
2439 FmgrInfo *in_functions;
2444 ExprState **defexprs;
2445 MemoryContext oldcontext;
2446 bool volatile_defexprs;
2448 cstate = BeginCopy(true, rel, NULL, NULL, attnamelist, options);
2449 oldcontext = MemoryContextSwitchTo(cstate->copycontext);
2451 /* Initialize state variables */
2452 cstate->fe_eof = false;
2453 cstate->eol_type = EOL_UNKNOWN;
2454 cstate->cur_relname = RelationGetRelationName(cstate->rel);
2455 cstate->cur_lineno = 0;
2456 cstate->cur_attname = NULL;
2457 cstate->cur_attval = NULL;
2459 /* Set up variables to avoid per-attribute overhead. */
2460 initStringInfo(&cstate->attribute_buf);
2461 initStringInfo(&cstate->line_buf);
2462 cstate->line_buf_converted = false;
2463 cstate->raw_buf = (char *) palloc(RAW_BUF_SIZE + 1);
2464 cstate->raw_buf_index = cstate->raw_buf_len = 0;
2466 tupDesc = RelationGetDescr(cstate->rel);
2467 attr = tupDesc->attrs;
2468 num_phys_attrs = tupDesc->natts;
2470 volatile_defexprs = false;
2473 * Pick up the required catalog information for each attribute in the
2474 * relation, including the input function, the element type (to pass to
2475 * the input function), and info about defaults and constraints. (Which
2476 * input function we use depends on text/binary format choice.)
2478 in_functions = (FmgrInfo *) palloc(num_phys_attrs * sizeof(FmgrInfo));
2479 typioparams = (Oid *) palloc(num_phys_attrs * sizeof(Oid));
2480 defmap = (int *) palloc(num_phys_attrs * sizeof(int));
2481 defexprs = (ExprState **) palloc(num_phys_attrs * sizeof(ExprState *));
2483 for (attnum = 1; attnum <= num_phys_attrs; attnum++)
2485 /* We don't need info for dropped attributes */
2486 if (attr[attnum - 1]->attisdropped)
2489 /* Fetch the input function and typioparam info */
2491 getTypeBinaryInputInfo(attr[attnum - 1]->atttypid,
2492 &in_func_oid, &typioparams[attnum - 1]);
2494 getTypeInputInfo(attr[attnum - 1]->atttypid,
2495 &in_func_oid, &typioparams[attnum - 1]);
2496 fmgr_info(in_func_oid, &in_functions[attnum - 1]);
2498 /* Get default info if needed */
2499 if (!list_member_int(cstate->attnumlist, attnum))
2501 /* attribute is NOT to be copied from input */
2502 /* use default value if one exists */
2503 Node *defexpr = build_column_default(cstate->rel, attnum);
2505 if (defexpr != NULL)
2507 /* Initialize expressions in copycontext. */
2508 defexprs[num_defaults] = ExecInitExpr(
2509 expression_planner((Expr *) defexpr), NULL);
2510 defmap[num_defaults] = attnum - 1;
2513 if (!volatile_defexprs)
2514 volatile_defexprs = contain_volatile_functions(defexpr);
2519 /* We keep those variables in cstate. */
2520 cstate->in_functions = in_functions;
2521 cstate->typioparams = typioparams;
2522 cstate->defmap = defmap;
2523 cstate->defexprs = defexprs;
2524 cstate->volatile_defexprs = volatile_defexprs;
2525 cstate->num_defaults = num_defaults;
2526 cstate->is_program = is_program;
2530 Assert(!is_program); /* the grammar does not allow this */
2531 if (whereToSendOutput == DestRemote)
2532 ReceiveCopyBegin(cstate);
2534 cstate->copy_file = stdin;
2538 cstate->filename = pstrdup(filename);
2540 if (cstate->is_program)
2542 cstate->copy_file = OpenPipeStream(cstate->filename, PG_BINARY_R);
2543 if (cstate->copy_file == NULL)
2545 (errmsg("could not execute command \"%s\": %m",
2546 cstate->filename)));
2552 cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_R);
2553 if (cstate->copy_file == NULL)
2555 (errcode_for_file_access(),
2556 errmsg("could not open file \"%s\" for reading: %m",
2557 cstate->filename)));
2559 fstat(fileno(cstate->copy_file), &st);
2560 if (S_ISDIR(st.st_mode))
2562 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2563 errmsg("\"%s\" is a directory", cstate->filename)));
2567 if (!cstate->binary)
2569 /* must rely on user to tell us... */
2570 cstate->file_has_oids = cstate->oids;
2574 /* Read and verify binary header */
2579 if (CopyGetData(cstate, readSig, 11, 11) != 11 ||
2580 memcmp(readSig, BinarySignature, 11) != 0)
2582 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2583 errmsg("COPY file signature not recognized")));
2585 if (!CopyGetInt32(cstate, &tmp))
2587 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2588 errmsg("invalid COPY file header (missing flags)")));
2589 cstate->file_has_oids = (tmp & (1 << 16)) != 0;
2591 if ((tmp >> 16) != 0)
2593 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2594 errmsg("unrecognized critical flags in COPY file header")));
2595 /* Header extension length */
2596 if (!CopyGetInt32(cstate, &tmp) ||
2599 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2600 errmsg("invalid COPY file header (missing length)")));
2601 /* Skip extension header, if present */
2604 if (CopyGetData(cstate, readSig, 1, 1) != 1)
2606 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2607 errmsg("invalid COPY file header (wrong length)")));
2611 if (cstate->file_has_oids && cstate->binary)
2613 getTypeBinaryInputInfo(OIDOID,
2614 &in_func_oid, &cstate->oid_typioparam);
2615 fmgr_info(in_func_oid, &cstate->oid_in_function);
2618 /* create workspace for CopyReadAttributes results */
2619 if (!cstate->binary)
2621 AttrNumber attr_count = list_length(cstate->attnumlist);
2622 int nfields = cstate->file_has_oids ? (attr_count + 1) : attr_count;
2624 cstate->max_fields = nfields;
2625 cstate->raw_fields = (char **) palloc(nfields * sizeof(char *));
2628 MemoryContextSwitchTo(oldcontext);
2634 * Read raw fields in the next line for COPY FROM in text or csv mode.
2635 * Return false if no more lines.
2637 * An internal temporary buffer is returned via 'fields'. It is valid until
2638 * the next call of the function. Since the function returns all raw fields
2639 * in the input file, 'nfields' could be different from the number of columns
2642 * NOTE: force_not_null option are not applied to the returned fields.
2645 NextCopyFromRawFields(CopyState cstate, char ***fields, int *nfields)
2650 /* only available for text or csv input */
2651 Assert(!cstate->binary);
2653 /* on input just throw the header line away */
2654 if (cstate->cur_lineno == 0 && cstate->header_line)
2656 cstate->cur_lineno++;
2657 if (CopyReadLine(cstate))
2658 return false; /* done */
2661 cstate->cur_lineno++;
2663 /* Actually read the line into memory here */
2664 done = CopyReadLine(cstate);
2667 * EOF at start of line means we're done. If we see EOF after some
2668 * characters, we act as though it was newline followed by EOF, ie,
2669 * process the line and then exit loop on next iteration.
2671 if (done && cstate->line_buf.len == 0)
2674 /* Parse the line into de-escaped field values */
2675 if (cstate->csv_mode)
2676 fldct = CopyReadAttributesCSV(cstate);
2678 fldct = CopyReadAttributesText(cstate);
2680 *fields = cstate->raw_fields;
2686 * Read next tuple from file for COPY FROM. Return false if no more tuples.
2688 * 'econtext' is used to evaluate default expression for each columns not
2689 * read from the file. It can be NULL when no default values are used, i.e.
2690 * when all columns are read from the file.
2692 * 'values' and 'nulls' arrays must be the same length as columns of the
2693 * relation passed to BeginCopyFrom. This function fills the arrays.
2694 * Oid of the tuple is returned with 'tupleOid' separately.
2697 NextCopyFrom(CopyState cstate, ExprContext *econtext,
2698 Datum *values, bool *nulls, Oid *tupleOid)
2701 Form_pg_attribute *attr;
2702 AttrNumber num_phys_attrs,
2704 num_defaults = cstate->num_defaults;
2705 FmgrInfo *in_functions = cstate->in_functions;
2706 Oid *typioparams = cstate->typioparams;
2710 bool file_has_oids = cstate->file_has_oids;
2711 int *defmap = cstate->defmap;
2712 ExprState **defexprs = cstate->defexprs;
2714 tupDesc = RelationGetDescr(cstate->rel);
2715 attr = tupDesc->attrs;
2716 num_phys_attrs = tupDesc->natts;
2717 attr_count = list_length(cstate->attnumlist);
2718 nfields = file_has_oids ? (attr_count + 1) : attr_count;
2720 /* Initialize all values for row to NULL */
2721 MemSet(values, 0, num_phys_attrs * sizeof(Datum));
2722 MemSet(nulls, true, num_phys_attrs * sizeof(bool));
2724 if (!cstate->binary)
2726 char **field_strings;
2732 /* read raw fields in the next line */
2733 if (!NextCopyFromRawFields(cstate, &field_strings, &fldct))
2736 /* check for overflowing fields */
2737 if (nfields > 0 && fldct > nfields)
2739 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2740 errmsg("extra data after last expected column")));
2744 /* Read the OID field if present */
2747 if (fieldno >= fldct)
2749 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2750 errmsg("missing data for OID column")));
2751 string = field_strings[fieldno++];
2755 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2756 errmsg("null OID in COPY data")));
2757 else if (cstate->oids && tupleOid != NULL)
2759 cstate->cur_attname = "oid";
2760 cstate->cur_attval = string;
2761 *tupleOid = DatumGetObjectId(DirectFunctionCall1(oidin,
2762 CStringGetDatum(string)));
2763 if (*tupleOid == InvalidOid)
2765 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2766 errmsg("invalid OID in COPY data")));
2767 cstate->cur_attname = NULL;
2768 cstate->cur_attval = NULL;
2772 /* Loop to read the user attributes on the line. */
2773 foreach(cur, cstate->attnumlist)
2775 int attnum = lfirst_int(cur);
2778 if (fieldno >= fldct)
2780 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2781 errmsg("missing data for column \"%s\"",
2782 NameStr(attr[m]->attname))));
2783 string = field_strings[fieldno++];
2785 if (cstate->convert_select_flags &&
2786 !cstate->convert_select_flags[m])
2788 /* ignore input field, leaving column as NULL */
2792 if (cstate->csv_mode && string == NULL &&
2793 cstate->force_notnull_flags[m])
2795 /* Go ahead and read the NULL string */
2796 string = cstate->null_print;
2799 cstate->cur_attname = NameStr(attr[m]->attname);
2800 cstate->cur_attval = string;
2801 values[m] = InputFunctionCall(&in_functions[m],
2804 attr[m]->atttypmod);
2807 cstate->cur_attname = NULL;
2808 cstate->cur_attval = NULL;
2811 Assert(fieldno == nfields);
2819 cstate->cur_lineno++;
2821 if (!CopyGetInt16(cstate, &fld_count))
2823 /* EOF detected (end of file, or protocol-level EOF) */
2827 if (fld_count == -1)
2830 * Received EOF marker. In a V3-protocol copy, wait for the
2831 * protocol-level EOF, and complain if it doesn't come
2832 * immediately. This ensures that we correctly handle CopyFail,
2833 * if client chooses to send that now.
2835 * Note that we MUST NOT try to read more data in an old-protocol
2836 * copy, since there is no protocol-level EOF marker then. We
2837 * could go either way for copy from file, but choose to throw
2838 * error if there's data after the EOF marker, for consistency
2839 * with the new-protocol case.
2843 if (cstate->copy_dest != COPY_OLD_FE &&
2844 CopyGetData(cstate, &dummy, 1, 1) > 0)
2846 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2847 errmsg("received copy data after EOF marker")));
2851 if (fld_count != attr_count)
2853 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2854 errmsg("row field count is %d, expected %d",
2855 (int) fld_count, attr_count)));
2861 cstate->cur_attname = "oid";
2863 DatumGetObjectId(CopyReadBinaryAttribute(cstate,
2865 &cstate->oid_in_function,
2866 cstate->oid_typioparam,
2869 if (isnull || loaded_oid == InvalidOid)
2871 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2872 errmsg("invalid OID in COPY data")));
2873 cstate->cur_attname = NULL;
2874 if (cstate->oids && tupleOid != NULL)
2875 *tupleOid = loaded_oid;
2879 foreach(cur, cstate->attnumlist)
2881 int attnum = lfirst_int(cur);
2884 cstate->cur_attname = NameStr(attr[m]->attname);
2886 values[m] = CopyReadBinaryAttribute(cstate,
2892 cstate->cur_attname = NULL;
2897 * Now compute and insert any defaults available for the columns not
2898 * provided by the input data. Anything not processed here or above will
2901 for (i = 0; i < num_defaults; i++)
2904 * The caller must supply econtext and have switched into the
2905 * per-tuple memory context in it.
2907 Assert(econtext != NULL);
2908 Assert(CurrentMemoryContext == econtext->ecxt_per_tuple_memory);
2910 values[defmap[i]] = ExecEvalExpr(defexprs[i], econtext,
2911 &nulls[defmap[i]], NULL);
2918 * Clean up storage and release resources for COPY FROM.
2921 EndCopyFrom(CopyState cstate)
2923 /* No COPY FROM related resources except memory. */
2929 * Read the next input line and stash it in line_buf, with conversion to
2932 * Result is true if read was terminated by EOF, false if terminated
2933 * by newline. The terminating newline or EOF marker is not included
2934 * in the final value of line_buf.
2937 CopyReadLine(CopyState cstate)
2941 resetStringInfo(&cstate->line_buf);
2942 cstate->line_buf_valid = true;
2944 /* Mark that encoding conversion hasn't occurred yet */
2945 cstate->line_buf_converted = false;
2947 /* Parse data and transfer into line_buf */
2948 result = CopyReadLineText(cstate);
2953 * Reached EOF. In protocol version 3, we should ignore anything
2954 * after \. up to the protocol end of copy data. (XXX maybe better
2955 * not to treat \. as special?)
2957 if (cstate->copy_dest == COPY_NEW_FE)
2961 cstate->raw_buf_index = cstate->raw_buf_len;
2962 } while (CopyLoadRawBuf(cstate));
2968 * If we didn't hit EOF, then we must have transferred the EOL marker
2969 * to line_buf along with the data. Get rid of it.
2971 switch (cstate->eol_type)
2974 Assert(cstate->line_buf.len >= 1);
2975 Assert(cstate->line_buf.data[cstate->line_buf.len - 1] == '\n');
2976 cstate->line_buf.len--;
2977 cstate->line_buf.data[cstate->line_buf.len] = '\0';
2980 Assert(cstate->line_buf.len >= 1);
2981 Assert(cstate->line_buf.data[cstate->line_buf.len - 1] == '\r');
2982 cstate->line_buf.len--;
2983 cstate->line_buf.data[cstate->line_buf.len] = '\0';
2986 Assert(cstate->line_buf.len >= 2);
2987 Assert(cstate->line_buf.data[cstate->line_buf.len - 2] == '\r');
2988 Assert(cstate->line_buf.data[cstate->line_buf.len - 1] == '\n');
2989 cstate->line_buf.len -= 2;
2990 cstate->line_buf.data[cstate->line_buf.len] = '\0';
2993 /* shouldn't get here */
2999 /* Done reading the line. Convert it to server encoding. */
3000 if (cstate->need_transcoding)
3004 cvt = pg_any_to_server(cstate->line_buf.data,
3005 cstate->line_buf.len,
3006 cstate->file_encoding);
3007 if (cvt != cstate->line_buf.data)
3009 /* transfer converted data back to line_buf */
3010 resetStringInfo(&cstate->line_buf);
3011 appendBinaryStringInfo(&cstate->line_buf, cvt, strlen(cvt));
3016 /* Now it's safe to use the buffer in error messages */
3017 cstate->line_buf_converted = true;
3023 * CopyReadLineText - inner loop of CopyReadLine for text mode
3026 CopyReadLineText(CopyState cstate)
3031 bool need_data = false;
3032 bool hit_eof = false;
3033 bool result = false;
3037 bool first_char_in_line = true;
3038 bool in_quote = false,
3039 last_was_esc = false;
3041 char escapec = '\0';
3043 if (cstate->csv_mode)
3045 quotec = cstate->quote[0];
3046 escapec = cstate->escape[0];
3047 /* ignore special escape processing if it's the same as quotec */
3048 if (quotec == escapec)
3052 mblen_str[1] = '\0';
3055 * The objective of this loop is to transfer the entire next input line
3056 * into line_buf. Hence, we only care for detecting newlines (\r and/or
3057 * \n) and the end-of-copy marker (\.).
3059 * In CSV mode, \r and \n inside a quoted field are just part of the data
3060 * value and are put in line_buf. We keep just enough state to know if we
3061 * are currently in a quoted field or not.
3063 * These four characters, and the CSV escape and quote characters, are
3064 * assumed the same in frontend and backend encodings.
3066 * For speed, we try to move data from raw_buf to line_buf in chunks
3067 * rather than one character at a time. raw_buf_ptr points to the next
3068 * character to examine; any characters from raw_buf_index to raw_buf_ptr
3069 * have been determined to be part of the line, but not yet transferred to
3072 * For a little extra speed within the loop, we copy raw_buf and
3073 * raw_buf_len into local variables.
3075 copy_raw_buf = cstate->raw_buf;
3076 raw_buf_ptr = cstate->raw_buf_index;
3077 copy_buf_len = cstate->raw_buf_len;
3085 * Load more data if needed. Ideally we would just force four bytes
3086 * of read-ahead and avoid the many calls to
3087 * IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(), but the COPY_OLD_FE protocol
3088 * does not allow us to read too far ahead or we might read into the
3089 * next data, so we read-ahead only as far we know we can. One
3090 * optimization would be to read-ahead four byte here if
3091 * cstate->copy_dest != COPY_OLD_FE, but it hardly seems worth it,
3092 * considering the size of the buffer.
3094 if (raw_buf_ptr >= copy_buf_len || need_data)
3099 * Try to read some more data. This will certainly reset
3100 * raw_buf_index to zero, and raw_buf_ptr must go with it.
3102 if (!CopyLoadRawBuf(cstate))
3105 copy_buf_len = cstate->raw_buf_len;
3108 * If we are completely out of data, break out of the loop,
3111 if (copy_buf_len <= 0)
3119 /* OK to fetch a character */
3120 prev_raw_ptr = raw_buf_ptr;
3121 c = copy_raw_buf[raw_buf_ptr++];
3123 if (cstate->csv_mode)
3126 * If character is '\\' or '\r', we may need to look ahead below.
3127 * Force fetch of the next character if we don't already have it.
3128 * We need to do this before changing CSV state, in case one of
3129 * these characters is also the quote or escape character.
3131 * Note: old-protocol does not like forced prefetch, but it's OK
3132 * here since we cannot validly be at EOF.
3134 if (c == '\\' || c == '\r')
3136 IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(0);
3140 * Dealing with quotes and escapes here is mildly tricky. If the
3141 * quote char is also the escape char, there's no problem - we
3142 * just use the char as a toggle. If they are different, we need
3143 * to ensure that we only take account of an escape inside a
3144 * quoted field and immediately preceding a quote char, and not
3145 * the second in a escape-escape sequence.
3147 if (in_quote && c == escapec)
3148 last_was_esc = !last_was_esc;
3149 if (c == quotec && !last_was_esc)
3150 in_quote = !in_quote;
3152 last_was_esc = false;
3155 * Updating the line count for embedded CR and/or LF chars is
3156 * necessarily a little fragile - this test is probably about the
3157 * best we can do. (XXX it's arguable whether we should do this
3158 * at all --- is cur_lineno a physical or logical count?)
3160 if (in_quote && c == (cstate->eol_type == EOL_NL ? '\n' : '\r'))
3161 cstate->cur_lineno++;
3165 if (c == '\r' && (!cstate->csv_mode || !in_quote))
3167 /* Check for \r\n on first line, _and_ handle \r\n. */
3168 if (cstate->eol_type == EOL_UNKNOWN ||
3169 cstate->eol_type == EOL_CRNL)
3172 * If need more data, go back to loop top to load it.
3174 * Note that if we are at EOF, c will wind up as '\0' because
3175 * of the guaranteed pad of raw_buf.
3177 IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(0);
3180 c = copy_raw_buf[raw_buf_ptr];
3184 raw_buf_ptr++; /* eat newline */
3185 cstate->eol_type = EOL_CRNL; /* in case not set yet */
3189 /* found \r, but no \n */
3190 if (cstate->eol_type == EOL_CRNL)
3192 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3194 errmsg("literal carriage return found in data") :
3195 errmsg("unquoted carriage return found in data"),
3197 errhint("Use \"\\r\" to represent carriage return.") :
3198 errhint("Use quoted CSV field to represent carriage return.")));
3201 * if we got here, it is the first line and we didn't find
3202 * \n, so don't consume the peeked character
3204 cstate->eol_type = EOL_CR;
3207 else if (cstate->eol_type == EOL_NL)
3209 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3211 errmsg("literal carriage return found in data") :
3212 errmsg("unquoted carriage return found in data"),
3214 errhint("Use \"\\r\" to represent carriage return.") :
3215 errhint("Use quoted CSV field to represent carriage return.")));
3216 /* If reach here, we have found the line terminator */
3221 if (c == '\n' && (!cstate->csv_mode || !in_quote))
3223 if (cstate->eol_type == EOL_CR || cstate->eol_type == EOL_CRNL)
3225 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3227 errmsg("literal newline found in data") :
3228 errmsg("unquoted newline found in data"),
3230 errhint("Use \"\\n\" to represent newline.") :
3231 errhint("Use quoted CSV field to represent newline.")));
3232 cstate->eol_type = EOL_NL; /* in case not set yet */
3233 /* If reach here, we have found the line terminator */
3238 * In CSV mode, we only recognize \. alone on a line. This is because
3239 * \. is a valid CSV data value.
3241 if (c == '\\' && (!cstate->csv_mode || first_char_in_line))
3245 IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(0);
3246 IF_NEED_REFILL_AND_EOF_BREAK(0);
3249 * get next character
3250 * Note: we do not change c so if it isn't \., we can fall
3251 * through and continue processing for file encoding.
3254 c2 = copy_raw_buf[raw_buf_ptr];
3258 raw_buf_ptr++; /* consume the '.' */
3261 * Note: if we loop back for more data here, it does not
3262 * matter that the CSV state change checks are re-executed; we
3263 * will come back here with no important state changed.
3265 if (cstate->eol_type == EOL_CRNL)
3267 /* Get the next character */
3268 IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(0);
3269 /* if hit_eof, c2 will become '\0' */
3270 c2 = copy_raw_buf[raw_buf_ptr++];
3274 if (!cstate->csv_mode)
3276 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3277 errmsg("end-of-copy marker does not match previous newline style")));
3279 NO_END_OF_COPY_GOTO;
3281 else if (c2 != '\r')
3283 if (!cstate->csv_mode)
3285 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3286 errmsg("end-of-copy marker corrupt")));
3288 NO_END_OF_COPY_GOTO;
3292 /* Get the next character */
3293 IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(0);
3294 /* if hit_eof, c2 will become '\0' */
3295 c2 = copy_raw_buf[raw_buf_ptr++];
3297 if (c2 != '\r' && c2 != '\n')
3299 if (!cstate->csv_mode)
3301 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3302 errmsg("end-of-copy marker corrupt")));
3304 NO_END_OF_COPY_GOTO;
3307 if ((cstate->eol_type == EOL_NL && c2 != '\n') ||
3308 (cstate->eol_type == EOL_CRNL && c2 != '\n') ||
3309 (cstate->eol_type == EOL_CR && c2 != '\r'))
3312 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3313 errmsg("end-of-copy marker does not match previous newline style")));
3317 * Transfer only the data before the \. into line_buf, then
3318 * discard the data and the \. sequence.
3320 if (prev_raw_ptr > cstate->raw_buf_index)
3321 appendBinaryStringInfo(&cstate->line_buf,
3322 cstate->raw_buf + cstate->raw_buf_index,
3323 prev_raw_ptr - cstate->raw_buf_index);
3324 cstate->raw_buf_index = raw_buf_ptr;
3325 result = true; /* report EOF */
3328 else if (!cstate->csv_mode)
3331 * If we are here, it means we found a backslash followed by
3332 * something other than a period. In non-CSV mode, anything
3333 * after a backslash is special, so we skip over that second
3334 * character too. If we didn't do that \\. would be
3335 * considered an eof-of copy, while in non-CSV mode it is a
3336 * literal backslash followed by a period. In CSV mode,
3337 * backslashes are not special, so we want to process the
3338 * character after the backslash just like a normal character,
3339 * so we don't increment in those cases.
3345 * This label is for CSV cases where \. appears at the start of a
3346 * line, but there is more text after it, meaning it was a data value.
3347 * We are more strict for \. in CSV mode because \. could be a data
3348 * value, while in non-CSV mode, \. cannot be a data value.
3353 * Process all bytes of a multi-byte character as a group.
3355 * We only support multi-byte sequences where the first byte has the
3356 * high-bit set, so as an optimization we can avoid this block
3357 * entirely if it is not set.
3359 if (cstate->encoding_embeds_ascii && IS_HIGHBIT_SET(c))
3364 /* All our encodings only read the first byte to get the length */
3365 mblen = pg_encoding_mblen(cstate->file_encoding, mblen_str);
3366 IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(mblen - 1);
3367 IF_NEED_REFILL_AND_EOF_BREAK(mblen - 1);
3368 raw_buf_ptr += mblen - 1;
3370 first_char_in_line = false;
3371 } /* end of outer loop */
3374 * Transfer any still-uncopied data to line_buf.
3382 * Return decimal value for a hexadecimal digit
3385 GetDecimalFromHex(char hex)
3387 if (isdigit((unsigned char) hex))
3390 return tolower((unsigned char) hex) - 'a' + 10;
3394 * Parse the current line into separate attributes (fields),
3395 * performing de-escaping as needed.
3397 * The input is in line_buf. We use attribute_buf to hold the result
3398 * strings. cstate->raw_fields[k] is set to point to the k'th attribute
3399 * string, or NULL when the input matches the null marker string.
3400 * This array is expanded as necessary.
3402 * (Note that the caller cannot check for nulls since the returned
3403 * string would be the post-de-escaping equivalent, which may look
3404 * the same as some valid data string.)
3406 * delim is the column delimiter string (must be just one byte for now).
3407 * null_print is the null marker string. Note that this is compared to
3408 * the pre-de-escaped input string.
3410 * The return value is the number of fields actually read.
3413 CopyReadAttributesText(CopyState cstate)
3415 char delimc = cstate->delim[0];
3422 * We need a special case for zero-column tables: check that the input
3423 * line is empty, and return.
3425 if (cstate->max_fields <= 0)
3427 if (cstate->line_buf.len != 0)
3429 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3430 errmsg("extra data after last expected column")));
3434 resetStringInfo(&cstate->attribute_buf);
3437 * The de-escaped attributes will certainly not be longer than the input
3438 * data line, so we can just force attribute_buf to be large enough and
3439 * then transfer data without any checks for enough space. We need to do
3440 * it this way because enlarging attribute_buf mid-stream would invalidate
3441 * pointers already stored into cstate->raw_fields[].
3443 if (cstate->attribute_buf.maxlen <= cstate->line_buf.len)
3444 enlargeStringInfo(&cstate->attribute_buf, cstate->line_buf.len);
3445 output_ptr = cstate->attribute_buf.data;
3447 /* set pointer variables for loop */
3448 cur_ptr = cstate->line_buf.data;
3449 line_end_ptr = cstate->line_buf.data + cstate->line_buf.len;
3451 /* Outer loop iterates over fields */
3455 bool found_delim = false;
3459 bool saw_non_ascii = false;
3461 /* Make sure there is enough space for the next value */
3462 if (fieldno >= cstate->max_fields)
3464 cstate->max_fields *= 2;
3465 cstate->raw_fields =
3466 repalloc(cstate->raw_fields, cstate->max_fields * sizeof(char *));
3469 /* Remember start of field on both input and output sides */
3470 start_ptr = cur_ptr;
3471 cstate->raw_fields[fieldno] = output_ptr;
3474 * Scan data for field.
3476 * Note that in this loop, we are scanning to locate the end of field
3477 * and also speculatively performing de-escaping. Once we find the
3478 * end-of-field, we can match the raw field contents against the null
3479 * marker string. Only after that comparison fails do we know that
3480 * de-escaping is actually the right thing to do; therefore we *must
3481 * not* throw any syntax errors before we've done the null-marker
3489 if (cur_ptr >= line_end_ptr)
3499 if (cur_ptr >= line_end_ptr)
3517 if (cur_ptr < line_end_ptr)
3523 val = (val << 3) + OCTVALUE(c);
3524 if (cur_ptr < line_end_ptr)
3530 val = (val << 3) + OCTVALUE(c);
3536 if (c == '\0' || IS_HIGHBIT_SET(c))
3537 saw_non_ascii = true;
3542 if (cur_ptr < line_end_ptr)
3544 char hexchar = *cur_ptr;
3546 if (isxdigit((unsigned char) hexchar))
3548 int val = GetDecimalFromHex(hexchar);
3551 if (cur_ptr < line_end_ptr)
3554 if (isxdigit((unsigned char) hexchar))
3557 val = (val << 4) + GetDecimalFromHex(hexchar);
3561 if (c == '\0' || IS_HIGHBIT_SET(c))
3562 saw_non_ascii = true;
3586 * in all other cases, take the char after '\'
3592 /* Add c to output string */
3596 /* Check whether raw input matched null marker */
3597 input_len = end_ptr - start_ptr;
3598 if (input_len == cstate->null_print_len &&
3599 strncmp(start_ptr, cstate->null_print, input_len) == 0)
3600 cstate->raw_fields[fieldno] = NULL;
3604 * At this point we know the field is supposed to contain data.
3606 * If we de-escaped any non-7-bit-ASCII chars, make sure the
3607 * resulting string is valid data for the db encoding.
3611 char *fld = cstate->raw_fields[fieldno];
3613 pg_verifymbstr(fld, output_ptr - fld, false);
3617 /* Terminate attribute value in output area */
3618 *output_ptr++ = '\0';
3621 /* Done if we hit EOL instead of a delim */
3626 /* Clean up state of attribute_buf */
3628 Assert(*output_ptr == '\0');
3629 cstate->attribute_buf.len = (output_ptr - cstate->attribute_buf.data);
3635 * Parse the current line into separate attributes (fields),
3636 * performing de-escaping as needed. This has exactly the same API as
3637 * CopyReadAttributesText, except we parse the fields according to
3638 * "standard" (i.e. common) CSV usage.
3641 CopyReadAttributesCSV(CopyState cstate)
3643 char delimc = cstate->delim[0];
3644 char quotec = cstate->quote[0];
3645 char escapec = cstate->escape[0];
3652 * We need a special case for zero-column tables: check that the input
3653 * line is empty, and return.
3655 if (cstate->max_fields <= 0)
3657 if (cstate->line_buf.len != 0)
3659 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3660 errmsg("extra data after last expected column")));
3664 resetStringInfo(&cstate->attribute_buf);
3667 * The de-escaped attributes will certainly not be longer than the input
3668 * data line, so we can just force attribute_buf to be large enough and
3669 * then transfer data without any checks for enough space. We need to do
3670 * it this way because enlarging attribute_buf mid-stream would invalidate
3671 * pointers already stored into cstate->raw_fields[].
3673 if (cstate->attribute_buf.maxlen <= cstate->line_buf.len)
3674 enlargeStringInfo(&cstate->attribute_buf, cstate->line_buf.len);
3675 output_ptr = cstate->attribute_buf.data;
3677 /* set pointer variables for loop */
3678 cur_ptr = cstate->line_buf.data;
3679 line_end_ptr = cstate->line_buf.data + cstate->line_buf.len;
3681 /* Outer loop iterates over fields */
3685 bool found_delim = false;
3686 bool saw_quote = false;
3691 /* Make sure there is enough space for the next value */
3692 if (fieldno >= cstate->max_fields)
3694 cstate->max_fields *= 2;
3695 cstate->raw_fields =
3696 repalloc(cstate->raw_fields, cstate->max_fields * sizeof(char *));
3699 /* Remember start of field on both input and output sides */
3700 start_ptr = cur_ptr;
3701 cstate->raw_fields[fieldno] = output_ptr;
3704 * Scan data for field,
3706 * The loop starts in "not quote" mode and then toggles between that
3707 * and "in quote" mode. The loop exits normally if it is in "not
3708 * quote" mode and a delimiter or line end is seen.
3718 if (cur_ptr >= line_end_ptr)
3721 /* unquoted field delimiter */
3727 /* start of quoted field (or part of field) */
3733 /* Add c to output string */
3741 if (cur_ptr >= line_end_ptr)
3743 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3744 errmsg("unterminated CSV quoted field")));
3748 /* escape within a quoted field */
3752 * peek at the next char if available, and escape it if it
3753 * is an escape char or a quote char
3755 if (cur_ptr < line_end_ptr)
3757 char nextc = *cur_ptr;
3759 if (nextc == escapec || nextc == quotec)
3761 *output_ptr++ = nextc;
3769 * end of quoted field. Must do this test after testing for
3770 * escape in case quote char and escape char are the same
3771 * (which is the common case).
3776 /* Add c to output string */
3782 /* Terminate attribute value in output area */
3783 *output_ptr++ = '\0';
3785 /* Check whether raw input matched null marker */
3786 input_len = end_ptr - start_ptr;
3787 if (!saw_quote && input_len == cstate->null_print_len &&
3788 strncmp(start_ptr, cstate->null_print, input_len) == 0)
3789 cstate->raw_fields[fieldno] = NULL;
3792 /* Done if we hit EOL instead of a delim */
3797 /* Clean up state of attribute_buf */
3799 Assert(*output_ptr == '\0');
3800 cstate->attribute_buf.len = (output_ptr - cstate->attribute_buf.data);
3807 * Read a binary attribute
3810 CopyReadBinaryAttribute(CopyState cstate,
3811 int column_no, FmgrInfo *flinfo,
3812 Oid typioparam, int32 typmod,
3818 if (!CopyGetInt32(cstate, &fld_size))
3820 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3821 errmsg("unexpected EOF in COPY data")));
3825 return ReceiveFunctionCall(flinfo, NULL, typioparam, typmod);
3829 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3830 errmsg("invalid field size")));
3832 /* reset attribute_buf to empty, and load raw data in it */
3833 resetStringInfo(&cstate->attribute_buf);
3835 enlargeStringInfo(&cstate->attribute_buf, fld_size);
3836 if (CopyGetData(cstate, cstate->attribute_buf.data,
3837 fld_size, fld_size) != fld_size)
3839 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3840 errmsg("unexpected EOF in COPY data")));
3842 cstate->attribute_buf.len = fld_size;
3843 cstate->attribute_buf.data[fld_size] = '\0';
3845 /* Call the column type's binary input converter */
3846 result = ReceiveFunctionCall(flinfo, &cstate->attribute_buf,
3847 typioparam, typmod);
3849 /* Trouble if it didn't eat the whole buffer */
3850 if (cstate->attribute_buf.cursor != cstate->attribute_buf.len)
3852 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
3853 errmsg("incorrect binary data format")));
3860 * Send text representation of one attribute, with conversion and escaping
3862 #define DUMPSOFAR() \
3865 CopySendData(cstate, start, ptr - start); \
3869 CopyAttributeOutText(CopyState cstate, char *string)
3874 char delimc = cstate->delim[0];
3876 if (cstate->need_transcoding)
3877 ptr = pg_server_to_any(string, strlen(string), cstate->file_encoding);
3882 * We have to grovel through the string searching for control characters
3883 * and instances of the delimiter character. In most cases, though, these
3884 * are infrequent. To avoid overhead from calling CopySendData once per
3885 * character, we dump out all characters between escaped characters in a
3886 * single call. The loop invariant is that the data from "start" to "ptr"
3887 * can be sent literally, but hasn't yet been.
3889 * We can skip pg_encoding_mblen() overhead when encoding is safe, because
3890 * in valid backend encodings, extra bytes of a multibyte character never
3891 * look like ASCII. This loop is sufficiently performance-critical that
3892 * it's worth making two copies of it to get the IS_HIGHBIT_SET() test out
3893 * of the normal safe-encoding path.
3895 if (cstate->encoding_embeds_ascii)
3898 while ((c = *ptr) != '\0')
3900 if ((unsigned char) c < (unsigned char) 0x20)
3903 * \r and \n must be escaped, the others are traditional. We
3904 * prefer to dump these using the C-like notation, rather than
3905 * a backslash and the literal character, because it makes the
3906 * dump file a bit more proof against Microsoftish data
3930 /* If it's the delimiter, must backslash it */
3933 /* All ASCII control chars are length 1 */
3935 continue; /* fall to end of loop */
3937 /* if we get here, we need to convert the control char */
3939 CopySendChar(cstate, '\\');
3940 CopySendChar(cstate, c);
3941 start = ++ptr; /* do not include char in next run */
3943 else if (c == '\\' || c == delimc)
3946 CopySendChar(cstate, '\\');
3947 start = ptr++; /* we include char in next run */
3949 else if (IS_HIGHBIT_SET(c))
3950 ptr += pg_encoding_mblen(cstate->file_encoding, ptr);
3958 while ((c = *ptr) != '\0')
3960 if ((unsigned char) c < (unsigned char) 0x20)
3963 * \r and \n must be escaped, the others are traditional. We
3964 * prefer to dump these using the C-like notation, rather than
3965 * a backslash and the literal character, because it makes the
3966 * dump file a bit more proof against Microsoftish data
3990 /* If it's the delimiter, must backslash it */
3993 /* All ASCII control chars are length 1 */
3995 continue; /* fall to end of loop */
3997 /* if we get here, we need to convert the control char */
3999 CopySendChar(cstate, '\\');
4000 CopySendChar(cstate, c);
4001 start = ++ptr; /* do not include char in next run */
4003 else if (c == '\\' || c == delimc)
4006 CopySendChar(cstate, '\\');
4007 start = ptr++; /* we include char in next run */
4018 * Send text representation of one attribute, with conversion and
4019 * CSV-style escaping
4022 CopyAttributeOutCSV(CopyState cstate, char *string,
4023 bool use_quote, bool single_attr)
4028 char delimc = cstate->delim[0];
4029 char quotec = cstate->quote[0];
4030 char escapec = cstate->escape[0];
4032 /* force quoting if it matches null_print (before conversion!) */
4033 if (!use_quote && strcmp(string, cstate->null_print) == 0)
4036 if (cstate->need_transcoding)
4037 ptr = pg_server_to_any(string, strlen(string), cstate->file_encoding);
4042 * Make a preliminary pass to discover if it needs quoting
4047 * Because '\.' can be a data value, quote it if it appears alone on a
4048 * line so it is not interpreted as the end-of-data marker.
4050 if (single_attr && strcmp(ptr, "\\.") == 0)
4056 while ((c = *tptr) != '\0')
4058 if (c == delimc || c == quotec || c == '\n' || c == '\r')
4063 if (IS_HIGHBIT_SET(c) && cstate->encoding_embeds_ascii)
4064 tptr += pg_encoding_mblen(cstate->file_encoding, tptr);
4073 CopySendChar(cstate, quotec);
4076 * We adopt the same optimization strategy as in CopyAttributeOutText
4079 while ((c = *ptr) != '\0')
4081 if (c == quotec || c == escapec)
4084 CopySendChar(cstate, escapec);
4085 start = ptr; /* we include char in next run */
4087 if (IS_HIGHBIT_SET(c) && cstate->encoding_embeds_ascii)
4088 ptr += pg_encoding_mblen(cstate->file_encoding, ptr);
4094 CopySendChar(cstate, quotec);
4098 /* If it doesn't need quoting, we can just dump it as-is */
4099 CopySendString(cstate, ptr);
4104 * CopyGetAttnums - build an integer list of attnums to be copied
4106 * The input attnamelist is either the user-specified column list,
4107 * or NIL if there was none (in which case we want all the non-dropped
4110 * rel can be NULL ... it's only used for error reports.
4113 CopyGetAttnums(TupleDesc tupDesc, Relation rel, List *attnamelist)
4115 List *attnums = NIL;
4117 if (attnamelist == NIL)
4119 /* Generate default column list */
4120 Form_pg_attribute *attr = tupDesc->attrs;
4121 int attr_count = tupDesc->natts;
4124 for (i = 0; i < attr_count; i++)
4126 if (attr[i]->attisdropped)
4128 attnums = lappend_int(attnums, i + 1);
4133 /* Validate the user-supplied list and extract attnums */
4136 foreach(l, attnamelist)
4138 char *name = strVal(lfirst(l));
4142 /* Lookup column name */
4143 attnum = InvalidAttrNumber;
4144 for (i = 0; i < tupDesc->natts; i++)
4146 if (tupDesc->attrs[i]->attisdropped)
4148 if (namestrcmp(&(tupDesc->attrs[i]->attname), name) == 0)
4150 attnum = tupDesc->attrs[i]->attnum;
4154 if (attnum == InvalidAttrNumber)
4158 (errcode(ERRCODE_UNDEFINED_COLUMN),
4159 errmsg("column \"%s\" of relation \"%s\" does not exist",
4160 name, RelationGetRelationName(rel))));
4163 (errcode(ERRCODE_UNDEFINED_COLUMN),
4164 errmsg("column \"%s\" does not exist",
4167 /* Check for duplicates */
4168 if (list_member_int(attnums, attnum))
4170 (errcode(ERRCODE_DUPLICATE_COLUMN),
4171 errmsg("column \"%s\" specified more than once",
4173 attnums = lappend_int(attnums, attnum);
4182 * copy_dest_startup --- executor startup
4185 copy_dest_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
4191 * copy_dest_receive --- receive one tuple
4194 copy_dest_receive(TupleTableSlot *slot, DestReceiver *self)
4196 DR_copy *myState = (DR_copy *) self;
4197 CopyState cstate = myState->cstate;
4199 /* Make sure the tuple is fully deconstructed */
4200 slot_getallattrs(slot);
4202 /* And send the data */
4203 CopyOneRowTo(cstate, InvalidOid, slot->tts_values, slot->tts_isnull);
4204 myState->processed++;
4208 * copy_dest_shutdown --- executor end
4211 copy_dest_shutdown(DestReceiver *self)
4217 * copy_dest_destroy --- release DestReceiver object
4220 copy_dest_destroy(DestReceiver *self)
4226 * CreateCopyDestReceiver -- create a suitable DestReceiver object
4229 CreateCopyDestReceiver(void)
4231 DR_copy *self = (DR_copy *) palloc(sizeof(DR_copy));
4233 self->pub.receiveSlot = copy_dest_receive;
4234 self->pub.rStartup = copy_dest_startup;
4235 self->pub.rShutdown = copy_dest_shutdown;
4236 self->pub.rDestroy = copy_dest_destroy;
4237 self->pub.mydest = DestCopyOut;
4239 self->cstate = NULL; /* will be set later */
4240 self->processed = 0;
4242 return (DestReceiver *) self;