Fix handling of R/W expanded datums that are passed to SQL functions.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 10 Aug 2022 17:37:25 +0000 (13:37 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 10 Aug 2022 17:37:25 +0000 (13:37 -0400)
commit309857f9c1825d0591579579bdde2a8c8bd3e491
tree2d187c6f8f5a4036f1a9b2bd6424c6556157e8ef
parent92dc33a3a229f7d24b8c97b6a18b417d79cf4d97
Fix handling of R/W expanded datums that are passed to SQL functions.

fmgr_sql must make expanded-datum arguments read-only, because
it's possible that the function body will pass the argument to
more than one callee function.  If one of those functions takes
the datum's R/W property as license to scribble on it, then later
callees will see an unexpected value, leading to wrong answers.

From a performance standpoint, it'd be nice to skip this in the
common case that the argument value is passed to only one callee.
However, detecting that seems fairly hard, and certainly not
something that I care to attempt in a back-patched bug fix.

Per report from Adam Mackler.  This has been broken since we
invented expanded datums, so back-patch to all supported branches.

Discussion: http://postgr.es/m/WScDU5qfoZ7PB2gXwNqwGGgDPmWzz08VdydcPFLhOwUKZcdWbblbo-0Lku-qhuEiZoXJ82jpiQU4hOjOcrevYEDeoAvz6nR0IU4IHhXnaCA=@mackler.email
Discussion: http://postgr.es/m/187436.1660143060@sss.pgh.pa.us
src/backend/executor/functions.c
src/test/regress/expected/create_function_sql.out
src/test/regress/sql/create_function_sql.sql