Don't corrupt plpython's "TD" dictionary in a recursive trigger call.
commitc5bec5426af989f2a9b0c1f4b97340798fe153f7
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 7 May 2024 22:15:00 +0000 (7 18:15 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 7 May 2024 22:15:00 +0000 (7 18:15 -0400)
treee77c0ca40ed0b527954d45100492456035f7a2b2
parent509199587df73f06eda898ae13284292f4ae573a
Don't corrupt plpython's "TD" dictionary in a recursive trigger call.

If a plpython-language trigger caused another one to be invoked,
the "TD" dictionary created for the inner one would overwrite the
outer one's "TD" dictionary.  This is more or less the same problem
that 1d2fe56e4 fixed for ordinary functions in plpython, so fix it
the same way, by saving and restoring "TD" during a recursive
invocation.

This fix makes an ABI-incompatible change in struct PLySavedArgs.
I'm not too worried about that because it seems highly unlikely that
any extension is messing with those structs.  We could imagine doing
something weird to preserve nominal ABI compatibility in the back
branches, like keeping the saved TD object in an extra element of
namedargs[].  However, that would only be very nominal compatibility:
if anything *is* touching PLySavedArgs, it would likely do the wrong
thing due to not knowing about the additional value.  So I judge it
not worth the ugliness to do something different there.

(I also changed struct PLyProcedure, but its added field fits
into formerly-padding space, so that should be safe.)

Per bug #18456 from Jacques Combrink.  This bug is very ancient,
so back-patch to all supported branches.

Discussion: https://postgr.es/m/3008982.1714853799@sss.pgh.pa.us
src/pl/plpython/expected/plpython_trigger.out
src/pl/plpython/plpy_exec.c
src/pl/plpython/plpy_procedure.c
src/pl/plpython/plpy_procedure.h
src/pl/plpython/sql/plpython_trigger.sql