PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

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

Definition at line 24 of file copy.h.

Definition at line 23 of file copy.h.

Function Documentation

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 2974 of file copy.c.

References AllocateFile(), Assert, 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, OIDOID, 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, TupleDescAttr, CopyStateData::typioparams, CopyStateData::volatile_defexprs, and whereToSendOutput.

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

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

Definition at line 2281 of file copy.c.

References AfterTriggerBeginQuery(), AfterTriggerEndQuery(), ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, CHECK_FOR_INTERRUPTS, CMD_INSERT, tupleDesc::constr, convert_tuples_by_name(), CopyStateData::copy_dest, COPY_OLD_FE, CopyFromErrorCallback(), CopyFromInsertBatch(), CreateExecutorState(), CopyStateData::cur_lineno, CurrentMemoryContext, do_convert_tuple(), 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(), ExecCloseIndices(), ExecConstraints(), ExecDropSingleTupleTableSlot(), ExecFindPartition(), ExecInitExtraTupleSlot(), ExecInsertIndexTuples(), ExecIRInsertTriggers(), ExecMaterializeSlot(), ExecOpenIndices(), ExecResetTupleTable(), ExecSetSlotDescriptor(), ExecSetupPartitionTupleRouting(), ExecStoreTuple(), FreeBulkInsertState(), FreeExecutorState(), CopyStateData::freeze, GetBulkInsertState(), GetCurrentCommandId(), GetCurrentSubTransactionId(), GetPerTupleExprContext, GetPerTupleMemoryContext, gettext_noop, heap_close, heap_form_tuple(), heap_insert(), HEAP_INSERT_FROZEN, HEAP_INSERT_SKIP_FSM, HEAP_INSERT_SKIP_WAL, heap_sync(), HeapTupleSetOid, i, InitResultRelInfo(), InvalidBuffer, InvalidOid, InvalidSubTransactionId, list_free(), makeNode, MakeTransitionCaptureState(), MAX_BUFFERED_TUPLES, MemoryContextSwitchTo(), tupleDesc::natts, NextCopyFrom(), NIL, NoLock, CopyStateData::num_dispatch, CopyStateData::num_partitions, palloc(), palloc0(), CopyStateData::partition_dispatch_info, CopyStateData::partition_tupconv_maps, CopyStateData::partition_tuple_slot, CopyStateData::partitions, pfree(), pq_endmsgread(), ErrorContextCallback::previous, CopyStateData::range_table, RelationData::rd_att, RelationData::rd_createSubid, RelationData::rd_newRelfilenodeSubid, RelationData::rd_rel, CopyStateData::rel, RelationGetDescr, RelationGetRelationName, RelationGetRelid, PartitionDispatchData::reldesc, ReleaseBulkInsertStatePin(), RELKIND_FOREIGN_TABLE, RELKIND_MATVIEW, RELKIND_PARTITIONED_TABLE, RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_VIEW, ResetPerTupleExprContext, ResultRelInfo::ri_FdwRoutine, ResultRelInfo::ri_NumIndices, ResultRelInfo::ri_PartitionCheck, ResultRelInfo::ri_RelationDesc, ResultRelInfo::ri_TrigDesc, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, TransitionCaptureState::tcs_map, TransitionCaptureState::tcs_original_insert_tuple, ThereAreNoPriorRegisteredSnapshots(), ThereAreNoReadyPortals(), CopyStateData::transition_capture, CopyStateData::transition_tupconv_maps, TriggerDesc::trig_insert_before_row, TriggerDesc::trig_insert_instead_row, RelationData::trigdesc, PartitionDispatchData::tupslot, values, CopyStateData::volatile_defexprs, and XLogIsNeeded.

Referenced by copy_table(), and DoCopy().

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

Definition at line 2176 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().

