Fix memory leakage in plpgsql DO blocks that use cast expressions.
authorTom Lane
Mon, 24 Apr 2023 18:19:46 +0000 (14:19 -0400)
committerTom Lane
Mon, 24 Apr 2023 18:19:46 +0000 (14:19 -0400)
commit2ba890ce7ed2b1362174f5a6686b94dcd9628949
tree47f58da38ac879849bf064ff20cbde8b6fd877af
parent34e09e71aca51a9e722ff3ec819c0d92f4247bdd
Fix memory leakage in plpgsql DO blocks that use cast expressions.

Commit 04fe805a1 modified plpgsql so that datatype casts make use of
expressions cached by plancache.c, in place of older code where these
expression trees were managed by plpgsql itself.  However, I (tgl)
forgot that we use a separate, shorter-lived cast info hashtable in
DO blocks.  The new mechanism thus resulted in session-lifespan
leakage of the plancache data once a DO block containing one or more
casts terminated.  To fix, split the cast hash table into two parts,
one that tracks only the plancache's CachedExpressions and one that
tracks the expression state trees generated from them.  DO blocks need
their own expression state trees and hence their own version of the
second hash table, but there's no reason they can't share the
CachedExpressions with regular plpgsql functions.

Per report from Ajit Awekar.  Back-patch to v12 where the issue
was introduced.

Ajit Awekar and Tom Lane

Discussion: https://postgr.es/m/CAHv6PyrNaqdvyWUspzd3txYQguFTBSnhx+m6tS06TnM+KWc_LQ@mail.gmail.com
src/pl/plpgsql/src/pl_exec.c
src/pl/plpgsql/src/plpgsql.h