Skip to content

Commit 90d91b5

Browse files
authored
JSONPath index acceleration (#20823)
1 parent eafa471 commit 90d91b5

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/current/v25.4/jsonpath.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ docs_area: reference.sql
1111

1212
JSONPath expressions and functions are used to query and filter [`JSONB`]({% link {{ page.version.version }}/jsonb.md %}) data. A [JSONPath expression](#jsonpath-expression) is a string that identifies one or more elements in a JSON document, and is used as a [JSONPath function](#jsonpath-functions) argument.
1313

14+
{% include_cached new-in.html version="v25.4" %} [GIN indexes]({% link {{ page.version.version }}/inverted-indexes.md %}) on `JSONB` columns automatically accelerate [`jsonb_path_exists`](#jsonpath-functions) queries in certain [filter patterns](#index-accelerated-patterns). This can significantly improve query performance when filtering large datasets.
15+
1416
## JSONPath expression
1517

1618
A JSONPath expression consists of an optional [mode](#structural-error-handling) (`lax` or `strict`), followed by a scalar expression (such as `1 + 2`), a predicate expression (such as `1 != 2` or `exists($)`), or a path-based expression rooted at `$`. A path-based expression is composed of one or more [accessor](#accessor-operators) operators that are optionally interleaved with one or more [filter expressions](#filter-expressions) introduced by `?`. Expressions can optionally include scalar expressions, [predicate operators](#predicate-operators) for conditional logic, and a [method](#methods) applied to the current value. The path is evaluated left to right, and each stage refines or filters the result.
@@ -135,7 +137,7 @@ Use JSONPath functions to extract or evaluate target `JSONB` data according to a
135137

136138
| Function | Description | If no match |
137139
|-------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------|
138-
| `jsonb_path_exists(jsonb, jsonpath)` | Returns true if any match is found. | `false` |
140+
| `jsonb_path_exists(jsonb, jsonpath)` | Returns true if any match is found. Accelerated by GIN indexes for certain [filter patterns](#index-accelerated-patterns). | `false` |
139141
| `jsonb_path_match(jsonb, jsonpath)` | Returns true if the path expression evaluates to true. Only useful with [predicate check expressions](#check-expressions), as it expects a single Boolean value. | `false` |
140142
| `jsonb_path_query(jsonb, jsonpath)` | Returns all matches as a set of `JSONB` values. | `NULL` |
141143
| `jsonb_path_query_array(jsonb, jsonpath)` | Returns all matches as a single `JSONB` array. | `[]` |
@@ -416,6 +418,22 @@ For example, the following JSONPath expression selects all elements in an `items
416418
$.items[*] ? (@.price > 100).name;
417419
~~~
418420

421+
<a name="index-accelerated-patterns"></a>
422+
423+
[GIN indexes]({% link {{ page.version.version }}/inverted-indexes.md %}) on `JSONB` columns automatically accelerate [`jsonb_path_exists`](#jsonpath-functions) when used in `WHERE` clause filters with the following patterns:
424+
425+
- Keychain mode (accessing nested fields): `$.[key|wildcard].[key|wildcard]...`
426+
- For example, `WHERE jsonb_path_exists(data, '$.players[*].stats.ppg')`
427+
- Filter with equality check: `$.[key|wildcard]? (@.[key|wildcard]... == value)` where `value` is a string, number, `null`, or boolean
428+
- For example, `WHERE jsonb_path_exists(data, '$.players[*] ? (@.stats.ppg == 25)')`
429+
430+
Index acceleration is **not** supported for:
431+
432+
- [`strict` mode](#structural-error-handling) queries
433+
- Root-level paths (`$` or `$[*]`)
434+
- Filters with inequality checks (for example, `@.price > 100`)
435+
- Comparison expressions without filters (for example, `$.a.b.c == 12`)
436+
419437
### Filter with comparison operators
420438

421439
The following query returns all players who averaged more than 25 points per game:

0 commit comments

Comments
 (0)