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 399 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().

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

◆ StoreViewQuery()

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

Definition at line 557 of file view.c.

References DefineViewRules(), and UpdateRangeTableOfViewParse().

Referenced by create_ctas_internal(), and DefineVirtualRelation().

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