PostgreSQL Source Code  git master
createas.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * createas.c
4  * Execution of CREATE TABLE ... AS, a/k/a SELECT INTO.
5  * Since CREATE MATERIALIZED VIEW shares syntax and most behaviors,
6  * we implement that here, too.
7  *
8  * We implement this by diverting the query's normal output to a
9  * specialized DestReceiver type.
10  *
11  * Formerly, CTAS was implemented as a variant of SELECT, which led
12  * to assorted legacy behaviors that we still try to preserve, notably that
13  * we must return a tuples-processed count in the QueryCompletion. (We no
14  * longer do that for CTAS ... WITH NO DATA, however.)
15  *
16  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
17  * Portions Copyright (c) 1994, Regents of the University of California
18  *
19  *
20  * IDENTIFICATION
21  * src/backend/commands/createas.c
22  *
23  *-------------------------------------------------------------------------
24  */
25 #include "postgres.h"
26 
27 #include "access/heapam.h"
28 #include "access/htup_details.h"
29 #include "access/reloptions.h"
30 #include "access/sysattr.h"
31 #include "access/tableam.h"
32 #include "access/xact.h"
33 #include "access/xlog.h"
34 #include "catalog/namespace.h"
35 #include "catalog/toasting.h"
36 #include "commands/createas.h"
37 #include "commands/matview.h"
38 #include "commands/prepare.h"
39 #include "commands/tablecmds.h"
40 #include "commands/view.h"
41 #include "miscadmin.h"
42 #include "nodes/makefuncs.h"
43 #include "nodes/nodeFuncs.h"
44 #include "parser/parse_clause.h"
45 #include "rewrite/rewriteHandler.h"
46 #include "storage/smgr.h"
47 #include "tcop/tcopprot.h"
48 #include "utils/builtins.h"
49 #include "utils/lsyscache.h"
50 #include "utils/rel.h"
51 #include "utils/rls.h"
52 #include "utils/snapmgr.h"
53 
54 typedef struct
55 {
56  DestReceiver pub; /* publicly-known function pointers */
57  IntoClause *into; /* target relation specification */
58  /* These fields are filled by intorel_startup: */
59  Relation rel; /* relation to write to */
60  ObjectAddress reladdr; /* address of rel, for ExecCreateTableAs */
61  CommandId output_cid; /* cmin to insert in output tuples */
62  int ti_options; /* table_tuple_insert performance options */
63  BulkInsertState bistate; /* bulk insert state */
64 } DR_intorel;
65 
66 /* utility functions for CTAS definition creation */
67 static ObjectAddress create_ctas_internal(List *attrList, IntoClause *into);
68 static ObjectAddress create_ctas_nodata(List *tlist, IntoClause *into);
69 
70 /* DestReceiver routines for collecting data */
71 static void intorel_startup(DestReceiver *self, int operation, TupleDesc typeinfo);
72 static bool intorel_receive(TupleTableSlot *slot, DestReceiver *self);
73 static void intorel_shutdown(DestReceiver *self);
74 static void intorel_destroy(DestReceiver *self);
75 
76 
77 /*
78  * create_ctas_internal
79  *
80  * Internal utility used for the creation of the definition of a relation
81  * created via CREATE TABLE AS or a materialized view. Caller needs to
82  * provide a list of attributes (ColumnDef nodes).
83  */
84 static ObjectAddress
86 {
87  CreateStmt *create = makeNode(CreateStmt);
88  bool is_matview;
89  char relkind;
90  Datum toast_options;
91  static char *validnsps[] = HEAP_RELOPT_NAMESPACES;
92  ObjectAddress intoRelationAddr;
93 
94  /* This code supports both CREATE TABLE AS and CREATE MATERIALIZED VIEW */
95  is_matview = (into->viewQuery != NULL);
96  relkind = is_matview ? RELKIND_MATVIEW : RELKIND_RELATION;
97 
98  /*
99  * Create the target relation by faking up a CREATE TABLE parsetree and
100  * passing it to DefineRelation.
101  */
102  create->relation = into->rel;
103  create->tableElts = attrList;
104  create->inhRelations = NIL;
105  create->ofTypename = NULL;
106  create->constraints = NIL;
107  create->options = into->options;
108  create->oncommit = into->onCommit;
109  create->tablespacename = into->tableSpaceName;
110  create->if_not_exists = false;
111  create->accessMethod = into->accessMethod;
112 
113  /*
114  * Create the relation. (This will error out if there's an existing view,
115  * so we don't need more code to complain if "replace" is false.)
116  */
117  intoRelationAddr = DefineRelation(create, relkind, InvalidOid, NULL, NULL);
118 
119  /*
120  * If necessary, create a TOAST table for the target table. Note that
121  * NewRelationCreateToastTable ends with CommandCounterIncrement(), so
122  * that the TOAST table will be visible for insertion.
123  */
125 
126  /* parse and validate reloptions for the toast table */
127  toast_options = transformRelOptions((Datum) 0,
128  create->options,
129  "toast",
130  validnsps,
131  true, false);
132 
133  (void) heap_reloptions(RELKIND_TOASTVALUE, toast_options, true);
134 
135  NewRelationCreateToastTable(intoRelationAddr.objectId, toast_options);
136 
137  /* Create the "view" part of a materialized view. */
138  if (is_matview)
139  {
140  /* StoreViewQuery scribbles on tree, so make a copy */
141  Query *query = (Query *) copyObject(into->viewQuery);
142 
143  StoreViewQuery(intoRelationAddr.objectId, query, false);
145  }
146 
147  return intoRelationAddr;
148 }
149 
150 
151 /*
152  * create_ctas_nodata
153  *
154  * Create CTAS or materialized view when WITH NO DATA is used, starting from
155  * the targetlist of the SELECT or view definition.
156  */
157 static ObjectAddress
159 {
160  List *attrList;
161  ListCell *t,
162  *lc;
163 
164  /*
165  * Build list of ColumnDefs from non-junk elements of the tlist. If a
166  * column name list was specified in CREATE TABLE AS, override the column
167  * names in the query. (Too few column names are OK, too many are not.)
168  */
169  attrList = NIL;
170  lc = list_head(into->colNames);
171  foreach(t, tlist)
172  {
173  TargetEntry *tle = (TargetEntry *) lfirst(t);
174 
175  if (!tle->resjunk)
176  {
177  ColumnDef *col;
178  char *colname;
179 
180  if (lc)
181  {
182  colname = strVal(lfirst(lc));
183  lc = lnext(into->colNames, lc);
184  }
185  else
186  colname = tle->resname;
187 
188  col = makeColumnDef(colname,
189  exprType((Node *) tle->expr),
190  exprTypmod((Node *) tle->expr),
191  exprCollation((Node *) tle->expr));
192 
193  /*
194  * It's possible that the column is of a collatable type but the
195  * collation could not be resolved, so double-check. (We must
196  * check this here because DefineRelation would adopt the type's
197  * default collation rather than complaining.)
198  */
199  if (!OidIsValid(col->collOid) &&
201  ereport(ERROR,
202  (errcode(ERRCODE_INDETERMINATE_COLLATION),
203  errmsg("no collation was derived for column \"%s\" with collatable type %s",
204  col->colname,
206  errhint("Use the COLLATE clause to set the collation explicitly.")));
207 
208  attrList = lappend(attrList, col);
209  }
210  }
211 
212  if (lc != NULL)
213  ereport(ERROR,
214  (errcode(ERRCODE_SYNTAX_ERROR),
215  errmsg("too many column names were specified")));
216 
217  /* Create the relation definition using the ColumnDef list */
218  return create_ctas_internal(attrList, into);
219 }
220 
221 
222 /*
223  * ExecCreateTableAs -- execute a CREATE TABLE AS command
224  */
227  ParamListInfo params, QueryEnvironment *queryEnv,
228  QueryCompletion *qc)
229 {
230  Query *query = castNode(Query, stmt->query);
231  IntoClause *into = stmt->into;
232  bool is_matview = (into->viewQuery != NULL);
234  Oid save_userid = InvalidOid;
235  int save_sec_context = 0;
236  int save_nestlevel = 0;
237  ObjectAddress address;
238  List *rewritten;
239  PlannedStmt *plan;
240  QueryDesc *queryDesc;
241 
242  /* Check if the relation exists or not */
243  if (CreateTableAsRelExists(stmt))
244  return InvalidObjectAddress;
245 
246  /*
247  * Create the tuple receiver object and insert info it will need
248  */
249  dest = CreateIntoRelDestReceiver(into);
250 
251  /*
252  * The contained Query could be a SELECT, or an EXECUTE utility command.
253  * If the latter, we just pass it off to ExecuteQuery.
254  */
255  if (query->commandType == CMD_UTILITY &&
256  IsA(query->utilityStmt, ExecuteStmt))
257  {
258  ExecuteStmt *estmt = castNode(ExecuteStmt, query->utilityStmt);
259 
260  Assert(!is_matview); /* excluded by syntax */
261  ExecuteQuery(pstate, estmt, into, params, dest, qc);
262 
263  /* get object address that intorel_startup saved for us */
264  address = ((DR_intorel *) dest)->reladdr;
265 
266  return address;
267  }
268  Assert(query->commandType == CMD_SELECT);
269 
270  /*
271  * For materialized views, lock down security-restricted operations and
272  * arrange to make GUC variable changes local to this command. This is
273  * not necessary for security, but this keeps the behavior similar to
274  * REFRESH MATERIALIZED VIEW. Otherwise, one could create a materialized
275  * view not possible to refresh.
276  */
277  if (is_matview)
278  {
279  GetUserIdAndSecContext(&save_userid, &save_sec_context);
280  SetUserIdAndSecContext(save_userid,
281  save_sec_context | SECURITY_RESTRICTED_OPERATION);
282  save_nestlevel = NewGUCNestLevel();
283  }
284 
285  if (into->skipData)
286  {
287  /*
288  * If WITH NO DATA was specified, do not go through the rewriter,
289  * planner and executor. Just define the relation using a code path
290  * similar to CREATE VIEW. This avoids dump/restore problems stemming
291  * from running the planner before all dependencies are set up.
292  */
293  address = create_ctas_nodata(query->targetList, into);
294  }
295  else
296  {
297  /*
298  * Parse analysis was done already, but we still have to run the rule
299  * rewriter. We do not do AcquireRewriteLocks: we assume the query
300  * either came straight from the parser, or suitable locks were
301  * acquired by plancache.c.
302  */
303  rewritten = QueryRewrite(query);
304 
305  /* SELECT should never rewrite to more or less than one SELECT query */
306  if (list_length(rewritten) != 1)
307  elog(ERROR, "unexpected rewrite result for %s",
308  is_matview ? "CREATE MATERIALIZED VIEW" :
309  "CREATE TABLE AS SELECT");
310  query = linitial_node(Query, rewritten);
311  Assert(query->commandType == CMD_SELECT);
312 
313  /* plan the query */
314  plan = pg_plan_query(query, pstate->p_sourcetext,
315  CURSOR_OPT_PARALLEL_OK, params);
316 
317  /*
318  * Use a snapshot with an updated command ID to ensure this query sees
319  * results of any previously executed queries. (This could only
320  * matter if the planner executed an allegedly-stable function that
321  * changed the database contents, but let's do it anyway to be
322  * parallel to the EXPLAIN code path.)
323  */
326 
327  /* Create a QueryDesc, redirecting output to our tuple receiver */
328  queryDesc = CreateQueryDesc(plan, pstate->p_sourcetext,
330  dest, params, queryEnv, 0);
331 
332  /* call ExecutorStart to prepare the plan for execution */
333  ExecutorStart(queryDesc, GetIntoRelEFlags(into));
334 
335  /* run the plan to completion */
336  ExecutorRun(queryDesc, ForwardScanDirection, 0L, true);
337 
338  /* save the rowcount if we're given a qc to fill */
339  if (qc)
340  SetQueryCompletion(qc, CMDTAG_SELECT, queryDesc->estate->es_processed);
341 
342  /* get object address that intorel_startup saved for us */
343  address = ((DR_intorel *) dest)->reladdr;
344 
345  /* and clean up */
346  ExecutorFinish(queryDesc);
347  ExecutorEnd(queryDesc);
348 
349  FreeQueryDesc(queryDesc);
350 
352  }
353 
354  if (is_matview)
355  {
356  /* Roll back any GUC changes */
357  AtEOXact_GUC(false, save_nestlevel);
358 
359  /* Restore userid and security context */
360  SetUserIdAndSecContext(save_userid, save_sec_context);
361  }
362 
363  return address;
364 }
365 
366 /*
367  * GetIntoRelEFlags --- compute executor flags needed for CREATE TABLE AS
368  *
369  * This is exported because EXPLAIN and PREPARE need it too. (Note: those
370  * callers still need to deal explicitly with the skipData flag; since they
371  * use different methods for suppressing execution, it doesn't seem worth
372  * trying to encapsulate that part.)
373  */
374 int
376 {
377  int flags = 0;
378 
379  if (intoClause->skipData)
380  flags |= EXEC_FLAG_WITH_NO_DATA;
381 
382  return flags;
383 }
384 
385 /*
386  * CreateTableAsRelExists --- check existence of relation for CreateTableAsStmt
387  *
388  * Utility wrapper checking if the relation pending for creation in this
389  * CreateTableAsStmt query already exists or not. Returns true if the
390  * relation exists, otherwise false.
391  */
392 bool
394 {
395  Oid nspid;
396  IntoClause *into = ctas->into;
397 
398  nspid = RangeVarGetCreationNamespace(into->rel);
399 
400  if (get_relname_relid(into->rel->relname, nspid))
401  {
402  if (!ctas->if_not_exists)
403  ereport(ERROR,
404  (errcode(ERRCODE_DUPLICATE_TABLE),
405  errmsg("relation \"%s\" already exists",
406  into->rel->relname)));
407 
408  /* The relation exists and IF NOT EXISTS has been specified */
409  ereport(NOTICE,
410  (errcode(ERRCODE_DUPLICATE_TABLE),
411  errmsg("relation \"%s\" already exists, skipping",
412  into->rel->relname)));
413  return true;
414  }
415 
416  /* Relation does not exist, it can be created */
417  return false;
418 }
419 
420 /*
421  * CreateIntoRelDestReceiver -- create a suitable DestReceiver object
422  *
423  * intoClause will be NULL if called from CreateDestReceiver(), in which
424  * case it has to be provided later. However, it is convenient to allow
425  * self->into to be filled in immediately for other callers.
426  */
427 DestReceiver *
429 {
430  DR_intorel *self = (DR_intorel *) palloc0(sizeof(DR_intorel));
431 
432  self->pub.receiveSlot = intorel_receive;
433  self->pub.rStartup = intorel_startup;
434  self->pub.rShutdown = intorel_shutdown;
435  self->pub.rDestroy = intorel_destroy;
436  self->pub.mydest = DestIntoRel;
437  self->into = intoClause;
438  /* other private fields will be set during intorel_startup */
439 
440  return (DestReceiver *) self;
441 }
442 
443 /*
444  * intorel_startup --- executor startup
445  */
446 static void
447 intorel_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
448 {
449  DR_intorel *myState = (DR_intorel *) self;
450  IntoClause *into = myState->into;
451  bool is_matview;
452  List *attrList;
453  ObjectAddress intoRelationAddr;
454  Relation intoRelationDesc;
455  ListCell *lc;
456  int attnum;
457 
458  Assert(into != NULL); /* else somebody forgot to set it */
459 
460  /* This code supports both CREATE TABLE AS and CREATE MATERIALIZED VIEW */
461  is_matview = (into->viewQuery != NULL);
462 
463  /*
464  * Build column definitions using "pre-cooked" type and collation info. If
465  * a column name list was specified in CREATE TABLE AS, override the
466  * column names derived from the query. (Too few column names are OK, too
467  * many are not.)
468  */
469  attrList = NIL;
470  lc = list_head(into->colNames);
471  for (attnum = 0; attnum < typeinfo->natts; attnum++)
472  {
473  Form_pg_attribute attribute = TupleDescAttr(typeinfo, attnum);
474  ColumnDef *col;
475  char *colname;
476 
477  if (lc)
478  {
479  colname = strVal(lfirst(lc));
480  lc = lnext(into->colNames, lc);
481  }
482  else
483  colname = NameStr(attribute->attname);
484 
485  col = makeColumnDef(colname,
486  attribute->atttypid,
487  attribute->atttypmod,
488  attribute->attcollation);
489 
490  /*
491  * It's possible that the column is of a collatable type but the
492  * collation could not be resolved, so double-check. (We must check
493  * this here because DefineRelation would adopt the type's default
494  * collation rather than complaining.)
495  */
496  if (!OidIsValid(col->collOid) &&
498  ereport(ERROR,
499  (errcode(ERRCODE_INDETERMINATE_COLLATION),
500  errmsg("no collation was derived for column \"%s\" with collatable type %s",
501  col->colname,
503  errhint("Use the COLLATE clause to set the collation explicitly.")));
504 
505  attrList = lappend(attrList, col);
506  }
507 
508  if (lc != NULL)
509  ereport(ERROR,
510  (errcode(ERRCODE_SYNTAX_ERROR),
511  errmsg("too many column names were specified")));
512 
513  /*
514  * Actually create the target table
515  */
516  intoRelationAddr = create_ctas_internal(attrList, into);
517 
518  /*
519  * Finally we can open the target table
520  */
521  intoRelationDesc = table_open(intoRelationAddr.objectId, AccessExclusiveLock);
522 
523  /*
524  * Make sure the constructed table does not have RLS enabled.
525  *
526  * check_enable_rls() will ereport(ERROR) itself if the user has requested
527  * something invalid, and otherwise will return RLS_ENABLED if RLS should
528  * be enabled here. We don't actually support that currently, so throw
529  * our own ereport(ERROR) if that happens.
530  */
531  if (check_enable_rls(intoRelationAddr.objectId, InvalidOid, false) == RLS_ENABLED)
532  ereport(ERROR,
533  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
534  errmsg("policies not yet implemented for this command")));
535 
536  /*
537  * Tentatively mark the target as populated, if it's a matview and we're
538  * going to fill it; otherwise, no change needed.
539  */
540  if (is_matview && !into->skipData)
541  SetMatViewPopulatedState(intoRelationDesc, true);
542 
543  /*
544  * Fill private fields of myState for use by later routines
545  */
546  myState->rel = intoRelationDesc;
547  myState->reladdr = intoRelationAddr;
548  myState->output_cid = GetCurrentCommandId(true);
550 
551  /*
552  * If WITH NO DATA is specified, there is no need to set up the state for
553  * bulk inserts as there are no tuples to insert.
554  */
555  if (!into->skipData)
556  myState->bistate = GetBulkInsertState();
557  else
558  myState->bistate = NULL;
559 
560  /*
561  * Valid smgr_targblock implies something already wrote to the relation.
562  * This may be harmless, but this function hasn't planned for it.
563  */
564  Assert(RelationGetTargetBlock(intoRelationDesc) == InvalidBlockNumber);
565 }
566 
567 /*
568  * intorel_receive --- receive one tuple
569  */
570 static bool
572 {
573  DR_intorel *myState = (DR_intorel *) self;
574 
575  /* Nothing to insert if WITH NO DATA is specified. */
576  if (!myState->into->skipData)
577  {
578  /*
579  * Note that the input slot might not be of the type of the target
580  * relation. That's supported by table_tuple_insert(), but slightly
581  * less efficient than inserting with the right slot - but the
582  * alternative would be to copy into a slot of the right type, which
583  * would not be cheap either. This also doesn't allow accessing per-AM
584  * data (say a tuple's xmin), but since we don't do that here...
585  */
586  table_tuple_insert(myState->rel,
587  slot,
588  myState->output_cid,
589  myState->ti_options,
590  myState->bistate);
591  }
592 
593  /* We know this is a newly created relation, so there are no indexes */
594 
595  return true;
596 }
597 
598 /*
599  * intorel_shutdown --- executor end
600  */
601 static void
603 {
604  DR_intorel *myState = (DR_intorel *) self;
605  IntoClause *into = myState->into;
606 
607  if (!into->skipData)
608  {
609  FreeBulkInsertState(myState->bistate);
610  table_finish_bulk_insert(myState->rel, myState->ti_options);
611  }
612 
613  /* close rel, but keep lock until commit */
614  table_close(myState->rel, NoLock);
615  myState->rel = NULL;
616 }
617 
618 /*
619  * intorel_destroy --- release DestReceiver object
620  */
621 static void
623 {
624  pfree(self);
625 }
RangeVar * relation
Definition: parsenodes.h:2183
bytea * heap_reloptions(char relkind, Datum reloptions, bool validate)
Definition: reloptions.c:2013
#define NIL
Definition: pg_list.h:65
uint32 CommandId
Definition: c.h:601
OnCommitAction oncommit
Definition: parsenodes.h:2192
List * inhRelations
Definition: parsenodes.h:2185
Oid typeOid
Definition: parsenodes.h:222
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:728
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
List * QueryRewrite(Query *parsetree)
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
int errhint(const char *fmt,...)
Definition: elog.c:1156
OnCommitAction onCommit
Definition: primnodes.h:116
EState * estate
Definition: execdesc.h:48
Datum transformRelOptions(Datum oldOptions, List *defList, const char *namspace, char *validnsps[], bool acceptOidsOff, bool isReset)
Definition: reloptions.c:1149
#define SECURITY_RESTRICTED_OPERATION
Definition: miscadmin.h:312
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:322
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:607
char * tableSpaceName
Definition: primnodes.h:117
#define castNode(_type_, nodeptr)
Definition: nodes.h:605
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:105
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:267
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:130
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:786
int ti_options
Definition: createas.c:62
Definition: nodes.h:536
#define strVal(v)
Definition: value.h:65
int errcode(int sqlerrcode)
Definition: elog.c:698
static void intorel_shutdown(DestReceiver *self)
Definition: createas.c:602
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
CommandId output_cid
Definition: createas.c:61
static void table_finish_bulk_insert(Relation rel, int options)
Definition: tableam.h:1564
#define EXEC_FLAG_WITH_NO_DATA
Definition: executor.h:61
void PopActiveSnapshot(void)
Definition: snapmgr.c:759
bool skipData
Definition: primnodes.h:119
BulkInsertState bistate
Definition: createas.c:63
unsigned int Oid
Definition: postgres_ext.h:31
char * resname
Definition: primnodes.h:1457
Node * utilityStmt
Definition: parsenodes.h:128
#define linitial_node(type, l)
Definition: pg_list.h:177
void NewRelationCreateToastTable(Oid relOid, Datum reloptions)
Definition: toasting.c:73
#define OidIsValid(objectId)
Definition: c.h:710
#define RelationGetTargetBlock(relation)
Definition: rel.h:574
static void table_tuple_insert(Relation rel, TupleTableSlot *slot, CommandId cid, int options, struct BulkInsertStateData *bistate)
Definition: tableam.h:1371
List * targetList
Definition: parsenodes.h:150
List * constraints
Definition: parsenodes.h:2190
bool if_not_exists
Definition: parsenodes.h:2195
char * accessMethod
Definition: parsenodes.h:2194
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:459
char * relname
Definition: primnodes.h:68
BulkInsertState GetBulkInsertState(void)
Definition: heapam.c:2007
ObjectAddress reladdr
Definition: createas.c:60
int GetIntoRelEFlags(IntoClause *intoClause)
Definition: createas.c:375
static ObjectAddress create_ctas_internal(List *attrList, IntoClause *into)
Definition: createas.c:85
void pfree(void *pointer)
Definition: mcxt.c:1169
bool resjunk
Definition: primnodes.h:1462
#define ERROR
Definition: elog.h:46
Node * viewQuery
Definition: primnodes.h:118
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:298
void PushCopiedSnapshot(Snapshot snapshot)
Definition: snapmgr.c:716
QueryDesc * CreateQueryDesc(PlannedStmt *plannedstmt, const char *sourceText, Snapshot snapshot, Snapshot crosscheck_snapshot, DestReceiver *dest, ParamListInfo params, QueryEnvironment *queryEnv, int instrument_options)
Definition: pquery.c:67
Oid get_relname_relid(const char *relname, Oid relnamespace)
Definition: lsyscache.c:1856
#define NoLock
Definition: lockdefs.h:34
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
Definition: miscinit.c:600
Oid collOid
Definition: parsenodes.h:685
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:6230
void ExecuteQuery(ParseState *pstate, ExecuteStmt *stmt, IntoClause *intoClause, ParamListInfo params, DestReceiver *dest, QueryCompletion *qc)
Definition: prepare.c:184
Relation rel
Definition: createas.c:59
DestReceiver * CreateIntoRelDestReceiver(IntoClause *intoClause)
Definition: createas.c:428
bool CreateTableAsRelExists(CreateTableAsStmt *ctas)
Definition: createas.c:393
DestReceiver pub
Definition: createas.c:56
List * options
Definition: parsenodes.h:2191
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
#define HEAP_RELOPT_NAMESPACES
Definition: reloptions.h:61
const char * p_sourcetext
Definition: parse_node.h:181
ObjectAddress DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, ObjectAddress *typaddress, const char *queryString)
Definition: tablecmds.c:627
List * options
Definition: primnodes.h:115
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:399
List * lappend(List *list, void *datum)
Definition: list.c:336
char * tablespacename
Definition: parsenodes.h:2193
#define InvalidSnapshot
Definition: snapshot.h:123
static bool intorel_receive(TupleTableSlot *slot, DestReceiver *self)
Definition: createas.c:571
void * palloc0(Size size)
Definition: mcxt.c:1093
uintptr_t Datum
Definition: postgres.h:411
void CommandCounterIncrement(void)
Definition: xact.c:1021
#define TABLE_INSERT_SKIP_FSM
Definition: tableam.h:234
void StoreViewQuery(Oid viewOid, Query *viewParse, bool replace)
Definition: view.c:562
#define InvalidOid
Definition: postgres_ext.h:36
int16 attnum
Definition: pg_attribute.h:83
#define ereport(elevel,...)
Definition: elog.h:157
IntoClause * into
Definition: parsenodes.h:3409
#define NOTICE
Definition: elog.h:37
CmdType commandType
Definition: parsenodes.h:120
List * tableElts
Definition: parsenodes.h:2184
int check_enable_rls(Oid relid, Oid checkAsUser, bool noError)
Definition: rls.c:52
#define makeNode(_type_)
Definition: nodes.h:584
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
void FreeBulkInsertState(BulkInsertState bistate)
Definition: heapam.c:2021
static void SetQueryCompletion(QueryCompletion *qc, CommandTag commandTag, uint64 nprocessed)
Definition: cmdtag.h:36
Expr * expr
Definition: primnodes.h:1455
uint64 es_processed
Definition: execnodes.h:604
static void intorel_destroy(DestReceiver *self)
Definition: createas.c:622
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
#define InvalidBlockNumber
Definition: block.h:33
static int list_length(const List *l)
Definition: pg_list.h:149
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:759
TypeName * typeName
Definition: parsenodes.h:671
ObjectAddress ExecCreateTableAs(ParseState *pstate, CreateTableAsStmt *stmt, ParamListInfo params, QueryEnvironment *queryEnv, QueryCompletion *qc)
Definition: createas.c:226
IntoClause * into
Definition: createas.c:57
static ObjectAddress create_ctas_nodata(List *tlist, IntoClause *into)
Definition: createas.c:158
#define AccessExclusiveLock
Definition: lockdefs.h:45
int NewGUCNestLevel(void)
Definition: guc.c:6216
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:2828
const ObjectAddress InvalidObjectAddress
int errmsg(const char *fmt,...)
Definition: elog.c:909
RangeVar * rel
Definition: primnodes.h:112
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
PlannedStmt * pg_plan_query(Query *querytree, const char *query_string, int cursorOptions, ParamListInfo boundParams)
Definition: postgres.c:829
static void intorel_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: createas.c:447
char * colname
Definition: parsenodes.h:670
CommandId GetCurrentCommandId(bool used)
Definition: xact.c:761
#define copyObject(obj)
Definition: nodes.h:652
List * colNames
Definition: primnodes.h:113
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
bool type_is_collatable(Oid typid)
Definition: lsyscache.c:3028
Definition: pg_list.h:50
void SetMatViewPopulatedState(Relation relation, bool newstate)
Definition: matview.c:84
char * accessMethod
Definition: primnodes.h:114
ColumnDef * makeColumnDef(const char *colname, Oid typeOid, int32 typmod, Oid collOid)
Definition: makefuncs.c:490
TypeName * ofTypename
Definition: parsenodes.h:2189
Oid RangeVarGetCreationNamespace(const RangeVar *newRelation)
Definition: namespace.c:450