Thanks to visit codestin.com
Credit goes to doxygen.postgresql.org

PostgreSQL Source Code git master
copyfromparse.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <unistd.h>
#include <sys/stat.h>
#include "commands/copyapi.h"
#include "commands/copyfrom_internal.h"
#include "commands/progress.h"
#include "executor/executor.h"
#include "libpq/libpq.h"
#include "libpq/pqformat.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "port/pg_bswap.h"
#include "utils/builtins.h"
#include "utils/rel.h"
Include dependency graph for copyfromparse.c:

Go to the source code of this file.

Macros

#define ISOCTAL(c)   (((c) >= '0') && ((c) <= '7'))
 
#define OCTVALUE(c)   ((c) - '0')
 
#define IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(extralen)
 
#define IF_NEED_REFILL_AND_EOF_BREAK(extralen)
 
#define REFILL_LINEBUF
 

Functions

static bool CopyReadLine (CopyFromState cstate, bool is_csv)
 
static bool CopyReadLineText (CopyFromState cstate, bool is_csv)
 
static int CopyReadAttributesText (CopyFromState cstate)
 
static int CopyReadAttributesCSV (CopyFromState cstate)
 
static Datum CopyReadBinaryAttribute (CopyFromState cstate, FmgrInfo *flinfo, Oid typioparam, int32 typmod, bool *isnull)
 
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)
 
static int CopyGetData (CopyFromState cstate, void *databuf, int minread, int maxread)
 
static bool CopyGetInt32 (CopyFromState cstate, int32 *val)
 
static bool CopyGetInt16 (CopyFromState cstate, int16 *val)
 
static void CopyLoadInputBuf (CopyFromState cstate)
 
static int CopyReadBinaryData (CopyFromState cstate, char *dest, int nbytes)
 
void ReceiveCopyBegin (CopyFromState cstate)
 
void ReceiveCopyBinaryHeader (CopyFromState cstate)
 
static void CopyConvertBuf (CopyFromState cstate)
 
static void CopyConversionError (CopyFromState cstate)
 
static void CopyLoadRawBuf (CopyFromState cstate)
 
bool NextCopyFromRawFields (CopyFromState cstate, char ***fields, int *nfields)
 
bool NextCopyFrom (CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls)
 
bool CopyFromTextOneRow (CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls)
 
bool CopyFromCSVOneRow (CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls)
 
bool CopyFromBinaryOneRow (CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls)
 
static int GetDecimalFromHex (char hex)
 

Variables

static const char BinarySignature [11] = "PGCOPY\n\377\r\n\0"
 

Macro Definition Documentation

◆ IF_NEED_REFILL_AND_EOF_BREAK

#define IF_NEED_REFILL_AND_EOF_BREAK (   extralen)
Value:
if (1) \
{ \
if (input_buf_ptr + (extralen) >= copy_buf_len && hit_eof) \
{ \
if (extralen) \
input_buf_ptr = copy_buf_len; /* consume the partial character */ \
/* backslash just before EOF, treat as data char */ \
result = true; \
break; \
} \
} else ((void) 0)

Definition at line 109 of file copyfromparse.c.

◆ IF_NEED_REFILL_AND_NOT_EOF_CONTINUE

#define IF_NEED_REFILL_AND_NOT_EOF_CONTINUE (   extralen)
Value:
if (1) \
{ \
if (input_buf_ptr + (extralen) >= copy_buf_len && !hit_eof) \
{ \
input_buf_ptr = prev_raw_ptr; /* undo fetch */ \
need_data = true; \
continue; \
} \
} else ((void) 0)

Definition at line 97 of file copyfromparse.c.

◆ ISOCTAL

#define ISOCTAL (   c)    (((c) >= '0') && ((c) <= '7'))

Definition at line 78 of file copyfromparse.c.

◆ OCTVALUE

#define OCTVALUE (   c)    ((c) - '0')

Definition at line 79 of file copyfromparse.c.

◆ REFILL_LINEBUF

#define REFILL_LINEBUF
Value:
if (1) \
{ \
if (input_buf_ptr > cstate->input_buf_index) \
{ \
appendBinaryStringInfo(&cstate->line_buf, \
cstate->input_buf + cstate->input_buf_index, \
input_buf_ptr - cstate->input_buf_index); \
cstate->input_buf_index = input_buf_ptr; \
} \
} else ((void) 0)

Definition at line 126 of file copyfromparse.c.

Function Documentation

◆ CopyConversionError()

static void CopyConversionError ( CopyFromState  cstate)
static

Definition at line 533 of file copyfromparse.c.

534{
535 Assert(cstate->raw_buf_len > 0);
537
538 if (!cstate->need_transcoding)
539 {
540 /*
541 * Everything up to input_buf_len was successfully verified, and
542 * input_buf_len points to the invalid or incomplete character.
543 */
545 cstate->raw_buf + cstate->input_buf_len,
546 cstate->raw_buf_len - cstate->input_buf_len);
547 }
548 else
549 {
550 /*
551 * raw_buf_index points to the invalid or untranslatable character. We
552 * let the conversion routine report the error, because it can provide
553 * a more specific error message than we could here. An earlier call
554 * to the conversion routine in CopyConvertBuf() detected that there
555 * is an error, now we call the conversion routine again with
556 * noError=false, to have it throw the error.
557 */
558 unsigned char *src;
559 int srclen;
560 unsigned char *dst;
561 int dstlen;
562
563 src = (unsigned char *) cstate->raw_buf + cstate->raw_buf_index;
564 srclen = cstate->raw_buf_len - cstate->raw_buf_index;
565 dst = (unsigned char *) cstate->input_buf + cstate->input_buf_len;
566 dstlen = INPUT_BUF_SIZE - cstate->input_buf_len + 1;
567
569 cstate->file_encoding,
571 src, srclen,
572 dst, dstlen,
573 false);
574
575 /*
576 * The conversion routine should have reported an error, so this
577 * should not be reached.
578 */
579 elog(ERROR, "encoding conversion failed without error");
580 }
581}
#define INPUT_BUF_SIZE
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
Assert(PointerIsAligned(start, uint64))
int GetDatabaseEncoding(void)
Definition: mbutils.c:1262
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)
Definition: mbutils.c:470
void report_invalid_encoding(int encoding, const char *mbstr, int len)
Definition: mbutils.c:1699

References Assert(), CopyFromStateData::conversion_proc, elog, ERROR, CopyFromStateData::file_encoding, GetDatabaseEncoding(), CopyFromStateData::input_buf, CopyFromStateData::input_buf_len, INPUT_BUF_SIZE, CopyFromStateData::input_reached_error, CopyFromStateData::need_transcoding, pg_do_encoding_conversion_buf(), CopyFromStateData::raw_buf, CopyFromStateData::raw_buf_index, CopyFromStateData::raw_buf_len, and report_invalid_encoding().

Referenced by CopyLoadInputBuf().

◆ CopyConvertBuf()

static void CopyConvertBuf ( CopyFromState  cstate)
static

Definition at line 400 of file copyfromparse.c.

401{
402 /*
403 * If the file and server encoding are the same, no encoding conversion is
404 * required. However, we still need to verify that the input is valid for
405 * the encoding.
406 */
407 if (!cstate->need_transcoding)
408 {
409 /*
410 * When conversion is not required, input_buf and raw_buf are the
411 * same. raw_buf_len is the total number of bytes in the buffer, and
412 * input_buf_len tracks how many of those bytes have already been
413 * verified.
414 */
415 int preverifiedlen = cstate->input_buf_len;
416 int unverifiedlen = cstate->raw_buf_len - cstate->input_buf_len;
417 int nverified;
418
419 if (unverifiedlen == 0)
420 {
421 /*
422 * If no more raw data is coming, report the EOF to the caller.
423 */
424 if (cstate->raw_reached_eof)
425 cstate->input_reached_eof = true;
426 return;
427 }
428
429 /*
430 * Verify the new data, including any residual unverified bytes from
431 * previous round.
432 */
433 nverified = pg_encoding_verifymbstr(cstate->file_encoding,
434 cstate->raw_buf + preverifiedlen,
435 unverifiedlen);
436 if (nverified == 0)
437 {
438 /*
439 * Could not verify anything.
440 *
441 * If there is no more raw input data coming, it means that there
442 * was an incomplete multi-byte sequence at the end. Also, if
443 * there's "enough" input left, we should be able to verify at
444 * least one character, and a failure to do so means that we've
445 * hit an invalid byte sequence.
446 */
447 if (cstate->raw_reached_eof || unverifiedlen >= pg_encoding_max_length(cstate->file_encoding))
448 cstate->input_reached_error = true;
449 return;
450 }
451 cstate->input_buf_len += nverified;
452 }
453 else
454 {
455 /*
456 * Encoding conversion is needed.
457 */
458 int nbytes;
459 unsigned char *src;
460 int srclen;
461 unsigned char *dst;
462 int dstlen;
463 int convertedlen;
464
465 if (RAW_BUF_BYTES(cstate) == 0)
466 {
467 /*
468 * If no more raw data is coming, report the EOF to the caller.
469 */
470 if (cstate->raw_reached_eof)
471 cstate->input_reached_eof = true;
472 return;
473 }
474
475 /*
476 * First, copy down any unprocessed data.
477 */
478 nbytes = INPUT_BUF_BYTES(cstate);
479 if (nbytes > 0 && cstate->input_buf_index > 0)
480 memmove(cstate->input_buf, cstate->input_buf + cstate->input_buf_index,
481 nbytes);
482 cstate->input_buf_index = 0;
483 cstate->input_buf_len = nbytes;
484 cstate->input_buf[nbytes] = '\0';
485
486 src = (unsigned char *) cstate->raw_buf + cstate->raw_buf_index;
487 srclen = cstate->raw_buf_len - cstate->raw_buf_index;
488 dst = (unsigned char *) cstate->input_buf + cstate->input_buf_len;
489 dstlen = INPUT_BUF_SIZE - cstate->input_buf_len + 1;
490
491 /*
492 * Do the conversion. This might stop short, if there is an invalid
493 * byte sequence in the input. We'll convert as much as we can in
494 * that case.
495 *
496 * Note: Even if we hit an invalid byte sequence, we don't report the
497 * error until all the valid bytes have been consumed. The input
498 * might contain an end-of-input marker (\.), and we don't want to
499 * report an error if the invalid byte sequence is after the
500 * end-of-input marker. We might unnecessarily convert some data
501 * after the end-of-input marker as long as it's valid for the
502 * encoding, but that's harmless.
503 */
504 convertedlen = pg_do_encoding_conversion_buf(cstate->conversion_proc,
505 cstate->file_encoding,
507 src, srclen,
508 dst, dstlen,
509 true);
510 if (convertedlen == 0)
511 {
512 /*
513 * Could not convert anything. If there is no more raw input data
514 * coming, it means that there was an incomplete multi-byte
515 * sequence at the end. Also, if there is plenty of input left,
516 * we should be able to convert at least one character, so a
517 * failure to do so must mean that we've hit a byte sequence
518 * that's invalid.
519 */
520 if (cstate->raw_reached_eof || srclen >= MAX_CONVERSION_INPUT_LENGTH)
521 cstate->input_reached_error = true;
522 return;
523 }
524 cstate->raw_buf_index += convertedlen;
525 cstate->input_buf_len += strlen((char *) dst);
526 }
527}
#define RAW_BUF_BYTES(cstate)
#define INPUT_BUF_BYTES(cstate)
#define MAX_CONVERSION_INPUT_LENGTH
Definition: pg_wchar.h:320
int pg_encoding_verifymbstr(int encoding, const char *mbstr, int len)
Definition: wchar.c:2202
int pg_encoding_max_length(int encoding)
Definition: wchar.c:2213

