BUG #19957: jsonpath `last` returns the wrong index after a filter containing `exists($x[0])`

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 #19957: jsonpath `last` returns the wrong index after a filter containing `exists($x[0])`
Date: 2026-11-29 14:54:32
Message-ID: 19957-c7ec007cd15aa526@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: 19957
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

In `$[0 ? (exists($x[0])), last]` on `[10, 20, 30]`, the filter is true and
returns `0`, so the path should select elements 0 and 2. Instead `last`
evaluates to 1, which is the last index of `$x` (`[1, 2]`), not of the array
being subscripted. With `exists($x[*])`, or without the filter, `last` is 2.

## Steps to reproduce

```sql
SELECT jsonb_path_query_array('[10, 20, 30]', '$[0 ? (exists($x[0])),
last]', '{"x": [1, 2]}') AS exists_index,
jsonb_path_query_array('[10, 20, 30]', '$[0 ? (exists($x[*])),
last]', '{"x": [1, 2]}') AS exists_wildcard,
jsonb_path_query_array('[10, 20, 30]', '$[0, last]') AS no_filter;
```

Run with `psql -X -v ON_ERROR_STOP=1 -P null=NULL -f tmp.sql`.

## Actual result

| exists_index | exists_wildcard | no_filter |
| --- | --- | --- |
| [10, 20] | [10, 30] | [10, 30] |

## Expected result

| exists_index | exists_wildcard | no_filter |
| --- | --- | --- |
| [10, 30] | [10, 30] | [10, 30] |

## Environment

PostgreSQL 19.0 (Homebrew), macOS arm64.

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Pavan 2026-11-29 15:02:17 Re: BUG #19952: psql \d+ omits nondefault replica identity for partitioned tables
Previous Message PG Bug reporting form 2026-11-29 14:50:52 BUG #19956: json #>, #>> and json_extract_path resolve a negative array index against an earlier sibling array