Re: BUG #19736: `JSON_OBJECT ... ABSENT ON NULL` accepts invalid keys when the value is NULL

From: Joao Rito <joaovictor(dot)rito(at)gmail(dot)com>
To: Yeongjae Min <dudwo990413(at)gmail(dot)com>
Cc: Dmitry Dolgov <9erthalion6(at)gmail(dot)com>, pgsql-hackers(at)lists(dot)postgresql(dot)org, pgsql-bugs(at)lists(dot)postgresql(dot)org, theshallow27(at)gmail(dot)com
Subject: Re: BUG #19736: `JSON_OBJECT ... ABSENT ON NULL` accepts invalid keys when the value is NULL
Date: 2026-11-15 08:46:50
Message-ID: 20261115084650.45010-1-joaovictor.rito@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-hackers

Hi Yeongjae,

Thanks for the patch. I tested v3 on current master (81b2179b492, built
with cassert and tap tests) and it applies cleanly. On master, with a
NULL key and a NULL value, JSON_OBJECT ... ABSENT ON NULL RETURNING json
returns {} instead of an error. The same happens for an array key, a ROW
key, and json and jsonb keys, with RETURNING json and jsonb, and for a
domain over int[] with RETURNING json. A jsonb NULL key with a NULL
value already fails on master ("argument 1: key must not be null"), so
the NULL key problem is only in the json path. Two aggregate cases,
json_object_agg_strict(ARRAY[1], NULL::text) and
jsonb_object_agg_strict('{"a":1}'::jsonb, NULL::text), also returned an
empty object on master. Dmitry already raised the aggregate case. I only
confirm it on current master.

With the patch all of these raise an error: "null value not allowed for
object key" for the json path and "key value must be scalar, not array,
composite, or json" for non-scalar keys. A composite key with a cast to
json and a NULL value is now rejected, as it already was with a non-NULL
value. A valid key with a NULL value is still omitted (tested with text,
int, numeric, bool, timestamptz, name, bytea, int4range and enum keys).
A NULL key with a non-NULL value still fails as on master. With WITH
UNIQUE KEYS, duplicate keys with omitted values and a NULL key with a
NULL value (json) give the same result as master. json_build_object,
jsonb_build_object and json_object(text[]) also give the same output as
master. The regress suite passes (243 subtests) and the server log has
no TRAP or assertion lines.

What I did not test: check-world, the isolation and tap suites,
pgindent, the docs change and its build, the back branches, other
platforms (I ran one Linux build), planning or performance effects, and
a detailed read of the C code.

I found no problem with the patch from what I could test.

Regards,
Joao Rito

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Zsolt Parragi 2026-11-15 16:15:02 Re: BUG #19897: Recovery fails after "LOG: consistent recovery state reached"
Previous Message David Rowley 2026-11-15 06:54:49 Re: BUG #19898: int4_avg_accum_inv wraps below INT64_MIN instead of raising an error

Browse pgsql-hackers by date

  From Date Subject
Next Message Chengpeng Yan 2026-11-15 08:51:47 Re: Add a greedy join search algorithm to handle large join problems
Previous Message Joao Rito 2026-11-15 08:26:41 Re: XMLConcat() and XMLRoot() silently drop xml values with an invalid declaration