diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c index 4fb6ec60d4..ffd946a445 100644 --- a/src/backend/utils/cache/relcache.c +++ b/src/backend/utils/cache/relcache.c @@ -2643,20 +2643,22 @@ RelationClearRelation(Relation relation, bool rebuild) * no PartitionDirectory with a pointer to the old entry. * * To avoid leaking memory, we've got to make sure that the next - * time rd_pdoldcxt is destroyed, the old context will be freed. - * If rd_pdoldcxt is NULL, we can just make it point to the - * old descriptor's context; otherwise, we make this context of - * the previous one. + * time rd_pdoldcxt is destroyed, all the old contexts will be + * freed. So, if there's already an older context present, make + * this the parent of the previous one. * * Note that newrel and relation have already been swapped, so the - * "old" partition descriptor is actually the one hanging off of - * newrel. + * "old" partition descriptor and the associated "old" context are + * actually those that are hanging off of newrel. */ - if (relation->rd_pdoldcxt == NULL) - relation->rd_pdoldcxt = newrel->rd_pdcxt; - else - MemoryContextSetParent(newrel->rd_pdcxt, + Assert(relation->rd_pdoldcxt == NULL); + relation->rd_pdoldcxt = newrel->rd_pdcxt; + if (newrel->rd_pdoldcxt) + { + MemoryContextSetParent(newrel->rd_pdoldcxt, relation->rd_pdoldcxt); + newrel->rd_pdoldcxt = NULL; + } newrel->rd_partdesc = NULL; newrel->rd_pdcxt = NULL; }