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

References AllocateFile(), Assert, CopyStateData::attnumlist, CopyStateData::attribute_buf, tupleDesc::attrs, 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, initLongStringInfo(), InvalidOid, CopyStateData::is_program, CopyStateData::line_buf, CopyStateData::line_buf_converted, list_length(), list_member_int(), CopyStateData::max_fields, MemoryContextSwitchTo(), tupleDesc::natts, NULL, CopyStateData::num_defaults, CopyStateData::oid_in_function, CopyStateData::oid_typioparam, OIDOID, CopyStateData::oids, OpenPipeStream(), palloc(), PG_BINARY_R, pstrdup(), CopyStateData::raw_buf, CopyStateData::raw_buf_index, CopyStateData::raw_buf_len, RAW_BUF_SIZE, CopyStateData::raw_fields, ReceiveCopyBegin(), CopyStateData::rel, RelationGetDescr, RelationGetRelationName, CopyStateData::typioparams, CopyStateData::volatile_defexprs, and whereToSendOutput.

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

2891 {
2892  CopyState cstate;
2893  bool pipe = (filename == NULL);
2894  TupleDesc tupDesc;
2895  Form_pg_attribute *attr;
2896  AttrNumber num_phys_attrs,
2897  num_defaults;
2898  FmgrInfo *in_functions;
2899  Oid *typioparams;
2900  int attnum;
2901  Oid in_func_oid;
2902  int *defmap;
2903  ExprState **defexprs;
2904  MemoryContext oldcontext;
2905  bool volatile_defexprs;
2906 
2907  cstate = BeginCopy(pstate, true, rel, NULL, InvalidOid, attnamelist, options);
2908  oldcontext = MemoryContextSwitchTo(cstate->copycontext);
2909 
2910  /* Initialize state variables */
2911  cstate->fe_eof = false;
2912  cstate->eol_type = EOL_UNKNOWN;
2913  cstate->cur_relname = RelationGetRelationName(cstate->rel);
2914  cstate->cur_lineno = 0;
2915  cstate->cur_attname = NULL;
2916  cstate->cur_attval = NULL;
2917 
2918  /* Set up variables to avoid per-attribute overhead. */
2920  initLongStringInfo(&cstate->line_buf);
2921  cstate->line_buf_converted = false;
2922  cstate->raw_buf = (char *) palloc(RAW_BUF_SIZE + 1);
2923  cstate->raw_buf_index = cstate->raw_buf_len = 0;
2924 
2925  tupDesc = RelationGetDescr(cstate->rel);
2926  attr = tupDesc->attrs;
2927  num_phys_attrs = tupDesc->natts;
2928  num_defaults = 0;
2929  volatile_defexprs = false;
2930 
2931  /*
2932  * Pick up the required catalog information for each attribute in the
2933  * relation, including the input function, the element type (to pass to
2934  * the input function), and info about defaults and constraints. (Which
2935  * input function we use depends on text/binary format choice.)
2936  */
2937  in_functions = (FmgrInfo *) palloc(num_phys_attrs * sizeof(FmgrInfo));
2938  typioparams = (Oid *) palloc(num_phys_attrs * sizeof(Oid));
2939  defmap = (int *) palloc(num_phys_attrs * sizeof(int));
2940  defexprs = (ExprState **) palloc(num_phys_attrs * sizeof(ExprState *));
2941 
2942  for (attnum = 1; attnum <= num_phys_attrs; attnum++)
2943  {
2944  /* We don't need info for dropped attributes */
2945  if (attr[attnum - 1]->attisdropped)
2946  continue;
2947 
2948  /* Fetch the input function and typioparam info */
2949  if (cstate->binary)
2950  getTypeBinaryInputInfo(attr[attnum - 1]->atttypid,
2951  &in_func_oid, &typioparams[attnum - 1]);
2952  else
2953  getTypeInputInfo(attr[attnum - 1]->atttypid,
2954  &in_func_oid, &typioparams[attnum - 1]);
2955  fmgr_info(in_func_oid, &in_functions[attnum - 1]);
2956 
2957  /* Get default info if needed */
2958  if (!list_member_int(cstate->attnumlist, attnum))
2959  {
2960  /* attribute is NOT to be copied from input */
2961  /* use default value if one exists */
2962  Expr *defexpr = (Expr *) build_column_default(cstate->rel,
2963  attnum);
2964 
2965  if (defexpr != NULL)
2966  {
2967  /* Run the expression through planner */
2968  defexpr = expression_planner(defexpr);
2969 
2970  /* Initialize executable expression in copycontext */
2971  defexprs[num_defaults] = ExecInitExpr(defexpr, NULL);
2972  defmap[num_defaults] = attnum - 1;
2973  num_defaults++;
2974 
2975  /*
2976  * If a default expression looks at the table being loaded,
2977  * then it could give the wrong answer when using
2978  * multi-insert. Since database access can be dynamic this is
2979  * hard to test for exactly, so we use the much wider test of
2980  * whether the default expression is volatile. We allow for
2981  * the special case of when the default expression is the
2982  * nextval() of a sequence which in this specific case is
2983  * known to be safe for use with the multi-insert
2984  * optimization. Hence we use this special case function
2985  * checker rather than the standard check for
2986  * contain_volatile_functions().
2987  */
2988  if (!volatile_defexprs)
2989  volatile_defexprs = contain_volatile_functions_not_nextval((Node *) defexpr);
2990  }
2991  }
2992  }
2993 
2994  /* We keep those variables in cstate. */
2995  cstate->in_functions = in_functions;
2996  cstate->typioparams = typioparams;
2997  cstate->defmap = defmap;
2998  cstate->defexprs = defexprs;
2999  cstate->volatile_defexprs = volatile_defexprs;
3000  cstate->num_defaults = num_defaults;
3001  cstate->is_program = is_program;
3002 
3003  if (data_source_cb)
3004  {
3005  cstate->copy_dest = COPY_CALLBACK;
3006  cstate->data_source_cb = data_source_cb;
3007  }
3008  else if (pipe)
3009  {
3010  Assert(!is_program); /* the grammar does not allow this */
3012  ReceiveCopyBegin(cstate);
3013  else
3014  cstate->copy_file = stdin;
3015  }
3016  else
3017  {
3018  cstate->filename = pstrdup(filename);
3019 
3020  if (cstate->is_program)
3021  {
3022  cstate->copy_file = OpenPipeStream(cstate->filename, PG_BINARY_R);
3023  if (cstate->copy_file == NULL)
3024  ereport(ERROR,
3026  errmsg("could not execute command \"%s\": %m",
3027  cstate->filename)));
3028  }
3029  else
3030  {
3031  struct stat st;
3032 
3033  cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_R);
3034  if (cstate->copy_file == NULL)
3035  {
3036  /* copy errno because ereport subfunctions might change it */
3037  int save_errno = errno;
3038 
3039  ereport(ERROR,
3041  errmsg("could not open file \"%s\" for reading: %m",
3042  cstate->filename),
3043  (save_errno == ENOENT || save_errno == EACCES) ?
3044  errhint("COPY FROM instructs the PostgreSQL server process to read a file. "
3045  "You may want a client-side facility such as psql's \\copy.") : 0));
3046  }
3047 
3048  if (fstat(fileno(cstate->copy_file), &st))
3049  ereport(ERROR,
3051  errmsg("could not stat file \"%s\": %m",
3052  cstate->filename)));
3053 
3054  if (S_ISDIR(st.st_mode))
3055  ereport(ERROR,
3056  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3057  errmsg("\"%s\" is a directory", cstate->filename)));
3058  }
3059  }
3060 
3061  if (!cstate->binary)
3062  {
3063  /* must rely on user to tell us... */
3064  cstate->file_has_oids = cstate->oids;
3065  }
3066  else
3067  {
3068  /* Read and verify binary header */
3069  char readSig[11];
3070  int32 tmp;
3071 
3072  /* Signature */
3073  if (CopyGetData(cstate, readSig, 11, 11) != 11 ||
3074  memcmp(readSig, BinarySignature, 11) != 0)
3075  ereport(ERROR,
3076  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3077  errmsg("COPY file signature not recognized")));
3078  /* Flags field */
3079  if (!CopyGetInt32(cstate, &tmp))
3080  ereport(ERROR,
3081  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3082  errmsg("invalid COPY file header (missing flags)")));
3083  cstate->file_has_oids = (tmp & (1 << 16)) != 0;
3084  tmp &= ~(1 << 16);
3085  if ((tmp >> 16) != 0)
3086  ereport(ERROR,
3087  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3088  errmsg("unrecognized critical flags in COPY file header")));
3089  /* Header extension length */
3090  if (!CopyGetInt32(cstate, &tmp) ||
3091  tmp < 0)
3092  ereport(ERROR,
3093  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3094  errmsg("invalid COPY file header (missing length)")));
3095  /* Skip extension header, if present */
3096  while (tmp-- > 0)
3097  {
3098  if (CopyGetData(cstate, readSig, 1, 1) != 1)
3099  ereport(ERROR,
3100  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3101  errmsg("invalid COPY file header (wrong length)")));
3102  }
3103  }
3104 
3105  if (cstate->file_has_oids && cstate->binary)
3106  {
3108  &in_func_oid, &cstate->oid_typioparam);
3109  fmgr_info(in_func_oid, &cstate->oid_in_function);
3110  }
3111 
3112  /* create workspace for CopyReadAttributes results */
3113  if (!cstate->binary)
3114  {
3115  AttrNumber attr_count = list_length(cstate->attnumlist);
3116  int nfields = cstate->file_has_oids ? (attr_count + 1) : attr_count;
3117 
3118  cstate->max_fields = nfields;
3119  cstate->raw_fields = (char **) palloc(nfields * sizeof(char *));
3120  }
3121 
3122  MemoryContextSwitchTo(oldcontext);
3123 
3124  return cstate;
3125 }
Definition: fmgr.h:53
static CopyState BeginCopy(ParseState *pstate, bool is_from, Relation rel, RawStmt *raw_query, Oid queryRelId, List *attnamelist, List *options)
Definition: copy.c:1378
bool contain_volatile_functions_not_nextval(Node *clause)
Definition: clauses.c:994
static bool CopyGetInt32(CopyState cstate, int32 *val)
Definition: copy.c:681
int errhint(const char *fmt,...)
Definition: elog.c:987
char ** raw_fields
Definition: copy.c:187
bool binary
Definition: copy.c:114
#define RelationGetDescr(relation)
Definition: rel.h:425
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
bool file_has_oids
Definition: copy.c:157
char * pstrdup(const char *in)
Definition: mcxt.c:1077
static void ReceiveCopyBegin(CopyState cstate)
Definition: copy.c:379
void initLongStringInfo(StringInfo str)
Definition: stringinfo.c:81
Expr * expression_planner(Expr *expr)
Definition: planner.c:5382
Form_pg_attribute * attrs
Definition: tupdesc.h:74
StringInfoData line_buf
Definition: copy.c:196
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: nodes.h:520
int raw_buf_index
Definition: copy.c:209
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:1040
int natts
Definition: tupdesc.h:73
bool line_buf_converted
Definition: copy.c:197
signed int int32
Definition: c.h:256
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
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execQual.c:4267
#define ERROR
Definition: elog.h:43
static int CopyGetData(CopyState cstate, void *databuf, int minread, int maxread)
Definition: copy.c:555
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:159
bool list_member_int(const List *list, int datum)
Definition: list.c:485
char * raw_buf
Definition: copy.c:208
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:2057
FmgrInfo oid_in_function
Definition: copy.c:158
#define RelationGetRelationName(relation)
Definition: rel.h:433
static const char BinarySignature[11]
Definition: copy.c:287
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
int raw_buf_len
Definition: copy.c:210
int max_fields
Definition: copy.c:186
FILE * OpenPipeStream(const char *command, const char *mode)
Definition: fd.c:2147
void getTypeBinaryInputInfo(Oid type, Oid *typReceive, Oid *typIOParam)
Definition: lsyscache.c:2633
#define ereport(elevel, rest)
Definition: elog.h:122
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2567
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:207
#define InvalidOid
Definition: postgres_ext.h:36
EolType eol_type
Definition: copy.c:102
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static int list_length(const List *l)
Definition: pg_list.h:89
static char * filename
Definition: pg_dumpall.c:87
void * palloc(Size size)
Definition: mcxt.c:849
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:182
const char * cur_attval
Definition: copy.c:140
CommandDest whereToSendOutput
Definition: postgres.c:86
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
uint64 CopyFrom ( CopyState  cstate)

