From dc5c7713bcd620e4b8cee6c282e55afdd2a97749 Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Tue, 16 Nov 1999 04:14:03 +0000
Subject: [PATCH] Commit to make clearer distinction for temp names and real
 names. Thanks to Tom Lane for ideas.

---
 src/backend/catalog/heap.c          | 16 ++++++++--------
 src/backend/catalog/index.c         | 12 ++++++------
 src/backend/storage/buffer/bufmgr.c |  4 ++--
 src/backend/storage/smgr/md.c       |  8 ++++----
 src/backend/utils/cache/relcache.c  |  6 +++---
 src/backend/utils/cache/syscache.c  |  4 ++--
 src/backend/utils/cache/temprel.c   | 24 +++++++++++++++++++++---
 src/include/utils/rel.h             | 24 +++++++++++++++++++++---
 src/include/utils/temprel.h         |  5 +++--
 9 files changed, 70 insertions(+), 33 deletions(-)

diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index fe05d241947..cc0ce0293b6 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.107 1999/11/07 23:08:00 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.108 1999/11/16 04:13:55 momjian Exp $
  *
  *
  * INTERFACE ROUTINES
@@ -290,7 +290,7 @@ heap_create(char *relname,
 	 * ----------------
 	 */
 	MemSet((char *) rel->rd_rel, 0, sizeof *rel->rd_rel);
-	strcpy(RelationGetRelationName(rel), relname);
+	strcpy(RelationGetPhysicalRelationName(rel), relname);
 	rel->rd_rel->relkind = RELKIND_UNCATALOGED;
 	rel->rd_rel->relnatts = natts;
 	if (tupDesc->constr)
@@ -798,7 +798,7 @@ heap_create_with_catalog(char *relname,
 
 	/* temp tables can mask non-temp tables */
 	if ((!istemp && RelnameFindRelid(relname)) ||
-		(istemp && get_temp_rel_by_name(relname) != NULL))
+		(istemp && get_temp_rel_by_username(relname) != NULL))
 		elog(ERROR, "Relation '%s' already exists", relname);
 
 	/* save user relation name because heap_create changes it */
@@ -810,7 +810,7 @@ heap_create_with_catalog(char *relname,
 	}
 
 	/* ----------------
-	 *	get_temp_rel_by_name() couldn't check the simultaneous
+	 *	get_temp_rel_by_username() couldn't check the simultaneous
 	 *	creation. Uniqueness will be really checked by unique
 	 *	indexes of system tables but we couldn't check it here.
 	 *	We have to pospone to create the disk file for this
@@ -1448,7 +1448,7 @@ heap_destroy_with_catalog(char *relname)
 {
 	Relation	rel;
 	Oid			rid;
-	bool		istemp = (get_temp_rel_by_name(relname) != NULL);
+	bool		istemp = (get_temp_rel_by_username(relname) != NULL);
 
 	/* ----------------
 	 *	Open and lock the relation.
@@ -1518,9 +1518,6 @@ heap_destroy_with_catalog(char *relname)
 
 	DeleteComments(RelationGetRelid(rel));
 
-	if (istemp)
-		remove_temp_relation(rid);
-
 	/* ----------------
 	 *	delete type tuple.	here we want to see the effects
 	 *	of the deletions we just did, so we use setheapoverride().
@@ -1565,6 +1562,9 @@ heap_destroy_with_catalog(char *relname)
 	 * ----------------
 	 */
 	RelationForgetRelation(rid);
+
+	if (istemp)
+		remove_temp_relation(rid);
 }
 
 /*
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index 65d297a2508..203d3f1ef80 100644
--- a/src/backend/catalog/index.c
+++ b/src/backend/catalog/index.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.94 1999/11/04 08:00:56 inoue Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.95 1999/11/16 04:13:55 momjian Exp $
  *
  *
  * INTERFACE ROUTINES
@@ -120,7 +120,7 @@ GetHeapRelationOid(char *heapRelationName, char *indexRelationName, bool istemp)
 	indoid = RelnameFindRelid(indexRelationName);
 
 	if ((!istemp && OidIsValid(indoid)) ||
-		(istemp && get_temp_rel_by_name(indexRelationName) != NULL))
+		(istemp && get_temp_rel_by_username(indexRelationName) != NULL))
 		elog(ERROR, "Cannot create index: '%s' already exists",
 			 indexRelationName);
 
@@ -948,7 +948,7 @@ index_create(char *heapRelationName,
 	Oid			heapoid;
 	Oid			indexoid;
 	PredInfo   *predInfo;
-	bool		istemp = (get_temp_rel_by_name(heapRelationName) != NULL);
+	bool		istemp = (get_temp_rel_by_username(heapRelationName) != NULL);
 	char	   *temp_relname = NULL;
 
 	/* ----------------
@@ -1182,9 +1182,6 @@ index_destroy(Oid indexId)
 	}
 	heap_close(attributeRelation, RowExclusiveLock);
 
-	/* does something only if it is a temp index */
-	remove_temp_relation(indexId);
-
 	/* ----------------
 	 * fix INDEX relation
 	 * ----------------
@@ -1211,6 +1208,9 @@ index_destroy(Oid indexId)
 	index_close(userindexRelation);
 
 	RelationForgetRelation(indexId);
+
+	/* does something only if it is a temp index */
+	remove_temp_relation(indexId);
 }
 
 /* ----------------------------------------------------------------
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 19bbceb6b1e..bd5773e98e5 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/storage/buffer/bufmgr.c,v 1.65 1999/11/07 23:08:14 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/storage/buffer/bufmgr.c,v 1.66 1999/11/16 04:13:56 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -622,7 +622,7 @@ BufferAlloc(Relation reln,
 	}
 
 	/* record the database name and relation name for this buffer */
