PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
view.c File Reference
#include "postgres.h"
#include "access/heapam.h"
#include "access/xact.h"
#include "catalog/namespace.h"
#include "commands/defrem.h"
#include "commands/tablecmds.h"
#include "commands/view.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "parser/analyze.h"
#include "parser/parse_relation.h"
#include "rewrite/rewriteDefine.h"
#include "rewrite/rewriteManip.h"
#include "rewrite/rewriteHandler.h"
#include "rewrite/rewriteSupport.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
Include dependency graph for view.c:

Go to the source code of this file.

Functions

static void checkViewTupleDesc (TupleDesc newdesc, TupleDesc olddesc)
 
void validateWithCheckOption (char *value)
 
static ObjectAddress DefineVirtualRelation (RangeVar *relation, List *tlist, bool replace, List *options, Query *viewParse)
 
static void DefineViewRules (Oid viewOid, Query *viewParse, bool replace)
 
static QueryUpdateRangeTableOfViewParse (Oid viewOid, Query *viewParse)
 
ObjectAddress DefineView (ViewStmt *stmt, const char *queryString, int stmt_location, int stmt_len)
 
void StoreViewQuery (Oid viewOid, Query *viewParse, bool replace)
 

Function Documentation

static void checkViewTupleDesc ( TupleDesc  newdesc,
TupleDesc  olddesc 
)
static

Definition at line 274 of file view.c.

References tupleDesc::attrs, ereport, errcode(), errmsg(), ERROR, format_type_with_typemod(), i, NameStr, and tupleDesc::natts.

Referenced by DefineVirtualRelation().

275 {
276  int i;
277 
278  if (newdesc->natts < olddesc->natts)
279  ereport(ERROR,
280  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
281  errmsg("cannot drop columns from view")));
282  /* we can ignore tdhasoid */
283 
284  for (i = 0; i < olddesc->natts; i++)
285  {
286  Form_pg_attribute newattr = newdesc->attrs[i];
287  Form_pg_attribute oldattr = olddesc->attrs[i];
288 
289  /* XXX msg not right, but we don't support DROP COL on view anyway */
290  if (newattr->attisdropped != oldattr->attisdropped)
291  ereport(ERROR,
292  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
293  errmsg("cannot drop columns from view")));
294 
295  if (strcmp(NameStr(newattr->attname), NameStr(oldattr->attname)) != 0)
296  ereport(ERROR,
297  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
298  errmsg("cannot change name of view column \"%s\" to \"%s\"",
299  NameStr(oldattr->attname),
300  NameStr(newattr->attname))));
301  /* XXX would it be safe to allow atttypmod to change? Not sure */
302  if (newattr->atttypid != oldattr->atttypid ||
303  newattr->atttypmod != oldattr->atttypmod)
304  ereport(ERROR,
305  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
306  errmsg("cannot change data type of view column \"%s\" from %s to %s",
307  NameStr(oldattr->attname),
308  format_type_with_typemod(oldattr->atttypid,
309  oldattr->atttypmod),
310  format_type_with_typemod(newattr->atttypid,
311  newattr->atttypmod))));
312  /* We can ignore the remaining attributes of an attribute... */
313  }
314 
315  /*
316  * We ignore the constraint fields. The new view desc can't have any
317  * constraints, and the only ones that could be on the old view are
318  * defaults, which we are happy to leave in place.
319  */
320 }
Form_pg_attribute * attrs
Definition: tupdesc.h:74
int errcode(int sqlerrcode)
Definition: elog.c:575
int natts
Definition: tupdesc.h:73
#define ERROR
Definition: elog.h:43
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
#define ereport(elevel, rest)
Definition: elog.h:122
char * format_type_with_typemod(Oid type_oid, int32 typemod)
Definition: format_type.c:113
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define NameStr(name)
Definition: c.h:499
ObjectAddress DefineView ( ViewStmt stmt,
const char *  queryString,
int  stmt_location,
int  stmt_len 
)

Definition at line 417 of file view.c.

