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 430 of file createas.c.

431 {
432  DR_intorel *self = (DR_intorel *) palloc0(sizeof(DR_intorel));
433 
434  self->pub.receiveSlot = intorel_receive;
435  self->pub.rStartup = intorel_startup;
436  self->pub.rShutdown = intorel_shutdown;
437  self->pub.rDestroy = intorel_destroy;
438  self->pub.mydest = DestIntoRel;
439  self->into = intoClause;
440  /* other private fields will be set during intorel_startup */
441 
442  return (DestReceiver *) self;
443 }
static void intorel_shutdown(DestReceiver *self)
Definition: createas.c:604
static bool intorel_receive(TupleTableSlot *slot, DestReceiver *self)
Definition: createas.c:573
static void intorel_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: createas.c:449
static void intorel_destroy(DestReceiver *self)
Definition: createas.c:624
@ 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 383 of file createas.c.

384 {
385  Oid nspid;
386  Oid oldrelid;
387  ObjectAddress address;
388  IntoClause *into = ctas->into;
389 
391 
392  oldrelid = get_relname_relid(into->rel->relname, nspid);
393  if (OidIsValid(oldrelid))
394  {
395  if (!ctas->if_not_exists)
396  ereport(ERROR,
397  (errcode(ERRCODE_DUPLICATE_TABLE),
398  errmsg("relation \"%s\" already exists",
399  into->rel->relname)));
400 
401  /*
402  * The relation exists and IF NOT EXISTS has been specified.
403  *
404  * If we are in an extension script, insist that the pre-existing
405  * object be a member of the extension, to avoid security risks.
406  */
407  ObjectAddressSet(address, RelationRelationId, oldrelid);
409 
410  /* OK to skip */
411  ereport(NOTICE,
412  (errcode(ERRCODE_DUPLICATE_TABLE),
413  errmsg("relation \"%s\" already exists, skipping",
414  into->rel->relname)));
415  return true;
416  }
417 
418  /* Relation does not exist, it can be created */
419  return false;
420 }
#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:3882
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 
232  /* Check if the relation exists or not */
234  return InvalidObjectAddress;
235 
236  /*
237  * Create the tuple receiver object and insert info it will need
238  */
240 
241  /*
242  * The contained Query could be a SELECT, or an EXECUTE utility command.
243  * If the latter, we just pass it off to ExecuteQuery.
244  */
245  if (query->commandType == CMD_UTILITY &&
246  IsA(query->utilityStmt, ExecuteStmt))
247  {
248  ExecuteStmt *estmt = castNode(ExecuteStmt, query->utilityStmt);
249 
250  Assert(!is_matview); /* excluded by syntax */
251  ExecuteQuery(pstate, estmt, into, params, dest, qc);
252 
253  /* get object address that intorel_startup saved for us */
254  address = ((DR_intorel *) dest)->reladdr;
255 
256  return address;
257  }
258  Assert(query->commandType == CMD_SELECT);
259 
260  /*
261  * For materialized views, always skip data during table creation, and use
262  * REFRESH instead (see below).
263  */
264  if (is_matview)
265  {
266  do_refresh = !into->skipData;
267  into->skipData = true;
268  }
269 
270  if (into->skipData)
271  {
272  /*
273  * If WITH NO DATA was specified, do not go through the rewriter,
274  * planner and executor. Just define the relation using a code path
275  * similar to CREATE VIEW. This avoids dump/restore problems stemming
276  * from running the planner before all dependencies are set up.
277  */
278  address = create_ctas_nodata(query->targetList, into);
279 
280  /*
281  * For materialized views, reuse the REFRESH logic, which locks down
282  * security-restricted operations and restricts the search_path. This
283  * reduces the chance that a subsequent refresh will fail.
284  */
285  if (do_refresh)
286  RefreshMatViewByOid(address.objectId, true, false, false,
287  pstate->p_sourcetext, qc);
288 
289  }
290  else
291  {
292  List *rewritten;
293  PlannedStmt *plan;
294  QueryDesc *queryDesc;
295 
296  Assert(!is_matview);
297 
298  /*
299  * Parse analysis was done already, but we still have to run the rule
300  * rewriter. We do not do AcquireRewriteLocks: we assume the query
301  * either came straight from the parser, or suitable locks were
302  * acquired by plancache.c.
303  */
304  rewritten = QueryRewrite(query);
305 
306  /* SELECT should never rewrite to more or less than one SELECT query */
307  if (list_length(rewritten) != 1)
308  elog(ERROR, "unexpected rewrite result for CREATE TABLE AS SELECT");
309  query = linitial_node(Query, rewritten);
310  Assert(query->commandType == CMD_SELECT);
311 
312  /* plan the query */
313  plan = pg_plan_query(query, pstate->p_sourcetext,
314  CURSOR_OPT_PARALLEL_OK, params);
315 
316  /*
317  * Use a snapshot with an updated command ID to ensure this query sees
318  * results of any previously executed queries. (This could only
319  * matter if the planner executed an allegedly-stable function that
320  * changed the database contents, but let's do it anyway to be
321  * parallel to the EXPLAIN code path.)
322  */
325 
326  /* Create a QueryDesc, redirecting output to our tuple receiver */
327  queryDesc = CreateQueryDesc(plan, pstate->p_sourcetext,
329  dest, params, queryEnv, 0);
330 
331  /* call ExecutorStart to prepare the plan for execution */
332  ExecutorStart(queryDesc, GetIntoRelEFlags(into));
333 
334  /* run the plan to completion */
335  ExecutorRun(queryDesc, ForwardScanDirection, 0, true);
336 
337  /* save the rowcount if we're given a qc to fill */
338  if (qc)
339  SetQueryCompletion(qc, CMDTAG_SELECT, queryDesc->estate->es_processed);
340 
341  /* get object address that intorel_startup saved for us */
342  address = ((DR_intorel *) dest)->reladdr;
343 
344  /* and clean up */
345  ExecutorFinish(queryDesc);
346  ExecutorEnd(queryDesc);
347 
348  FreeQueryDesc(queryDesc);
349 
351  }
352 
353  return address;
354 }
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:383
DestReceiver * CreateIntoRelDestReceiver(IntoClause *intoClause)
Definition: createas.c:430
static ObjectAddress create_ctas_nodata(List *tlist, IntoClause *into)
Definition: createas.c:153
int GetIntoRelEFlags(IntoClause *intoClause)
Definition: createas.c:365
#define elog(elevel,...)
Definition: elog.h:225
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:462
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:402
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:119
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:292
#define stmt
Definition: indent_codes.h:59
ObjectAddress RefreshMatViewByOid(Oid matviewOid, bool is_create, bool skipData, bool concurrent, const char *queryString, QueryCompletion *qc)
Definition: matview.c:165
#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:3281
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:894
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:675
bool skipData
Definition: primnodes.h:170
Definition: pg_list.h:54
const char * p_sourcetext
Definition: parse_node.h:193
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, 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 365 of file createas.c.

366 {
367  int flags = 0;
368 
369  if (intoClause->skipData)
370  flags |= EXEC_FLAG_WITH_NO_DATA;
371 
372  return flags;
373 }
#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().