Skip to content

Commit 90f4284

Browse files
committed
Small cleanup of temp-table handling. Disallow creation of a non-temp
table that inherits from a temp table. Make sure the right things happen if one creates a temp table, creates another temp that inherits from it, then renames the first one. (Previously, system would end up trying to delete the temp tables in the wrong order.)
1 parent 7558da6 commit 90f4284

File tree

8 files changed

+42
-28
lines changed

8 files changed

+42
-28
lines changed

src/backend/catalog/heap.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.153 2000/12/22 19:21:37 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.154 2000/12/22 23:12:03 tgl Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -801,7 +801,7 @@ heap_create_with_catalog(char *relname,
801801

802802
/* temp tables can mask non-temp tables */
803803
if ((!istemp && RelnameFindRelid(relname)) ||
804-
(istemp && get_temp_rel_by_username(relname) != NULL))
804+
(istemp && is_temp_rel_name(relname)))
805805
elog(ERROR, "Relation '%s' already exists", relname);
806806

807807
if (istemp)
@@ -813,7 +813,7 @@ heap_create_with_catalog(char *relname,
813813
}
814814

815815
/* ----------------
816-
* get_temp_rel_by_username() couldn't check the simultaneous
816+
* RelnameFindRelid couldn't detect simultaneous
817817
* creation. Uniqueness will be really checked by unique
818818
* indexes of system tables but we couldn't check it here.
819819
* We have to postpone creating the disk file for this
@@ -1404,7 +1404,7 @@ heap_drop_with_catalog(const char *relname,
14041404
Relation rel;
14051405
Oid rid;
14061406
bool has_toasttable;
1407-
bool istemp = (get_temp_rel_by_username(relname) != NULL);
1407+
bool istemp = is_temp_rel_name(relname);
14081408
int i;
14091409

14101410
/* ----------------

src/backend/catalog/index.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.132 2000/12/09 20:31:43 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.133 2000/12/22 23:12:03 tgl Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -145,7 +145,7 @@ GetHeapRelationOid(char *heapRelationName, char *indexRelationName, bool istemp)
145145
indoid = RelnameFindRelid(indexRelationName);
146146

147147
if ((!istemp && OidIsValid(indoid)) ||
148-
(istemp && get_temp_rel_by_username(indexRelationName) != NULL))
148+
(istemp && is_temp_rel_name(indexRelationName)))
149149
elog(ERROR, "Cannot create index: '%s' already exists",
150150
indexRelationName);
151151

@@ -885,7 +885,7 @@ index_create(char *heapRelationName,
885885
TupleDesc indexTupDesc;
886886
Oid heapoid;
887887
Oid indexoid;
888-
bool istemp = (get_temp_rel_by_username(heapRelationName) != NULL);
888+
bool istemp = is_temp_rel_name(heapRelationName);
889889
char *temp_relname = NULL;
890890

891891
SetReindexProcessing(false);

src/backend/commands/command.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.113 2000/12/05 19:57:55 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.114 2000/12/22 23:12:05 tgl Exp $
1212
*
1313
* NOTES
1414
* The PerformAddAttribute() code, like most of the relation
@@ -1237,10 +1237,9 @@ AlterTableAddConstraint(char *relationName,
12371237
int i;
12381238
bool found = false;
12391239

1240-
if (get_temp_rel_by_username(fkconstraint->pktable_name)!=NULL &&
1241-
get_temp_rel_by_username(relationName)==NULL) {
1240+
if (is_temp_rel_name(fkconstraint->pktable_name) &&
1241+
!is_temp_rel_name(relationName))
12421242
elog(ERROR, "ALTER TABLE / ADD CONSTRAINT: Unable to reference temporary table from permanent table constraint.");
1243-
}
12441243

12451244
/*
12461245
* Grab an exclusive lock on the pk table, so that someone

src/backend/commands/creatinh.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/creatinh.c,v 1.68 2000/12/14 00:41:09 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/creatinh.c,v 1.69 2000/12/22 23:12:05 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -24,8 +24,9 @@
2424
#include "catalog/pg_type.h"
2525
#include "commands/creatinh.h"
2626
#include "miscadmin.h"
27-
#include "utils/syscache.h"
2827
#include "optimizer/clauses.h"
28+
#include "utils/syscache.h"
29+
#include "utils/temprel.h"
2930

3031
/* ----------------
3132
* local stuff
@@ -34,7 +35,7 @@
3435

3536
static int checkAttrExists(const char *attributeName,
3637
const char *attributeType, List *schema);
37-
static List *MergeAttributes(List *schema, List *supers,
38+
static List *MergeAttributes(List *schema, List *supers, bool istemp,
3839
List **supOids, List **supconstr);
3940
static void StoreCatalogInheritance(Oid relationId, List *supers);
4041
static void setRelhassubclassInRelation(Oid relationId, bool relhassubclass);
@@ -71,7 +72,7 @@ DefineRelation(CreateStmt *stmt, char relkind)
7172
* including inherited attributes.
7273
* ----------------
7374
*/
74-
schema = MergeAttributes(schema, stmt->inhRelnames,
75+
schema = MergeAttributes(schema, stmt->inhRelnames, stmt->istemp,
7576
&inheritOids, &old_constraints);
7677

7778
numberOfAttributes = length(schema);
@@ -283,6 +284,7 @@ change_varattnos_of_a_node(Node *node, const AttrNumber *newattno)
283284
* 'schema' is the column/attribute definition for the table. (It's a list
284285
* of ColumnDef's.) It is destructively changed.
285286
* 'supers' is a list of names (as Value objects) of parent relations.
287+
* 'istemp' is TRUE if we are creating a temp relation.
286288
*
287289
* Output arguments:
288290
* 'supOids' receives an integer list of the OIDs of the parent relations.
@@ -311,7 +313,7 @@ change_varattnos_of_a_node(Node *node, const AttrNumber *newattno)
311313
* stud_emp {7:percent}
312314
*/
313315
static List *
314-
MergeAttributes(List *schema, List *supers,
316+
MergeAttributes(List *schema, List *supers, bool istemp,
315317
List **supOids, List **supconstr)
316318
{
317319
List *entry;
@@ -378,6 +380,9 @@ MergeAttributes(List *schema, List *supers,
378380

379381
if (relation->rd_rel->relkind != RELKIND_RELATION)
380382
elog(ERROR, "CREATE TABLE: inherited relation \"%s\" is not a table", name);
383+
/* Permanent rels cannot inherit from temporary ones */
384+
if (!istemp && is_temp_rel_name(name))
385+
elog(ERROR, "CREATE TABLE: cannot inherit from temp relation \"%s\"", name);
381386

382387
parentOids = lappendi(parentOids, relation->rd_id);
383388
setRelhassubclassInRelation(relation->rd_id, true);

src/backend/commands/vacuum.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.178 2000/12/22 00:51:53 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.179 2000/12/22 23:12:05 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -274,8 +274,8 @@ getrels(NameData *VacRelP)
274274
char *nontemp_relname;
275275

276276
/* We must re-map temp table names bjm 2000-04-06 */
277-
if ((nontemp_relname =
278-
get_temp_rel_by_username(NameStr(*VacRelP))) == NULL)
277+
nontemp_relname = get_temp_rel_by_username(NameStr(*VacRelP));
278+
if (nontemp_relname == NULL)
279279
nontemp_relname = NameStr(*VacRelP);
280280

281281
ScanKeyEntryInitialize(&key, 0x0, Anum_pg_class_relname,

src/backend/utils/cache/relcache.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.120 2000/12/09 20:32:44 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.121 2000/12/22 23:12:06 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1526,7 +1526,7 @@ RelationNameGetRelation(const char *relationName)
15261526
* ----------------
15271527
*/
15281528
temprelname = get_temp_rel_by_username(relationName);
1529-
if (temprelname)
1529+
if (temprelname != NULL)
15301530
relationName = temprelname;
15311531

15321532
/* ----------------

src/backend/utils/cache/temprel.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/cache/Attic/temprel.c,v 1.31 2000/11/16 22:30:33 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/cache/Attic/temprel.c,v 1.32 2000/12/22 23:12:07 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -152,13 +152,19 @@ rename_temp_relation(const char *oldname,
152152
continue; /* ignore non-matching entries */
153153

154154
/* We are renaming a temp table --- is it OK to do so? */
155-
if (get_temp_rel_by_username(newname) != NULL)
155+
if (is_temp_rel_name(newname))
156156
elog(ERROR, "Cannot rename temp table \"%s\": temp table \"%s\" already exists",
157157
oldname, newname);
158158

159159
/*
160160
* Create a new mapping entry and mark the old one deleted in this
161161
* xact. One of these entries will be deleted at xact end.
162+
*
163+
* NOTE: the new mapping entry is inserted into the list just after
164+
* the old one. We could alternatively insert it before the old one,
165+
* but that'd take more code. It does need to be in one spot or the
166+
* other, to ensure that deletion of temp rels happens in the right
167+
* order during remove_all_temp_relations().
162168
*/
163169
oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
164170

@@ -168,7 +174,7 @@ rename_temp_relation(const char *oldname,
168174
StrNCpy(NameStr(new_temp_rel->user_relname), newname, NAMEDATALEN);
169175
new_temp_rel->created_in_cur_xact = true;
170176

171-
temp_rels = lcons(new_temp_rel, temp_rels);
177+
lnext(l) = lcons(new_temp_rel, lnext(l));
172178

173179
temp_rel->deleted_in_cur_xact = true;
174180

@@ -178,7 +184,7 @@ rename_temp_relation(const char *oldname,
178184
}
179185

180186
/* Old name does not match any temp table name, what about new? */
181-
if (get_temp_rel_by_username(newname) != NULL)
187+
if (is_temp_rel_name(newname))
182188
elog(ERROR, "Cannot rename \"%s\" to \"%s\": a temp table by that name already exists",
183189
oldname, newname);
184190

@@ -205,7 +211,8 @@ remove_all_temp_relations(void)
205211
* Scan the list and delete all entries not already deleted.
206212
* We need not worry about list entries getting deleted from under us,
207213
* because remove_temp_rel_by_relid() doesn't remove entries, only
208-
* mark them dead.
214+
* mark them dead. Note that entries will be deleted in reverse order
215+
* of creation --- that's critical for cases involving inheritance.
209216
*/
210217
foreach(l, temp_rels)
211218
{
@@ -286,7 +293,8 @@ AtEOXact_temp_relations(bool isCommit)
286293
/*
287294
* Map user name to physical name --- returns NULL if no entry.
288295
*
289-
* This is the normal way to test whether a name is a temp table name.
296+
* This also supports testing whether a name is a temp table name;
297+
* see is_temp_rel_name() macro.
290298
*/
291299
char *
292300
get_temp_rel_by_username(const char *user_relname)

src/include/utils/temprel.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: temprel.h,v 1.12 2000/11/08 22:10:03 tgl Exp $
10+
* $Id: temprel.h,v 1.13 2000/12/22 23:12:07 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -28,4 +28,6 @@ extern void AtEOXact_temp_relations(bool isCommit);
2828
extern char *get_temp_rel_by_username(const char *user_relname);
2929
extern char *get_temp_rel_by_physicalname(const char *relname);
3030

31+
#define is_temp_rel_name(relname) (get_temp_rel_by_username(relname) != NULL)
32+
3133
#endif /* TEMPREL_H */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy