78#define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
79#define OCTVALUE(c) ((c) - '0')
97#define IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(extralen) \
100 if (input_buf_ptr + (extralen) >= copy_buf_len && !hit_eof) \
102 input_buf_ptr = prev_raw_ptr; \
109#define IF_NEED_REFILL_AND_EOF_BREAK(extralen) \
112 if (input_buf_ptr + (extralen) >= copy_buf_len && hit_eof) \
115 input_buf_ptr = copy_buf_len; \
126#define REFILL_LINEBUF \
129 if (input_buf_ptr > cstate->input_buf_index) \
131 appendBinaryStringInfo(&cstate->line_buf, \
132 cstate->input_buf + cstate->input_buf_index, \
133 input_buf_ptr - cstate->input_buf_index); \
134 cstate->input_buf_index = input_buf_ptr; \
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);
792 for (
int i = 0;
i < lines_to_skip;
i++)
810 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
811 errmsg(
"wrong number of fields in header line: got %d, expected %d",
821 Assert(fldnum < cstate->max_fields);
826 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
827 errmsg(
"column name mismatch in header line field %d: got null value (\"%s\"), expected \"%s\"",
833 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
834 errmsg(
"column name mismatch in header line field %d: got \"%s\", expected \"%s\"",
835 fldnum, colName,
NameStr(attr->attname))));
887 int *defmap = cstate->
defmap;
891 num_phys_attrs = tupDesc->
natts;
895 MemSet(nulls,
true, num_phys_attrs *
sizeof(
bool));
907 for (
i = 0;
i < num_defaults;
i++)
954 char **field_strings;
968 if (attr_count > 0 && fldct > attr_count)
970 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
971 errmsg(
"extra data after last expected column")));
982 if (fieldno >= fldct)
984 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
985 errmsg(
"missing data for column \"%s\"",
987 string = field_strings[fieldno++];
998 if (
string == NULL &&
1029 Assert(econtext != NULL);
1065 errmsg(
"skipping row due to data type incompatibility at line %" PRIu64
" for column \"%s\": \"%s\"",
1073 errmsg(
"skipping row due to data type incompatibility at line %" PRIu64
" for column \"%s\": null input",
1088 Assert(fieldno == attr_count);
1116 if (fld_count == -1)
1130 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1131 errmsg(
"received copy data after EOF marker")));
1135 if (fld_count != attr_count)
1137 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1138 errmsg(
"row field count is %d, expected %d",
1139 (
int) fld_count, attr_count)));
1192 }
while (inbytes > 0);
1245 char *copy_input_buf;
1248 bool need_data =
false;
1249 bool hit_eof =
false;
1250 bool result =
false;
1253 bool in_quote =
false,
1254 last_was_esc =
false;
1256 char escapec =
'\0';
1263 if (quotec == escapec)
1309 if (input_buf_ptr >= copy_buf_len || need_data)
1332 prev_raw_ptr = input_buf_ptr;
1333 c = copy_input_buf[input_buf_ptr++];
1356 if (in_quote &&
c == escapec)
1357 last_was_esc = !last_was_esc;
1358 if (
c == quotec && !last_was_esc)
1359 in_quote = !in_quote;
1361 last_was_esc =
false;
1374 if (
c ==
'\r' && (!is_csv || !in_quote))
1389 c = copy_input_buf[input_buf_ptr];
1401 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1403 errmsg(
"literal carriage return found in data") :
1404 errmsg(
"unquoted carriage return found in data"),
1406 errhint(
"Use \"\\r\" to represent carriage return.") :
1407 errhint(
"Use quoted CSV field to represent carriage return.")));
1418 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1420 errmsg(
"literal carriage return found in data") :
1421 errmsg(
"unquoted carriage return found in data"),
1423 errhint(
"Use \"\\r\" to represent carriage return.") :
1424 errhint(
"Use quoted CSV field to represent carriage return.")));
1430 if (
c ==
'\n' && (!is_csv || !in_quote))
1434 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1436 errmsg(
"literal newline found in data") :
1437 errmsg(
"unquoted newline found in data"),
1439 errhint(
"Use \"\\n\" to represent newline.") :
1440 errhint(
"Use quoted CSV field to represent newline.")));
1450 if (
c ==
'\\' && !is_csv)
1463 c2 = copy_input_buf[input_buf_ptr];
1473 c2 = copy_input_buf[input_buf_ptr++];
1477 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1478 errmsg(
"end-of-copy marker does not match previous newline style")));
1479 else if (c2 !=
'\r')
1481 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1482 errmsg(
"end-of-copy marker is not alone on its line")));
1488 c2 = copy_input_buf[input_buf_ptr++];
1490 if (c2 !=
'\r' && c2 !=
'\n')
1492 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1493 errmsg(
"end-of-copy marker is not alone on its line")));
1499 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1500 errmsg(
"end-of-copy marker does not match previous newline style")));
1508 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1509 errmsg(
"end-of-copy marker is not alone on its line")));
1547 if (isdigit((
unsigned char) hex))
1589 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1590 errmsg(
"extra data after last expected column")));
1615 bool found_delim =
false;
1619 bool saw_non_ascii =
false;
1630 start_ptr = cur_ptr;
1649 if (cur_ptr >= line_end_ptr)
1659 if (cur_ptr >= line_end_ptr)
1677 if (cur_ptr < line_end_ptr)
1684 if (cur_ptr < line_end_ptr)
1697 saw_non_ascii =
true;
1702 if (cur_ptr < line_end_ptr)
1704 char hexchar = *cur_ptr;
1706 if (isxdigit((
unsigned char) hexchar))
1711 if (cur_ptr < line_end_ptr)
1714 if (isxdigit((
unsigned char) hexchar))
1722 saw_non_ascii =
true;
1757 input_len = end_ptr - start_ptr;
1781 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1782 errmsg(
"unexpected default marker in COPY data"),
1783 errdetail(
"Column \"%s\" has no default value.",
1804 *output_ptr++ =
'\0';
1814 Assert(*output_ptr ==
'\0');
1845 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1846 errmsg(
"extra data after last expected column")));
1871 bool found_delim =
false;
1872 bool saw_quote =
false;
1886 start_ptr = cur_ptr;
1904 if (cur_ptr >= line_end_ptr)
1927 if (cur_ptr >= line_end_ptr)
1929 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1930 errmsg(
"unterminated CSV quoted field")));
1941 if (cur_ptr < line_end_ptr)
1943 char nextc = *cur_ptr;
1945 if (nextc == escapec || nextc == quotec)
1947 *output_ptr++ = nextc;
1969 *output_ptr++ =
'\0';
1972 input_len = end_ptr - start_ptr;
1996 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1997 errmsg(
"unexpected default marker in COPY data"),
1998 errdetail(
"Column \"%s\" has no default value.",
2011 Assert(*output_ptr ==
'\0');
2031 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2032 errmsg(
"unexpected EOF in COPY data")));
2040 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2041 errmsg(
"invalid field size")));
2048 fld_size) != fld_size)
2050 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2051 errmsg(
"unexpected EOF in COPY data")));
2058 typioparam, typmod);
2063 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
2064 errmsg(
"incorrect binary data format")));
void pgstat_progress_update_param(int index, int64 val)
static Datum values[MAXATTR]
#define IS_HIGHBIT_SET(ch)
#define pg_attribute_always_inline
#define MemSet(start, val, len)
char * CopyLimitPrintoutLength(const char *str)
#define RAW_BUF_BYTES(cstate)
#define INPUT_BUF_BYTES(cstate)
static pg_attribute_always_inline bool CopyFromTextLikeOneRow(CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls, bool is_csv)
static pg_attribute_always_inline bool NextCopyFromRawFieldsInternal(CopyFromState cstate, char ***fields, int *nfields, bool is_csv)
bool CopyFromTextOneRow(CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls)
bool CopyFromCSVOneRow(CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls)
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)
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 int GetDecimalFromHex(char hex)
void ReceiveCopyBegin(CopyFromState cstate)
static bool CopyReadLineText(CopyFromState cstate, bool is_csv)
#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 bool CopyReadLine(CopyFromState cstate, bool is_csv)
static int CopyReadBinaryData(CopyFromState cstate, char *dest, int nbytes)
bool CopyFromBinaryOneRow(CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls)
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, Node *escontext, Datum *result)
Datum ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf, Oid typioparam, int32 typmod)
Assert(PointerIsAligned(start, uint64))
@ COPY_LOG_VERBOSITY_VERBOSE
#define COPY_HEADER_MATCH
#define COPY_HEADER_FALSE
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 * repalloc(void *pointer, Size size)
void pfree(void *pointer)
MemoryContext CurrentMemoryContext
#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
unsigned char pg_ascii_tolower(unsigned char ch)
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)
bool(* CopyFromOneRow)(CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls)
copy_data_source_cb data_source_cb
const struct CopyFromRoutine * routine
StringInfoData attribute_buf
bool * convert_select_flags
ErrorSaveContext * escontext
MemoryContext ecxt_per_tuple_memory
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
int pg_encoding_verifymbstr(int encoding, const char *mbstr, int len)
int pg_encoding_max_length(int encoding)