From 80dab5bd69cd5bcd064a7da9f3d1b22eb770925a Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Fri, 24 Nov 2000 04:16:12 +0000
Subject: [PATCH] A catalog cache that never caches isn't much of a cache :-(. 
 Mea culpa. Thanks to Brian Hirt for pointing out the performance lossage.

---
 src/backend/utils/cache/catcache.c | 28 +++++++++++++++-------------
 src/include/utils/catcache.h       |  4 ++--
 2 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/src/backend/utils/cache/catcache.c b/src/backend/utils/cache/catcache.c
index 3d8e7d80ba8..3e1c428a532 100644
--- a/src/backend/utils/cache/catcache.c
+++ b/src/backend/utils/cache/catcache.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/utils/cache/catcache.c,v 1.72 2000/11/16 22:30:33 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/utils/cache/catcache.c,v 1.73 2000/11/24 04:16:12 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -258,8 +258,8 @@ CatalogCacheInitializeCache(CatCache *cache)
 				  &cache->cc_skey[i].sk_func);
 		cache->cc_skey[i].sk_nargs = cache->cc_skey[i].sk_func.fn_nargs;
 
-		/* Initialize sk_attno suitably for index scans */
-		cache->cc_skey[i].sk_attno = i+1;
+		/* Initialize sk_attno suitably for HeapKeyTest() and heap scans */
+		cache->cc_skey[i].sk_attno = cache->cc_key[i];
 
 		CACHE4_elog(DEBUG, "CatalogCacheInit %s %d %p",
 					cache->cc_relname,
@@ -664,8 +664,8 @@ InitCatCache(int id,
 
 	/* ----------------
 	 *	initialize the cache's relation information for the relation
-	 *	corresponding to this cache and initialize some of the the new
-	 *	cache's other internal fields.
+	 *	corresponding to this cache, and initialize some of the new
+	 *	cache's other internal fields.  But don't open the relation yet.
 	 * ----------------
 	 */
 	cp->cc_relname = relname;
@@ -885,10 +885,20 @@ SearchCatCache(CatCache *cache,
 		RetrieveIndexResult indexRes;
 		HeapTupleData tuple;
 		Buffer		buffer;
+		int			i;
 
 		CACHE2_elog(DEBUG, "SearchCatCache(%s): performing index scan",
 					cache->cc_relname);
 
+		/*
+		 * For an index scan, sk_attno has to be set to the index attribute
+		 * number(s), not the heap attribute numbers.  We assume that the
+		 * index corresponds exactly to the cache keys (or its first N
+		 * keys do, anyway).
+		 */
+		for (i = 0; i < cache->cc_nkeys; ++i)
+			cur_skey[i].sk_attno = i+1;
+
 		idesc = index_openr(cache->cc_indname);
 		isd = index_beginscan(idesc, false, cache->cc_nkeys, cur_skey);
 		tuple.t_datamcxt = CurrentMemoryContext;
@@ -915,18 +925,10 @@ SearchCatCache(CatCache *cache,
 	else
 	{
 		HeapScanDesc sd;
-		int			i;
 
 		CACHE2_elog(DEBUG, "SearchCatCache(%s): performing heap scan",
 					cache->cc_relname);
 
-		/*
-		 * For a heap scan, sk_attno has to be set to the heap attribute
-		 * number(s), not the index attribute numbers.
-		 */
-		for (i = 0; i < cache->cc_nkeys; ++i)
-			cur_skey[i].sk_attno = cache->cc_key[i];
-
 		sd = heap_beginscan(relation, 0, SnapshotNow,
 							cache->cc_nkeys, cur_skey);
 
diff --git a/src/include/utils/catcache.h b/src/include/utils/catcache.h
index ed78284a478..70c988ca185 100644
--- a/src/include/utils/catcache.h
+++ b/src/include/utils/catcache.h
@@ -13,7 +13,7 @@
  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: catcache.h,v 1.28 2000/11/16 22:30:49 tgl Exp $
+ * $Id: catcache.h,v 1.29 2000/11/24 04:16:11 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -70,7 +70,7 @@ typedef struct catcache
 	short		cc_nkeys;		/* number of keys (1..4) */
 	short		cc_key[4];		/* AttrNumber of each key */
 	PGFunction	cc_hashfunc[4]; /* hash function to use for each key */
-	ScanKeyData cc_skey[4];		/* precomputed key info for indexscans */
+	ScanKeyData cc_skey[4];		/* precomputed key info for heap scans */
 	Dllist		cc_lrulist;		/* overall LRU list, most recent first */
 	Dllist		cc_cache[NCCBUCK]; /* hash buckets */
 } CatCache;
-- 
GitLab