PostgreSQL Source Code  git master
copy.h File Reference
#include "nodes/execnodes.h"
#include "nodes/parsenodes.h"
#include "parser/parse_node.h"
#include "tcop/dest.h"
Include dependency graph for copy.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct CopyStateDataCopyState
 
typedef int(* copy_data_source_cb) (void *outbuf, int minread, int maxread)
 

Functions

void DoCopy (ParseState *state, const CopyStmt *stmt, int stmt_location, int stmt_len, uint64 *processed)
 
void ProcessCopyOptions (ParseState *pstate, CopyState cstate, bool is_from, List *options)
 
CopyState BeginCopyFrom (ParseState *pstate, Relation rel, const char *filename, bool is_program, copy_data_source_cb data_source_cb, List *attnamelist, List *options)
 
void EndCopyFrom (CopyState cstate)
 
bool NextCopyFrom (CopyState cstate, ExprContext *econtext, Datum *values, bool *nulls, Oid *tupleOid)
 
bool NextCopyFromRawFields (CopyState cstate, char ***fields, int *nfields)
 
void CopyFromErrorCallback (void *arg)
 
uint64 CopyFrom (CopyState cstate)
 
DestReceiverCreateCopyDestReceiver (void)
 

Typedef Documentation

◆ copy_data_source_cb

typedef int(* copy_data_source_cb) (void *outbuf, int minread, int maxread)

Definition at line 24 of file copy.h.

◆ CopyState

Definition at line 23 of file copy.h.

Function Documentation

◆ BeginCopyFrom()

CopyState BeginCopyFrom ( ParseState pstate,
Relation  rel,
const char *  filename,
bool  is_program,
copy_data_source_cb  data_source_cb,
List attnamelist,
List options 
)

Definition at line 2986 of file copy.c.

References AllocateFile(), Assert, attnum, CopyStateData::attnumlist, CopyStateData::attribute_buf, BeginCopy(), CopyStateData::binary, BinarySignature, build_column_default(), contain_volatile_functions_not_nextval(), COPY_CALLBACK, CopyStateData::copy_dest, CopyStateData::copy_file, CopyStateData::copycontext, CopyGetData(), CopyGetInt32(), CopyStateData::cur_attname, CopyStateData::cur_attval, CopyStateData::cur_lineno, CopyStateData::cur_relname, CopyStateData::data_source_cb, CopyStateData::defexprs, CopyStateData::defmap, DestRemote, CopyStateData::eol_type, EOL_UNKNOWN, ereport, errcode(), errcode_for_file_access(), errhint(), errmsg(), ERROR, ExecInitExpr(), expression_planner(), CopyStateData::fe_eof, CopyStateData::file_has_oids, CopyStateData::filename, fmgr_info(), getTypeBinaryInputInfo(), getTypeInputInfo(), CopyStateData::in_functions, initStringInfo(), InvalidOid, CopyStateData::is_program, CopyStateData::line_buf, CopyStateData::line_buf_converted, list_length(), list_member_int(), CopyStateData::max_fields, MemoryContextSwitchTo(), tupleDesc::natts, CopyStateData::num_defaults, CopyStateData::oid_in_function, CopyStateData::oid_typioparam, CopyStateData::oids, OpenPipeStream(), ParseState::p_rtable, palloc(), PG_BINARY_R, pstrdup(), CopyStateData::range_table, CopyStateData::raw_buf, CopyStateData::raw_buf_index, CopyStateData::raw_buf_len, RAW_BUF_SIZE, CopyStateData::raw_fields, ReceiveCopyBegin(), CopyStateData::rel, RelationGetDescr, RelationGetRelationName, S_ISDIR, stat, TupleDescAttr, CopyStateData::typioparams, CopyStateData::volatile_defexprs, and whereToSendOutput.

Referenced by copy_table(), DoCopy(), file_acquire_sample_rows(), fileBeginForeignScan(), and fileReScanForeignScan().

2993 {
2994  CopyState cstate;
2995  bool pipe = (filename == NULL);
2996  TupleDesc tupDesc;
2997  AttrNumber num_phys_attrs,
2998  num_defaults;
2999  FmgrInfo *in_functions;
3000  Oid *typioparams;
3001  int attnum;
3002  Oid in_func_oid;
3003  int *defmap;
3004  ExprState **defexprs;
3005  MemoryContext oldcontext;
3006  bool volatile_defexprs;
3007 
3008  cstate = BeginCopy(pstate, true, rel, NULL, InvalidOid, attnamelist, options);
3009  oldcontext = MemoryContextSwitchTo(cstate->copycontext);
3010 
3011  /* Initialize state variables */
3012  cstate->fe_eof = false;
3013  cstate->eol_type = EOL_UNKNOWN;
3014  cstate->cur_relname = RelationGetRelationName(cstate->rel);
3015  cstate->cur_lineno = 0;
3016  cstate->cur_attname = NULL;
3017  cstate->cur_attval = NULL;
3018 
3019  /* Set up variables to avoid per-attribute overhead. */
3020  initStringInfo(&cstate->attribute_buf);
3021  initStringInfo(&cstate->line_buf);
3022  cstate->line_buf_converted = false;
3023  cstate->raw_buf = (char *) palloc(RAW_BUF_SIZE + 1);
3024  cstate->raw_buf_index = cstate->raw_buf_len = 0;
3025 
3026  /* Assign range table, we'll need it in CopyFrom. */
3027  if (pstate)
3028  cstate->range_table = pstate->p_rtable;
3029 
3030  tupDesc = RelationGetDescr(cstate->rel);
3031  num_phys_attrs = tupDesc->natts;
3032  num_defaults = 0;
3033  volatile_defexprs = false;
3034 
3035  /*
3036  * Pick up the required catalog information for each attribute in the
3037  * relation, including the input function, the element type (to pass to
3038  * the input function), and info about defaults and constraints. (Which
3039  * input function we use depends on text/binary format choice.)
3040  */
3041  in_functions = (FmgrInfo *) palloc(num_phys_attrs * sizeof(FmgrInfo));
3042  typioparams = (Oid *) palloc(num_phys_attrs * sizeof(Oid));
3043  defmap = (int *) palloc(num_phys_attrs * sizeof(int));
3044  defexprs = (ExprState **) palloc(num_phys_attrs * sizeof(ExprState *));
3045 
3046  for (attnum = 1; attnum <= num_phys_attrs; attnum++)
3047  {
3048  Form_pg_attribute att = TupleDescAttr(tupDesc, attnum - 1);
3049 
3050  /* We don't need info for dropped attributes */
3051  if (att->attisdropped)
3052  continue;
3053 
3054  /* Fetch the input function and typioparam info */
3055  if (cstate->binary)
3056  getTypeBinaryInputInfo(att->atttypid,
3057  &in_func_oid, &typioparams[attnum - 1]);
3058  else
3059  getTypeInputInfo(att->atttypid,
3060  &in_func_oid, &typioparams[attnum - 1]);
3061  fmgr_info(in_func_oid, &in_functions[attnum - 1]);
3062 
3063  /* Get default info if needed */
3064  if (!list_member_int(cstate->attnumlist, attnum))
3065  {
3066  /* attribute is NOT to be copied from input */
3067  /* use default value if one exists */
3068  Expr *defexpr = (Expr *) build_column_default(cstate->rel,
3069  attnum);
3070 
3071  if (defexpr != NULL)
3072  {
3073  /* Run the expression through planner */
3074  defexpr = expression_planner(defexpr);
3075 
3076  /* Initialize executable expression in copycontext */
3077  defexprs[num_defaults] = ExecInitExpr(defexpr, NULL);
3078  defmap[num_defaults] = attnum - 1;
3079  num_defaults++;
3080 
3081  /*
3082  * If a default expression looks at the table being loaded,
3083  * then it could give the wrong answer when using
3084  * multi-insert. Since database access can be dynamic this is
3085  * hard to test for exactly, so we use the much wider test of
3086  * whether the default expression is volatile. We allow for
3087  * the special case of when the default expression is the
3088  * nextval() of a sequence which in this specific case is
3089  * known to be safe for use with the multi-insert
3090  * optimization. Hence we use this special case function
3091  * checker rather than the standard check for
3092  * contain_volatile_functions().
3093  */
3094  if (!volatile_defexprs)
3095  volatile_defexprs = contain_volatile_functions_not_nextval((Node *) defexpr);
3096  }
3097  }
3098  }
3099 
3100  /* We keep those variables in cstate. */
3101  cstate->in_functions = in_functions;
3102  cstate->typioparams = typioparams;
3103  cstate->defmap = defmap;
3104  cstate->defexprs = defexprs;
3105  cstate->volatile_defexprs = volatile_defexprs;
3106  cstate->num_defaults = num_defaults;
3107  cstate->is_program = is_program;
3108 
3109  if (data_source_cb)
3110  {
3111  cstate->copy_dest = COPY_CALLBACK;
3112  cstate->data_source_cb = data_source_cb;
3113  }
3114  else if (pipe)
3115  {
3116  Assert(!is_program); /* the grammar does not allow this */
3118  ReceiveCopyBegin(cstate);
3119  else
3120  cstate->copy_file = stdin;
3121  }
3122  else
3123  {
3124  cstate->filename = pstrdup(filename);
3125 
3126  if (cstate->is_program)
3127  {
3128  cstate->copy_file = OpenPipeStream(cstate->filename, PG_BINARY_R);
3129  if (cstate->copy_file == NULL)
3130  ereport(ERROR,
3132  errmsg("could not execute command \"%s\": %m",
3133  cstate->filename)));
3134  }
3135  else
3136  {
3137  struct stat st;
3138 
3139  cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_R);
3140  if (cstate->copy_file == NULL)
3141  {
3142  /* copy errno because ereport subfunctions might change it */
3143  int save_errno = errno;
3144 
3145  ereport(ERROR,
3147  errmsg("could not open file \"%s\" for reading: %m",
3148  cstate->filename),
3149  (save_errno == ENOENT || save_errno == EACCES) ?
3150  errhint("COPY FROM instructs the PostgreSQL server process to read a file. "
3151  "You may want a client-side facility such as psql's \\copy.") : 0));
3152  }
3153 
3154  if (fstat(fileno(cstate->copy_file), &st))
3155  ereport(ERROR,
3157  errmsg("could not stat file \"%s\": %m",
3158  cstate->filename)));
3159 
3160  if (S_ISDIR(st.st_mode))
3161  ereport(ERROR,
3162  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3163  errmsg("\"%s\" is a directory", cstate->filename)));
3164  }
3165  }
3166 
3167  if (!cstate->binary)
3168  {
3169  /* must rely on user to tell us... */
3170  cstate->file_has_oids = cstate->oids;
3171  }
3172  else
3173  {
3174  /* Read and verify binary header */
3175  char readSig[11];
3176  int32 tmp;
3177 
3178  /* Signature */
3179  if (CopyGetData(cstate, readSig, 11, 11) != 11 ||
3180  memcmp(readSig, BinarySignature, 11) != 0)
3181  ereport(ERROR,
3182  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3183  errmsg("COPY file signature not recognized")));
3184  /* Flags field */
3185  if (!CopyGetInt32(cstate, &tmp))
3186  ereport(ERROR,
3187  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3188  errmsg("invalid COPY file header (missing flags)")));
3189  cstate->file_has_oids = (tmp & (1 << 16)) != 0;
3190  tmp &= ~(1 << 16);
3191  if ((tmp >> 16) != 0)
3192  ereport(ERROR,
3193  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3194  errmsg("unrecognized critical flags in COPY file header")));
3195  /* Header extension length */
3196  if (!CopyGetInt32(cstate, &tmp) ||
3197  tmp < 0)
3198  ereport(ERROR,
3199  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3200  errmsg("invalid COPY file header (missing length)")));
3201  /* Skip extension header, if present */
3202  while (tmp-- > 0)
3203  {
3204  if (CopyGetData(cstate, readSig, 1, 1) != 1)
3205  ereport(ERROR,
3206  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3207  errmsg("invalid COPY file header (wrong length)")));
3208  }
3209  }
3210 
3211  if (cstate->file_has_oids && cstate->binary)
3212  {
3213  getTypeBinaryInputInfo(OIDOID,
3214  &in_func_oid, &cstate->oid_typioparam);
3215  fmgr_info(in_func_oid, &cstate->oid_in_function);
3216  }
3217 
3218  /* create workspace for CopyReadAttributes results */
3219  if (!cstate->binary)
3220  {
3221  AttrNumber attr_count = list_length(cstate->attnumlist);
3222  int nfields = cstate->file_has_oids ? (attr_count + 1) : attr_count;
3223 
3224  cstate->max_fields = nfields;
3225  cstate->raw_fields = (char **) palloc(nfields * sizeof(char *));
3226  }
3227 
3228  MemoryContextSwitchTo(oldcontext);
3229 
3230  return cstate;
3231 }
Definition: fmgr.h:56
List * range_table
Definition: copy.c:169
static CopyState BeginCopy(ParseState *pstate, bool is_from, Relation rel, RawStmt *raw_query, Oid queryRelId, List *attnamelist, List *options)
Definition: copy.c:1389
bool contain_volatile_functions_not_nextval(Node *clause)
Definition: clauses.c:1008
static bool CopyGetInt32(CopyState cstate, int32 *val)
Definition: copy.c:683
int errhint(const char *fmt,...)
Definition: elog.c:987
char ** raw_fields
Definition: copy.c:189
bool binary
Definition: copy.c:118
#define RelationGetDescr(relation)
Definition: rel.h:433
AttrNumber num_defaults
Definition: copy.c:160
FmgrInfo * in_functions
Definition: copy.c:164
List * attnumlist
Definition: copy.c:114
char * filename
Definition: copy.c:115
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:93
bool file_has_oids
Definition: copy.c:161
char * pstrdup(const char *in)
Definition: mcxt.c:1161
static void ReceiveCopyBegin(CopyState cstate)
Definition: copy.c:381
Expr * expression_planner(Expr *expr)
Definition: planner.c:5888
StringInfoData line_buf
Definition: copy.c:198
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: nodes.h:517
int raw_buf_index
Definition: copy.c:211
int errcode(int sqlerrcode)
Definition: elog.c:575
bool fe_eof
Definition: copy.c:105
unsigned int Oid
Definition: postgres_ext.h:31
bool volatile_defexprs
Definition: copy.c:168
#define PG_BINARY_R
Definition: c.h:1082
int natts
Definition: tupdesc.h:82
bool line_buf_converted
Definition: copy.c:199
signed int int32
Definition: c.h:313
CopyDest copy_dest
Definition: copy.c:101
const char * cur_attname
Definition: copy.c:143
Relation rel
Definition: copy.c:112
MemoryContext copycontext
Definition: copy.c:149
copy_data_source_cb data_source_cb
Definition: copy.c:117
#define ERROR
Definition: elog.h:43
static int CopyGetData(CopyState cstate, void *databuf, int minread, int maxread)
Definition: copy.c:557
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:124
bool list_member_int(const List *list, int datum)
Definition: list.c:485
char * raw_buf
Definition: copy.c:210
ExprState ** defexprs
Definition: copy.c:167
const char * cur_relname
Definition: copy.c:141
int errcode_for_file_access(void)
Definition: elog.c:598
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2336
FmgrInfo oid_in_function
Definition: copy.c:162
#define RelationGetRelationName(relation)
Definition: rel.h:441
static const char BinarySignature[11]
Definition: copy.c:289
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:197
int raw_buf_len
Definition: copy.c:212
int max_fields
Definition: copy.c:188
FILE * OpenPipeStream(const char *command, const char *mode)
Definition: fd.c:2435
void getTypeBinaryInputInfo(Oid type, Oid *typReceive, Oid *typIOParam)
Definition: lsyscache.c:2683
#define ereport(elevel, rest)
Definition: elog.h:122
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2617
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define stat(a, b)
Definition: win32_port.h:266
Oid * typioparams
Definition: copy.c:165
bool is_program
Definition: copy.c:116
Node * build_column_default(Relation rel, int attrno)
#define RAW_BUF_SIZE
Definition: copy.c:209
#define InvalidOid
Definition: postgres_ext.h:36
int16 attnum
Definition: pg_attribute.h:79
EolType eol_type
Definition: copy.c:106
#define Assert(condition)
Definition: c.h:699
static int list_length(const List *l)
Definition: pg_list.h:89
#define S_ISDIR(m)
Definition: win32_port.h:307
static char * filename
Definition: pg_dumpall.c:87
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797
FILE * copy_file
Definition: copy.c:102
int cur_lineno
Definition: copy.c:142
StringInfoData attribute_buf
Definition: copy.c:184
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:119
const char * cur_attval
Definition: copy.c:144
CommandDest whereToSendOutput
Definition: postgres.c:90
Oid oid_typioparam
Definition: copy.c:163
int16 AttrNumber
Definition: attnum.h:21
int * defmap
Definition: copy.c:166
List * p_rtable
Definition: parse_node.h:174
bool oids
Definition: copy.c:119

