Re: BUG #19956: json #>, #>> and json_extract_path resolve a negative array index against an earlier sibling array

From: Andrey Rachitskiy <pl0h0yp1(at)gmail(dot)com>
To: e1638616(at)u(dot)nus(dot)edu, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19956: json #>, #>> and json_extract_path resolve a negative array index against an earlier sibling array
Date: 2026-11-30 10:21:42
Message-ID: CAB8bMivNUG=qOa5tsZfqYFYw757YTMGRJoi06EO=vr7cb0VVDw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

вс, 29 нояб. 2026 г. в 21:41, PG Bug reporting form <noreply(at)postgresql(dot)org
>:

> The following bug has been logged on the website:
>
> Bug reference: 19956
> Logged by: Yibo Dong
> Email address: e1638616(at)u(dot)nus(dot)edu
> PostgreSQL version: 19.0
> Operating system: macOS 15 arm64 (Homebrew)
> Description:
>
> ## Description
>
> For the `json` type, the multi-level path functions (`#>`, `#>>`,
> `json_extract_path`, `json_extract_path_text`) accept negative array
> indexes
> that count from the end of the array. The index is resolved against the
> wrong array whenever another array at the same nesting depth appears
> earlier
> in the document, even if that array is not on the path.
>
> In `'{"x": [1, 2, 3], "a": [7, 8]}'::json #> '{a,-1}'`, the result is NULL
> instead of `8`. If the keys are swapped, the result is `8`. In `'[[1, 2],
> [3]]'::json #> '{1,-2}'`, the result is `3`, although `-2` is out of range
> for `[3]`. The chained `->` operators and the same documents cast to
> `jsonb`
> give the expected results.
>
> The negative index appears to be converted to a positive one in place when
> the walker enters the first array at that depth (`get_array_start` in
> `jsonfuncs.c` adds that array's length to `path_indexes[lex_level]`). Later
> arrays at the same depth then reuse the converted index.
>
>
Hi, Yibo!

Thanks for the report.
I have reviewed the code and agree with the analysis.
Attached a patch to fix it.

--
Regards,
Rachitskiy Andrey

Attachment Content-Type Size
0001-Fix-json-negative-subscripts-on-the-selected-array.patch text/x-patch 5.6 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Raja Sai pranav 2026-11-30 11:23:40 Re: BUG #19928: pg_upgrade fails on v18 schema with NOT NULL ... NO INHERIT and inheritance
Previous Message Laurenz Albe 2026-11-30 10:06:32 Re: BUG #19871: JSON_TABLE evaluates each PASSING argument once per column - a named variable $v gets different valu