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 407 of file view.c.

References _, ViewStmt::aliases, CASCADED_CHECK_OPTION, CMD_SELECT, Query::commandType, copyObject, DefineVirtualRelation(), DefElem::defname, elog, ereport, errcode(), errhint(), errmsg(), ERROR, Query::hasModifyingCTE, IsA, isQueryUsingTempRelation(), lappend(), lfirst, lfirst_node, list_head(), lnext(), LOCAL_CHECK_OPTION, makeDefElem(), makeNode, makeString(), NIL, NOTICE, ViewStmt::options, parse_analyze(), pstrdup(), ViewStmt::query, RangeVar::relname, RangeVar::relpersistence, ViewStmt::replace, TargetEntry::resjunk, TargetEntry::resname, RawStmt::stmt, RawStmt::stmt_len, RawStmt::stmt_location, strVal, Query::targetList, Query::utilityStmt, ViewStmt::view, view_query_is_auto_updatable(), and ViewStmt::withCheckOption.

Referenced by ProcessUtilitySlow().

409 {
410  RawStmt *rawstmt;
411  Query *viewParse;
412  RangeVar *view;
413  ListCell *cell;
414  bool check_option;
415  ObjectAddress address;
416 
417  /*
418  * Run parse analysis to convert the raw parse tree to a Query. Note this
419  * also acquires sufficient locks on the source table(s).
420  */
421  rawstmt = makeNode(RawStmt);
422  rawstmt->stmt = stmt->query;
423  rawstmt->stmt_location = stmt_location;
424  rawstmt->stmt_len = stmt_len;
425 
426  viewParse = parse_analyze(rawstmt, queryString, NULL, 0, NULL);
427 
428  /*
429  * The grammar should ensure that the result is a single SELECT Query.
430  * However, it doesn't forbid SELECT INTO, so we have to check for that.
431  */
432  if (!IsA(viewParse, Query))
433  elog(ERROR, "unexpected parse analysis result");
434  if (viewParse->utilityStmt != NULL &&
435  IsA(viewParse->utilityStmt, CreateTableAsStmt))
436  ereport(ERROR,
437  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
438  errmsg("views must not contain SELECT INTO")));
439  if (viewParse->commandType != CMD_SELECT)
440  elog(ERROR, "unexpected parse analysis result");
441 
442  /*
443  * Check for unsupported cases. These tests are redundant with ones in
444  * DefineQueryRewrite(), but that function will complain about a bogus ON
445  * SELECT rule, and we'd rather the message complain about a view.
446  */
447  if (viewParse->hasModifyingCTE)
448  ereport(ERROR,
449  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
450  errmsg("views must not contain data-modifying statements in WITH")));
451 
452  /*
453  * If the user specified the WITH CHECK OPTION, add it to the list of
454  * reloptions.
455  */
456  if (stmt->withCheckOption == LOCAL_CHECK_OPTION)
457  stmt->options = lappend(stmt->options,
458  makeDefElem("check_option",
459  (Node *) makeString("local"), -1));
460  else if (stmt->withCheckOption == CASCADED_CHECK_OPTION)
461  stmt->options = lappend(stmt->options,
462  makeDefElem("check_option",
463  (Node *) makeString("cascaded"), -1));
464 
465  /*
466  * Check that the view is auto-updatable if WITH CHECK OPTION was
467  * specified.
468  */
469  check_option = false;
470 
471  foreach(cell, stmt->options)
472  {
473  DefElem *defel = (DefElem *) lfirst(cell);
474 
475  if (strcmp(defel->defname, "check_option") == 0)
476  check_option = true;
477  }
478 
479  /*
480  * If the check option is specified, look to see if the view is actually
481  * auto-updatable or not.
482  */
483  if (check_option)
484  {
485  const char *view_updatable_error =
486  view_query_is_auto_updatable(viewParse, true);
487 
488  if (view_updatable_error)
489  ereport(ERROR,
490  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
491  errmsg("WITH CHECK OPTION is supported only on automatically updatable views"),
492  errhint("%s", _(view_updatable_error))));
493  }
494 
495  /*
496  * If a list of column names was given, run through and insert these into
497  * the actual query tree. - thomas 2000-03-08
498  */
499  if (stmt->aliases != NIL)
500  {
501  ListCell *alist_item = list_head(stmt->aliases);
502  ListCell *targetList;
503 
504  foreach(targetList, viewParse->targetList)
505  {
506  TargetEntry *te = lfirst_node(TargetEntry, targetList);
507 
508  /* junk columns don't get aliases */
509  if (te->resjunk)
510  continue;
511  te->resname = pstrdup(strVal(lfirst(alist_item)));
512  alist_item = lnext(stmt->aliases, alist_item);
513  if (alist_item == NULL)
514  break; /* done assigning aliases */
515  }
516 
517  if (alist_item != NULL)
518  ereport(ERROR,
519  (errcode(ERRCODE_SYNTAX_ERROR),
520  errmsg("CREATE VIEW specifies more column "
521  "names than columns")));
522  }
523 
524  /* Unlogged views are not sensible. */
525  if (stmt->view->relpersistence == RELPERSISTENCE_UNLOGGED)
526  ereport(ERROR,
527  (errcode(ERRCODE_SYNTAX_ERROR),
528  errmsg("views cannot be unlogged because they do not have storage")));
529 
530  /*
531  * If the user didn't explicitly ask for a temporary view, check whether
532  * we need one implicitly. We allow TEMP to be inserted automatically as
533  * long as the CREATE command is consistent with that --- no explicit
534  * schema name.
535  */
536  view = copyObject(stmt->view); /* don't corrupt original command */
537  if (view->relpersistence == RELPERSISTENCE_PERMANENT
538  && isQueryUsingTempRelation(viewParse))
539  {
540  view->relpersistence = RELPERSISTENCE_TEMP;
541  ereport(NOTICE,
542  (errmsg("view \"%s\" will be a temporary view",
543  view->relname)));
544  }
545 
546  /*
547  * Create the view relation
548  *
549  * NOTE: if it already exists and replace is false, the xact will be
550  * aborted.
551  */
552  address = DefineVirtualRelation(view, viewParse->targetList,
553  stmt->replace, stmt->options, viewParse);
554 
555  return address;
556 }
bool replace
Definition: parsenodes.h:3269
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
int errhint(const char *fmt,...)
Definition: elog.c:1156
bool isQueryUsingTempRelation(Query *query)
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:322
char * pstrdup(const char *in)
Definition: mcxt.c:1299
Definition: nodes.h:536
#define strVal(v)
Definition: value.h:65
int errcode(int sqlerrcode)
Definition: elog.c:698
String * makeString(char *str)
Definition: value.c:51
char * resname
Definition: primnodes.h:1457
Node * utilityStmt
Definition: parsenodes.h:128
DefElem * makeDefElem(char *name, Node *arg, int location)
Definition: makefuncs.c:546
RangeVar * view
Definition: parsenodes.h:3266
List * targetList
Definition: parsenodes.h:150
char * relname
Definition: primnodes.h:68
bool resjunk
Definition: primnodes.h:1462
#define ERROR
Definition: elog.h:46
List * aliases
Definition: parsenodes.h:3267
#define lfirst_node(type, lc)
Definition: pg_list.h:172
Node * stmt
Definition: parsenodes.h:1572
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
List * lappend(List *list, void *datum)
Definition: list.c:336
int stmt_len
Definition: parsenodes.h:1574
int stmt_location
Definition: parsenodes.h:1573
#define ereport(elevel,...)
Definition: elog.h:157
Query * parse_analyze(RawStmt *parseTree, const char *sourceText, Oid *paramTypes, int numParams, QueryEnvironment *queryEnv)
Definition: analyze.c:110
#define NOTICE
Definition: elog.h:37
CmdType commandType
Definition: parsenodes.h:120
#define makeNode(_type_)
Definition: nodes.h:584
List * options
Definition: parsenodes.h:3270
#define lfirst(lc)
Definition: pg_list.h:169
static ObjectAddress DefineVirtualRelation(RangeVar *relation, List *tlist, bool replace, List *options, Query *viewParse)
Definition: view.c:50
char relpersistence
Definition: primnodes.h:71
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
bool hasModifyingCTE
Definition: parsenodes.h:139
char * defname
Definition: parsenodes.h:758
ViewCheckOption withCheckOption
Definition: parsenodes.h:3271
Node * query
Definition: parsenodes.h:3268
#define copyObject(obj)
Definition: nodes.h:652
const char * view_query_is_auto_updatable(Query *viewquery, bool check_cols)
#define _(x)
Definition: elog.c:89

◆ StoreViewQuery()

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

Definition at line 562 of file view.c.

References DefineViewRules(), and UpdateRangeTableOfViewParse().

Referenced by create_ctas_internal(), and DefineVirtualRelation().

563 {
564  /*
565  * The range table of 'viewParse' does not contain entries for the "OLD"
566  * and "NEW" relations. So... add them!
567  */
568  viewParse = UpdateRangeTableOfViewParse(viewOid, viewParse);
569 
570  /*
571  * Now create the rules associated with the view.
572  */
573  DefineViewRules(viewOid, viewParse, replace);
574 }
static Query * UpdateRangeTableOfViewParse(Oid viewOid, Query *viewParse)
Definition: view.c:344
static void DefineViewRules(Oid viewOid, Query *viewParse, bool replace)
Definition: view.c:307