1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/*-------------------------------------------------------------------------
*
* rewriteSupport.c--
*
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteSupport.c,v 1.16 1998/02/26 04:35:21 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "catalog/catname.h"
#include "catalog/pg_rewrite.h"
#include "utils/syscache.h" /* for SearchSysCache */
#include "nodes/pg_list.h"
#include "nodes/parsenodes.h"
#include "utils/builtins.h" /* for textout */
#include "utils/rel.h" /* for Relation, RelationData ... */
#include "utils/elog.h" /* for elog */
#include "storage/buf.h" /* for InvalidBuffer */
#include "rewrite/rewriteSupport.h"
#include "access/heapam.h"
#include "catalog/pg_class.h"
#include "catalog/pg_proc.h"
#include "catalog/indexing.h"
#include "utils/catcache.h" /* for CacheContext */
#include "utils/mcxt.h" /* MemoryContext stuff */
#include "utils/palloc.h"
#include "fmgr.h"
/*
* RuleIdGetActionInfo -
* given a rule oid, look it up and return the rule-event-qual and
* list of parsetrees for the rule (in parseTrees)
*/
#ifdef NOT_USED
static Node *
RuleIdGetActionInfo(Oid ruleoid, bool *instead_flag, Query **parseTrees)
{
HeapTuple ruletuple;
char *ruleaction = NULL;
bool action_is_null = false;
bool instead_is_null = false;
Relation ruleRelation = NULL;
TupleDesc ruleTupdesc = NULL;
Query *ruleparse = NULL;
char *rule_evqual_string = NULL;
Node *rule_evqual = NULL;
ruleRelation = heap_openr(RewriteRelationName);
ruleTupdesc = RelationGetTupleDescriptor(ruleRelation);
ruletuple = SearchSysCacheTuple(RULOID,
ObjectIdGetDatum(ruleoid),
0, 0, 0);
if (ruletuple == NULL)
elog(ERROR, "rule %u isn't in rewrite system relation", ruleoid);
ruleaction = (char *) heap_getattr(ruletuple,
Anum_pg_rewrite_ev_action,
ruleTupdesc,
&action_is_null);
rule_evqual_string = (char *) heap_getattr(ruletuple,
Anum_pg_rewrite_ev_qual,
ruleTupdesc, &action_is_null);
*instead_flag = !!heap_getattr(ruletuple,
Anum_pg_rewrite_is_instead,
ruleTupdesc, &instead_is_null);
if (action_is_null || instead_is_null)
{
elog(ERROR, "internal error: rewrite rule not properly set up");
}
ruleaction = textout((struct varlena *) ruleaction);
rule_evqual_string = textout((struct varlena *) rule_evqual_string);
ruleparse = (Query *) stringToNode(ruleaction);
rule_evqual = (Node *) stringToNode(rule_evqual_string);
heap_close(ruleRelation);
*parseTrees = ruleparse;
return rule_evqual;
}
#endif
int
IsDefinedRewriteRule(char *ruleName)
{
Relation RewriteRelation = NULL;
HeapScanDesc scanDesc = NULL;
ScanKeyData scanKey;
HeapTuple tuple = NULL;
/*
* Open the pg_rewrite relation.
*/
RewriteRelation = heap_openr(RewriteRelationName);
/*
* Scan the RuleRelation ('pg_rewrite') until we find a tuple
*/
ScanKeyEntryInitialize(&scanKey, 0, Anum_pg_rewrite_rulename,
NameEqualRegProcedure, PointerGetDatum(ruleName));
scanDesc = heap_beginscan(RewriteRelation,
0, false, 1, &scanKey);
tuple = heap_getnext(scanDesc, 0, (Buffer *) NULL);
/*
* return whether or not the rewrite rule existed
*/
heap_close(RewriteRelation);
heap_endscan(scanDesc);
return (HeapTupleIsValid(tuple));
}
static void
setRelhasrulesInRelation(Oid relationId, bool relhasrules)
{
Relation relationRelation;
HeapTuple tuple;
HeapTuple newTuple;
Relation idescs[Num_pg_class_indices];
Form_pg_class relp;
/*
* Lock a relation given its Oid. Go to the RelationRelation (i.e.
* pg_relation), find the appropriate tuple, and add the specified
* lock to it.
*/
relationRelation = heap_openr(RelationRelationName);
tuple = ClassOidIndexScan(relationRelation, relationId);
/*
* Create a new tuple (i.e. a copy of the old tuple with its rule lock
* field changed and replace the old tuple in the RelationRelation
* NOTE: XXX ??? do we really need to make that copy ????
*/
newTuple = heap_copytuple(tuple);
relp = (Form_pg_class) GETSTRUCT(newTuple);
relp->relhasrules = relhasrules;
heap_replace(relationRelation, &(tuple->t_ctid), newTuple);
/* keep the catalog indices up to date */
CatalogOpenIndices(Num_pg_class_indices, Name_pg_class_indices, idescs);
CatalogIndexInsert(idescs, Num_pg_class_indices, relationRelation,
newTuple);
CatalogCloseIndices(Num_pg_class_indices, idescs);
/* be tidy */
pfree(tuple);
pfree(newTuple);
heap_close(relationRelation);
}
void
prs2_addToRelation(Oid relid,
Oid ruleId,
CmdType event_type,
AttrNumber attno,
bool isInstead,
Node *qual,
List *actions)
{
Relation relation;
RewriteRule *thisRule;
RuleLock *rulelock;
MemoryContext oldcxt;
/*
* create an in memory RewriteRule data structure which is cached by
* every Relation descriptor. (see utils/cache/relcache.c)
*/
oldcxt = MemoryContextSwitchTo((MemoryContext) CacheCxt);
thisRule = (RewriteRule *) palloc(sizeof(RewriteRule));
MemoryContextSwitchTo(oldcxt);
thisRule->ruleId = ruleId;
thisRule->event = event_type;
thisRule->attrno = attno;
thisRule->qual = qual;
thisRule->actions = actions;
thisRule->isInstead = isInstead;
relation = heap_open(relid);
/*
* modify or create a RuleLock cached by Relation
*/
if (relation->rd_rules == NULL)
{
oldcxt = MemoryContextSwitchTo((MemoryContext) CacheCxt);
rulelock = (RuleLock *) palloc(sizeof(RuleLock));
rulelock->numLocks = 1;
rulelock->rules = (RewriteRule **) palloc(sizeof(RewriteRule *));
rulelock->rules[0] = thisRule;
relation->rd_rules = rulelock;
MemoryContextSwitchTo(oldcxt);
/*
* the fact that relation->rd_rules is NULL means the relhasrules
* attribute of the tuple of this relation in pg_class is false.
* We need to set it to true.
*/
setRelhasrulesInRelation(relid, TRUE);
}
else
{
int numlock;
rulelock = relation->rd_rules;
numlock = rulelock->numLocks;
/* expand, for safety reasons */
oldcxt = MemoryContextSwitchTo((MemoryContext) CacheCxt);
rulelock->rules =
(RewriteRule **) repalloc(rulelock->rules,
sizeof(RewriteRule *) * (numlock + 1));
MemoryContextSwitchTo(oldcxt);
rulelock->rules[numlock] = thisRule;
rulelock->numLocks++;
}
heap_close(relation);
return;
}
void
prs2_deleteFromRelation(Oid relid, Oid ruleId)
{
RuleLock *rulelock;
Relation relation;
int numlock;
int i;
MemoryContext oldcxt;
relation = heap_open(relid);
rulelock = relation->rd_rules;
Assert(rulelock != NULL);
numlock = rulelock->numLocks;
for (i = 0; i < numlock; i++)
{
if (rulelock->rules[i]->ruleId == ruleId)
break;
}
Assert(i < numlock);
oldcxt = MemoryContextSwitchTo((MemoryContext) CacheCxt);
pfree(rulelock->rules[i]);
MemoryContextSwitchTo(oldcxt);
if (numlock == 1)
{
relation->rd_rules = NULL;
/*
* we don't have rules any more, flag the relhasrules attribute of
* the tuple of this relation in pg_class false.
*/
setRelhasrulesInRelation(relid, FALSE);
}
else
{
rulelock->rules[i] = rulelock->rules[numlock - 1];
rulelock->rules[numlock - 1] = NULL;
rulelock->numLocks--;
}
heap_close(relation);
}
|