Re: BUG #19971: JSON_VALUE RETURNING json drops the quotes of a jsonb string DEFAULT

From: Abed Alawieh <abedalaweye(at)gmail(dot)com>
To: e1638616(at)u(dot)nus(dot)edu, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19971: JSON_VALUE RETURNING json drops the quotes of a jsonb string DEFAULT
Date: 2026-12-05 23:36:34
Message-ID: CAAr_ne4--jni0JSGuZV-ZRCs5S_VSxZnatXQ5ge5yZAx_q4AqQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi,

Thanks for the report. I can reproduce this on master.

JSON_VALUE omits the quotes of a scalar string result, except when it
returns json or jsonb: ExecEvalJsonExprPath() keeps the quotes in that
case. But a jsonb-valued ON EMPTY or ON ERROR DEFAULT is coerced at run
time with the quotes omitted whatever the RETURNING type is, so
the DEFAULT loses them. A json-valued DEFAULT and RETURNING jsonb aren't
affected, because the first is cast when the query is parsed and the second
needs no coercion.

The attached patch keeps the quotes when coercing the DEFAULT of a
JSON_VALUE that returns json or jsonb, which makes the DEFAULT behave like
a string found by the path:

SELECT JSON_VALUE(jsonb '{}', '$.a' RETURNING json
DEFAULT '"123"'::jsonb ON EMPTY);
json_value
------------
"123"

It adds regression tests for ON EMPTY and ON ERROR to sqljson_queryfuncs.
The fix doesn't change any node or catalog contents, so I think it could be
back-patched to 17, where JSON_VALUE was added.

A related point I left alone: with RETURNING a domain over json, both the
path result and the DEFAULT lose their quotes, because the exact-type check
in ExecEvalJsonExprPath() doesn't look through domains. That seems to be a
separate question.

Regards,
Abed Alawieh

On Sun, Dec 6, 2026 at 1:28 AM PG Bug reporting form <noreply(at)postgresql(dot)org>
wrote:

> The following bug has been logged on the website:
>
> Bug reference: 19971
> Logged by: Yibo Dong
> Email address: e1638616(at)u(dot)nus(dot)edu
> PostgreSQL version: 19.0
> Operating system: macOS 15 arm64.
> Description:
>
> With RETURNING json, a jsonb DEFAULT "123" is returned as the number 123,
> and "abc" fails with "invalid input syntax for type json". A JSON string
> found by the path, a json DEFAULT and RETURNING jsonb all keep the quotes.
> A domain over jsonb as the RETURNING type and DEFAULT ... ON ERROR behave
> the same way.
>
> Steps to reproduce:
>
> SELECT JSON_VALUE(jsonb '"123"', '$' RETURNING json) AS path_json,
> JSON_VALUE(jsonb '{}', '$.a' RETURNING json DEFAULT '"123"'::json ON
> EMPTY) AS json_default,
> JSON_VALUE(jsonb '{}', '$.a' RETURNING jsonb DEFAULT '"123"'::jsonb
> ON EMPTY) AS jsonb_to_jsonb,
> JSON_VALUE(jsonb '{}', '$.a' RETURNING json DEFAULT '"123"'::jsonb
> ON
> EMPTY) AS jsonb_to_json;
>
> Actual result:
>
> path_json | json_default | jsonb_to_jsonb | jsonb_to_json
> -----------+--------------+----------------+---------------
> "123" | "123" | "123" | 123
>
> Expected result: jsonb_to_json is "123", like the other three columns.
>
> PostgreSQL 19.0 (Homebrew), macOS 15 arm64.
>
>
>
>
>
>
>

Attachment Content-Type Size
v1-0001-Keep-the-quotes-of-a-jsonb-string-DEFAULT-in-JSON.patch application/octet-stream 5.0 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Abed Alawieh 2026-12-05 23:39:07 Re: BUG #19970: json #> finds a nested key under an earlier duplicate key
Previous Message Ishjaap Singh 2026-12-05 23:14:10 Re: BUG #19936: Planning is slower on 19.0 with many partial indexes on a SQL function