1 /*-------------------------------------------------------------------------
4 * Implements the COPY utility command.
6 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.219 2004/04/06 13:21:33 momjian Exp $
13 *-------------------------------------------------------------------------
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
23 #include "access/genam.h"
24 #include "access/heapam.h"
25 #include "access/printtup.h"
26 #include "catalog/catname.h"
27 #include "catalog/index.h"
28 #include "catalog/namespace.h"
29 #include "catalog/pg_index.h"
30 #include "catalog/pg_shadow.h"
31 #include "catalog/pg_type.h"
32 #include "commands/copy.h"
33 #include "commands/trigger.h"
34 #include "executor/executor.h"
35 #include "libpq/libpq.h"
36 #include "libpq/pqformat.h"
37 #include "mb/pg_wchar.h"
38 #include "miscadmin.h"
39 #include "nodes/makefuncs.h"
40 #include "parser/parse_coerce.h"
41 #include "parser/parse_relation.h"
42 #include "rewrite/rewriteHandler.h"
43 #include "storage/fd.h"
44 #include "tcop/pquery.h"
45 #include "tcop/tcopprot.h"
46 #include "utils/acl.h"
47 #include "utils/builtins.h"
48 #include "utils/relcache.h"
49 #include "utils/lsyscache.h"
50 #include "utils/syscache.h"
53 #define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
54 #define OCTVALUE(c) ((c) - '0')
57 * Represents the different source/dest cases we need to worry about at
62 COPY_FILE, /* to/from file */
63 COPY_OLD_FE, /* to/from frontend (2.0 protocol) */
64 COPY_NEW_FE /* to/from frontend (3.0 protocol) */
68 * State indicator showing what stopped CopyReadAttribute()
70 typedef enum CopyReadResult
77 * Represents the end-of-line terminator type of the input
88 static const char BinarySignature[11] = "PGCOPY\n\377\r\n\0";
91 * Static communication variables ... pretty grotty, but COPY has
92 * never been reentrant...
94 static CopyDest copy_dest;
95 static FILE *copy_file; /* used if copy_dest == COPY_FILE */
96 static StringInfo copy_msgbuf; /* used if copy_dest == COPY_NEW_FE */
97 static bool fe_eof; /* true if detected end of copy data */
98 static EolType eol_type; /* EOL type of input */
99 static int client_encoding; /* remote side's character encoding */
100 static int server_encoding; /* local encoding */
102 /* these are just for error messages, see copy_in_error_callback */
103 static bool copy_binary; /* is it a binary copy? */
104 static const char *copy_relname; /* table name for error messages */
105 static int copy_lineno; /* line number for error messages */
106 static const char *copy_attname; /* current att for error messages */
110 * These static variables are used to avoid incurring overhead for each
111 * attribute processed. attribute_buf is reused on each CopyReadAttribute
112 * call to hold the string being read in. Under normal use it will soon
113 * grow to a suitable size, and then we will avoid palloc/pfree overhead
114 * for subsequent attributes. Note that CopyReadAttribute returns a pointer
115 * to attribute_buf's data buffer!
117 static StringInfoData attribute_buf;
120 * Similarly, line_buf holds the whole input line being processed (its
121 * cursor field points to the next character to be read by CopyReadAttribute).
122 * The input cycle is first to read the whole line into line_buf, convert it
123 * to server encoding, and then extract individual attribute fields into
124 * attribute_buf. (We used to have CopyReadAttribute read the input source
125 * directly, but that caused a lot of encoding issues and unnecessary logic
128 static StringInfoData line_buf;
129 static bool line_buf_converted;
131 /* non-export function prototypes */
132 static void CopyTo(Relation rel, List *attnumlist, bool binary, bool oids,
133 char *delim, char *null_print);
134 static void CopyFrom(Relation rel, List *attnumlist, bool binary, bool oids,
135 char *delim, char *null_print);
136 static bool CopyReadLine(void);
137 static char *CopyReadAttribute(const char *delim, const char *null_print,
138 CopyReadResult *result, bool *isnull);
139 static Datum CopyReadBinaryAttribute(int column_no, FmgrInfo *flinfo,
140 Oid typelem, bool *isnull);
141 static void CopyAttributeOut(char *string, char *delim);
142 static List *CopyGetAttnums(Relation rel, List *attnamelist);
143 static void limit_printout_length(StringInfo buf);
145 /* Internal communications functions */
146 static void SendCopyBegin(bool binary, int natts);
147 static void ReceiveCopyBegin(bool binary, int natts);
148 static void SendCopyEnd(bool binary);
149 static void CopySendData(void *databuf, int datasize);
150 static void CopySendString(const char *str);
151 static void CopySendChar(char c);
152 static void CopySendEndOfRow(bool binary);
153 static void CopyGetData(void *databuf, int datasize);
154 static int CopyGetChar(void);
156 #define CopyGetEof() (fe_eof)
157 static int CopyPeekChar(void);
158 static void CopyDonePeek(int c, bool pickup);
159 static void CopySendInt32(int32 val);
160 static int32 CopyGetInt32(void);
161 static void CopySendInt16(int16 val);
162 static int16 CopyGetInt16(void);
166 * Send copy start/stop messages for frontend copies. These have changed
167 * in past protocol redesigns.
170 SendCopyBegin(bool binary, int natts)
172 if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
176 int16 format = (binary ? 1 : 0);
179 pq_beginmessage(&buf, 'H');
180 pq_sendbyte(&buf, format); /* overall format */
181 pq_sendint(&buf, natts, 2);
182 for (i = 0; i < natts; i++)
183 pq_sendint(&buf, format, 2); /* per-column formats */
185 copy_dest = COPY_NEW_FE;
186 copy_msgbuf = makeStringInfo();
188 else if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 2)
193 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
194 errmsg("COPY BINARY is not supported to stdout or from stdin")));
195 pq_putemptymessage('H');
196 /* grottiness needed for old COPY OUT protocol */
198 copy_dest = COPY_OLD_FE;
205 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
206 errmsg("COPY BINARY is not supported to stdout or from stdin")));
207 pq_putemptymessage('B');
208 /* grottiness needed for old COPY OUT protocol */
210 copy_dest = COPY_OLD_FE;
215 ReceiveCopyBegin(bool binary, int natts)
217 if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
221 int16 format = (binary ? 1 : 0);
224 pq_beginmessage(&buf, 'G');
225 pq_sendbyte(&buf, format); /* overall format */
226 pq_sendint(&buf, natts, 2);
227 for (i = 0; i < natts; i++)
228 pq_sendint(&buf, format, 2); /* per-column formats */
230 copy_dest = COPY_NEW_FE;
231 copy_msgbuf = makeStringInfo();
233 else if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 2)
238 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
239 errmsg("COPY BINARY is not supported to stdout or from stdin")));
240 pq_putemptymessage('G');
241 copy_dest = COPY_OLD_FE;
248 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
249 errmsg("COPY BINARY is not supported to stdout or from stdin")));
250 pq_putemptymessage('D');
251 copy_dest = COPY_OLD_FE;
253 /* We *must* flush here to ensure FE knows it can send. */
258 SendCopyEnd(bool binary)
260 if (copy_dest == COPY_NEW_FE)
264 /* Need to flush out file trailer word */
265 CopySendEndOfRow(true);
269 /* Shouldn't have any unsent data */
270 Assert(copy_msgbuf->len == 0);
272 /* Send Copy Done message */
273 pq_putemptymessage('c');
277 /* The FE/BE protocol uses \n as newline for all platforms */
278 CopySendData("\\.\n", 3);
279 pq_endcopyout(false);
284 * CopySendData sends output data to the destination (file or frontend)
285 * CopySendString does the same for null-terminated strings
286 * CopySendChar does the same for single characters
287 * CopySendEndOfRow does the appropriate thing at end of each data row
289 * NB: no data conversion is applied by these functions
293 CopySendData(void *databuf, int datasize)
298 fwrite(databuf, datasize, 1, copy_file);
299 if (ferror(copy_file))
301 (errcode_for_file_access(),
302 errmsg("could not write to COPY file: %m")));
305 if (pq_putbytes((char *) databuf, datasize))
307 /* no hope of recovering connection sync, so FATAL */
309 (errcode(ERRCODE_CONNECTION_FAILURE),
310 errmsg("connection lost during COPY to stdout")));
314 appendBinaryStringInfo(copy_msgbuf, (char *) databuf, datasize);
320 CopySendString(const char *str)
322 CopySendData((void *) str, strlen(str));
332 CopySendEndOfRow(bool binary)
339 /* Default line termination depends on platform */
343 CopySendString("\r\n");
348 /* The FE/BE protocol uses \n as newline for all platforms */
353 /* The FE/BE protocol uses \n as newline for all platforms */
356 /* Dump the accumulated row as one CopyData message */
357 (void) pq_putmessage('d', copy_msgbuf->data, copy_msgbuf->len);
358 /* Reset copy_msgbuf to empty */
359 copy_msgbuf->len = 0;
360 copy_msgbuf->data[0] = '\0';
366 * CopyGetData reads data from the source (file or frontend)
367 * CopyGetChar does the same for single characters
369 * CopyGetEof checks if EOF was detected by previous Get operation.
371 * Note: when copying from the frontend, we expect a proper EOF mark per
372 * protocol; if the frontend simply drops the connection, we raise error.
373 * It seems unwise to allow the COPY IN to complete normally in that case.
375 * NB: no data conversion is applied by these functions
378 CopyGetData(void *databuf, int datasize)
383 fread(databuf, datasize, 1, copy_file);
388 if (pq_getbytes((char *) databuf, datasize))
390 /* Only a \. terminator is legal EOF in old protocol */
392 (errcode(ERRCODE_CONNECTION_FAILURE),
393 errmsg("unexpected EOF on client connection")));
397 while (datasize > 0 && !fe_eof)
401 while (copy_msgbuf->cursor >= copy_msgbuf->len)
403 /* Try to receive another message */
407 mtype = pq_getbyte();
410 (errcode(ERRCODE_CONNECTION_FAILURE),
411 errmsg("unexpected EOF on client connection")));
412 if (pq_getmessage(copy_msgbuf, 0))
414 (errcode(ERRCODE_CONNECTION_FAILURE),
415 errmsg("unexpected EOF on client connection")));
418 case 'd': /* CopyData */
420 case 'c': /* CopyDone */
421 /* COPY IN correctly terminated by frontend */
424 case 'f': /* CopyFail */
426 (errcode(ERRCODE_QUERY_CANCELED),
427 errmsg("COPY from stdin failed: %s",
428 pq_getmsgstring(copy_msgbuf))));
430 case 'H': /* Flush */
433 * Ignore Flush/Sync for the convenience of
434 * client libraries (such as libpq) that may
435 * send those without noticing that the command
436 * they just sent was COPY.
441 (errcode(ERRCODE_PROTOCOL_VIOLATION),
442 errmsg("unexpected message type 0x%02X during COPY from stdin",
447 avail = copy_msgbuf->len - copy_msgbuf->cursor;
448 if (avail > datasize)
450 pq_copymsgbytes(copy_msgbuf, databuf, avail);
451 databuf = (void *) ((char *) databuf + avail);
466 ch = getc(copy_file);
472 /* Only a \. terminator is legal EOF in old protocol */
474 (errcode(ERRCODE_CONNECTION_FAILURE),
475 errmsg("unexpected EOF on client connection")));
499 * CopyPeekChar reads a byte in "peekable" mode.
501 * after each call to CopyPeekChar, a call to CopyDonePeek _must_
502 * follow, unless EOF was returned.
504 * CopyDonePeek will either take the peeked char off the stream
505 * (if pickup is true) or leave it on the stream (if pickup is false).
515 ch = getc(copy_file);
521 /* Only a \. terminator is legal EOF in old protocol */
523 (errcode(ERRCODE_CONNECTION_FAILURE),
524 errmsg("unexpected EOF on client connection")));
548 CopyDonePeek(int c, bool pickup)
551 return; /* can't unget an EOF */
557 /* We don't want to pick it up - so put it back in there */
558 ungetc(c, copy_file);
560 /* If we wanted to pick it up, it's already done */
565 /* We want to pick it up */
570 * If we didn't want to pick it up, just leave it where it
577 /* We don't want to pick it up - so put it back in there */
578 copy_msgbuf->cursor--;
580 /* If we wanted to pick it up, it's already done */
587 * These functions do apply some data conversion
591 * CopySendInt32 sends an int32 in network byte order
594 CopySendInt32(int32 val)
598 buf = htonl((uint32) val);
599 CopySendData(&buf, sizeof(buf));
603 * CopyGetInt32 reads an int32 that appears in network byte order
610 CopyGetData(&buf, sizeof(buf));
611 return (int32) ntohl(buf);
615 * CopySendInt16 sends an int16 in network byte order
618 CopySendInt16(int16 val)
622 buf = htons((uint16) val);
623 CopySendData(&buf, sizeof(buf));
627 * CopyGetInt16 reads an int16 that appears in network byte order
634 CopyGetData(&buf, sizeof(buf));
635 return (int16) ntohs(buf);
640 * DoCopy executes the SQL COPY statement.
642 * Either unload or reload contents of table <relation>, depending on <from>.
643 * (<from> = TRUE means we are inserting into the table.)
645 * If <pipe> is false, transfer is between the table and the file named
646 * <filename>. Otherwise, transfer is between the table and our regular
647 * input/output stream. The latter could be either stdin/stdout or a
648 * socket, depending on whether we're running under Postmaster control.
650 * Iff <binary>, unload or reload in the binary format, as opposed to the
651 * more wasteful but more robust and portable text format.
653 * Iff <oids>, unload or reload the format that includes OID information.
654 * On input, we accept OIDs whether or not the table has an OID column,
655 * but silently drop them if it does not. On output, we report an error
656 * if the user asks for OIDs in a table that has none (not providing an
657 * OID column might seem friendlier, but could seriously confuse programs).
659 * If in the text format, delimit columns with delimiter <delim> and print
660 * NULL values as <null_print>.
662 * When loading in the text format from an input stream (as opposed to
663 * a file), recognize a "." on a line by itself as EOF. Also recognize
664 * a stream EOF. When unloading in the text format to an output stream,
665 * write a "." on a line by itself at the end of the data.
667 * Do not allow a Postgres user without superuser privilege to read from
668 * or write to a file.
670 * Do not allow the copy if user doesn't have proper permission to access
674 DoCopy(const CopyStmt *stmt)
676 RangeVar *relation = stmt->relation;
677 char *filename = stmt->filename;
678 bool is_from = stmt->is_from;
679 bool pipe = (stmt->filename == NULL);
681 List *attnamelist = stmt->attlist;
686 char *null_print = NULL;
688 AclMode required_access = (is_from ? ACL_INSERT : ACL_SELECT);
691 /* Extract options from the statement node tree */
692 foreach(option, stmt->options)
694 DefElem *defel = (DefElem *) lfirst(option);
696 if (strcmp(defel->defname, "binary") == 0)
700 (errcode(ERRCODE_SYNTAX_ERROR),
701 errmsg("conflicting or redundant options")));
702 binary = intVal(defel->arg);
704 else if (strcmp(defel->defname, "oids") == 0)
708 (errcode(ERRCODE_SYNTAX_ERROR),
709 errmsg("conflicting or redundant options")));
710 oids = intVal(defel->arg);
712 else if (strcmp(defel->defname, "delimiter") == 0)
716 (errcode(ERRCODE_SYNTAX_ERROR),
717 errmsg("conflicting or redundant options")));
718 delim = strVal(defel->arg);
720 else if (strcmp(defel->defname, "null") == 0)
724 (errcode(ERRCODE_SYNTAX_ERROR),
725 errmsg("conflicting or redundant options")));
726 null_print = strVal(defel->arg);
729 elog(ERROR, "option \"%s\" not recognized",
735 (errcode(ERRCODE_SYNTAX_ERROR),
736 errmsg("cannot specify DELIMITER in BINARY mode")));
738 if (binary && null_print)
740 (errcode(ERRCODE_SYNTAX_ERROR),
741 errmsg("cannot specify NULL in BINARY mode")));
751 * Open and lock the relation, using the appropriate lock type.
753 rel = heap_openrv(relation, (is_from ? RowExclusiveLock : AccessShareLock));
755 /* check read-only transaction */
756 if (XactReadOnly && !is_from && !isTempNamespace(RelationGetNamespace(rel)))
758 (errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
759 errmsg("transaction is read-only")));
761 /* Check permissions. */
762 aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(),
764 if (aclresult != ACLCHECK_OK)
765 aclcheck_error(aclresult, ACL_KIND_CLASS,
766 RelationGetRelationName(rel));
767 if (!pipe && !superuser())
769 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
770 errmsg("must be superuser to COPY to or from a file"),
771 errhint("Anyone can COPY to stdout or from stdin. "
772 "psql's \\copy command also works for anyone.")));
775 * Presently, only single-character delimiter strings are supported.
777 if (strlen(delim) != 1)
779 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
780 errmsg("COPY delimiter must be a single character")));
783 * Don't allow the delimiter to appear in the null string.
785 if (strchr(null_print, delim[0]) != NULL)
787 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
788 errmsg("COPY delimiter must not appear in the NULL specification")));
791 * Don't allow COPY w/ OIDs to or from a table without them
793 if (oids && !rel->rd_rel->relhasoids)
795 (errcode(ERRCODE_UNDEFINED_COLUMN),
796 errmsg("table \"%s\" does not have OIDs",
797 RelationGetRelationName(rel))));
800 * Generate or convert list of attributes to process
802 attnumlist = CopyGetAttnums(rel, attnamelist);
805 * Set up variables to avoid per-attribute overhead.
807 initStringInfo(&attribute_buf);
808 initStringInfo(&line_buf);
809 line_buf_converted = false;
811 client_encoding = pg_get_client_encoding();
812 server_encoding = GetDatabaseEncoding();
814 copy_dest = COPY_FILE; /* default */
820 { /* copy from file to database */
821 if (rel->rd_rel->relkind != RELKIND_RELATION)
823 if (rel->rd_rel->relkind == RELKIND_VIEW)
825 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
826 errmsg("cannot copy to view \"%s\"",
827 RelationGetRelationName(rel))));
828 else if (rel->rd_rel->relkind == RELKIND_SEQUENCE)
830 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
831 errmsg("cannot copy to sequence \"%s\"",
832 RelationGetRelationName(rel))));
835 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
836 errmsg("cannot copy to non-table relation \"%s\"",
837 RelationGetRelationName(rel))));
841 if (whereToSendOutput == Remote)
842 ReceiveCopyBegin(binary, length(attnumlist));
850 copy_file = AllocateFile(filename, PG_BINARY_R);
852 if (copy_file == NULL)
854 (errcode_for_file_access(),
855 errmsg("could not open file \"%s\" for reading: %m",
858 fstat(fileno(copy_file), &st);
859 if (S_ISDIR(st.st_mode))
863 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
864 errmsg("\"%s\" is a directory", filename)));
867 CopyFrom(rel, attnumlist, binary, oids, delim, null_print);
870 { /* copy from database to file */
871 if (rel->rd_rel->relkind != RELKIND_RELATION)
873 if (rel->rd_rel->relkind == RELKIND_VIEW)
875 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
876 errmsg("cannot copy from view \"%s\"",
877 RelationGetRelationName(rel))));
878 else if (rel->rd_rel->relkind == RELKIND_SEQUENCE)
880 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
881 errmsg("cannot copy from sequence \"%s\"",
882 RelationGetRelationName(rel))));
885 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
886 errmsg("cannot copy from non-table relation \"%s\"",
887 RelationGetRelationName(rel))));
891 if (whereToSendOutput == Remote)
892 SendCopyBegin(binary, length(attnumlist));
898 mode_t oumask; /* Pre-existing umask value */
902 * Prevent write to relative path ... too easy to shoot
903 * oneself in the foot by overwriting a database file ...
905 if (!is_absolute_path(filename))
907 (errcode(ERRCODE_INVALID_NAME),
908 errmsg("relative path not allowed for COPY to file")));
910 oumask = umask((mode_t) 022);
911 copy_file = AllocateFile(filename, PG_BINARY_W);
914 if (copy_file == NULL)
916 (errcode_for_file_access(),
917 errmsg("could not open file \"%s\" for writing: %m",
920 fstat(fileno(copy_file), &st);
921 if (S_ISDIR(st.st_mode))
925 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
926 errmsg("\"%s\" is a directory", filename)));
929 CopyTo(rel, attnumlist, binary, oids, delim, null_print);
934 /* we assume only the write case could fail here */
935 if (FreeFile(copy_file))
937 (errcode_for_file_access(),
938 errmsg("could not write to file \"%s\": %m",
941 else if (whereToSendOutput == Remote && !is_from)
943 pfree(attribute_buf.data);
944 pfree(line_buf.data);
947 * Close the relation. If reading, we can release the AccessShareLock
948 * we got; if writing, we should hold the lock until end of
949 * transaction to ensure that updates will be committed before lock is
952 heap_close(rel, (is_from ? NoLock : AccessShareLock));
957 * Copy from relation TO file.
960 CopyTo(Relation rel, List *attnumlist, bool binary, bool oids,
961 char *delim, char *null_print)
965 HeapScanDesc scandesc;
968 Form_pg_attribute *attr;
969 FmgrInfo *out_functions;
975 MemoryContext oldcontext;
976 MemoryContext mycontext;
978 tupDesc = rel->rd_att;
979 attr = tupDesc->attrs;
980 num_phys_attrs = tupDesc->natts;
981 attr_count = length(attnumlist);
984 * Get info about the columns we need to process.
986 * +1's here are to avoid palloc(0) in a zero-column table.
988 out_functions = (FmgrInfo *) palloc((num_phys_attrs + 1) * sizeof(FmgrInfo));
989 elements = (Oid *) palloc((num_phys_attrs + 1) * sizeof(Oid));
990 isvarlena = (bool *) palloc((num_phys_attrs + 1) * sizeof(bool));
991 foreach(cur, attnumlist)
993 int attnum = lfirsti(cur);
997 getTypeBinaryOutputInfo(attr[attnum - 1]->atttypid,
998 &out_func_oid, &elements[attnum - 1],
999 &isvarlena[attnum - 1]);
1001 getTypeOutputInfo(attr[attnum - 1]->atttypid,
1002 &out_func_oid, &elements[attnum - 1],
1003 &isvarlena[attnum - 1]);
1004 fmgr_info(out_func_oid, &out_functions[attnum - 1]);
1008 * Create a temporary memory context that we can reset once per row to
1009 * recover palloc'd memory. This avoids any problems with leaks
1010 * inside datatype output routines, and should be faster than retail
1011 * pfree's anyway. (We don't need a whole econtext as CopyFrom does.)
1013 mycontext = AllocSetContextCreate(CurrentMemoryContext,
1015 ALLOCSET_DEFAULT_MINSIZE,
1016 ALLOCSET_DEFAULT_INITSIZE,
1017 ALLOCSET_DEFAULT_MAXSIZE);
1021 /* Generate header for a binary copy */
1025 CopySendData((char *) BinarySignature, 11);
1031 /* No header extension */
1038 * For non-binary copy, we need to convert null_print to client
1039 * encoding, because it will be sent directly with CopySendString.
1041 if (server_encoding != client_encoding)
1042 null_print = (char *)
1043 pg_server_to_client((unsigned char *) null_print,
1044 strlen(null_print));
1047 mySnapshot = CopyQuerySnapshot();
1049 scandesc = heap_beginscan(rel, mySnapshot, 0, NULL);
1051 while ((tuple = heap_getnext(scandesc, ForwardScanDirection)) != NULL)
1053 bool need_delim = false;
1055 CHECK_FOR_INTERRUPTS();
1057 MemoryContextReset(mycontext);
1058 oldcontext = MemoryContextSwitchTo(mycontext);
1062 /* Binary per-tuple header */
1063 CopySendInt16(attr_count);
1064 /* Send OID if wanted --- note attr_count doesn't include it */
1067 Oid oid = HeapTupleGetOid(tuple);
1069 /* Hack --- assume Oid is same size as int32 */
1070 CopySendInt32(sizeof(int32));
1076 /* Text format has no per-tuple header, but send OID if wanted */
1079 string = DatumGetCString(DirectFunctionCall1(oidout,
1080 ObjectIdGetDatum(HeapTupleGetOid(tuple))));
1081 CopySendString(string);
1086 foreach(cur, attnumlist)
1088 int attnum = lfirsti(cur);
1092 value = heap_getattr(tuple, attnum, tupDesc, &isnull);
1097 CopySendChar(delim[0]);
1104 CopySendString(null_print); /* null indicator */
1106 CopySendInt32(-1); /* null marker */
1112 string = DatumGetCString(FunctionCall3(&out_functions[attnum - 1],
1114 ObjectIdGetDatum(elements[attnum - 1]),
1115 Int32GetDatum(attr[attnum - 1]->atttypmod)));
1116 CopyAttributeOut(string, delim);
1122 outputbytes = DatumGetByteaP(FunctionCall2(&out_functions[attnum - 1],
1124 ObjectIdGetDatum(elements[attnum - 1])));
1125 /* We assume the result will not have been toasted */
1126 CopySendInt32(VARSIZE(outputbytes) - VARHDRSZ);
1127 CopySendData(VARDATA(outputbytes),
1128 VARSIZE(outputbytes) - VARHDRSZ);
1133 CopySendEndOfRow(binary);
1135 MemoryContextSwitchTo(oldcontext);
1138 heap_endscan(scandesc);
1142 /* Generate trailer for a binary copy */
1146 MemoryContextDelete(mycontext);
1148 pfree(out_functions);
1155 * error context callback for COPY FROM
1158 copy_in_error_callback(void *arg)
1162 /* can't usefully display the data */
1164 errcontext("COPY %s, line %d, column %s",
1165 copy_relname, copy_lineno, copy_attname);
1167 errcontext("COPY %s, line %d", copy_relname, copy_lineno);
1173 /* error is relevant to a particular column */
1174 limit_printout_length(&attribute_buf);
1175 errcontext("COPY %s, line %d, column %s: \"%s\"",
1176 copy_relname, copy_lineno, copy_attname,
1177 attribute_buf.data);
1181 /* error is relevant to a particular line */
1182 if (!line_buf_converted)
1184 /* didn't convert the encoding yet... */
1185 line_buf_converted = true;
1186 if (client_encoding != server_encoding)
1190 cvt = (char *) pg_client_to_server((unsigned char *) line_buf.data,
1192 if (cvt != line_buf.data)
1194 /* transfer converted data back to line_buf */
1196 line_buf.data[0] = '\0';
1197 appendBinaryStringInfo(&line_buf, cvt, strlen(cvt));
1201 limit_printout_length(&line_buf);
1202 errcontext("COPY %s, line %d: \"%s\"",
1203 copy_relname, copy_lineno,
1210 * Make sure we don't print an unreasonable amount of COPY data in a message.
1212 * It would seem a lot easier to just use the sprintf "precision" limit to
1213 * truncate the string. However, some versions of glibc have a bug/misfeature
1214 * that vsnprintf will always fail (return -1) if it is asked to truncate
1215 * a string that contains invalid byte sequences for the current encoding.
1216 * So, do our own truncation. We assume we can alter the StringInfo buffer
1217 * holding the input data.
1220 limit_printout_length(StringInfo buf)
1222 #define MAX_COPY_DATA_DISPLAY 100
1226 /* Fast path if definitely okay */
1227 if (buf->len <= MAX_COPY_DATA_DISPLAY)
1230 /* Apply encoding-dependent truncation */
1231 len = pg_mbcliplen(buf->data, buf->len, MAX_COPY_DATA_DISPLAY);
1232 if (buf->len <= len)
1233 return; /* no need to truncate */
1235 buf->data[len] = '\0';
1237 /* Add "..." to show we truncated the input */
1238 appendStringInfoString(buf, "...");
1242 * Copy FROM file to relation.
1245 CopyFrom(Relation rel, List *attnumlist, bool binary, bool oids,
1246 char *delim, char *null_print)
1250 Form_pg_attribute *attr;
1251 AttrNumber num_phys_attrs,
1254 FmgrInfo *in_functions;
1255 FmgrInfo oid_in_function;
1258 ExprState **constraintexprs;
1259 bool hasConstraints = false;
1267 ResultRelInfo *resultRelInfo;
1268 EState *estate = CreateExecutorState(); /* for ExecConstraints() */
1269 TupleTable tupleTable;
1270 TupleTableSlot *slot;
1273 ExprState **defexprs; /* array of default att expressions */
1274 ExprContext *econtext; /* used for ExecEvalExpr for default atts */
1275 MemoryContext oldcontext = CurrentMemoryContext;
1276 ErrorContextCallback errcontext;
1278 tupDesc = RelationGetDescr(rel);
1279 attr = tupDesc->attrs;
1280 num_phys_attrs = tupDesc->natts;
1281 attr_count = length(attnumlist);
1285 * We need a ResultRelInfo so we can use the regular executor's
1286 * index-entry-making machinery. (There used to be a huge amount of
1287 * code here that basically duplicated execUtils.c ...)
1289 resultRelInfo = makeNode(ResultRelInfo);
1290 resultRelInfo->ri_RangeTableIndex = 1; /* dummy */
1291 resultRelInfo->ri_RelationDesc = rel;
1292 resultRelInfo->ri_TrigDesc = CopyTriggerDesc(rel->trigdesc);
1294 ExecOpenIndices(resultRelInfo);
1296 estate->es_result_relations = resultRelInfo;
1297 estate->es_num_result_relations = 1;
1298 estate->es_result_relation_info = resultRelInfo;
1300 /* Set up a dummy tuple table too */
1301 tupleTable = ExecCreateTupleTable(1);
1302 slot = ExecAllocTableSlot(tupleTable);
1303 ExecSetSlotDescriptor(slot, tupDesc, false);
1305 econtext = GetPerTupleExprContext(estate);
1308 * Pick up the required catalog information for each attribute in the
1309 * relation, including the input function, the element type (to pass
1310 * to the input function), and info about defaults and constraints.
1311 * (Which input function we use depends on text/binary format choice.)
1312 * +1's here are to avoid palloc(0) in a zero-column table.
1314 in_functions = (FmgrInfo *) palloc((num_phys_attrs + 1) * sizeof(FmgrInfo));
1315 elements = (Oid *) palloc((num_phys_attrs + 1) * sizeof(Oid));
1316 defmap = (int *) palloc((num_phys_attrs + 1) * sizeof(int));
1317 defexprs = (ExprState **) palloc((num_phys_attrs + 1) * sizeof(ExprState *));
1318 constraintexprs = (ExprState **) palloc0((num_phys_attrs + 1) * sizeof(ExprState *));
1320 for (i = 0; i < num_phys_attrs; i++)
1322 /* We don't need info for dropped attributes */
1323 if (attr[i]->attisdropped)
1326 /* Fetch the input function and typelem info */
1328 getTypeBinaryInputInfo(attr[i]->atttypid,
1329 &in_func_oid, &elements[i]);
1331 getTypeInputInfo(attr[i]->atttypid,
1332 &in_func_oid, &elements[i]);
1333 fmgr_info(in_func_oid, &in_functions[i]);
1335 /* Get default info if needed */
1336 if (!intMember(i + 1, attnumlist))
1338 /* attribute is NOT to be copied from input */
1339 /* use default value if one exists */
1340 Node *defexpr = build_column_default(rel, i + 1);
1342 if (defexpr != NULL)
1344 defexprs[num_defaults] = ExecPrepareExpr((Expr *) defexpr,
1346 defmap[num_defaults] = i;
1351 /* If it's a domain type, set up to check domain constraints */
1352 if (get_typtype(attr[i]->atttypid) == 'd')
1358 * Easiest way to do this is to use parse_coerce.c to set up
1359 * an expression that checks the constraints. (At present,
1360 * the expression might contain a length-coercion-function
1361 * call and/or CoerceToDomain nodes.) The bottom of the
1362 * expression is a Param node so that we can fill in the
1363 * actual datum during the data input loop.
1365 prm = makeNode(Param);
1366 prm->paramkind = PARAM_EXEC;
1368 prm->paramtype = getBaseType(attr[i]->atttypid);
1370 node = coerce_to_domain((Node *) prm,
1373 COERCE_IMPLICIT_CAST);
1375 constraintexprs[i] = ExecPrepareExpr((Expr *) node,
1377 hasConstraints = true;
1382 * Check BEFORE STATEMENT insertion triggers. It's debateable whether
1383 * we should do this for COPY, since it's not really an "INSERT"
1384 * statement as such. However, executing these triggers maintains
1385 * consistency with the EACH ROW triggers that we already fire on
1388 ExecBSInsertTriggers(estate, resultRelInfo);
1392 file_has_oids = oids; /* must rely on user to tell us this... */
1396 /* Read and verify binary header */
1401 CopyGetData(readSig, 11);
1402 if (CopyGetEof() || memcmp(readSig, BinarySignature, 11) != 0)
1404 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1405 errmsg("COPY file signature not recognized")));
1407 tmp = CopyGetInt32();
1410 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1411 errmsg("invalid COPY file header (missing flags)")));
1412 file_has_oids = (tmp & (1 << 16)) != 0;
1414 if ((tmp >> 16) != 0)
1416 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1417 errmsg("unrecognized critical flags in COPY file header")));
1418 /* Header extension length */
1419 tmp = CopyGetInt32();
1420 if (CopyGetEof() || tmp < 0)
1422 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1423 errmsg("invalid COPY file header (missing length)")));
1424 /* Skip extension header, if present */
1427 CopyGetData(readSig, 1);
1430 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1431 errmsg("invalid COPY file header (wrong length)")));
1435 if (file_has_oids && binary)
1437 getTypeBinaryInputInfo(OIDOID,
1438 &in_func_oid, &oid_in_element);
1439 fmgr_info(in_func_oid, &oid_in_function);
1442 values = (Datum *) palloc((num_phys_attrs + 1) * sizeof(Datum));
1443 nulls = (char *) palloc((num_phys_attrs + 1) * sizeof(char));
1445 /* Make room for a PARAM_EXEC value for domain constraint checks */
1447 econtext->ecxt_param_exec_vals = (ParamExecData *)
1448 palloc0(sizeof(ParamExecData));
1450 /* Initialize static variables */
1452 eol_type = EOL_UNKNOWN;
1453 copy_binary = binary;
1454 copy_relname = RelationGetRelationName(rel);
1456 copy_attname = NULL;
1458 /* Set up callback to identify error line number */
1459 errcontext.callback = copy_in_error_callback;
1460 errcontext.arg = NULL;
1461 errcontext.previous = error_context_stack;
1462 error_context_stack = &errcontext;
1467 Oid loaded_oid = InvalidOid;
1469 CHECK_FOR_INTERRUPTS();
1473 /* Reset the per-tuple exprcontext */
1474 ResetPerTupleExprContext(estate);
1476 /* Switch into its memory context */
1477 MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
1479 /* Initialize all values for row to NULL */
1480 MemSet(values, 0, num_phys_attrs * sizeof(Datum));
1481 MemSet(nulls, 'n', num_phys_attrs * sizeof(char));
1485 CopyReadResult result = NORMAL_ATTR;
1488 /* Actually read the line into memory here */
1489 done = CopyReadLine();
1492 * EOF at start of line means we're done. If we see EOF
1493 * after some characters, we act as though it was newline
1494 * followed by EOF, ie, process the line and then exit loop
1495 * on next iteration.
1497 if (done && line_buf.len == 0)
1502 string = CopyReadAttribute(delim, null_print,
1507 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1508 errmsg("null OID in COPY data")));
1511 copy_attname = "oid";
1512 loaded_oid = DatumGetObjectId(DirectFunctionCall1(oidin,
1513 CStringGetDatum(string)));
1514 if (loaded_oid == InvalidOid)
1516 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1517 errmsg("invalid OID in COPY data")));
1518 copy_attname = NULL;
1523 * Loop to read the user attributes on the line.
1525 foreach(cur, attnumlist)
1527 int attnum = lfirsti(cur);
1531 * If prior attr on this line was ended by newline,
1534 if (result != NORMAL_ATTR)
1536 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1537 errmsg("missing data for column \"%s\"",
1538 NameStr(attr[m]->attname))));
1540 string = CopyReadAttribute(delim, null_print,
1545 /* we read an SQL NULL, no need to do anything */
1549 copy_attname = NameStr(attr[m]->attname);
1550 values[m] = FunctionCall3(&in_functions[m],
1551 CStringGetDatum(string),
1552 ObjectIdGetDatum(elements[m]),
1553 Int32GetDatum(attr[m]->atttypmod));
1555 copy_attname = NULL;
1560 * Complain if there are more fields on the input line.
1562 * Special case: if we're reading a zero-column table, we won't
1563 * yet have called CopyReadAttribute() at all; so no error if
1566 if (result == NORMAL_ATTR && line_buf.len != 0)
1568 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1569 errmsg("extra data after last expected column")));
1576 fld_count = CopyGetInt16();
1577 if (CopyGetEof() || fld_count == -1)
1583 if (fld_count != attr_count)
1585 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1586 errmsg("row field count is %d, expected %d",
1587 (int) fld_count, attr_count)));
1591 copy_attname = "oid";
1593 DatumGetObjectId(CopyReadBinaryAttribute(0,
1597 if (isnull || loaded_oid == InvalidOid)
1599 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1600 errmsg("invalid OID in COPY data")));
1601 copy_attname = NULL;
1605 foreach(cur, attnumlist)
1607 int attnum = lfirsti(cur);
1610 copy_attname = NameStr(attr[m]->attname);
1612 values[m] = CopyReadBinaryAttribute(i,
1616 nulls[m] = isnull ? 'n' : ' ';
1617 copy_attname = NULL;
1622 * Now compute and insert any defaults available for the columns
1623 * not provided by the input data. Anything not processed here or
1624 * above will remain NULL.
1626 for (i = 0; i < num_defaults; i++)
1628 values[defmap[i]] = ExecEvalExpr(defexprs[i], econtext,
1631 nulls[defmap[i]] = ' ';
1635 * Next apply any domain constraints
1639 ParamExecData *prmdata = &econtext->ecxt_param_exec_vals[0];
1641 for (i = 0; i < num_phys_attrs; i++)
1643 ExprState *exprstate = constraintexprs[i];
1645 if (exprstate == NULL)
1646 continue; /* no constraint for this attr */
1648 /* Insert current row's value into the Param value */
1649 prmdata->value = values[i];
1650 prmdata->isnull = (nulls[i] == 'n');
1653 * Execute the constraint expression. Allow the
1654 * expression to replace the value (consider e.g. a
1655 * timestamp precision restriction).
1657 values[i] = ExecEvalExpr(exprstate, econtext,
1659 nulls[i] = isnull ? 'n' : ' ';
1664 * And now we can form the input tuple.
1666 tuple = heap_formtuple(tupDesc, values, nulls);
1668 if (oids && file_has_oids)
1669 HeapTupleSetOid(tuple, loaded_oid);
1672 * Triggers and stuff need to be invoked in query context.
1674 MemoryContextSwitchTo(oldcontext);
1678 /* BEFORE ROW INSERT Triggers */
1679 if (resultRelInfo->ri_TrigDesc &&
1680 resultRelInfo->ri_TrigDesc->n_before_row[TRIGGER_EVENT_INSERT] > 0)
1684 newtuple = ExecBRInsertTriggers(estate, resultRelInfo, tuple);
1686 if (newtuple == NULL) /* "do nothing" */
1688 else if (newtuple != tuple) /* modified by Trigger(s) */
1690 heap_freetuple(tuple);
1697 /* Place tuple in tuple slot */
1698 ExecStoreTuple(tuple, slot, InvalidBuffer, false);
1701 * Check the constraints of the tuple
1703 if (rel->rd_att->constr)
1704 ExecConstraints(resultRelInfo, slot, estate);
1707 * OK, store the tuple and create index entries for it
1709 simple_heap_insert(rel, tuple);
1711 if (resultRelInfo->ri_NumIndices > 0)
1712 ExecInsertIndexTuples(slot, &(tuple->t_self), estate, false);
1714 /* AFTER ROW INSERT Triggers */
1715 ExecARInsertTriggers(estate, resultRelInfo, tuple);
1722 error_context_stack = errcontext.previous;
1724 MemoryContextSwitchTo(oldcontext);
1727 * Execute AFTER STATEMENT insertion triggers
1729 ExecASInsertTriggers(estate, resultRelInfo);
1736 pfree(in_functions);
1740 ExecDropTupleTable(tupleTable, true);
1742 ExecCloseIndices(resultRelInfo);
1744 FreeExecutorState(estate);
1749 * Read the next input line and stash it in line_buf, with conversion to
1752 * Result is true if read was terminated by EOF, false if terminated
1759 bool change_encoding = (client_encoding != server_encoding);
1768 /* reset line_buf to empty */
1770 line_buf.data[0] = '\0';
1771 line_buf.cursor = 0;
1773 /* mark that encoding conversion hasn't occurred yet */
1774 line_buf_converted = false;
1776 /* set default status */
1780 * In this loop we only care for detecting newlines (\r and/or \n)
1781 * and the end-of-copy marker (\.). For backwards compatibility
1782 * we allow backslashes to escape newline characters. Backslashes
1783 * other than the end marker get put into the line_buf, since
1784 * CopyReadAttribute does its own escape processing. These four
1785 * characters, and only these four, are assumed the same in frontend
1786 * and backend encodings. We do not assume that second and later bytes
1787 * of a frontend multibyte character couldn't look like ASCII characters.
1799 if (eol_type == EOL_NL)
1801 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1802 errmsg("literal carriage return found in data"),
1803 errhint("Use \"\\r\" to represent carriage return.")));
1804 /* Check for \r\n on first line, _and_ handle \r\n. */
1805 if (eol_type == EOL_UNKNOWN || eol_type == EOL_CRNL)
1807 int c2 = CopyPeekChar();
1811 CopyDonePeek(c2, true); /* eat newline */
1812 eol_type = EOL_CRNL;
1816 /* found \r, but no \n */
1817 if (eol_type == EOL_CRNL)
1819 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1820 errmsg("literal carriage return found in data"),
1821 errhint("Use \"\\r\" to represent carriage return.")));
1824 * if we got here, it is the first line and we didn't
1825 * get \n, so put it back
1827 CopyDonePeek(c2, false);
1835 if (eol_type == EOL_CR || eol_type == EOL_CRNL)
1837 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1838 errmsg("literal newline found in data"),
1839 errhint("Use \"\\n\" to represent newline.")));
1853 if (eol_type == EOL_CRNL)
1858 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1859 errmsg("end-of-copy marker does not match previous newline style")));
1862 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1863 errmsg("end-of-copy marker corrupt")));
1866 if (c != '\r' && c != '\n')
1868 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1869 errmsg("end-of-copy marker corrupt")));
1870 if ((eol_type == EOL_NL && c != '\n') ||
1871 (eol_type == EOL_CRNL && c != '\n') ||
1872 (eol_type == EOL_CR && c != '\r'))
1874 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1875 errmsg("end-of-copy marker does not match previous newline style")));
1878 * In protocol version 3, we should ignore anything
1879 * after \. up to the protocol end of copy data. (XXX
1880 * maybe better not to treat \. as special?)
1882 if (copy_dest == COPY_NEW_FE)
1887 result = true; /* report EOF */
1890 /* not EOF mark, so emit \ and following char literally */
1891 appendStringInfoCharMacro(&line_buf, '\\');
1894 appendStringInfoCharMacro(&line_buf, c);
1897 * When client encoding != server, must be careful to read the
1898 * extra bytes of a multibyte character exactly, since the encoding
1899 * might not ensure they don't look like ASCII. When the encodings
1900 * are the same, we need not do this, since no server encoding we
1901 * use has ASCII-like following bytes.
1903 if (change_encoding)
1906 mblen = pg_encoding_mblen(client_encoding, s);
1907 for (j = 1; j < mblen; j++)
1915 appendStringInfoCharMacro(&line_buf, c);
1918 break; /* out of outer loop */
1920 } /* end of outer loop */
1923 * Done reading the line. Convert it to server encoding.
1925 * Note: set line_buf_converted to true *before* attempting conversion;
1926 * this prevents infinite recursion during error reporting should
1927 * pg_client_to_server() issue an error, due to copy_in_error_callback
1928 * again attempting the same conversion. We'll end up issuing the message
1929 * without conversion, which is bad but better than nothing ...
1931 line_buf_converted = true;
1933 if (change_encoding)
1935 cvt = (char *) pg_client_to_server((unsigned char *) line_buf.data,
1937 if (cvt != line_buf.data)
1939 /* transfer converted data back to line_buf */
1941 line_buf.data[0] = '\0';
1942 appendBinaryStringInfo(&line_buf, cvt, strlen(cvt));
1950 * Read the value of a single attribute, performing de-escaping as needed.
1952 * delim is the column delimiter string (must be just one byte for now).
1953 * null_print is the null marker string. Note that this is compared to
1954 * the pre-de-escaped input string.
1956 * *result is set to indicate what terminated the read:
1957 * NORMAL_ATTR: column delimiter
1958 * END_OF_LINE: end of line
1959 * In either case, the string read up to the terminator is returned.
1961 * *isnull is set true or false depending on whether the input matched
1962 * the null marker. Note that the caller cannot check this since the
1963 * returned string will be the post-de-escaping equivalent, which may
1964 * look the same as some valid data string.
1968 CopyReadAttribute(const char *delim, const char *null_print,
1969 CopyReadResult *result, bool *isnull)
1972 char delimc = delim[0];
1973 int start_cursor = line_buf.cursor;
1977 /* reset attribute_buf to empty */
1978 attribute_buf.len = 0;
1979 attribute_buf.data[0] = '\0';
1981 /* set default status */
1982 *result = END_OF_LINE;
1986 end_cursor = line_buf.cursor;
1987 if (line_buf.cursor >= line_buf.len)
1989 c = line_buf.data[line_buf.cursor++];
1992 *result = NORMAL_ATTR;
1997 if (line_buf.cursor >= line_buf.len)
1999 c = line_buf.data[line_buf.cursor++];
2014 if (line_buf.cursor < line_buf.len)
2016 c = line_buf.data[line_buf.cursor];
2020 val = (val << 3) + OCTVALUE(c);
2021 if (line_buf.cursor < line_buf.len)
2023 c = line_buf.data[line_buf.cursor];
2027 val = (val << 3) + OCTVALUE(c);
2054 * in all other cases, take the char after '\' literally
2058 appendStringInfoCharMacro(&attribute_buf, c);
2061 /* check whether raw input matched null marker */
2062 input_len = end_cursor - start_cursor;
2063 if (input_len == strlen(null_print) &&
2064 strncmp(&line_buf.data[start_cursor], null_print, input_len) == 0)
2069 return attribute_buf.data;
2073 * Read a binary attribute
2076 CopyReadBinaryAttribute(int column_no, FmgrInfo *flinfo, Oid typelem,
2082 fld_size = CopyGetInt32();
2085 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2086 errmsg("unexpected EOF in COPY data")));
2094 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2095 errmsg("invalid field size")));
2097 /* reset attribute_buf to empty, and load raw data in it */
2098 attribute_buf.len = 0;
2099 attribute_buf.data[0] = '\0';
2100 attribute_buf.cursor = 0;
2102 enlargeStringInfo(&attribute_buf, fld_size);
2104 CopyGetData(attribute_buf.data, fld_size);
2107 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2108 errmsg("unexpected EOF in COPY data")));
2110 attribute_buf.len = fld_size;
2111 attribute_buf.data[fld_size] = '\0';
2113 /* Call the column type's binary input converter */
2114 result = FunctionCall2(flinfo,
2115 PointerGetDatum(&attribute_buf),
2116 ObjectIdGetDatum(typelem));
2118 /* Trouble if it didn't eat the whole buffer */
2119 if (attribute_buf.cursor != attribute_buf.len)
2121 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
2122 errmsg("incorrect binary data format")));
2129 * Send text representation of one attribute, with conversion and escaping
2132 CopyAttributeOut(char *server_string, char *delim)
2136 char delimc = delim[0];
2141 same_encoding = (server_encoding == client_encoding);
2143 string = (char *) pg_server_to_client((unsigned char *) server_string,
2144 strlen(server_string));
2146 string = server_string;
2148 for (; (c = *string) != '\0'; string += mblen)
2155 CopySendString("\\b");
2158 CopySendString("\\f");
2161 CopySendString("\\n");
2164 CopySendString("\\r");
2167 CopySendString("\\t");
2170 CopySendString("\\v");
2173 CopySendString("\\\\");
2181 * We can skip pg_encoding_mblen() overhead when encoding
2182 * is same, because in valid backend encodings, extra
2183 * bytes of a multibyte character never look like ASCII.
2187 /* send additional bytes of the char, if any */
2188 mblen = pg_encoding_mblen(client_encoding, string);
2189 for (i = 1; i < mblen; i++)
2190 CopySendChar(string[i]);
2198 * CopyGetAttnums - build an integer list of attnums to be copied
2200 * The input attnamelist is either the user-specified column list,
2201 * or NIL if there was none (in which case we want all the non-dropped
2205 CopyGetAttnums(Relation rel, List *attnamelist)
2207 List *attnums = NIL;
2209 if (attnamelist == NIL)
2211 /* Generate default column list */
2212 TupleDesc tupDesc = RelationGetDescr(rel);
2213 Form_pg_attribute *attr = tupDesc->attrs;
2214 int attr_count = tupDesc->natts;
2217 for (i = 0; i < attr_count; i++)
2219 if (attr[i]->attisdropped)
2221 attnums = lappendi(attnums, i + 1);
2226 /* Validate the user-supplied list and extract attnums */
2229 foreach(l, attnamelist)
2231 char *name = strVal(lfirst(l));
2234 /* Lookup column name, ereport on failure */
2235 /* Note we disallow system columns here */
2236 attnum = attnameAttNum(rel, name, false);
2237 /* Check for duplicates */
2238 if (intMember(attnum, attnums))
2240 (errcode(ERRCODE_DUPLICATE_COLUMN),
2241 errmsg("column \"%s\" specified more than once",
2243 attnums = lappendi(attnums, attnum);