2177 {
2178  CopyState cstate = (CopyState) arg;
2179 
2180  if (cstate->binary)
2181  {
2182  /* can't usefully display the data */
2183  if (cstate->cur_attname)
2184  errcontext("COPY %s, line %d, column %s",
2185  cstate->cur_relname, cstate->cur_lineno,
2186  cstate->cur_attname);
2187  else
2188  errcontext("COPY %s, line %d",
2189  cstate->cur_relname, cstate->cur_lineno);
2190  }
2191  else
2192  {
2193  if (cstate->cur_attname && cstate->cur_attval)
2194  {
2195  /* error is relevant to a particular column */
2196  char *attval;
2197 
2198  attval = limit_printout_length(cstate->cur_attval);
2199  errcontext("COPY %s, line %d, column %s: \"%s\"",
2200  cstate->cur_relname, cstate->cur_lineno,
2201  cstate->cur_attname, attval);
2202  pfree(attval);
2203  }
2204  else if (cstate->cur_attname)
2205  {
2206  /* error is relevant to a particular column, value is NULL */
2207  errcontext("COPY %s, line %d, column %s: null input",
2208  cstate->cur_relname, cstate->cur_lineno,
2209  cstate->cur_attname);
2210  }
2211  else
2212  {
2213  /*
2214  * Error is relevant to a particular line.
2215  *
2216  * If line_buf still contains the correct line, and it's already
2217  * transcoded, print it. If it's still in a foreign encoding, it's
2218  * quite likely that the error is precisely a failure to do
2219  * encoding conversion (ie, bad data). We dare not try to convert
2220  * it, and at present there's no way to regurgitate it without
2221  * conversion. So we have to punt and just report the line number.
2222  */
2223  if (cstate->line_buf_valid &&
2224  (cstate->line_buf_converted || !cstate->need_transcoding))
2225  {
2226  char *lineval;
2227 
2228  lineval = limit_printout_length(cstate->line_buf.data);
2229  errcontext("COPY %s, line %d: \"%s\"",
2230  cstate->cur_relname, cstate->cur_lineno, lineval);
2231  pfree(lineval);
2232  }
2233  else
2234  {
2235  errcontext("COPY %s, line %d",
2236  cstate->cur_relname, cstate->cur_lineno);
2237  }
2238  }
2239  }
2240 }
bool binary
Definition: copy.c:114
bool need_transcoding
Definition: copy.c:104
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:139
void pfree(void *pointer)
Definition: mcxt.c:949
static char * limit_printout_length(const char *str)
Definition: copy.c:2252
const char * cur_relname
Definition: copy.c:137
#define errcontext
Definition: elog.h:164
struct CopyStateData * CopyState
Definition: copy.h:23
void * arg
int cur_lineno
Definition: copy.c:138
const char * cur_attval
Definition: copy.c:140
DestReceiver* CreateCopyDestReceiver ( void  )

Definition at line 4837 of file copy.c.

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

Referenced by CreateDestReceiver().

