77 *
88 *
99 * IDENTIFICATION
10- * $Header: /cvsroot/pgsql/src/backend/utils/cache/Attic/temprel.c,v 1.10 1999/07/17 20:18:02 momjian Exp $
10+ * $Header: /cvsroot/pgsql/src/backend/utils/cache/Attic/temprel.c,v 1.11 1999/09/04 19:55:50 momjian Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
1818 * When a temp table is created, a linked list of temp table tuples is
1919 * stored here. When a relname cache lookup is done, references to user-named
2020 * temp tables are converted to the internal temp table names.
21- *
2221 */
2322
2423#include <sys/types.h>
2524
2625#include "postgres.h"
2726#include "access/heapam.h"
27+ #include "access/xact.h"
2828#include "catalog/heap.h"
2929#include "catalog/index.h"
3030#include "utils/temprel.h"
@@ -41,7 +41,10 @@ static List *temp_rels = NIL;
4141typedef struct TempTable
4242{
4343char * user_relname ;
44- HeapTuple pg_class_tuple ;
44+ char * relname ;
45+ Oid relid ;
46+ char relkind ;
47+ TransactionId xid ;
4548}TempTable ;
4649
4750
@@ -55,11 +58,15 @@ create_temp_relation(char *relname, HeapTuple pg_class_tuple)
5558
5659temp_rel = palloc (sizeof (TempTable ));
5760temp_rel -> user_relname = palloc (NAMEDATALEN );
61+ temp_rel -> relname = palloc (NAMEDATALEN );
5862
5963/* save user-supplied name */
6064strcpy (temp_rel -> user_relname ,relname );
61-
62- temp_rel -> pg_class_tuple = heap_copytuple (pg_class_tuple );
65+ StrNCpy (temp_rel -> relname , ((Form_pg_class )
66+ GETSTRUCT (pg_class_tuple ))-> relname .data ,NAMEDATALEN );
67+ temp_rel -> relid = pg_class_tuple -> t_data -> t_oid ;
68+ temp_rel -> relkind = ((Form_pg_class )GETSTRUCT (pg_class_tuple ))-> relkind ;
69+ temp_rel -> xid = GetCurrentTransactionId ();
6370
6471temp_rels = lcons (temp_rel ,temp_rels );
6572
@@ -79,22 +86,19 @@ remove_all_temp_relations(void)
7986while (l != NIL )
8087{
8188TempTable * temp_rel = lfirst (l );
82- Form_pg_class classtuple ;
83-
84- classtuple = (Form_pg_class )GETSTRUCT (temp_rel -> pg_class_tuple );
8589
8690next = lnext (l );/* do this first, l is deallocated */
8791
88- if (classtuple -> relkind != RELKIND_INDEX )
92+ if (temp_rel -> relkind != RELKIND_INDEX )
8993{
90- char relname [NAMEDATALEN ];
94+ char relname [NAMEDATALEN ];
9195
9296/* safe from deallocation */
9397strcpy (relname ,temp_rel -> user_relname );
9498heap_destroy_with_catalog (relname );
9599}
96100else
97- index_destroy (temp_rel -> pg_class_tuple -> t_data -> t_oid );
101+ index_destroy (temp_rel -> relid );
98102
99103l = next ;
100104}
@@ -118,10 +122,56 @@ remove_temp_relation(Oid relid)
118122{
119123TempTable * temp_rel = lfirst (l );
120124
121- if (temp_rel -> pg_class_tuple -> t_data -> t_oid == relid )
125+ if (temp_rel -> relid == relid )
126+ {
127+ pfree (temp_rel -> user_relname );
128+ pfree (temp_rel -> relname );
129+ pfree (temp_rel );
130+ /* remove from linked list */
131+ if (prev != NIL )
132+ {
133+ lnext (prev )= lnext (l );
134+ pfree (l );
135+ l = lnext (prev );
136+ }
137+ else
138+ {
139+ temp_rels = lnext (l );
140+ pfree (l );
141+ l = temp_rels ;
142+ }
143+ }
144+ else
145+ {
146+ prev = l ;
147+ l = lnext (l );
148+ }
149+
150+ }
151+
152+ MemoryContextSwitchTo (oldcxt );
153+ }
154+
155+ /* remove entries from aborted transactions */
156+ void
157+ invalidate_temp_relations (void )
158+ {
159+ MemoryContext oldcxt ;
160+ List * l ,
161+ * prev ;
162+
163+ oldcxt = MemoryContextSwitchTo ((MemoryContext )CacheCxt );
164+
165+ prev = NIL ;
166+ l = temp_rels ;
167+ while (l != NIL )
168+ {
169+ TempTable * temp_rel = lfirst (l );
170+
171+ if (temp_rel -> xid == GetCurrentTransactionId ())
122172{
123173pfree (temp_rel -> user_relname );
124- pfree (temp_rel -> pg_class_tuple );
174+ pfree (temp_rel -> relname );
125175pfree (temp_rel );
126176/* remove from linked list */
127177if (prev != NIL )
@@ -148,7 +198,7 @@ remove_temp_relation(Oid relid)
148198MemoryContextSwitchTo (oldcxt );
149199}
150200
151- HeapTuple
201+ char *
152202get_temp_rel_by_name (char * user_relname )
153203{
154204List * l ;
@@ -158,7 +208,7 @@ get_temp_rel_by_name(char *user_relname)
158208TempTable * temp_rel = lfirst (l );
159209
160210if (strcmp (temp_rel -> user_relname ,user_relname )== 0 )
161- return temp_rel -> pg_class_tuple ;
211+ return temp_rel -> relname ;
162212}
163213return NULL ;
164214}