Definition at line 2297 of file copy.c.

References AfterTriggerBeginQuery(), AfterTriggerEndQuery(), ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, CHECK_FOR_INTERRUPTS, tupleDesc::constr, 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(), ExecCloseIndices(), ExecConstraints(), ExecDropSingleTupleTableSlot(), ExecFindPartition(), ExecInitExtraTupleSlot(), ExecInsertIndexTuples(), ExecIRInsertTriggers(), ExecMaterializeSlot(), ExecOpenIndices(), ExecResetTupleTable(), ExecSetSlotDescriptor(), ExecStoreTuple(), FreeBulkInsertState(), FreeExecutorState(), CopyStateData::freeze, GetBulkInsertState(), GetCurrentCommandId(), GetCurrentSubTransactionId(), GetPerTupleExprContext, GetPerTupleMemoryContext, 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, MAX_BUFFERED_TUPLES, MemoryContextSwitchTo(), tupleDesc::natts, NextCopyFrom(), NIL, NoLock, NULL, CopyStateData::num_dispatch, CopyStateData::num_partitions, palloc(), 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, ThereAreNoPriorRegisteredSnapshots(), ThereAreNoReadyPortals(), 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().

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  ExprContext *econtext;
2307  TupleTableSlot *myslot;
2308  MemoryContext oldcontext = CurrentMemoryContext;
2309 
2310  ErrorContextCallback errcallback;
2311  CommandId mycid = GetCurrentCommandId(true);
2312  int hi_options = 0; /* start with default heap_insert options */
2313  BulkInsertState bistate;
2314  uint64 processed = 0;
2315  bool useHeapMultiInsert;
2316  int nBufferedTuples = 0;
2317  int prev_leaf_part_index = -1;
2318 
2319 #define MAX_BUFFERED_TUPLES 1000
2320  HeapTuple *bufferedTuples = NULL; /* initialize to silence warning */
2321  Size bufferedTuplesSize = 0;
2322  int firstBufferedLineNo = 0;
2323 
2324  Assert(cstate->rel);
2325 
2326  /*
2327  * The target must be a plain relation or have an INSTEAD OF INSERT row
2328  * trigger. (Currently, such triggers are only allowed on views, so we
2329  * only hint about them in the view case.)
2330  */
2331  if (cstate->rel->rd_rel->relkind != RELKIND_RELATION &&
2332  cstate->rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE &&
2333  !(cstate->rel->trigdesc &&
2335  {
2336  if (cstate->rel->rd_rel->relkind == RELKIND_VIEW)
2337  ereport(ERROR,
2338  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2339  errmsg("cannot copy to view \"%s\"",
2340  RelationGetRelationName(cstate->rel)),
2341  errhint("To enable copying to a view, provide an INSTEAD OF INSERT trigger.")));
2342  else if (cstate->rel->rd_rel->relkind == RELKIND_MATVIEW)
2343  ereport(ERROR,
2344  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2345  errmsg("cannot copy to materialized view \"%s\"",
2346  RelationGetRelationName(cstate->rel))));
2347  else if (cstate->rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
2348  ereport(ERROR,
2349  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2350  errmsg("cannot copy to foreign table \"%s\"",
2351  RelationGetRelationName(cstate->rel))));
2352  else if (cstate->rel->rd_rel->relkind == RELKIND_SEQUENCE)
2353  ereport(ERROR,
2354  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2355  errmsg("cannot copy to sequence \"%s\"",
2356  RelationGetRelationName(cstate->rel))));
2357  else
2358  ereport(ERROR,
2359  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2360  errmsg("cannot copy to non-table relation \"%s\"",
2361  RelationGetRelationName(cstate->rel))));
2362  }
2363 
2364  tupDesc = RelationGetDescr(cstate->rel);
2365 
2366  /*----------
2367  * Check to see if we can avoid writing WAL
2368  *
2369  * If archive logging/streaming is not enabled *and* either
2370  * - table was created in same transaction as this COPY
2371  * - data is being written to relfilenode created in this transaction
2372  * then we can skip writing WAL. It's safe because if the transaction
2373  * doesn't commit, we'll discard the table (or the new relfilenode file).
2374  * If it does commit, we'll have done the heap_sync at the bottom of this
2375  * routine first.
2376  *
2377  * As mentioned in comments in utils/rel.h, the in-same-transaction test
2378  * is not always set correctly, since in rare cases rd_newRelfilenodeSubid
2379  * can be cleared before the end of the transaction. The exact case is
2380  * when a relation sets a new relfilenode twice in same transaction, yet
2381  * the second one fails in an aborted subtransaction, e.g.
2382  *
2383  * BEGIN;
2384  * TRUNCATE t;
2385  * SAVEPOINT save;
2386  * TRUNCATE t;
2387  * ROLLBACK TO save;
2388  * COPY ...
2389  *
2390  * Also, if the target file is new-in-transaction, we assume that checking
2391  * FSM for free space is a waste of time, even if we must use WAL because
2392  * of archiving. This could possibly be wrong, but it's unlikely.
2393  *
2394  * The comments for heap_insert and RelationGetBufferForTuple specify that
2395  * skipping WAL logging is only safe if we ensure that our tuples do not
2396  * go into pages containing tuples from any other transactions --- but this
2397  * must be the case if we have a new table or new relfilenode, so we need
2398  * no additional work to enforce that.
2399  *----------
2400  */
2401  /* createSubid is creation check, newRelfilenodeSubid is truncation check */
2402  if (cstate->rel->rd_createSubid != InvalidSubTransactionId ||
2404  {
2405  hi_options |= HEAP_INSERT_SKIP_FSM;
2406  if (!XLogIsNeeded())
2407  hi_options |= HEAP_INSERT_SKIP_WAL;
2408  }
2409 
2410  /*
2411  * Optimize if new relfilenode was created in this subxact or one of its
2412  * committed children and we won't see those rows later as part of an
2413  * earlier scan or command. This ensures that if this subtransaction
2414  * aborts then the frozen rows won't be visible after xact cleanup. Note
2415  * that the stronger test of exactly which subtransaction created it is
2416  * crucial for correctness of this optimization.
2417  */
2418  if (cstate->freeze)
2419  {
2421  ereport(ERROR,
2422  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
2423  errmsg("cannot perform FREEZE because of prior transaction activity")));
2424 
2425  if (cstate->rel->rd_createSubid != GetCurrentSubTransactionId() &&
2427  ereport(ERROR,
2428  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2429  errmsg("cannot perform FREEZE because the table was not created or truncated in the current subtransaction")));
2430 
2431  hi_options |= HEAP_INSERT_FROZEN;
2432  }
2433 
2434  /*
2435  * We need a ResultRelInfo so we can use the regular executor's
2436  * index-entry-making machinery. (There used to be a huge amount of code
2437  * here that basically duplicated execUtils.c ...)
2438  */
2439  resultRelInfo = makeNode(ResultRelInfo);
2440  InitResultRelInfo(resultRelInfo,
2441  cstate->rel,
2442  1, /* dummy rangetable index */
2443  NULL,
2444  0);
2445 
2446  ExecOpenIndices(resultRelInfo, false);
2447 
2448  estate->es_result_relations = resultRelInfo;
2449  estate->es_num_result_relations = 1;
2450  estate->es_result_relation_info = resultRelInfo;
2451  estate->es_range_table = cstate->range_table;
2452 
2453  /* Set up a tuple slot too */
2454  myslot = ExecInitExtraTupleSlot(estate);
2455  ExecSetSlotDescriptor(myslot, tupDesc);
2456  /* Triggers might need a slot as well */
2457  estate->es_trig_tuple_slot = ExecInitExtraTupleSlot(estate);
2458 
2459  /*
2460  * It's more efficient to prepare a bunch of tuples for insertion, and
2461  * insert them in one heap_multi_insert() call, than call heap_insert()
2462  * separately for every tuple. However, we can't do that if there are
2463  * BEFORE/INSTEAD OF triggers, or we need to evaluate volatile default
2464  * expressions. Such triggers or expressions might query the table we're
2465  * inserting to, and act differently if the tuples that have already been
2466  * processed and prepared for insertion are not there. We also can't do
2467  * it if the table is partitioned.
2468  */
2469  if ((resultRelInfo->ri_TrigDesc != NULL &&
2470  (resultRelInfo->ri_TrigDesc->trig_insert_before_row ||
2471  resultRelInfo->ri_TrigDesc->trig_insert_instead_row)) ||
2472  cstate->partition_dispatch_info != NULL ||
2473  cstate->volatile_defexprs)
2474  {
2475  useHeapMultiInsert = false;
2476  }
2477  else
2478  {
2479  useHeapMultiInsert = true;
2480  bufferedTuples = palloc(MAX_BUFFERED_TUPLES * sizeof(HeapTuple));
2481  }
2482 
2483  /* Prepare to catch AFTER triggers. */
2485 
2486  /*
2487  * Check BEFORE STATEMENT insertion triggers. It's debatable whether we
2488  * should do this for COPY, since it's not really an "INSERT" statement as
2489  * such. However, executing these triggers maintains consistency with the
2490  * EACH ROW triggers that we already fire on COPY.
2491  */
2492  ExecBSInsertTriggers(estate, resultRelInfo);
2493 
2494  values = (Datum *) palloc(tupDesc->natts * sizeof(Datum));
2495  nulls = (bool *) palloc(tupDesc->natts * sizeof(bool));
2496 
2497  bistate = GetBulkInsertState();
2498  econtext = GetPerTupleExprContext(estate);
2499 
2500  /* Set up callback to identify error line number */
2501  errcallback.callback = CopyFromErrorCallback;
2502  errcallback.arg = (void *) cstate;
2503  errcallback.previous = error_context_stack;
2504  error_context_stack = &errcallback;
2505 
2506  for (;;)
2507  {
2508  TupleTableSlot *slot,
2509  *oldslot;
2510  bool skip_tuple;
2511  Oid loaded_oid = InvalidOid;
2512 
2514 
2515  if (nBufferedTuples == 0)
2516  {
2517  /*
2518  * Reset the per-tuple exprcontext. We can only do this if the
2519  * tuple buffer is empty. (Calling the context the per-tuple
2520  * memory context is a bit of a misnomer now.)
2521  */
2522  ResetPerTupleExprContext(estate);
2523  }
2524 
2525  /* Switch into its memory context */
2527 
2528  if (!NextCopyFrom(cstate, econtext, values, nulls, &loaded_oid))
2529  break;
2530 
2531  /* And now we can form the input tuple. */
2532  tuple = heap_form_tuple(tupDesc, values, nulls);
2533 
2534  if (loaded_oid != InvalidOid)
2535  HeapTupleSetOid(tuple, loaded_oid);
2536 
2537  /*
2538  * Constraints might reference the tableoid column, so initialize
2539  * t_tableOid before evaluating them.
2540  */
2541  tuple->t_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
2542 
2543  /* Triggers and stuff need to be invoked in query context. */
2544  MemoryContextSwitchTo(oldcontext);
2545 
2546  /* Place tuple in tuple slot --- but slot shouldn't free it */
2547  slot = myslot;
2548  ExecStoreTuple(tuple, slot, InvalidBuffer, false);
2549 
2550  /* Determine the partition to heap_insert the tuple into */
2551  oldslot = slot;
2552  if (cstate->partition_dispatch_info)
2553  {
2554  int leaf_part_index;
2555  TupleConversionMap *map;
2556 
2557  /*
2558  * Away we go ... If we end up not finding a partition after all,
2559  * ExecFindPartition() does not return and errors out instead.
2560  * Otherwise, the returned value is to be used as an index into
2561  * arrays mt_partitions[] and mt_partition_tupconv_maps[] that
2562  * will get us the ResultRelInfo and TupleConversionMap for the
2563  * partition, respectively.
2564  */
2565  leaf_part_index = ExecFindPartition(resultRelInfo,
2566  cstate->partition_dispatch_info,
2567  slot,
2568  estate);
2569  Assert(leaf_part_index >= 0 &&
2570  leaf_part_index < cstate->num_partitions);
2571 
2572  /*
2573  * If this tuple is mapped to a partition that is not same as the
2574  * previous one, we'd better make the bulk insert mechanism gets a
2575  * new buffer.
2576  */
2577  if (prev_leaf_part_index != leaf_part_index)
2578  {
2579  ReleaseBulkInsertStatePin(bistate);
2580  prev_leaf_part_index = leaf_part_index;
2581  }
2582 
2583  /*
2584  * Save the old ResultRelInfo and switch to the one corresponding
2585  * to the selected partition.
2586  */
2587  saved_resultRelInfo = resultRelInfo;
2588  resultRelInfo = cstate->partitions + leaf_part_index;
2589 
2590  /* We do not yet have a way to insert into a foreign partition */
2591  if (resultRelInfo->ri_FdwRoutine)
2592  ereport(ERROR,
2593  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2594  errmsg("cannot route inserted tuples to a foreign table")));
2595 
2596  /*
2597  * For ExecInsertIndexTuples() to work on the partition's indexes
2598  */
2599  estate->es_result_relation_info = resultRelInfo;
2600 
2601  /*
2602  * We might need to convert from the parent rowtype to the
2603  * partition rowtype.
2604  */
2605  map = cstate->partition_tupconv_maps[leaf_part_index];
2606  if (map)
2607  {
2608  Relation partrel = resultRelInfo->ri_RelationDesc;
2609 
2610  tuple = do_convert_tuple(tuple, map);
2611 
2612  /*
2613  * We must use the partition's tuple descriptor from this
2614  * point on. Use a dedicated slot from this point on until
2615  * we're finished dealing with the partition.
2616  */
2617  slot = cstate->partition_tuple_slot;
2618  Assert(slot != NULL);
2619  ExecSetSlotDescriptor(slot, RelationGetDescr(partrel));
2620  ExecStoreTuple(tuple, slot, InvalidBuffer, true);
2621  }
2622 
2623  tuple->t_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
2624  }
2625 
2626  skip_tuple = false;
2627 
2628  /* BEFORE ROW INSERT Triggers */
2629  if (resultRelInfo->ri_TrigDesc &&
2630  resultRelInfo->ri_TrigDesc->trig_insert_before_row)
2631  {
2632  slot = ExecBRInsertTriggers(estate, resultRelInfo, slot);
2633 
2634  if (slot == NULL) /* "do nothing" */
2635  skip_tuple = true;
2636  else /* trigger might have changed tuple */
2637  tuple = ExecMaterializeSlot(slot);
2638  }
2639 
2640  if (!skip_tuple)
2641  {
2642  if (resultRelInfo->ri_TrigDesc &&
2643  resultRelInfo->ri_TrigDesc->trig_insert_instead_row)
2644  {
2645  /* Pass the data to the INSTEAD ROW INSERT trigger */
2646  ExecIRInsertTriggers(estate, resultRelInfo, slot);
2647  }
2648  else
2649  {
2650  /* Check the constraints of the tuple */
2651  if (cstate->rel->rd_att->constr ||
2652  resultRelInfo->ri_PartitionCheck)
2653  ExecConstraints(resultRelInfo, slot, oldslot, estate);
2654 
2655  if (useHeapMultiInsert)
2656  {
2657  /* Add this tuple to the tuple buffer */
2658  if (nBufferedTuples == 0)
2659  firstBufferedLineNo = cstate->cur_lineno;
2660  bufferedTuples[nBufferedTuples++] = tuple;
2661  bufferedTuplesSize += tuple->t_len;
2662 
2663  /*
2664  * If the buffer filled up, flush it. Also flush if the
2665  * total size of all the tuples in the buffer becomes
2666  * large, to avoid using large amounts of memory for the
2667  * buffer when the tuples are exceptionally wide.
2668  */
2669  if (nBufferedTuples == MAX_BUFFERED_TUPLES ||
2670  bufferedTuplesSize > 65535)
2671  {
2672  CopyFromInsertBatch(cstate, estate, mycid, hi_options,
2673  resultRelInfo, myslot, bistate,
2674  nBufferedTuples, bufferedTuples,
2675  firstBufferedLineNo);
2676  nBufferedTuples = 0;
2677  bufferedTuplesSize = 0;
2678  }
2679  }
2680  else
2681  {
2682  List *recheckIndexes = NIL;
2683 
2684  /* OK, store the tuple and create index entries for it */
2685  heap_insert(resultRelInfo->ri_RelationDesc, tuple, mycid,
2686  hi_options, bistate);
2687 
2688  if (resultRelInfo->ri_NumIndices > 0)
2689  recheckIndexes = ExecInsertIndexTuples(slot,
2690  &(tuple->t_self),
2691  estate,
2692  false,
2693  NULL,
2694  NIL);
2695 
2696  /* AFTER ROW INSERT Triggers */
2697  ExecARInsertTriggers(estate, resultRelInfo, tuple,
2698  recheckIndexes);
2699 
2700  list_free(recheckIndexes);
2701  }
2702  }
2703 
2704  /*
2705  * We count only tuples not suppressed by a BEFORE INSERT trigger;
2706  * this is the same definition used by execMain.c for counting
2707  * tuples inserted by an INSERT command.
2708  */
2709  processed++;
2710 
2711  if (saved_resultRelInfo)
2712  {
2713  resultRelInfo = saved_resultRelInfo;
2714  estate->es_result_relation_info = resultRelInfo;
2715  }
2716  }
2717  }
2718 
2719  /* Flush any remaining buffered tuples */
2720  if (nBufferedTuples > 0)
2721  CopyFromInsertBatch(cstate, estate, mycid, hi_options,
2722  resultRelInfo, myslot, bistate,
2723  nBufferedTuples, bufferedTuples,
2724  firstBufferedLineNo);
2725 
2726  /* Done, clean up */
2727  error_context_stack = errcallback.previous;
2728 
2729  FreeBulkInsertState(bistate);
2730 
2731  MemoryContextSwitchTo(oldcontext);
2732 
2733  /*
2734  * In the old protocol, tell pqcomm that we can process normal protocol
2735  * messages again.
2736  */
2737  if (cstate->copy_dest == COPY_OLD_FE)
2738  pq_endmsgread();
2739 
2740  /* Execute AFTER STATEMENT insertion triggers */
2741  ExecASInsertTriggers(estate, resultRelInfo);
2742 
2743  /* Handle queued AFTER triggers */
2744  AfterTriggerEndQuery(estate);
2745 
2746  pfree(values);
2747  pfree(nulls);
2748 
2749  ExecResetTupleTable(estate->es_tupleTable, false);
2750 
2751  ExecCloseIndices(resultRelInfo);
2752 
2753  /* Close all the partitioned tables, leaf partitions, and their indices */
2754  if (cstate->partition_dispatch_info)
2755  {
2756  int i;
2757 
2758  /*
2759  * Remember cstate->partition_dispatch_info[0] corresponds to the root
2760  * partitioned table, which we must not try to close, because it is
2761  * the main target table of COPY that will be closed eventually by
2762  * DoCopy(). Also, tupslot is NULL for the root partitioned table.
2763  */
2764  for (i = 1; i < cstate->num_dispatch; i++)
2765  {
2767 
2768  heap_close(pd->reldesc, NoLock);
2770  }
2771  for (i = 0; i < cstate->num_partitions; i++)
2772  {
2773  ResultRelInfo *resultRelInfo = cstate->partitions + i;
2774 
2775  ExecCloseIndices(resultRelInfo);
2776  heap_close(resultRelInfo->ri_RelationDesc, NoLock);
2777  }
2778 
2779  /* Release the standalone partition tuple descriptor */
2781  }
2782 
2783  FreeExecutorState(estate);
2784 
2785  /*
2786  * If we skipped writing WAL, then we need to sync the heap (but not
2787  * indexes since those use WAL anyway)
2788  */
2789  if (hi_options & HEAP_INSERT_SKIP_WAL)
2790  heap_sync(cstate->rel);
2791 
2792  return processed;
2793 }
int ri_NumIndices
Definition: execnodes.h:338
#define NIL
Definition: pg_list.h:69
uint32 CommandId
Definition: c.h:411
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:1255
Relation ri_RelationDesc
Definition: execnodes.h:337
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:2192
List * ExecInsertIndexTuples(TupleTableSlot *slot, ItemPointer tupleid, EState *estate, bool noDupErr, bool *specConflict, List *arbiterIndexes)
Definition: execIndexing.c:271
#define ResetPerTupleExprContext(estate)
Definition: executor.h:348
#define RelationGetDescr(relation)
Definition: rel.h:425
#define HEAP_INSERT_FROZEN
Definition: heapam.h:30
ResultRelInfo * partitions
Definition: copy.c:170
#define XLogIsNeeded()
Definition: xlog.h:145
#define MAX_BUFFERED_TUPLES
TupleTableSlot * ExecIRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2251
#define RELKIND_MATVIEW
Definition: pg_class.h:165
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define InvalidBuffer
Definition: buf.h:25
bool ThereAreNoPriorRegisteredSnapshots(void)
Definition: snapmgr.c:1603
int errcode(int sqlerrcode)
Definition: elog.c:575
SubTransactionId rd_newRelfilenodeSubid
Definition: rel.h:110
void heap_sync(Relation rel)
Definition: heapam.c:9138
#define HEAP_INSERT_SKIP_WAL
Definition: heapam.h:28
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:692
#define heap_close(r, l)
Definition: heapam.h:97
List * es_range_table
Definition: execnodes.h:374
Form_pg_class rd_rel
Definition: rel.h:113
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
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:169
Relation rel
Definition: copy.c:108
#define GetPerTupleExprContext(estate)
Definition: executor.h:339
BulkInsertState GetBulkInsertState(void)
Definition: heapam.c:2322
void pfree(void *pointer)
Definition: mcxt.c:950
bool ThereAreNoReadyPortals(void)
Definition: portalmem.c:1127
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:2801
ItemPointerData t_self
Definition: htup.h:65
TriggerDesc * trigdesc
Definition: rel.h:119
int num_dispatch
Definition: copy.c:168
uint32 t_len
Definition: htup.h:64
void ExecBSInsertTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2111
#define NoLock
Definition: lockdefs.h:34
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:216
ResultRelInfo * es_result_relations
Definition: execnodes.h:384
#define RelationGetRelationName(relation)
Definition: rel.h:433
struct FdwRoutine * ri_FdwRoutine
Definition: execnodes.h:345
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:390
#define ereport(elevel, rest)
Definition: elog.h:122
Oid heap_insert(Relation relation, HeapTuple tup, CommandId cid, int options, BulkInsertState bistate)
Definition: heapam.c:2399
TriggerDesc * ri_TrigDesc
Definition: execnodes.h:341
EState * CreateExecutorState(void)
Definition: execUtils.c:73
SubTransactionId rd_createSubid
Definition: rel.h:109
#define RELKIND_PARTITIONED_TABLE
Definition: pg_class.h:168
bool trig_insert_before_row
Definition: reltrigger.h:55
List * es_tupleTable
Definition: execnodes.h:401
void ExecResetTupleTable(List *tupleTable, bool shouldFree)
Definition: execTuples.c:156
uintptr_t Datum
Definition: postgres.h:372
TupleTableSlot * tupslot
Definition: partition.h:66
void ExecSetSlotDescriptor(TupleTableSlot *slot, TupleDesc tupdesc)
Definition: execTuples.c:247
void ExecASInsertTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2163
int ExecFindPartition(ResultRelInfo *resultRelInfo, PartitionDispatch *pd, TupleTableSlot *slot, EState *estate)
Definition: execMain.c:3227
int es_num_result_relations
Definition: execnodes.h:385
List * ri_PartitionCheck
Definition: execnodes.h:355
TupleDesc rd_att
Definition: rel.h:114
bool freeze
Definition: copy.c:116
void pq_endmsgread(void)
Definition: pqcomm.c:1215
#define InvalidOid
Definition: postgres_ext.h:36
void ExecConstraints(ResultRelInfo *resultRelInfo, TupleTableSlot *slot, TupleTableSlot *orig_slot, EState *estate)
Definition: execMain.c:1828
void AfterTriggerBeginQuery(void)
Definition: trigger.c:4150
#define makeNode(_type_)
Definition: nodes.h:568
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
void FreeBulkInsertState(BulkInsertState bistate)
Definition: heapam.c:2336
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:649
TupleConstr * constr
Definition: tupdesc.h:76
size_t Size
Definition: c.h:356
#define InvalidSubTransactionId
Definition: c.h:403
HeapTuple ExecMaterializeSlot(TupleTableSlot *slot)
Definition: execTuples.c:725
void ReleaseBulkInsertStatePin(BulkInsertState bistate)
Definition: heapam.c:2348
void ExecARInsertTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple, List *recheckIndexes)
Definition: trigger.c:2239
HeapTuple do_convert_tuple(HeapTuple tuple, TupleConversionMap *map)
Definition: tupconvert.c:341
#define GetPerTupleMemoryContext(estate)
Definition: executor.h:344
#define HEAP_INSERT_SKIP_FSM
Definition: heapam.h:29
static Datum values[MAXATTR]
Definition: bootstrap.c:162
void AfterTriggerEndQuery(EState *estate)
Definition: trigger.c:4170
void(* callback)(void *arg)
Definition: elog.h:239
void * palloc(Size size)
Definition: mcxt.c:849
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:3191
int i
int cur_lineno
Definition: copy.c:138
PartitionDispatch * partition_dispatch_info
Definition: copy.c:167
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
TupleTableSlot * ExecBRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2173
CommandId GetCurrentCommandId(bool used)
Definition: xact.c:687
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:413
void ExecCloseIndices(ResultRelInfo *resultRelInfo)
Definition: execIndexing.c:224
int num_partitions
Definition: copy.c:169
ResultRelInfo * es_result_relation_info
Definition: execnodes.h:386
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:114
bool need_transcoding
Definition: copy.c:104
StringInfoData line_buf
Definition: copy.c:196
bool line_buf_valid
Definition: copy.c:198
bool line_buf_converted
Definition: copy.c:197
const char * cur_attname
Definition: copy.c:139
void pfree(void *pointer)
Definition: mcxt.c:950
static char * limit_printout_length(const char *str)
Definition: copy.c:2268
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 4742 of file copy.c.

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

