41 bool isSelect,
bool requireColumnNameMatch);
63 bool nulls[Natts_pg_rewrite] = {0};
71 bool is_update =
false;
99 bool replaces[Natts_pg_rewrite] = {0};
104 errmsg(
"rule \"%s\" for relation \"%s\" already exists",
110 replaces[Anum_pg_rewrite_ev_type - 1] =
true;
111 replaces[Anum_pg_rewrite_is_instead - 1] =
true;
112 replaces[Anum_pg_rewrite_ev_qual - 1] =
true;
113 replaces[Anum_pg_rewrite_ev_action - 1] =
true;
129 Anum_pg_rewrite_oid);
150 myself.
classId = RewriteRelationId;
154 referenced.
classId = RelationRelationId;
167 if (event_qual != NULL)
182 return rewriteObjectId;
255 if (event_relation->
rd_rel->relkind != RELKIND_RELATION &&
256 event_relation->
rd_rel->relkind != RELKIND_MATVIEW &&
257 event_relation->
rd_rel->relkind != RELKIND_VIEW &&
258 event_relation->
rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
260 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
261 errmsg(
"relation \"%s\" cannot have rules",
267 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
268 errmsg(
"permission denied: \"%s\" is a system catalog",
284 if (query->resultRelation == 0)
291 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
292 errmsg(
"rule actions on OLD are not implemented"),
293 errhint(
"Use views or triggers instead.")));
296 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
297 errmsg(
"rule actions on NEW are not implemented"),
298 errhint(
"Use triggers instead.")));
308 if (event_relation->
rd_rel->relkind != RELKIND_VIEW &&
309 event_relation->
rd_rel->relkind != RELKIND_MATVIEW)
311 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
312 errmsg(
"relation \"%s\" cannot have ON SELECT rules",
321 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
322 errmsg(
"INSTEAD NOTHING rules on SELECT are not implemented"),
323 errhint(
"Use views instead.")));
330 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
331 errmsg(
"multiple actions for rules on SELECT are not implemented")));
340 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
341 errmsg(
"rules on SELECT must have action INSTEAD SELECT")));
346 if (query->hasModifyingCTE)
348 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
349 errmsg(
"rules on SELECT must not contain data-modifying statements in WITH")));
354 if (event_qual != NULL)
356 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
357 errmsg(
"event qualifications are not implemented for rules on SELECT")));
366 event_relation->
rd_rel->relkind !=
372 if (!replace && event_relation->
rd_rules != NULL)
383 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
384 errmsg(
"\"%s\" is already a view",
403 if (strncmp(rulename,
"_RET", 4) != 0 ||
407 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
408 errmsg(
"view rule for \"%s\" must be named \"%s\"",
424 bool haveReturning =
false;
434 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
435 errmsg(
"cannot have multiple RETURNING lists in a rule")));
436 haveReturning =
true;
437 if (event_qual != NULL)
439 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
440 errmsg(
"RETURNING lists are not supported in conditional rules")));
443 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
444 errmsg(
"RETURNING lists are not supported in non-INSTEAD rules")));
457 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
458 errmsg(
"non-view rule for \"%s\" must not be named \"%s\"",
507 bool requireColumnNameMatch)
513 Assert(isSelect || !requireColumnNameMatch);
516 foreach(tllist, targetList)
528 if (
i > resultDesc->
natts)
530 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
532 errmsg(
"SELECT rule's target list has too many entries") :
533 errmsg(
"RETURNING list has too many entries")));
556 if (attr->attisdropped)
558 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
560 errmsg(
"cannot convert relation containing dropped columns to view") :
561 errmsg(
"cannot create a RETURNING list for a relation containing dropped columns")));
564 if (requireColumnNameMatch && strcmp(tle->resname,
attname) != 0)
566 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
567 errmsg(
"SELECT rule's target entry %d has different column name from column \"%s\"",
569 errdetail(
"SELECT target entry is named \"%s\".",
574 if (attr->atttypid != tletypid)
576 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
578 errmsg(
"SELECT rule's target entry %d has different type from column \"%s\"",
580 errmsg(
"RETURNING list's entry %d has different type from column \"%s\"",
583 errdetail(
"SELECT target entry has type %s, but column has type %s.",
586 errdetail(
"RETURNING list entry has type %s, but column has type %s.",
597 if (attr->atttypmod != tletypmod &&
598 attr->atttypmod != -1 && tletypmod != -1)
600 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
602 errmsg(
"SELECT rule's target entry %d has different size from column \"%s\"",
604 errmsg(
"RETURNING list's entry %d has different size from column \"%s\"",
607 errdetail(
"SELECT target entry has type %s, but column has type %s.",
611 errdetail(
"RETURNING list entry has type %s, but column has type %s.",
617 if (
i != resultDesc->
natts)
619 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
621 errmsg(
"SELECT rule's target list has too few entries") :
622 errmsg(
"RETURNING list has too few entries")));
656 foreach(l, qry->rteperminfos)
681 if (qry->hasSubLinks)
696 Oid eventRelationOid;
699 bool changed =
false;
710 (
errcode(ERRCODE_UNDEFINED_OBJECT),
711 errmsg(
"rule \"%s\" for relation \"%s\" does not exist",
719 eventRelationOid = ruleform->ev_class;
720 Assert(eventRelationOid == owningRel);
768 if (form->relkind != RELKIND_RELATION &&
769 form->relkind != RELKIND_VIEW &&
770 form->relkind != RELKIND_PARTITIONED_TABLE)
772 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
778 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
779 errmsg(
"permission denied: \"%s\" is a system catalog",
825 (
errcode(ERRCODE_UNDEFINED_OBJECT),
826 errmsg(
"rule \"%s\" for relation \"%s\" does not exist",
829 ruleOid = ruleform->oid;
835 errmsg(
"rule \"%s\" for relation \"%s\" already exists",
844 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
845 errmsg(
"renaming an ON SELECT rule is not allowed")));
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
static Datum values[MAXATTR]
#define CStringGetTextDatum(s)
#define Assert(condition)
bool IsSystemRelation(Relation relation)
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
bool IsSystemClass(Oid relid, Form_pg_class reltuple)
void recordDependencyOnExpr(const ObjectAddress *depender, Node *expr, List *rtable, DependencyType behavior)
int errdetail(const char *fmt,...)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
bool allowSystemTableMods
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
void heap_freetuple(HeapTuple htup)
#define HeapTupleIsValid(tuple)
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
void CacheInvalidateRelcache(Relation relation)
#define AccessExclusiveLock
char get_rel_relkind(Oid relid)
char * get_rel_name(Oid relid)
char * pstrdup(const char *in)
void namestrcpy(Name name, const char *str)
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Oid exprType(const Node *expr)
int32 exprTypmod(const Node *expr)
#define query_tree_walker(q, w, c, f)
#define expression_tree_walker(n, w, c)
#define QTW_IGNORE_RC_SUBQUERIES
#define IsA(nodeptr, _type_)
#define castNode(_type_, nodeptr)
#define InvokeObjectPostCreateHook(classId, objectId, subId)
#define InvokeObjectPostAlterHook(classId, objectId, subId)
ObjectType get_relkind_objtype(char relkind)
#define ObjectAddressSet(addr, class_id, object_id)
char * nodeToString(const void *obj)
void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause)
FormData_pg_attribute * Form_pg_attribute
int errdetail_relkind_not_supported(char relkind)
FormData_pg_class * Form_pg_class
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
long deleteDependencyRecordsFor(Oid classId, Oid objectId, bool skipExtensionDeps)
#define lfirst_node(type, lc)
static int list_length(const List *l)
#define linitial_node(type, l)
FormData_pg_rewrite * Form_pg_rewrite
static Datum PointerGetDatum(const void *X)
static Datum BoolGetDatum(bool X)
static Datum ObjectIdGetDatum(Oid X)
static Datum NameGetDatum(const NameData *X)
static char DatumGetChar(Datum X)
static Datum CharGetDatum(char X)
#define RelationGetRelid(relation)
#define RelationGetDescr(relation)
#define RelationGetRelationName(relation)
static Oid InsertRule(const char *rulname, int evtype, Oid eventrel_oid, bool evinstead, Node *event_qual, List *action, bool replace)
void setRuleCheckAsUser(Node *node, Oid userid)
static bool setRuleCheckAsUser_walker(Node *node, Oid *context)
ObjectAddress DefineRule(RuleStmt *stmt, const char *queryString)
static void RangeVarCallbackForRenameRule(const RangeVar *rv, Oid relid, Oid oldrelid, void *arg)
static void checkRuleResultList(List *targetList, TupleDesc resultDesc, bool isSelect, bool requireColumnNameMatch)
static void setRuleCheckAsUser_Query(Query *qry, Oid userid)
ObjectAddress DefineQueryRewrite(const char *rulename, Oid event_relid, Node *event_qual, CmdType event_type, bool is_instead, bool replace, List *action)
void EnableDisableRule(Relation rel, const char *rulename, char fires_when)
ObjectAddress RenameRewriteRule(RangeVar *relation, const char *oldName, const char *newName)
#define RULE_FIRES_ON_ORIGIN
Query * getInsertSelectQuery(Query *parsetree, Query ***subquery_ptr)
void SetRelationRuleStatus(Oid relationId, bool relHasRules)
bool IsDefinedRewriteRule(Oid owningRel, const char *ruleName)
#define ViewSelectRuleName
void relation_close(Relation relation, LOCKMODE lockmode)
Relation relation_open(Oid relationId, LOCKMODE lockmode)
#define ERRCODE_DUPLICATE_OBJECT
void ReleaseSysCache(HeapTuple tuple)
HeapTuple SearchSysCache1(int cacheId, Datum key1)
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
#define SearchSysCacheCopy2(cacheId, key1, key2)
void table_close(Relation relation, LOCKMODE lockmode)
Relation table_open(Oid relationId, LOCKMODE lockmode)
#define TupleDescAttr(tupdesc, i)