PostgreSQL Source Code  git master
createas.h File Reference
Include dependency graph for createas.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

ObjectAddress ExecCreateTableAs (ParseState *pstate, CreateTableAsStmt *stmt, ParamListInfo params, QueryEnvironment *queryEnv, QueryCompletion *qc)
 
int GetIntoRelEFlags (IntoClause *intoClause)
 
DestReceiverCreateIntoRelDestReceiver (IntoClause *intoClause)
 
bool CreateTableAsRelExists (CreateTableAsStmt *ctas)
 

Function Documentation

◆ CreateIntoRelDestReceiver()

DestReceiver* CreateIntoRelDestReceiver ( IntoClause intoClause)

Definition at line 433 of file createas.c.

434 {
435  DR_intorel *self = (DR_intorel *) palloc0(sizeof(DR_intorel));
436 
437  self->pub.receiveSlot = intorel_receive;
438  self->pub.rStartup = intorel_startup;
439  self->pub.rShutdown = intorel_shutdown;
440  self->pub.rDestroy = intorel_destroy;
441  self->pub.mydest = DestIntoRel;
442  self->into = intoClause;
443  /* other private fields will be set during intorel_startup */
444 
445  return (DestReceiver *) self;
446 }
static void intorel_shutdown(DestReceiver *self)
Definition: createas.c:607
static bool intorel_receive(TupleTableSlot *slot, DestReceiver *self)
Definition: createas.c:576
static void intorel_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: createas.c:452
static void intorel_destroy(DestReceiver *self)
Definition: createas.c:627
@ DestIntoRel
Definition: dest.h:94
void * palloc0(Size size)
Definition: mcxt.c:1347

References DestIntoRel, intorel_destroy(), intorel_receive(), intorel_shutdown(), intorel_startup(), and palloc0().

Referenced by CreateDestReceiver(), ExecCreateTableAs(), and ExplainOnePlan().

◆ CreateTableAsRelExists()

bool CreateTableAsRelExists ( CreateTableAsStmt ctas)

Definition at line 386 of file createas.c.

387 {
388  Oid nspid;
389  Oid oldrelid;
390  ObjectAddress address;
391  IntoClause *into = ctas->into;
392 
394 
395  oldrelid = get_relname_relid(into->rel->relname, nspid);
396  if (OidIsValid(oldrelid))
397  {
398  if (!ctas->if_not_exists)
399  ereport(ERROR,
400  (errcode(ERRCODE_DUPLICATE_TABLE),
401  errmsg("relation \"%s\" already exists",
402  into->rel->relname)));
403 
404  /*
405  * The relation exists and IF NOT EXISTS has been specified.
406  *
407  * If we are in an extension script, insist that the pre-existing
408  * object be a member of the extension, to avoid security risks.
409  */
410  ObjectAddressSet(address, RelationRelationId, oldrelid);
412 
413  /* OK to skip */
414  ereport(NOTICE,
415  (errcode(ERRCODE_DUPLICATE_TABLE),
416  errmsg("relation \"%s\" already exists, skipping",
417  into->rel->relname)));
418  return true;
419  }
420 
421  /* Relation does not exist, it can be created */
422  return false;
423 }
#define OidIsValid(objectId)
Definition: c.h:775
int nspid
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define NOTICE
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:149
Oid get_relname_relid(const char *relname, Oid relnamespace)
Definition: lsyscache.c:1885
Oid RangeVarGetCreationNamespace(const RangeVar *newRelation)
Definition: namespace.c:639
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
void checkMembershipInCurrentExtension(const ObjectAddress *object)
Definition: pg_depend.c:259
unsigned int Oid
Definition: postgres_ext.h:31
IntoClause * into
Definition: parsenodes.h:3897
RangeVar * rel
Definition: primnodes.h:162
char * relname
Definition: primnodes.h:82

References checkMembershipInCurrentExtension(), ereport, errcode(), errmsg(), ERROR, get_relname_relid(), CreateTableAsStmt::if_not_exists, CreateTableAsStmt::into, NOTICE, nspid, ObjectAddressSet, OidIsValid, RangeVarGetCreationNamespace(), IntoClause::rel, and RangeVar::relname.

Referenced by ExecCreateTableAs(), and ExplainOneUtility().

◆ ExecCreateTableAs()

ObjectAddress ExecCreateTableAs ( ParseState pstate,
CreateTableAsStmt stmt,
ParamListInfo  params,
QueryEnvironment queryEnv,
QueryCompletion qc 
)