References ViewStmt::aliases, CASCADED_CHECK_OPTION, castNode, CMD_SELECT, Query::commandType, copyObject(), DefineVirtualRelation(), DefElem::defname, elog, ereport, errcode(), errhint(), errmsg(), ERROR, Query::hasModifyingCTE, IsA, isQueryUsingTempRelation(), lappend(), lfirst, list_head(), lnext, LOCAL_CHECK_OPTION, makeDefElem(), makeNode, makeString(), NIL, NOTICE, NULL, ViewStmt::options, parse_analyze(), pg_strcasecmp(), pstrdup(), ViewStmt::query, RangeVar::relname, RangeVar::relpersistence, RELPERSISTENCE_PERMANENT, RELPERSISTENCE_TEMP, RELPERSISTENCE_UNLOGGED, 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().

419 {
420  RawStmt *rawstmt;
421  Query *viewParse;
422  RangeVar *view;
423  ListCell *cell;
424  bool check_option;
425  ObjectAddress address;
426 
427  /*
428  * Run parse analysis to convert the raw parse tree to a Query. Note this
429  * also acquires sufficient locks on the source table(s).
430  *
431  * Since parse analysis scribbles on its input, copy the raw parse tree;
432  * this ensures we don't corrupt a prepared statement, for example.
433  */
434  rawstmt = makeNode(RawStmt);
435  rawstmt->stmt = (Node *) copyObject(stmt->query);
436  rawstmt->stmt_location = stmt_location;
437  rawstmt->stmt_len = stmt_len;
438 
439  viewParse = parse_analyze(rawstmt, queryString, NULL, 0);
440 
441  /*
442  * The grammar should ensure that the result is a single SELECT Query.
443  * However, it doesn't forbid SELECT INTO, so we have to check for that.
444  */
445  if (!IsA(viewParse, Query))
446  elog(ERROR, "unexpected parse analysis result");
447  if (viewParse->utilityStmt != NULL &&
448  IsA(viewParse->utilityStmt, CreateTableAsStmt))
449  ereport(ERROR,
450  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
451  errmsg("views must not contain SELECT INTO")));
452  if (viewParse->commandType != CMD_SELECT)
453  elog(ERROR, "unexpected parse analysis result");
454 
455  /*
456  * Check for unsupported cases. These tests are redundant with ones in
457  * DefineQueryRewrite(), but that function will complain about a bogus ON
458  * SELECT rule, and we'd rather the message complain about a view.
459  */
460  if (viewParse->hasModifyingCTE)
461  ereport(ERROR,
462  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
463  errmsg("views must not contain data-modifying statements in WITH")));
464 
465  /*
466  * If the user specified the WITH CHECK OPTION, add it to the list of
467  * reloptions.
468  */
469  if (stmt->withCheckOption == LOCAL_CHECK_OPTION)
470  stmt->options = lappend(stmt->options,
471  makeDefElem("check_option",
472  (Node *) makeString("local"), -1));
473  else if (stmt->withCheckOption == CASCADED_CHECK_OPTION)
474  stmt->options = lappend(stmt->options,
475  makeDefElem("check_option",
476  (Node *) makeString("cascaded"), -1));
477 
478  /*
479  * Check that the view is auto-updatable if WITH CHECK OPTION was
480  * specified.
481  */
482  check_option = false;
483 
484  foreach(cell, stmt->options)
485  {
486  DefElem *defel = (DefElem *) lfirst(cell);
487 
488  if (pg_strcasecmp(defel->defname, "check_option") == 0)
489  check_option = true;
490  }
491 
492  /*
493  * If the check option is specified, look to see if the view is actually
494  * auto-updatable or not.
495  */
496  if (check_option)
497  {
498  const char *view_updatable_error =
499  view_query_is_auto_updatable(viewParse, true);
500 
501  if (view_updatable_error)
502  ereport(ERROR,
503  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
504  errmsg("WITH CHECK OPTION is supported only on automatically updatable views"),
505  errhint("%s", view_updatable_error)));
506  }
507 
508  /*
509  * If a list of column names was given, run through and insert these into
510  * the actual query tree. - thomas 2000-03-08
511  */
512  if (stmt->aliases != NIL)
513  {
514  ListCell *alist_item = list_head(stmt->aliases);
515  ListCell *targetList;
516 
517  foreach(targetList, viewParse->targetList)
518  {
519  TargetEntry *te = castNode(TargetEntry, lfirst(targetList));
520 
521  /* junk columns don't get aliases */
522  if (te->resjunk)
523  continue;
524  te->resname = pstrdup(strVal(lfirst(alist_item)));
525  alist_item = lnext(alist_item);
526  if (alist_item == NULL)
527  break; /* done assigning aliases */
528  }
529 
530  if (alist_item != NULL)
531  ereport(ERROR,
532  (errcode(ERRCODE_SYNTAX_ERROR),
533  errmsg("CREATE VIEW specifies more column "
534  "names than columns")));
535  }
536 
537  /* Unlogged views are not sensible. */
539  ereport(ERROR,
540  (errcode(ERRCODE_SYNTAX_ERROR),
541  errmsg("views cannot be unlogged because they do not have storage")));
542 
543  /*
544  * If the user didn't explicitly ask for a temporary view, check whether
545  * we need one implicitly. We allow TEMP to be inserted automatically as
546  * long as the CREATE command is consistent with that --- no explicit
547  * schema name.
548  */
549  view = copyObject(stmt->view); /* don't corrupt original command */
551  && isQueryUsingTempRelation(viewParse))
552  {
554  ereport(NOTICE,
555  (errmsg("view \"%s\" will be a temporary view",
556  view->relname)));
557  }
558 
559  /*
560  * Create the view relation
561  *
562  * NOTE: if it already exists and replace is false, the xact will be
563  * aborted.
564  */
565  address = DefineVirtualRelation(view, viewParse->targetList,
566  stmt->replace, stmt->options, viewParse);
567 
568  return address;
569 }
Value * makeString(char *str)
Definition: value.c:53
bool replace
Definition: parsenodes.h:2918
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:569
int errhint(const char *fmt,...)
Definition: elog.c:987
bool isQueryUsingTempRelation(Query *query)
#define RELPERSISTENCE_UNLOGGED
Definition: pg_class.h:171
#define castNode(_type_, nodeptr)
Definition: nodes.h:587
char * pstrdup(const char *in)
Definition: mcxt.c:1077
Definition: nodes.h:518
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
char * resname
Definition: primnodes.h:1354
Node * utilityStmt
Definition: parsenodes.h:111
DefElem * makeDefElem(char *name, Node *arg, int location)
Definition: makefuncs.c:543
RangeVar * view
Definition: parsenodes.h:2915
List * targetList
Definition: parsenodes.h:131
Query * parse_analyze(RawStmt *parseTree, const char *sourceText, Oid *paramTypes, int numParams)
Definition: analyze.c:96
void * copyObject(const void *from)
Definition: copyfuncs.c:4592
#define RELPERSISTENCE_PERMANENT
Definition: pg_class.h:170
char * relname
Definition: primnodes.h:68
bool resjunk
Definition: primnodes.h:1359
#define ERROR
Definition: elog.h:43
List * aliases
Definition: parsenodes.h:2916
Node * stmt
Definition: parsenodes.h:1376
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
#define lnext(lc)
Definition: pg_list.h:105
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
int stmt_len
Definition: parsenodes.h:1378
int stmt_location
Definition: parsenodes.h:1377
#define NOTICE
Definition: elog.h:37
CmdType commandType
Definition: parsenodes.h:103
#define makeNode(_type_)
Definition: nodes.h:566
#define NULL
Definition: c.h:229
List * options
Definition: parsenodes.h:2919
#define lfirst(lc)
Definition: pg_list.h:106
static ObjectAddress DefineVirtualRelation(RangeVar *relation, List *tlist, bool replace, List *options, Query *viewParse)
Definition: view.c:67
char relpersistence
Definition: primnodes.h:71
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool hasModifyingCTE
Definition: parsenodes.h:122
char * defname
Definition: parsenodes.h:708
ViewCheckOption withCheckOption
Definition: parsenodes.h:2920
#define elog
Definition: elog.h:219
Node * query
Definition: parsenodes.h:2917
#define RELPERSISTENCE_TEMP
Definition: pg_class.h:172
const char * view_query_is_auto_updatable(Query *viewquery, bool check_cols)
static void DefineViewRules ( Oid  viewOid,
Query viewParse,
bool  replace 
)
static

