]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Enforce RETURNING typmod on SQL/JSON DEFAULT behavior expressions
authorAmit Langote <amitlan@postgresql.org>
Mon, 6 Jul 2026 23:13:28 +0000 (08:13 +0900)
committerAmit Langote <amitlan@postgresql.org>
Mon, 6 Jul 2026 23:26:50 +0000 (08:26 +0900)
commit71cd10cd249baae25bb420ea4a6d5eae9fe1dd5f
treec744be5e16af48cd9a12ec77e2e187193dab4710
parent52af6fef43755de40472149c9d7f1adc9bf77b5e
Enforce RETURNING typmod on SQL/JSON DEFAULT behavior expressions

transformJsonBehavior() coerced an ON EMPTY / ON ERROR DEFAULT
expression only when its type differed from the RETURNING type's OID.
When the base type matched but the RETURNING type carried a type
modifier (e.g. numeric(4,1) or varchar(3)), the coercion that enforces
the typmod was skipped, so the DEFAULT value could violate the
declared type:

    SELECT JSON_VALUE(jsonb '{}', '$.a'
                      RETURNING numeric(4,1) DEFAULT 99999.999 ON EMPTY);

returned 99999.999, which 99999.999::numeric(4,1) would reject; the
value could even be stored into a numeric(4,1) column, as later
coercions trust its already-correct type label.

Fix by also coercing when the RETURNING type has a typmod, except for
a NULL constant.  coerce_to_target_type() is a no-op when the typmod
already matches.  The matching-OID short-circuit dates to 74c96699be3.

Reported-by: Ewan Young <kdbase.hack@gmail.com>
Author: Ewan Young <kdbase.hack@gmail.com>
Discussion: https://postgr.es/m/CAON2xHPO9f4cAmyGn1mQ=VqoS7wN5rz4yOiqudxX78zninZpCw@mail.gmail.com
Backpatch-through: 17
src/backend/parser/parse_expr.c
src/test/regress/expected/sqljson_jsontable.out
src/test/regress/expected/sqljson_queryfuncs.out
src/test/regress/sql/sqljson_jsontable.sql
src/test/regress/sql/sqljson_queryfuncs.sql