OSDN Git Service

Change Copyright from PostgreSQL, Inc to PostgreSQL Global Development Group.
[pg-rex/syncrep.git] / src / backend / commands / _deadcode / version.c
1 /*-------------------------------------------------------------------------
2  *
3  * version.c
4  *        This file contains all the rules that govern all version semantics.
5  *
6  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *      The version stuff has not been tested under postgres95 and probably
10  *      doesn't work! - jolly 8/19/95
11  *
12  *
13  *      $Id: version.c,v 1.25 2001/01/24 19:42:53 momjian Exp $
14  *
15  * NOTES
16  *      At the point the version is defined, 2 physical relations are created
17  *      <vname>_added and <vname>_deleted.
18  *
19  *      In addition, 4 rules are defined which govern the semantics of
20  *      versions w.r.t retrieves, appends, replaces and deletes.
21  *
22  *-------------------------------------------------------------------------
23  */
24
25 #include "postgres.h"
26
27
28 #define MAX_QUERY_LEN 1024
29
30 char            rule_buf[MAX_QUERY_LEN];
31
32 /*
33  * problem: the version system assumes that the rules it declares will
34  *                      be fired in the order of declaration, it also assumes
35  *                      goh's silly instead semantics.  Unfortunately, it is a pain
36  *                      to make the version system work with the new semantics.
37  *                      However the whole problem can be solved, and some nice
38  *                      functionality can be achieved if we get multiple action rules
39  *                      to work.  So thats what I did                                           -- glass
40  *
41  * Well, at least they've been working for about 20 minutes.
42  *
43  * So any comments in this code about 1 rule per transction are false...:)
44  *
45  */
46
47 /*
48  *      This is needed because the rule system only allows
49  *      *1* rule to be defined per transaction.
50  *
51  * NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
52  * OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
53  * OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
54  * OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
55  * OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
56  * OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
57  * OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
58  * OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
59  * OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
60  * OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
61  * OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
62  * OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
63  * OOOOOOOOOOOOOOOOOOO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
64  *
65  * DONT DO THAT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
66  *
67  * If you commit the current Xact all the palloced memory GOES AWAY
68  * and could be re-palloced in the new Xact and the whole hell breaks
69  * loose and poor people like me spend 2 hours of their live chassing
70  * a strange memory bug instead of watching the "Get Smart" marathon
71  * in NICK !
72  * DO NOT COMMIT THE XACT, just increase the Cid counter!
73  *                                                                                                              _sp.
74  */
75 #ifdef NOT_USED
76 static void
77 eval_as_new_xact(char *query)
78 {
79
80         /*
81          * WARNING! do not uncomment the following lines WARNING!
82          * CommitTransactionCommand(); StartTransactionCommand();
83          */
84         CommandCounterIncrement();
85         pg_exec_query(query);
86 }
87
88 #endif
89 /*
90  *      Define a version.
91  */
92 #ifdef NOT_USED
93 void
94 DefineVersion(char *name, char *fromRelname, char *date)
95 {
96         char       *bname;
97         static char saved_basename[512];
98         static char saved_snapshot[512];
99
100         if (date == NULL)
101         {
102                 /* no time ranges */
103                 bname = fromRelname;
104                 strcpy(saved_basename, (char *) bname);
105                 *saved_snapshot = (char) NULL;
106         }
107         else
108         {
109                 /* version is a snapshot */
110                 bname = fromRelname;
111                 strcpy(saved_basename, (char *) bname);
112                 sprintf(saved_snapshot, "['%s']", date);
113         }
114
115
116         /*
117          * Calls the routine ``GetAttrList'' get the list of attributes from
118          * the base relation. Code is put here so that we only need to look up
119          * the attribute once for both appends and replaces.
120          */
121         setAttrList(bname);
122
123         VersionCreate(name, saved_basename);
124         VersionAppend(name, saved_basename);
125         VersionDelete(name, saved_basename, saved_snapshot);
126         VersionReplace(name, saved_basename, saved_snapshot);
127         VersionRetrieve(name, saved_basename, saved_snapshot);
128 }
129
130 #endif
131
132 /*
133  *      Creates the deltas.
134  */
135 #ifdef NOT_USED
136 void
137 VersionCreate(char *vname, char *bname)
138 {
139         static char query_buf[MAX_QUERY_LEN];
140
141         /*
142          * Creating the dummy version relation for triggering rules.
143          */
144         sprintf(query_buf, "SELECT * INTO TABLE %s from %s where 1 =2",
145                         vname, bname);
146
147         pg_exec_query(query_buf);
148
149         /*
150          * Creating the ``v_added'' relation
151          */
152         sprintf(query_buf, "SELECT * INTO TABLE %s_added from %s where 1 = 2",
153                         vname, bname);
154         eval_as_new_xact(query_buf);
155
156         /*
157          * Creating the ``v_deleted'' relation.
158          */
159         sprintf(query_buf, "CREATE TABLE %s_del (DOID oid)", vname);
160         eval_as_new_xact(query_buf);
161 }
162
163 #endif
164
165
166 /*
167  * Given the relation name, does a catalog lookup for that relation and
168  * sets the global variable 'attr_list' with the list of attributes (names)
169  * for that relation.
170  */
171 #ifdef NOT_USED
172 static void
173 setAttrList(char *bname)
174 {
175         Relation        rel;
176         int                     i = 0;
177         int                     maxattrs = 0;
178         char       *attrname;
179         char            temp_buf[512];
180         int                     notfirst = 0;
181
182         rel = heap_openr(bname);
183         if (rel == NULL)
184         {
185                 elog(ERROR, "Unable to expand all -- amopenr failed ");
186                 return;
187         }
188         maxattrs = RelationGetNumberOfAttributes(rel);
189
190         attr_list[0] = '\0';
191
192         for (i = maxattrs - 1; i > -1; --i)
193         {
194                 attrname = NameStr(rel->rd_att->attrs[i]->attname);
195
196                 if (notfirst == 1)
197                         sprintf(temp_buf, ", %s = new.%s", attrname, attrname);
198                 else
199                 {
200                         sprintf(temp_buf, "%s = new.%s", attrname, attrname);
201                         notfirst = 1;
202                 }
203                 strcat(attr_list, temp_buf);
204         }
205
206         heap_close(rel);
207
208         return;
209 }
210
211 #endif
212
213 /*
214  * This routine defines the rule governing the append semantics of
215  * versions.  All tuples appended to a version gets appended to the
216  * <vname>_added relation.
217  */
218 #ifdef NOT_USED
219 static void
220 VersionAppend(char *vname, char *bname)
221 {
222         sprintf(rule_buf,
223                         "define rewrite rule %s_append is on INSERT to %s do instead append %s_added(%s)",
224                         vname, vname, vname, attr_list);
225
226         eval_as_new_xact(rule_buf);
227 }
228
229 #endif
230
231 /*
232  * This routine defines the rule governing the retrieval semantics of
233  * versions.  To retrieve tuples from a version , we need to:
234  *
235  *              1. Retrieve all tuples in the <vname>_added relation.
236  *              2. Retrieve all tuples in the base relation which are not in
237  *                 the <vname>_del relation.
238  */
239 #ifdef NOT_USED
240 void
241 VersionRetrieve(char *vname, char *bname, char *snapshot)
242 {
243
244         sprintf(rule_buf,
245                         "define rewrite rule %s_retrieve is on SELECT to %s do instead\n\
246 SELECT %s_1.oid, %s_1.* from _%s in %s%s, %s_1 in (%s_added | _%s) \
247 where _%s.oid !!= '%s_del.DOID'",
248                         vname, vname, vname, vname, bname,
249                         bname, snapshot,
250                         vname, vname, bname, bname, vname);
251
252         eval_as_new_xact(rule_buf);
253
254         /* printf("%s\n",rule_buf); */
255
256 }
257
258 #endif
259
260 /*
261  * This routine defines the rules that govern the delete semantics of
262  * versions. Two things happens when we delete a tuple from a version:
263  *
264  *         1. If the tuple to be deleted was added to the version *after*
265  *                the version was created, then we simply delete the tuple
266  *                from the <vname>_added relation.
267  *         2. If the tuple to be deleted is actually in the base relation,
268  *                then we have to mark that tuple as being deleted by adding
269  *                it to the <vname>_del relation.
270  */
271 #ifdef NOT_USED
272 void
273 VersionDelete(char *vname, char *bname, char *snapshot)
274 {
275
276         sprintf(rule_buf,
277                         "define rewrite rule %s_delete1 is on delete to %s do instead\n \
278 [delete %s_added where current.oid = %s_added.oid\n \
279  append %s_del(DOID = current.oid) from _%s in %s%s \
280  where current.oid = _%s.oid] \n",
281                         vname, vname, vname, vname, vname,
282                         bname, bname, snapshot, bname);
283
284         eval_as_new_xact(rule_buf);
285 #ifdef OLD_REWRITE
286         sprintf(rule_buf,
287                         "define rewrite rule %s_delete2 is on delete to %s do instead \n \
288     append %s_del(DOID = current.oid) from _%s in %s%s \
289     where current.oid = _%s.oid \n",
290                         vname, vname, vname, bname, bname, snapshot, bname);
291
292         eval_as_new_xact(rule_buf);
293 #endif   /* OLD_REWRITE */
294 }
295
296 #endif
297
298 /*
299  *      This routine defines the rules that govern the update semantics
300  *      of versions. To update a tuple in a version:
301  *
302  *              1. If the tuple is in <vname>_added, we simply ``replace''
303  *                 the tuple (as per postgres style).
304  *              2. if the tuple is in the base relation, then two things have to
305  *                 happen:
306  *                 2.1  The tuple is marked ``deleted'' from the base relation by
307  *                              adding the tuple to the <vname>_del relation.
308  *                 2.2  A copy of the tuple is appended to the <vname>_added relation
309  */
310 #ifdef NOT_USED
311 void
312 VersionReplace(char *vname, char *bname, char *snapshot)
313 {
314         sprintf(rule_buf,
315                         "define rewrite rule %s_replace1 is on replace to %s do instead \n\
316 [replace %s_added(%s) where current.oid = %s_added.oid \n\
317  append %s_del(DOID = current.oid) from _%s in %s%s \
318  where current.oid = _%s.oid\n\
319  append %s_added(%s) from _%s in %s%s \
320  where current.oid !!= '%s_added.oid' and current.oid = _%s.oid]\n",
321                         vname, vname, vname, attr_list, vname,
322                         vname, bname, bname, snapshot, bname,
323                         vname, attr_list, bname, bname, snapshot, vname, bname);
324
325         eval_as_new_xact(rule_buf);
326
327 /*      printf("%s\n",rule_buf); */
328 #ifdef OLD_REWRITE
329         sprintf(rule_buf,
330                         "define rewrite rule %s_replace2 is on replace to %s do \n\
331     append %s_del(DOID = current.oid) from _%s in %s%s \
332     where current.oid = _%s.oid\n",
333                         vname, vname, vname, bname, bname, snapshot, bname);
334
335         eval_as_new_xact(rule_buf);
336
337         sprintf(rule_buf,
338                         "define rewrite rule %s_replace3 is on replace to %s do instead\n\
339     append %s_added(%s) from _%s in %s%s \
340     where current.oid !!= '%s_added.oid' and current.oid = \
341     _%s.oid\n",
342         vname, vname, vname, attr_list, bname, bname, snapshot, vname, bname);
343
344         eval_as_new_xact(rule_buf);
345 #endif   /* OLD_REWRITE */
346 /*      printf("%s\n",rule_buf); */
347
348 }
349
350 #endif