References CopyFromStateData::conversion_proc, CopyFromStateData::file_encoding, GetDatabaseEncoding(), CopyFromStateData::input_buf, INPUT_BUF_BYTES, CopyFromStateData::input_buf_index, CopyFromStateData::input_buf_len, INPUT_BUF_SIZE, CopyFromStateData::input_reached_eof, CopyFromStateData::input_reached_error, MAX_CONVERSION_INPUT_LENGTH, CopyFromStateData::need_transcoding, pg_do_encoding_conversion_buf(), pg_encoding_max_length(), pg_encoding_verifymbstr(), CopyFromStateData::raw_buf, RAW_BUF_BYTES, CopyFromStateData::raw_buf_index, CopyFromStateData::raw_buf_len, and CopyFromStateData::raw_reached_eof.

Referenced by CopyLoadInputBuf().

◆ CopyFromBinaryOneRow()

bool CopyFromBinaryOneRow ( CopyFromState  cstate,
ExprContext econtext,
Datum values,
bool *  nulls 
)

Definition at line 1095 of file copyfromparse.c.

1097{
1098 TupleDesc tupDesc;
1099 AttrNumber attr_count;
1100 FmgrInfo *in_functions = cstate->in_functions;
1101 Oid *typioparams = cstate->typioparams;
1102 int16 fld_count;
1103 ListCell *cur;
1104
1105 tupDesc = RelationGetDescr(cstate->rel);
1106 attr_count = list_length(cstate->attnumlist);
1107
1108 cstate->cur_lineno++;
1109
1110 if (!CopyGetInt16(cstate, &fld_count))
1111 {
1112 /* EOF detected (end of file, or protocol-level EOF) */
1113 return false;
1114 }
1115
1116 if (fld_count == -1)
1117 {
1118 /*
1119 * Received EOF marker. Wait for the protocol-level EOF, and complain
1120 * if it doesn't come immediately. In COPY FROM STDIN, this ensures
1121 * that we correctly handle CopyFail, if client chooses to send that
1122 * now. When copying from file, we could ignore the rest of the file
1123 * like in text mode, but we choose to be consistent with the COPY
1124 * FROM STDIN case.
1125 */
1126 char dummy;
1127
1128 if (CopyReadBinaryData(cstate, &dummy, 1) > 0)
1129 ereport(ERROR,
1130 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1131 errmsg("received copy data after EOF marker")));
1132 return false;
1133 }
1134
1135 if (fld_count != attr_count)
1136 ereport(ERROR,
1137 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1138 errmsg("row field count is %d, expected %d",
1139 (int) fld_count, attr_count)));
1140
1141 foreach(cur, cstate->attnumlist)
1142 {
1143 int attnum = lfirst_int(cur);
1144 int m = attnum - 1;
1145 Form_pg_attribute att = TupleDescAttr(tupDesc, m);
1146
1147 cstate->cur_attname = NameStr(att->attname);
1148 values[m] = CopyReadBinaryAttribute(cstate,
1149 &in_functions[m],
1150 typioparams[m],
1151 att->atttypmod,
1152 &nulls[m]);
1153 cstate->cur_attname = NULL;
1154 }
1155
1156 return true;
1157}
int16 AttrNumber
Definition: attnum.h:21
static Datum values[MAXATTR]
Definition: bootstrap.c:153
#define NameStr(name)
Definition: c.h:752
int16_t int16
Definition: c.h:534
static bool CopyGetInt16(CopyFromState cstate, int16 *val)
static Datum CopyReadBinaryAttribute(CopyFromState cstate, FmgrInfo *flinfo, Oid typioparam, int32 typmod, bool *isnull)
static int CopyReadBinaryData(CopyFromState cstate, char *dest, int nbytes)
struct cursor * cur
Definition: ecpg.c:29
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ereport(elevel,...)
Definition: elog.h:150
int16 attnum
Definition: pg_attribute.h:74
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:202
static int list_length(const List *l)
Definition: pg_list.h:152
#define lfirst_int(lc)
Definition: pg_list.h:173
unsigned int Oid
Definition: postgres_ext.h:32
#define RelationGetDescr(relation)
Definition: rel.h:540
const char * cur_attname
Definition: fmgr.h:57
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:160

References attnum, CopyFromStateData::attnumlist, CopyGetInt16(), CopyReadBinaryAttribute(), CopyReadBinaryData(), cur, CopyFromStateData::cur_attname, CopyFromStateData::cur_lineno, ereport, errcode(), errmsg(), ERROR, CopyFromStateData::in_functions, lfirst_int, list_length(), NameStr, CopyFromStateData::rel, RelationGetDescr, TupleDescAttr(), CopyFromStateData::typioparams, and values.

◆ CopyFromCSVOneRow()

bool CopyFromCSVOneRow ( CopyFromState  cstate,
ExprContext econtext,
Datum values,
bool *  nulls 
)

Definition at line 933 of file copyfromparse.c.

935{
936 return CopyFromTextLikeOneRow(cstate, econtext, values, nulls, true);
937}
static pg_attribute_always_inline bool CopyFromTextLikeOneRow(CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls, bool is_csv)

References CopyFromTextLikeOneRow(), and values.

◆ CopyFromTextLikeOneRow()

static pg_attribute_always_inline bool CopyFromTextLikeOneRow ( CopyFromState  cstate,
ExprContext econtext,
Datum values,
bool *  nulls,
bool  is_csv 
)
static

Definition at line 946 of file copyfromparse.c.

948{
949 TupleDesc tupDesc;
950 AttrNumber attr_count;
951 FmgrInfo *in_functions = cstate->in_functions;
952 Oid *typioparams = cstate->typioparams;
953 ExprState **defexprs = cstate->defexprs;
954 char **field_strings;
955 ListCell *cur;
956 int fldct;
957 int fieldno;
958 char *string;
959
960 tupDesc = RelationGetDescr(cstate->rel);
961 attr_count = list_length(cstate->attnumlist);
962
963 /* read raw fields in the next line */
964 if (!NextCopyFromRawFieldsInternal(cstate, &field_strings, &fldct, is_csv))
965 return false;
966
967 /* check for overflowing fields */
968 if (attr_count > 0 && fldct > attr_count)
970 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
971 errmsg("extra data after last expected column")));
972
973 fieldno = 0;
974
975 /* Loop to read the user attributes on the line. */
976 foreach(cur, cstate->attnumlist)
977 {
978 int attnum = lfirst_int(cur);
979 int m = attnum - 1;
980 Form_pg_attribute att = TupleDescAttr(tupDesc, m);
981
982 if (fieldno >= fldct)
984 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
985 errmsg("missing data for column \"%s\"",
986 NameStr(att->attname))));
987 string = field_strings[fieldno++];
988
989 if (cstate->convert_select_flags &&
990 !cstate->convert_select_flags[m])
991 {
992 /* ignore input field, leaving column as NULL */
993 continue;
994 }
995
996 if (is_csv)
997 {
998 if (string == NULL &&
999 cstate->opts.force_notnull_flags[m])
1000 {
1001 /*
1002 * FORCE_NOT_NULL option is set and column is NULL - convert
1003 * it to the NULL string.
1004 */
1005 string = cstate->opts.null_print;
1006 }
1007 else if (string != NULL && cstate->opts.force_null_flags[m]
1008 && strcmp(string, cstate->opts.null_print) == 0)
1009 {
1010 /*
1011 * FORCE_NULL option is set and column matches the NULL
1012 * string. It must have been quoted, or otherwise the string
1013 * would already have been set to NULL. Convert it to NULL as
1014 * specified.
1015 */
1016 string = NULL;
1017 }
1018 }
1019
1020 cstate->cur_attname = NameStr(att->attname);
1021 cstate->cur_attval = string;
1022
1023 if (string != NULL)
1024 nulls[m] = false;
1025
1026 if (cstate->defaults[m])
1027 {
1028 /* We must have switched into the per-tuple memory context */
1029 Assert(econtext != NULL);
1031
1032 values[m] = ExecEvalExpr(defexprs[m], econtext, &nulls[m]);
1033 }
1034
1035 /*
1036 * If ON_ERROR is specified with IGNORE, skip rows with soft errors
1037 */
1038 else if (!InputFunctionCallSafe(&in_functions[m],
1039 string,
1040 typioparams[m],
1041 att->atttypmod,
1042 (Node *) cstate->escontext,
1043 &values[m]))
1044 {
1046
1047 cstate->num_errors++;
1048
1050 {
1051 /*
1052 * Since we emit line number and column info in the below
1053 * notice message, we suppress error context information other
1054 * than the relation name.
1055 */
1056 Assert(!cstate->relname_only);
1057 cstate->relname_only = true;
1058
1059 if (cstate->cur_attval)
1060 {
1061 char *attval;
1062
1063 attval = CopyLimitPrintoutLength(cstate->cur_attval);
1065 errmsg("skipping row due to data type incompatibility at line %" PRIu64 " for column \"%s\": \"%s\"",
1066 cstate->cur_lineno,
1067 cstate->cur_attname,
1068 attval));
1069 pfree(attval);
1070 }
1071 else
1073 errmsg("skipping row due to data type incompatibility at line %" PRIu64 " for column \"%s\": null input",
1074 cstate->cur_lineno,
1075 cstate->cur_attname));
1076
1077 /* reset relname_only */
1078 cstate->relname_only = false;
1079 }
1080
1081 return true;
1082 }
1083
1084 cstate->cur_attname = NULL;
1085 cstate->cur_attval = NULL;
1086 }
1087
1088 Assert(fieldno == attr_count);
1089
1090 return true;
1091}
char * CopyLimitPrintoutLength(const char *str)
Definition: copyfrom.c:333
static pg_attribute_always_inline bool NextCopyFromRawFieldsInternal(CopyFromState cstate, char ***fields, int *nfields, bool is_csv)
#define NOTICE
Definition: elog.h:35
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:390
bool InputFunctionCallSafe(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod, Node *escontext, Datum *result)
Definition: fmgr.c:1584
@ COPY_ON_ERROR_STOP
Definition: copy.h:36
@ COPY_LOG_VERBOSITY_VERBOSE
Definition: copy.h:48
void pfree(void *pointer)
Definition: mcxt.c:1594
MemoryContext CurrentMemoryContext
Definition: mcxt.c:160
char string[11]
Definition: preproc-type.c:52
CopyLogVerbosityChoice log_verbosity
Definition: copy.h:85
CopyOnErrorChoice on_error
Definition: copy.h:84
char * null_print
Definition: copy.h:66
bool * force_notnull_flags
Definition: copy.h:79
bool * force_null_flags
Definition: copy.h:82
CopyFormatOptions opts
const char * cur_attval
ErrorSaveContext * escontext
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:281
Definition: nodes.h:135

