79 #define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
80 #define OCTVALUE(c) ((c) - '0')
98 #define IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(extralen) \
101 if (input_buf_ptr + (extralen) >= copy_buf_len && !hit_eof) \
103 input_buf_ptr = prev_raw_ptr; \
110 #define IF_NEED_REFILL_AND_EOF_BREAK(extralen) \
113 if (input_buf_ptr + (extralen) >= copy_buf_len && hit_eof) \
116 input_buf_ptr = copy_buf_len; \
127 #define REFILL_LINEBUF \
130 if (input_buf_ptr > cstate->input_buf_index) \
132 appendBinaryStringInfo(&cstate->line_buf, \
133 cstate->input_buf + cstate->input_buf_index, \
134 input_buf_ptr - cstate->input_buf_index); \
135 cstate->input_buf_index = input_buf_ptr; \
140 #define NO_END_OF_COPY_GOTO \
143 input_buf_ptr = prev_raw_ptr + 1; \
144 goto not_end_of_copy; \
163 int minread,
int maxread);
180 for (
i = 0;
i < natts;
i++)
199 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
200 errmsg(
"COPY file signature not recognized")));
204 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
205 errmsg(
"invalid COPY file header (missing flags)")));
206 if ((tmp & (1 << 16)) != 0)
208 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
209 errmsg(
"invalid COPY file header (WITH OIDS)")));
211 if ((tmp >> 16) != 0)
213 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
214 errmsg(
"unrecognized critical flags in COPY file header")));
219 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
220 errmsg(
"invalid COPY file header (missing length)")));
226 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
227 errmsg(
"invalid COPY file header (wrong length)")));
252 bytesread = fread(databuf, 1, maxread, cstate->
copy_file);
256 errmsg(
"could not read from COPY file: %m")));
261 while (maxread > 0 && bytesread < minread && !cstate->raw_reached_eof)
277 (
errcode(ERRCODE_CONNECTION_FAILURE),
278 errmsg(
"unexpected EOF on client connection with an open transaction")));
293 (
errcode(ERRCODE_PROTOCOL_VIOLATION),
294 errmsg(
"unexpected message type 0x%02X during COPY from stdin",
302 (
errcode(ERRCODE_CONNECTION_FAILURE),
303 errmsg(
"unexpected EOF on client connection with an open transaction")));
316 (
errcode(ERRCODE_QUERY_CANCELED),
317 errmsg(
"COPY from stdin failed: %s",
338 databuf = (
void *) ((
char *) databuf + avail);
419 if (unverifiedlen == 0)
434 cstate->
raw_buf + preverifiedlen,
510 if (convertedlen == 0)
579 elog(
ERROR,
"encoding conversion failed without error");
630 cstate->
raw_buf[nbytes] =
'\0';
703 int copied_bytes = 0;
710 copied_bytes = nbytes;
735 copied_bytes += copy_bytes;
736 }
while (copied_bytes < nbytes);
784 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
785 errmsg(
"wrong number of fields in header line: field count is %d, expected %d",
799 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
800 errmsg(
"column name mismatch in header line field %d: got null value (\"%s\"), expected \"%s\"",
806 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
807 errmsg(
"column name mismatch in header line field %d: got \"%s\", expected \"%s\"",
808 fldnum, colName,
NameStr(attr->attname))));
862 int *defmap = cstate->
defmap;
866 num_phys_attrs = tupDesc->
natts;
871 MemSet(nulls,
true, num_phys_attrs *
sizeof(
bool));
875 char **field_strings;
886 if (attr_count > 0 && fldct > attr_count)
888 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
889 errmsg(
"extra data after last expected column")));
900 if (fieldno >= fldct)
902 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
903 errmsg(
"missing data for column \"%s\"",
905 string = field_strings[fieldno++];
916 if (
string == NULL &&
950 Assert(fieldno == attr_count);
980 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
981 errmsg(
"received copy data after EOF marker")));
985 if (fld_count != attr_count)
987 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
988 errmsg(
"row field count is %d, expected %d",
989 (
int) fld_count, attr_count)));
1012 for (
i = 0;
i < num_defaults;
i++)
1018 Assert(econtext != NULL);
1061 }
while (inbytes > 0);
1114 char *copy_input_buf;
1117 bool need_data =
false;
1118 bool hit_eof =
false;
1119 bool result =
false;
1122 bool first_char_in_line =
true;
1123 bool in_quote =
false,
1124 last_was_esc =
false;
1126 char escapec =
'\0';
1133 if (quotec == escapec)
1184 if (input_buf_ptr >= copy_buf_len || need_data)
1207 prev_raw_ptr = input_buf_ptr;
1208 c = copy_input_buf[input_buf_ptr++];
1221 if (
c ==
'\\' ||
c ==
'\r')
1234 if (in_quote &&
c == escapec)
1235 last_was_esc = !last_was_esc;
1236 if (
c == quotec && !last_was_esc)
1237 in_quote = !in_quote;
1239 last_was_esc =
false;
1267 c = copy_input_buf[input_buf_ptr];
1279 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1281 errmsg(
"literal carriage return found in data") :
1282 errmsg(
"unquoted carriage return found in data"),
1284 errhint(
"Use \"\\r\" to represent carriage return.") :
1285 errhint(
"Use quoted CSV field to represent carriage return.")));
1296 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1298 errmsg(
"literal carriage return found in data") :
1299 errmsg(
"unquoted carriage return found in data"),
1301 errhint(
"Use \"\\r\" to represent carriage return.") :
1302 errhint(
"Use quoted CSV field to represent carriage return.")));
1312 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1314 errmsg(
"literal newline found in data") :
1315 errmsg(
"unquoted newline found in data"),
1317 errhint(
"Use \"\\n\" to represent newline.") :
1318 errhint(
"Use quoted CSV field to represent newline.")));
1328 if (
c ==
'\\' && (!cstate->
opts.
csv_mode || first_char_in_line))
1341 c2 = copy_input_buf[input_buf_ptr];
1357 c2 = copy_input_buf[input_buf_ptr++];
1363 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1364 errmsg(
"end-of-copy marker does not match previous newline style")));
1368 else if (c2 !=
'\r')
1372 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1373 errmsg(
"end-of-copy marker corrupt")));
1382 c2 = copy_input_buf[input_buf_ptr++];
1384 if (c2 !=
'\r' && c2 !=
'\n')
1388 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1389 errmsg(
"end-of-copy marker corrupt")));
1399 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1400 errmsg(
"end-of-copy marker does not match previous newline style")));
1439 first_char_in_line =
false;
1456 if (isdigit((
unsigned char) hex))
1459 return tolower((
unsigned char) hex) -
'a' + 10;
1498 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1499 errmsg(
"extra data after last expected column")));
1524 bool found_delim =
false;
1528 bool saw_non_ascii =
false;
1539 start_ptr = cur_ptr;
1558 if (cur_ptr >= line_end_ptr)
1568 if (cur_ptr >= line_end_ptr)
1586 if (cur_ptr < line_end_ptr)
1593 if (cur_ptr < line_end_ptr)
1606 saw_non_ascii =
true;
1611 if (cur_ptr < line_end_ptr)
1613 char hexchar = *cur_ptr;
1615 if (isxdigit((
unsigned char) hexchar))
1620 if (cur_ptr < line_end_ptr)
1623 if (isxdigit((
unsigned char) hexchar))
1631 saw_non_ascii =
true;
1666 input_len = end_ptr - start_ptr;
1687 *output_ptr++ =
'\0';
1697 Assert(*output_ptr ==
'\0');
1698 cstate->attribute_buf.len = (output_ptr - cstate->attribute_buf.data);
1728 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1729 errmsg(
"extra data after last expected column")));
1754 bool found_delim =
false;
1755 bool saw_quote =
false;
1769 start_ptr = cur_ptr;
1787 if (cur_ptr >= line_end_ptr)
1810 if (cur_ptr >= line_end_ptr)
1812 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1813 errmsg(
"unterminated CSV quoted field")));
1824 if (cur_ptr < line_end_ptr)
1826 char nextc = *cur_ptr;
1828 if (nextc == escapec || nextc == quotec)
1830 *output_ptr++ = nextc;
1852 *output_ptr++ =
'\0';
1855 input_len = end_ptr - start_ptr;
1868 Assert(*output_ptr ==
'\0');
1888 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1889 errmsg(
"unexpected EOF in COPY data")));
1897 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1898 errmsg(
"invalid field size")));
1905 fld_size) != fld_size)
1907 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1908 errmsg(
"unexpected EOF in COPY data")));
1915 typioparam, typmod);
1920 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1921 errmsg(
"incorrect binary data format")));
void pgstat_progress_update_param(int index, int64 val)
static Datum values[MAXATTR]
#define IS_HIGHBIT_SET(ch)
#define MemSet(start, val, len)
#define RAW_BUF_BYTES(cstate)
#define INPUT_BUF_BYTES(cstate)
static int CopyReadAttributesCSV(CopyFromState cstate)
static bool CopyGetInt16(CopyFromState cstate, int16 *val)
static void CopyConversionError(CopyFromState cstate)
static bool CopyGetInt32(CopyFromState cstate, int32 *val)
static void CopyLoadRawBuf(CopyFromState cstate)
#define NO_END_OF_COPY_GOTO
static void CopyLoadInputBuf(CopyFromState cstate)
void ReceiveCopyBinaryHeader(CopyFromState cstate)
static int CopyGetData(CopyFromState cstate, void *databuf, int minread, int maxread)
static Datum CopyReadBinaryAttribute(CopyFromState cstate, FmgrInfo *flinfo, Oid typioparam, int32 typmod, bool *isnull)
static bool CopyReadLineText(CopyFromState cstate)
static int GetDecimalFromHex(char hex)
void ReceiveCopyBegin(CopyFromState cstate)
#define IF_NEED_REFILL_AND_EOF_BREAK(extralen)
static int CopyReadAttributesText(CopyFromState cstate)
static const char BinarySignature[11]
#define IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(extralen)
static int CopyReadBinaryData(CopyFromState cstate, char *dest, int nbytes)
static bool CopyReadLine(CopyFromState cstate)
static void CopyConvertBuf(CopyFromState cstate)
bool NextCopyFrom(CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls)
bool NextCopyFromRawFields(CopyFromState cstate, char ***fields, int *nfields)
int errcode_for_file_access(void)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Datum ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf, Oid typioparam, int32 typmod)
#define PQ_SMALL_MESSAGE_LIMIT
#define PQ_LARGE_MESSAGE_LIMIT
Assert(fmt[strlen(fmt) - 1] !='\n')
int GetDatabaseEncoding(void)
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
int pg_do_encoding_conversion_buf(Oid proc, int src_encoding, int dest_encoding, unsigned char *src, int srclen, unsigned char *dest, int destlen, bool noError)
void report_invalid_encoding(int encoding, const char *mbstr, int len)
int pg_database_encoding_max_length(void)
MemoryContext CurrentMemoryContext
void * repalloc(void *pointer, Size size)
#define HOLD_CANCEL_INTERRUPTS()
#define RESUME_CANCEL_INTERRUPTS()
int namestrcmp(Name name, const char *str)
FormData_pg_attribute * Form_pg_attribute
static int list_length(const List *l)
#define MAX_CONVERSION_INPUT_LENGTH
int pq_getmessage(StringInfo s, int maxlen)
void pq_startmsgread(void)
#define PROGRESS_COPY_BYTES_PROCESSED
#define RelationGetDescr(relation)
StringInfo makeStringInfo(void)
void resetStringInfo(StringInfo str)
void enlargeStringInfo(StringInfo str, int needed)
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
copy_data_source_cb data_source_cb
StringInfoData attribute_buf
bool * convert_select_flags
MemoryContext ecxt_per_tuple_memory
#define TupleDescAttr(tupdesc, i)
int pg_encoding_verifymbstr(int encoding, const char *mbstr, int len)