Definition at line 221 of file createas.c.

224 {
225  Query *query = castNode(Query, stmt->query);
226  IntoClause *into = stmt->into;
227  bool is_matview = (into->viewQuery != NULL);
228  bool do_refresh = false;
230  ObjectAddress address;
231  List *rewritten;
232  PlannedStmt *plan;
233  QueryDesc *queryDesc;
234 
235  /* Check if the relation exists or not */
237  return InvalidObjectAddress;
238 
239  /*
240  * Create the tuple receiver object and insert info it will need
241  */
243 
244  /*
245  * The contained Query could be a SELECT, or an EXECUTE utility command.
246  * If the latter, we just pass it off to ExecuteQuery.
247  */
248  if (query->commandType == CMD_UTILITY &&
249  IsA(query->utilityStmt, ExecuteStmt))
250  {
251  ExecuteStmt *estmt = castNode(ExecuteStmt, query->utilityStmt);
252 
253  Assert(!is_matview); /* excluded by syntax */
254  ExecuteQuery(pstate, estmt, into, params, dest, qc);
255 
256  /* get object address that intorel_startup saved for us */
257  address = ((DR_intorel *) dest)->reladdr;
258 
259  return address;
260  }
261  Assert(query->commandType == CMD_SELECT);
262 
263  /*
264  * For materialized views, always skip data during table creation, and use
265  * REFRESH instead (see below).
266  */
267  if (is_matview)
268  {
269  do_refresh = !into->skipData;
270  into->skipData = true;
271  }
272 
273  if (into->skipData)
274  {
275  /*
276  * If WITH NO DATA was specified, do not go through the rewriter,
277  * planner and executor. Just define the relation using a code path
278  * similar to CREATE VIEW. This avoids dump/restore problems stemming
279  * from running the planner before all dependencies are set up.
280  */
281  address = create_ctas_nodata(query->targetList, into);
282  }
283  else
284  {
285  /*
286  * Parse analysis was done already, but we still have to run the rule
287  * rewriter. We do not do AcquireRewriteLocks: we assume the query
288  * either came straight from the parser, or suitable locks were
289  * acquired by plancache.c.
290  */
291  rewritten = QueryRewrite(query);
292 
293  /* SELECT should never rewrite to more or less than one SELECT query */
294  if (list_length(rewritten) != 1)
295  elog(ERROR, "unexpected rewrite result for %s",
296  is_matview ? "CREATE MATERIALIZED VIEW" :
297  "CREATE TABLE AS SELECT");
298  query = linitial_node(Query, rewritten);
299  Assert(query->commandType == CMD_SELECT);
300 
301  /* plan the query */
302  plan = pg_plan_query(query, pstate->p_sourcetext,
303  CURSOR_OPT_PARALLEL_OK, params);
304 
305  /*
306  * Use a snapshot with an updated command ID to ensure this query sees
307  * results of any previously executed queries. (This could only
308  * matter if the planner executed an allegedly-stable function that
309  * changed the database contents, but let's do it anyway to be
310  * parallel to the EXPLAIN code path.)
311  */
314 
315  /* Create a QueryDesc, redirecting output to our tuple receiver */
316  queryDesc = CreateQueryDesc(plan, pstate->p_sourcetext,
318  dest, params, queryEnv, 0);
319 
320  /* call ExecutorStart to prepare the plan for execution */
321  ExecutorStart(queryDesc, GetIntoRelEFlags(into));
322 
323  /* run the plan to completion */
324  ExecutorRun(queryDesc, ForwardScanDirection, 0, true);
325 
326  /* save the rowcount if we're given a qc to fill */
327  if (qc)
328  SetQueryCompletion(qc, CMDTAG_SELECT, queryDesc->estate->es_processed);
329 
330  /* get object address that intorel_startup saved for us */
331  address = ((DR_intorel *) dest)->reladdr;
332 
333  /* and clean up */
334  ExecutorFinish(queryDesc);
335  ExecutorEnd(queryDesc);
336 
337  FreeQueryDesc(queryDesc);
338 
340  }
341 
342  /*
343  * For materialized views, reuse the REFRESH logic, which locks down
344  * security-restricted operations and restricts the search_path. This
345  * reduces the chance that a subsequent refresh will fail.
346  */
347  if (do_refresh)
348  {
349  RefreshMatViewByOid(address.objectId, false, false,
350  pstate->p_sourcetext, NULL, qc);
351 
352  if (qc)
353  qc->commandTag = CMDTAG_SELECT;
354  }
355 
356  return address;
357 }
void ExecuteQuery(ParseState *pstate, ExecuteStmt *stmt, IntoClause *intoClause, ParamListInfo params, DestReceiver *dest, QueryCompletion *qc)
Definition: prepare.c:147
#define Assert(condition)
Definition: c.h:858
static void SetQueryCompletion(QueryCompletion *qc, CommandTag commandTag, uint64 nprocessed)
Definition: cmdtag.h:37
bool CreateTableAsRelExists(CreateTableAsStmt *ctas)
Definition: createas.c:386
DestReceiver * CreateIntoRelDestReceiver(IntoClause *intoClause)
Definition: createas.c:433
static ObjectAddress create_ctas_nodata(List *tlist, IntoClause *into)
Definition: createas.c:153
int GetIntoRelEFlags(IntoClause *intoClause)
Definition: createas.c:368
#define elog(elevel,...)
Definition: elog.h:224
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:467
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:407
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:124
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:297
#define stmt
Definition: indent_codes.h:59
ObjectAddress RefreshMatViewByOid(Oid matviewOid, bool skipData, bool concurrent, const char *queryString, ParamListInfo params, QueryCompletion *qc)
Definition: matview.c:162
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
@ CMD_UTILITY
Definition: nodes.h:270
@ CMD_SELECT
Definition: nodes.h:265
#define castNode(_type_, nodeptr)
Definition: nodes.h:176
const ObjectAddress InvalidObjectAddress
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:3296
static int list_length(const List *l)
Definition: pg_list.h:152
#define linitial_node(type, l)
Definition: pg_list.h:181
#define plan(x)
Definition: pg_regress.c:162
PlannedStmt * pg_plan_query(Query *querytree, const char *query_string, int cursorOptions, ParamListInfo boundParams)
Definition: postgres.c:886
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:105
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
List * QueryRewrite(Query *parsetree)
@ ForwardScanDirection
Definition: sdir.h:28
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:712
void PopActiveSnapshot(void)
Definition: snapmgr.c:743
void PushCopiedSnapshot(Snapshot snapshot)
Definition: snapmgr.c:700
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:770
#define InvalidSnapshot
Definition: snapshot.h:123
uint64 es_processed
Definition: execnodes.h:671
bool skipData
Definition: primnodes.h:170
Definition: pg_list.h:54
const char * p_sourcetext
Definition: parse_node.h:193
CommandTag commandTag
Definition: cmdtag.h:31
EState * estate
Definition: execdesc.h:48
CmdType commandType
Definition: parsenodes.h:121
Node * utilityStmt
Definition: parsenodes.h:136
List * targetList
Definition: parsenodes.h:191

