int i;
/*
- * nbuckets is the number of hash buckets to use in this catcache.
- * Currently we just use a hard-wired estimate of an appropriate size for
- * each cache; maybe later make them dynamically resizable?
+ * nbuckets is the initial number of hash buckets to use in this catcache.
+ * It will be enlarged later if it becomes too full.
*
* nbuckets must be a power of two. We check this via Assert rather than
* a full runtime check because the values will be coming from constant
*
* Note: we rely on zeroing to initialize all the dlist headers correctly
*/
- cp = (CatCache *) palloc0(sizeof(CatCache) + nbuckets * sizeof(dlist_head));
+ cp = (CatCache *) palloc0(sizeof(CatCache));
+ cp->cc_bucket = palloc0(nbuckets * sizeof(dlist_head));
/*
* initialize the cache's relation information for the relation
return cp;
}
+/*
+ * Enlarge a catcache, doubling the number of buckets.
+ */
+static void
+RehashCatCache(CatCache *cp)
+{
+ dlist_head *newbucket;
+ int newnbuckets;
+ int i;
+
+ elog(DEBUG1, "rehashing catalog cache id %d for %s; %d tups, %d buckets",
+ cp->id, cp->cc_relname, cp->cc_ntup, cp->cc_nbuckets);
+
+ /* Allocate a new, larger, hash table. */
+ newnbuckets = cp->cc_nbuckets * 2;
+ newbucket = (dlist_head *) MemoryContextAllocZero(CacheMemoryContext, newnbuckets * sizeof(dlist_head));
+
+ /* Move all entries from old hash table to new. */
+ for (i = 0; i < cp->cc_nbuckets; i++)
+ {
+ dlist_mutable_iter iter;
+ dlist_foreach_modify(iter, &cp->cc_bucket[i])
+ {
+ CatCTup *ct = dlist_container(CatCTup, cache_elem, iter.cur);
+ int hashIndex = HASH_INDEX(ct->hash_value, newnbuckets);
+
+ dlist_delete(iter.cur);
+ dlist_push_head(&newbucket[hashIndex], &ct->cache_elem);
+ }
+ }
+
+ /* Switch to the new array. */
+ pfree(cp->cc_bucket);
+ cp->cc_nbuckets = newnbuckets;
+ cp->cc_bucket = newbucket;
+}
+
/*
* CatalogCacheInitializeCache
*
cache->cc_ntup++;
CacheHdr->ch_ntup++;
+ /*
+ * If the hash table has become too full, enlarge the buckets array.
+ * Quite arbitrarily, we enlarge when fill factor > 2.
+ */
+ if (cache->cc_ntup > cache->cc_nbuckets * 2)
+ RehashCatCache(cache);
+
return ct;
}
0,
0
},
- 32
+ 16
},
{AccessMethodRelationId, /* AMNAME */
AmNameIndexId,
Anum_pg_amproc_amprocrighttype,
Anum_pg_amproc_amprocnum
},
- 64
+ 16
},
{AttributeRelationId, /* ATTNAME */
AttributeRelidNameIndexId,
0,
0
},
- 2048
+ 32
},
{AttributeRelationId, /* ATTNUM */
AttributeRelidNumIndexId,
0,
0
},
- 2048
+ 128
},
{AuthMemRelationId, /* AUTHMEMMEMROLE */
AuthMemMemRoleIndexId,
0,
0
},
- 128
+ 8
},
{AuthMemRelationId, /* AUTHMEMROLEMEM */
AuthMemRoleMemIndexId,
0,
0
},
- 128
+ 8
},
{AuthIdRelationId, /* AUTHNAME */
AuthIdRolnameIndexId,
0,
0
},
- 128
+ 8
},
{AuthIdRelationId, /* AUTHOID */
AuthIdOidIndexId,
0,
0
},
- 128
+ 8
},
{
CastRelationId, /* CASTSOURCETARGET */
Anum_pg_opclass_opcnamespace,
0
},
- 64
+ 8
},
{OperatorClassRelationId, /* CLAOID */
OpclassOidIndexId,
0,
0
},
- 64
+ 8
},
{CollationRelationId, /* COLLNAMEENCNSP */
CollationNameEncNspIndexId,
Anum_pg_collation_collnamespace,
0
},
- 64
+ 8
},
{CollationRelationId, /* COLLOID */
CollationOidIndexId,
0,
0
},
- 64
+ 8
},
{ConversionRelationId, /* CONDEFAULT */
ConversionDefaultIndexId,
Anum_pg_conversion_contoencoding,
ObjectIdAttributeNumber,
},
- 128
+ 8
},
{ConversionRelationId, /* CONNAMENSP */
ConversionNameNspIndexId,
0,
0
},
- 128
+ 8
},
{ConstraintRelationId, /* CONSTROID */
ConstraintOidIndexId,
0,
0
},
- 1024
+ 16
},
{ConversionRelationId, /* CONVOID */
ConversionOidIndexId,
0,
0
},
- 128
+ 8
},
{DatabaseRelationId, /* DATABASEOID */
DatabaseOidIndexId,
Anum_pg_default_acl_defaclobjtype,
0
},
- 256
+ 8
},
{EnumRelationId, /* ENUMOID */
EnumOidIndexId,
0,
0
},
- 256
+ 8
},
{EnumRelationId, /* ENUMTYPOIDNAME */
EnumTypIdLabelIndexId,
0,
0
},
- 256
+ 8
},
{EventTriggerRelationId, /* EVENTTRIGGERNAME */
EventTriggerNameIndexId,
0,
0
},
- 8
+ 2
},
{ForeignDataWrapperRelationId, /* FOREIGNDATAWRAPPEROID */
ForeignDataWrapperOidIndexId,
0,
0
},
- 8
+ 2
},
{ForeignServerRelationId, /* FOREIGNSERVERNAME */
ForeignServerNameIndexId,
0,
0
},
- 32
+ 2
},
{ForeignServerRelationId, /* FOREIGNSERVEROID */
ForeignServerOidIndexId,
0,
0
},
- 32
+ 2
},
{ForeignTableRelationId, /* FOREIGNTABLEREL */
ForeignTableRelidIndexId,
0,
0
},
- 128
+ 4
},
{IndexRelationId, /* INDEXRELID */
IndexRelidIndexId,
0,
0
},
- 1024
+ 64
},
{LanguageRelationId, /* LANGNAME */
LanguageNameIndexId,
0,
0
},
- 256
+ 4
},
{NamespaceRelationId, /* NAMESPACEOID */
NamespaceOidIndexId,
0,
0
},
- 256
+ 16
},
{OperatorRelationId, /* OPERNAMENSP */
OperatorNameNspIndexId,
Anum_pg_operator_oprright,
Anum_pg_operator_oprnamespace
},
- 1024
+ 256
},
{OperatorRelationId, /* OPEROID */
OperatorOidIndexId,
0,
0
},
- 1024
+ 32
},
{OperatorFamilyRelationId, /* OPFAMILYAMNAMENSP */
OpfamilyAmNameNspIndexId,
Anum_pg_opfamily_opfnamespace,
0
},
- 64
+ 8
},
{OperatorFamilyRelationId, /* OPFAMILYOID */
OpfamilyOidIndexId,
0,
0
},
- 64
+ 8
},
{ProcedureRelationId, /* PROCNAMEARGSNSP */
ProcedureNameArgsNspIndexId,
Anum_pg_proc_pronamespace,
0
},
- 2048
+ 128
},
{ProcedureRelationId, /* PROCOID */
ProcedureOidIndexId,
0,
0
},
- 2048
+ 128
},
{RangeRelationId, /* RANGETYPE */
RangeTypidIndexId,
0,
0
},
- 64
+ 4
},
{RelationRelationId, /* RELNAMENSP */
ClassNameNspIndexId,
0,
0
},
- 1024
+ 128
},
{RelationRelationId, /* RELOID */
ClassOidIndexId,
0,
0
},
- 1024
+ 128
},
{RewriteRelationId, /* RULERELNAME */
RewriteRelRulenameIndexId,
0,
0
},
- 1024
+ 8
},
{StatisticRelationId, /* STATRELATTINH */
StatisticRelidAttnumInhIndexId,
Anum_pg_statistic_stainherit,
0
},
- 1024
+ 128
},
{TableSpaceRelationId, /* TABLESPACEOID */
TablespaceOidIndexId,
0,
0,
},
- 16
+ 4
},
{TSConfigMapRelationId, /* TSCONFIGMAP */
TSConfigMapIndexId,
Anum_pg_ts_config_map_mapseqno,
0
},
- 4
+ 2
},
{TSConfigRelationId, /* TSCONFIGNAMENSP */
TSConfigNameNspIndexId,
0,
0
},
- 16
+ 2
},
{TSConfigRelationId, /* TSCONFIGOID */
TSConfigOidIndexId,
0,
0
},
- 16
+ 2
},
{TSDictionaryRelationId, /* TSDICTNAMENSP */
TSDictionaryNameNspIndexId,
0,
0
},
- 16
+ 2
},
{TSDictionaryRelationId, /* TSDICTOID */
TSDictionaryOidIndexId,
0,
0
},
- 16
+ 2
},
{TSParserRelationId, /* TSPARSERNAMENSP */
TSParserNameNspIndexId,
0,
0
},
- 4
+ 2
},
{TSParserRelationId, /* TSPARSEROID */
TSParserOidIndexId,
0,
0
},
- 4
+ 2
},
{TSTemplateRelationId, /* TSTEMPLATENAMENSP */
TSTemplateNameNspIndexId,
0,
0
},
- 16
+ 2
},
{TSTemplateRelationId, /* TSTEMPLATEOID */
TSTemplateOidIndexId,
0,
0
},
- 16
+ 2
},
{TypeRelationId, /* TYPENAMENSP */
TypeNameNspIndexId,
0,
0
},
- 1024
+ 64
},
{TypeRelationId, /* TYPEOID */
TypeOidIndexId,
0,
0
},
- 1024
+ 64
},
{UserMappingRelationId, /* USERMAPPINGOID */
UserMappingOidIndexId,
0,
0
},
- 128
+ 2
},
{UserMappingRelationId, /* USERMAPPINGUSERSERVER */
UserMappingUserServerIndexId,
0,
0
},
- 128
+ 2
}
};