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
 

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, 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)
 
DestReceiverCreateCopyDestReceiver (void)
 

Typedef Documentation

Definition at line 23 of file copy.h.

Function Documentation

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

Definition at line 2876 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(), CopyStateData::copy_file, CopyStateData::copycontext, CopyGetData(), CopyGetInt32(), CopyStateData::cur_attname, CopyStateData::cur_attval, CopyStateData::cur_lineno, CopyStateData::cur_relname, 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 DoCopy(), file_acquire_sample_rows(), fileBeginForeignScan(), and fileReScanForeignScan().

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

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

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

Definition at line 4728 of file copy.c.

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

Referenced by CreateDestReceiver().

4729 {
4730  DR_copy *self = (DR_copy *) palloc(sizeof(DR_copy));
4731 
4732  self->pub.receiveSlot = copy_dest_receive;
4733  self->pub.rStartup = copy_dest_startup;
4734  self->pub.rShutdown = copy_dest_shutdown;
4735  self->pub.rDestroy = copy_dest_destroy;
4736  self->pub.mydest = DestCopyOut;
4737 
4738  self->cstate = NULL; /* will be set later */
4739  self->processed = 0;
4740 
4741  return (DestReceiver *) self;
4742 }
Definition: copy.c:212
static void copy_dest_destroy(DestReceiver *self)
Definition: copy.c:4719
static void copy_dest_shutdown(DestReceiver *self)
Definition: copy.c:4710
static void copy_dest_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: copy.c:4682
static bool copy_dest_receive(TupleTableSlot *slot, DestReceiver *self)
Definition: copy.c:4691
#define NULL
Definition: c.h:226
void * palloc(Size size)
Definition: mcxt.c:891
void DoCopy ( ParseState state,
const CopyStmt stmt,
int  stmt_location,
int  stmt_len,
uint64 *  processed 
)

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