Definition at line 323 of file view.c.

References CMD_SELECT, DefineQueryRewrite(), list_make1, NULL, pstrdup(), and ViewSelectRuleName.

Referenced by StoreViewQuery().

324 {
325  /*
326  * Set up the ON SELECT rule. Since the query has already been through
327  * parse analysis, we use DefineQueryRewrite() directly.
328  */
330  viewOid,
331  NULL,
332  CMD_SELECT,
333  true,
334  replace,
335  list_make1(viewParse));
336 
337  /*
338  * Someday: automatic ON INSERT, etc
339  */
340 }
char * pstrdup(const char *in)
Definition: mcxt.c:1077
#define ViewSelectRuleName
#define list_make1(x1)
Definition: pg_list.h:133
ObjectAddress DefineQueryRewrite(char *rulename, Oid event_relid, Node *event_qual, CmdType event_type, bool is_instead, bool replace, List *action)
#define NULL
Definition: c.h:229
static ObjectAddress DefineVirtualRelation ( RangeVar relation,
List tlist,
bool  replace,
List options,
Query viewParse 
)
static

Definition at line 67 of file view.c.

References AccessExclusiveLock, AlterTableInternal(), Assert, AT_AddColumnToView, AT_ReplaceRelOptions, BuildDescForRelation(), CheckTableNotInUse(), checkViewTupleDesc(), ColumnDef::collOid, ColumnDef::colname, CommandCounterIncrement(), CreateStmt::constraints, AlterTableCmd::def, DefineRelation(), ereport, errcode(), errhint(), errmsg(), ERROR, TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), CreateStmt::if_not_exists, CreateStmt::inhRelations, InvalidOid, lappend(), lfirst, list_length(), list_make1, makeColumnDef(), makeNode, tupleDesc::natts, NIL, NoLock, NULL, ObjectAddressSet, ObjectAddress::objectId, OidIsValid, CreateStmt::oncommit, ONCOMMIT_NOOP, options, CreateStmt::options, RangeVarGetAndCheckCreationNamespace(), RelationData::rd_att, RelationData::rd_rel, CreateStmt::relation, relation_close(), relation_open(), RelationGetRelationName, RelationRelationId, RELKIND_VIEW, RangeVar::relpersistence, TargetEntry::resjunk, TargetEntry::resname, skip(), StoreViewQuery(), AlterTableCmd::subtype, CreateStmt::tableElts, CreateStmt::tablespacename, and type_is_collatable().

