Re: BUG #19970: json #> finds a nested key under an earlier duplicate key

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 #19970: json #> finds a nested key under an earlier duplicate key
Date: 2026-12-05 23:39:07
Message-ID: CAAr_ne7eBkBHSkiRXQF-kFb+utgEx_F3HsK4yeeULNcM4hwL9g@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. The docs say of
json values with duplicate keys that "the processing functions consider the
last value as the operative one", and a chain of -> operators does so, but
the path functions (#>, #>>, json_extract_path() and
json_extract_path_text()) keep a value found under an earlier duplicate of
an intermediate key.
While looking at this I found a second bug in the same code, which doesn't
need duplicate keys:

SELECT '{"x": [1, 2, 3], "a": [4, 5]}'::json #> '{a,-1}';

returns NULL instead of 5. get_array_start() converts a negative subscript
by modifying path_indexes in place, for the first array it meets at that
nesting level, even when that array isn't on the path, and the converted
subscript is then used for the array that is. jsonb is not affected.

Two patches are attached:

0001 fixes the negative subscripts: the subscript is resolved only for an
array on the path, and kept in a separate per-level array so that
path_indexes stays intact. This also skips counting the elements of arrays
that aren't on the path. It has no catalog or ABI impact, so I think it
can be back-patched, though older branches may need small adjustments
around json_count_array_elements() and the error reporting.

0002 fixes the reported bug: whenever an intermediate key of the path
matches again, any result found so far is discarded. It depends on 0001
for duplicate keys combined with negative subscripts, e.g. '{"a": [4], "a":
[1, 2, 3]}'::json #> '{a,-1}', which now returns 3.

Both patches add regression tests to json.sql. 0002 turns some non-NULL
results into NULL for json with duplicate keys, behavior that dates back to
9.3, so you may prefer to apply it to master only.

Regards,
Abed Alawieh

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

> The following bug has been logged on the website:
>
> Bug reference: 19970
> Logged by: Yibo Dong
> Email address: e1638616(at)u(dot)nus(dot)edu
> PostgreSQL version: 19.0
> Operating system: macOS 15 arm64.
> Description:
>
> For json with duplicate keys, the last value is the operative one, and
> -> 'a' returns {"c": 2}. #> '{a,b}' and json_extract_path(j, 'a', 'b')
> (also #>> and json_extract_path_text) return 1 from the earlier "a",
> while -> 'a' -> 'b' and the jsonb cast return NULL.
>
> Steps to reproduce:
>
> SELECT '{"a": {"b": 1}, "a": {"c": 2}}'::json -> 'a' AS a_value,
> '{"a": {"b": 1}, "a": {"c": 2}}'::json -> 'a' -> 'b' AS chained,
> '{"a": {"b": 1}, "a": {"c": 2}}'::json #> '{a,b}' AS path_op,
> json_extract_path('{"a": {"b": 1}, "a": {"c": 2}}', 'a', 'b') AS
> extract_path,
> '{"a": {"b": 1}, "a": {"c": 2}}'::json::jsonb #> '{a,b}' AS
> via_jsonb;
>
> Actual result (psql -P null=NULL):
>
> a_value | chained | path_op | extract_path | via_jsonb
> ----------+---------+---------+--------------+-----------
> {"c": 2} | NULL | 1 | 1 | NULL
>
> Expected result: path_op and extract_path are NULL, like chained and
> via_jsonb.
>
> PostgreSQL 19.0 (Homebrew), macOS 15 arm64.
>
>
>
>
>
>
>

Attachment Content-Type Size
v1-0001-Resolve-negative-json-path-subscripts-only-for-th.patch application/octet-stream 5.5 KB
v1-0002-Use-the-last-duplicate-key-at-every-level-of-json.patch application/octet-stream 4.4 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Previous Message Abed Alawieh 2026-12-05 23:36:34 Re: BUG #19971: JSON_VALUE RETURNING json drops the quotes of a jsonb string DEFAULT