diff --git a/src/backend/utils/cache/partcache.c b/src/backend/utils/cache/partcache.c index 3107075c9ad6..b25751b3793f 100644 --- a/src/backend/utils/cache/partcache.c +++ b/src/backend/utils/cache/partcache.c @@ -404,21 +404,28 @@ generate_partition_qual(Relation rel) /* * Save a copy in the relcache. The order of these operations is fairly * critical to avoid memory leaks and ensure that we don't leave a corrupt - * relcache entry if we fail partway through copyObject. + * relcache entry if we fail partway through copyObject. rd_partcheckcxt + * and rd_partcheck are assigned last, after copyObject() succeeds. * * If, as is definitely possible, the partcheck list is NIL, then we do * not need to make a context to hold it. */ if (result != NIL) { - rel->rd_partcheckcxt = AllocSetContextCreate(CacheMemoryContext, - "partition constraint", - ALLOCSET_SMALL_SIZES); - MemoryContextCopyAndSetIdentifier(rel->rd_partcheckcxt, + MemoryContext partcheckcxt; + List *partcheck; + + partcheckcxt = AllocSetContextCreate(CacheMemoryContext, + "partition constraint", + ALLOCSET_SMALL_SIZES); + MemoryContextCopyAndSetIdentifier(partcheckcxt, RelationGetRelationName(rel)); - oldcxt = MemoryContextSwitchTo(rel->rd_partcheckcxt); - rel->rd_partcheck = copyObject(result); + oldcxt = MemoryContextSwitchTo(partcheckcxt); + partcheck = copyObject(result); MemoryContextSwitchTo(oldcxt); + + rel->rd_partcheckcxt = partcheckcxt; + rel->rd_partcheck = partcheck; } else rel->rd_partcheck = NIL;