References Assert(), attnum, CopyFromStateData::attnumlist, CopyFromStateData::convert_select_flags, COPY_LOG_VERBOSITY_VERBOSE, COPY_ON_ERROR_STOP, CopyLimitPrintoutLength(), cur, CopyFromStateData::cur_attname, CopyFromStateData::cur_attval, CopyFromStateData::cur_lineno, CurrentMemoryContext, CopyFromStateData::defaults, CopyFromStateData::defexprs, ExprContext::ecxt_per_tuple_memory, ereport, errcode(), errmsg(), ERROR, CopyFromStateData::escontext, ExecEvalExpr(), CopyFormatOptions::force_notnull_flags, CopyFormatOptions::force_null_flags, CopyFromStateData::in_functions, InputFunctionCallSafe(), lfirst_int, list_length(), CopyFormatOptions::log_verbosity, NameStr, NextCopyFromRawFieldsInternal(), NOTICE, CopyFormatOptions::null_print, CopyFromStateData::num_errors, CopyFormatOptions::on_error, CopyFromStateData::opts, pfree(), CopyFromStateData::rel, RelationGetDescr, CopyFromStateData::relname_only, TupleDescAttr(), CopyFromStateData::typioparams, and values.

Referenced by CopyFromCSVOneRow(), and CopyFromTextOneRow().

◆ CopyFromTextOneRow()

bool CopyFromTextOneRow ( CopyFromState  cstate,
ExprContext econtext,
Datum values,
bool *  nulls 
)

Definition at line 925 of file copyfromparse.c.

927{
928 return CopyFromTextLikeOneRow(cstate, econtext, values, nulls, false);
929}

References CopyFromTextLikeOneRow(), and values.

◆ CopyGetData()

static int CopyGetData ( CopyFromState  cstate,
void *  databuf,
int  minread,
int  maxread 
)
static

Definition at line 245 of file copyfromparse.c.

246{
247 int bytesread = 0;
248
249 switch (cstate->copy_src)
250 {
251 case COPY_FILE:
252 bytesread = fread(databuf, 1, maxread, cstate->copy_file);
253 if (ferror(cstate->copy_file))
256 errmsg("could not read from COPY file: %m")));
257 if (bytesread == 0)
258 cstate->raw_reached_eof = true;
259 break;
260 case COPY_FRONTEND:
261 while (maxread > 0 && bytesread < minread && !cstate->raw_reached_eof)
262 {
263 int avail;
264
265 while (cstate->fe_msgbuf->cursor >= cstate->fe_msgbuf->len)
266 {
267 /* Try to receive another message */
268 int mtype;
269 int maxmsglen;
270
271 readmessage:
274 mtype = pq_getbyte();
275 if (mtype == EOF)
277 (errcode(ERRCODE_CONNECTION_FAILURE),
278 errmsg("unexpected EOF on client connection with an open transaction")));
279 /* Validate message type and set packet size limit */
280 switch (mtype)
281 {
282 case PqMsg_CopyData:
283 maxmsglen = PQ_LARGE_MESSAGE_LIMIT;
284 break;
285 case PqMsg_CopyDone:
286 case PqMsg_CopyFail:
287 case PqMsg_Flush:
288 case PqMsg_Sync:
289 maxmsglen = PQ_SMALL_MESSAGE_LIMIT;
290 break;
291 default:
293 (errcode(ERRCODE_PROTOCOL_VIOLATION),
294 errmsg("unexpected message type 0x%02X during COPY from stdin",
295 mtype)));
296 maxmsglen = 0; /* keep compiler quiet */
297 break;
298 }
299 /* Now collect the message body */
300 if (pq_getmessage(cstate->fe_msgbuf, maxmsglen))
302 (errcode(ERRCODE_CONNECTION_FAILURE),
303 errmsg("unexpected EOF on client connection with an open transaction")));
305 /* ... and process it */
306 switch (mtype)
307 {
308 case PqMsg_CopyData:
309 break;
310 case PqMsg_CopyDone:
311 /* COPY IN correctly terminated by frontend */
312 cstate->raw_reached_eof = true;
313 return bytesread;
314 case PqMsg_CopyFail:
316 (errcode(ERRCODE_QUERY_CANCELED),
317 errmsg("COPY from stdin failed: %s",
318 pq_getmsgstring(cstate->fe_msgbuf))));
319 break;
320 case PqMsg_Flush:
321 case PqMsg_Sync:
322
323 /*
324 * Ignore Flush/Sync for the convenience of client
325 * libraries (such as libpq) that may send those
326 * without noticing that the command they just
327 * sent was COPY.
328 */
329 goto readmessage;
330 default:
331 Assert(false); /* NOT REACHED */
332 }
333 }
334 avail = cstate->fe_msgbuf->len - cstate->fe_msgbuf->cursor;
335 if (avail > maxread)
336 avail = maxread;
337 pq_copymsgbytes(cstate->fe_msgbuf, databuf, avail);
338 databuf = (void *) ((char *) databuf + avail);
339 maxread -= avail;
340 bytesread += avail;
341 }
342 break;
343 case COPY_CALLBACK:
344 bytesread = cstate->data_source_cb(databuf, minread, maxread);
345 break;
346 }
347
348 return bytesread;
349}
@ COPY_FILE
Definition: copyto.c:45
@ COPY_CALLBACK
Definition: copyto.c:47
@ COPY_FRONTEND
Definition: copyto.c:46
int errcode_for_file_access(void)
Definition: elog.c:877
#define PQ_SMALL_MESSAGE_LIMIT
Definition: libpq.h:30
#define PQ_LARGE_MESSAGE_LIMIT
Definition: libpq.h:31
#define HOLD_CANCEL_INTERRUPTS()
Definition: miscadmin.h:141
#define RESUME_CANCEL_INTERRUPTS()
Definition: miscadmin.h:143
int pq_getmessage(StringInfo s, int maxlen)
Definition: pqcomm.c:1203
int pq_getbyte(void)
Definition: pqcomm.c:963
void pq_startmsgread(void)
Definition: pqcomm.c:1141
const char * pq_getmsgstring(StringInfo msg)
Definition: pqformat.c:579
void pq_copymsgbytes(StringInfo msg, void *buf, int datalen)
Definition: pqformat.c:528
#define PqMsg_CopyDone
Definition: protocol.h:64
#define PqMsg_CopyData
Definition: protocol.h:65
#define PqMsg_Sync
Definition: protocol.h:27
#define PqMsg_CopyFail
Definition: protocol.h:29
#define PqMsg_Flush
Definition: protocol.h:24
copy_data_source_cb data_source_cb

References Assert(), COPY_CALLBACK, COPY_FILE, CopyFromStateData::copy_file, COPY_FRONTEND, CopyFromStateData::copy_src, StringInfoData::cursor, CopyFromStateData::data_source_cb, ereport, errcode(), errcode_for_file_access(), errmsg(), ERROR, CopyFromStateData::fe_msgbuf, HOLD_CANCEL_INTERRUPTS, StringInfoData::len, pq_copymsgbytes(), pq_getbyte(), pq_getmessage(), pq_getmsgstring(), PQ_LARGE_MESSAGE_LIMIT, PQ_SMALL_MESSAGE_LIMIT, pq_startmsgread(), PqMsg_CopyData, PqMsg_CopyDone, PqMsg_CopyFail, PqMsg_Flush, PqMsg_Sync, CopyFromStateData::raw_reached_eof, and RESUME_CANCEL_INTERRUPTS.

Referenced by CopyLoadRawBuf(), and CopyReadLine().

◆ CopyGetInt16()

static bool CopyGetInt16 ( CopyFromState  cstate,
int16 val 
)
inlinestatic

Definition at line 379 of file copyfromparse.c.

380{
381 uint16 buf;
382
383 if (CopyReadBinaryData(cstate, (char *) &buf, sizeof(buf)) != sizeof(buf))
384 {
385 *val = 0; /* suppress compiler warning */
386 return false;
387 }
388 *val = (int16) pg_ntoh16(buf);
389 return true;
390}
uint16_t uint16
Definition: c.h:538
long val
Definition: informix.c:689
#define pg_ntoh16(x)
Definition: pg_bswap.h:124
static char * buf
Definition: pg_test_fsync.c:72

References buf, CopyReadBinaryData(), pg_ntoh16, and val.

Referenced by CopyFromBinaryOneRow().

◆ CopyGetInt32()

static bool CopyGetInt32 ( CopyFromState  cstate,
int32 val 
)
inlinestatic

Definition at line 362 of file copyfromparse.c.