◆ CopyFrom()

uint64 CopyFrom ( CopyState  cstate)

Definition at line 2297 of file copy.c.

References AfterTriggerBeginQuery(), AfterTriggerEndQuery(), ErrorContextCallback::arg, Assert, FdwRoutine::BeginForeignInsert, ErrorContextCallback::callback, CHECK_FOR_INTERRUPTS, CheckValidResultRel(), CMD_INSERT, tupleDesc::constr, ConvertPartitionTupleSlot(), CopyStateData::copy_dest, COPY_OLD_FE, CopyFromErrorCallback(), CopyFromInsertBatch(), CreateExecutorState(), CopyStateData::cur_lineno, CurrentMemoryContext, FdwRoutine::EndForeignInsert, ereport, errcode(), errhint(), errmsg(), ERROR, error_context_stack, EState::es_num_result_relations, EState::es_range_table, EState::es_result_relation_info, EState::es_result_relations, EState::es_trig_tuple_slot, EState::es_tupleTable, ExecARInsertTriggers(), ExecASInsertTriggers(), ExecBRInsertTriggers(), ExecBSInsertTriggers(), ExecCleanUpTriggerState(), ExecCleanupTupleRouting(), ExecCloseIndices(), ExecConstraints(), ExecFindPartition(), FdwRoutine::ExecForeignInsert, ExecInitExtraTupleSlot(), ExecInitPartitionInfo(), ExecInsertIndexTuples(), ExecIRInsertTriggers(), ExecMaterializeSlot(), ExecOpenIndices(), ExecResetTupleTable(), ExecSetupChildParentMapForLeaf(), ExecSetupPartitionTupleRouting(), ExecStoreTuple(), FreeBulkInsertState(), FreeExecutorState(), CopyStateData::freeze, GetBulkInsertState(), GetCurrentCommandId(), GetCurrentSubTransactionId(), GetPerTupleExprContext, GetPerTupleMemoryContext, heap_form_tuple(), heap_insert(), HEAP_INSERT_FROZEN, HEAP_INSERT_SKIP_FSM, HEAP_INSERT_SKIP_WAL, heap_sync(), HeapTupleSetOid, InitResultRelInfo(), InvalidateCatalogSnapshot(), InvalidBuffer, InvalidOid, InvalidSubTransactionId, list_free(), makeNode, MakeTransitionCaptureState(), MAX_BUFFERED_TUPLES, MemoryContextSwitchTo(), tupleDesc::natts, NextCopyFrom(), NIL, ModifyTableState::operation, palloc(), PartitionTupleRouting::parent_child_tupconv_maps, PartitionTupleRouting::partition_dispatch_info, CopyStateData::partition_tuple_routing, PartitionTupleRouting::partition_tuple_slot, PartitionTupleRouting::partitions, pfree(), PlanState::plan, pq_endmsgread(), ErrorContextCallback::previous, ModifyTableState::ps, CopyStateData::range_table, RelationData::rd_att, RelationData::rd_createSubid, RelationData::rd_newRelfilenodeSubid, RelationData::rd_rel, CopyStateData::rel, RelationGetDescr, RelationGetRelationName, RelationGetRelid, ReleaseBulkInsertStatePin(), ResetPerTupleExprContext, ModifyTableState::resultRelInfo, ResultRelInfo::ri_FdwRoutine, ResultRelInfo::ri_NumIndices, ResultRelInfo::ri_PartitionCheck, ResultRelInfo::ri_RelationDesc, ResultRelInfo::ri_TrigDesc, PlanState::state, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, TransitionCaptureState::tcs_map, TransitionCaptureState::tcs_original_insert_tuple, ThereAreNoPriorRegisteredSnapshots(), ThereAreNoReadyPortals(), CopyStateData::transition_capture, TriggerDesc::trig_insert_before_row, TriggerDesc::trig_insert_instead_row, RelationData::trigdesc, TupConvMapForLeaf(), values, CopyStateData::volatile_defexprs, and XLogIsNeeded.

Referenced by copy_table(), and DoCopy().