References Assert, castNode, CMD_SELECT, CMD_UTILITY, QueryCompletion::commandTag, Query::commandType, create_ctas_nodata(), CreateIntoRelDestReceiver(), CreateQueryDesc(), CreateTableAsRelExists(), CURSOR_OPT_PARALLEL_OK, generate_unaccent_rules::dest, elog, ERROR, EState::es_processed, QueryDesc::estate, ExecuteQuery(), ExecutorEnd(), ExecutorFinish(), ExecutorRun(), ExecutorStart(), ForwardScanDirection, FreeQueryDesc(), GetActiveSnapshot(), GetIntoRelEFlags(), InvalidObjectAddress, InvalidSnapshot, IsA, linitial_node, list_length(), ObjectAddress::objectId, ParseState::p_sourcetext, pg_plan_query(), plan, PopActiveSnapshot(), PushCopiedSnapshot(), QueryRewrite(), RefreshMatViewByOid(), SetQueryCompletion(), IntoClause::skipData, stmt, Query::targetList, UpdateActiveSnapshotCommandId(), and Query::utilityStmt.

Referenced by ProcessUtilitySlow().

◆ GetIntoRelEFlags()

int GetIntoRelEFlags ( IntoClause intoClause)

Definition at line 368 of file createas.c.

369 {
370  int flags = 0;
371 
372  if (intoClause->skipData)
373  flags |= EXEC_FLAG_WITH_NO_DATA;
374 
375  return flags;
376 }
#define EXEC_FLAG_WITH_NO_DATA
Definition: executor.h:71

References EXEC_FLAG_WITH_NO_DATA, and IntoClause::skipData.

Referenced by ExecCreateTableAs(), ExecuteQuery(), and ExplainOnePlan().