776 {
777  CopyState cstate;
778  bool is_from = stmt->is_from;
779  bool pipe = (stmt->filename == NULL);
780  Relation rel;
781  Oid relid;
782  RawStmt *query = NULL;
783  List *range_table = NIL;
784 
785  /* Disallow COPY to/from file or program except to superusers. */
786  if (!pipe && !superuser())
787  {
788  if (stmt->is_program)
789  ereport(ERROR,
790  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
791  errmsg("must be superuser to COPY to or from an external program"),
792  errhint("Anyone can COPY to stdout or from stdin. "
793  "psql's \\copy command also works for anyone.")));
794  else
795  ereport(ERROR,
796  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
797  errmsg("must be superuser to COPY to or from a file"),
798  errhint("Anyone can COPY to stdout or from stdin. "
799  "psql's \\copy command also works for anyone.")));
800  }
801 
802  if (stmt->relation)
803  {
804  TupleDesc tupDesc;
805  AclMode required_access = (is_from ? ACL_INSERT : ACL_SELECT);
806  List *attnums;
807  ListCell *cur;
808  RangeTblEntry *rte;
809 
810  Assert(!stmt->query);
811 
812  /* Open and lock the relation, using the appropriate lock type. */
813  rel = heap_openrv(stmt->relation,
814  (is_from ? RowExclusiveLock : AccessShareLock));
815 
816  relid = RelationGetRelid(rel);
817 
818  rte = makeNode(RangeTblEntry);
819  rte->rtekind = RTE_RELATION;
820  rte->relid = RelationGetRelid(rel);
821  rte->relkind = rel->rd_rel->relkind;
822  rte->requiredPerms = required_access;
823  range_table = list_make1(rte);
824 
825  tupDesc = RelationGetDescr(rel);
826  attnums = CopyGetAttnums(tupDesc, rel, stmt->attlist);
827  foreach(cur, attnums)
828  {
829  int attno = lfirst_int(cur) -
831 
832  if (is_from)
833  rte->insertedCols = bms_add_member(rte->insertedCols, attno);
834  else
835  rte->selectedCols = bms_add_member(rte->selectedCols, attno);
836  }
837  ExecCheckRTPerms(range_table, true);
838 
839  /*
840  * Permission check for row security policies.
841  *
842  * check_enable_rls will ereport(ERROR) if the user has requested
843  * something invalid and will otherwise indicate if we should enable
844  * RLS (returns RLS_ENABLED) or not for this COPY statement.
845  *
846  * If the relation has a row security policy and we are to apply it
847  * then perform a "query" copy and allow the normal query processing
848  * to handle the policies.
849  *
850  * If RLS is not enabled for this, then just fall through to the
851  * normal non-filtering relation handling.
852  */
853  if (check_enable_rls(rte->relid, InvalidOid, false) == RLS_ENABLED)
854  {
856  ColumnRef *cr;
857  ResTarget *target;
858  RangeVar *from;
859  List *targetList = NIL;
860 
861  if (is_from)
862  ereport(ERROR,
863  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
864  errmsg("COPY FROM not supported with row-level security"),
865  errhint("Use INSERT statements instead.")));
866 
867  /*
868  * Build target list
869  *
870  * If no columns are specified in the attribute list of the COPY
871  * command, then the target list is 'all' columns. Therefore, '*'
872  * should be used as the target list for the resulting SELECT
873  * statement.
874  *
875  * In the case that columns are specified in the attribute list,
876  * create a ColumnRef and ResTarget for each column and add them
877  * to the target list for the resulting SELECT statement.
878  */
879  if (!stmt->attlist)
880  {
881  cr = makeNode(ColumnRef);
883  cr->location = -1;
884 
885  target = makeNode(ResTarget);
886  target->name = NULL;
887  target->indirection = NIL;
888  target->val = (Node *) cr;
889  target->location = -1;
890 
891  targetList = list_make1(target);
892  }
893  else
894  {
895  ListCell *lc;
896 
897  foreach(lc, stmt->attlist)
898  {
899  /*
900  * Build the ColumnRef for each column. The ColumnRef
901  * 'fields' property is a String 'Value' node (see
902  * nodes/value.h) that corresponds to the column name
903  * respectively.
904  */
905  cr = makeNode(ColumnRef);
906  cr->fields = list_make1(lfirst(lc));
907  cr->location = -1;
908 
909  /* Build the ResTarget and add the ColumnRef to it. */
910  target = makeNode(ResTarget);
911  target->name = NULL;
912  target->indirection = NIL;
913  target->val = (Node *) cr;
914  target->location = -1;
915 
916  /* Add each column to the SELECT statement's target list */
917  targetList = lappend(targetList, target);
918  }
919  }
920 
921  /*
922  * Build RangeVar for from clause, fully qualified based on the
923  * relation which we have opened and locked.
924  */
926  RelationGetRelationName(rel), -1);
927 
928  /* Build query */
929  select = makeNode(SelectStmt);
930  select->targetList = targetList;
931  select->fromClause = list_make1(from);
932 
933  query = makeNode(RawStmt);
934  query->stmt = (Node *) select;
935  query->stmt_location = stmt_location;
936  query->stmt_len = stmt_len;
937 
938  /*
939  * Close the relation for now, but keep the lock on it to prevent
940  * changes between now and when we start the query-based COPY.
941  *
942  * We'll reopen it later as part of the query-based COPY.
943  */
944  heap_close(rel, NoLock);
945  rel = NULL;
946  }
947  }
948  else
949  {
950  Assert(stmt->query);
951 
952  query = makeNode(RawStmt);
953  query->stmt = stmt->query;
954  query->stmt_location = stmt_location;
955  query->stmt_len = stmt_len;
956 
957  relid = InvalidOid;
958  rel = NULL;
959  }
960 
961  if (is_from)
962  {
963  Assert(rel);
964 
965  /* check read-only transaction and parallel mode */
966  if (XactReadOnly && !rel->rd_islocaltemp)
967  PreventCommandIfReadOnly("COPY FROM");
968  PreventCommandIfParallelMode("COPY FROM");
969 
970  cstate = BeginCopyFrom(pstate, rel, stmt->filename, stmt->is_program,
971  stmt->attlist, stmt->options);
972  cstate->range_table = range_table;
973  *processed = CopyFrom(cstate); /* copy from file to database */
974  EndCopyFrom(cstate);
975  }
976  else
977  {
978  cstate = BeginCopyTo(pstate, rel, query, relid,
979  stmt->filename, stmt->is_program,
980  stmt->attlist, stmt->options);
981  *processed = DoCopyTo(cstate); /* copy from database to file */
982  EndCopyTo(cstate);
983  }
984 
985  /*
986  * Close the relation. If reading, we can release the AccessShareLock we
987  * got; if writing, we should hold the lock until end of transaction to
988  * ensure that updates will be committed before lock is released.
989  */
990  if (rel != NULL)
991  heap_close(rel, (is_from ? NoLock : AccessShareLock));
992 }
List * indirection
Definition: parsenodes.h:432
#define NIL
Definition: pg_list.h:69
List * range_table
Definition: copy.c:163
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:1836
List * fromClause
Definition: parsenodes.h:1426
#define RelationGetDescr(relation)
Definition: rel.h:425
char * name
Definition: parsenodes.h:431
bool rd_islocaltemp
Definition: rel.h:90
#define AccessShareLock
Definition: lockdefs.h:36
Definition: nodes.h:508
struct cursor * cur
Definition: ecpg.c:28
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
#define select(n, r, w, e, timeout)
Definition: win32.h:384
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
AclMode requiredPerms
Definition: parsenodes.h:965
#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:1839
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:967
static CopyState BeginCopyTo(ParseState *pstate, Relation rel, RawStmt *query, Oid queryRelId, const char *filename, bool is_program, List *attnamelist, List *options)
Definition: copy.c:1754
#define ERROR
Definition: elog.h:43
#define lfirst_int(lc)
Definition: pg_list.h:107
CopyState BeginCopyFrom(ParseState *pstate, Relation rel, const char *filename, bool is_program, List *attnamelist, List *options)
Definition: copy.c:2876
RangeVar * relation
Definition: parsenodes.h:1833
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3006
Node * stmt
Definition: parsenodes.h:1337
#define NoLock
Definition: lockdefs.h:34
List * targetList
Definition: parsenodes.h:1425
static uint64 DoCopyTo(CopyState cstate)
Definition: copy.c:1883
#define RowExclusiveLock
Definition: lockdefs.h:38
List * options
Definition: parsenodes.h:1841
#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:4610
#define ACL_SELECT
Definition: parsenodes.h:66
int stmt_len
Definition: parsenodes.h:1339
int stmt_location
Definition: parsenodes.h:1338
static uint64 CopyFrom(CopyState cstate)
Definition: copy.c:2289
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:3418
#define makeNode(_type_)
Definition: nodes.h:556
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#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:1834
RTEKind rtekind
Definition: parsenodes.h:882
bool is_from
Definition: parsenodes.h:1838
int errmsg(const char *fmt,...)
Definition: elog.c:797
Bitmapset * insertedCols
Definition: parsenodes.h:968
bool ExecCheckRTPerms(List *rangeTable, bool ereport_on_violation)
Definition: execMain.c:548
char * filename
Definition: parsenodes.h:1840
static void EndCopyTo(CopyState cstate)
Definition: copy.c:1918
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 3418 of file copy.c.

