pgsql: Enforce RETURNING typmod for empty-set JSON_ARRAY(query)

From: Richard Guo <rguo(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Enforce RETURNING typmod for empty-set JSON_ARRAY(query)
Date: 2026-05-08 08:24:19
Message-ID: E1wLGVP-000pa6-2B@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Enforce RETURNING typmod for empty-set JSON_ARRAY(query)

Commit 8d829f5a0 introduced a COALESCE wrapper around the
JSON_ARRAYAGG subquery so that JSON_ARRAY(query) returns '[]' rather
than NULL when the subquery yields no rows, per the SQL/JSON standard.

The empty-array Const used as the COALESCE fallback was, however,
built with typmod -1 and the type input function was likewise invoked
with typmod -1. As a result, any length restriction from the
RETURNING clause was silently bypassed on the empty-set path, while
the non-empty path enforced it via the JSON_ARRAYAGG coercion.

Build the empty-array Const using the typmod of the COALESCE's
non-empty argument, and pass that typmod to OidInputFunctionCall as
well so the value is length-checked at parse time. This makes the
empty-set and non-empty-set paths behave consistently.

Reported-by: Ayush Tiwari <ayushtiwari(dot)slg01(at)gmail(dot)com>
Author: Richard Guo <guofenglinux(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAJTYsWXPYqa58YXrU+SQMVonsAhjLS46HNUMU=wO5zm9MgY3_g@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/9d124a14b3d4803afee5efc4f794dfb72016cb88

Modified Files
--------------
src/backend/parser/parse_expr.c | 12 ++++++++----
src/test/regress/expected/sqljson.out | 19 +++++++++++++++++++
src/test/regress/sql/sqljson.sql | 8 ++++++++
3 files changed, 35 insertions(+), 4 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message John Naylor 2026-05-08 09:46:10 pgsql: Fix universal builds on MacOS
Previous Message Amit Kapila 2026-05-08 04:36:09 pgsql: Use schema-qualified names in EXCEPT clause error messages.