/* *It'spossiblethatthecolumnisofacollatabletypebutthe *collationcouldnotberesolved,sodouble-check.
*/ if (type_is_collatable(exprType((Node *) tle->expr)))
{ if (!OidIsValid(def->collOid))
ereport(ERROR,
(errcode(ERRCODE_INDETERMINATE_COLLATION),
errmsg("could not determine which collation to use for view column \"%s\"",
def->colname),
errhint("Use the COLLATE clause to set the collation explicitly.")));
} else
Assert(!OidIsValid(def->collOid));
if (OidIsValid(viewOid) && replace)
{
Relation rel;
TupleDesc descriptor;
List *atcmds = NIL;
AlterTableCmd *atcmd;
ObjectAddress address;
/* Relation is already locked, but we must build a relcache entry. */
rel = relation_open(viewOid, NoLock);
/* Make sure it *is* a view. */ if (rel->rd_rel->relkind != RELKIND_VIEW)
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("\"%s\" is not a view",
RelationGetRelationName(rel))));
/* Also check it's not in use already */
CheckTableNotInUse(rel, "CREATE OR REPLACE VIEW");
if (newdesc->natts < olddesc->natts)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
errmsg("cannot drop columns from view")));
for (i = 0; i < olddesc->natts; i++)
{
Form_pg_attribute newattr = TupleDescAttr(newdesc, i);
Form_pg_attribute oldattr = TupleDescAttr(olddesc, i);
/* XXX msg not right, but we don't support DROP COL on view anyway */ if (newattr->attisdropped != oldattr->attisdropped)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
errmsg("cannot drop columns from view")));
if (strcmp(NameStr(newattr->attname), NameStr(oldattr->attname)) != 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
errmsg("cannot change name of view column \"%s\" to \"%s\"",
NameStr(oldattr->attname),
NameStr(newattr->attname)),
errhint("Use ALTER VIEW ... RENAME COLUMN ... to change name of view column instead.")));
/* *Wecannotallowtype,typmod,orcollationtochange,sincethese *propertiesmaybeembeddedinVarsofotherviews/rulesreferencing *thisone.Othercolumnattributescanbeignored.
*/ if (newattr->atttypid != oldattr->atttypid ||
newattr->atttypmod != oldattr->atttypmod)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
errmsg("cannot change data type of view column \"%s\" from %s to %s",
NameStr(oldattr->attname),
format_type_with_typemod(oldattr->atttypid,
oldattr->atttypmod),
format_type_with_typemod(newattr->atttypid,
newattr->atttypmod))));
/* *Atthispoint,attcollationsshouldbebothvalidorbothinvalid, *soapplyingget_collation_nameunconditionallyshouldbefine.
*/ if (newattr->attcollation != oldattr->attcollation)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
errmsg("cannot change collation of view column \"%s\" from \"%s\" to \"%s\"",
NameStr(oldattr->attname),
get_collation_name(oldattr->attcollation),
get_collation_name(newattr->attcollation))));
}
/* *ThegrammarshouldensurethattheresultisasingleSELECTQuery. *However,itdoesn'tforbidSELECTINTO,sowehavetocheckforthat.
*/ if (!IsA(viewParse, Query))
elog(ERROR, "unexpected parse analysis result"); if (viewParse->utilityStmt != NULL &&
IsA(viewParse->utilityStmt, CreateTableAsStmt))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("views must not contain SELECT INTO"))); if (viewParse->commandType != CMD_SELECT)
elog(ERROR, "unexpected parse analysis result");
/* *Checkforunsupportedcases.Thesetestsareredundantwithonesin *DefineQueryRewrite(),butthatfunctionwillcomplainaboutabogusON *SELECTrule,andwe'dratherthemessagecomplainaboutaview.
*/ if (viewParse->hasModifyingCTE)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("views must not contain data-modifying statements in WITH")));
if (view_updatable_error)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("WITH CHECK OPTION is supported only on automatically updatable views"),
errhint("%s", _(view_updatable_error))));
}
/* junk columns don't get aliases */ if (te->resjunk) continue;
te->resname = pstrdup(strVal(lfirst(alist_item)));
alist_item = lnext(stmt->aliases, alist_item); if (alist_item == NULL) break; /* done assigning aliases */
}
if (alist_item != NULL)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("CREATE VIEW specifies more column " "names than columns")));
}
/* Unlogged views are not sensible. */ if (stmt->view->relpersistence == RELPERSISTENCE_UNLOGGED)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("views cannot be unlogged because they do not have storage")));
/* *Iftheuserdidn'texplicitlyaskforatemporaryview,checkwhether *weneedoneimplicitly.WeallowTEMPtobeinsertedautomaticallyas *longastheCREATEcommandisconsistentwiththat---noexplicit *schemaname.
*/
view = copyObject(stmt->view); /* don't corrupt original command */ if (view->relpersistence == RELPERSISTENCE_PERMANENT
&& isQueryUsingTempRelation(viewParse))
{
view->relpersistence = RELPERSISTENCE_TEMP;
ereport(NOTICE,
(errmsg("view \"%s\" will be a temporary view",
view->relname)));
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.