Referenced by CreateDestReceiver().

4743 {
4744  DR_copy *self = (DR_copy *) palloc(sizeof(DR_copy));
4745 
4746  self->pub.receiveSlot = copy_dest_receive;
4747  self->pub.rStartup = copy_dest_startup;
4748  self->pub.rShutdown = copy_dest_shutdown;
4749  self->pub.rDestroy = copy_dest_destroy;
4750  self->pub.mydest = DestCopyOut;
4751 
4752  self->cstate = NULL; /* will be set later */
4753  self->processed = 0;
4754 
4755  return (DestReceiver *) self;
4756 }
Definition: copy.c:214
static void copy_dest_destroy(DestReceiver *self)
Definition: copy.c:4733
static void copy_dest_shutdown(DestReceiver *self)
Definition: copy.c:4724
static void copy_dest_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: copy.c:4696
static bool copy_dest_receive(TupleTableSlot *slot, DestReceiver *self)
Definition: copy.c:4705
#define NULL
Definition: c.h:229
void * palloc(Size size)
Definition: mcxt.c:849
void DoCopy ( ParseState state,
const CopyStmt stmt,
int  stmt_location,
int  stmt_len,
uint64 *  processed 
)

Definition at line 780 of file copy.c.

References AccessShareLock, ACL_INSERT, ACL_SELECT, 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, NULL, CopyStmt::options, PreventCommandIfParallelMode(), PreventCommandIfReadOnly(), pstrdup(), CopyStmt::query, CopyStateData::range_table, RelationData::rd_islocaltemp, RelationData::rd_rel, CopyStmt::relation, RelationGetDescr, RelationGetNamespace, RelationGetRelationName, RelationGetRelid, RangeTblEntry::relid, RangeTblEntry::relkind, RangeTblEntry::requiredPerms, RLS_ENABLED, RowExclusiveLock, RTE_RELATION, RangeTblEntry::rtekind, select, RangeTblEntry::selectedCols, RawStmt::stmt, RawStmt::stmt_len, RawStmt::stmt_location, superuser(), SelectStmt::targetList, ResTarget::val, and XactReadOnly.

