1 /*-------------------------------------------------------------------------
6 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/rewrite/rewriteSupport.c
13 *-------------------------------------------------------------------------
17 #include "access/heapam.h"
18 #include "catalog/indexing.h"
19 #include "catalog/pg_class.h"
20 #include "catalog/pg_rewrite.h"
21 #include "rewrite/rewriteSupport.h"
22 #include "utils/fmgroids.h"
23 #include "utils/inval.h"
24 #include "utils/lsyscache.h"
25 #include "utils/rel.h"
26 #include "utils/syscache.h"
27 #include "utils/tqual.h"
31 * Is there a rule by the given name?
34 IsDefinedRewriteRule(Oid owningRel, const char *ruleName)
36 return SearchSysCacheExists2(RULERELNAME,
37 ObjectIdGetDatum(owningRel),
38 PointerGetDatum(ruleName));
43 * SetRelationRuleStatus
44 * Set the value of the relation's relhasrules field in pg_class;
45 * if the relation is becoming a view, also adjust its relkind.
47 * NOTE: caller must be holding an appropriate lock on the relation.
49 * NOTE: an important side-effect of this operation is that an SI invalidation
50 * message is sent out to all backends --- including me --- causing relcache
51 * entries to be flushed or updated with the new set of rules for the table.
52 * This must happen even if we find that no change is needed in the pg_class
56 SetRelationRuleStatus(Oid relationId, bool relHasRules,
57 bool relIsBecomingView)
59 Relation relationRelation;
61 Form_pg_class classForm;
64 * Find the tuple to update in pg_class, using syscache for the lookup.
66 relationRelation = heap_open(RelationRelationId, RowExclusiveLock);
67 tuple = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relationId));
68 if (!HeapTupleIsValid(tuple))
69 elog(ERROR, "cache lookup failed for relation %u", relationId);
70 classForm = (Form_pg_class) GETSTRUCT(tuple);
72 if (classForm->relhasrules != relHasRules ||
73 (relIsBecomingView && classForm->relkind != RELKIND_VIEW))
76 classForm->relhasrules = relHasRules;
77 if (relIsBecomingView)
78 classForm->relkind = RELKIND_VIEW;
80 simple_heap_update(relationRelation, &tuple->t_self, tuple);
82 /* Keep the catalog indexes up to date */
83 CatalogUpdateIndexes(relationRelation, tuple);
87 /* no need to change tuple, but force relcache rebuild anyway */
88 CacheInvalidateRelcacheByTuple(tuple);
91 heap_freetuple(tuple);
92 heap_close(relationRelation, RowExclusiveLock);
98 * If missing_ok is false, throw an error if rule name not found. If
99 * true, just return InvalidOid.
102 get_rewrite_oid(Oid relid, const char *rulename, bool missing_ok)
107 /* Find the rule's pg_rewrite tuple, get its OID */
108 tuple = SearchSysCache2(RULERELNAME,
109 ObjectIdGetDatum(relid),
110 PointerGetDatum(rulename));
111 if (!HeapTupleIsValid(tuple))
116 (errcode(ERRCODE_UNDEFINED_OBJECT),
117 errmsg("rule \"%s\" for relation \"%s\" does not exist",
118 rulename, get_rel_name(relid))));
120 Assert(relid == ((Form_pg_rewrite) GETSTRUCT(tuple))->ev_class);
121 ruleoid = HeapTupleGetOid(tuple);
122 ReleaseSysCache(tuple);
127 * Find rule oid, given only a rule name but no rel OID.
129 * If there's more than one, it's an error. If there aren't any, that's an
130 * error, too. In general, this should be avoided - it is provided to support
131 * syntax that is compatible with pre-7.3 versions of PG, where rule names
132 * were unique across the entire database.
135 get_rewrite_oid_without_relid(const char *rulename,
136 Oid *reloid, bool missing_ok)
138 Relation RewriteRelation;
139 HeapScanDesc scanDesc;
140 ScanKeyData scanKeyData;
144 /* Search pg_rewrite for such a rule */
145 ScanKeyInit(&scanKeyData,
146 Anum_pg_rewrite_rulename,
147 BTEqualStrategyNumber, F_NAMEEQ,
148 CStringGetDatum(rulename));
150 RewriteRelation = heap_open(RewriteRelationId, AccessShareLock);
151 scanDesc = heap_beginscan(RewriteRelation, SnapshotNow, 1, &scanKeyData);
153 htup = heap_getnext(scanDesc, ForwardScanDirection);
154 if (!HeapTupleIsValid(htup))
158 (errcode(ERRCODE_UNDEFINED_OBJECT),
159 errmsg("rule \"%s\" does not exist", rulename)));
160 ruleoid = InvalidOid;
164 ruleoid = HeapTupleGetOid(htup);
166 *reloid = ((Form_pg_rewrite) GETSTRUCT(htup))->ev_class;
168 htup = heap_getnext(scanDesc, ForwardScanDirection);
169 if (HeapTupleIsValid(htup))
171 (errcode(ERRCODE_DUPLICATE_OBJECT),
172 errmsg("there are multiple rules named \"%s\"", rulename),
173 errhint("Specify a relation name as well as a rule name.")));
175 heap_endscan(scanDesc);
176 heap_close(RewriteRelation, AccessShareLock);