2298 {
2299  HeapTuple tuple;
2300  TupleDesc tupDesc;
2301  Datum *values;
2302  bool *nulls;
2303  ResultRelInfo *resultRelInfo;
2304  ResultRelInfo *saved_resultRelInfo = NULL;
2305  EState *estate = CreateExecutorState(); /* for ExecConstraints() */
2306  ModifyTableState *mtstate;
2307  ExprContext *econtext;
2308  TupleTableSlot *myslot;
2309  MemoryContext oldcontext = CurrentMemoryContext;
2310 
2311  ErrorContextCallback errcallback;
2312  CommandId mycid = GetCurrentCommandId(true);
2313  int hi_options = 0; /* start with default heap_insert options */
2314  BulkInsertState bistate;
2315  uint64 processed = 0;
2316  bool useHeapMultiInsert;
2317  int nBufferedTuples = 0;
2318  int prev_leaf_part_index = -1;
2319 
2320 #define MAX_BUFFERED_TUPLES 1000
2321  HeapTuple *bufferedTuples = NULL; /* initialize to silence warning */
2322  Size bufferedTuplesSize = 0;
2323  int firstBufferedLineNo = 0;
2324 
2325  Assert(cstate->rel);
2326 
2327  /*
2328  * The target must be a plain, foreign, or partitioned relation, or have
2329  * an INSTEAD OF INSERT row trigger. (Currently, such triggers are only
2330  * allowed on views, so we only hint about them in the view case.)
2331  */
2332  if (cstate->rel->rd_rel->relkind != RELKIND_RELATION &&
2333  cstate->rel->rd_rel->relkind != RELKIND_FOREIGN_TABLE &&
2334  cstate->rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE &&
2335  !(cstate->rel->trigdesc &&
2337  {
2338  if (cstate->rel->rd_rel->relkind == RELKIND_VIEW)
2339  ereport(ERROR,
2340  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2341  errmsg("cannot copy to view \"%s\"",
2342  RelationGetRelationName(cstate->rel)),
2343  errhint("To enable copying to a view, provide an INSTEAD OF INSERT trigger.")));
2344  else if (cstate->rel->rd_rel->relkind == RELKIND_MATVIEW)
2345  ereport(ERROR,
2346  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2347  errmsg("cannot copy to materialized view \"%s\"",
2348  RelationGetRelationName(cstate->rel))));
2349  else if (cstate->rel->rd_rel->relkind == RELKIND_SEQUENCE)
2350  ereport(ERROR,
2351  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2352  errmsg("cannot copy to sequence \"%s\"",
2353  RelationGetRelationName(cstate->rel))));
2354  else
2355  ereport(ERROR,
2356  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2357  errmsg("cannot copy to non-table relation \"%s\"",
2358  RelationGetRelationName(cstate->rel))));
2359  }
2360 
2361  tupDesc = RelationGetDescr(cstate->rel);
2362 
2363  /*----------
2364  * Check to see if we can avoid writing WAL
2365  *
2366  * If archive logging/streaming is not enabled *and* either
2367  * - table was created in same transaction as this COPY
2368  * - data is being written to relfilenode created in this transaction
2369  * then we can skip writing WAL. It's safe because if the transaction
2370  * doesn't commit, we'll discard the table (or the new relfilenode file).
2371  * If it does commit, we'll have done the heap_sync at the bottom of this
2372  * routine first.
2373  *
2374  * As mentioned in comments in utils/rel.h, the in-same-transaction test
2375  * is not always set correctly, since in rare cases rd_newRelfilenodeSubid
2376  * can be cleared before the end of the transaction. The exact case is
2377  * when a relation sets a new relfilenode twice in same transaction, yet
2378  * the second one fails in an aborted subtransaction, e.g.
2379  *
2380  * BEGIN;
2381  * TRUNCATE t;
2382  * SAVEPOINT save;
2383  * TRUNCATE t;
2384  * ROLLBACK TO save;
2385  * COPY ...
2386  *
2387  * Also, if the target file is new-in-transaction, we assume that checking
2388  * FSM for free space is a waste of time, even if we must use WAL because
2389  * of archiving. This could possibly be wrong, but it's unlikely.
2390  *
2391  * The comments for heap_insert and RelationGetBufferForTuple specify that
2392  * skipping WAL logging is only safe if we ensure that our tuples do not
2393  * go into pages containing tuples from any other transactions --- but this
2394  * must be the case if we have a new table or new relfilenode, so we need
2395  * no additional work to enforce that.
2396  *----------
2397  */
2398  /* createSubid is creation check, newRelfilenodeSubid is truncation check */
2399  if (cstate->rel->rd_createSubid != InvalidSubTransactionId ||
2401  {
2402  hi_options |= HEAP_INSERT_SKIP_FSM;
2403  if (!XLogIsNeeded())
2404  hi_options |= HEAP_INSERT_SKIP_WAL;
2405  }
2406 
2407  /*
2408  * Optimize if new relfilenode was created in this subxact or one of its
2409  * committed children and we won't see those rows later as part of an
2410  * earlier scan or command. The subxact test ensures that if this subxact
2411  * aborts then the frozen rows won't be visible after xact cleanup. Note
2412  * that the stronger test of exactly which subtransaction created it is
2413  * crucial for correctness of this optimization. The test for an earlier
2414  * scan or command tolerates false negatives. FREEZE causes other sessions
2415  * to see rows they would not see under MVCC, and a false negative merely
2416  * spreads that anomaly to the current session.
2417  */
2418  if (cstate->freeze)
2419  {
2420  /*
2421  * Tolerate one registration for the benefit of FirstXactSnapshot.
2422  * Scan-bearing queries generally create at least two registrations,
2423  * though relying on that is fragile, as is ignoring ActiveSnapshot.
2424  * Clear CatalogSnapshot to avoid counting its registration. We'll
2425  * still detect ongoing catalog scans, each of which separately
2426  * registers the snapshot it uses.
2427  */
2430  ereport(ERROR,
2431  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
2432  errmsg("cannot perform FREEZE because of prior transaction activity")));
2433 
2434  if (cstate->rel->rd_createSubid != GetCurrentSubTransactionId() &&
2436  ereport(ERROR,
2437  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2438  errmsg("cannot perform FREEZE because the table was not created or truncated in the current subtransaction")));
2439 
2440  hi_options |= HEAP_INSERT_FROZEN;
2441  }
2442 
2443  /*
2444  * We need a ResultRelInfo so we can use the regular executor's
2445  * index-entry-making machinery. (There used to be a huge amount of code
2446  * here that basically duplicated execUtils.c ...)
2447  */
2448  resultRelInfo = makeNode(ResultRelInfo);
2449  InitResultRelInfo(resultRelInfo,
2450  cstate->rel,
2451  1, /* dummy rangetable index */
2452  NULL,
2453  0);
2454 
2455  /* Verify the named relation is a valid target for INSERT */
2456  CheckValidResultRel(resultRelInfo, CMD_INSERT);
2457 
2458  ExecOpenIndices(resultRelInfo, false);
2459 
2460  estate->es_result_relations = resultRelInfo;
2461  estate->es_num_result_relations = 1;
2462  estate->es_result_relation_info = resultRelInfo;
2463  estate->es_range_table = cstate->range_table;
2464 
2465  /* Set up a tuple slot too */
2466  myslot = ExecInitExtraTupleSlot(estate, tupDesc);
2467  /* Triggers might need a slot as well */
2468  estate->es_trig_tuple_slot = ExecInitExtraTupleSlot(estate, NULL);
2469 
2470  /*
2471  * Set up a ModifyTableState so we can let FDW(s) init themselves for
2472  * foreign-table result relation(s).
2473  */
2474  mtstate = makeNode(ModifyTableState);
2475  mtstate->ps.plan = NULL;
2476  mtstate->ps.state = estate;
2477  mtstate->operation = CMD_INSERT;
2478  mtstate->resultRelInfo = estate->es_result_relations;
2479 
2480  if (resultRelInfo->ri_FdwRoutine != NULL &&
2481  resultRelInfo->ri_FdwRoutine->BeginForeignInsert != NULL)
2482  resultRelInfo->ri_FdwRoutine->BeginForeignInsert(mtstate,
2483  resultRelInfo);
2484 
2485  /* Prepare to catch AFTER triggers. */
2487 
2488  /*
2489  * If there are any triggers with transition tables on the named relation,
2490  * we need to be prepared to capture transition tuples.
2491  */
2492  cstate->transition_capture =
2494  RelationGetRelid(cstate->rel),
2495  CMD_INSERT);
2496 
2497  /*
2498  * If the named relation is a partitioned table, initialize state for
2499  * CopyFrom tuple routing.
2500  */
2501  if (cstate->rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
2502  {
2503  PartitionTupleRouting *proute;
2504 
2505  proute = cstate->partition_tuple_routing =
2506  ExecSetupPartitionTupleRouting(NULL, cstate->rel);
2507 
2508  /*
2509  * If we are capturing transition tuples, they may need to be
2510  * converted from partition format back to partitioned table format
2511  * (this is only ever necessary if a BEFORE trigger modifies the
2512  * tuple).
2513  */
2514  if (cstate->transition_capture != NULL)
2516  }
2517 
2518  /*
2519  * It's more efficient to prepare a bunch of tuples for insertion, and
2520  * insert them in one heap_multi_insert() call, than call heap_insert()
2521  * separately for every tuple. However, we can't do that if there are
2522  * BEFORE/INSTEAD OF triggers, or we need to evaluate volatile default
2523  * expressions. Such triggers or expressions might query the table we're
2524  * inserting to, and act differently if the tuples that have already been
2525  * processed and prepared for insertion are not there. We also can't do
2526  * it if the table is foreign or partitioned.
2527  */
2528  if ((resultRelInfo->ri_TrigDesc != NULL &&
2529  (resultRelInfo->ri_TrigDesc->trig_insert_before_row ||
2530  resultRelInfo->ri_TrigDesc->trig_insert_instead_row)) ||
2531  resultRelInfo->ri_FdwRoutine != NULL ||
2532  cstate->partition_tuple_routing != NULL ||
2533  cstate->volatile_defexprs)
2534  {
2535  useHeapMultiInsert = false;
2536  }
2537  else
2538  {
2539  useHeapMultiInsert = true;
2540  bufferedTuples = palloc(MAX_BUFFERED_TUPLES * sizeof(HeapTuple));
2541  }
2542 
2543  /*
2544  * Check BEFORE STATEMENT insertion triggers. It's debatable whether we
2545  * should do this for COPY, since it's not really an "INSERT" statement as
2546  * such. However, executing these triggers maintains consistency with the
2547  * EACH ROW triggers that we already fire on COPY.
2548  */
2549  ExecBSInsertTriggers(estate, resultRelInfo);
2550 
2551  values = (Datum *) palloc(tupDesc->natts * sizeof(Datum));
2552  nulls = (bool *) palloc(tupDesc->natts * sizeof(bool));
2553 
2554  bistate = GetBulkInsertState();
2555  econtext = GetPerTupleExprContext(estate);
2556 
2557  /* Set up callback to identify error line number */
2558  errcallback.callback = CopyFromErrorCallback;
2559  errcallback.arg = (void *) cstate;
2560  errcallback.previous = error_context_stack;
2561  error_context_stack = &errcallback;
2562 
2563  for (;;)
2564  {
2565  TupleTableSlot *slot;
2566  bool skip_tuple;
2567  Oid loaded_oid = InvalidOid;
2568 
2570 
2571  if (nBufferedTuples == 0)
2572  {
2573  /*
2574  * Reset the per-tuple exprcontext. We can only do this if the
2575  * tuple buffer is empty. (Calling the context the per-tuple
2576  * memory context is a bit of a misnomer now.)
2577  */
2578  ResetPerTupleExprContext(estate);
2579  }
2580 
2581  /* Switch into its memory context */
2583 
2584  if (!NextCopyFrom(cstate, econtext, values, nulls, &loaded_oid))
2585  break;
2586 
2587  /* And now we can form the input tuple. */
2588  tuple = heap_form_tuple(tupDesc, values, nulls);
2589 
2590  if (loaded_oid != InvalidOid)
2591  HeapTupleSetOid(tuple, loaded_oid);
2592 
2593  /*
2594  * Constraints might reference the tableoid column, so initialize
2595  * t_tableOid before evaluating them.
2596  */
2597  tuple->t_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
2598 
2599  /* Triggers and stuff need to be invoked in query context. */
2600  MemoryContextSwitchTo(oldcontext);
2601 
2602  /* Place tuple in tuple slot --- but slot shouldn't free it */
2603  slot = myslot;
2604  ExecStoreTuple(tuple, slot, InvalidBuffer, false);
2605 
2606  /* Determine the partition to heap_insert the tuple into */
2607  if (cstate->partition_tuple_routing)
2608  {
2609  int leaf_part_index;
2611 
2612  /*
2613  * Away we go ... If we end up not finding a partition after all,
2614  * ExecFindPartition() does not return and errors out instead.
2615  * Otherwise, the returned value is to be used as an index into
2616  * arrays mt_partitions[] and mt_partition_tupconv_maps[] that
2617  * will get us the ResultRelInfo and TupleConversionMap for the
2618  * partition, respectively.
2619  */
2620  leaf_part_index = ExecFindPartition(resultRelInfo,
2621  proute->partition_dispatch_info,
2622  slot,
2623  estate);
2624  Assert(leaf_part_index >= 0 &&
2625  leaf_part_index < proute->num_partitions);
2626 
2627  /*
2628  * If this tuple is mapped to a partition that is not same as the
2629  * previous one, we'd better make the bulk insert mechanism gets a
2630  * new buffer.
2631  */
2632  if (prev_leaf_part_index != leaf_part_index)
2633  {
2634  ReleaseBulkInsertStatePin(bistate);
2635  prev_leaf_part_index = leaf_part_index;
2636  }
2637 
2638  /*
2639  * Save the old ResultRelInfo and switch to the one corresponding
2640  * to the selected partition.
2641  */
2642  saved_resultRelInfo = resultRelInfo;
2643  resultRelInfo = proute->partitions[leaf_part_index];
2644  if (resultRelInfo == NULL)
2645  {
2646  resultRelInfo = ExecInitPartitionInfo(mtstate,
2647  saved_resultRelInfo,
2648  proute, estate,
2649  leaf_part_index);
2650  Assert(resultRelInfo != NULL);
2651  }
2652 
2653  /*
2654  * For ExecInsertIndexTuples() to work on the partition's indexes
2655  */
2656  estate->es_result_relation_info = resultRelInfo;
2657 
2658  /*
2659  * If we're capturing transition tuples, we might need to convert
2660  * from the partition rowtype to parent rowtype.
2661  */
2662  if (cstate->transition_capture != NULL)
2663  {
2664  if (resultRelInfo->ri_TrigDesc &&
2665  resultRelInfo->ri_TrigDesc->trig_insert_before_row)
2666  {
2667  /*
2668  * If there are any BEFORE triggers on the partition,
2669  * we'll have to be ready to convert their result back to
2670  * tuplestore format.
2671  */
2673  cstate->transition_capture->tcs_map =
2674  TupConvMapForLeaf(proute, saved_resultRelInfo,
2675  leaf_part_index);
2676  }
2677  else
2678  {
2679  /*
2680  * Otherwise, just remember the original unconverted
2681  * tuple, to avoid a needless round trip conversion.
2682  */
2684  cstate->transition_capture->tcs_map = NULL;
2685  }
2686  }
2687 
2688  /*
2689  * We might need to convert from the parent rowtype to the
2690  * partition rowtype.
2691  */
2692  tuple = ConvertPartitionTupleSlot(proute->parent_child_tupconv_maps[leaf_part_index],
2693  tuple,
2694  proute->partition_tuple_slot,
2695  &slot);
2696 
2697  tuple->t_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
2698  }
2699 
2700  skip_tuple = false;
2701 
2702  /* BEFORE ROW INSERT Triggers */
2703  if (resultRelInfo->ri_TrigDesc &&
2704  resultRelInfo->ri_TrigDesc->trig_insert_before_row)
2705  {
2706  slot = ExecBRInsertTriggers(estate, resultRelInfo, slot);
2707 
2708  if (slot == NULL) /* "do nothing" */
2709  skip_tuple = true;
2710  else /* trigger might have changed tuple */
2711  tuple = ExecMaterializeSlot(slot);
2712  }
2713 
2714  if (!skip_tuple)
2715  {
2716  if (resultRelInfo->ri_TrigDesc &&
2717  resultRelInfo->ri_TrigDesc->trig_insert_instead_row)
2718  {
2719  /* Pass the data to the INSTEAD ROW INSERT trigger */
2720  ExecIRInsertTriggers(estate, resultRelInfo, slot);
2721  }
2722  else
2723  {
2724  /*
2725  * We always check the partition constraint, including when
2726  * the tuple got here via tuple-routing. However we don't
2727  * need to in the latter case if no BR trigger is defined on
2728  * the partition. Note that a BR trigger might modify the
2729  * tuple such that the partition constraint is no longer
2730  * satisfied, so we need to check in that case.
2731  */
2732  bool check_partition_constr =
2733  (resultRelInfo->ri_PartitionCheck != NIL);
2734 
2735  if (saved_resultRelInfo != NULL &&
2736  !(resultRelInfo->ri_TrigDesc &&
2737  resultRelInfo->ri_TrigDesc->trig_insert_before_row))
2738  check_partition_constr = false;
2739 
2740  /*
2741  * If the target is a plain table, check the constraints of
2742  * the tuple.
2743  */
2744  if (resultRelInfo->ri_FdwRoutine == NULL &&
2745  (resultRelInfo->ri_RelationDesc->rd_att->constr ||
2746  check_partition_constr))
2747  ExecConstraints(resultRelInfo, slot, estate, true);
2748 
2749  if (useHeapMultiInsert)
2750  {
2751  /* Add this tuple to the tuple buffer */
2752  if (nBufferedTuples == 0)
2753  firstBufferedLineNo = cstate->cur_lineno;
2754  bufferedTuples[nBufferedTuples++] = tuple;
2755  bufferedTuplesSize += tuple->t_len;
2756 
2757  /*
2758  * If the buffer filled up, flush it. Also flush if the
2759  * total size of all the tuples in the buffer becomes
2760  * large, to avoid using large amounts of memory for the
2761  * buffer when the tuples are exceptionally wide.
2762  */
2763  if (nBufferedTuples == MAX_BUFFERED_TUPLES ||
2764  bufferedTuplesSize > 65535)
2765  {
2766  CopyFromInsertBatch(cstate, estate, mycid, hi_options,
2767  resultRelInfo, myslot, bistate,
2768  nBufferedTuples, bufferedTuples,
2769  firstBufferedLineNo);
2770  nBufferedTuples = 0;
2771  bufferedTuplesSize = 0;
2772  }
2773  }
2774  else
2775  {
2776  List *recheckIndexes = NIL;
2777 
2778  /* OK, store the tuple */
2779  if (resultRelInfo->ri_FdwRoutine != NULL)
2780  {
2781  slot = resultRelInfo->ri_FdwRoutine->ExecForeignInsert(estate,
2782  resultRelInfo,
2783  slot,
2784  NULL);
2785 
2786  if (slot == NULL) /* "do nothing" */
2787  goto next_tuple;
2788 
2789  /* FDW might have changed tuple */
2790  tuple = ExecMaterializeSlot(slot);
2791 
2792  /*
2793  * AFTER ROW Triggers might reference the tableoid
2794  * column, so initialize t_tableOid before evaluating
2795  * them.
2796  */
2797  tuple->t_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
2798  }
2799  else
2800  heap_insert(resultRelInfo->ri_RelationDesc, tuple,
2801  mycid, hi_options, bistate);
2802 
2803  /* And create index entries for it */
2804  if (resultRelInfo->ri_NumIndices > 0)
2805  recheckIndexes = ExecInsertIndexTuples(slot,
2806  &(tuple->t_self),
2807  estate,
2808  false,
2809  NULL,
2810  NIL);
2811 
2812  /* AFTER ROW INSERT Triggers */
2813  ExecARInsertTriggers(estate, resultRelInfo, tuple,
2814  recheckIndexes, cstate->transition_capture);
2815 
2816  list_free(recheckIndexes);
2817  }
2818  }
2819 
2820  /*
2821  * We count only tuples not suppressed by a BEFORE INSERT trigger
2822  * or FDW; this is the same definition used by nodeModifyTable.c
2823  * for counting tuples inserted by an INSERT command.
2824  */
2825  processed++;
2826  }
2827 
2828 next_tuple:
2829  /* Restore the saved ResultRelInfo */
2830  if (saved_resultRelInfo)
2831  {
2832  resultRelInfo = saved_resultRelInfo;
2833  estate->es_result_relation_info = resultRelInfo;
2834  }
2835  }
2836 
2837  /* Flush any remaining buffered tuples */
2838  if (nBufferedTuples > 0)
2839  CopyFromInsertBatch(cstate, estate, mycid, hi_options,
2840  resultRelInfo, myslot, bistate,
2841  nBufferedTuples, bufferedTuples,
2842  firstBufferedLineNo);
2843 
2844  /* Done, clean up */
2845  error_context_stack = errcallback.previous;
2846 
2847  FreeBulkInsertState(bistate);
2848 
2849  MemoryContextSwitchTo(oldcontext);
2850 
2851  /*
2852  * In the old protocol, tell pqcomm that we can process normal protocol
2853  * messages again.
2854  */
2855  if (cstate->copy_dest == COPY_OLD_FE)
2856  pq_endmsgread();
2857 
2858  /* Execute AFTER STATEMENT insertion triggers */
2859  ExecASInsertTriggers(estate, resultRelInfo, cstate->transition_capture);
2860 
2861  /* Handle queued AFTER triggers */
2862  AfterTriggerEndQuery(estate);
2863 
2864  pfree(values);
2865  pfree(nulls);
2866 
2867  ExecResetTupleTable(estate->es_tupleTable, false);
2868 
2869  /* Allow the FDW to shut down */
2870  if (resultRelInfo->ri_FdwRoutine != NULL &&
2871  resultRelInfo->ri_FdwRoutine->EndForeignInsert != NULL)
2872  resultRelInfo->ri_FdwRoutine->EndForeignInsert(estate,
2873  resultRelInfo);
2874 
2875  ExecCloseIndices(resultRelInfo);
2876 
2877  /* Close all the partitioned tables, leaf partitions, and their indices */
2878  if (cstate->partition_tuple_routing)
2880 
2881  /* Close any trigger target relations */
2882  ExecCleanUpTriggerState(estate);
2883 
2884  FreeExecutorState(estate);
2885 
2886  /*
2887  * If we skipped writing WAL, then we need to sync the heap (but not
2888  * indexes since those use WAL anyway)
2889  */
2890  if (hi_options & HEAP_INSERT_SKIP_WAL)
2891  heap_sync(cstate->rel);
2892 
2893  return processed;
2894 }
int ri_NumIndices
Definition: execnodes.h:400
#define NIL
Definition: pg_list.h:69
uint32 CommandId
Definition: c.h:488
int ExecFindPartition(ResultRelInfo *resultRelInfo, PartitionDispatch *pd, TupleTableSlot *slot, EState *estate)
TupleTableSlot * ExecStoreTuple(HeapTuple tuple, TupleTableSlot *slot, Buffer buffer, bool shouldFree)
Definition: execTuples.c:356
void InitResultRelInfo(ResultRelInfo *resultRelInfo, Relation resultRelationDesc, Index resultRelationIndex, Relation partition_root, int instrument_options)
Definition: execMain.c:1305
Relation ri_RelationDesc
Definition: execnodes.h:397
List * range_table
Definition: copy.c:169
int errhint(const char *fmt,...)
Definition: elog.c:987
void CopyFromErrorCallback(void *arg)
Definition: copy.c:2192
TupleConversionMap * TupConvMapForLeaf(PartitionTupleRouting *proute, ResultRelInfo *rootRelInfo, int leaf_index)
List * ExecInsertIndexTuples(TupleTableSlot *slot, ItemPointer tupleid, EState *estate, bool noDupErr, bool *specConflict, List *arbiterIndexes)
Definition: execIndexing.c:271
#define ResetPerTupleExprContext(estate)
Definition: executor.h:498
#define RelationGetDescr(relation)
Definition: rel.h:433
#define HEAP_INSERT_FROZEN
Definition: heapam.h:30
BeginForeignInsert_function BeginForeignInsert
Definition: fdwapi.h:214
ResultRelInfo * resultRelInfo
Definition: execnodes.h:1043
ExecForeignInsert_function ExecForeignInsert
Definition: fdwapi.h:210
#define XLogIsNeeded()
Definition: xlog.h:146
#define MAX_BUFFERED_TUPLES
TupleTableSlot * ExecIRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2596
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define InvalidBuffer
Definition: buf.h:25
bool ThereAreNoPriorRegisteredSnapshots(void)
Definition: snapmgr.c:1655
int errcode(int sqlerrcode)
Definition: elog.c:575
CmdType operation
Definition: execnodes.h:1037
void ExecSetupChildParentMapForLeaf(PartitionTupleRouting *proute)
SubTransactionId rd_newRelfilenodeSubid
Definition: rel.h:81
void ExecARInsertTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple, List *recheckIndexes, TransitionCaptureState *transition_capture)
Definition: trigger.c:2581
void heap_sync(Relation rel)
Definition: heapam.c:9370
#define HEAP_INSERT_SKIP_WAL
Definition: heapam.h:28
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1074
EState * state
Definition: execnodes.h:914
TupleTableSlot * partition_tuple_slot
List * es_range_table
Definition: execnodes.h:480
Form_pg_class rd_rel
Definition: rel.h:84
unsigned int Oid
Definition: postgres_ext.h:31
void ExecConstraints(ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate, bool check_partition_constraint)
Definition: execMain.c:1962
bool volatile_defexprs
Definition: copy.c:168
void(* callback)(void *arg)
Definition: elog.h:239
struct ErrorContextCallback * previous
Definition: elog.h:238
int natts
Definition: tupdesc.h:82
HeapTuple tcs_original_insert_tuple
Definition: trigger.h:82
ResultRelInfo ** partitions
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc)
Definition: execTuples.c:931
void ExecOpenIndices(ResultRelInfo *resultRelInfo, bool speculative)
Definition: execIndexing.c:149
PartitionTupleRouting * ExecSetupPartitionTupleRouting(ModifyTableState *mtstate, Relation rel)
Definition: execPartition.c:76
CopyDest copy_dest
Definition: copy.c:101
#define HeapTupleSetOid(tuple, oid)
Definition: htup_details.h:710
ErrorContextCallback * error_context_stack
Definition: elog.c:88
bool trig_insert_instead_row
Definition: reltrigger.h:57
void FreeExecutorState(EState *estate)
Definition: execUtils.c:188
Relation rel
Definition: copy.c:112
#define GetPerTupleExprContext(estate)
Definition: executor.h:489
BulkInsertState GetBulkInsertState(void)
Definition: heapam.c:2364
void pfree(void *pointer)
Definition: mcxt.c:1031
bool ThereAreNoReadyPortals(void)
Definition: portalmem.c:1198
#define ERROR
Definition: elog.h:43
PlanState ps
Definition: execnodes.h:1036
void ExecCleanupTupleRouting(ModifyTableState *mtstate, PartitionTupleRouting *proute)
static void CopyFromInsertBatch(CopyState cstate, EState *estate, CommandId mycid, int hi_options, ResultRelInfo *resultRelInfo, TupleTableSlot *myslot, BulkInsertState bistate, int nBufferedTuples, HeapTuple *bufferedTuples, int firstBufferedLineNo)
Definition: copy.c:2902
TupleConversionMap * tcs_map
Definition: trigger.h:73
ItemPointerData t_self
Definition: htup.h:65
TriggerDesc * trigdesc
Definition: rel.h:90
void CheckValidResultRel(ResultRelInfo *resultRelInfo, CmdType operation)
Definition: execMain.c:1104
uint32 t_len
Definition: htup.h:64
void ExecBSInsertTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2447
ResultRelInfo * es_result_relations
Definition: execnodes.h:490
#define RelationGetRelationName(relation)
Definition: rel.h:441
struct FdwRoutine * ri_FdwRoutine
Definition: execnodes.h:421
Oid t_tableOid
Definition: htup.h:66
PartitionDispatch * partition_dispatch_info
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
TupleTableSlot * es_trig_tuple_slot
Definition: execnodes.h:512
#define ereport(elevel, rest)
Definition: elog.h:122
Oid heap_insert(Relation relation, HeapTuple tup, CommandId cid, int options, BulkInsertState bistate)
Definition: heapam.c:2441
void InvalidateCatalogSnapshot(void)
Definition: snapmgr.c:510
TriggerDesc * ri_TrigDesc
Definition: execnodes.h:409
EState * CreateExecutorState(void)
Definition: execUtils.c:80
SubTransactionId rd_createSubid
Definition: rel.h:80
bool trig_insert_before_row
Definition: reltrigger.h:55
List * es_tupleTable
Definition: execnodes.h:525
void ExecResetTupleTable(List *tupleTable, bool shouldFree)
Definition: execTuples.c:186
void ExecASInsertTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2504
uintptr_t Datum
Definition: postgres.h:367
HeapTuple ConvertPartitionTupleSlot(TupleConversionMap *map, HeapTuple tuple, TupleTableSlot *new_slot, TupleTableSlot **p_my_slot)
TransitionCaptureState * MakeTransitionCaptureState(TriggerDesc *trigdesc, Oid relid, CmdType cmdType)
Definition: trigger.c:4644
int es_num_result_relations
Definition: execnodes.h:491
List * ri_PartitionCheck
Definition: execnodes.h:454
TupleDesc rd_att
Definition: rel.h:85
bool freeze
Definition: copy.c:120
Plan * plan
Definition: execnodes.h:912
void pq_endmsgread(void)
Definition: pqcomm.c:1234
#define InvalidOid
Definition: postgres_ext.h:36
PartitionTupleRouting * partition_tuple_routing
Definition: copy.c:172
void AfterTriggerBeginQuery(void)
Definition: trigger.c:4766
#define makeNode(_type_)
Definition: nodes.h:565
#define Assert(condition)
Definition: c.h:699
void FreeBulkInsertState(BulkInsertState bistate)
Definition: heapam.c:2378
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:641
TupleConstr * constr
Definition: tupdesc.h:87
size_t Size
Definition: c.h:433
#define InvalidSubTransactionId
Definition: c.h:480
HeapTuple ExecMaterializeSlot(TupleTableSlot *slot)
Definition: execTuples.c:781
void ReleaseBulkInsertStatePin(BulkInsertState bistate)
Definition: heapam.c:2390
#define GetPerTupleMemoryContext(estate)
Definition: executor.h:494
TupleConversionMap ** parent_child_tupconv_maps
void ExecCleanUpTriggerState(EState *estate)
Definition: execMain.c:1474
#define HEAP_INSERT_SKIP_FSM
Definition: heapam.h:29
static Datum values[MAXATTR]
Definition: bootstrap.c:164
void AfterTriggerEndQuery(EState *estate)
Definition: trigger.c:4786
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797
void list_free(List *list)
Definition: list.c:1133
bool NextCopyFrom(CopyState cstate, ExprContext *econtext, Datum *values, bool *nulls, Oid *tupleOid)
Definition: copy.c:3297
int cur_lineno
Definition: copy.c:142
TransitionCaptureState * transition_capture
Definition: copy.c:174
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
TupleTableSlot * ExecBRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2515
CommandId GetCurrentCommandId(bool used)
Definition: xact.c:679
ResultRelInfo * ExecInitPartitionInfo(ModifyTableState *mtstate, ResultRelInfo *resultRelInfo, PartitionTupleRouting *proute, EState *estate, int partidx)
Definition: pg_list.h:45
#define RelationGetRelid(relation)
Definition: rel.h:407
void ExecCloseIndices(ResultRelInfo *resultRelInfo)
Definition: execIndexing.c:224
EndForeignInsert_function EndForeignInsert
Definition: fdwapi.h:215
ResultRelInfo * es_result_relation_info
Definition: execnodes.h:492