-	strcpy(buf->sb_relname, RelationGetRelationName(reln));
+	strcpy(buf->sb_relname, RelationGetPhysicalRelationName(reln));
 	strcpy(buf->sb_dbname, DatabaseName);
 
 	INIT_BUFFERTAG(&(buf->tag), reln, blockNum);
diff --git a/src/backend/storage/smgr/md.c b/src/backend/storage/smgr/md.c
index bf885e676d5..0cf2893d79c 100644
--- a/src/backend/storage/smgr/md.c
+++ b/src/backend/storage/smgr/md.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/storage/smgr/md.c,v 1.59 1999/11/07 23:08:19 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/storage/smgr/md.c,v 1.60 1999/11/16 04:13:56 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -121,7 +121,7 @@ mdcreate(Relation reln)
 	char	   *path;
 
 	Assert(reln->rd_unlinked && reln->rd_fd < 0);
-	path = relpath(RelationGetRelationName(reln));
+	path = relpath(RelationGetPhysicalRelationName(reln));
 #ifndef __CYGWIN32__
 	fd = FileNameOpenFile(path, O_RDWR | O_CREAT | O_EXCL, 0600);
 #else
@@ -319,7 +319,7 @@ mdopen(Relation reln)
 	int			vfd;
 
 	Assert(reln->rd_fd < 0);
-	path = relpath(RelationGetRelationName(reln));
+	path = relpath(RelationGetPhysicalRelationName(reln));
 
 #ifndef __CYGWIN32__
 	fd = FileNameOpenFile(path, O_RDWR, 0600);
@@ -1011,7 +1011,7 @@ _mdfd_openseg(Relation reln, int segno, int oflags)
 			   *fullpath;
 
 	/* be sure we have enough space for the '.segno', if any */
-	path = relpath(RelationGetRelationName(reln));
+	path = relpath(RelationGetPhysicalRelationName(reln));
 
 	dofree = false;
 	if (segno > 0)
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index f780cf96b39..a0b97158602 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.76 1999/11/07 23:08:26 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.77 1999/11/16 04:13:58 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -962,7 +962,7 @@ formrdesc(char *relationName,
 	relation->rd_rel = (Form_pg_class)
 		palloc((Size) (sizeof(*relation->rd_rel)));
 	MemSet(relation->rd_rel, 0, sizeof(FormData_pg_class));
-	strcpy(RelationGetRelationName(relation), relationName);
+	strcpy(RelationGetPhysicalRelationName(relation), relationName);
 
 	/* ----------------
 	   initialize attribute tuple form
@@ -1177,7 +1177,7 @@ RelationNameGetRelation(char *relationName)
 	 *	we only index temp rels by their real names.
 	 * ----------------
 	 */
-	temprelname = get_temp_rel_by_name(relationName);
+	temprelname = get_temp_rel_by_username(relationName);
 	if (temprelname)
 		relationName = temprelname;
 
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 3cb393b802f..476fce4db8c 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/utils/cache/syscache.c,v 1.38 1999/11/01 02:29:25 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/utils/cache/syscache.c,v 1.39 1999/11/16 04:13:59 momjian Exp $
  *
  * NOTES
  *	  These routines allow the parser/planner/executor to perform
@@ -472,7 +472,7 @@ SearchSysCacheTuple(int cacheId,/* cache selection code */
 		char *nontemp_relname;
 
 		if ((nontemp_relname =
-			 get_temp_rel_by_name(DatumGetPointer(key1))) != NULL)
+			 get_temp_rel_by_username(DatumGetPointer(key1))) != NULL)
 			key1 = PointerGetDatum(nontemp_relname);
 	}
 	
