From 8e6ef0c33af0d8b2c5f5bbecd4e46b6ca79d8e64 Mon Sep 17 00:00:00 2001 From: Matthias van de Meent Date: Tue, 6 Oct 2026 16:29:30 +0200 Subject: [PATCH v3 6/6] relcache: Reduce number of allocations into shape cache Proxy contexts have low per-context overhead, but significant overhead per allocation. To reduce the per-context overhead, we allocate most shape arrays in one go, so that we avoid increasing the total overhead. --- src/backend/utils/cache/relcache.c | 40 +++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c index d1a0cb3b011..a8850f04c27 100644 --- a/src/backend/utils/cache/relcache.c +++ b/src/backend/utils/cache/relcache.c @@ -1697,28 +1697,50 @@ IndexSupportInitialize(oidvector *indclass, AttrNumber nKeyAtts, Oid *opcInType; RegProcedure *support; FmgrInfo *supportinfo; + size_t bulkallocSize = 0; + char *bulkalloc; MemoryContext shapectx; + int nprocs = maxSupportNumber * nKeyAtts; + + /* supportinfo */ + bulkallocSize += sizeof(FmgrInfo) * nprocs; + /* support */ + bulkallocSize += sizeof(RegProcedure) * nprocs; + /* opcInType */ + bulkallocSize += sizeof(Oid) * nKeyAtts; + /* opFamily */ + bulkallocSize += sizeof(Oid) * nKeyAtts; shapectx = ProxyContextCreate(RelShapeContext, "index shape"); + bulkalloc = MemoryContextAllocZero(shapectx, bulkallocSize); + if (maxSupportNumber > 0) { - int nprocs = maxSupportNumber * nKeyAtts; - supportinfo = (FmgrInfo *) - MemoryContextAllocZero(shapectx, nprocs * sizeof(FmgrInfo)); - support = (RegProcedure *) - MemoryContextAllocZero(shapectx, nprocs * sizeof(RegProcedure)); + supportinfo = (FmgrInfo *) bulkalloc; + bulkalloc += nprocs * sizeof(FmgrInfo); + bulkallocSize -= nprocs * sizeof(FmgrInfo); + + support = (RegProcedure *) bulkalloc; + bulkalloc += nprocs * sizeof(RegProcedure); + bulkallocSize -= nprocs * sizeof(RegProcedure); } else { + Assert(nprocs == 0); supportinfo = NULL; support = NULL; } - opFamily = (Oid *) - MemoryContextAllocZero(shapectx, nKeyAtts * sizeof(Oid)); - opcInType = (Oid *) - MemoryContextAllocZero(shapectx, nKeyAtts * sizeof(Oid)); + opFamily = (Oid *) bulkalloc; + bulkalloc += nKeyAtts * sizeof(Oid); + bulkallocSize -= nKeyAtts * sizeof(Oid); + + opcInType = (Oid *) bulkalloc; + bulkalloc += nKeyAtts * sizeof(Oid); + bulkallocSize -= nKeyAtts * sizeof(Oid); + + Assert(bulkallocSize == 0); for (int attIndex = 0; attIndex < nKeyAtts; attIndex++) { -- 2.54.0 (Apple Git-157)