◆ CopyFromErrorCallback()

void CopyFromErrorCallback ( void *  arg)

Definition at line 2192 of file copy.c.

References CopyStateData::binary, CopyStateData::cur_attname, CopyStateData::cur_attval, CopyStateData::cur_lineno, CopyStateData::cur_relname, StringInfoData::data, errcontext, limit_printout_length(), CopyStateData::line_buf, CopyStateData::line_buf_converted, CopyStateData::line_buf_valid, CopyStateData::need_transcoding, and pfree().

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

2193 {
2194  CopyState cstate = (CopyState) arg;
2195 
2196  if (cstate->binary)
2197  {
2198  /* can't usefully display the data */
2199  if (cstate->cur_attname)
2200  errcontext("COPY %s, line %d, column %s",
2201  cstate->cur_relname, cstate->cur_lineno,
2202  cstate->cur_attname);
2203  else
2204  errcontext("COPY %s, line %d",
2205  cstate->cur_relname, cstate->cur_lineno);
2206  }
2207  else
2208  {
2209  if (cstate->cur_attname && cstate->cur_attval)
2210  {
2211  /* error is relevant to a particular column */
2212  char *attval;
2213 
2214  attval = limit_printout_length(cstate->cur_attval);
2215  errcontext("COPY %s, line %d, column %s: \"%s\"",
2216  cstate->cur_relname, cstate->cur_lineno,
2217  cstate->cur_attname, attval);
2218  pfree(attval);
2219  }
2220  else if (cstate->cur_attname)
2221  {
2222  /* error is relevant to a particular column, value is NULL */
2223  errcontext("COPY %s, line %d, column %s: null input",
2224  cstate->cur_relname, cstate->cur_lineno,
2225  cstate->cur_attname);
2226  }
2227  else
2228  {
2229  /*
2230  * Error is relevant to a particular line.
2231  *
2232  * If line_buf still contains the correct line, and it's already
2233  * transcoded, print it. If it's still in a foreign encoding, it's
2234  * quite likely that the error is precisely a failure to do
2235  * encoding conversion (ie, bad data). We dare not try to convert
2236  * it, and at present there's no way to regurgitate it without
2237  * conversion. So we have to punt and just report the line number.
2238  */
2239  if (cstate->line_buf_valid &&
2240  (cstate->line_buf_converted || !cstate->need_transcoding))
2241  {
2242  char *lineval;
2243 
2244  lineval = limit_printout_length(cstate->line_buf.data);
2245  errcontext("COPY %s, line %d: \"%s\"",
2246  cstate->cur_relname, cstate->cur_lineno, lineval);
2247  pfree(lineval);
2248  }
2249  else
2250  {
2251  errcontext("COPY %s, line %d",
2252  cstate->cur_relname, cstate->cur_lineno);
2253  }
2254  }
2255  }
2256 }
bool binary
Definition: copy.c:118
bool need_transcoding
Definition: copy.c:108
StringInfoData line_buf
Definition: copy.c:198
bool line_buf_valid
Definition: copy.c:200
bool line_buf_converted
Definition: copy.c:199
const char * cur_attname
Definition: copy.c:143
void pfree(void *pointer)
Definition: mcxt.c:1031
static char * limit_printout_length(const char *str)
Definition: copy.c:2268
const char * cur_relname
Definition: copy.c:141
#define errcontext
Definition: elog.h:164
struct CopyStateData * CopyState
Definition: copy.h:23
void * arg
int cur_lineno
Definition: copy.c:142
const char * cur_attval
Definition: copy.c:144