363{
364 uint32 buf;
365
366 if (CopyReadBinaryData(cstate, (char *) &buf, sizeof(buf)) != sizeof(buf))
367 {
368 *val = 0; /* suppress compiler warning */
369 return false;
370 }
371 *val = (int32) pg_ntoh32(buf);
372 return true;
373}
int32_t int32
Definition: c.h:535
uint32_t uint32
Definition: c.h:539
#define pg_ntoh32(x)
Definition: pg_bswap.h:125

References buf, CopyReadBinaryData(), pg_ntoh32, and val.

Referenced by CopyReadBinaryAttribute(), and ReceiveCopyBinaryHeader().

◆ CopyLoadInputBuf()

static void CopyLoadInputBuf ( CopyFromState  cstate)
static

Definition at line 650 of file copyfromparse.c.

651{
652 int nbytes = INPUT_BUF_BYTES(cstate);
653
654 /*
655 * The caller has updated input_buf_index to indicate how much of the
656 * input has been consumed and isn't needed anymore. If input_buf is the
657 * same physical area as raw_buf, update raw_buf_index accordingly.
658 */
659 if (cstate->raw_buf == cstate->input_buf)
660 {
661 Assert(!cstate->need_transcoding);
662 Assert(cstate->input_buf_index >= cstate->raw_buf_index);
663 cstate->raw_buf_index = cstate->input_buf_index;
664 }
665
666 for (;;)
667 {
668 /* If we now have some unconverted data, try to convert it */
669 CopyConvertBuf(cstate);
670
671 /* If we now have some more input bytes ready, return them */
672 if (INPUT_BUF_BYTES(cstate) > nbytes)
673 return;
674
675 /*
676 * If we reached an invalid byte sequence, or we're at an incomplete
677 * multi-byte character but there is no more raw input data, report
678 * conversion error.
679 */
680 if (cstate->input_reached_error)
681 CopyConversionError(cstate);
682
683 /* no more input, and everything has been converted */
684 if (cstate->input_reached_eof)
685 break;
686
687 /* Try to load more raw data */
688 Assert(!cstate->raw_reached_eof);
689 CopyLoadRawBuf(cstate);
690 }
691}
static void CopyConversionError(CopyFromState cstate)
static void CopyLoadRawBuf(CopyFromState cstate)
static void CopyConvertBuf(CopyFromState cstate)

References Assert(), CopyConversionError(), CopyConvertBuf(), CopyLoadRawBuf(), CopyFromStateData::input_buf, INPUT_BUF_BYTES, CopyFromStateData::input_buf_index, CopyFromStateData::input_reached_eof, CopyFromStateData::input_reached_error, CopyFromStateData::need_transcoding, CopyFromStateData::raw_buf, CopyFromStateData::raw_buf_index, and CopyFromStateData::raw_reached_eof.

Referenced by CopyReadLineText().

◆ CopyLoadRawBuf()

static void CopyLoadRawBuf ( CopyFromState  cstate)
static

Definition at line 590 of file copyfromparse.c.

591{
592 int nbytes;
593 int inbytes;
594
595 /*
596 * In text mode, if encoding conversion is not required, raw_buf and
597 * input_buf point to the same buffer. Their len/index better agree, too.
598 */
599 if (cstate->raw_buf == cstate->input_buf)
600 {
601 Assert(!cstate->need_transcoding);
602 Assert(cstate->raw_buf_index == cstate->input_buf_index);
603 Assert(cstate->input_buf_len <= cstate->raw_buf_len);
604 }
605
606 /*
607 * Copy down the unprocessed data if any.
608 */
609 nbytes = RAW_BUF_BYTES(cstate);
610 if (nbytes > 0 && cstate->raw_buf_index > 0)
611 memmove(cstate->raw_buf, cstate->raw_buf + cstate->raw_buf_index,
612 nbytes);
613 cstate->raw_buf_len -= cstate->raw_buf_index;
614 cstate->raw_buf_index = 0;
615
616 /*
617 * If raw_buf and input_buf are in fact the same buffer, adjust the
618 * input_buf variables, too.
619 */
620 if (cstate->raw_buf == cstate->input_buf)
621 {
622 cstate->input_buf_len -= cstate->input_buf_index;
623 cstate->input_buf_index = 0;
624 }
625
626 /* Load more data */
627 inbytes = CopyGetData(cstate, cstate->raw_buf + cstate->raw_buf_len,
628 1, RAW_BUF_SIZE - cstate->raw_buf_len);
629 nbytes += inbytes;
630 cstate->raw_buf[nbytes] = '\0';
631 cstate->raw_buf_len = nbytes;
632
633 cstate->bytes_processed += inbytes;
635
636 if (inbytes == 0)
637 cstate->raw_reached_eof = true;
638}
void pgstat_progress_update_param(int index, int64 val)
#define RAW_BUF_SIZE
static int CopyGetData(CopyFromState cstate, void *databuf, int minread, int maxread)
#define PROGRESS_COPY_BYTES_PROCESSED
Definition: progress.h:147

References Assert(), CopyFromStateData::bytes_processed, CopyGetData(), CopyFromStateData::input_buf, CopyFromStateData::input_buf_index, CopyFromStateData::input_buf_len, CopyFromStateData::need_transcoding, pgstat_progress_update_param(), PROGRESS_COPY_BYTES_PROCESSED, CopyFromStateData::raw_buf, RAW_BUF_BYTES, CopyFromStateData::raw_buf_index, CopyFromStateData::raw_buf_len, RAW_BUF_SIZE, and CopyFromStateData::raw_reached_eof.

Referenced by CopyLoadInputBuf(), and CopyReadBinaryData().

◆ CopyReadAttributesCSV()

static int CopyReadAttributesCSV ( CopyFromState  cstate)
static

Definition at line 1827 of file copyfromparse.c.

1828{
1829 char delimc = cstate->opts.delim[0];
1830 char quotec = cstate->opts.quote[0];
1831 char escapec = cstate->opts.escape[0];
1832 int fieldno;
1833 char *output_ptr;
1834 char *cur_ptr;
1835 char *line_end_ptr;
1836
1837 /*
1838 * We need a special case for zero-column tables: check that the input
1839 * line is empty, and return.
1840 */
1841 if (cstate->max_fields <= 0)
1842 {
1843 if (cstate->line_buf.len != 0)
1844 ereport(ERROR,
1845 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1846 errmsg("extra data after last expected column")));
1847 return 0;
1848 }
1849
1851
1852 /*
1853 * The de-escaped attributes will certainly not be longer than the input
1854 * data line, so we can just force attribute_buf to be large enough and
1855 * then transfer data without any checks for enough space. We need to do
1856 * it this way because enlarging attribute_buf mid-stream would invalidate
1857 * pointers already stored into cstate->raw_fields[].
1858 */
1859 if (cstate->attribute_buf.maxlen <= cstate->line_buf.len)
1860 enlargeStringInfo(&cstate->attribute_buf, cstate->line_buf.len);
1861 output_ptr = cstate->attribute_buf.data;
1862
1863 /* set pointer variables for loop */
1864 cur_ptr = cstate->line_buf.data;
1865 line_end_ptr = cstate->line_buf.data + cstate->line_buf.len;
1866
1867 /* Outer loop iterates over fields */
1868 fieldno = 0;
1869 for (;;)
1870 {
1871 bool found_delim = false;
1872 bool saw_quote = false;
1873 char *start_ptr;
1874 char *end_ptr;
1875 int input_len;
1876
1877 /* Make sure there is enough space for the next value */
1878 if (fieldno >= cstate->max_fields)
1879 {
1880 cstate->max_fields *= 2;
1881 cstate->raw_fields =
1882 repalloc(cstate->raw_fields, cstate->max_fields * sizeof(char *));
1883 }
1884
1885 /* Remember start of field on both input and output sides */
1886 start_ptr = cur_ptr;
1887 cstate->raw_fields[fieldno] = output_ptr;
1888
1889 /*
1890 * Scan data for field,
1891 *
1892 * The loop starts in "not quote" mode and then toggles between that
1893 * and "in quote" mode. The loop exits normally if it is in "not
1894 * quote" mode and a delimiter or line end is seen.
1895 */
1896 for (;;)
1897 {
1898 char c;
1899
1900 /* Not in quote */
1901 for (;;)
1902 {
1903 end_ptr = cur_ptr;
1904 if (cur_ptr >= line_end_ptr)
1905 goto endfield;
1906 c = *cur_ptr++;
1907 /* unquoted field delimiter */
1908 if (c == delimc)
1909 {
1910 found_delim = true;
1911 goto endfield;
1912 }
1913 /* start of quoted field (or part of field) */
1914 if (c == quotec)
1915 {
1916 saw_quote = true;
1917 break;
1918 }
1919 /* Add c to output string */
1920 *output_ptr++ = c;
1921 }
1922
1923 /* In quote */
1924 for (;;)
1925 {
1926 end_ptr = cur_ptr;
1927 if (cur_ptr >= line_end_ptr)
1928 ereport(ERROR,
1929 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1930 errmsg("unterminated CSV quoted field")));
1931
1932 c = *cur_ptr++;
1933
1934 /* escape within a quoted field */
1935 if (c == escapec)
1936 {
1937 /*
1938 * peek at the next char if available, and escape it if it
1939 * is an escape char or a quote char
1940 */
1941 if (cur_ptr < line_end_ptr)
1942 {
1943 char nextc = *cur_ptr;
1944
1945 if (nextc == escapec || nextc == quotec)
1946 {
1947 *output_ptr++ = nextc;
1948 cur_ptr++;
1949 continue;
1950 }
1951 }
1952 }
1953
1954 /*
1955 * end of quoted field. Must do this test after testing for
1956 * escape in case quote char and escape char are the same
1957 * (which is the common case).
1958 */
1959 if (c == quotec)
1960 break;
1961
1962 /* Add c to output string */
1963 *output_ptr++ = c;
1964 }
1965 }
1966endfield:
1967
1968 /* Terminate attribute value in output area */
1969 *output_ptr++ = '\0';
1970
1971 /* Check whether raw input matched null marker */
1972 input_len = end_ptr - start_ptr;
1973 if (!saw_quote && input_len == cstate->opts.null_print_len &&
1974 strncmp(start_ptr, cstate->opts.null_print, input_len) == 0)
1975 cstate->raw_fields[fieldno] = NULL;
1976 /* Check whether raw input matched default marker */
1977 else if (fieldno < list_length(cstate->attnumlist) &&
1978 cstate->opts.default_print &&
1979 input_len == cstate->opts.default_print_len &&
1980 strncmp(start_ptr, cstate->opts.default_print, input_len) == 0)
1981 {
1982 /* fieldno is 0-index and attnum is 1-index */
1983 int m = list_nth_int(cstate->attnumlist, fieldno) - 1;
1984
1985 if (cstate->defexprs[m] != NULL)
1986 {
1987 /* defaults contain entries for all physical attributes */
1988 cstate->defaults[m] = true;
1989 }
1990 else
1991 {
1992 TupleDesc tupDesc = RelationGetDescr(cstate->rel);
1993 Form_pg_attribute att = TupleDescAttr(tupDesc, m);
1994
1995 ereport(ERROR,
1996 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1997 errmsg("unexpected default marker in COPY data"),
1998 errdetail("Column \"%s\" has no default value.",
1999 NameStr(att->attname))));
2000 }
2001 }
2002
2003 fieldno++;
2004 /* Done if we hit EOL instead of a delim */
2005 if (!found_delim)
2006 break;
2007 }
2008
2009 /* Clean up state of attribute_buf */
2010 output_ptr--;
2011 Assert(*output_ptr == '\0');
2012 cstate->attribute_buf.len = (output_ptr - cstate->attribute_buf.data);
2013
2014 return fieldno;
2015}
int errdetail(const char *fmt,...)
Definition: elog.c:1207
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1610
static int list_nth_int(const List *list, int n)
Definition: pg_list.h:310
char * c
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:126
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:337
int default_print_len
Definition: copy.h:70
int null_print_len
Definition: copy.h:67
char * quote
Definition: copy.h:72
char * escape
Definition: copy.h:73
char * delim
Definition: copy.h:71
char * default_print
Definition: copy.h:69
StringInfoData line_buf
StringInfoData attribute_buf

