| From: | PG Bug reporting form <noreply(at)postgresql(dot)org> |
|---|---|
| To: | pgsql-bugs(at)lists(dot)postgresql(dot)org |
| Cc: | e1638616(at)u(dot)nus(dot)edu |
| Subject: | BUG #19956: json #>, #>> and json_extract_path resolve a negative array index against an earlier sibling array |
| Date: | 2026-11-29 14:50:52 |
| Message-ID: | 19956-ac9ffa5c4604bf26@postgresql.org |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
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.
## Steps to reproduce
```sql
CREATE TABLE t (id int, j json);
INSERT INTO t VALUES (1, '{"x": [1, 2, 3], "a": [7, 8]}'),
(2, '{"a": [7, 8], "x": [1, 2, 3]}'),
(3, '[[1, 2], [3]]');
SELECT id,
j #> '{a,-1}' AS path_op,
j #>> '{a,-1}' AS path_text_op,
json_extract_path(j, 'a', '-1') AS extract_path,
j -> 'a' -> -1 AS chained,
j::jsonb #> '{a,-1}' AS via_jsonb
FROM t WHERE id IN (1, 2) ORDER BY id;
SELECT j #> '{1,-2}' AS path_op,
j -> 1 -> -2 AS chained,
j::jsonb #> '{1,-2}' AS via_jsonb
FROM t WHERE id = 3;
```
Save the SQL as `tmp.sql` in an empty database and run:
```sh
psql -X -v ON_ERROR_STOP=1 -P null=NULL -f tmp.sql
```
## Expected result
The docs describe `#>` path elements as "field keys or array indexes", and
say for `->` that "negative integers count from the end" of the array. In
rows 1 and 2, `a` is `[7, 8]`, so `-1` selects `8` whatever the key order.
In row 3, element `1` is `[3]`, which has no element `-2`, so the result is
NULL. The chained `->` lookups and the jsonb casts give these values.
| id | path_op | path_text_op | extract_path | chained | via_jsonb |
| --- | --- | --- | --- | --- | --- |
| 1 | 8 | 8 | 8 | 8 | 8 |
| 2 | 8 | 8 | 8 | 8 | 8 |
| path_op | chained | via_jsonb |
| --- | --- | --- |
| NULL | NULL | NULL |
## Actual result
| id | path_op | path_text_op | extract_path | chained | via_jsonb |
| --- | --- | --- | --- | --- | --- |
| 1 | NULL | NULL | NULL | 8 | 8 |
| 2 | 8 | 8 | 8 | 8 | 8 |
| path_op | chained | via_jsonb |
| --- | --- | --- |
| 3 | NULL | NULL |
## Environment
PostgreSQL 19.0 (Homebrew), macOS arm64.
| From | Date | Subject | |
|---|---|---|---|
| Next Message | PG Bug reporting form | 2026-11-29 14:54:32 | BUG #19957: jsonpath `last` returns the wrong index after a filter containing `exists($x[0])` |
| Previous Message | ZizhuanLiu X-MAN | 2026-11-29 12:45:31 | Re: BUG #19789: "expression not found in statistics object" for a boolean domain column in an MCV statistics object |