◆ CreateCopyDestReceiver()

DestReceiver* CreateCopyDestReceiver ( void  )

Definition at line 4849 of file copy.c.

References copy_dest_destroy(), copy_dest_receive(), copy_dest_shutdown(), copy_dest_startup(), DestCopyOut, and palloc().

Referenced by CreateDestReceiver().

4850 {
4851  DR_copy *self = (DR_copy *) palloc(sizeof(DR_copy));
4852 
4853  self->pub.receiveSlot = copy_dest_receive;
4854  self->pub.rStartup = copy_dest_startup;
4855  self->pub.rShutdown = copy_dest_shutdown;
4856  self->pub.rDestroy = copy_dest_destroy;
4857  self->pub.mydest = DestCopyOut;
4858 
4859  self->cstate = NULL; /* will be set later */
4860  self->processed = 0;
4861 
4862  return (DestReceiver *) self;
4863 }
Definition: copy.c:216
static void copy_dest_destroy(DestReceiver *self)
Definition: copy.c:4840
static void copy_dest_shutdown(DestReceiver *self)
Definition: copy.c:4831
static void copy_dest_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: copy.c:4803
static bool copy_dest_receive(TupleTableSlot *slot, DestReceiver *self)
Definition: copy.c:4812
void * palloc(Size size)
Definition: mcxt.c:924

◆ DoCopy()

void DoCopy ( ParseState state,
const CopyStmt stmt,
int  stmt_location,
int  stmt_len,
uint64 *  processed 
)

Definition at line 782 of file copy.c.

References AccessShareLock, ACL_INSERT, ACL_SELECT, addRangeTableEntryForRelation(), Assert, CopyStmt::attlist, BeginCopyFrom(), BeginCopyTo(), bms_add_member(), check_enable_rls(), CopyFrom(), CopyGetAttnums(), cur, DoCopyTo(), EndCopyFrom(), EndCopyTo(), ereport, errcode(), errhint(), errmsg(), ERROR, ExecCheckRTPerms(), ColumnRef::fields, CopyStmt::filename, FirstLowInvalidHeapAttributeNumber, SelectStmt::fromClause, get_namespace_name(), GetUserId(), heap_close, heap_openrv(), ResTarget::indirection, RangeTblEntry::insertedCols, InvalidOid, CopyStmt::is_from, is_member_of_role(), CopyStmt::is_program, lappend(), lfirst, lfirst_int, list_make1, ColumnRef::location, ResTarget::location, makeNode, makeRangeVar(), ResTarget::name, NIL, NoLock, CopyStmt::options, ParseState::p_rtable, PreventCommandIfParallelMode(), PreventCommandIfReadOnly(), pstrdup(), CopyStmt::query, RelationData::rd_islocaltemp, CopyStateData::rel, CopyStmt::relation, RelationGetDescr, RelationGetNamespace, RelationGetRelationName, RelationGetRelid, RangeTblEntry::relid, RangeTblEntry::requiredPerms, RLS_ENABLED, RowExclusiveLock, select, RangeTblEntry::selectedCols, RawStmt::stmt, RawStmt::stmt_len, RawStmt::stmt_location, SelectStmt::targetList, ResTarget::val, and XactReadOnly.

Referenced by standard_ProcessUtility().