Referenced by standard_ProcessUtility().

783 {
784  CopyState cstate;
785  bool is_from = stmt->is_from;
786  bool pipe = (stmt->filename == NULL);
787  Relation rel;
788  Oid relid;
789  RawStmt *query = NULL;
790  List *range_table = NIL;
791 
792  /* Disallow COPY to/from file or program except to superusers. */
793  if (!pipe && !superuser())
794  {
795  if (stmt->is_program)
796  ereport(ERROR,
797  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
798  errmsg("must be superuser to COPY to or from an external program"),
799  errhint("Anyone can COPY to stdout or from stdin. "
800  "psql's \\copy command also works for anyone.")));
801  else
802  ereport(ERROR,
803  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
804  errmsg("must be superuser to COPY to or from a file"),
805  errhint("Anyone can COPY to stdout or from stdin. "
806  "psql's \\copy command also works for anyone.")));
807  }
808 
809  if (stmt->relation)
810  {
811  TupleDesc tupDesc;
812  AclMode required_access = (is_from ? ACL_INSERT : ACL_SELECT);
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 = makeNode(RangeTblEntry);
826  rte->rtekind = RTE_RELATION;
827  rte->relid = RelationGetRelid(rel);
828  rte->relkind = rel->rd_rel->relkind;
829  rte->requiredPerms = required_access;
830  range_table = list_make1(rte);
831 
832  tupDesc = RelationGetDescr(rel);
833  attnums = CopyGetAttnums(tupDesc, rel, stmt->attlist);
834  foreach(cur, attnums)
835  {
836  int attno = lfirst_int(cur) -
838 
839  if (is_from)
840  rte->insertedCols = bms_add_member(rte->insertedCols, attno);
841  else
842  rte->selectedCols = bms_add_member(rte->selectedCols, attno);
843  }
844  ExecCheckRTPerms(range_table, true);
845 
846  /*
847  * Permission check for row security policies.
848  *
849  * check_enable_rls will ereport(ERROR) if the user has requested
850  * something invalid and will otherwise indicate if we should enable
851  * RLS (returns RLS_ENABLED) or not for this COPY statement.
852  *
853  * If the relation has a row security policy and we are to apply it
854  * then perform a "query" copy and allow the normal query processing
855  * to handle the policies.
856  *
857  * If RLS is not enabled for this, then just fall through to the
858  * normal non-filtering relation handling.
859  */
860  if (check_enable_rls(rte->relid, InvalidOid, false) == RLS_ENABLED)
861  {
863  ColumnRef *cr;
864  ResTarget *target;
865  RangeVar *from;
866  List *targetList = NIL;
867 
868  if (is_from)
869  ereport(ERROR,
870  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
871  errmsg("COPY FROM not supported with row-level security"),
872  errhint("Use INSERT statements instead.")));
873 
874  /*
875  * Build target list
876  *
877  * If no columns are specified in the attribute list of the COPY
878  * command, then the target list is 'all' columns. Therefore, '*'
879  * should be used as the target list for the resulting SELECT
880  * statement.
881  *
882  * In the case that columns are specified in the attribute list,
883  * create a ColumnRef and ResTarget for each column and add them
884  * to the target list for the resulting SELECT statement.
885  */
886  if (!stmt->attlist)
887  {
888  cr = makeNode(ColumnRef);
890  cr->location = -1;
891 
892  target = makeNode(ResTarget);
893  target->name = NULL;
894  target->indirection = NIL;
895  target->val = (Node *) cr;
896  target->location = -1;
897 
898  targetList = list_make1(target);
899  }
900  else
901  {
902  ListCell *lc;
903 
904  foreach(lc, stmt->attlist)
905  {
906  /*
907  * Build the ColumnRef for each column. The ColumnRef
908  * 'fields' property is a String 'Value' node (see
909  * nodes/value.h) that corresponds to the column name
910  * respectively.
911  */
912  cr = makeNode(ColumnRef);
913  cr->fields = list_make1(lfirst(lc));
914  cr->location = -1;
915 
916  /* Build the ResTarget and add the ColumnRef to it. */
917  target = makeNode(ResTarget);
918  target->name = NULL;
919  target->indirection = NIL;
920  target->val = (Node *) cr;
921  target->location = -1;
922 
923  /* Add each column to the SELECT statement's target list */
924  targetList = lappend(targetList, target);
925  }
926  }
927 
928  /*
929  * Build RangeVar for from clause, fully qualified based on the
930  * relation which we have opened and locked.
931  */
934  -1);
935 
936  /* Build query */
937  select = makeNode(SelectStmt);
938  select->targetList = targetList;
939  select->fromClause = list_make1(from);
940 
941  query = makeNode(RawStmt);
942  query->stmt = (Node *) select;
943  query->stmt_location = stmt_location;
944  query->stmt_len = stmt_len;
945 
946  /*
947  * Close the relation for now, but keep the lock on it to prevent
948  * changes between now and when we start the query-based COPY.
949  *
950  * We'll reopen it later as part of the query-based COPY.
951  */
952  heap_close(rel, NoLock);
953  rel = NULL;
954  }
955  }
956  else
957  {
958  Assert(stmt->query);
959 
960  query = makeNode(RawStmt);
961  query->stmt = stmt->query;
962  query->stmt_location = stmt_location;
963  query->stmt_len = stmt_len;
964 
965  relid = InvalidOid;
966  rel = NULL;
967  }
968 
969  if (is_from)
970  {
971  Assert(rel);
972 
973  /* check read-only transaction and parallel mode */
974  if (XactReadOnly && !rel->rd_islocaltemp)
975  PreventCommandIfReadOnly("COPY FROM");
976  PreventCommandIfParallelMode("COPY FROM");
977 
978  cstate = BeginCopyFrom(pstate, rel, stmt->filename, stmt->is_program,
979  NULL, stmt->attlist, stmt->options);
980  cstate->range_table = range_table;
981  *processed = CopyFrom(cstate); /* copy from file to database */
982  EndCopyFrom(cstate);
983  }
984  else
985  {
986  cstate = BeginCopyTo(pstate, rel, query, relid,
987  stmt->filename, stmt->is_program,
988  stmt->attlist, stmt->options);
989  *processed = DoCopyTo(cstate); /* copy from database to file */
990  EndCopyTo(cstate);
991  }
992 
993  /*
994  * Close the relation. If reading, we can release the AccessShareLock we
995  * got; if writing, we should hold the lock until end of transaction to
996  * ensure that updates will be committed before lock is released.
997  */
998  if (rel != NULL)
999  heap_close(rel, (is_from ? NoLock : AccessShareLock));
1000 }
List * indirection
Definition: parsenodes.h:432
#define NIL
Definition: pg_list.h:69
List * range_table
Definition: copy.c:165
Node * val
Definition: parsenodes.h:433
int errhint(const char *fmt,...)
Definition: elog.c:987
void PreventCommandIfParallelMode(const char *cmdname)
Definition: utility.c:253
List * attlist
Definition: parsenodes.h:1889
List * fromClause
Definition: parsenodes.h:1465
#define RelationGetDescr(relation)
Definition: rel.h:425
char * name
Definition: parsenodes.h:431
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:2884
char * pstrdup(const char *in)
Definition: mcxt.c:1077
bool rd_islocaltemp
Definition: rel.h:90
#define AccessShareLock
Definition: lockdefs.h:36
Definition: nodes.h:520
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:2297
#define select(n, r, w, e, timeout)
Definition: win32.h:384
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
AclMode requiredPerms
Definition: parsenodes.h:1004
#define heap_close(r, l)
Definition: heapam.h:97
Form_pg_class rd_rel
Definition: rel.h:113
unsigned int Oid
Definition: postgres_ext.h:31
bool is_program
Definition: parsenodes.h:1892
int location
Definition: parsenodes.h:227
int location
Definition: parsenodes.h:434
#define list_make1(x1)
Definition: pg_list.h:133
uint32 AclMode
Definition: parsenodes.h:63
Bitmapset * selectedCols
Definition: parsenodes.h:1006
static CopyState BeginCopyTo(ParseState *pstate, Relation rel, RawStmt *query, Oid queryRelId, const char *filename, bool is_program, List *attnamelist, List *options)
Definition: copy.c:1762
#define ERROR
Definition: elog.h:43
#define lfirst_int(lc)
Definition: pg_list.h:107
RangeVar * relation
Definition: parsenodes.h:1886
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3006
Node * stmt
Definition: parsenodes.h:1376
#define NoLock
Definition: lockdefs.h:34
List * targetList
Definition: parsenodes.h:1464
static uint64 DoCopyTo(CopyState cstate)
Definition: copy.c:1891
#define RowExclusiveLock
Definition: lockdefs.h:38
List * options
Definition: parsenodes.h:1894
#define RelationGetRelationName(relation)
Definition: rel.h:433
#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:4624
#define ACL_SELECT
Definition: parsenodes.h:66
int stmt_len
Definition: parsenodes.h:1378
int stmt_location
Definition: parsenodes.h:1377
Relation heap_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: heapam.c:1315
#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:3432
#define makeNode(_type_)
Definition: nodes.h:568
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
#define ACL_INSERT
Definition: parsenodes.h:65
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:668
void PreventCommandIfReadOnly(const char *cmdname)
Definition: utility.c:235
Node * query
Definition: parsenodes.h:1887
RTEKind rtekind
Definition: parsenodes.h:916
bool is_from
Definition: parsenodes.h:1891
int errmsg(const char *fmt,...)
Definition: elog.c:797
Bitmapset * insertedCols
Definition: parsenodes.h:1007
bool ExecCheckRTPerms(List *rangeTable, bool ereport_on_violation)
Definition: execMain.c:562
char * filename
Definition: parsenodes.h:1893
static void EndCopyTo(CopyState cstate)
Definition: copy.c:1926
Definition: pg_list.h:45
#define RelationGetRelid(relation)
Definition: rel.h:413
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:419
List * fields
Definition: parsenodes.h:226
#define RelationGetNamespace(relation)
Definition: rel.h:440
void EndCopyFrom ( CopyState  cstate)

