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; \
154 int minread,
int maxread);
171 for (
i = 0;
i < natts;
i++)
190 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
191 errmsg(
"COPY file signature not recognized")));
195 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
196 errmsg(
"invalid COPY file header (missing flags)")));
197 if ((tmp & (1 << 16)) != 0)
199 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
200 errmsg(
"invalid COPY file header (WITH OIDS)")));
202 if ((tmp >> 16) != 0)
204 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
205 errmsg(
"unrecognized critical flags in COPY file header")));
210 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
211 errmsg(
"invalid COPY file header (missing length)")));
217 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
218 errmsg(
"invalid COPY file header (wrong length)")));
243 bytesread = fread(databuf, 1, maxread, cstate->
copy_file);
247 errmsg(
"could not read from COPY file: %m")));
252 while (maxread > 0 && bytesread < minread && !cstate->raw_reached_eof)
268 (
errcode(ERRCODE_CONNECTION_FAILURE),
269 errmsg(
"unexpected EOF on client connection with an open transaction")));
284 (
errcode(ERRCODE_PROTOCOL_VIOLATION),
285 errmsg(
"unexpected message type 0x%02X during COPY from stdin",
293 (
errcode(ERRCODE_CONNECTION_FAILURE),
294 errmsg(
"unexpected EOF on client connection with an open transaction")));
307 (
errcode(ERRCODE_QUERY_CANCELED),
308 errmsg(
"COPY from stdin failed: %s",
329 databuf = (
void *) ((
char *) databuf + avail);
410 if (unverifiedlen == 0)
425 cstate->
raw_buf + preverifiedlen,
501 if (convertedlen == 0)
570 elog(
ERROR,
"encoding conversion failed without error");
621 cstate->
raw_buf[nbytes] =
'\0';
694 int copied_bytes = 0;
701 copied_bytes = nbytes;
726 copied_bytes += copy_bytes;
727 }
while (copied_bytes < nbytes);
775 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
776 errmsg(
"wrong number of fields in header line: got %d, expected %d",
786 Assert(fldnum < cstate->max_fields);
791 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
792 errmsg(
"column name mismatch in header line field %d: got null value (\"%s\"), expected \"%s\"",
798 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
799 errmsg(
"column name mismatch in header line field %d: got \"%s\", expected \"%s\"",
800 fldnum, colName,
NameStr(attr->attname))));
855 int *defmap = cstate->
defmap;
859 num_phys_attrs = tupDesc->
natts;
864 MemSet(nulls,
true, num_phys_attrs *
sizeof(
bool));
869 char **field_strings;
880 if (attr_count > 0 && fldct > attr_count)
882 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
883 errmsg(
"extra data after last expected column")));
894 if (fieldno >= fldct)
896 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
897 errmsg(
"missing data for column \"%s\"",
899 string = field_strings[fieldno++];
910 if (
string == NULL &&
981 errmsg(
"skipping row due to data type incompatibility at line %llu for column \"%s\": \"%s\"",
989 errmsg(
"skipping row due to data type incompatibility at line %llu for column \"%s\": null input",
1004 Assert(fieldno == attr_count);
1020 if (fld_count == -1)
1034 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1035 errmsg(
"received copy data after EOF marker")));
1039 if (fld_count != attr_count)
1041 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1042 errmsg(
"row field count is %d, expected %d",
1043 (
int) fld_count, attr_count)));
1066 for (
i = 0;
i < num_defaults;
i++)
1072 Assert(econtext != NULL);
1115 }
while (inbytes > 0);
1168 char *copy_input_buf;
1171 bool need_data =
false;
1172 bool hit_eof =
false;
1173 bool result =
false;
1176 bool in_quote =
false,
1177 last_was_esc =
false;
1179 char escapec =
'\0';
1186 if (quotec == escapec)
1232 if (input_buf_ptr >= copy_buf_len || need_data)
1255 prev_raw_ptr = input_buf_ptr;
1256 c = copy_input_buf[input_buf_ptr++];
1279 if (in_quote &&
c == escapec)
1280 last_was_esc = !last_was_esc;
1281 if (
c == quotec && !last_was_esc)
1282 in_quote = !in_quote;
1284 last_was_esc =
false;
1312 c = copy_input_buf[input_buf_ptr];
1324 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1326 errmsg(
"literal carriage return found in data") :
1327 errmsg(
"unquoted carriage return found in data"),
1329 errhint(
"Use \"\\r\" to represent carriage return.") :
1330 errhint(
"Use quoted CSV field to represent carriage return.")));
1341 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1343 errmsg(
"literal carriage return found in data") :
1344 errmsg(
"unquoted carriage return found in data"),
1346 errhint(
"Use \"\\r\" to represent carriage return.") :
1347 errhint(
"Use quoted CSV field to represent carriage return.")));
1357 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1359 errmsg(
"literal newline found in data") :
1360 errmsg(
"unquoted newline found in data"),
1362 errhint(
"Use \"\\n\" to represent newline.") :
1363 errhint(
"Use quoted CSV field to represent newline.")));
1386 c2 = copy_input_buf[input_buf_ptr];
1396 c2 = copy_input_buf[input_buf_ptr++];
1400 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1401 errmsg(
"end-of-copy marker does not match previous newline style")));
1402 else if (c2 !=
'\r')
1404 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1405 errmsg(
"end-of-copy marker is not alone on its line")));
1411 c2 = copy_input_buf[input_buf_ptr++];
1413 if (c2 !=
'\r' && c2 !=
'\n')
1415 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1416 errmsg(
"end-of-copy marker is not alone on its line")));
1422 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1423 errmsg(
"end-of-copy marker does not match previous newline style")));
1431 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1432 errmsg(
"end-of-copy marker is not alone on its line")));
1470 if (isdigit((
unsigned char) hex))
1473 return tolower((
unsigned char) hex) -
'a' + 10;
1512 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1513 errmsg(
"extra data after last expected column")));
1538 bool found_delim =
false;
1542 bool saw_non_ascii =
false;
1553 start_ptr = cur_ptr;
1572 if (cur_ptr >= line_end_ptr)
1582 if (cur_ptr >= line_end_ptr)
1600 if (cur_ptr < line_end_ptr)
1607 if (cur_ptr < line_end_ptr)
1620 saw_non_ascii =
true;
1625 if (cur_ptr < line_end_ptr)
1627 char hexchar = *cur_ptr;
1629 if (isxdigit((
unsigned char) hexchar))
1634 if (cur_ptr < line_end_ptr)
1637 if (isxdigit((
unsigned char) hexchar))
1645 saw_non_ascii =
true;
1680 input_len = end_ptr - start_ptr;
1704 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1705 errmsg(
"unexpected default marker in COPY data"),
1706 errdetail(
"Column \"%s\" has no default value.",
1727 *output_ptr++ =
'\0';
1737 Assert(*output_ptr ==
'\0');
1768 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1769 errmsg(
"extra data after last expected column")));
1794 bool found_delim =
false;
1795 bool saw_quote =
false;
1809 start_ptr = cur_ptr;
1827 if (cur_ptr >= line_end_ptr)
1850 if (cur_ptr >= line_end_ptr)
1852 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1853 errmsg(
"unterminated CSV quoted field")));
1864 if (cur_ptr < line_end_ptr)
1866 char nextc = *cur_ptr;
1868 if (nextc == escapec || nextc == quotec)
1870 *output_ptr++ = nextc;
1892 *output_ptr++ =
'\0';
1895 input_len = end_ptr - start_ptr;
1919 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1920 errmsg(
"unexpected default marker in COPY data"),
1921 errdetail(
"Column \"%s\" has no default value.",
1934 Assert(*output_ptr ==
'\0');
1954 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1955 errmsg(
"unexpected EOF in COPY data")));
1963 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1964 errmsg(
"invalid field size")));
1971 fld_size) != fld_size)
1973 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1974 errmsg(
"unexpected EOF in COPY data")));
1981 typioparam, typmod);
1986 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1987 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)
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)
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)