jsquery
jsquery
jsquery : data type for jsonb inspection
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 2770 | jsquery | jsquery | 1.2 | FEAT | PostgreSQL | C |
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
--s-d-r | No | Yes | No | Yes | yes | no |
| Relationships | |
|---|---|
| See Also | pg_graphql pg_jsonschema plv8 jsonb_plperl jsonb_plpython3u pg_net pg_summarize age |
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PGDG | 1.2 | 18 17 16 15 14 | jsquery | - |
| RPM | PGDG | 1.2 | 18 17 16 15 14 | jsquery_$v | - |
| DEB | PGDG | 1.2 | 18 17 16 15 14 | postgresql-$v-jsquery | - |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
el8.x86_64 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 |
el8.aarch64 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 |
el9.x86_64 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 |
el9.aarch64 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 |
el10.x86_64 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 |
el10.aarch64 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 |
d12.x86_64 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 |
d12.aarch64 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 |
d13.x86_64 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 |
d13.aarch64 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 |
u22.x86_64 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 |
u22.aarch64 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 |
u24.x86_64 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 |
u24.aarch64 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 | PGDG 1.2 |
Source
Install
Make sure PGDG repo available:
pig repo add pgdg -u # add pgdg repo and update cacheInstall this extension with pig:
pig install jsquery; # install via package name, for the active PG version
pig install jsquery -v 18; # install for PG 18
pig install jsquery -v 17; # install for PG 17
pig install jsquery -v 16; # install for PG 16
pig install jsquery -v 15; # install for PG 15
pig install jsquery -v 14; # install for PG 14Create this extension with:
CREATE EXTENSION jsquery;Usage
JsQuery provides a query language for JSONB data, similar to what tsquery does for full-text search. It offers a concise way to search nested objects and arrays with index support via GIN.
Operators
| Operator | Description |
|---|---|
@@ | Match operator: test whether a jsonb value matches a jsquery expression |
Query Syntax
Expressions follow the pattern path operator value:
Binary operators:
=(equality),>,>=,<,<=(comparison)IN(list membership)&&(overlap),@>(contains),<@(contained in)
Unary operators:
= *(existence check)IS ARRAY,IS NUMERIC,IS OBJECT,IS STRING,IS BOOLEAN(type checking)
Path Expressions
| Symbol | Meaning |
|---|---|
# | Any array index |
#N | Specific array index N |
% | Any object key |
* | Any sequence of keys/indexes |
@# | Array/object length |
$ | Entire document |
“Every” semantics (all elements must match):
#:– all array elements%:– all object keys*:– all nested paths
Examples
Simple value matching:
SELECT * FROM jsonb_table WHERE data @@ 'name = "Alice"';
SELECT * FROM jsonb_table WHERE data @@ 'age > 21';
SELECT * FROM jsonb_table WHERE data @@ 'tags.#: IS STRING';Logical combinations:
SELECT * FROM jsonb_table WHERE data @@ 'a = 1 AND (b = 2 OR c = 3)';Array element matching (find array elements where both conditions hold):
SELECT * FROM jsonb_table WHERE data @@ '#(a = 1 AND b = 2)';Object key range matching:
SELECT * FROM jsonb_table WHERE data @@ '%($ >= 10 AND $ <= 20)';GIN Indexing
Two operator classes for different query patterns:
-- Best for range and exact searches when full path is known
CREATE INDEX ON jsonb_table USING gin (data jsonb_path_value_ops);
-- Best for exact value searches; supports % and * in paths
CREATE INDEX ON jsonb_table USING gin (data jsonb_value_path_ops);Optimizer hints for index usage:
SELECT * FROM jsonb_table WHERE data @@ 'x = 1 /*-- index */ AND y = 2';
SELECT * FROM jsonb_table WHERE data @@ 'x = 1 /*-- noindex */ AND y = 2';Schema Validation via CHECK Constraints
CREATE TABLE documents (
id serial PRIMARY KEY,
data jsonb CHECK (data @@ 'name IS STRING AND similar_ids.#: IS NUMERIC'::jsquery)
);Last updated on