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 &&
968 Assert(fieldno == attr_count);
998 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
999 errmsg(
"received copy data after EOF marker")));
1003 if (fld_count != attr_count)
1005 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1006 errmsg(
"row field count is %d, expected %d",
1007 (
int) fld_count, attr_count)));
1030 for (
i = 0;
i < num_defaults;
i++)
1036 Assert(econtext != NULL);
1079 }
while (inbytes > 0);
1132 char *copy_input_buf;
1135 bool need_data =
false;
1136 bool hit_eof =
false;
1137 bool result =
false;
1140 bool first_char_in_line =
true;
1141 bool in_quote =
false,
1142 last_was_esc =
false;
1144 char escapec =
'\0';
1151 if (quotec == escapec)
1200 if (input_buf_ptr >= copy_buf_len || need_data)
1223 prev_raw_ptr = input_buf_ptr;
1224 c = copy_input_buf[input_buf_ptr++];
1234 if (
c ==
'\\' ||
c ==
'\r')
1247 if (in_quote &&
c == escapec)
1248 last_was_esc = !last_was_esc;
1249 if (
c == quotec && !last_was_esc)
1250 in_quote = !in_quote;
1252 last_was_esc =
false;
1280 c = copy_input_buf[input_buf_ptr];
1292 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1294 errmsg(
"literal carriage return found in data") :
1295 errmsg(
"unquoted carriage return found in data"),
1297 errhint(
"Use \"\\r\" to represent carriage return.") :
1298 errhint(
"Use quoted CSV field to represent carriage return.")));
1309 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1311 errmsg(
"literal carriage return found in data") :
1312 errmsg(
"unquoted carriage return found in data"),
1314 errhint(
"Use \"\\r\" to represent carriage return.") :
1315 errhint(
"Use quoted CSV field to represent carriage return.")));
1325 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1327 errmsg(
"literal newline found in data") :
1328 errmsg(
"unquoted newline found in data"),
1330 errhint(
"Use \"\\n\" to represent newline.") :
1331 errhint(
"Use quoted CSV field to represent newline.")));
1341 if (
c ==
'\\' && (!cstate->
opts.
csv_mode || first_char_in_line))
1354 c2 = copy_input_buf[input_buf_ptr];
1370 c2 = copy_input_buf[input_buf_ptr++];
1376 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1377 errmsg(
"end-of-copy marker does not match previous newline style")));
1381 else if (c2 !=
'\r')
1385 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1386 errmsg(
"end-of-copy marker corrupt")));
1395 c2 = copy_input_buf[input_buf_ptr++];
1397 if (c2 !=
'\r' && c2 !=
'\n')
1401 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1402 errmsg(
"end-of-copy marker corrupt")));
1412 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1413 errmsg(
"end-of-copy marker does not match previous newline style")));
1452 first_char_in_line =
false;
1469 if (isdigit((
unsigned char) hex))
1472 return tolower((
unsigned char) hex) -
'a' + 10;
1511 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1512 errmsg(
"extra data after last expected column")));
1537 bool found_delim =
false;
1541 bool saw_non_ascii =
false;
1552 start_ptr = cur_ptr;
1571 if (cur_ptr >= line_end_ptr)
1581 if (cur_ptr >= line_end_ptr)
1599 if (cur_ptr < line_end_ptr)
1606 if (cur_ptr < line_end_ptr)
1619 saw_non_ascii =
true;
1624 if (cur_ptr < line_end_ptr)
1626 char hexchar = *cur_ptr;
1628 if (isxdigit((
unsigned char) hexchar))
1633 if (cur_ptr < line_end_ptr)
1636 if (isxdigit((
unsigned char) hexchar))
1644 saw_non_ascii =
true;
1679 input_len = end_ptr - start_ptr;
1703 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1704 errmsg(
"unexpected default marker in COPY data"),
1705 errdetail(
"Column \"%s\" has no default value.",
1726 *output_ptr++ =
'\0';
1736 Assert(*output_ptr ==
'\0');
1767 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1768 errmsg(
"extra data after last expected column")));
1793 bool found_delim =
false;
1794 bool saw_quote =
false;
1808 start_ptr = cur_ptr;
1826 if (cur_ptr >= line_end_ptr)
1849 if (cur_ptr >= line_end_ptr)
1851 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1852 errmsg(
"unterminated CSV quoted field")));
1863 if (cur_ptr < line_end_ptr)
1865 char nextc = *cur_ptr;
1867 if (nextc == escapec || nextc == quotec)
1869 *output_ptr++ = nextc;
1891 *output_ptr++ =
'\0';
1894 input_len = end_ptr - start_ptr;
1918 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1919 errmsg(
"unexpected default marker in COPY data"),
1920 errdetail(
"Column \"%s\" has no default value.",
1933 Assert(*output_ptr ==
'\0');
1953 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1954 errmsg(
"unexpected EOF in COPY data")));
1962 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1963 errmsg(
"invalid field size")));
1970 fld_size) != fld_size)
1972 (
errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1973 errmsg(
"unexpected EOF in COPY data")));
1980 typioparam, typmod);
1985 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1986 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)
elog(ERROR, "%s: %s", p2, msg)
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)
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Datum ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf, Oid typioparam, int32 typmod)
if(TABLE==NULL||TABLE_index==NULL)
#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)
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
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)