References Assert(), CopyFromStateData::attnumlist, CopyFromStateData::attribute_buf, StringInfoData::data, CopyFormatOptions::default_print, CopyFormatOptions::default_print_len, CopyFromStateData::defaults, CopyFromStateData::defexprs, CopyFormatOptions::delim, enlargeStringInfo(), ereport, errcode(), errdetail(), errmsg(), ERROR, CopyFormatOptions::escape, StringInfoData::len, CopyFromStateData::line_buf, list_length(), list_nth_int(), CopyFromStateData::max_fields, StringInfoData::maxlen, NameStr, CopyFormatOptions::null_print, CopyFormatOptions::null_print_len, CopyFromStateData::opts, CopyFormatOptions::quote, CopyFromStateData::raw_fields, CopyFromStateData::rel, RelationGetDescr, repalloc(), resetStringInfo(), and TupleDescAttr().

Referenced by NextCopyFromRawFieldsInternal().

◆ CopyReadAttributesText()

static int CopyReadAttributesText ( CopyFromState  cstate)
static

Definition at line 1573 of file copyfromparse.c.

1574{
1575 char delimc = cstate->opts.delim[0];
1576 int fieldno;
1577 char *output_ptr;
1578 char *cur_ptr;
1579 char *line_end_ptr;
1580
1581 /*
1582 * We need a special case for zero-column tables: check that the input
1583 * line is empty, and return.
1584 */
1585 if (cstate->max_fields <= 0)
1586 {
1587 if (cstate->line_buf.len != 0)
1588 ereport(ERROR,
1589 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1590 errmsg("extra data after last expected column")));
1591 return 0;
1592 }
1593
1595
1596 /*
1597 * The de-escaped attributes will certainly not be longer than the input
1598 * data line, so we can just force attribute_buf to be large enough and
1599 * then transfer data without any checks for enough space. We need to do
1600 * it this way because enlarging attribute_buf mid-stream would invalidate
1601 * pointers already stored into cstate->raw_fields[].
1602 */
1603 if (cstate->attribute_buf.maxlen <= cstate->line_buf.len)
1604 enlargeStringInfo(&cstate->attribute_buf, cstate->line_buf.len);
1605 output_ptr = cstate->attribute_buf.data;
1606
1607 /* set pointer variables for loop */
1608 cur_ptr = cstate->line_buf.data;
1609 line_end_ptr = cstate->line_buf.data + cstate->line_buf.len;
1610
1611 /* Outer loop iterates over fields */
1612 fieldno = 0;
1613 for (;;)
1614 {
1615 bool found_delim = false;
1616 char *start_ptr;
1617 char *end_ptr;
1618 int input_len;
1619 bool saw_non_ascii = false;
1620
1621 /* Make sure there is enough space for the next value */
1622 if (fieldno >= cstate->max_fields)
1623 {
1624 cstate->max_fields *= 2;
1625 cstate->raw_fields =
1626 repalloc(cstate->raw_fields, cstate->max_fields * sizeof(char *));
1627 }
1628
1629 /* Remember start of field on both input and output sides */
1630 start_ptr = cur_ptr;
1631 cstate->raw_fields[fieldno] = output_ptr;
1632
1633 /*
1634 * Scan data for field.
1635 *
1636 * Note that in this loop, we are scanning to locate the end of field
1637 * and also speculatively performing de-escaping. Once we find the
1638 * end-of-field, we can match the raw field contents against the null
1639 * marker string. Only after that comparison fails do we know that
1640 * de-escaping is actually the right thing to do; therefore we *must
1641 * not* throw any syntax errors before we've done the null-marker
1642 * check.
1643 */
1644 for (;;)
1645 {
1646 char c;
1647
1648 end_ptr = cur_ptr;
1649 if (cur_ptr >= line_end_ptr)
1650 break;
1651 c = *cur_ptr++;
1652 if (c == delimc)
1653 {
1654 found_delim = true;
1655 break;
1656 }
1657 if (c == '\\')
1658 {
1659 if (cur_ptr >= line_end_ptr)
1660 break;
1661 c = *cur_ptr++;
1662 switch (c)
1663 {
1664 case '0':
1665 case '1':
1666 case '2':
1667 case '3':
1668 case '4':
1669 case '5':
1670 case '6':
1671 case '7':
1672 {
1673 /* handle \013 */
1674 int val;
1675
1676 val = OCTVALUE(c);
1677 if (cur_ptr < line_end_ptr)
1678 {
1679 c = *cur_ptr;
1680 if (ISOCTAL(c))
1681 {
1682 cur_ptr++;
1683 val = (val << 3) + OCTVALUE(c);
1684 if (cur_ptr < line_end_ptr)
1685 {
1686 c = *cur_ptr;
1687 if (ISOCTAL(c))
1688 {
1689 cur_ptr++;
1690 val = (val << 3) + OCTVALUE(c);
1691 }
1692 }
1693 }
1694 }
1695 c = val & 0377;
1696 if (c == '\0' || IS_HIGHBIT_SET(c))
1697 saw_non_ascii = true;
1698 }
1699 break;
1700 case 'x':
1701 /* Handle \x3F */
1702 if (cur_ptr < line_end_ptr)
1703 {
1704 char hexchar = *cur_ptr;
1705
1706 if (isxdigit((unsigned char) hexchar))
1707 {
1708 int val = GetDecimalFromHex(hexchar);
1709
1710 cur_ptr++;
1711 if (cur_ptr < line_end_ptr)
1712 {
1713 hexchar = *cur_ptr;
1714 if (isxdigit((unsigned char) hexchar))
1715 {
1716 cur_ptr++;
1717 val = (val << 4) + GetDecimalFromHex(hexchar);
1718 }
1719 }
1720 c = val & 0xff;
1721 if (c == '\0' || IS_HIGHBIT_SET(c))
1722 saw_non_ascii = true;
1723 }
1724 }
1725 break;
1726 case 'b':
1727 c = '\b';
1728 break;
1729 case 'f':
1730 c = '\f';
1731 break;
1732 case 'n':
1733 c = '\n';
1734 break;
1735 case 'r':
1736 c = '\r';
1737 break;
1738 case 't':
1739 c = '\t';
1740 break;
1741 case 'v':
1742 c = '\v';
1743 break;
1744
1745 /*
1746 * in all other cases, take the char after '\'
1747 * literally
1748 */
1749 }
1750 }
1751
1752 /* Add c to output string */
1753 *output_ptr++ = c;
1754 }
1755
1756 /* Check whether raw input matched null marker */
1757 input_len = end_ptr - start_ptr;
1758 if (input_len == cstate->opts.null_print_len &&
1759 strncmp(start_ptr, cstate->opts.null_print, input_len) == 0)
1760 cstate->raw_fields[fieldno] = NULL;
1761 /* Check whether raw input matched default marker */
1762 else if (fieldno < list_length(cstate->attnumlist) &&
1763 cstate->opts.default_print &&
1764 input_len == cstate->opts.default_print_len &&
1765 strncmp(start_ptr, cstate->opts.default_print, input_len) == 0)
1766 {
1767 /* fieldno is 0-indexed and attnum is 1-indexed */
1768 int m = list_nth_int(cstate->attnumlist, fieldno) - 1;
1769
1770 if (cstate->defexprs[m] != NULL)
1771 {
1772 /* defaults contain entries for all physical attributes */
1773 cstate->defaults[m] = true;
1774 }
1775 else
1776 {
1777 TupleDesc tupDesc = RelationGetDescr(cstate->rel);
1778 Form_pg_attribute att = TupleDescAttr(tupDesc, m);
1779
1780 ereport(ERROR,
1781 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1782 errmsg("unexpected default marker in COPY data"),
1783 errdetail("Column \"%s\" has no default value.",
1784 NameStr(att->attname))));
1785 }
1786 }
1787 else
1788 {
1789 /*
1790 * At this point we know the field is supposed to contain data.
1791 *
1792 * If we de-escaped any non-7-bit-ASCII chars, make sure the
1793 * resulting string is valid data for the db encoding.
1794 */
1795 if (saw_non_ascii)
1796 {
1797 char *fld = cstate->raw_fields[fieldno];
1798
1799 pg_verifymbstr(fld, output_ptr - fld, false);
1800 }
1801 }
1802
1803 /* Terminate attribute value in output area */
1804 *output_ptr++ = '\0';
1805
1806 fieldno++;
1807 /* Done if we hit EOL instead of a delim */
1808 if (!found_delim)
1809 break;
1810 }
1811
1812 /* Clean up state of attribute_buf */
1813 output_ptr--;
1814 Assert(*output_ptr == '\0');
1815 cstate->attribute_buf.len = (output_ptr - cstate->attribute_buf.data);
1816
1817 return fieldno;
1818}
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1155
#define OCTVALUE(c)
Definition: copyfromparse.c:79
#define ISOCTAL(c)
Definition: copyfromparse.c:78
static int GetDecimalFromHex(char hex)
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: mbutils.c:1557

