1
0
mirror of https://github.com/postgres/postgres.git synced 2026-10-23 13:29:09 +03:00

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.  b0feda79fd
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 6185c9737c.

Author: Chaitanya Choudhary <chaitanyyachoudhary@gmail.com>
Author: Shihao Zhong <zhong950419@gmail.com>
Discussion: https://postgr.es/m/19693-2ecd2b52c838b3e5@postgresql.org
Backpatch-through: 17
This commit is contained in:
Michael Paquier
2026-09-19 20:13:56 +09:00
parent c86738fd4a
commit fdb88a51cd
3 changed files with 50 additions and 4 deletions

View File

@@ -3339,10 +3339,14 @@ JsonItemFromDatum(Datum val, Oid typid, int32 typmod, JsonbValue *res)
break;
case TEXTOID:
case VARCHAROID:
res->type = jbvString;
res->val.string.val = VARDATA_ANY(DatumGetPointer(val));
res->val.string.len = VARSIZE_ANY_EXHDR(DatumGetPointer(val));
break;
{
text *txt = DatumGetTextPP(val);
res->type = jbvString;
res->val.string.val = VARDATA_ANY(txt);
res->val.string.len = VARSIZE_ANY_EXHDR(txt);
break;
}
case DATEOID:
case TIMEOID:
case TIMETZOID:

View File

@@ -1473,6 +1473,32 @@ SELECT JSON_QUERY(jsonb 'null', '$"Xyz"' PASSING 1 AS "Xyz");
1
(1 row)
-- Test PASSING and toasted values
CREATE TABLE test_passing_toast (t text);
ALTER TABLE test_passing_toast ALTER COLUMN t SET STORAGE EXTENDED;
INSERT INTO test_passing_toast(t)
SELECT repeat(string_agg(to_char(g.i, 'FM0000'), ''), 50)
FROM generate_series(1, 500) g(i);
ALTER TABLE test_passing_toast ALTER COLUMN t SET STORAGE EXTERNAL;
INSERT INTO test_passing_toast(t)
SELECT repeat(string_agg(to_char(g.i, 'FM0000'), ''), 50)
FROM generate_series(1, 500) g(i);
SELECT count(*) AS toasted_chunks
FROM test_passing_toast WHERE pg_column_toast_chunk_id(t) IS NOT NULL;
toasted_chunks
----------------
2
(1 row)
SELECT JSON_VALUE(jsonb 'null', '$a' PASSING t AS a) = t AS ok
FROM test_passing_toast;
ok
----
t
t
(2 rows)
DROP TABLE test_passing_toast;
-- Test ON ERROR / EMPTY value validity for the function; all fail.
SELECT JSON_EXISTS(jsonb '1', '$' DEFAULT 1 ON ERROR);
ERROR: invalid ON ERROR behavior

View File

@@ -488,6 +488,22 @@ SELECT JSON_QUERY(jsonb 'null', '$Xyz' PASSING 1 AS Xyz);
SELECT JSON_QUERY(jsonb 'null', '$Xyz' PASSING 1 AS "Xyz");
SELECT JSON_QUERY(jsonb 'null', '$"Xyz"' PASSING 1 AS "Xyz");
-- Test PASSING and toasted values
CREATE TABLE test_passing_toast (t text);
ALTER TABLE test_passing_toast ALTER COLUMN t SET STORAGE EXTENDED;
INSERT INTO test_passing_toast(t)
SELECT repeat(string_agg(to_char(g.i, 'FM0000'), ''), 50)
FROM generate_series(1, 500) g(i);
ALTER TABLE test_passing_toast ALTER COLUMN t SET STORAGE EXTERNAL;
INSERT INTO test_passing_toast(t)
SELECT repeat(string_agg(to_char(g.i, 'FM0000'), ''), 50)
FROM generate_series(1, 500) g(i);
SELECT count(*) AS toasted_chunks
FROM test_passing_toast WHERE pg_column_toast_chunk_id(t) IS NOT NULL;
SELECT JSON_VALUE(jsonb 'null', '$a' PASSING t AS a) = t AS ok
FROM test_passing_toast;
DROP TABLE test_passing_toast;
-- Test ON ERROR / EMPTY value validity for the function; all fail.
SELECT JSON_EXISTS(jsonb '1', '$' DEFAULT 1 ON ERROR);
SELECT JSON_VALUE(jsonb '1', '$' EMPTY ON ERROR);