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

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

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

517 {
518  /*
519  * Now create the rules associated with the view.
520  */
521  DefineViewRules(viewOid, viewParse, replace);
522 }
static void DefineViewRules(Oid viewOid, Query *viewParse, bool replace)
Definition: view.c:337

References DefineViewRules().

Referenced by create_ctas_internal(), and DefineVirtualRelation().