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

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

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_fixedparams(), 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().

◆ StoreViewQuery()

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

Definition at line 578 of file view.c.

579 {
580  /*
581  * The range table of 'viewParse' does not contain entries for the "OLD"
582  * and "NEW" relations. So... add them!
583  */
584  viewParse = UpdateRangeTableOfViewParse(viewOid, viewParse);
585 
586  /*
587  * Now create the rules associated with the view.
588  */
589  DefineViewRules(viewOid, viewParse, replace);
590 }
static void DefineViewRules(Oid viewOid, Query *viewParse, bool replace)
Definition: view.c:323
static Query * UpdateRangeTableOfViewParse(Oid viewOid, Query *viewParse)
Definition: view.c:360

References DefineViewRules(), and UpdateRangeTableOfViewParse().

Referenced by create_ctas_internal(), and DefineVirtualRelation().