1 /*-------------------------------------------------------------------------
6 * Portions Copyright (c) 1996-2014, 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 "access/htup_details.h"
19 #include "catalog/indexing.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.
46 * NOTE: caller must be holding an appropriate lock on the relation.
48 * NOTE: an important side-effect of this operation is that an SI invalidation
49 * message is sent out to all backends --- including me --- causing relcache
50 * entries to be flushed or updated with the new set of rules for the table.
51 * This must happen even if we find that no change is needed in the pg_class
55 SetRelationRuleStatus(Oid relationId, bool relHasRules)
57 Relation relationRelation;
59 Form_pg_class classForm;
62 * Find the tuple to update in pg_class, using syscache for the lookup.
64 relationRelation = heap_open(RelationRelationId, RowExclusiveLock);
65 tuple = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relationId));
66 if (!HeapTupleIsValid(tuple))
67 elog(ERROR, "cache lookup failed for relation %u", relationId);
68 classForm = (Form_pg_class) GETSTRUCT(tuple);
70 if (classForm->relhasrules != relHasRules)
73 classForm->relhasrules = relHasRules;
75 simple_heap_update(relationRelation, &tuple->t_self, tuple);
77 /* Keep the catalog indexes up to date */
78 CatalogUpdateIndexes(relationRelation, tuple);
82 /* no need to change tuple, but force relcache rebuild anyway */
83 CacheInvalidateRelcacheByTuple(tuple);
86 heap_freetuple(tuple);
87 heap_close(relationRelation, RowExclusiveLock);
93 * If missing_ok is false, throw an error if rule name not found. If
94 * true, just return InvalidOid.
97 get_rewrite_oid(Oid relid, const char *rulename, bool missing_ok)
102 /* Find the rule's pg_rewrite tuple, get its OID */
103 tuple = SearchSysCache2(RULERELNAME,
104 ObjectIdGetDatum(relid),
105 PointerGetDatum(rulename));
106 if (!HeapTupleIsValid(tuple))
111 (errcode(ERRCODE_UNDEFINED_OBJECT),
112 errmsg("rule \"%s\" for relation \"%s\" does not exist",
113 rulename, get_rel_name(relid))));
115 Assert(relid == ((Form_pg_rewrite) GETSTRUCT(tuple))->ev_class);
116 ruleoid = HeapTupleGetOid(tuple);
117 ReleaseSysCache(tuple);
122 * Find rule oid, given only a rule name but no rel OID.
124 * If there's more than one, it's an error. If there aren't any, that's an
125 * error, too. In general, this should be avoided - it is provided to support
126 * syntax that is compatible with pre-7.3 versions of PG, where rule names
127 * were unique across the entire database.
130 get_rewrite_oid_without_relid(const char *rulename,
131 Oid *reloid, bool missing_ok)
133 Relation RewriteRelation;
134 HeapScanDesc scanDesc;
135 ScanKeyData scanKeyData;
139 /* Search pg_rewrite for such a rule */
140 ScanKeyInit(&scanKeyData,
141 Anum_pg_rewrite_rulename,
142 BTEqualStrategyNumber, F_NAMEEQ,
143 CStringGetDatum(rulename));
145 RewriteRelation = heap_open(RewriteRelationId, AccessShareLock);
146 scanDesc = heap_beginscan_catalog(RewriteRelation, 1, &scanKeyData);
148 htup = heap_getnext(scanDesc, ForwardScanDirection);
149 if (!HeapTupleIsValid(htup))
153 (errcode(ERRCODE_UNDEFINED_OBJECT),
154 errmsg("rule \"%s\" does not exist", rulename)));
155 ruleoid = InvalidOid;
159 ruleoid = HeapTupleGetOid(htup);
161 *reloid = ((Form_pg_rewrite) GETSTRUCT(htup))->ev_class;
163 htup = heap_getnext(scanDesc, ForwardScanDirection);
164 if (HeapTupleIsValid(htup))
166 (errcode(ERRCODE_DUPLICATE_OBJECT),
167 errmsg("there are multiple rules named \"%s\"", rulename),
168 errhint("Specify a relation name as well as a rule name.")));
170 heap_endscan(scanDesc);
171 heap_close(RewriteRelation, AccessShareLock);