summaryrefslogtreecommitdiff
path: root/src/backend/utils/cache/temprel.c
blob: 99c75dbe5439e465979b0a2184d7d43bfb24fb52 (plain)
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
/*-------------------------------------------------------------------------
 *
 * temprel.c--
 *	  POSTGRES temporary relation handling
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/backend/utils/cache/Attic/temprel.c,v 1.1 1999/02/02 03:45:03 momjian Exp $
 *
 *-------------------------------------------------------------------------
 */

/*
 * This implements temp tables by modifying the relname cache lookups
 * of pg_class.
 * When a temp table is created, a linked list of temp table tuples is
 * stored here.  When a relname cache lookup is done, references to user-named
 * temp tables are converted to the internal temp table names.
 *
 */

#include <sys/types.h>
#include <stdio.h>
#include <string.h>

#include "postgres.h"
#include "miscadmin.h"
#include "nodes/pg_list.h"
#include "utils/mcxt.h"
#include "utils/temprel.h"
#include "access/htup.h"
#include "access/heapam.h"
#include "catalog/heap.h"
#include "catalog/index.h"
#include "catalog/pg_class.h"

GlobalMemory CacheCxt;

/* ----------------
 *		global variables
 * ----------------
 */

static List *temp_rels = NIL;

typedef struct TempTable
{
	char 		*user_relname;
	HeapTuple	pg_class_tuple;
} TempTable;


void
create_temp_relation(char *relname, HeapTuple pg_class_tuple)
{
	MemoryContext oldcxt;
	TempTable	*temp_rel;

	oldcxt = MemoryContextSwitchTo((MemoryContext) CacheCxt);

	temp_rel = palloc(sizeof(TempTable));
	temp_rel->user_relname = palloc(NAMEDATALEN);

	/* save user-supplied name */
	strcpy(temp_rel->user_relname, relname);
            
	temp_rel->pg_class_tuple = heap_copytuple(pg_class_tuple);

	temp_rels = lcons(temp_rel, temp_rels);

	MemoryContextSwitchTo(oldcxt);
}

void
remove_all_temp_relations(void)
{
	List *l, *next;

	l = temp_rels;
	while (l != NIL)
	{
		TempTable	*temp_rel = lfirst(l);
		Form_pg_class classtuple;

		classtuple = (Form_pg_class)GETSTRUCT(temp_rel->pg_class_tuple);

		next = lnext(l); /* do this first, l is deallocated */

		if (classtuple->relkind != RELKIND_INDEX)
		{
			char relname[NAMEDATALEN];

			/* safe from deallocation */
			strcpy(relname, temp_rel->user_relname); 
			heap_destroy_with_catalog(relname);
		}
		else
			index_destroy(temp_rel->pg_class_tuple->t_data->t_oid);

		l = next;
	}
}

/* we don't have the relname for indexes, so we just pass the oid */
void
remove_temp_relation(Oid relid)
{

	MemoryContext oldcxt;
	List		*l, *prev;
	
	oldcxt = MemoryContextSwitchTo((MemoryContext) CacheCxt);

	prev = NIL;
	l = temp_rels;
	while (l != NIL)
	{
		TempTable	*temp_rel = lfirst(l);

		if (temp_rel->pg_class_tuple->t_data->t_oid == relid)
		{
			pfree(temp_rel->user_relname);
			pfree(temp_rel->pg_class_tuple);
			pfree(temp_rel);
			/* remove from linked list */
			if (prev != NIL)
			{
				lnext(prev) = lnext(l);
				pfree(l);
				l = lnext(prev);
			}
			else
			{
				temp_rels = lnext(l);
				pfree(l);
				l = temp_rels;
			}
		}
		else
		{
			prev = l;
			l = lnext(l);
		}

	}

	MemoryContextSwitchTo(oldcxt);
}

HeapTuple
get_temp_rel_by_name(char *user_relname)
{
	List *l;

	foreach(l, temp_rels)
	{
		TempTable	*temp_rel = lfirst(l);

		if (strcmp(temp_rel->user_relname, user_relname) == 0)
			return temp_rel->pg_class_tuple;
	}
	return NULL;
}