Commit 2c5aa2ac authored by Tom Lane's avatar Tom Lane

Do some restructuring to improve performance of the catcaches. Teach

CatalogCacheFlushRelation (formerly called SystemCacheRelationFlushed)
how to distinguish tuples it should flush from those it needn't; this
means a relcache flush event now only removes the catcache entries
it ought to, rather than zapping the caches completely as it used to.
Testing with the regression tests indicates that this considerably
improves the lifespan of catcache entries.  Also, rearrange catcache
data structures so that the limit on number of cached tuples applies
globally across all the catcaches, rather than being per-catcache.
It was a little silly to have the same size limit on both, say,
pg_attribute caches and pg_am caches (there being only four possible
rows in the latter...).  Doing LRU removal across all the caches
instead of locally in each one should reduce cache reload traffic
in the more heavily used caches and improve the efficiency of
cache memory use.
parent 41c377f5
This diff is collapsed.
......@@ -34,7 +34,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/cache/inval.c,v 1.43 2001/06/01 20:23:06 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/cache/inval.c,v 1.44 2001/06/18 03:35:07 tgl Exp $
*
* Note - this code is real crufty... badly needs a rewrite to improve
* readability and portability. (Shouldn't assume Oid == Index, for example)
......@@ -478,16 +478,20 @@ CacheIdInvalidate(Index cacheId,
}
/*
* ResetSystemCaches
* InvalidateSystemCaches
*
* This blows away all tuples in the system catalog caches and
* all the cached relation descriptors (and closes their files too).
* Relation descriptors that have positive refcounts are then rebuilt.
*
* We call this when we see a shared-inval-queue overflow signal,
* since that tells us we've lost some shared-inval messages and hence
* don't know what needs to be invalidated.
*/
static void
ResetSystemCaches(void)
InvalidateSystemCaches(void)
{
ResetSystemCache();
ResetCatalogCaches();
RelationCacheInvalidate();
}
......@@ -643,7 +647,7 @@ DiscardInvalid(void)
elog(DEBUG, "DiscardInvalid called");
#endif /* defined(INVALIDDEBUG) */
InvalidateSharedInvalid(CacheIdInvalidate, ResetSystemCaches);
InvalidateSharedInvalid(CacheIdInvalidate, InvalidateSystemCaches);
}
/*
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.137 2001/06/12 05:55:49 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.138 2001/06/18 03:35:07 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -1657,7 +1657,7 @@ RelationClearRelation(Relation relation, bool rebuildIt)
MemoryContextSwitchTo(oldcxt);
/* Clear out catcache's entries for this relation */
SystemCacheRelationFlushed(RelationGetRelid(relation));
CatalogCacheFlushRelation(RelationGetRelid(relation));
/*
* Free all the subsidiary data structures of the relcache entry. We
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/cache/syscache.c,v 1.62 2001/06/12 05:55:49 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/cache/syscache.c,v 1.63 2001/06/18 03:35:07 tgl Exp $
*
* NOTES
* These routines allow the parser/planner/executor to perform
......@@ -54,7 +54,12 @@
Add your entry to the cacheinfo[] array below. All cache lists are
alphabetical, so add it in the proper place. Specify the relation
name, index name, number of keys, and key attribute numbers.
name, index name, number of keys, and key attribute numbers. If the
relation contains tuples that are associated with a particular relation
(for example, its attributes, rules, triggers, etc) then specify the
attribute number that contains the OID of the associated relation.
This is used by CatalogCacheFlushRelation() to remove the correct
tuples during a table drop or relcache invalidation event.
In include/catalog/indexing.h, add a define for the number of indexes
on the relation, add define(s) for the index name(s), add an extern
......@@ -76,12 +81,12 @@
/*
* struct cachedesc: information defining a single syscache
*
*/
struct cachedesc
{
char *name; /* name of the relation being cached */
char *indname; /* name of index relation for this cache */
int reloidattr; /* attr number of rel OID reference, or 0 */
int nkeys; /* # of keys needed for cache lookup */
int key[4]; /* attribute numbers of key attrs */
};
......@@ -89,6 +94,7 @@ struct cachedesc
static struct cachedesc cacheinfo[] = {
{AggregateRelationName, /* AGGNAME */
AggregateNameTypeIndex,
0,
2,
{
Anum_pg_aggregate_aggname,
......@@ -98,6 +104,7 @@ static struct cachedesc cacheinfo[] = {
}},
{AccessMethodRelationName, /* AMNAME */
AmNameIndex,
0,
1,
{
Anum_pg_am_amname,
......@@ -107,6 +114,7 @@ static struct cachedesc cacheinfo[] = {
}},
{AccessMethodOperatorRelationName, /* AMOPOPID */
AccessMethodOpidIndex,
0,
3,
{
Anum_pg_amop_amopclaid,
......@@ -116,6 +124,7 @@ static struct cachedesc cacheinfo[] = {
}},
{AccessMethodOperatorRelationName, /* AMOPSTRATEGY */
AccessMethodStrategyIndex,
0,
3,
{
Anum_pg_amop_amopid,
......@@ -125,6 +134,7 @@ static struct cachedesc cacheinfo[] = {
}},
{AttributeRelationName, /* ATTNAME */
AttributeRelidNameIndex,
Anum_pg_attribute_attrelid,
2,
{
Anum_pg_attribute_attrelid,
......@@ -134,6 +144,7 @@ static struct cachedesc cacheinfo[] = {
}},
{AttributeRelationName, /* ATTNUM */
AttributeRelidNumIndex,
Anum_pg_attribute_attrelid,
2,
{
Anum_pg_attribute_attrelid,
......@@ -143,6 +154,7 @@ static struct cachedesc cacheinfo[] = {
}},
{OperatorClassRelationName, /* CLADEFTYPE */
OpclassDeftypeIndex,
0,
1,
{
Anum_pg_opclass_opcdeftype,
......@@ -152,6 +164,7 @@ static struct cachedesc cacheinfo[] = {
}},
{OperatorClassRelationName, /* CLANAME */
OpclassNameIndex,
0,
1,
{
Anum_pg_opclass_opcname,
......@@ -161,6 +174,7 @@ static struct cachedesc cacheinfo[] = {
}},
{GroupRelationName, /* GRONAME */
GroupNameIndex,
0,
1,
{
Anum_pg_group_groname,
......@@ -170,6 +184,7 @@ static struct cachedesc cacheinfo[] = {
}},
{GroupRelationName, /* GROSYSID */
GroupSysidIndex,
0,
1,
{
Anum_pg_group_grosysid,
......@@ -179,6 +194,7 @@ static struct cachedesc cacheinfo[] = {
}},
{IndexRelationName, /* INDEXRELID */
IndexRelidIndex,
Anum_pg_index_indrelid,
1,
{
Anum_pg_index_indexrelid,
......@@ -188,6 +204,7 @@ static struct cachedesc cacheinfo[] = {
}},
{InheritsRelationName, /* INHRELID */
InheritsRelidSeqnoIndex,
Anum_pg_inherits_inhrelid,
2,
{
Anum_pg_inherits_inhrelid,
......@@ -197,6 +214,7 @@ static struct cachedesc cacheinfo[] = {
}},
{LanguageRelationName, /* LANGNAME */
LanguageNameIndex,
0,
1,
{
Anum_pg_language_lanname,
......@@ -206,6 +224,7 @@ static struct cachedesc cacheinfo[] = {
}},
{LanguageRelationName, /* LANGOID */
LanguageOidIndex,
0,
1,
{
ObjectIdAttributeNumber,
......@@ -215,6 +234,7 @@ static struct cachedesc cacheinfo[] = {
}},
{OperatorRelationName, /* OPERNAME */
OperatorNameIndex,
0,
4,
{
Anum_pg_operator_oprname,
......@@ -224,6 +244,7 @@ static struct cachedesc cacheinfo[] = {
}},
{OperatorRelationName, /* OPEROID */
OperatorOidIndex,
0,
1,
{
ObjectIdAttributeNumber,
......@@ -233,6 +254,7 @@ static struct cachedesc cacheinfo[] = {
}},
{ProcedureRelationName, /* PROCNAME */
ProcedureNameIndex,
0,
3,
{
Anum_pg_proc_proname,
......@@ -242,6 +264,7 @@ static struct cachedesc cacheinfo[] = {
}},
{ProcedureRelationName, /* PROCOID */
ProcedureOidIndex,
0,
1,
{
ObjectIdAttributeNumber,
......@@ -251,6 +274,7 @@ static struct cachedesc cacheinfo[] = {
}},
{RelationRelationName, /* RELNAME */
ClassNameIndex,
ObjectIdAttributeNumber,
1,
{
Anum_pg_class_relname,
......@@ -260,6 +284,7 @@ static struct cachedesc cacheinfo[] = {
}},
{RelationRelationName, /* RELOID */
ClassOidIndex,
ObjectIdAttributeNumber,
1,
{
ObjectIdAttributeNumber,
......@@ -269,6 +294,7 @@ static struct cachedesc cacheinfo[] = {
}},
{RewriteRelationName, /* RULENAME */
RewriteRulenameIndex,
Anum_pg_rewrite_ev_class,
1,
{
Anum_pg_rewrite_rulename,
......@@ -278,6 +304,7 @@ static struct cachedesc cacheinfo[] = {
}},
{ShadowRelationName, /* SHADOWNAME */
ShadowNameIndex,
0,
1,
{
Anum_pg_shadow_usename,
......@@ -287,6 +314,7 @@ static struct cachedesc cacheinfo[] = {
}},
{ShadowRelationName, /* SHADOWSYSID */
ShadowSysidIndex,
0,
1,
{
Anum_pg_shadow_usesysid,
......@@ -296,6 +324,7 @@ static struct cachedesc cacheinfo[] = {
}},
{StatisticRelationName, /* STATRELATT */
StatisticRelidAttnumIndex,
Anum_pg_statistic_starelid,
2,
{
Anum_pg_statistic_starelid,
......@@ -305,6 +334,7 @@ static struct cachedesc cacheinfo[] = {
}},
{TypeRelationName, /* TYPENAME */
TypeNameIndex,
Anum_pg_type_typrelid,
1,
{
Anum_pg_type_typname,
......@@ -314,6 +344,7 @@ static struct cachedesc cacheinfo[] = {
}},
{TypeRelationName, /* TYPEOID */
TypeOidIndex,
Anum_pg_type_typrelid,
1,
{
ObjectIdAttributeNumber,
......@@ -323,8 +354,7 @@ static struct cachedesc cacheinfo[] = {
}}
};
static CatCache *SysCache[
lengthof(cacheinfo)];
static CatCache *SysCache[lengthof(cacheinfo)];
static int SysCacheSize = lengthof(cacheinfo);
static bool CacheInitialized = false;
......@@ -358,6 +388,7 @@ InitCatalogCache(void)
SysCache[cacheId] = InitCatCache(cacheId,
cacheinfo[cacheId].name,
cacheinfo[cacheId].indname,
cacheinfo[cacheId].reloidattr,
cacheinfo[cacheId].nkeys,
cacheinfo[cacheId].key);
if (!PointerIsValid(SysCache[cacheId]))
......
......@@ -13,32 +13,49 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: catcache.h,v 1.32 2001/03/22 04:01:11 momjian Exp $
* $Id: catcache.h,v 1.33 2001/06/18 03:35:07 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#ifndef CATCACHE_H
#define CATCACHE_H
/* #define CACHEDEBUG *//* turns DEBUG elogs on */
#include "access/htup.h"
#include "lib/dllist.h"
/*
* struct catctup: individual tuple in the cache.
* struct catcache: information for managing a cache.
* struct catcacheheader: information for managing all the caches.
*/
typedef struct catcache
{
int id; /* cache identifier --- see syscache.h */
struct catcache *cc_next; /* link to next catcache */
char *cc_relname; /* name of relation the tuples come from */
char *cc_indname; /* name of index matching cache keys */
int cc_reloidattr; /* AttrNumber of relation OID, or 0 */
TupleDesc cc_tupdesc; /* tuple descriptor (copied from reldesc) */
int cc_ntup; /* # of tuples currently in this cache */
int cc_size; /* # of hash buckets in this cache */
int cc_nkeys; /* number of keys (1..4) */
int 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 heap scans */
Dllist cc_bucket[1]; /* hash buckets --- VARIABLE LENGTH ARRAY */
} CatCache; /* VARIABLE LENGTH STRUCT */
typedef struct catctup
{
int ct_magic; /* for Assert checks */
#define CT_MAGIC 0x57261502
CatCache *my_cache; /* link to owning catcache */
/*
* Each tuple in a cache is a member of two lists: one lists all the
* elements in that cache in LRU order, and the other lists just the
* elements in one hashbucket, also in LRU order.
* elements in all the caches in LRU order, and the other lists just
* the elements in one hashbucket of one cache, also in LRU order.
*
* A tuple marked "dead" must not be returned by subsequent searches.
* However, it won't be physically deleted from the cache until its
......@@ -52,30 +69,14 @@ typedef struct catctup
} CatCTup;
/* voodoo constants */
#define NCCBUCK 500 /* CatCache buckets */
#define MAXTUP 500 /* Maximum # of tuples stored per cache */
typedef struct catcache
typedef struct catcacheheader
{
int id; /* cache identifier --- see syscache.h */
struct catcache *cc_next; /* link to next catcache */
char *cc_relname; /* name of relation the tuples come from */
char *cc_indname; /* name of index matching cache keys */
TupleDesc cc_tupdesc; /* tuple descriptor (copied from reldesc) */
short cc_ntup; /* # of tuples in this cache */
short cc_maxtup; /* max # of tuples allowed (LRU) */
short cc_size; /* # of hash buckets in this cache */
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 heap scans */
Dllist cc_lrulist; /* overall LRU list, most recent first */
Dllist cc_cache[NCCBUCK]; /* hash buckets */
} CatCache;
CatCache *ch_caches; /* head of list of CatCache structs */
int ch_ntup; /* # of tuples in all caches */
int ch_maxtup; /* max # of tuples allowed (LRU) */
Dllist ch_lrulist; /* overall LRU list, most recent first */
} CatCacheHeader;
#define InvalidCatalogCacheId (-1)
/* this extern duplicates utils/memutils.h... */
extern MemoryContext CacheMemoryContext;
......@@ -84,6 +85,7 @@ extern void CreateCacheMemoryContext(void);
extern void AtEOXact_CatCache(bool isCommit);
extern CatCache *InitCatCache(int id, char *relname, char *indname,
int reloidattr,
int nkeys, int *key);
extern HeapTuple SearchCatCache(CatCache *cache,
......@@ -91,8 +93,8 @@ extern HeapTuple SearchCatCache(CatCache *cache,
Datum v3, Datum v4);
extern void ReleaseCatCache(HeapTuple tuple);
extern void ResetSystemCache(void);
extern void SystemCacheRelationFlushed(Oid relId);
extern void ResetCatalogCaches(void);
extern void CatalogCacheFlushRelation(Oid relId);
extern void CatalogCacheIdInvalidate(int cacheId, Index hashIndex,
ItemPointer pointer);
extern void PrepareToInvalidateCacheTuple(Relation relation,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment