Fix oversight in previous error-reporting patch; mustn't pfree path string
[PostgreSQL.git] / src / backend / rewrite / rewriteSupport.c
blob4d4825d4a51bfa5cb7dae86de14ea39bdf847768
1 /*-------------------------------------------------------------------------
3 * rewriteSupport.c
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * IDENTIFICATION
11 * $PostgreSQL$
13 *-------------------------------------------------------------------------
15 #include "postgres.h"
17 #include "access/heapam.h"
18 #include "catalog/indexing.h"
19 #include "catalog/pg_class.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?
28 bool
29 IsDefinedRewriteRule(Oid owningRel, const char *ruleName)
31 return SearchSysCacheExists(RULERELNAME,
32 ObjectIdGetDatum(owningRel),
33 PointerGetDatum(ruleName),
34 0, 0);
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
49 * row.
51 void
52 SetRelationRuleStatus(Oid relationId, bool relHasRules,
53 bool relIsBecomingView)
55 Relation relationRelation;
56 HeapTuple tuple;
57 Form_pg_class classForm;
60 * Find the tuple to update in pg_class, using syscache for the lookup.
62 relationRelation = heap_open(RelationRelationId, RowExclusiveLock);
63 tuple = SearchSysCacheCopy(RELOID,
64 ObjectIdGetDatum(relationId),
65 0, 0, 0);
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 ||
71 (relIsBecomingView && classForm->relkind != RELKIND_VIEW))
73 /* Do the update */
74 classForm->relhasrules = relHasRules;
75 if (relIsBecomingView)
76 classForm->relkind = RELKIND_VIEW;
78 simple_heap_update(relationRelation, &tuple->t_self, tuple);
80 /* Keep the catalog indexes up to date */
81 CatalogUpdateIndexes(relationRelation, tuple);
83 else
85 /* no need to change tuple, but force relcache rebuild anyway */
86 CacheInvalidateRelcacheByTuple(tuple);
89 heap_freetuple(tuple);
90 heap_close(relationRelation, RowExclusiveLock);