Fix PASSING of toasted text values in JSON query functions

The PASSING arguments of JSON_EXISTS(), JSON_QUERY(), JSON_VALUE() and
JSON_TABLE() are evaluated by plain expression steps and given to the
jsonpath executor as-is.

When dealing with a varlena, JsonItemFromDatum() was building a
jbvString directly the contents of a varlena.  For a varatt_external
(varatt_external[oid/oid8] on HEAD), this ignored that the contents of
the varlena need to be detoasted, leading to incorrect contents
returned.

Let's detoast the value, like the jsonb and json cases.  b0feda79fdf0
has fixed the same issue for subscript values in jsonb.

Note that the results of DatumGetTextPP() are allocated in the memory
context used for the evaluation of the expression, which is reset once
per row.

Oversight in 6185c9737cf4.

Author: Chaitanya Choudhary <[email protected]>
Author: Shihao Zhong <[email protected]>
Discussion: https://postgr.es/m/[email protected]
Backpatch-through: 17

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/fdb88a51cda0ec0e09b986ec68bcea3935424794

Modified Files
--------------
src/backend/utils/adt/jsonpath_exec.c            | 12 +++++++----
src/test/regress/expected/sqljson_queryfuncs.out | 26 ++++++++++++++++++++++++
src/test/regress/sql/sqljson_queryfuncs.sql      | 16 +++++++++++++++
3 files changed, 50 insertions(+), 4 deletions(-)

Reply via email to