From 0e14dfe0fb4930303597811fd211bf1d4c499010 Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Sat, 4 Sep 1999 22:00:30 +0000
Subject: [PATCH] Intercept temp table lookups further up to map temp names.

---
 src/backend/catalog/heap.c         | 14 ++------------
 src/backend/catalog/index.c        | 11 +----------
 src/backend/catalog/indexing.c     | 11 +----------
 src/backend/utils/cache/syscache.c | 13 ++++++++++++-
 src/backend/utils/cache/temprel.c  | 10 +++++++++-
 5 files changed, 25 insertions(+), 34 deletions(-)

diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index 1592be2687b..b571cf3f817 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.93 1999/07/17 20:16:48 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.94 1999/09/04 22:00:29 momjian Exp $
  *
  *
  * INTERFACE ROUTINES
@@ -237,8 +237,7 @@ heap_create(char *relname,
 	if (istemp)
 	{
 		/* replace relname of caller */
-		snprintf(relname, NAMEDATALEN, "pg_temp.%d.%u",
-				 MyProcPid, uniqueId++);
+		snprintf(relname, NAMEDATALEN, "pg_temp.%d.%u", MyProcPid, uniqueId++);
 	}
 
 	/* ----------------
@@ -789,15 +788,6 @@ heap_create_with_catalog(char *relname,
 		(istemp && get_temp_rel_by_name(relname) != NULL))
 		elog(ERROR, "Relation '%s' already exists", relname);
 
-	/* invalidate cache so non-temp table is masked by temp */
-	if (istemp)
-	{
-		Oid			relid = RelnameFindRelid(relname);
-
-		if (relid != InvalidOid)
-			RelationForgetRelation(relid);
-	}
-
 	/* save user relation name because heap_create changes it */
 	if (istemp)
 	{
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index 5c44746cb2c..d52731bac23 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.87 1999/07/20 17:14:05 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.88 1999/09/04 22:00:29 momjian Exp $
  *
  *
  * INTERFACE ROUTINES
@@ -975,15 +975,6 @@ index_create(char *heapRelationName,
 												numatts,
 												attNums);
 
-	/* invalidate cache so possible non-temp index is masked by temp */
-	if (istemp)
-	{
-		Oid			relid = RelnameFindRelid(indexRelationName);
-
-		if (relid != InvalidOid)
-			RelationForgetRelation(relid);
-	}
-
 	/* save user relation name because heap_create changes it */
 	if (istemp)
 	{
diff --git a/src/backend/catalog/indexing.c b/src/backend/catalog/indexing.c
index f8a115582a5..9007258f2e8 100644
--- a/src/backend/catalog/indexing.c
+++ b/src/backend/catalog/indexing.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/catalog/indexing.c,v 1.43 1999/09/04 19:55:49 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/catalog/indexing.c,v 1.44 1999/09/04 22:00:29 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -24,7 +24,6 @@
 #include "catalog/pg_index.h"
 #include "miscadmin.h"
 #include "utils/syscache.h"
-#include "utils/temprel.h"
 
 /*
  * Names of indices on the following system catalogs:
@@ -449,15 +448,7 @@ ClassNameIndexScan(Relation heapRelation, char *relName)
 	Relation	idesc;
 	ScanKeyData skey[1];
 	HeapTuple	tuple;
-	char		*hold_rel;
 	
-	/*
-	 * we have to do this before looking in system tables because temp
-	 * table namespace takes precedence
-	 */
-	if ((hold_rel = get_temp_rel_by_name(relName)) != NULL)
-		relName = hold_rel;
-
 	ScanKeyEntryInitialize(&skey[0],
 						   (bits16) 0x0,
 						   (AttrNumber) 1,
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 470780676c2..c9c7770f8cc 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.34 1999/08/09 03:13:30 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/utils/cache/syscache.c,v 1.35 1999/09/04 22:00:30 momjian Exp $
  *
  * NOTES
  *	  These routines allow the parser/planner/executor to perform
@@ -35,6 +35,7 @@
 #include "catalog/pg_shadow.h"
 #include "catalog/pg_type.h"
 #include "utils/catcache.h"
+#include "utils/temprel.h"
 
 extern bool AMI_OVERRIDE;		/* XXX style */
 
@@ -487,6 +488,16 @@ SearchSysCacheTuple(int cacheId,/* cache selection code */
 				 cacheId);
 	}
 
+	/* temp table name remapping */
+	if (cacheId == RELNAME)
+	{
+		char *nontemp_relname;
+
+		if ((nontemp_relname =
+			 get_temp_rel_by_name(DatumGetPointer(key1))) != NULL)
+			key1 = PointerGetDatum(nontemp_relname);
+	}
+	
 	tp = SearchSysCache(SysCache[cacheId], key1, key2, key3, key4);
 	if (!HeapTupleIsValid(tp))
 	{
diff --git a/src/backend/utils/cache/temprel.c b/src/backend/utils/cache/temprel.c
index 75cfe14fbca..ece1c9519f6 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.12 1999/09/04 21:45:48 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/utils/cache/Attic/temprel.c,v 1.13 1999/09/04 22:00:30 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -117,6 +117,8 @@ remove_temp_relation(Oid relid)
 	MemoryContext oldcxt;
 	List	   *l,
 			   *prev;
+			   
+elog(NOTICE,"oid = %d", relid);
 
 	oldcxt = MemoryContextSwitchTo((MemoryContext) CacheCxt);
 
@@ -126,8 +128,11 @@ remove_temp_relation(Oid relid)
 	{
 		TempTable  *temp_rel = lfirst(l);
 
+elog(NOTICE,"check oid = %d", temp_rel->relid);
+
 		if (temp_rel->relid == relid)
 		{
+elog(NOTICE,"removed");
 			pfree(temp_rel->user_relname);
 			pfree(temp_rel->relname);
 			pfree(temp_rel);
@@ -212,7 +217,10 @@ get_temp_rel_by_name(char *user_relname)
 		TempTable  *temp_rel = lfirst(l);
 
 		if (strcmp(temp_rel->user_relname, user_relname) == 0)
+		{
+			elog(NOTICE,"found");
 			return temp_rel->relname;
+		}
 	}
 	return NULL;
 }
-- 
GitLab