1
/*-------------------------------------------------------------------------
6
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
7
* Portions Copyright (c) 1994, Regents of the University of California
11
* $PostgreSQL: pgsql/src/backend/rewrite/rewriteSupport.c,v 1.60 2004-12-31 22:00:46 pgsql Exp $
13
*-------------------------------------------------------------------------
17
#include "access/heapam.h"
18
#include "catalog/catname.h"
19
#include "catalog/indexing.h"
20
#include "rewrite/rewriteSupport.h"
21
#include "utils/inval.h"
22
#include "utils/syscache.h"
26
* Is there a rule by the given name?
29
IsDefinedRewriteRule(Oid owningRel, const char *ruleName)
31
return SearchSysCacheExists(RULERELNAME,
32
ObjectIdGetDatum(owningRel),
33
PointerGetDatum(ruleName),
39
* SetRelationRuleStatus
40
* Set the value of the relation's relhasrules field in pg_class;
41
* if the relation is becoming a view, also adjust its relkind.
43
* NOTE: caller must be holding an appropriate lock on the relation.
45
* NOTE: an important side-effect of this operation is that an SI invalidation
46
* message is sent out to all backends --- including me --- causing relcache
47
* entries to be flushed or updated with the new set of rules for the table.
48
* This must happen even if we find that no change is needed in the pg_class
52
SetRelationRuleStatus(Oid relationId, bool relHasRules,
53
bool relIsBecomingView)
55
Relation relationRelation;
57
Form_pg_class classForm;
60
* Find the tuple to update in pg_class, using syscache for the
63
relationRelation = heap_openr(RelationRelationName, RowExclusiveLock);
64
tuple = SearchSysCacheCopy(RELOID,
65
ObjectIdGetDatum(relationId),
67
if (!HeapTupleIsValid(tuple))
68
elog(ERROR, "cache lookup failed for relation %u", relationId);
69
classForm = (Form_pg_class) GETSTRUCT(tuple);
71
if (classForm->relhasrules != relHasRules ||
72
(relIsBecomingView && classForm->relkind != RELKIND_VIEW))
75
classForm->relhasrules = relHasRules;
76
if (relIsBecomingView)
77
classForm->relkind = RELKIND_VIEW;
79
simple_heap_update(relationRelation, &tuple->t_self, tuple);
81
/* Keep the catalog indexes up to date */
82
CatalogUpdateIndexes(relationRelation, tuple);
86
/* no need to change tuple, but force relcache rebuild anyway */
87
CacheInvalidateRelcacheByTuple(tuple);
90
heap_freetuple(tuple);
91
heap_close(relationRelation, RowExclusiveLock);