diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index fcd778c62a..4cf80d5bde 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -17472,6 +17472,24 @@ ATExecAttachPartition(List **wqueue, Relation rel, PartitionCmd *cmd, ObjectAddressSet(address, RelationRelationId, RelationGetRelid(attachrel)); + /* + * If the partition we just attached is partitioned itself, invalidate + * relcache for all descendent partitions too to ensure that their + * rd_partcheck expression trees are rebuilt; partitions already locked + * at the beginning of this function. + */ + if (attachrel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE) + { + ListCell *l; + + foreach(l, attachrel_children) + { + Oid partOid = lfirst(l); + + CacheInvalidateRelcacheByRelid(partOid); + } + } + /* keep our lock until commit */ table_close(attachrel, NoLock); @@ -18162,6 +18180,26 @@ DetachPartitionFinalize(Relation rel, Relation partRel, bool concurrent, * included in its partition descriptor. */ CacheInvalidateRelcache(rel); + + /* + * If the partition we just detached is partitioned itself, invalidate + * relcache for all descendent partitions too to ensure that their + * rd_partcheck expression trees are rebuilt; must lock partitions + * before doing so. + */ + if (partRel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE) + { + List *partRel_children = + find_all_inheritors(RelationGetRelid(partRel), + AccessExclusiveLock, NULL); + + foreach(cell, partRel_children) + { + Oid partOid = lfirst(l); + + CacheInvalidateRelcacheByRelid(partOid); + } + } } /*