OSDN Git Service

Add a missing_ok argument to get_object_address().
[pg-rex/syncrep.git] / src / backend / rewrite / rewriteSupport.c
1 /*-------------------------------------------------------------------------
2  *
3  * rewriteSupport.c
4  *
5  *
6  * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        src/backend/rewrite/rewriteSupport.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
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"
28
29
30 /*
31  * Is there a rule by the given name?
32  */
33 bool
34 IsDefinedRewriteRule(Oid owningRel, const char *ruleName)
35 {
36         return SearchSysCacheExists2(RULERELNAME,
37                                                                  ObjectIdGetDatum(owningRel),
38                                                                  PointerGetDatum(ruleName));
39 }
40
41
42 /*
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.
46  *
47  * NOTE: caller must be holding an appropriate lock on the relation.
48  *
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
53  * row.
54  */
55 void
56 SetRelationRuleStatus(Oid relationId, bool relHasRules,
57                                           bool relIsBecomingView)
58 {
59         Relation        relationRelation;
60         HeapTuple       tuple;
61         Form_pg_class classForm;
62
63         /*
64          * Find the tuple to update in pg_class, using syscache for the lookup.
65          */
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);
71
72         if (classForm->relhasrules != relHasRules ||
73                 (relIsBecomingView && classForm->relkind != RELKIND_VIEW))
74         {
75                 /* Do the update */
76                 classForm->relhasrules = relHasRules;
77                 if (relIsBecomingView)
78                         classForm->relkind = RELKIND_VIEW;
79
80                 simple_heap_update(relationRelation, &tuple->t_self, tuple);
81
82                 /* Keep the catalog indexes up to date */
83                 CatalogUpdateIndexes(relationRelation, tuple);
84         }
85         else
86         {
87                 /* no need to change tuple, but force relcache rebuild anyway */
88                 CacheInvalidateRelcacheByTuple(tuple);
89         }
90
91         heap_freetuple(tuple);
92         heap_close(relationRelation, RowExclusiveLock);
93 }
94
95 /*
96  * Find rule oid.
97  *
98  * If missing_ok is false, throw an error if rule name not found.  If
99  * true, just return InvalidOid.
100  */
101 Oid
102 get_rewrite_oid(Oid relid, const char *rulename, bool missing_ok)
103 {
104         HeapTuple       tuple;
105         Oid                     ruleoid;
106
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))
112         {
113                 if (missing_ok)
114                         return InvalidOid;
115                 ereport(ERROR,
116                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
117                                  errmsg("rule \"%s\" for relation \"%s\" does not exist",
118                                                 rulename, get_rel_name(relid))));
119         }
120         Assert(relid == ((Form_pg_rewrite) GETSTRUCT(tuple))->ev_class);
121         ruleoid = HeapTupleGetOid(tuple);
122         ReleaseSysCache(tuple);
123         return ruleoid;
124 }
125
126 /*
127  * Find rule oid, given only a rule name but no rel OID.
128  *
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.
133  */
134 Oid
135 get_rewrite_oid_without_relid(const char *rulename,
136                                                           Oid *reloid, bool missing_ok)
137 {
138         Relation        RewriteRelation;
139         HeapScanDesc scanDesc;
140         ScanKeyData scanKeyData;
141         HeapTuple       htup;
142         Oid                     ruleoid;
143
144         /* Search pg_rewrite for such a rule */
145         ScanKeyInit(&scanKeyData,
146                                 Anum_pg_rewrite_rulename,
147                                 BTEqualStrategyNumber, F_NAMEEQ,
148                                 CStringGetDatum(rulename));
149
150         RewriteRelation = heap_open(RewriteRelationId, AccessShareLock);
151         scanDesc = heap_beginscan(RewriteRelation, SnapshotNow, 1, &scanKeyData);
152
153         htup = heap_getnext(scanDesc, ForwardScanDirection);
154         if (!HeapTupleIsValid(htup))
155         {
156                 if (!missing_ok)
157                         ereport(ERROR,
158                                         (errcode(ERRCODE_UNDEFINED_OBJECT),
159                                          errmsg("rule \"%s\" does not exist", rulename)));
160                 ruleoid = InvalidOid;
161         }
162         else
163         {
164                 ruleoid = HeapTupleGetOid(htup);
165                 if (reloid != NULL)
166                         *reloid = ((Form_pg_rewrite) GETSTRUCT(htup))->ev_class;
167
168                 htup = heap_getnext(scanDesc, ForwardScanDirection);
169                 if (HeapTupleIsValid(htup))
170                         ereport(ERROR,
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.")));
174         }
175         heap_endscan(scanDesc);
176         heap_close(RewriteRelation, AccessShareLock);
177
178         return ruleoid;
179 }