785 {
786  CopyState cstate;
787  bool is_from = stmt->is_from;
788  bool pipe = (stmt->filename == NULL);
789  Relation rel;
790  Oid relid;
791  RawStmt *query = NULL;
792 
793  /*
794  * Disallow COPY to/from file or program except to users with the
795  * appropriate role.
796  */
797  if (!pipe)
798  {
799  if (stmt->is_program)
800  {
801  if (!is_member_of_role(GetUserId(), DEFAULT_ROLE_EXECUTE_SERVER_PROGRAM))
802  ereport(ERROR,
803  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
804  errmsg("must be superuser or a member of the pg_execute_server_program role to COPY to or from an external program"),
805  errhint("Anyone can COPY to stdout or from stdin. "
806  "psql's \\copy command also works for anyone.")));
807  }
808  else
809  {
810  if (is_from && !is_member_of_role(GetUserId(), DEFAULT_ROLE_READ_SERVER_FILES))
811  ereport(ERROR,
812  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
813  errmsg("must be superuser or a member of the pg_read_server_files role to COPY from a file"),
814  errhint("Anyone can COPY to stdout or from stdin. "
815  "psql's \\copy command also works for anyone.")));
816 
817  if (!is_from && !is_member_of_role(GetUserId(), DEFAULT_ROLE_WRITE_SERVER_FILES))
818  ereport(ERROR,
819  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
820  errmsg("must be superuser or a member of the pg_write_server_files role to COPY to a file"),
821  errhint("Anyone can COPY to stdout or from stdin. "
822  "psql's \\copy command also works for anyone.")));
823  }
824  }
825 
826  if (stmt->relation)
827  {
828  TupleDesc tupDesc;
829  List *attnums;
830  ListCell *cur;
831  RangeTblEntry *rte;
832 
833  Assert(!stmt->query);
834 
835  /* Open and lock the relation, using the appropriate lock type. */
836  rel = heap_openrv(stmt->relation,
837  (is_from ? RowExclusiveLock : AccessShareLock));
838 
839  relid = RelationGetRelid(rel);
840 
841  rte = addRangeTableEntryForRelation(pstate, rel, NULL, false, false);
842  rte->requiredPerms = (is_from ? ACL_INSERT : ACL_SELECT);
843 
844  tupDesc = RelationGetDescr(rel);
845  attnums = CopyGetAttnums(tupDesc, rel, stmt->attlist);
846  foreach(cur, attnums)
847  {
848  int attno = lfirst_int(cur) -
850 
851  if (is_from)
852  rte->insertedCols = bms_add_member(rte->insertedCols, attno);
853  else
854  rte->selectedCols = bms_add_member(rte->selectedCols, attno);
855  }
856  ExecCheckRTPerms(pstate->p_rtable, true);
857 
858  /*
859  * Permission check for row security policies.
860  *
861  * check_enable_rls will ereport(ERROR) if the user has requested
862  * something invalid and will otherwise indicate if we should enable
863  * RLS (returns RLS_ENABLED) or not for this COPY statement.
864  *
865  * If the relation has a row security policy and we are to apply it
866  * then perform a "query" copy and allow the normal query processing
867  * to handle the policies.
868  *
869  * If RLS is not enabled for this, then just fall through to the
870  * normal non-filtering relation handling.
871  */
872  if (check_enable_rls(rte->relid, InvalidOid, false) == RLS_ENABLED)
873  {
875  ColumnRef *cr;
876  ResTarget *target;
877  RangeVar *from;
878  List *targetList = NIL;
879 
880  if (is_from)
881  ereport(ERROR,
882  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
883  errmsg("COPY FROM not supported with row-level security"),
884  errhint("Use INSERT statements instead.")));
885 
886  /*
887  * Build target list
888  *
889  * If no columns are specified in the attribute list of the COPY
890  * command, then the target list is 'all' columns. Therefore, '*'
891  * should be used as the target list for the resulting SELECT
892  * statement.
893  *
894  * In the case that columns are specified in the attribute list,
895  * create a ColumnRef and ResTarget for each column and add them
896  * to the target list for the resulting SELECT statement.
897  */
898  if (!stmt->attlist)
899  {
900  cr = makeNode(ColumnRef);
902  cr->location = -1;
903 
904  target = makeNode(ResTarget);
905  target->name = NULL;
906  target->indirection = NIL;
907  target->val = (Node *) cr;
908  target->location = -1;
909 
910  targetList = list_make1(target);
911  }
912  else
913  {
914  ListCell *lc;
915 
916  foreach(lc, stmt->attlist)
917  {
918  /*
919  * Build the ColumnRef for each column. The ColumnRef
920  * 'fields' property is a String 'Value' node (see
921  * nodes/value.h) that corresponds to the column name
922  * respectively.
923  */
924  cr = makeNode(ColumnRef);
925  cr->fields = list_make1(lfirst(lc));
926  cr->location = -1;
927 
928  /* Build the ResTarget and add the ColumnRef to it. */
929  target = makeNode(ResTarget);
930  target->name = NULL;
931  target->indirection = NIL;
932  target->val = (Node *) cr;
933  target->location = -1;
934 
935  /* Add each column to the SELECT statement's target list */
936  targetList = lappend(targetList, target);
937  }
938  }
939 
940  /*
941  * Build RangeVar for from clause, fully qualified based on the
942  * relation which we have opened and locked.
943  */
946  -1);
947 
948  /* Build query */
949  select = makeNode(SelectStmt);
950  select->targetList = targetList;
951  select->fromClause = list_make1(from);
952 
953  query = makeNode(RawStmt);
954  query->stmt = (Node *) select;
955  query->stmt_location = stmt_location;
956  query->stmt_len = stmt_len;
957 
958  /*
959  * Close the relation for now, but keep the lock on it to prevent
960  * changes between now and when we start the query-based COPY.
961  *
962  * We'll reopen it later as part of the query-based COPY.
963  */
964  heap_close(rel, NoLock);
965  rel = NULL;
966  }
967  }
968  else
969  {
970  Assert(stmt->query);
971 
972  query = makeNode(RawStmt);
973  query->stmt = stmt->query;
974  query->stmt_location = stmt_location;
975  query->stmt_len = stmt_len;
976 
977  relid = InvalidOid;
978  rel = NULL;
979  }
980 
981  if (is_from)
982  {
983  Assert(rel);
984 
985  /* check read-only transaction and parallel mode */
986  if (XactReadOnly && !rel->rd_islocaltemp)
987  PreventCommandIfReadOnly("COPY FROM");
988  PreventCommandIfParallelMode("COPY FROM");
989 
990  cstate = BeginCopyFrom(pstate, rel, stmt->filename, stmt->is_program,
991  NULL, stmt->attlist, stmt->options);
992  *processed = CopyFrom(cstate); /* copy from file to database */
993  EndCopyFrom(cstate);
994  }
995  else
996  {
997  cstate = BeginCopyTo(pstate, rel, query, relid,
998  stmt->filename, stmt->is_program,
999  stmt->attlist, stmt->options);
1000  *processed = DoCopyTo(cstate); /* copy from database to file */
1001  EndCopyTo(cstate);
1002  }
1003 
1004  /*
1005  * Close the relation. If reading, we can release the AccessShareLock we
1006  * got; if writing, we should hold the lock until end of transaction to
1007  * ensure that updates will be committed before lock is released.
1008  */
1009  if (rel != NULL)
1010  heap_close(rel, (is_from ? NoLock : AccessShareLock));
1011 }
List * indirection
Definition: parsenodes.h:442
#define NIL
Definition: pg_list.h:69
Node * val
Definition: parsenodes.h:443
int errhint(const char *fmt,...)
Definition: elog.c:987
void PreventCommandIfParallelMode(const char *cmdname)
Definition: utility.c:257
List * attlist
Definition: parsenodes.h:1956
List * fromClause
Definition: parsenodes.h:1540
#define RelationGetDescr(relation)
Definition: rel.h:433
char * name
Definition: parsenodes.h:441
Oid GetUserId(void)
Definition: miscinit.c:379
CopyState BeginCopyFrom(ParseState *pstate, Relation rel, const char *filename, bool is_program, copy_data_source_cb data_source_cb, List *attnamelist, List *options)
Definition: copy.c:2986
char * pstrdup(const char *in)
Definition: mcxt.c:1161
bool rd_islocaltemp
Definition: rel.h:60
#define AccessShareLock
Definition: lockdefs.h:36
Definition: nodes.h:517
struct cursor * cur
Definition: ecpg.c:28
int errcode(int sqlerrcode)
Definition: elog.c:575
uint64 CopyFrom(CopyState cstate)
Definition: copy.c:2297
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
AclMode requiredPerms
Definition: parsenodes.h:1070
#define heap_close(r, l)
Definition: heapam.h:97
unsigned int Oid
Definition: postgres_ext.h:31
bool is_program
Definition: parsenodes.h:1959
int location
Definition: parsenodes.h:237
int location
Definition: parsenodes.h:444
#define list_make1(x1)
Definition: pg_list.h:139
Bitmapset * selectedCols
Definition: parsenodes.h:1072
static CopyState BeginCopyTo(ParseState *pstate, Relation rel, RawStmt *query, Oid queryRelId, const char *filename, bool is_program, List *attnamelist, List *options)
Definition: copy.c:1754
#define ERROR
Definition: elog.h:43
#define lfirst_int(lc)
Definition: pg_list.h:107
RangeVar * relation
Definition: parsenodes.h:1953
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3051
Node * stmt
Definition: parsenodes.h:1450
#define NoLock
Definition: lockdefs.h:34
List * targetList
Definition: parsenodes.h:1539
static uint64 DoCopyTo(CopyState cstate)
Definition: copy.c:1892
#define RowExclusiveLock
Definition: lockdefs.h:38
List * options
Definition: parsenodes.h:1961
#define select(n, r, w, e, timeout)
Definition: win32_port.h:447
#define RelationGetRelationName(relation)
Definition: rel.h:441
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
static List * CopyGetAttnums(TupleDesc tupDesc, Relation rel, List *attnamelist)
Definition: copy.c:4730
#define ACL_SELECT
Definition: parsenodes.h:75
int stmt_len
Definition: parsenodes.h:1452
int stmt_location
Definition: parsenodes.h:1451
Relation heap_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: heapam.c:1323
#define InvalidOid
Definition: postgres_ext.h:36
bool XactReadOnly
Definition: xact.c:76
bool is_member_of_role(Oid member, Oid role)
Definition: acl.c:4857
int check_enable_rls(Oid relid, Oid checkAsUser, bool noError)
Definition: rls.c:52
void EndCopyFrom(CopyState cstate)
Definition: copy.c:3538
#define makeNode(_type_)
Definition: nodes.h:565
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
#define ACL_INSERT
Definition: parsenodes.h:74
RangeTblEntry * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, Alias *alias, bool inh, bool inFromCl)
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:764
void PreventCommandIfReadOnly(const char *cmdname)
Definition: utility.c:239
Node * query
Definition: parsenodes.h:1954
bool is_from
Definition: parsenodes.h:1958
int errmsg(const char *fmt,...)
Definition: elog.c:797
Bitmapset * insertedCols
Definition: parsenodes.h:1073
bool ExecCheckRTPerms(List *rangeTable, bool ereport_on_violation)
Definition: execMain.c:574
char * filename
Definition: parsenodes.h:1960
static void EndCopyTo(CopyState cstate)
Definition: copy.c:1927
Definition: pg_list.h:45
#define RelationGetRelid(relation)
Definition: rel.h:407
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:421
List * fields
Definition: parsenodes.h:236
#define RelationGetNamespace(relation)
Definition: rel.h:448

◆ EndCopyFrom()

void EndCopyFrom ( CopyState  cstate)

Definition at line 3538 of file copy.c.

References EndCopy().

Referenced by DoCopy(), file_acquire_sample_rows(), fileEndForeignScan(), and fileReScanForeignScan().

3539 {
3540  /* No COPY FROM related resources except memory. */
3541 
3542  EndCopy(cstate);
3543 }
static void EndCopy(CopyState cstate)
Definition: copy.c:1731

◆ NextCopyFrom()

bool NextCopyFrom ( CopyState  cstate,
ExprContext econtext,
Datum values,
bool nulls,
Oid tupleOid 
)

Definition at line 3297 of file copy.c.

References Assert, attnum, CopyStateData::attnumlist, CopyStateData::binary, CopyStateData::convert_select_flags, CopyStateData::copy_dest, COPY_OLD_FE, CopyGetData(), CopyGetInt16(), CopyReadBinaryAttribute(), CStringGetDatum, CopyStateData::csv_mode, cur, CopyStateData::cur_attname, CopyStateData::cur_attval, CopyStateData::cur_lineno, CurrentMemoryContext, DatumGetObjectId, CopyStateData::defexprs, CopyStateData::defmap, DirectFunctionCall1, ExprContext::ecxt_per_tuple_memory, ereport, errcode(), errmsg(), ERROR, ExecEvalExpr(), CopyStateData::file_has_oids, CopyStateData::force_notnull_flags, CopyStateData::force_null_flags, i, CopyStateData::in_functions, InputFunctionCall(), InvalidOid, lfirst_int, list_length(), MemSet, NameStr, tupleDesc::natts, NextCopyFromRawFields(), CopyStateData::null_print, CopyStateData::num_defaults, CopyStateData::oid_in_function, CopyStateData::oid_typioparam, oidin(), CopyStateData::oids, CopyStateData::rel, RelationGetDescr, TupleDescAttr, and CopyStateData::typioparams.

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