4838 {
4839  DR_copy *self = (DR_copy *) palloc(sizeof(DR_copy));
4840 
4841  self->pub.receiveSlot = copy_dest_receive;
4842  self->pub.rStartup = copy_dest_startup;
4843  self->pub.rShutdown = copy_dest_shutdown;
4844  self->pub.rDestroy = copy_dest_destroy;
4845  self->pub.mydest = DestCopyOut;
4846 
4847  self->cstate = NULL; /* will be set later */
4848  self->processed = 0;
4849 
4850  return (DestReceiver *) self;
4851 }
Definition: copy.c:216
static void copy_dest_destroy(DestReceiver *self)
Definition: copy.c:4828
static void copy_dest_shutdown(DestReceiver *self)
Definition: copy.c:4819
static void copy_dest_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: copy.c:4791
static bool copy_dest_receive(TupleTableSlot *slot, DestReceiver *self)
Definition: copy.c:4800
void * palloc(Size size)
Definition: mcxt.c:848
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(), heap_close, heap_openrv(), ResTarget::indirection, RangeTblEntry::insertedCols, InvalidOid, CopyStmt::is_from, 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, CopyStmt::relation, RelationGetDescr, RelationGetNamespace, RelationGetRelationName, RelationGetRelid, RangeTblEntry::relid, RangeTblEntry::requiredPerms, RLS_ENABLED, RowExclusiveLock, select, RangeTblEntry::selectedCols, RawStmt::stmt, RawStmt::stmt_len, RawStmt::stmt_location, superuser(), 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  /* Disallow COPY to/from file or program except to superusers. */
794  if (!pipe && !superuser())
795  {
796  if (stmt->is_program)
797  ereport(ERROR,
798  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
799  errmsg("must be superuser to COPY to or from an external program"),
800  errhint("Anyone can COPY to stdout or from stdin. "
801  "psql's \\copy command also works for anyone.")));
802  else
803  ereport(ERROR,
804  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
805  errmsg("must be superuser to COPY to or from a file"),
806  errhint("Anyone can COPY to stdout or from stdin. "
807  "psql's \\copy command also works for anyone.")));
808  }
809 
810  if (stmt->relation)
811  {
812  TupleDesc tupDesc;
813  List *attnums;
814  ListCell *cur;
815  RangeTblEntry *rte;
816 
817  Assert(!stmt->query);
818 
819  /* Open and lock the relation, using the appropriate lock type. */
820  rel = heap_openrv(stmt->relation,
821  (is_from ? RowExclusiveLock : AccessShareLock));
822 
823  relid = RelationGetRelid(rel);
824 
825  rte = addRangeTableEntryForRelation(pstate, rel, NULL, false, false);
826  rte->requiredPerms = (is_from ? ACL_INSERT : ACL_SELECT);
827 
828  tupDesc = RelationGetDescr(rel);
829  attnums = CopyGetAttnums(tupDesc, rel, stmt->attlist);
830  foreach(cur, attnums)
831  {
832  int attno = lfirst_int(cur) -
834 
835  if (is_from)
836  rte->insertedCols = bms_add_member(rte->insertedCols, attno);
837  else
838  rte->selectedCols = bms_add_member(rte->selectedCols, attno);
839  }
840  ExecCheckRTPerms(pstate->p_rtable, true);
841 
842  /*
843  * Permission check for row security policies.
844  *
845  * check_enable_rls will ereport(ERROR) if the user has requested
846  * something invalid and will otherwise indicate if we should enable
847  * RLS (returns RLS_ENABLED) or not for this COPY statement.
848  *
849  * If the relation has a row security policy and we are to apply it
850  * then perform a "query" copy and allow the normal query processing
851  * to handle the policies.
852  *
853  * If RLS is not enabled for this, then just fall through to the
854  * normal non-filtering relation handling.
855  */
856  if (check_enable_rls(rte->relid, InvalidOid, false) == RLS_ENABLED)
857  {
859  ColumnRef *cr;
860  ResTarget *target;
861  RangeVar *from;
862  List *targetList = NIL;
863 
864  if (is_from)
865  ereport(ERROR,
866  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
867  errmsg("COPY FROM not supported with row-level security"),
868  errhint("Use INSERT statements instead.")));
869 
870  /*
871  * Build target list
872  *
873  * If no columns are specified in the attribute list of the COPY
874  * command, then the target list is 'all' columns. Therefore, '*'
875  * should be used as the target list for the resulting SELECT
876  * statement.
877  *
878  * In the case that columns are specified in the attribute list,
879  * create a ColumnRef and ResTarget for each column and add them
880  * to the target list for the resulting SELECT statement.
881  */
882  if (!stmt->attlist)
883  {
884  cr = makeNode(ColumnRef);
886  cr->location = -1;
887 
888  target = makeNode(ResTarget);
889  target->name = NULL;
890  target->indirection = NIL;
891  target->val = (Node *) cr;
892  target->location = -1;
893 
894  targetList = list_make1(target);
895  }
896  else
897  {
898  ListCell *lc;
899 
900  foreach(lc, stmt->attlist)
901  {
902  /*
903  * Build the ColumnRef for each column. The ColumnRef
904  * 'fields' property is a String 'Value' node (see
905  * nodes/value.h) that corresponds to the column name
906  * respectively.
907  */
908  cr = makeNode(ColumnRef);
909  cr->fields = list_make1(lfirst(lc));
910  cr->location = -1;
911 
912  /* Build the ResTarget and add the ColumnRef to it. */
913  target = makeNode(ResTarget);
914  target->name = NULL;
915  target->indirection = NIL;
916  target->val = (Node *) cr;
917  target->location = -1;
918 
919  /* Add each column to the SELECT statement's target list */
920  targetList = lappend(targetList, target);
921  }
922  }
923 
924  /*
925  * Build RangeVar for from clause, fully qualified based on the
926  * relation which we have opened and locked.
927  */
930  -1);
931 
932  /* Build query */
933  select = makeNode(SelectStmt);
934  select->targetList = targetList;
935  select->fromClause = list_make1(from);
936 
937  query = makeNode(RawStmt);
938  query->stmt = (Node *) select;
939  query->stmt_location = stmt_location;
940  query->stmt_len = stmt_len;
941 
942  /*
943  * Close the relation for now, but keep the lock on it to prevent
944  * changes between now and when we start the query-based COPY.
945  *
946  * We'll reopen it later as part of the query-based COPY.
947  */
948  heap_close(rel, NoLock);
949  rel = NULL;
950  }
951  }
952  else
953  {
954  Assert(stmt->query);
955 
956  query = makeNode(RawStmt);
957  query->stmt = stmt->query;
958  query->stmt_location = stmt_location;
959  query->stmt_len = stmt_len;
960 
961  relid = InvalidOid;
962  rel = NULL;
963  }
964 
965  if (is_from)
966  {
967  Assert(rel);
968 
969  /* check read-only transaction and parallel mode */
970  if (XactReadOnly && !rel->rd_islocaltemp)
971  PreventCommandIfReadOnly("COPY FROM");
972  PreventCommandIfParallelMode("COPY FROM");
973 
974  cstate = BeginCopyFrom(pstate, rel, stmt->filename, stmt->is_program,
975  NULL, stmt->attlist, stmt->options);
976  *processed = CopyFrom(cstate); /* copy from file to database */
977  EndCopyFrom(cstate);
978  }
979  else
980  {
981  cstate = BeginCopyTo(pstate, rel, query, relid,
982  stmt->filename, stmt->is_program,
983  stmt->attlist, stmt->options);
984  *processed = DoCopyTo(cstate); /* copy from database to file */
985  EndCopyTo(cstate);
986  }
987 
988  /*
989  * Close the relation. If reading, we can release the AccessShareLock we
990  * got; if writing, we should hold the lock until end of transaction to
991  * ensure that updates will be committed before lock is released.
992  */
993  if (rel != NULL)
994  heap_close(rel, (is_from ? NoLock : AccessShareLock));
995 }
List * indirection
Definition: parsenodes.h:440
#define NIL
Definition: pg_list.h:69
Node * val
Definition: parsenodes.h:441
int errhint(const char *fmt,...)
Definition: elog.c:987
void PreventCommandIfParallelMode(const char *cmdname)
Definition: utility.c:254
List * attlist
Definition: parsenodes.h:1945
List * fromClause
Definition: parsenodes.h:1515
#define RelationGetDescr(relation)
Definition: rel.h:428
char * name
Definition: parsenodes.h:439
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:2974
char * pstrdup(const char *in)
Definition: mcxt.c:1076
bool rd_islocaltemp
Definition: rel.h:90
#define AccessShareLock
Definition: lockdefs.h:36
Definition: nodes.h:510
struct cursor * cur
Definition: ecpg.c:28
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
uint64 CopyFrom(CopyState cstate)
Definition: copy.c:2281
#define select(n, r, w, e, timeout)
Definition: win32.h:374
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
AclMode requiredPerms
Definition: parsenodes.h:1053
#define heap_close(r, l)
Definition: heapam.h:97
unsigned int Oid
Definition: postgres_ext.h:31
bool is_program
Definition: parsenodes.h:1948
int location
Definition: parsenodes.h:235
int location
Definition: parsenodes.h:442
#define list_make1(x1)
Definition: pg_list.h:139
Bitmapset * selectedCols
Definition: parsenodes.h:1055
static CopyState BeginCopyTo(ParseState *pstate, Relation rel, RawStmt *query, Oid queryRelId, const char *filename, bool is_program, List *attnamelist, List *options)
Definition: copy.c:1738
#define ERROR
Definition: elog.h:43
#define lfirst_int(lc)
Definition: pg_list.h:107
RangeVar * relation
Definition: parsenodes.h:1942
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3033
Node * stmt
Definition: parsenodes.h:1425
#define NoLock
Definition: lockdefs.h:34
List * targetList
Definition: parsenodes.h:1514
static uint64 DoCopyTo(CopyState cstate)
Definition: copy.c:1876
#define RowExclusiveLock
Definition: lockdefs.h:38
List * options
Definition: parsenodes.h:1950
#define RelationGetRelationName(relation)
Definition: rel.h:436
#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:4718
#define ACL_SELECT
Definition: parsenodes.h:73
int stmt_len
Definition: parsenodes.h:1427
int stmt_location
Definition: parsenodes.h:1426
Relation heap_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: heapam.c:1318
#define InvalidOid
Definition: postgres_ext.h:36
bool XactReadOnly
Definition: xact.c:77
int check_enable_rls(Oid relid, Oid checkAsUser, bool noError)
Definition: rls.c:53
void EndCopyFrom(CopyState cstate)
Definition: copy.c:3526
#define makeNode(_type_)
Definition: nodes.h:558
#define Assert(condition)
Definition: c.h:681
#define lfirst(lc)
Definition: pg_list.h:106
#define ACL_INSERT
Definition: parsenodes.h:72
RangeTblEntry * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, Alias *alias, bool inh, bool inFromCl)
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
void PreventCommandIfReadOnly(const char *cmdname)
Definition: utility.c:236
Node * query
Definition: parsenodes.h:1943
bool is_from
Definition: parsenodes.h:1947
int errmsg(const char *fmt,...)
Definition: elog.c:797
Bitmapset * insertedCols
Definition: parsenodes.h:1056
bool ExecCheckRTPerms(List *rangeTable, bool ereport_on_violation)
Definition: execMain.c:570
char * filename
Definition: parsenodes.h:1949
static void EndCopyTo(CopyState cstate)
Definition: copy.c:1911
Definition: pg_list.h:45
#define RelationGetRelid(relation)
Definition: rel.h:416
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:419
List * fields
Definition: parsenodes.h:234
#define RelationGetNamespace(relation)
Definition: rel.h:443
void EndCopyFrom ( CopyState  cstate)

