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

◆ StoreViewQuery()

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

Definition at line 565 of file view.c.

References DefineViewRules(), and UpdateRangeTableOfViewParse().

Referenced by create_ctas_internal(), and DefineVirtualRelation().

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