diff --git a/src/backend/utils/cache/temprel.c b/src/backend/utils/cache/temprel.c
index 0119f9e59ec..d57152d550d 100644
--- a/src/backend/utils/cache/temprel.c
+++ b/src/backend/utils/cache/temprel.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/utils/cache/Attic/temprel.c,v 1.15 1999/11/07 23:08:26 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/utils/cache/Attic/temprel.c,v 1.16 1999/11/16 04:13:59 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -150,7 +150,6 @@ remove_temp_relation(Oid relid)
 			prev = l;
 			l = lnext(l);
 		}
-
 	}
 
 	MemoryContextSwitchTo(oldcxt);
@@ -203,7 +202,7 @@ invalidate_temp_relations(void)
 }
 
 char *
-get_temp_rel_by_name(char *user_relname)
+get_temp_rel_by_username(char *user_relname)
 {
 	List	   *l;
 
@@ -216,3 +215,22 @@ get_temp_rel_by_name(char *user_relname)
 	}
 	return NULL;
 }
+
+char *
+get_temp_rel_by_physicalname(char *relname)
+{
+	List	   *l;
+
+	/* already physical, needed for bootstrapping temp tables */
+	if (strncmp(relname,"pg_temp.", strlen("pg_temp.")) == 0)
+		return relname;
+		
+	foreach(l, temp_rels)
+	{
+		TempTable  *temp_rel = lfirst(l);
+
+		if (strcmp(temp_rel->relname, relname) == 0)
+			return temp_rel->user_relname;
+	}
+	return NULL;
+}
diff --git a/src/include/utils/rel.h b/src/include/utils/rel.h
index b22ad294cea..689d5d02950 100644
--- a/src/include/utils/rel.h
+++ b/src/include/utils/rel.h
@@ -6,7 +6,7 @@
  *
  * Copyright (c) 1994, Regents of the University of California
  *
- * $Id: rel.h,v 1.29 1999/11/07 23:08:33 momjian Exp $
+ * $Id: rel.h,v 1.30 1999/11/16 04:14:03 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -20,7 +20,6 @@
 #include "rewrite/prs2lock.h"
 #include "storage/fd.h"
 
-
 /*
  * LockRelId and LockInfo really belong to lmgr.h, but it's more convenient
  * to declare them here so we can have a LockInfoData field in a Relation.
@@ -176,7 +175,26 @@ typedef Relation *RelationPtr;
  *
  *	  Returns a Relation Name
  */
-#define RelationGetRelationName(relation) (NameStr((relation)->rd_rel->relname))
+/* added to prevent circular dependency.  bjm 1999/11/15 */
+char 	   *get_temp_rel_by_physicalname(char *relname);
+#define RelationGetRelationName(relation) \
+(\
+	(strncmp(RelationGetPhysicalRelationName(relation), \
+	 "pg_temp.", strlen("pg_temp.")) != 0) \
+	? \
+		RelationGetPhysicalRelationName(relation) \
+	: \
+		get_temp_rel_by_physicalname( \
+			RelationGetPhysicalRelationName(relation)) \
+)
+
+
+/*
+ * RelationGetPhysicalRelationName
+ *
+ *	  Returns a Relation Name
+ */
+#define RelationGetPhysicalRelationName(relation) (NameStr((relation)->rd_rel->relname))
 
 /*
  * RelationGetNumberOfAttributes
diff --git a/src/include/utils/temprel.h b/src/include/utils/temprel.h
index 248a6e134e0..39e5157bd4b 100644
--- a/src/include/utils/temprel.h
+++ b/src/include/utils/temprel.h
@@ -6,7 +6,7 @@
  *
  * Copyright (c) 1994, Regents of the University of California
  *
- * $Id: temprel.h,v 1.5 1999/09/04 19:55:50 momjian Exp $
+ * $Id: temprel.h,v 1.6 1999/11/16 04:14:03 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -19,6 +19,7 @@ void		create_temp_relation(char *relname, HeapTuple pg_class_tuple);
 void		remove_all_temp_relations(void);
 void		invalidate_temp_relations(void);
 void		remove_temp_relation(Oid relid);
-char 	   *get_temp_rel_by_name(char *user_relname);
+char 	   *get_temp_rel_by_username(char *user_relname);
+char 	   *get_temp_rel_by_physicalname(char *relname);
 
 #endif	 /* TEMPREL_H */
-- 
GitLab