Referenced by DefineView().

69 {
70  Oid viewOid;
71  LOCKMODE lockmode;
72  CreateStmt *createStmt = makeNode(CreateStmt);
73  List *attrList;
74  ListCell *t;
75 
76  /*
77  * create a list of ColumnDef nodes based on the names and types of the
78  * (non-junk) targetlist items from the view's SELECT list.
79  */
80  attrList = NIL;
81  foreach(t, tlist)
82  {
83  TargetEntry *tle = (TargetEntry *) lfirst(t);
84 
85  if (!tle->resjunk)
86  {
87  ColumnDef *def = makeColumnDef(tle->resname,
88  exprType((Node *) tle->expr),
89  exprTypmod((Node *) tle->expr),
90  exprCollation((Node *) tle->expr));
91 
92  /*
93  * It's possible that the column is of a collatable type but the
94  * collation could not be resolved, so double-check.
95  */
96  if (type_is_collatable(exprType((Node *) tle->expr)))
97  {
98  if (!OidIsValid(def->collOid))
99  ereport(ERROR,
100  (errcode(ERRCODE_INDETERMINATE_COLLATION),
101  errmsg("could not determine which collation to use for view column \"%s\"",
102  def->colname),
103  errhint("Use the COLLATE clause to set the collation explicitly.")));
104  }
105  else
106  Assert(!OidIsValid(def->collOid));
107 
108  attrList = lappend(attrList, def);
109  }
110  }
111 
112  if (attrList == NIL)
113  ereport(ERROR,
114  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
115  errmsg("view must have at least one column")));
116 
117  /*
118  * Look up, check permissions on, and lock the creation namespace; also
119  * check for a preexisting view with the same name. This will also set
120  * relation->relpersistence to RELPERSISTENCE_TEMP if the selected
121  * namespace is temporary.
122  */
123  lockmode = replace ? AccessExclusiveLock : NoLock;
124  (void) RangeVarGetAndCheckCreationNamespace(relation, lockmode, &viewOid);
125 
126  if (OidIsValid(viewOid) && replace)
127  {
128  Relation rel;
130  List *atcmds = NIL;
131  AlterTableCmd *atcmd;
132  ObjectAddress address;
133 
134  /* Relation is already locked, but we must build a relcache entry. */
135  rel = relation_open(viewOid, NoLock);
136 
137  /* Make sure it *is* a view. */
138  if (rel->rd_rel->relkind != RELKIND_VIEW)
139  ereport(ERROR,
140  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
141  errmsg("\"%s\" is not a view",
142  RelationGetRelationName(rel))));
143 
144  /* Also check it's not in use already */
145  CheckTableNotInUse(rel, "CREATE OR REPLACE VIEW");
146 
147  /*
148  * Due to the namespace visibility rules for temporary objects, we
149  * should only end up replacing a temporary view with another
150  * temporary view, and similarly for permanent views.
151  */
152  Assert(relation->relpersistence == rel->rd_rel->relpersistence);
153 
154  /*
155  * Create a tuple descriptor to compare against the existing view, and
156  * verify that the old column list is an initial prefix of the new
157  * column list.
158  */
159  descriptor = BuildDescForRelation(attrList);
160  checkViewTupleDesc(descriptor, rel->rd_att);
161 
162  /*
163  * If new attributes have been added, we must add pg_attribute entries
164  * for them. It is convenient (although overkill) to use the ALTER
165  * TABLE ADD COLUMN infrastructure for this.
166  *
167  * Note that we must do this before updating the query for the view,
168  * since the rules system requires that the correct view columns be in
169  * place when defining the new rules.
170  */
171  if (list_length(attrList) > rel->rd_att->natts)
172  {
173  ListCell *c;
174  int skip = rel->rd_att->natts;
175 
176  foreach(c, attrList)
177  {
178  if (skip > 0)
179  {
180  skip--;
181  continue;
182  }
183  atcmd = makeNode(AlterTableCmd);
184  atcmd->subtype = AT_AddColumnToView;
185  atcmd->def = (Node *) lfirst(c);
186  atcmds = lappend(atcmds, atcmd);
187  }
188 
189  AlterTableInternal(viewOid, atcmds, true);
190 
191  /* Make the new view columns visible */
193  }
194 
195  /*
196  * Update the query for the view.
197  *
198  * Note that we must do this before updating the view options, because
199  * the new options may not be compatible with the old view query (for
200  * example if we attempt to add the WITH CHECK OPTION, we require that
201  * the new view be automatically updatable, but the old view may not
202  * have been).
203  */
204  StoreViewQuery(viewOid, viewParse, replace);
205 
206  /* Make the new view query visible */
208 
209  /*
210  * Finally update the view options.
211  *
212  * The new options list replaces the existing options list, even if
213  * it's empty.
214  */
215  atcmd = makeNode(AlterTableCmd);
216  atcmd->subtype = AT_ReplaceRelOptions;
217  atcmd->def = (Node *) options;
218  atcmds = list_make1(atcmd);
219 
220  AlterTableInternal(viewOid, atcmds, true);
221 
222  ObjectAddressSet(address, RelationRelationId, viewOid);
223 
224  /*
225  * Seems okay, so return the OID of the pre-existing view.
226  */
227  relation_close(rel, NoLock); /* keep the lock! */
228 
229  return address;
230  }
231  else
232  {
233  ObjectAddress address;
234 
235  /*
236  * Set the parameters for keys/inheritance etc. All of these are
237  * uninteresting for views...
238  */
239  createStmt->relation = relation;
240  createStmt->tableElts = attrList;
241  createStmt->inhRelations = NIL;
242  createStmt->constraints = NIL;
243  createStmt->options = options;
244  createStmt->oncommit = ONCOMMIT_NOOP;
245  createStmt->tablespacename = NULL;
246  createStmt->if_not_exists = false;
247 
248  /*
249  * Create the relation (this will error out if there's an existing
250  * view, so we don't need more code to complain if "replace" is
251  * false).
252  */
253  address = DefineRelation(createStmt, RELKIND_VIEW, InvalidOid, NULL,
254  NULL);
255  Assert(address.objectId != InvalidOid);
256 
257  /* Make the new view relation visible */
259 
260  /* Store the query for the view */
261  StoreViewQuery(address.objectId, viewParse, replace);
262 
263  return address;
264  }
265 }
RangeVar * relation
Definition: parsenodes.h:1936
#define NIL
Definition: pg_list.h:69
OnCommitAction oncommit
Definition: parsenodes.h:1945
static void skip(struct vars *v)
Definition: regc_lex.c:1109
List * inhRelations
Definition: parsenodes.h:1938
int errhint(const char *fmt,...)
Definition: elog.c:987
int LOCKMODE
Definition: lockdefs.h:26
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:273
#define RelationRelationId
Definition: pg_class.h:29
Definition: nodes.h:518
int errcode(int sqlerrcode)
Definition: elog.c:575
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1263
AlterTableType subtype
Definition: parsenodes.h:1724
static void checkViewTupleDesc(TupleDesc newdesc, TupleDesc olddesc)
Definition: view.c:274
Form_pg_class rd_rel
Definition: rel.h:113
unsigned int Oid
Definition: postgres_ext.h:31
char * resname
Definition: primnodes.h:1354
#define OidIsValid(objectId)
Definition: c.h:538
int natts
Definition: tupdesc.h:73
List * constraints
Definition: parsenodes.h:1943
#define list_make1(x1)
Definition: pg_list.h:133
bool if_not_exists
Definition: parsenodes.h:1947
bool resjunk
Definition: primnodes.h:1359
#define ERROR
Definition: elog.h:43
char * c
#define NoLock
Definition: lockdefs.h:34
Oid collOid
Definition: parsenodes.h:641
#define RelationGetRelationName(relation)
Definition: rel.h:433
List * options
Definition: parsenodes.h:1944
void CheckTableNotInUse(Relation rel, const char *stmt)
Definition: tablecmds.c:2970
ObjectAddress DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, ObjectAddress *typaddress, const char *queryString)
Definition: tablecmds.c:483
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
char * tablespacename
Definition: parsenodes.h:1946
static char ** options
Oid RangeVarGetAndCheckCreationNamespace(RangeVar *relation, LOCKMODE lockmode, Oid *existing_relation_id)
Definition: namespace.c:507
void CommandCounterIncrement(void)
Definition: xact.c:922
void StoreViewQuery(Oid viewOid, Query *viewParse, bool replace)
Definition: view.c:575
TupleDesc rd_att
Definition: rel.h:114
#define InvalidOid
Definition: postgres_ext.h:36
List * tableElts
Definition: parsenodes.h:1937
#define makeNode(_type_)
Definition: nodes.h:566
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1352
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static int list_length(const List *l)
Definition: pg_list.h:89
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:745
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
char relpersistence
Definition: primnodes.h:71
void AlterTableInternal(Oid relid, List *cmds, bool recurse)
Definition: tablecmds.c:3073
#define AccessExclusiveLock
Definition: lockdefs.h:46
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define RELKIND_VIEW
Definition: pg_class.h:164
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1117
char * colname
Definition: parsenodes.h:631
TupleDesc BuildDescForRelation(List *schema)
Definition: tupdesc.c:675
bool type_is_collatable(Oid typid)
Definition: lsyscache.c:2774
Definition: pg_list.h:45
ColumnDef * makeColumnDef(const char *colname, Oid typeOid, int32 typmod, Oid collOid)
Definition: makefuncs.c:487
void StoreViewQuery ( Oid  viewOid,
Query viewParse,
bool  replace 
)