Definition at line 3432 of file copy.c.

References EndCopy().

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

3433 {
3434  /* No COPY FROM related resources except memory. */
3435 
3436  EndCopy(cstate);
3437 }
static void EndCopy(CopyState cstate)
Definition: copy.c:1739
bool NextCopyFrom ( CopyState  cstate,
ExprContext econtext,
Datum values,
bool nulls,
Oid tupleOid 
)

Definition at line 3191 of file copy.c.

References Assert, CopyStateData::attnumlist, tupleDesc::attrs, 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(), NULL, CopyStateData::null_print, CopyStateData::num_defaults, CopyStateData::oid_in_function, CopyStateData::oid_typioparam, oidin(), CopyStateData::oids, CopyStateData::rel, RelationGetDescr, and CopyStateData::typioparams.

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

3193 {
3194  TupleDesc tupDesc;
3195  Form_pg_attribute *attr;
3196  AttrNumber num_phys_attrs,
3197  attr_count,
3198  num_defaults = cstate->num_defaults;
3199  FmgrInfo *in_functions = cstate->in_functions;
3200  Oid *typioparams = cstate->typioparams;
3201  int i;
3202  int nfields;
3203  bool isnull;
3204  bool file_has_oids = cstate->file_has_oids;
3205  int *defmap = cstate->defmap;
3206  ExprState **defexprs = cstate->defexprs;
3207 
3208  tupDesc = RelationGetDescr(cstate->rel);
3209  attr = tupDesc->attrs;
3210  num_phys_attrs = tupDesc->natts;
3211  attr_count = list_length(cstate->attnumlist);
3212  nfields = file_has_oids ? (attr_count + 1) : attr_count;
3213 
3214  /* Initialize all values for row to NULL */
3215  MemSet(values, 0, num_phys_attrs * sizeof(Datum));
3216  MemSet(nulls, true, num_phys_attrs * sizeof(bool));
3217 
3218  if (!cstate->binary)
3219  {
3220  char **field_strings;
3221  ListCell *cur;
3222  int fldct;
3223  int fieldno;
3224  char *string;
3225 
3226  /* read raw fields in the next line */
3227  if (!NextCopyFromRawFields(cstate, &field_strings, &fldct))
3228  return false;
3229 
3230  /* check for overflowing fields */
3231  if (nfields > 0 && fldct > nfields)
3232  ereport(ERROR,
3233  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3234  errmsg("extra data after last expected column")));
3235 
3236  fieldno = 0;
3237 
3238  /* Read the OID field if present */
3239  if (file_has_oids)
3240  {
3241  if (fieldno >= fldct)
3242  ereport(ERROR,
3243  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3244  errmsg("missing data for OID column")));
3245  string = field_strings[fieldno++];
3246 
3247  if (string == NULL)
3248  ereport(ERROR,
3249  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3250  errmsg("null OID in COPY data")));
3251  else if (cstate->oids && tupleOid != NULL)
3252  {
3253  cstate->cur_attname = "oid";
3254  cstate->cur_attval = string;
3256  CStringGetDatum(string)));
3257  if (*tupleOid == InvalidOid)
3258  ereport(ERROR,
3259  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3260  errmsg("invalid OID in COPY data")));
3261  cstate->cur_attname = NULL;
3262  cstate->cur_attval = NULL;
3263  }
3264  }
3265 
3266  /* Loop to read the user attributes on the line. */
3267  foreach(cur, cstate->attnumlist)
3268  {
3269  int attnum = lfirst_int(cur);
3270  int m = attnum - 1;
3271 
3272  if (fieldno >= fldct)
3273  ereport(ERROR,
3274  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3275  errmsg("missing data for column \"%s\"",
3276  NameStr(attr[m]->attname))));
3277  string = field_strings[fieldno++];
3278 
3279  if (cstate->convert_select_flags &&
3280  !cstate->convert_select_flags[m])
3281  {
3282  /* ignore input field, leaving column as NULL */
3283  continue;
3284  }
3285 
3286  if (cstate->csv_mode)
3287  {
3288  if (string == NULL &&
3289  cstate->force_notnull_flags[m])
3290  {
3291  /*
3292  * FORCE_NOT_NULL option is set and column is NULL -
3293  * convert it to the NULL string.
3294  */
3295  string = cstate->null_print;
3296  }
3297  else if (string != NULL && cstate->force_null_flags[m]
3298  && strcmp(string, cstate->null_print) == 0)
3299  {
3300  /*
3301  * FORCE_NULL option is set and column matches the NULL
3302  * string. It must have been quoted, or otherwise the
3303  * string would already have been set to NULL. Convert it
3304  * to NULL as specified.
3305  */
3306  string = NULL;
3307  }
3308  }
3309 
3310  cstate->cur_attname = NameStr(attr[m]->attname);
3311  cstate->cur_attval = string;
3312  values[m] = InputFunctionCall(&in_functions[m],
3313  string,
3314  typioparams[m],
3315  attr[m]->atttypmod);
3316  if (string != NULL)
3317  nulls[m] = false;
3318  cstate->cur_attname = NULL;
3319  cstate->cur_attval = NULL;
3320  }
3321 
3322  Assert(fieldno == nfields);
3323  }
3324  else
3325  {
3326  /* binary */
3327  int16 fld_count;
3328  ListCell *cur;
3329 
3330  cstate->cur_lineno++;
3331 
3332  if (!CopyGetInt16(cstate, &fld_count))
3333  {
3334  /* EOF detected (end of file, or protocol-level EOF) */
3335  return false;
3336  }
3337 
3338  if (fld_count == -1)
3339  {
3340  /*
3341  * Received EOF marker. In a V3-protocol copy, wait for the
3342  * protocol-level EOF, and complain if it doesn't come
3343  * immediately. This ensures that we correctly handle CopyFail,
3344  * if client chooses to send that now.
3345  *
3346  * Note that we MUST NOT try to read more data in an old-protocol
3347  * copy, since there is no protocol-level EOF marker then. We
3348  * could go either way for copy from file, but choose to throw
3349  * error if there's data after the EOF marker, for consistency
3350  * with the new-protocol case.
3351  */
3352  char dummy;
3353 
3354  if (cstate->copy_dest != COPY_OLD_FE &&
3355  CopyGetData(cstate, &dummy, 1, 1) > 0)
3356  ereport(ERROR,
3357  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3358  errmsg("received copy data after EOF marker")));
3359  return false;
3360  }
3361 
3362  if (fld_count != attr_count)
3363  ereport(ERROR,
3364  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3365  errmsg("row field count is %d, expected %d",
3366  (int) fld_count, attr_count)));
3367 
3368  if (file_has_oids)
3369  {
3370  Oid loaded_oid;
3371 
3372  cstate->cur_attname = "oid";
3373  loaded_oid =
3375  0,
3376  &cstate->oid_in_function,
3377  cstate->oid_typioparam,
3378  -1,
3379  &isnull));
3380  if (isnull || loaded_oid == InvalidOid)
3381  ereport(ERROR,
3382  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3383  errmsg("invalid OID in COPY data")));
3384  cstate->cur_attname = NULL;
3385  if (cstate->oids && tupleOid != NULL)
3386  *tupleOid = loaded_oid;
3387  }
3388 
3389  i = 0;
3390  foreach(cur, cstate->attnumlist)
3391  {
3392  int attnum = lfirst_int(cur);
3393  int m = attnum - 1;
3394 
3395  cstate->cur_attname = NameStr(attr[m]->attname);
3396  i++;
3397  values[m] = CopyReadBinaryAttribute(cstate,
3398  i,
3399  &in_functions[m],
3400  typioparams[m],
3401  attr[m]->atttypmod,
3402  &nulls[m]);
3403  cstate->cur_attname = NULL;
3404  }
3405  }
3406 
3407  /*
3408  * Now compute and insert any defaults available for the columns not
3409  * provided by the input data. Anything not processed here or above will
3410  * remain NULL.
3411  */
3412  for (i = 0; i < num_defaults; i++)
3413  {
3414  /*
3415  * The caller must supply econtext and have switched into the
3416  * per-tuple memory context in it.
3417  */
3418  Assert(econtext != NULL);
3420 
3421  values[defmap[i]] = ExecEvalExpr(defexprs[i], econtext,
3422  &nulls[defmap[i]]);
3423  }
3424 
3425  return true;
3426 }
signed short int16
Definition: c.h:255
bool NextCopyFromRawFields(CopyState cstate, char ***fields, int *nfields)
Definition: copy.c:3139
static Datum CopyReadBinaryAttribute(CopyState cstate, int column_no, FmgrInfo *flinfo, Oid typioparam, int32 typmod, bool *isnull)
Definition: copy.c:4321
Definition: fmgr.h:53
bool csv_mode
Definition: copy.c:117
bool binary
Definition: copy.c:114
#define RelationGetDescr(relation)
Definition: rel.h:425
FmgrInfo * in_functions
Definition: copy.c:160
AttrNumber num_defaults
Definition: copy.c:156
List * attnumlist
Definition: copy.c:110
bool file_has_oids
Definition: copy.c:157
#define DatumGetObjectId(X)
Definition: postgres.h:506
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:136
Form_pg_attribute * attrs
Definition: tupdesc.h:74
struct cursor * cur
Definition: ecpg.c:28
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:857
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:576
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 ExecEvalExpr(expr, econtext, isNull)
Definition: executor.h:73
#define ERROR
Definition: elog.h:43
static int CopyGetData(CopyState cstate, void *databuf, int minread, int maxread)
Definition: copy.c:555
#define lfirst_int(lc)
Definition: pg_list.h:107
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:184
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:1932
#define InvalidOid
Definition: postgres_ext.h:36
bool * force_notnull_flags
Definition: copy.c:129
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static int list_length(const List *l)
Definition: pg_list.h:89
static Datum values[MAXATTR]
Definition: bootstrap.c:162
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define NameStr(name)
Definition: c.h:499
static bool CopyGetInt16(CopyState cstate, int16 *val)
Definition: copy.c:710
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 3139 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().