References Assert(), CopyFromStateData::attnumlist, CopyFromStateData::attribute_buf, StringInfoData::data, CopyFormatOptions::default_print, CopyFormatOptions::default_print_len, CopyFromStateData::defaults, CopyFromStateData::defexprs, CopyFormatOptions::delim, enlargeStringInfo(), ereport, errcode(), errdetail(), errmsg(), ERROR, GetDecimalFromHex(), if(), IS_HIGHBIT_SET, ISOCTAL, StringInfoData::len, CopyFromStateData::line_buf, list_length(), list_nth_int(), CopyFromStateData::max_fields, StringInfoData::maxlen, NameStr, CopyFormatOptions::null_print, CopyFormatOptions::null_print_len, OCTVALUE, CopyFromStateData::opts, pg_verifymbstr(), CopyFromStateData::raw_fields, CopyFromStateData::rel, RelationGetDescr, repalloc(), resetStringInfo(), TupleDescAttr(), and val.

Referenced by NextCopyFromRawFieldsInternal().

◆ CopyReadBinaryAttribute()

static Datum CopyReadBinaryAttribute ( CopyFromState  cstate,
FmgrInfo flinfo,
Oid  typioparam,
int32  typmod,
bool *  isnull 
)
static

Definition at line 2022 of file copyfromparse.c.

2025{
2026 int32 fld_size;
2027 Datum result;
2028
2029 if (!CopyGetInt32(cstate, &fld_size))
2030 ereport(ERROR,
2031 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2032 errmsg("unexpected EOF in COPY data")));
2033 if (fld_size == -1)
2034 {
2035 *isnull = true;
2036 return ReceiveFunctionCall(flinfo, NULL, typioparam, typmod);
2037 }
2038 if (fld_size < 0)
2039 ereport(ERROR,
2040 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2041 errmsg("invalid field size")));
2042
2043 /* reset attribute_buf to empty, and load raw data in it */
2045
2046 enlargeStringInfo(&cstate->attribute_buf, fld_size);
2047 if (CopyReadBinaryData(cstate, cstate->attribute_buf.data,
2048 fld_size) != fld_size)
2049 ereport(ERROR,
2050 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2051 errmsg("unexpected EOF in COPY data")));
2052
2053 cstate->attribute_buf.len = fld_size;
2054 cstate->attribute_buf.data[fld_size] = '\0';
2055
2056 /* Call the column type's binary input converter */
2057 result = ReceiveFunctionCall(flinfo, &cstate->attribute_buf,
2058 typioparam, typmod);
2059
2060 /* Trouble if it didn't eat the whole buffer */
2061 if (cstate->attribute_buf.cursor != cstate->attribute_buf.len)
2062 ereport(ERROR,
2063 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
2064 errmsg("incorrect binary data format")));
2065
2066 *isnull = false;
2067 return result;
2068}
static bool CopyGetInt32(CopyFromState cstate, int32 *val)
Datum ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf, Oid typioparam, int32 typmod)
Definition: fmgr.c:1696
uint64_t Datum
Definition: postgres.h:70

References CopyFromStateData::attribute_buf, CopyGetInt32(), CopyReadBinaryData(), StringInfoData::cursor, StringInfoData::data, enlargeStringInfo(), ereport, errcode(), errmsg(), ERROR, StringInfoData::len, ReceiveFunctionCall(), and resetStringInfo().

Referenced by CopyFromBinaryOneRow().

◆ CopyReadBinaryData()

static int CopyReadBinaryData ( CopyFromState  cstate,
char *  dest,
int  nbytes 
)
static

Definition at line 701 of file copyfromparse.c.

702{
703 int copied_bytes = 0;
704
705 if (RAW_BUF_BYTES(cstate) >= nbytes)
706 {
707 /* Enough bytes are present in the buffer. */
708 memcpy(dest, cstate->raw_buf + cstate->raw_buf_index, nbytes);
709 cstate->raw_buf_index += nbytes;
710 copied_bytes = nbytes;
711 }
712 else
713 {
714 /*
715 * Not enough bytes in the buffer, so must read from the file. Need
716 * to loop since 'nbytes' could be larger than the buffer size.
717 */
718 do
719 {
720 int copy_bytes;
721
722 /* Load more data if buffer is empty. */
723 if (RAW_BUF_BYTES(cstate) == 0)
724 {
725 CopyLoadRawBuf(cstate);
726 if (cstate->raw_reached_eof)
727 break; /* EOF */
728 }
729
730 /* Transfer some bytes. */
731 copy_bytes = Min(nbytes - copied_bytes, RAW_BUF_BYTES(cstate));
732 memcpy(dest, cstate->raw_buf + cstate->raw_buf_index, copy_bytes);
733 cstate->raw_buf_index += copy_bytes;
734 dest += copy_bytes;
735 copied_bytes += copy_bytes;
736 } while (copied_bytes < nbytes);
737 }
738
739 return copied_bytes;
740}
#define Min(x, y)
Definition: c.h:1004

References CopyLoadRawBuf(), generate_unaccent_rules::dest, Min, CopyFromStateData::raw_buf, RAW_BUF_BYTES, CopyFromStateData::raw_buf_index, and CopyFromStateData::raw_reached_eof.

Referenced by CopyFromBinaryOneRow(), CopyGetInt16(), CopyGetInt32(), CopyReadBinaryAttribute(), and ReceiveCopyBinaryHeader().

◆ CopyReadLine()

static bool CopyReadLine ( CopyFromState  cstate,
bool  is_csv 
)
static

Definition at line 1167 of file copyfromparse.c.

1168{
1169 bool result;
1170
1171 resetStringInfo(&cstate->line_buf);
1172 cstate->line_buf_valid = false;
1173
1174 /* Parse data and transfer into line_buf */
1175 result = CopyReadLineText(cstate, is_csv);
1176
1177 if (result)
1178 {
1179 /*
1180 * Reached EOF. In protocol version 3, we should ignore anything
1181 * after \. up to the protocol end of copy data. (XXX maybe better
1182 * not to treat \. as special?)
1183 */
1184 if (cstate->copy_src == COPY_FRONTEND)
1185 {
1186 int inbytes;
1187
1188 do
1189 {
1190 inbytes = CopyGetData(cstate, cstate->input_buf,
1191 1, INPUT_BUF_SIZE);
1192 } while (inbytes > 0);
1193 cstate->input_buf_index = 0;
1194 cstate->input_buf_len = 0;
1195 cstate->raw_buf_index = 0;
1196 cstate->raw_buf_len = 0;
1197 }
1198 }
1199 else
1200 {
1201 /*
1202 * If we didn't hit EOF, then we must have transferred the EOL marker
1203 * to line_buf along with the data. Get rid of it.
1204 */
1205 switch (cstate->eol_type)
1206 {
1207 case EOL_NL:
1208 Assert(cstate->line_buf.len >= 1);
1209 Assert(cstate->line_buf.data[cstate->line_buf.len - 1] == '\n');
1210 cstate->line_buf.len--;
1211 cstate->line_buf.data[cstate->line_buf.len] = '\0';
1212 break;
1213 case EOL_CR:
1214 Assert(cstate->line_buf.len >= 1);
1215 Assert(cstate->line_buf.data[cstate->line_buf.len - 1] == '\r');
1216 cstate->line_buf.len--;
1217 cstate->line_buf.data[cstate->line_buf.len] = '\0';
1218 break;
1219 case EOL_CRNL:
1220 Assert(cstate->line_buf.len >= 2);
1221 Assert(cstate->line_buf.data[cstate->line_buf.len - 2] == '\r');
1222 Assert(cstate->line_buf.data[cstate->line_buf.len - 1] == '\n');
1223 cstate->line_buf.len -= 2;
1224 cstate->line_buf.data[cstate->line_buf.len] = '\0';
1225 break;
1226 case EOL_UNKNOWN:
1227 /* shouldn't get here */
1228 Assert(false);
1229 break;
1230 }
1231 }
1232
1233 /* Now it's safe to use the buffer in error messages */
1234 cstate->line_buf_valid = true;
1235
1236 return result;
1237}
@ EOL_CR
@ EOL_CRNL
@ EOL_UNKNOWN
@ EOL_NL
static bool CopyReadLineText(CopyFromState cstate, bool is_csv)

References Assert(), COPY_FRONTEND, CopyFromStateData::copy_src, CopyGetData(), CopyReadLineText(), StringInfoData::data, EOL_CR, EOL_CRNL, EOL_NL, CopyFromStateData::eol_type, EOL_UNKNOWN, CopyFromStateData::input_buf, CopyFromStateData::input_buf_index, CopyFromStateData::input_buf_len, INPUT_BUF_SIZE, StringInfoData::len, CopyFromStateData::line_buf, CopyFromStateData::line_buf_valid, CopyFromStateData::raw_buf_index, CopyFromStateData::raw_buf_len, and resetStringInfo().

Referenced by NextCopyFromRawFieldsInternal().

◆ CopyReadLineText()

static bool CopyReadLineText ( CopyFromState  cstate,
bool  is_csv 
)
static

Definition at line 1243 of file copyfromparse.c.

