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: got %d, expected %d",
795 Assert(fldnum < cstate->max_fields);
800 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
801 errmsg(
"column name mismatch in header line field %d: got null value (\"%s\"), expected \"%s\"",
807 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
808 errmsg(
"column name mismatch in header line field %d: got \"%s\", expected \"%s\"",
809 fldnum, colName,
NameStr(attr->attname))));
864 int *defmap = cstate->
defmap;
868 num_phys_attrs = tupDesc->
natts;
873 MemSet(nulls,
true, num_phys_attrs *
sizeof(
bool));
878 char **field_strings;
889 if (attr_count > 0 && fldct > attr_count)
891 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
892 errmsg(
"extra data after last expected column")));
903 if (fieldno >= fldct)
905 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
906 errmsg(
"missing data for column \"%s\"",
908 string = field_strings[fieldno++];
919 if (
string == NULL &&
990 errmsg(
"skipping row due to data type incompatibility at line %llu for column \"%s\": \"%s\"",
998 errmsg(
"skipping row due to data type incompatibility at line %llu for column \"%s\": null input",
1013 Assert(fieldno == attr_count);
1029 if (fld_count == -1)
1043 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1044 errmsg(
"received copy data after EOF marker")));
1048 if (fld_count != attr_count)
1050 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1051 errmsg(
"row field count is %d, expected %d",
1052 (
int) fld_count, attr_count)));
1075 for (
i = 0;
i < num_defaults;
i++)
1081 Assert(econtext != NULL);
1124 }
while (inbytes > 0);
1177 char *copy_input_buf;
1180 bool need_data =
false;
1181 bool hit_eof =
false;
1182 bool result =
false;
1185 bool first_char_in_line =
true;
1186 bool in_quote =
false,
1187 last_was_esc =
false;
1189 char escapec =
'\0';
1196 if (quotec == escapec)
1242 if (input_buf_ptr >= copy_buf_len || need_data)
1265 prev_raw_ptr = input_buf_ptr;
1266 c = copy_input_buf[input_buf_ptr++];
1276 if (
c ==
'\\' ||
c ==
'\r')
1289 if (in_quote &&
c == escapec)
1290 last_was_esc = !last_was_esc;
1291 if (
c == quotec && !last_was_esc)
1292 in_quote = !in_quote;
1294 last_was_esc =
false;
1322 c = copy_input_buf[input_buf_ptr];
1334 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1336 errmsg(
"literal carriage return found in data") :
1337 errmsg(
"unquoted carriage return found in data"),
1339 errhint(
"Use \"\\r\" to represent carriage return.") :
1340 errhint(
"Use quoted CSV field to represent carriage return.")));
1351 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1353 errmsg(
"literal carriage return found in data") :
1354 errmsg(
"unquoted carriage return found in data"),
1356 errhint(
"Use \"\\r\" to represent carriage return.") :
1357 errhint(
"Use quoted CSV field to represent carriage return.")));
1367 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1369 errmsg(
"literal newline found in data") :
1370 errmsg(
"unquoted newline found in data"),
1372 errhint(
"Use \"\\n\" to represent newline.") :
1373 errhint(
"Use quoted CSV field to represent newline.")));
1383 if (
c ==
'\\' && (!cstate->
opts.
csv_mode || first_char_in_line))
1396 c2 = copy_input_buf[input_buf_ptr];
1412 c2 = copy_input_buf[input_buf_ptr++];
1418 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1419 errmsg(
"end-of-copy marker does not match previous newline style")));
1423 else if (c2 !=
'\r')
1427 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1428 errmsg(
"end-of-copy marker corrupt")));
1437 c2 = copy_input_buf[input_buf_ptr++];
1439 if (c2 !=
'\r' && c2 !=
'\n')
1443 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1444 errmsg(
"end-of-copy marker corrupt")));
1454 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1455 errmsg(
"end-of-copy marker does not match previous newline style")));
1494 first_char_in_line =
false;
1511 if (isdigit((
unsigned char) hex))
1514 return tolower((
unsigned char) hex) -
'a' + 10;
1553 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1554 errmsg(
"extra data after last expected column")));
1579 bool found_delim =
false;
1583 bool saw_non_ascii =
false;
1594 start_ptr = cur_ptr;
1613 if (cur_ptr >= line_end_ptr)
1623 if (cur_ptr >= line_end_ptr)
1641 if (cur_ptr < line_end_ptr)
1648 if (cur_ptr < line_end_ptr)
1661 saw_non_ascii =
true;
1666 if (cur_ptr < line_end_ptr)
1668 char hexchar = *cur_ptr;
1670 if (isxdigit((
unsigned char) hexchar))
1675 if (cur_ptr < line_end_ptr)
1678 if (isxdigit((
unsigned char) hexchar))
1686 saw_non_ascii =
true;
1721 input_len = end_ptr - start_ptr;
1745 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1746 errmsg(
"unexpected default marker in COPY data"),
1747 errdetail(
"Column \"%s\" has no default value.",
1768 *output_ptr++ =
'\0';
1778 Assert(*output_ptr ==
'\0');
1809 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1810 errmsg(
"extra data after last expected column")));
1835 bool found_delim =
false;
1836 bool saw_quote =
false;
1850 start_ptr = cur_ptr;
1868 if (cur_ptr >= line_end_ptr)
1891 if (cur_ptr >= line_end_ptr)
1893 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1894 errmsg(
"unterminated CSV quoted field")));
1905 if (cur_ptr < line_end_ptr)
1907 char nextc = *cur_ptr;
1909 if (nextc == escapec || nextc == quotec)
1911 *output_ptr++ = nextc;
1933 *output_ptr++ =
'\0';
1936 input_len = end_ptr - start_ptr;
1960 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1961 errmsg(
"unexpected default marker in COPY data"),
1962 errdetail(
"Column \"%s\" has no default value.",
1975 Assert(*output_ptr ==
'\0');
1995 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1996 errmsg(
"unexpected EOF in COPY data")));
2004 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2005 errmsg(
"invalid field size")));
2012 fld_size) != fld_size)
2014 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2015 errmsg(
"unexpected EOF in COPY data")));
2022 typioparam, typmod);
2027 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
2028 errmsg(
"incorrect binary data format")));
void pgstat_progress_update_param(int index, int64 val)
static Datum values[MAXATTR]
#define IS_HIGHBIT_SET(ch)
#define Assert(condition)
#define MemSet(start, val, len)
char * CopyLimitPrintoutLength(const char *str)
#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 errdetail(const char *fmt,...)
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)
bool InputFunctionCallSafe(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod, fmNodePtr escontext, Datum *result)
Datum ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf, Oid typioparam, int32 typmod)
@ COPY_LOG_VERBOSITY_VERBOSE
if(TABLE==NULL||TABLE_index==NULL)
#define PQ_SMALL_MESSAGE_LIMIT
#define PQ_LARGE_MESSAGE_LIMIT
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)
void pfree(void *pointer)
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)
static int list_nth_int(const List *list, int n)
#define MAX_CONVERSION_INPUT_LENGTH
int pq_getmessage(StringInfo s, int maxlen)
void pq_startmsgread(void)
#define PROGRESS_COPY_BYTES_PROCESSED
#define PqMsg_CopyInResponse
#define RelationGetDescr(relation)
StringInfo makeStringInfo(void)
void resetStringInfo(StringInfo str)
void enlargeStringInfo(StringInfo str, int needed)
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
copy_data_source_cb data_source_cb
StringInfoData attribute_buf
bool * convert_select_flags
ErrorSaveContext * escontext
MemoryContext ecxt_per_tuple_memory
#define TupleDescAttr(tupdesc, i)
int pg_encoding_verifymbstr(int encoding, const char *mbstr, int len)
int pg_encoding_max_length(int encoding)