Definition at line 575 of file view.c.

References DefineViewRules(), and UpdateRangeTableOfViewParse().

Referenced by create_ctas_internal(), and DefineVirtualRelation().

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

Definition at line 360 of file view.c.

References AccessShareLock, addRangeTableEntryForRelation(), copyObject(), lcons(), make_parsestate(), makeAlias(), NIL, NULL, OffsetVarNodes(), relation_close(), relation_open(), RangeTblEntry::requiredPerms, and Query::rtable.

Referenced by StoreViewQuery().

361 {
362  Relation viewRel;
363  List *new_rt;
364  RangeTblEntry *rt_entry1,
365  *rt_entry2;
366  ParseState *pstate;
367 
368  /*
369  * Make a copy of the given parsetree. It's not so much that we don't
370  * want to scribble on our input, it's that the parser has a bad habit of
371  * outputting multiple links to the same subtree for constructs like
372  * BETWEEN, and we mustn't have OffsetVarNodes increment the varno of a
373  * Var node twice. copyObject will expand any multiply-referenced subtree
374  * into multiple copies.
375  */
376  viewParse = (Query *) copyObject(viewParse);
377 
378  /* Create a dummy ParseState for addRangeTableEntryForRelation */
379  pstate = make_parsestate(NULL);
380 
381  /* need to open the rel for addRangeTableEntryForRelation */
382  viewRel = relation_open(viewOid, AccessShareLock);
383 
384  /*
385  * Create the 2 new range table entries and form the new range table...
386  * OLD first, then NEW....
387  */
388  rt_entry1 = addRangeTableEntryForRelation(pstate, viewRel,
389  makeAlias("old", NIL),
390  false, false);
391  rt_entry2 = addRangeTableEntryForRelation(pstate, viewRel,
392  makeAlias("new", NIL),
393  false, false);
394  /* Must override addRangeTableEntry's default access-check flags */
395  rt_entry1->requiredPerms = 0;
396  rt_entry2->requiredPerms = 0;
397 
398  new_rt = lcons(rt_entry1, lcons(rt_entry2, viewParse->rtable));
399 
400  viewParse->rtable = new_rt;
401 
402  /*
403  * Now offset all var nodes by 2, and jointree RT indexes too.
404  */
405  OffsetVarNodes((Node *) viewParse, 2, 0);
406 
408 
409  return viewParse;
410 }
#define NIL
Definition: pg_list.h:69
void OffsetVarNodes(Node *node, int offset, int sublevels_up)
Definition: rewriteManip.c:424
#define AccessShareLock
Definition: lockdefs.h:36
Definition: nodes.h:518
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1263
AclMode requiredPerms
Definition: parsenodes.h:1004
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:44
void * copyObject(const void *from)
Definition: copyfuncs.c:4592
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:384
List * rtable
Definition: parsenodes.h:128
List * lcons(void *datum, List *list)
Definition: list.c:259
#define NULL
Definition: c.h:229
RangeTblEntry * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, Alias *alias, bool inh, bool inFromCl)
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1117
Definition: pg_list.h:45
void validateWithCheckOption ( char *  value)

Definition at line 46 of file view.c.

References ereport, errcode(), errdetail(), errmsg(), ERROR, NULL, and pg_strcasecmp().

47 {
48  if (value == NULL ||
49  (pg_strcasecmp(value, "local") != 0 &&
50  pg_strcasecmp(value, "cascaded") != 0))
51  {
52  ereport(ERROR,
53  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
54  errmsg("invalid value for \"check_option\" option"),
55  errdetail("Valid values are \"local\" and \"cascaded\".")));
56  }
57 }
static struct @76 value
int errcode(int sqlerrcode)
Definition: elog.c:575
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
int errmsg(const char *fmt,...)
Definition: elog.c:797