1244{
1245 char *copy_input_buf;
1246 int input_buf_ptr;
1247 int copy_buf_len;
1248 bool need_data = false;
1249 bool hit_eof = false;
1250 bool result = false;
1251
1252 /* CSV variables */
1253 bool in_quote = false,
1254 last_was_esc = false;
1255 char quotec = '\0';
1256 char escapec = '\0';
1257
1258 if (is_csv)
1259 {
1260 quotec = cstate->opts.quote[0];
1261 escapec = cstate->opts.escape[0];
1262 /* ignore special escape processing if it's the same as quotec */
1263 if (quotec == escapec)
1264 escapec = '\0';
1265 }
1266
1267 /*
1268 * The objective of this loop is to transfer the entire next input line
1269 * into line_buf. Hence, we only care for detecting newlines (\r and/or
1270 * \n) and the end-of-copy marker (\.).
1271 *
1272 * In CSV mode, \r and \n inside a quoted field are just part of the data
1273 * value and are put in line_buf. We keep just enough state to know if we
1274 * are currently in a quoted field or not.
1275 *
1276 * The input has already been converted to the database encoding. All
1277 * supported server encodings have the property that all bytes in a
1278 * multi-byte sequence have the high bit set, so a multibyte character
1279 * cannot contain any newline or escape characters embedded in the
1280 * multibyte sequence. Therefore, we can process the input byte-by-byte,
1281 * regardless of the encoding.
1282 *
1283 * For speed, we try to move data from input_buf to line_buf in chunks
1284 * rather than one character at a time. input_buf_ptr points to the next
1285 * character to examine; any characters from input_buf_index to
1286 * input_buf_ptr have been determined to be part of the line, but not yet
1287 * transferred to line_buf.
1288 *
1289 * For a little extra speed within the loop, we copy input_buf and
1290 * input_buf_len into local variables.
1291 */
1292 copy_input_buf = cstate->input_buf;
1293 input_buf_ptr = cstate->input_buf_index;
1294 copy_buf_len = cstate->input_buf_len;
1295
1296 for (;;)
1297 {
1298 int prev_raw_ptr;
1299 char c;
1300
1301 /*
1302 * Load more data if needed.
1303 *
1304 * TODO: We could just force four bytes of read-ahead and avoid the
1305 * many calls to IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(). That was
1306 * unsafe with the old v2 COPY protocol, but we don't support that
1307 * anymore.
1308 */
1309 if (input_buf_ptr >= copy_buf_len || need_data)
1310 {
1312
1313 CopyLoadInputBuf(cstate);
1314 /* update our local variables */
1315 hit_eof = cstate->input_reached_eof;
1316 input_buf_ptr = cstate->input_buf_index;
1317 copy_buf_len = cstate->input_buf_len;
1318
1319 /*
1320 * If we are completely out of data, break out of the loop,
1321 * reporting EOF.
1322 */
1323 if (INPUT_BUF_BYTES(cstate) <= 0)
1324 {
1325 result = true;
1326 break;
1327 }
1328 need_data = false;
1329 }
1330
1331 /* OK to fetch a character */
1332 prev_raw_ptr = input_buf_ptr;
1333 c = copy_input_buf[input_buf_ptr++];
1334
1335 if (is_csv)
1336 {
1337 /*
1338 * If character is '\r', we may need to look ahead below. Force
1339 * fetch of the next character if we don't already have it. We
1340 * need to do this before changing CSV state, in case '\r' is also
1341 * the quote or escape character.
1342 */
1343 if (c == '\r')
1344 {
1346 }
1347
1348 /*
1349 * Dealing with quotes and escapes here is mildly tricky. If the
1350 * quote char is also the escape char, there's no problem - we
1351 * just use the char as a toggle. If they are different, we need
1352 * to ensure that we only take account of an escape inside a
1353 * quoted field and immediately preceding a quote char, and not
1354 * the second in an escape-escape sequence.
1355 */
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;
1360 if (c != escapec)
1361 last_was_esc = false;
1362
1363 /*
1364 * Updating the line count for embedded CR and/or LF chars is
1365 * necessarily a little fragile - this test is probably about the
1366 * best we can do. (XXX it's arguable whether we should do this
1367 * at all --- is cur_lineno a physical or logical count?)
1368 */
1369 if (in_quote && c == (cstate->eol_type == EOL_NL ? '\n' : '\r'))
1370 cstate->cur_lineno++;
1371 }
1372
1373 /* Process \r */
1374 if (c == '\r' && (!is_csv || !in_quote))
1375 {
1376 /* Check for \r\n on first line, _and_ handle \r\n. */
1377 if (cstate->eol_type == EOL_UNKNOWN ||
1378 cstate->eol_type == EOL_CRNL)
1379 {
1380 /*
1381 * If need more data, go back to loop top to load it.
1382 *
1383 * Note that if we are at EOF, c will wind up as '\0' because
1384 * of the guaranteed pad of input_buf.
1385 */
1387
1388 /* get next char */
1389 c = copy_input_buf[input_buf_ptr];
1390
1391 if (c == '\n')
1392 {
1393 input_buf_ptr++; /* eat newline */
1394 cstate->eol_type = EOL_CRNL; /* in case not set yet */
1395 }
1396 else
1397 {
1398 /* found \r, but no \n */
1399 if (cstate->eol_type == EOL_CRNL)
1400 ereport(ERROR,
1401 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1402 !is_csv ?
1403 errmsg("literal carriage return found in data") :
1404 errmsg("unquoted carriage return found in data"),
1405 !is_csv ?
1406 errhint("Use \"\\r\" to represent carriage return.") :
1407 errhint("Use quoted CSV field to represent carriage return.")));
1408
1409 /*
1410 * if we got here, it is the first line and we didn't find
1411 * \n, so don't consume the peeked character
1412 */
1413 cstate->eol_type = EOL_CR;
1414 }
1415 }
1416 else if (cstate->eol_type == EOL_NL)
1417 ereport(ERROR,
1418 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1419 !is_csv ?
1420 errmsg("literal carriage return found in data") :
1421 errmsg("unquoted carriage return found in data"),
1422 !is_csv ?
1423 errhint("Use \"\\r\" to represent carriage return.") :
1424 errhint("Use quoted CSV field to represent carriage return.")));
1425 /* If reach here, we have found the line terminator */
1426 break;
1427 }
1428
1429 /* Process \n */
1430 if (c == '\n' && (!is_csv || !in_quote))
1431 {
1432 if (cstate->eol_type == EOL_CR || cstate->eol_type == EOL_CRNL)
1433 ereport(ERROR,
1434 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1435 !is_csv ?
1436 errmsg("literal newline found in data") :
1437 errmsg("unquoted newline found in data"),
1438 !is_csv ?
1439 errhint("Use \"\\n\" to represent newline.") :
1440 errhint("Use quoted CSV field to represent newline.")));
1441 cstate->eol_type = EOL_NL; /* in case not set yet */
1442 /* If reach here, we have found the line terminator */
1443 break;
1444 }
1445
1446 /*
1447 * Process backslash, except in CSV mode where backslash is a normal
1448 * character.
1449 */
1450 if (c == '\\' && !is_csv)
1451 {
1452 char c2;
1453
1456
1457 /* -----
1458 * get next character
1459 * Note: we do not change c so if it isn't \., we can fall
1460 * through and continue processing.
1461 * -----
1462 */
1463 c2 = copy_input_buf[input_buf_ptr];
1464
1465 if (c2 == '.')
1466 {
1467 input_buf_ptr++; /* consume the '.' */
1468 if (cstate->eol_type == EOL_CRNL)
1469 {
1470 /* Get the next character */
1472 /* if hit_eof, c2 will become '\0' */
1473 c2 = copy_input_buf[input_buf_ptr++];
1474
1475 if (c2 == '\n')
1476 ereport(ERROR,
1477 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1478 errmsg("end-of-copy marker does not match previous newline style")));
1479 else if (c2 != '\r')
1480 ereport(ERROR,
1481 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1482 errmsg("end-of-copy marker is not alone on its line")));
1483 }
1484
1485 /* Get the next character */
1487 /* if hit_eof, c2 will become '\0' */
1488 c2 = copy_input_buf[input_buf_ptr++];
1489
1490 if (c2 != '\r' && c2 != '\n')
1491 ereport(ERROR,
1492 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1493 errmsg("end-of-copy marker is not alone on its line")));
1494
1495 if ((cstate->eol_type == EOL_NL && c2 != '\n') ||
1496 (cstate->eol_type == EOL_CRNL && c2 != '\n') ||
1497 (cstate->eol_type == EOL_CR && c2 != '\r'))
1498 ereport(ERROR,
1499 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1500 errmsg("end-of-copy marker does not match previous newline style")));
1501
1502 /*
1503 * If there is any data on this line before the \., complain.
1504 */
1505 if (cstate->line_buf.len > 0 ||
1506 prev_raw_ptr > cstate->input_buf_index)
1507 ereport(ERROR,
1508 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1509 errmsg("end-of-copy marker is not alone on its line")));
1510
1511 /*
1512 * Discard the \. and newline, then report EOF.
1513 */
1514 cstate->input_buf_index = input_buf_ptr;
1515 result = true; /* report EOF */
1516 break;
1517 }
1518 else
1519 {
1520 /*
1521 * If we are here, it means we found a backslash followed by
1522 * something other than a period. In non-CSV mode, anything
1523 * after a backslash is special, so we skip over that second
1524 * character too. If we didn't do that \\. would be
1525 * considered an eof-of copy, while in non-CSV mode it is a
1526 * literal backslash followed by a period.
1527 */
1528 input_buf_ptr++;
1529 }
1530 }
1531 } /* end of outer loop */
1532
1533 /*
1534 * Transfer any still-uncopied data to line_buf.
1535 */
1537
1538 return result;
1539}
#define REFILL_LINEBUF
static void CopyLoadInputBuf(CopyFromState cstate)
#define IF_NEED_REFILL_AND_EOF_BREAK(extralen)
#define IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(extralen)
Definition: copyfromparse.c:97
int errhint(const char *fmt,...)
Definition: elog.c:1321

References CopyLoadInputBuf(), CopyFromStateData::cur_lineno, EOL_CR, EOL_CRNL, EOL_NL, CopyFromStateData::eol_type, EOL_UNKNOWN, ereport, errcode(), errhint(), errmsg(), ERROR, CopyFormatOptions::escape, IF_NEED_REFILL_AND_EOF_BREAK, IF_NEED_REFILL_AND_NOT_EOF_CONTINUE, CopyFromStateData::input_buf, INPUT_BUF_BYTES, CopyFromStateData::input_buf_index, CopyFromStateData::input_buf_len, CopyFromStateData::input_reached_eof, StringInfoData::len, CopyFromStateData::line_buf, CopyFromStateData::opts, CopyFormatOptions::quote, and REFILL_LINEBUF.

Referenced by CopyReadLine().

◆ GetDecimalFromHex()

static int GetDecimalFromHex ( char  hex)
static

Definition at line 1545 of file copyfromparse.c.

