Prevent clobbering of utility statements in SQL function caches.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 29 Nov 2022 16:46:33 +0000 (11:46 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 29 Nov 2022 16:46:33 +0000 (11:46 -0500)
commit5dfc2b753b0f40ea036bcf621f827b800a422aac
treebb4cbc0027b6c9f2a631bc9a5da8d0fbdcdb2a22
parent556c0b913be150b92ee9fb250d2d5acc6d28f4c2
Prevent clobbering of utility statements in SQL function caches.

This is an oversight in commit 7c337b6b5: I apparently didn't think
about the possibility of a SQL function being executed multiple
times within a query.  In that case, functions.c's primitive caching
mechanism allows the same utility parse tree to be presented for
execution more than once.  We have to tell ProcessUtility to make
a working copy of the parse tree, or bad things happen.

Normally I'd add a regression test, but I think the reported crasher
is dependent on some rather random implementation choices that are
nowhere near functions.c, so its usefulness as a long-lived test
feels questionable.  In any case, this fix is clearly correct given
the design choices of 7c337b6b5.

Per bug #17702 from Xin Wen.  Thanks to Daniel Gustafsson for
analysis.  Back-patch to v14 where the faulty commit came in
(before that, the responsibility for copying scribble-able
utility parse trees lay elsewhere).

Discussion: https://postgr.es/m/17702-ad24fdcdd1e9047a@postgresql.org
src/backend/executor/functions.c