diff --git a/src/backend/utils/cache/partcache.c b/src/backend/utils/cache/partcache.c index 2b55f25e75..5f0b4dbc0c 100644 --- a/src/backend/utils/cache/partcache.c +++ b/src/backend/utils/cache/partcache.c @@ -373,7 +373,12 @@ generate_partition_qual(Relation rel) elog(ERROR, "unexpected whole-row reference found in partition key"); /* Save a copy in the relcache */ - oldcxt = MemoryContextSwitchTo(CacheMemoryContext); + rel->rd_partcheckcxt = AllocSetContextCreate(CacheMemoryContext, + "partition constraint", + ALLOCSET_DEFAULT_SIZES); + MemoryContextCopyAndSetIdentifier(rel->rd_partcheckcxt, + RelationGetRelationName(rel)); + oldcxt = MemoryContextSwitchTo(rel->rd_partcheckcxt); rel->rd_partcheck = copyObject(result); MemoryContextSwitchTo(oldcxt); diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c index eba77491fd..383570340a 100644 --- a/src/backend/utils/cache/relcache.c +++ b/src/backend/utils/cache/relcache.c @@ -1203,6 +1203,12 @@ RelationBuildDesc(Oid targetRelId, bool insertIt) /* It's fully valid */ relation->rd_isvalid = true; + /* + * Initialized by generate_partition_qual() if it's ever called in this + * session. + */ + relation->rd_partcheckcxt = NULL; + return relation; } @@ -2313,8 +2319,8 @@ RelationDestroyRelation(Relation relation, bool remember_tupdesc) MemoryContextDelete(relation->rd_partkeycxt); if (relation->rd_pdcxt) MemoryContextDelete(relation->rd_pdcxt); - if (relation->rd_partcheck) - pfree(relation->rd_partcheck); + if (relation->rd_partcheckcxt) + MemoryContextDelete(relation->rd_partcheckcxt); if (relation->rd_fdwroutine) pfree(relation->rd_fdwroutine); pfree(relation); @@ -5544,6 +5550,7 @@ load_relcache_init_file(bool shared) rel->rd_partkey = NULL; rel->rd_pdcxt = NULL; rel->rd_partdesc = NULL; + rel->rd_partcheckcxt = NULL; rel->rd_partcheck = NIL; rel->rd_indexprs = NIL; rel->rd_indpred = NIL; diff --git a/src/include/utils/rel.h b/src/include/utils/rel.h index 54028515a7..c2eae7ef62 100644 --- a/src/include/utils/rel.h +++ b/src/include/utils/rel.h @@ -198,6 +198,9 @@ typedef struct RelationData /* use "struct" here to avoid needing to include pgstat.h: */ struct PgStat_TableStatus *pgstat_info; /* statistics collection area */ + + /* Memory context to hold the content of rd_partcheck. */ + MemoryContext rd_partcheckcxt; } RelationData;