3140 {
3141  int fldct;
3142  bool done;
3143 
3144  /* only available for text or csv input */
3145  Assert(!cstate->binary);
3146 
3147  /* on input just throw the header line away */
3148  if (cstate->cur_lineno == 0 && cstate->header_line)
3149  {
3150  cstate->cur_lineno++;
3151  if (CopyReadLine(cstate))
3152  return false; /* done */
3153  }
3154 
3155  cstate->cur_lineno++;
3156 
3157  /* Actually read the line into memory here */
3158  done = CopyReadLine(cstate);
3159 
3160  /*
3161  * EOF at start of line means we're done. If we see EOF after some
3162  * characters, we act as though it was newline followed by EOF, ie,
3163  * process the line and then exit loop on next iteration.
3164  */
3165  if (done && cstate->line_buf.len == 0)
3166  return false;
3167 
3168  /* Parse the line into de-escaped field values */
3169  if (cstate->csv_mode)
3170  fldct = CopyReadAttributesCSV(cstate);
3171  else
3172  fldct = CopyReadAttributesText(cstate);
3173 
3174  *fields = cstate->raw_fields;
3175  *nfields = fldct;
3176  return true;
3177 }
bool csv_mode
Definition: copy.c:117
char ** raw_fields
Definition: copy.c:187
bool binary
Definition: copy.c:114
StringInfoData line_buf
Definition: copy.c:196
static int CopyReadAttributesCSV(CopyState cstate)
Definition: copy.c:4152
bool header_line
Definition: copy.c:118
#define Assert(condition)
Definition: c.h:675
static bool CopyReadLine(CopyState cstate)
Definition: copy.c:3448
int cur_lineno
Definition: copy.c:138
static int CopyReadAttributesText(CopyState cstate)
Definition: copy.c:3924
void ProcessCopyOptions ( ParseState pstate,
CopyState  cstate,
bool  is_from,
List options 
)

Definition at line 1020 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, DefElem::location, NIL, NULL, 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().

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