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

Go to the source code of this file.

Functions

ObjectAddress DefineView (ViewStmt *stmt, const char *queryString, int stmt_location, int stmt_len)
 
void StoreViewQuery (Oid viewOid, Query *viewParse, bool replace)
 

Function Documentation

◆ DefineView()

ObjectAddress DefineView ( ViewStmt stmt,
const char *  queryString,
int  stmt_location,
int  stmt_len 
)

Definition at line 356 of file view.c.

358 {
359  RawStmt *rawstmt;
360  Query *viewParse;
361  RangeVar *view;
362  ListCell *cell;
363  bool check_option;
364  ObjectAddress address;
365 
366  /*
367  * Run parse analysis to convert the raw parse tree to a Query. Note this
368  * also acquires sufficient locks on the source table(s).
369  */
370  rawstmt = makeNode(RawStmt);
371  rawstmt->stmt = stmt->query;
372  rawstmt->stmt_location = stmt_location;
373  rawstmt->stmt_len = stmt_len;
374 
375  viewParse = parse_analyze_fixedparams(rawstmt, queryString, NULL, 0, NULL);
376 
377  /*
378  * The grammar should ensure that the result is a single SELECT Query.
379  * However, it doesn't forbid SELECT INTO, so we have to check for that.
380  */
381  if (!IsA(viewParse, Query))
382  elog(ERROR, "unexpected parse analysis result");
383  if (viewParse->utilityStmt != NULL &&
384  IsA(viewParse->utilityStmt, CreateTableAsStmt))
385  ereport(ERROR,
386  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
387  errmsg("views must not contain SELECT INTO")));
388  if (viewParse->commandType != CMD_SELECT)
389  elog(ERROR, "unexpected parse analysis result");
390 
391  /*
392  * Check for unsupported cases. These tests are redundant with ones in
393  * DefineQueryRewrite(), but that function will complain about a bogus ON
394  * SELECT rule, and we'd rather the message complain about a view.
395  */
396  if (viewParse->hasModifyingCTE)
397  ereport(ERROR,
398  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
399  errmsg("views must not contain data-modifying statements in WITH")));
400 
401  /*
402  * If the user specified the WITH CHECK OPTION, add it to the list of
403  * reloptions.
404  */
405  if (stmt->withCheckOption == LOCAL_CHECK_OPTION)
406  stmt->options = lappend(stmt->options,
407  makeDefElem("check_option",
408  (Node *) makeString("local"), -1));
409  else if (stmt->withCheckOption == CASCADED_CHECK_OPTION)
410  stmt->options = lappend(stmt->options,
411  makeDefElem("check_option",
412  (Node *) makeString("cascaded"), -1));
413 
414  /*
415  * Check that the view is auto-updatable if WITH CHECK OPTION was
416  * specified.
417  */
418  check_option = false;
419 
420  foreach(cell, stmt->options)
421  {
422  DefElem *defel = (DefElem *) lfirst(cell);
423 
424  if (strcmp(defel->defname, "check_option") == 0)
425  check_option = true;
426  }
427 
428  /*
429  * If the check option is specified, look to see if the view is actually
430  * auto-updatable or not.
431  */
432  if (check_option)
433  {
434  const char *view_updatable_error =
435  view_query_is_auto_updatable(viewParse, true);
436 
437  if (view_updatable_error)
438  ereport(ERROR,
439  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
440  errmsg("WITH CHECK OPTION is supported only on automatically updatable views"),
441  errhint("%s", _(view_updatable_error))));
442  }
443 
444  /*
445  * If a list of column names was given, run through and insert these into
446  * the actual query tree. - thomas 2000-03-08
447  */
448  if (stmt->aliases != NIL)
449  {
450  ListCell *alist_item = list_head(stmt->aliases);
451  ListCell *targetList;
452 
453  foreach(targetList, viewParse->targetList)
454  {
455  TargetEntry *te = lfirst_node(TargetEntry, targetList);
456 
457  /* junk columns don't get aliases */
458  if (te->resjunk)
459  continue;
460  te->resname = pstrdup(strVal(lfirst(alist_item)));
461  alist_item = lnext(stmt->aliases, alist_item);
462  if (alist_item == NULL)
463  break; /* done assigning aliases */
464  }
465 
466  if (alist_item != NULL)
467  ereport(ERROR,
468  (errcode(ERRCODE_SYNTAX_ERROR),
469  errmsg("CREATE VIEW specifies more column "
470  "names than columns")));
471  }
472 
473  /* Unlogged views are not sensible. */
474  if (stmt->view->relpersistence == RELPERSISTENCE_UNLOGGED)
475  ereport(ERROR,
476  (errcode(ERRCODE_SYNTAX_ERROR),
477  errmsg("views cannot be unlogged because they do not have storage")));
478 
479  /*
480  * If the user didn't explicitly ask for a temporary view, check whether
481  * we need one implicitly. We allow TEMP to be inserted automatically as
482  * long as the CREATE command is consistent with that --- no explicit
483  * schema name.
484  */
485  view = copyObject(stmt->view); /* don't corrupt original command */
486  if (view->relpersistence == RELPERSISTENCE_PERMANENT
487  && isQueryUsingTempRelation(viewParse))
488  {
489  view->relpersistence = RELPERSISTENCE_TEMP;
490  ereport(NOTICE,
491  (errmsg("view \"%s\" will be a temporary view",
492  view->relname)));
493  }
494 
495  /*
496  * Create the view relation
497  *
498  * NOTE: if it already exists and replace is false, the xact will be
499  * aborted.
500  */
501  address = DefineVirtualRelation(view, viewParse->targetList,
502  stmt->replace, stmt->options, viewParse);
503 
504  return address;
505 }
int errhint(const char *fmt,...)
Definition: elog.c:1319
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define _(x)
Definition: elog.c:90
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define NOTICE
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:149
#define stmt
Definition: indent_codes.h:59
List * lappend(List *list, void *datum)
Definition: list.c:339
DefElem * makeDefElem(char *name, Node *arg, int location)
Definition: makefuncs.c:564
char * pstrdup(const char *in)
Definition: mcxt.c:1695
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
#define copyObject(obj)
Definition: nodes.h:224
@ CMD_SELECT
Definition: nodes.h:265
#define makeNode(_type_)
Definition: nodes.h:155
bool isQueryUsingTempRelation(Query *query)
@ CASCADED_CHECK_OPTION
Definition: parsenodes.h:3749
@ LOCAL_CHECK_OPTION
Definition: parsenodes.h:3748
Query * parse_analyze_fixedparams(RawStmt *parseTree, const char *sourceText, const Oid *paramTypes, int numParams, QueryEnvironment *queryEnv)
Definition: analyze.c:104
#define lfirst(lc)
Definition: pg_list.h:172
#define lfirst_node(type, lc)
Definition: pg_list.h:176
#define NIL
Definition: pg_list.h:68
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
const char * view_query_is_auto_updatable(Query *viewquery, bool check_cols)
char * defname
Definition: parsenodes.h:815
Definition: nodes.h:129
CmdType commandType
Definition: parsenodes.h:121
Node * utilityStmt
Definition: parsenodes.h:136
List * targetList
Definition: parsenodes.h:191
char * relname
Definition: primnodes.h:82
char relpersistence
Definition: primnodes.h:88
ParseLoc stmt_location
Definition: parsenodes.h:2028
ParseLoc stmt_len
Definition: parsenodes.h:2029
Node * stmt
Definition: parsenodes.h:2027
String * makeString(char *str)
Definition: value.c:63
#define strVal(v)
Definition: value.h:82
static ObjectAddress DefineVirtualRelation(RangeVar *relation, List *tlist, bool replace, List *options, Query *viewParse)
Definition: view.c:45

