A couple weeks ago I said:
> The right long-term solution is to arrange for all query-lifetime data
> to be allocated in a specific context that can be freed in-toto when
> we end the query. To do this cleanly, we have to fix the executor to
> not scribble on plan trees (executor state nodes should point to plan
> nodes, never vice-versa). I've been intending to do that for awhile,
> but it's not done yet ...
Just FYI, this is now done. Your example doesn't leak memory anymore
in CVS tip.
regards, tom lane