3299 {
3300  TupleDesc tupDesc;
3301  AttrNumber num_phys_attrs,
3302  attr_count,
3303  num_defaults = cstate->num_defaults;
3304  FmgrInfo *in_functions = cstate->in_functions;
3305  Oid *typioparams = cstate->typioparams;
3306  int i;
3307  int nfields;
3308  bool isnull;
3309  bool file_has_oids = cstate->file_has_oids;
3310  int *defmap = cstate->defmap;
3311  ExprState **defexprs = cstate->defexprs;
3312 
3313  tupDesc = RelationGetDescr(cstate->rel);
3314  num_phys_attrs = tupDesc->natts;
3315  attr_count = list_length(cstate->attnumlist);
3316  nfields = file_has_oids ? (attr_count + 1) : attr_count;
3317 
3318  /* Initialize all values for row to NULL */
3319  MemSet(values, 0, num_phys_attrs * sizeof(Datum));
3320  MemSet(nulls, true, num_phys_attrs * sizeof(bool));
3321 
3322  if (!cstate->binary)
3323  {
3324  char **field_strings;
3325  ListCell *cur;
3326  int fldct;
3327  int fieldno;
3328  char *string;
3329 
3330  /* read raw fields in the next line */
3331  if (!NextCopyFromRawFields(cstate, &field_strings, &fldct))
3332  return false;
3333 
3334  /* check for overflowing fields */
3335  if (nfields > 0 && fldct > nfields)
3336  ereport(ERROR,
3337  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3338  errmsg("extra data after last expected column")));
3339 
3340  fieldno = 0;
3341 
3342  /* Read the OID field if present */
3343  if (file_has_oids)
3344  {
3345  if (fieldno >= fldct)
3346  ereport(ERROR,
3347  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3348  errmsg("missing data for OID column")));
3349  string = field_strings[fieldno++];
3350 
3351  if (string == NULL)
3352  ereport(ERROR,
3353  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3354  errmsg("null OID in COPY data")));
3355  else if (cstate->oids && tupleOid != NULL)
3356  {
3357  cstate->cur_attname = "oid";
3358  cstate->cur_attval = string;
3360  CStringGetDatum(string)));
3361  if (*tupleOid == InvalidOid)
3362  ereport(ERROR,
3363  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3364  errmsg("invalid OID in COPY data")));
3365  cstate->cur_attname = NULL;
3366  cstate->cur_attval = NULL;
3367  }
3368  }
3369 
3370  /* Loop to read the user attributes on the line. */
3371  foreach(cur, cstate->attnumlist)
3372  {
3373  int attnum = lfirst_int(cur);
3374  int m = attnum - 1;
3375  Form_pg_attribute att = TupleDescAttr(tupDesc, m);
3376 
3377  if (fieldno >= fldct)
3378  ereport(ERROR,
3379  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3380  errmsg("missing data for column \"%s\"",
3381  NameStr(att->attname))));
3382  string = field_strings[fieldno++];
3383 
3384  if (cstate->convert_select_flags &&
3385  !cstate->convert_select_flags[m])
3386  {
3387  /* ignore input field, leaving column as NULL */
3388  continue;
3389  }
3390 
3391  if (cstate->csv_mode)
3392  {
3393  if (string == NULL &&
3394  cstate->force_notnull_flags[m])
3395  {
3396  /*
3397  * FORCE_NOT_NULL option is set and column is NULL -
3398  * convert it to the NULL string.
3399  */
3400  string = cstate->null_print;
3401  }
3402  else if (string != NULL && cstate->force_null_flags[m]
3403  && strcmp(string, cstate->null_print) == 0)
3404  {
3405  /*
3406  * FORCE_NULL option is set and column matches the NULL
3407  * string. It must have been quoted, or otherwise the
3408  * string would already have been set to NULL. Convert it
3409  * to NULL as specified.
3410  */
3411  string = NULL;
3412  }
3413  }
3414 
3415  cstate->cur_attname = NameStr(att->attname);
3416  cstate->cur_attval = string;
3417  values[m] = InputFunctionCall(&in_functions[m],
3418  string,
3419  typioparams[m],
3420  att->atttypmod);
3421  if (string != NULL)
3422  nulls[m] = false;
3423  cstate->cur_attname = NULL;
3424  cstate->cur_attval = NULL;
3425  }
3426 
3427  Assert(fieldno == nfields);
3428  }
3429  else
3430  {
3431  /* binary */
3432  int16 fld_count;
3433  ListCell *cur;
3434 
3435  cstate->cur_lineno++;
3436 
3437  if (!CopyGetInt16(cstate, &fld_count))
3438  {
3439  /* EOF detected (end of file, or protocol-level EOF) */
3440  return false;
3441  }
3442 
3443  if (fld_count == -1)
3444  {
3445  /*
3446  * Received EOF marker. In a V3-protocol copy, wait for the
3447  * protocol-level EOF, and complain if it doesn't come
3448  * immediately. This ensures that we correctly handle CopyFail,
3449  * if client chooses to send that now.
3450  *
3451  * Note that we MUST NOT try to read more data in an old-protocol
3452  * copy, since there is no protocol-level EOF marker then. We
3453  * could go either way for copy from file, but choose to throw
3454  * error if there's data after the EOF marker, for consistency
3455  * with the new-protocol case.
3456  */
3457  char dummy;
3458 
3459  if (cstate->copy_dest != COPY_OLD_FE &&
3460  CopyGetData(cstate, &dummy, 1, 1) > 0)
3461  ereport(ERROR,
3462  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3463  errmsg("received copy data after EOF marker")));
3464  return false;
3465  }
3466 
3467  if (fld_count != attr_count)
3468  ereport(ERROR,
3469  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3470  errmsg("row field count is %d, expected %d",
3471  (int) fld_count, attr_count)));
3472 
3473  if (file_has_oids)
3474  {
3475  Oid loaded_oid;
3476 
3477  cstate->cur_attname = "oid";
3478  loaded_oid =
3480  0,
3481  &cstate->oid_in_function,
3482  cstate->oid_typioparam,
3483  -1,
3484  &isnull));
3485  if (isnull || loaded_oid == InvalidOid)
3486  ereport(ERROR,
3487  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3488  errmsg("invalid OID in COPY data")));
3489  cstate->cur_attname = NULL;
3490  if (cstate->oids && tupleOid != NULL)
3491  *tupleOid = loaded_oid;
3492  }
3493 
3494  i = 0;
3495  foreach(cur, cstate->attnumlist)
3496  {
3497  int attnum = lfirst_int(cur);
3498  int m = attnum - 1;
3499  Form_pg_attribute att = TupleDescAttr(tupDesc, m);
3500 
3501  cstate->cur_attname = NameStr(att->attname);
3502  i++;
3503  values[m] = CopyReadBinaryAttribute(cstate,
3504  i,
3505  &in_functions[m],
3506  typioparams[m],
3507  att->atttypmod,
3508  &nulls[m]);
3509  cstate->cur_attname = NULL;
3510  }
3511  }
3512 
3513  /*
3514  * Now compute and insert any defaults available for the columns not
3515  * provided by the input data. Anything not processed here or above will
3516  * remain NULL.
3517  */
3518  for (i = 0; i < num_defaults; i++)
3519  {
3520  /*
3521  * The caller must supply econtext and have switched into the
3522  * per-tuple memory context in it.
3523  */
3524  Assert(econtext != NULL);
3526 
3527  values[defmap[i]] = ExecEvalExpr(defexprs[i], econtext,
3528  &nulls[defmap[i]]);
3529  }
3530 
3531  return true;
3532 }
signed short int16
Definition: c.h:312
bool NextCopyFromRawFields(CopyState cstate, char ***fields, int *nfields)
Definition: copy.c:3245
static Datum CopyReadBinaryAttribute(CopyState cstate, int column_no, FmgrInfo *flinfo, Oid typioparam, int32 typmod, bool *isnull)
Definition: copy.c:4427
Definition: fmgr.h:56
bool csv_mode
Definition: copy.c:121
bool binary
Definition: copy.c:118
#define RelationGetDescr(relation)
Definition: rel.h:433
FmgrInfo * in_functions
Definition: copy.c:164
AttrNumber num_defaults
Definition: copy.c:160
List * attnumlist
Definition: copy.c:114
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:93
bool file_has_oids
Definition: copy.c:161
#define DatumGetObjectId(X)
Definition: postgres.h:485
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:226
struct cursor * cur
Definition: ecpg.c:28
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:908
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:590
unsigned int Oid
Definition: postgres_ext.h:31
bool * force_null_flags
Definition: copy.c:135
int natts
Definition: tupdesc.h:82
bool * convert_select_flags
Definition: copy.c:138
CopyDest copy_dest
Definition: copy.c:101
char * null_print
Definition: copy.c:123
const char * cur_attname
Definition: copy.c:143
Relation rel
Definition: copy.c:112
#define ERROR
Definition: elog.h:43
static int CopyGetData(CopyState cstate, void *databuf, int minread, int maxread)
Definition: copy.c:557
#define lfirst_int(lc)
Definition: pg_list.h:107
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:281
ExprState ** defexprs
Definition: copy.c:167
#define CStringGetDatum(X)
Definition: postgres.h:563
char string[11]
Definition: preproc-type.c:46
FmgrInfo oid_in_function
Definition: copy.c:162
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:197
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#define ereport(elevel, rest)
Definition: elog.h:122
Oid * typioparams
Definition: copy.c:165
uintptr_t Datum
Definition: postgres.h:367
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1709
#define InvalidOid
Definition: postgres_ext.h:36
int16 attnum
Definition: pg_attribute.h:79
bool * force_notnull_flags
Definition: copy.c:133
#define Assert(condition)
Definition: c.h:699
static int list_length(const List *l)
Definition: pg_list.h:89
static Datum values[MAXATTR]
Definition: bootstrap.c:164
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define NameStr(name)
Definition: c.h:576
static bool CopyGetInt16(CopyState cstate, int16 *val)
Definition: copy.c:712
int cur_lineno
Definition: copy.c:142
Datum oidin(PG_FUNCTION_ARGS)
Definition: oid.c:117
const char * cur_attval
Definition: copy.c:144
Oid oid_typioparam
Definition: copy.c:163
int16 AttrNumber
Definition: attnum.h:21
int * defmap
Definition: copy.c:166
bool oids
Definition: copy.c:119

◆ NextCopyFromRawFields()

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

Definition at line 3245 of file copy.c.

References Assert, CopyStateData::binary, CopyReadAttributesCSV(), CopyReadAttributesText(), CopyReadLine(), CopyStateData::csv_mode, CopyStateData::cur_lineno, CopyStateData::header_line, StringInfoData::len, CopyStateData::line_buf, and CopyStateData::raw_fields.

Referenced by NextCopyFrom().

3246 {
3247  int fldct;
3248  bool done;
3249 
3250  /* only available for text or csv input */
3251  Assert(!cstate->binary);
3252 
3253  /* on input just throw the header line away */
3254  if (cstate->cur_lineno == 0 && cstate->header_line)
3255  {
3256  cstate->cur_lineno++;
3257  if (CopyReadLine(cstate))
3258  return false; /* done */
3259  }
3260 
3261  cstate->cur_lineno++;
3262 
3263  /* Actually read the line into memory here */
3264  done = CopyReadLine(cstate);
3265 
3266  /*
3267  * EOF at start of line means we're done. If we see EOF after some
3268  * characters, we act as though it was newline followed by EOF, ie,
3269  * process the line and then exit loop on next iteration.
3270  */
3271  if (done && cstate->line_buf.len == 0)
3272  return false;
3273 
3274  /* Parse the line into de-escaped field values */
3275  if (cstate->csv_mode)
3276  fldct = CopyReadAttributesCSV(cstate);
3277  else
3278  fldct = CopyReadAttributesText(cstate);
3279 
3280  *fields = cstate->raw_fields;
3281  *nfields = fldct;
3282  return true;
3283 }
bool csv_mode
Definition: copy.c:121
char ** raw_fields
Definition: copy.c:189
bool binary
Definition: copy.c:118
StringInfoData line_buf
Definition: copy.c:198
static int CopyReadAttributesCSV(CopyState cstate)
Definition: copy.c:4258
bool header_line
Definition: copy.c:122
#define Assert(condition)
Definition: c.h:699
static bool CopyReadLine(CopyState cstate)
Definition: copy.c:3554
int cur_lineno
Definition: copy.c:142
static int CopyReadAttributesText(CopyState cstate)
Definition: copy.c:4030

◆ ProcessCopyOptions()

void ProcessCopyOptions ( ParseState pstate,
CopyState  cstate,
bool  is_from,
List options 
)

Definition at line 1031 of file copy.c.

References DefElem::arg, CopyStateData::binary, castNode, CopyStateData::convert_select, CopyStateData::convert_selectively, CopyStateData::csv_mode, defGetBoolean(), defGetString(), DefElem::defname, CopyStateData::delim, ereport, errcode(), errmsg(), ERROR, CopyStateData::escape, CopyStateData::file_encoding, CopyStateData::force_notnull, CopyStateData::force_null, CopyStateData::force_quote, CopyStateData::force_quote_all, CopyStateData::freeze, CopyStateData::header_line, IsA, lfirst_node, DefElem::location, NIL, CopyStateData::null_print, CopyStateData::null_print_len, CopyStateData::oids, palloc0(), parser_errposition(), pg_char_to_encoding(), and CopyStateData::quote.

Referenced by BeginCopy(), and file_fdw_validator().