1546{
1547 if (isdigit((unsigned char) hex))
1548 return hex - '0';
1549 else
1550 return pg_ascii_tolower((unsigned char) hex) - 'a' + 10;
1551}
unsigned char pg_ascii_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:146

References pg_ascii_tolower().

Referenced by CopyReadAttributesText().

◆ NextCopyFrom()

bool NextCopyFrom ( CopyFromState  cstate,
ExprContext econtext,
Datum values,
bool *  nulls 
)

Definition at line 880 of file copyfromparse.c.

882{
883 TupleDesc tupDesc;
884 AttrNumber num_phys_attrs,
885 num_defaults = cstate->num_defaults;
886 int i;
887 int *defmap = cstate->defmap;
888 ExprState **defexprs = cstate->defexprs;
889
890 tupDesc = RelationGetDescr(cstate->rel);
891 num_phys_attrs = tupDesc->natts;
892
893 /* Initialize all values for row to NULL */
894 MemSet(values, 0, num_phys_attrs * sizeof(Datum));
895 MemSet(nulls, true, num_phys_attrs * sizeof(bool));
896 MemSet(cstate->defaults, false, num_phys_attrs * sizeof(bool));
897
898 /* Get one row from source */
899 if (!cstate->routine->CopyFromOneRow(cstate, econtext, values, nulls))
900 return false;
901
902 /*
903 * Now compute and insert any defaults available for the columns not
904 * provided by the input data. Anything not processed here or above will
905 * remain NULL.
906 */
907 for (i = 0; i < num_defaults; i++)
908 {
909 /*
910 * The caller must supply econtext and have switched into the
911 * per-tuple memory context in it.
912 */
913 Assert(econtext != NULL);
915
916 values[defmap[i]] = ExecEvalExpr(defexprs[defmap[i]], econtext,
917 &nulls[defmap[i]]);
918 }
919
920 return true;
921}
#define MemSet(start, val, len)
Definition: c.h:1020
int i
Definition: isn.c:77
bool(* CopyFromOneRow)(CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls)
Definition: copyapi.h:96
const struct CopyFromRoutine * routine

References Assert(), CopyFromRoutine::CopyFromOneRow, CurrentMemoryContext, CopyFromStateData::defaults, CopyFromStateData::defexprs, CopyFromStateData::defmap, ExprContext::ecxt_per_tuple_memory, ExecEvalExpr(), i, MemSet, TupleDescData::natts, CopyFromStateData::num_defaults, CopyFromStateData::rel, RelationGetDescr, CopyFromStateData::routine, and values.

Referenced by CopyFrom(), file_acquire_sample_rows(), and fileIterateForeignScan().

◆ NextCopyFromRawFields()

bool NextCopyFromRawFields ( CopyFromState  cstate,
char ***  fields,
int *  nfields 
)

Definition at line 747 of file copyfromparse.c.

748{
749 return NextCopyFromRawFieldsInternal(cstate, fields, nfields,
750 cstate->opts.csv_mode);
751}
bool csv_mode
Definition: copy.h:63

References CopyFormatOptions::csv_mode, NextCopyFromRawFieldsInternal(), and CopyFromStateData::opts.

◆ NextCopyFromRawFieldsInternal()

static pg_attribute_always_inline bool NextCopyFromRawFieldsInternal ( CopyFromState  cstate,
char ***  fields,
int *  nfields,
bool  is_csv 
)
static

Definition at line 771 of file copyfromparse.c.

772{
773 int fldct;
774 bool done = false;
775
776 /* only available for text or csv input */
777 Assert(!cstate->opts.binary);
778
779 /* on input check that the header line is correct if needed */
780 if (cstate->cur_lineno == 0 && cstate->opts.header_line != COPY_HEADER_FALSE)
781 {
782 ListCell *cur;
783 TupleDesc tupDesc;
784 int lines_to_skip = cstate->opts.header_line;
785
786 /* If set to "match", one header line is skipped */
787 if (cstate->opts.header_line == COPY_HEADER_MATCH)
788 lines_to_skip = 1;
789
790 tupDesc = RelationGetDescr(cstate->rel);
791
792 for (int i = 0; i < lines_to_skip; i++)
793 {
794 cstate->cur_lineno++;
795 if ((done = CopyReadLine(cstate, is_csv)))
796 break;
797 }
798
799 if (cstate->opts.header_line == COPY_HEADER_MATCH)
800 {
801 int fldnum;
802
803 if (is_csv)
804 fldct = CopyReadAttributesCSV(cstate);
805 else
806 fldct = CopyReadAttributesText(cstate);
807
808 if (fldct != list_length(cstate->attnumlist))
810 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
811 errmsg("wrong number of fields in header line: got %d, expected %d",
812 fldct, list_length(cstate->attnumlist))));
813
814 fldnum = 0;
815 foreach(cur, cstate->attnumlist)
816 {
817 int attnum = lfirst_int(cur);
818 char *colName;
819 Form_pg_attribute attr = TupleDescAttr(tupDesc, attnum - 1);
820
821 Assert(fldnum < cstate->max_fields);
822
823 colName = cstate->raw_fields[fldnum++];
824 if (colName == NULL)
826 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
827 errmsg("column name mismatch in header line field %d: got null value (\"%s\"), expected \"%s\"",
828 fldnum, cstate->opts.null_print, NameStr(attr->attname))));
829
830 if (namestrcmp(&attr->attname, colName) != 0)
831 {
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))));
836 }
837 }
838 }
839
840 if (done)
841 return false;
842 }
843
844 cstate->cur_lineno++;
845
846 /* Actually read the line into memory here */
847 done = CopyReadLine(cstate, is_csv);
848
849 /*
850 * EOF at start of line means we're done. If we see EOF after some
851 * characters, we act as though it was newline followed by EOF, ie,
852 * process the line and then exit loop on next iteration.
853 */
854 if (done && cstate->line_buf.len == 0)
855 return false;
856
857 /* Parse the line into de-escaped field values */
858 if (is_csv)
859 fldct = CopyReadAttributesCSV(cstate);
860 else
861 fldct = CopyReadAttributesText(cstate);
862
863 *fields = cstate->raw_fields;
864 *nfields = fldct;
865 return true;
866}
static int CopyReadAttributesCSV(CopyFromState cstate)
static int CopyReadAttributesText(CopyFromState cstate)
static bool CopyReadLine(CopyFromState cstate, bool is_csv)
#define COPY_HEADER_MATCH
Definition: copy.h:26
#define COPY_HEADER_FALSE
Definition: copy.h:27
int namestrcmp(Name name, const char *str)
Definition: name.c:247
int header_line
Definition: copy.h:64
bool binary
Definition: copy.h:61

References Assert(), attnum, CopyFromStateData::attnumlist, CopyFormatOptions::binary, COPY_HEADER_FALSE, COPY_HEADER_MATCH, CopyReadAttributesCSV(), CopyReadAttributesText(), CopyReadLine(), cur, CopyFromStateData::cur_lineno, ereport, errcode(), errmsg(), ERROR, CopyFormatOptions::header_line, i, StringInfoData::len, lfirst_int, CopyFromStateData::line_buf, list_length(), NameStr, namestrcmp(), CopyFormatOptions::null_print, CopyFromStateData::opts, CopyFromStateData::raw_fields, CopyFromStateData::rel, RelationGetDescr, and TupleDescAttr().

Referenced by CopyFromTextLikeOneRow(), and NextCopyFromRawFields().

◆ ReceiveCopyBegin()

void ReceiveCopyBegin ( CopyFromState  cstate)

Definition at line 170 of file copyfromparse.c.

171{
173 int natts = list_length(cstate->attnumlist);
174 int16 format = (cstate->opts.binary ? 1 : 0);
175 int i;
176
178 pq_sendbyte(&buf, format); /* overall format */
179 pq_sendint16(&buf, natts);
180 for (i = 0; i < natts; i++)
181 pq_sendint16(&buf, format); /* per-column formats */
183 cstate->copy_src = COPY_FRONTEND;
184 cstate->fe_msgbuf = makeStringInfo();
185 /* We *must* flush here to ensure FE knows it can send. */
186 pq_flush();
187}
#define pq_flush()
Definition: libpq.h:46
static char format
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:296
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:88
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:160
static void pq_sendint16(StringInfo buf, uint16 i)
Definition: pqformat.h:136
#define PqMsg_CopyInResponse
Definition: protocol.h:45
StringInfo makeStringInfo(void)
Definition: stringinfo.c:72

References CopyFromStateData::attnumlist, CopyFormatOptions::binary, buf, COPY_FRONTEND, CopyFromStateData::copy_src, CopyFromStateData::fe_msgbuf, format, i, list_length(), makeStringInfo(), CopyFromStateData::opts, pq_beginmessage(), pq_endmessage(), pq_flush, pq_sendbyte(), pq_sendint16(), and PqMsg_CopyInResponse.

Referenced by BeginCopyFrom().

◆ ReceiveCopyBinaryHeader()

void ReceiveCopyBinaryHeader ( CopyFromState  cstate)

Definition at line 190 of file copyfromparse.c.

191{
192 char readSig[11];
193 int32 tmp;
194
195 /* Signature */
196 if (CopyReadBinaryData(cstate, readSig, 11) != 11 ||
197 memcmp(readSig, BinarySignature, 11) != 0)
199 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
200 errmsg("COPY file signature not recognized")));
201 /* Flags field */
202 if (!CopyGetInt32(cstate, &tmp))
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)")));
210 tmp &= ~(1 << 16);
211 if ((tmp >> 16) != 0)
213 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
214 errmsg("unrecognized critical flags in COPY file header")));
215 /* Header extension length */
216 if (!CopyGetInt32(cstate, &tmp) ||
217 tmp < 0)
219 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
220 errmsg("invalid COPY file header (missing length)")));
221 /* Skip extension header, if present */
222 while (tmp-- > 0)
223 {
224 if (CopyReadBinaryData(cstate, readSig, 1) != 1)
226 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
227 errmsg("invalid COPY file header (wrong length)")));
228 }
229}
static const char BinarySignature[11]

References BinarySignature, CopyGetInt32(), CopyReadBinaryData(), ereport, errcode(), errmsg(), and ERROR.

Referenced by CopyFromBinaryStart().

Variable Documentation

◆ BinarySignature

const char BinarySignature[11] = "PGCOPY\n\377\r\n\0"
static

Definition at line 139 of file copyfromparse.c.

Referenced by ReceiveCopyBinaryHeader().