34 #define ISOCTAL(c) (((c) >= '0') && ((c) <= '7')) 35 #define OCTVALUE(c) ((c) - '0') 53 #define IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(extralen) \ 56 if (raw_buf_ptr + (extralen) >= copy_buf_len && !hit_eof) \ 58 raw_buf_ptr = prev_raw_ptr; \ 65 #define IF_NEED_REFILL_AND_EOF_BREAK(extralen) \ 68 if (raw_buf_ptr + (extralen) >= copy_buf_len && hit_eof) \ 71 raw_buf_ptr = copy_buf_len; \ 82 #define REFILL_LINEBUF \ 85 if (raw_buf_ptr > cstate->raw_buf_index) \ 87 appendBinaryStringInfo(&cstate->line_buf, \ 88 cstate->raw_buf + cstate->raw_buf_index, \ 89 raw_buf_ptr - cstate->raw_buf_index); \ 90 cstate->raw_buf_index = raw_buf_ptr; \ 95 #define NO_END_OF_COPY_GOTO \ 98 raw_buf_ptr = prev_raw_ptr + 1; \ 99 goto not_end_of_copy; \ 118 int minread,
int maxread);
138 for (i = 0; i < natts; i++)
149 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
150 errmsg(
"COPY BINARY is not supported to stdout or from stdin")));
170 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
171 errmsg(
"COPY file signature not recognized")));
175 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
176 errmsg(
"invalid COPY file header (missing flags)")));
177 if ((tmp & (1 << 16)) != 0)
179 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
180 errmsg(
"invalid COPY file header (WITH OIDS)")));
182 if ((tmp >> 16) != 0)
184 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
185 errmsg(
"unrecognized critical flags in COPY file header")));
190 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
191 errmsg(
"invalid COPY file header (missing length)")));
197 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
198 errmsg(
"invalid COPY file header (wrong length)")));
223 bytesread = fread(databuf, 1, maxread, cstate->
copy_file);
227 errmsg(
"could not read from COPY file: %m")));
244 (
errcode(ERRCODE_CONNECTION_FAILURE),
245 errmsg(
"unexpected EOF on client connection with an open transaction")));
250 while (maxread > 0 && bytesread < minread && !cstate->reached_eof)
265 (
errcode(ERRCODE_CONNECTION_FAILURE),
266 errmsg(
"unexpected EOF on client connection with an open transaction")));
269 (
errcode(ERRCODE_CONNECTION_FAILURE),
270 errmsg(
"unexpected EOF on client connection with an open transaction")));
282 (
errcode(ERRCODE_QUERY_CANCELED),
283 errmsg(
"COPY from stdin failed: %s",
298 (
errcode(ERRCODE_PROTOCOL_VIOLATION),
299 errmsg(
"unexpected message type 0x%02X during COPY from stdin",
308 databuf = (
void *) ((
char *) databuf + avail);
386 cstate->
raw_buf[nbytes] =
'\0';
391 return (inbytes > 0);
404 int copied_bytes = 0;
411 copied_bytes = nbytes;
435 copied_bytes += copy_bytes;
436 }
while (copied_bytes < nbytes);
515 int *defmap = cstate->
defmap;
519 num_phys_attrs = tupDesc->
natts;
524 MemSet(nulls,
true, num_phys_attrs *
sizeof(
bool));
528 char **field_strings;
539 if (attr_count > 0 && fldct > attr_count)
541 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
542 errmsg(
"extra data after last expected column")));
553 if (fieldno >= fldct)
555 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
556 errmsg(
"missing data for column \"%s\"",
558 string = field_strings[fieldno++];
569 if (
string == NULL &&
603 Assert(fieldno == attr_count);
638 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
639 errmsg(
"received copy data after EOF marker")));
643 if (fld_count != attr_count)
645 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
646 errmsg(
"row field count is %d, expected %d",
647 (
int) fld_count, attr_count)));
670 for (i = 0; i < num_defaults; i++)
789 bool need_data =
false;
790 bool hit_eof =
false;
795 bool first_char_in_line =
true;
796 bool in_quote =
false,
797 last_was_esc =
false;
806 if (quotec == escapec)
833 copy_raw_buf = cstate->
raw_buf;
852 if (raw_buf_ptr >= copy_buf_len || need_data)
869 if (copy_buf_len <= 0)
878 prev_raw_ptr = raw_buf_ptr;
879 c = copy_raw_buf[raw_buf_ptr++];
892 if (c ==
'\\' || c ==
'\r')
905 if (in_quote && c == escapec)
906 last_was_esc = !last_was_esc;
907 if (c == quotec && !last_was_esc)
908 in_quote = !in_quote;
910 last_was_esc =
false;
938 c = copy_raw_buf[raw_buf_ptr];
950 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
952 errmsg(
"literal carriage return found in data") :
953 errmsg(
"unquoted carriage return found in data"),
955 errhint(
"Use \"\\r\" to represent carriage return.") :
956 errhint(
"Use quoted CSV field to represent carriage return.")));
967 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
969 errmsg(
"literal carriage return found in data") :
970 errmsg(
"unquoted carriage return found in data"),
972 errhint(
"Use \"\\r\" to represent carriage return.") :
973 errhint(
"Use quoted CSV field to represent carriage return.")));
983 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
985 errmsg(
"literal newline found in data") :
986 errmsg(
"unquoted newline found in data"),
988 errhint(
"Use \"\\n\" to represent newline.") :
989 errhint(
"Use quoted CSV field to represent newline.")));
999 if (c ==
'\\' && (!cstate->
opts.
csv_mode || first_char_in_line))
1012 c2 = copy_raw_buf[raw_buf_ptr];
1028 c2 = copy_raw_buf[raw_buf_ptr++];
1034 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1035 errmsg(
"end-of-copy marker does not match previous newline style")));
1039 else if (c2 !=
'\r')
1043 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1044 errmsg(
"end-of-copy marker corrupt")));
1053 c2 = copy_raw_buf[raw_buf_ptr++];
1055 if (c2 !=
'\r' && c2 !=
'\n')
1059 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1060 errmsg(
"end-of-copy marker corrupt")));
1070 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1071 errmsg(
"end-of-copy marker does not match previous newline style")));
1131 raw_buf_ptr += mblen - 1;
1133 first_char_in_line =
false;
1150 if (isdigit((
unsigned char) hex))
1153 return tolower((
unsigned char) hex) -
'a' + 10;
1192 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1193 errmsg(
"extra data after last expected column")));
1218 bool found_delim =
false;
1222 bool saw_non_ascii =
false;
1233 start_ptr = cur_ptr;
1252 if (cur_ptr >= line_end_ptr)
1262 if (cur_ptr >= line_end_ptr)
1280 if (cur_ptr < line_end_ptr)
1287 if (cur_ptr < line_end_ptr)
1300 saw_non_ascii =
true;
1305 if (cur_ptr < line_end_ptr)
1307 char hexchar = *cur_ptr;
1309 if (isxdigit((
unsigned char) hexchar))
1314 if (cur_ptr < line_end_ptr)
1317 if (isxdigit((
unsigned char) hexchar))
1325 saw_non_ascii =
true;
1360 input_len = end_ptr - start_ptr;
1381 *output_ptr++ =
'\0';
1391 Assert(*output_ptr ==
'\0');
1392 cstate->attribute_buf.len = (output_ptr - cstate->attribute_buf.data);
1422 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1423 errmsg(
"extra data after last expected column")));
1448 bool found_delim =
false;
1449 bool saw_quote =
false;
1463 start_ptr = cur_ptr;
1481 if (cur_ptr >= line_end_ptr)
1504 if (cur_ptr >= line_end_ptr)
1506 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1507 errmsg(
"unterminated CSV quoted field")));
1518 if (cur_ptr < line_end_ptr)
1520 char nextc = *cur_ptr;
1522 if (nextc == escapec || nextc == quotec)
1524 *output_ptr++ = nextc;
1546 *output_ptr++ =
'\0';
1549 input_len = end_ptr - start_ptr;
1562 Assert(*output_ptr ==
'\0');
1582 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1583 errmsg(
"unexpected EOF in COPY data")));
1591 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1592 errmsg(
"invalid field size")));
1599 fld_size) != fld_size)
1601 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1602 errmsg(
"unexpected EOF in COPY data")));
1609 typioparam, typmod);
1614 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1615 errmsg(
"incorrect binary data format")));
copy_data_source_cb data_source_cb
void ReceiveCopyBegin(CopyFromState cstate)
#define HOLD_CANCEL_INTERRUPTS()
bool NextCopyFromRawFields(CopyFromState cstate, char ***fields, int *nfields)
bool NextCopyFrom(CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls)
int errhint(const char *fmt,...)
#define RAW_BUF_BYTES(cstate)
StringInfoData attribute_buf
#define RelationGetDescr(relation)
#define TupleDescAttr(tupdesc, i)
void pgstat_progress_update_param(int index, int64 val)
MemoryContext ecxt_per_tuple_memory
StringInfo makeStringInfo(void)
static const char BinarySignature[11]
int errcode(int sqlerrcode)
static bool CopyGetInt16(CopyFromState cstate, int16 *val)
#define MemSet(start, val, len)
static bool CopyReadLine(CopyFromState cstate)
#define PG_PROTOCOL_MAJOR(v)
static bool CopyGetInt32(CopyFromState cstate, int32 *val)
static int CopyReadBinaryData(CopyFromState cstate, char *dest, int nbytes)
static int CopyGetData(CopyFromState cstate, void *databuf, int minread, int maxread)
void pfree(void *pointer)
#define IS_HIGHBIT_SET(ch)
void pq_startmsgread(void)
Datum ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf, Oid typioparam, int32 typmod)
int pq_getbytes(char *s, size_t len)
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
int errcode_for_file_access(void)
int pg_encoding_mblen(int encoding, const char *mbstr)
void enlargeStringInfo(StringInfo str, int needed)
FormData_pg_attribute * Form_pg_attribute
void resetStringInfo(StringInfo str)
#define NO_END_OF_COPY_GOTO
MemoryContext CurrentMemoryContext
static bool CopyLoadRawBuf(CopyFromState cstate)
int pq_getmessage(StringInfo s, int maxlen)
static int CopyReadAttributesText(CopyFromState cstate)
static int GetDecimalFromHex(char hex)
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
bool * convert_select_flags
#define ereport(elevel,...)
#define Assert(condition)
static bool CopyReadLineText(CopyFromState cstate)
static int list_length(const List *l)
void ReceiveCopyBinaryHeader(CopyFromState cstate)
void * repalloc(void *pointer, Size size)
#define IF_NEED_REFILL_AND_EOF_BREAK(extralen)
static Datum values[MAXATTR]
static int CopyReadAttributesCSV(CopyFromState cstate)
int errmsg(const char *fmt,...)
#define PROGRESS_COPY_BYTES_PROCESSED
#define IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(extralen)
char * pg_any_to_server(const char *s, int len, int encoding)
static Datum CopyReadBinaryAttribute(CopyFromState cstate, FmgrInfo *flinfo, Oid typioparam, int32 typmod, bool *isnull)
ProtocolVersion FrontendProtocol
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
bool encoding_embeds_ascii
#define RESUME_CANCEL_INTERRUPTS()