On 2019-Mar-13, Robert Haas wrote: > RelationBuildPartitionDesc() creates basically all of the data > structures it needs and then copies them into rel->rd_pdcxt, which has > always seemed a bit inefficient to me. Another way to redesign this > would be to have the function create a temporary context, do all of > its work there, and then reparent the context under CacheMemoryContext > at the end. That means any leaks would go into a relatively > long-lifespan context, but on the other hand you wouldn't leak into > the same context a zillion times over, and you'd save the expense of > copying everything. I think that the biggest thing that is being > copied around here is the partition bounds, so maybe the leak wouldn't > amount to much, and we could also do things like list_free(inhoids) to > make it a little tighter.
I remember going over this code's memory allocation strategy a bit to avoid the copy while not incurring potential leaks CacheMemoryContext; as I recall, my idea was to use two contexts, one of which is temporary and used for any potentially leaky callees, and destroyed at the end of the function, and the other contains the good stuff and is reparented to CacheMemoryContext at the end. So if you have any accidental leaks, they don't affect a long-lived context. You have to be mindful of not calling leaky code when you're using the permanent one. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services