1035 {
1036  bool format_specified = false;
1037  ListCell *option;
1038 
1039  /* Support external use for option sanity checking */
1040  if (cstate == NULL)
1041  cstate = (CopyStateData *) palloc0(sizeof(CopyStateData));
1042 
1043  cstate->file_encoding = -1;
1044 
1045  /* Extract options from the statement node tree */
1046  foreach(option, options)
1047  {
1048  DefElem *defel = lfirst_node(DefElem, option);
1049 
1050  if (strcmp(defel->defname, "format") == 0)
1051  {
1052  char *fmt = defGetString(defel);
1053 
1054  if (format_specified)
1055  ereport(ERROR,
1056  (errcode(ERRCODE_SYNTAX_ERROR),
1057  errmsg("conflicting or redundant options"),
1058  parser_errposition(pstate, defel->location)));
1059  format_specified = true;
1060  if (strcmp(fmt, "text") == 0)
1061  /* default format */ ;
1062  else if (strcmp(fmt, "csv") == 0)
1063  cstate->csv_mode = true;
1064  else if (strcmp(fmt, "binary") == 0)
1065  cstate->binary = true;
1066  else
1067  ereport(ERROR,
1068  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1069  errmsg("COPY format \"%s\" not recognized", fmt),
1070  parser_errposition(pstate, defel->location)));
1071  }
1072  else if (strcmp(defel->defname, "oids") == 0)
1073  {
1074  if (cstate->oids)
1075  ereport(ERROR,
1076  (errcode(ERRCODE_SYNTAX_ERROR),
1077  errmsg("conflicting or redundant options"),
1078  parser_errposition(pstate, defel->location)));
1079  cstate->oids = defGetBoolean(defel);
1080  }
1081  else if (strcmp(defel->defname, "freeze") == 0)
1082  {
1083  if (cstate->freeze)
1084  ereport(ERROR,
1085  (errcode(ERRCODE_SYNTAX_ERROR),
1086  errmsg("conflicting or redundant options"),
1087  parser_errposition(pstate, defel->location)));
1088  cstate->freeze = defGetBoolean(defel);
1089  }
1090  else if (strcmp(defel->defname, "delimiter") == 0)
1091  {
1092  if (cstate->delim)
1093  ereport(ERROR,
1094  (errcode(ERRCODE_SYNTAX_ERROR),
1095  errmsg("conflicting or redundant options"),
1096  parser_errposition(pstate, defel->location)));
1097  cstate->delim = defGetString(defel);
1098  }
1099  else if (strcmp(defel->defname, "null") == 0)
1100  {
1101  if (cstate->null_print)
1102  ereport(ERROR,
1103  (errcode(ERRCODE_SYNTAX_ERROR),
1104  errmsg("conflicting or redundant options"),
1105  parser_errposition(pstate, defel->location)));
1106  cstate->null_print = defGetString(defel);
1107  }
1108  else if (strcmp(defel->defname, "header") == 0)
1109  {
1110  if (cstate->header_line)
1111  ereport(ERROR,
1112  (errcode(ERRCODE_SYNTAX_ERROR),
1113  errmsg("conflicting or redundant options"),
1114  parser_errposition(pstate, defel->location)));
1115  cstate->header_line = defGetBoolean(defel);
1116  }
1117  else if (strcmp(defel->defname, "quote") == 0)
1118  {
1119  if (cstate->quote)
1120  ereport(ERROR,
1121  (errcode(ERRCODE_SYNTAX_ERROR),
1122  errmsg("conflicting or redundant options"),
1123  parser_errposition(pstate, defel->location)));
1124  cstate->quote = defGetString(defel);
1125  }
1126  else if (strcmp(defel->defname, "escape") == 0)
1127  {
1128  if (cstate->escape)
1129  ereport(ERROR,
1130  (errcode(ERRCODE_SYNTAX_ERROR),
1131  errmsg("conflicting or redundant options"),
1132  parser_errposition(pstate, defel->location)));
1133  cstate->escape = defGetString(defel);
1134  }
1135  else if (strcmp(defel->defname, "force_quote") == 0)
1136  {
1137  if (cstate->force_quote || cstate->force_quote_all)
1138  ereport(ERROR,
1139  (errcode(ERRCODE_SYNTAX_ERROR),
1140  errmsg("conflicting or redundant options"),
1141  parser_errposition(pstate, defel->location)));
1142  if (defel->arg && IsA(defel->arg, A_Star))
1143  cstate->force_quote_all = true;
1144  else if (defel->arg && IsA(defel->arg, List))
1145  cstate->force_quote = castNode(List, defel->arg);
1146  else
1147  ereport(ERROR,
1148  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1149  errmsg("argument to option \"%s\" must be a list of column names",
1150  defel->defname),
1151  parser_errposition(pstate, defel->location)));
1152  }
1153  else if (strcmp(defel->defname, "force_not_null") == 0)
1154  {
1155  if (cstate->force_notnull)
1156  ereport(ERROR,
1157  (errcode(ERRCODE_SYNTAX_ERROR),
1158  errmsg("conflicting or redundant options"),
1159  parser_errposition(pstate, defel->location)));
1160  if (defel->arg && IsA(defel->arg, List))
1161  cstate->force_notnull = castNode(List, defel->arg);
1162  else
1163  ereport(ERROR,
1164  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1165  errmsg("argument to option \"%s\" must be a list of column names",
1166  defel->defname),
1167  parser_errposition(pstate, defel->location)));
1168  }
1169  else if (strcmp(defel->defname, "force_null") == 0)
1170  {
1171  if (cstate->force_null)
1172  ereport(ERROR,
1173  (errcode(ERRCODE_SYNTAX_ERROR),
1174  errmsg("conflicting or redundant options")));
1175  if (defel->arg && IsA(defel->arg, List))
1176  cstate->force_null = castNode(List, defel->arg);
1177  else
1178  ereport(ERROR,
1179  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1180  errmsg("argument to option \"%s\" must be a list of column names",
1181  defel->defname),
1182  parser_errposition(pstate, defel->location)));
1183  }
1184  else if (strcmp(defel->defname, "convert_selectively") == 0)
1185  {
1186  /*
1187  * Undocumented, not-accessible-from-SQL option: convert only the
1188  * named columns to binary form, storing the rest as NULLs. It's
1189  * allowed for the column list to be NIL.
1190  */
1191  if (cstate->convert_selectively)
1192  ereport(ERROR,
1193  (errcode(ERRCODE_SYNTAX_ERROR),
1194  errmsg("conflicting or redundant options"),
1195  parser_errposition(pstate, defel->location)));
1196  cstate->convert_selectively = true;
1197  if (defel->arg == NULL || IsA(defel->arg, List))
1198  cstate->convert_select = castNode(List, defel->arg);
1199  else
1200  ereport(ERROR,
1201  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1202  errmsg("argument to option \"%s\" must be a list of column names",
1203  defel->defname),
1204  parser_errposition(pstate, defel->location)));
1205  }
1206  else if (strcmp(defel->defname, "encoding") == 0)
1207  {
1208  if (cstate->file_encoding >= 0)
1209  ereport(ERROR,
1210  (errcode(ERRCODE_SYNTAX_ERROR),
1211  errmsg("conflicting or redundant options"),
1212  parser_errposition(pstate, defel->location)));
1214  if (cstate->file_encoding < 0)
1215  ereport(ERROR,
1216  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1217  errmsg("argument to option \"%s\" must be a valid encoding name",
1218  defel->defname),
1219  parser_errposition(pstate, defel->location)));
1220  }
1221  else
1222  ereport(ERROR,
1223  (errcode(ERRCODE_SYNTAX_ERROR),
1224  errmsg("option \"%s\" not recognized",
1225  defel->defname),
1226  parser_errposition(pstate, defel->location)));
1227  }
1228 
1229  /*
1230  * Check for incompatible options (must do these two before inserting
1231  * defaults)
1232  */
1233  if (cstate->binary && cstate->delim)
1234  ereport(ERROR,
1235  (errcode(ERRCODE_SYNTAX_ERROR),
1236  errmsg("cannot specify DELIMITER in BINARY mode")));
1237 
1238  if (cstate->binary && cstate->null_print)
1239  ereport(ERROR,
1240  (errcode(ERRCODE_SYNTAX_ERROR),
1241  errmsg("cannot specify NULL in BINARY mode")));
1242 
1243  /* Set defaults for omitted options */
1244  if (!cstate->delim)
1245  cstate->delim = cstate->csv_mode ? "," : "\t";
1246 
1247  if (!cstate->null_print)
1248  cstate->null_print = cstate->csv_mode ? "" : "\\N";
1249  cstate->null_print_len = strlen(cstate->null_print);
1250 
1251  if (cstate->csv_mode)
1252  {
1253  if (!cstate->quote)
1254  cstate->quote = "\"";
1255  if (!cstate->escape)
1256  cstate->escape = cstate->quote;
1257  }
1258 
1259  /* Only single-byte delimiter strings are supported. */
1260  if (strlen(cstate->delim) != 1)
1261  ereport(ERROR,
1262  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1263  errmsg("COPY delimiter must be a single one-byte character")));
1264 
1265  /* Disallow end-of-line characters */
1266  if (strchr(cstate->delim, '\r') != NULL ||
1267  strchr(cstate->delim, '\n') != NULL)
1268  ereport(ERROR,
1269  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1270  errmsg("COPY delimiter cannot be newline or carriage return")));
1271 
1272  if (strchr(cstate->null_print, '\r') != NULL ||
1273  strchr(cstate->null_print, '\n') != NULL)
1274  ereport(ERROR,
1275  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1276  errmsg("COPY null representation cannot use newline or carriage return")));
1277 
1278  /*
1279  * Disallow unsafe delimiter characters in non-CSV mode. We can't allow
1280  * backslash because it would be ambiguous. We can't allow the other
1281  * cases because data characters matching the delimiter must be
1282  * backslashed, and certain backslash combinations are interpreted
1283  * non-literally by COPY IN. Disallowing all lower case ASCII letters is
1284  * more than strictly necessary, but seems best for consistency and
1285  * future-proofing. Likewise we disallow all digits though only octal
1286  * digits are actually dangerous.
1287  */
1288  if (!cstate->csv_mode &&
1289  strchr("\\.abcdefghijklmnopqrstuvwxyz0123456789",
1290  cstate->delim[0]) != NULL)
1291  ereport(ERROR,
1292  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1293  errmsg("COPY delimiter cannot be \"%s\"", cstate->delim)));
1294 
1295  /* Check header */
1296  if (!cstate->csv_mode && cstate->header_line)
1297  ereport(ERROR,
1298  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1299  errmsg("COPY HEADER available only in CSV mode")));
1300 
1301  /* Check quote */
1302  if (!cstate->csv_mode && cstate->quote != NULL)
1303  ereport(ERROR,
1304  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1305  errmsg("COPY quote available only in CSV mode")));
1306 
1307  if (cstate->csv_mode && strlen(cstate->quote) != 1)
1308  ereport(ERROR,
1309  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1310  errmsg("COPY quote must be a single one-byte character")));
1311 
1312  if (cstate->csv_mode && cstate->delim[0] == cstate->quote[0])
1313  ereport(ERROR,
1314  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1315  errmsg("COPY delimiter and quote must be different")));
1316 
1317  /* Check escape */
1318  if (!cstate->csv_mode && cstate->escape != NULL)
1319  ereport(ERROR,
1320  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1321  errmsg("COPY escape available only in CSV mode")));
1322 
1323  if (cstate->csv_mode && strlen(cstate->escape) != 1)
1324  ereport(ERROR,
1325  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1326  errmsg("COPY escape must be a single one-byte character")));
1327 
1328  /* Check force_quote */
1329  if (!cstate->csv_mode && (cstate->force_quote || cstate->force_quote_all))
1330  ereport(ERROR,
1331  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1332  errmsg("COPY force quote available only in CSV mode")));
1333  if ((cstate->force_quote || cstate->force_quote_all) && is_from)
1334  ereport(ERROR,
1335  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1336  errmsg("COPY force quote only available using COPY TO")));
1337 
1338  /* Check force_notnull */
1339  if (!cstate->csv_mode && cstate->force_notnull != NIL)
1340  ereport(ERROR,
1341  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1342  errmsg("COPY force not null available only in CSV mode")));
1343  if (cstate->force_notnull != NIL && !is_from)
1344  ereport(ERROR,
1345  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1346  errmsg("COPY force not null only available using COPY FROM")));
1347 
1348  /* Check force_null */
1349  if (!cstate->csv_mode && cstate->force_null != NIL)
1350  ereport(ERROR,
1351  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1352  errmsg("COPY force null available only in CSV mode")));
1353 
1354  if (cstate->force_null != NIL && !is_from)
1355  ereport(ERROR,
1356  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1357  errmsg("COPY force null only available using COPY FROM")));
1358 
1359  /* Don't allow the delimiter to appear in the null string. */
1360  if (strchr(cstate->null_print, cstate->delim[0]) != NULL)
1361  ereport(ERROR,
1362  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1363  errmsg("COPY delimiter must not appear in the NULL specification")));
1364 
1365  /* Don't allow the CSV quote char to appear in the null string. */
1366  if (cstate->csv_mode &&
1367  strchr(cstate->null_print, cstate->quote[0]) != NULL)
1368  ereport(ERROR,
1369  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1370  errmsg("CSV quote character must not appear in the NULL specification")));
1371 }
#define NIL
Definition: pg_list.h:69
bool csv_mode
Definition: copy.c:121
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
int pg_char_to_encoding(const char *name)
Definition: encnames.c:551
bool binary
Definition: copy.c:118
#define castNode(_type_, nodeptr)
Definition: nodes.h:586
int errcode(int sqlerrcode)
Definition: elog.c:575
char * delim
Definition: copy.c:126
char * null_print
Definition: copy.c:123
bool defGetBoolean(DefElem *def)
Definition: define.c:111
#define ERROR
Definition: elog.h:43
char * defGetString(DefElem *def)
Definition: define.c:49
#define lfirst_node(type, lc)
Definition: pg_list.h:109
char * quote
Definition: copy.c:127
int location
Definition: parsenodes.h:733
char * escape
Definition: copy.c:128
#define ereport(elevel, rest)
Definition: elog.h:122
int null_print_len
Definition: copy.c:124
List * force_null
Definition: copy.c:134
Node * arg
Definition: parsenodes.h:731
int file_encoding
Definition: copy.c:107
void * palloc0(Size size)
Definition: mcxt.c:955
bool header_line
Definition: copy.c:122
bool freeze
Definition: copy.c:120
List * force_notnull
Definition: copy.c:132
List * convert_select
Definition: copy.c:137
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
bool force_quote_all
Definition: copy.c:130
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:730
Definition: pg_list.h:45
List * force_quote
Definition: copy.c:129
bool convert_selectively
Definition: copy.c:136
bool oids
Definition: copy.c:119