References _, CASCADED_CHECK_OPTION, CMD_SELECT, Query::commandType, copyObject, DefineVirtualRelation(), DefElem::defname, elog, ereport, errcode(), errhint(), errmsg(), ERROR, IsA, isQueryUsingTempRelation(), lappend(), lfirst, lfirst_node, list_head(), lnext(), LOCAL_CHECK_OPTION, makeDefElem(), makeNode, makeString(), NIL, NOTICE, parse_analyze_fixedparams(), pstrdup(), RangeVar::relname, RangeVar::relpersistence, RawStmt::stmt, stmt, RawStmt::stmt_len, RawStmt::stmt_location, strVal, Query::targetList, Query::utilityStmt, and view_query_is_auto_updatable().

Referenced by ProcessUtilitySlow().

◆ StoreViewQuery()

void StoreViewQuery ( Oid  viewOid,
Query viewParse,
bool  replace 
)

Definition at line 511 of file view.c.

512 {
513  /*
514  * Now create the rules associated with the view.
515  */
516  DefineViewRules(viewOid, viewParse, replace);
517 }
static void DefineViewRules(Oid viewOid, Query *viewParse, bool replace)
Definition: view.c:332

References DefineViewRules().

Referenced by create_ctas_internal(), and DefineVirtualRelation().