The simplest fix for this would be to give up on the idea that DO blocks use private simple_eval_estates, and make them use the shared one. However, that would result in intra-transaction memory bloat for transactions executing large numbers of DO blocks; see commit c7b849a89, which installed that arrangement to begin with. Since that change was based on a user complaint, this answer doesn't seem appetizing.
...
Or we could change things so that DO blocks use private cast_hash hashtables along with their private simple_eval_estates. This would give up some efficiency (since a DO block would then always need to do its own cast lookups) but it would be a simple and reliable fix.
I'm kind of inclined to go with the last choice, but I wonder if anyone wants to argue differently, or sees another feasible solution.
Not everyone uses large numbers of DO blocks, but casts apply everywhere, right?
If the choice is efficiency or memory bloat, can't we choose a point where we switch from one to the other? i.e. use private structures after N uses of the shared structures.
--
Simon Riggs http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services