From e6b92542a39b50d0c02f90ef56aa6930e26eca81 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Thu, 6 Oct 2005 22:43:16 +0000
Subject: [PATCH] Marginal speedup in RelationIsVisible and TypeIsVisible:
 avoid a redundant cache lookup in the success case.  This won't help much for
 cases where the given relation is far down the search path, but it does not
 hurt in any cases either; and it requires only a little new code.  Per gripe
 from Jim Nasby about slowness of \d with many tables.

---
 src/backend/catalog/namespace.c | 49 ++++++++++++++++++++++++++++-----
 1 file changed, 42 insertions(+), 7 deletions(-)

diff --git a/src/backend/catalog/namespace.c b/src/backend/catalog/namespace.c
index e74b331e376..dc627e42880 100644
--- a/src/backend/catalog/namespace.c
+++ b/src/backend/catalog/namespace.c
@@ -13,7 +13,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/catalog/namespace.c,v 1.77 2005/08/01 04:03:54 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/catalog/namespace.c,v 1.78 2005/10/06 22:43:16 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -335,12 +335,28 @@ RelationIsVisible(Oid relid)
 		/*
 		 * If it is in the path, it might still not be visible; it could
 		 * be hidden by another relation of the same name earlier in the
-		 * path. So we must do a slow check to see if this rel would be
-		 * found by RelnameGetRelid.
+		 * path. So we must do a slow check for conflicting relations.
 		 */
 		char	   *relname = NameStr(relform->relname);
+		ListCell   *l;
 
-		visible = (RelnameGetRelid(relname) == relid);
+		visible = false;
+		foreach(l, namespaceSearchPath)
+		{
+			Oid			namespaceId = lfirst_oid(l);
+
+			if (namespaceId == relnamespace)
+			{
+				/* Found it first in path */
+				visible = true;
+				break;
+			}
+			if (OidIsValid(get_relname_relid(relname, namespaceId)))
+			{
+				/* Found something else first in path */
+				break;
+			}
+		}
 	}
 
 	ReleaseSysCache(reltup);
@@ -417,12 +433,31 @@ TypeIsVisible(Oid typid)
 		/*
 		 * If it is in the path, it might still not be visible; it could
 		 * be hidden by another type of the same name earlier in the path.
-		 * So we must do a slow check to see if this type would be found
-		 * by TypenameGetTypid.
+		 * So we must do a slow check for conflicting types.
 		 */
 		char	   *typname = NameStr(typform->typname);
+		ListCell   *l;
 
-		visible = (TypenameGetTypid(typname) == typid);
+		visible = false;
+		foreach(l, namespaceSearchPath)
+		{
+			Oid			namespaceId = lfirst_oid(l);
+
+			if (namespaceId == typnamespace)
+			{
+				/* Found it first in path */
+				visible = true;
+				break;
+			}
+			if (SearchSysCacheExists(TYPENAMENSP,
+									 PointerGetDatum(typname),
+									 ObjectIdGetDatum(namespaceId),
+									 0, 0))
+			{
+				/* Found something else first in path */
+				break;
+			}
+		}
 	}
 
 	ReleaseSysCache(typtup);
-- 
GitLab