Hmm, count_leaf_partitions has to scan pg_inherits and do a syscache lookup for every single element therein ... this sounds slow.
In one of the callsites, we already have the partition descriptor available. We could just scan partdesc->is_leaf[] and add one for each 'true' value we see there. In the other callsite, we had the table open just a few lines before the place you call count_leaf_partitions. Maybe we can rejigger things by examining its state before closing it: if relkind is not partitioned we know leaf_partitions=0, and only if partitioned we count leaf partitions. I think that would save some work. I also wonder if it's worth writing a bespoke function for counting leaf partitions rather than relying on find_all_inheritors. I think there's probably not much point optimizing it further than that. If there was, then we could think about creating a data representation that we can build for the entire partitioning hierarchy in a single pass with the count of leaf partitions that sit below each specific non-leaf; but I think that's just over-engineering. -- Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/ "After a quick R of TFM, all I can say is HOLY CR** THAT IS COOL! PostgreSQL was amazing when I first started using it at 7.2, and I'm continually astounded by learning new features and techniques made available by the continuing work of the development team." Berend Tober, http://archives.postgresql.org/pgsql-hackers/2007-08/msg01009.php