Definition at line 3526 of file copy.c.

References EndCopy().

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

3527 {
3528  /* No COPY FROM related resources except memory. */
3529 
3530  EndCopy(cstate);
3531 }
static void EndCopy(CopyState cstate)
Definition: copy.c:1715
bool NextCopyFrom ( CopyState  cstate,
ExprContext econtext,
Datum values,
bool nulls,
Oid tupleOid 
)

Definition at line 3285 of file copy.c.

References Assert, 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().

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

Definition at line 3233 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().

3234 {
3235  int fldct;
3236  bool done;
3237 
3238  /* only available for text or csv input */
3239  Assert(!cstate->binary);
3240 
3241  /* on input just throw the header line away */
3242  if (cstate->cur_lineno == 0 && cstate->header_line)
3243  {
3244  cstate->cur_lineno++;
3245  if (CopyReadLine(cstate))
3246  return false; /* done */
3247  }
3248 
3249  cstate->cur_lineno++;
3250 
3251  /* Actually read the line into memory here */
3252  done = CopyReadLine(cstate);
3253 
3254  /*
3255  * EOF at start of line means we're done. If we see EOF after some
3256  * characters, we act as though it was newline followed by EOF, ie,
3257  * process the line and then exit loop on next iteration.
3258  */
3259  if (done && cstate->line_buf.len == 0)
3260  return false;
3261 
3262  /* Parse the line into de-escaped field values */
3263  if (cstate->csv_mode)
3264  fldct = CopyReadAttributesCSV(cstate);
3265  else
3266  fldct = CopyReadAttributesText(cstate);
3267 
3268  *fields = cstate->raw_fields;
3269  *nfields = fldct;
3270  return true;
3271 }
bool csv_mode
Definition: copy.c:117
char ** raw_fields
Definition: copy.c:189
bool binary
Definition: copy.c:114
StringInfoData line_buf
Definition: copy.c:198
static int CopyReadAttributesCSV(CopyState cstate)
Definition: copy.c:4246
bool header_line
Definition: copy.c:118
#define Assert(condition)
Definition: c.h:681
static bool CopyReadLine(CopyState cstate)
Definition: copy.c:3542
int cur_lineno
Definition: copy.c:138
static int CopyReadAttributesText(CopyState cstate)
Definition: copy.c:4018
void ProcessCopyOptions ( ParseState pstate,
CopyState  cstate,
bool  is_from,
List options 
)

Definition at line 1015 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().

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