References EndCopy().

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

3419 {
3420  /* No COPY FROM related resources except memory. */
3421 
3422  EndCopy(cstate);
3423 }
static void EndCopy(CopyState cstate)
Definition: copy.c:1731
bool NextCopyFrom ( CopyState  cstate,
ExprContext econtext,
Datum values,
bool nulls,
Oid tupleOid 
)

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

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

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

3126 {
3127  int fldct;
3128  bool done;
3129 
3130  /* only available for text or csv input */
3131  Assert(!cstate->binary);
3132 
3133  /* on input just throw the header line away */
3134  if (cstate->cur_lineno == 0 && cstate->header_line)
3135  {
3136  cstate->cur_lineno++;
3137  if (CopyReadLine(cstate))
3138  return false; /* done */
3139  }
3140 
3141  cstate->cur_lineno++;
3142 
3143  /* Actually read the line into memory here */
3144  done = CopyReadLine(cstate);
3145 
3146  /*
3147  * EOF at start of line means we're done. If we see EOF after some
3148  * characters, we act as though it was newline followed by EOF, ie,
3149  * process the line and then exit loop on next iteration.
3150  */
3151  if (done && cstate->line_buf.len == 0)
3152  return false;
3153 
3154  /* Parse the line into de-escaped field values */
3155  if (cstate->csv_mode)
3156  fldct = CopyReadAttributesCSV(cstate);
3157  else
3158  fldct = CopyReadAttributesText(cstate);
3159 
3160  *fields = cstate->raw_fields;
3161  *nfields = fldct;
3162  return true;
3163 }
bool csv_mode
Definition: copy.c:115
char ** raw_fields
Definition: copy.c:185
bool binary
Definition: copy.c:112
StringInfoData line_buf
Definition: copy.c:194
static int CopyReadAttributesCSV(CopyState cstate)
Definition: copy.c:4138
bool header_line
Definition: copy.c:116
#define Assert(condition)
Definition: c.h:671
static bool CopyReadLine(CopyState cstate)
Definition: copy.c:3434
int cur_lineno
Definition: copy.c:136
static int CopyReadAttributesText(CopyState cstate)
Definition: copy.c:3910
void ProcessCopyOptions ( ParseState pstate,
CopyState  cstate,
bool  is_from,
List options 
)

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

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