pgbson
pgbson : BSON data type and accessor functions for PostgreSQL
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 3910 | pgbson
|
pgbson
|
2.0.2 |
TYPE
|
MIT
|
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_jsonschema
jsquery
jsonb_plperl
jsonb_plpython3u
mongo_fdw
documentdb
documentdb_core
documentdb_distributed
|
Release tag 2.0.2 still ships extension SQL version 2.0; PGXN dist name is bson, CREATE EXTENSION name is pgbson, RPM package root is postgresbson, and the runtime dependency is libbson.
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PIGSTY
|
2.0.2 |
18
17
16
15
14
|
pgbson |
- |
| RPM | PIGSTY
|
2.0.2 |
18
17
16
15
14
|
postgresbson_$v |
libbson |
| DEB | PIGSTY
|
2.0.2 |
18
17
16
15
14
|
postgresql-$v-pgbson |
- |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
el8.x86_64
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
el8.aarch64
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
el9.x86_64
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
el9.aarch64
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
el10.x86_64
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
el10.aarch64
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
d12.x86_64
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
d12.aarch64
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
d13.x86_64
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
d13.aarch64
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
u22.x86_64
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
u22.aarch64
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
u24.x86_64
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
u24.aarch64
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
u26.x86_64
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
u26.aarch64
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
PIGSTY 2.0.2
|
Source
pig build pkg pgbson; # build rpm/debInstall
Make sure PGDG and PIGSTY repo available:
pig repo add pgsql -u # add both repo and update cacheInstall this extension with pig:
pig install pgbson; # install via package name, for the active PG version
pig install pgbson -v 18; # install for PG 18
pig install pgbson -v 17; # install for PG 17
pig install pgbson -v 16; # install for PG 16
pig install pgbson -v 15; # install for PG 15
pig install pgbson -v 14; # install for PG 14Create this extension with:
CREATE EXTENSION pgbson;Usage
Sources: README, META.json 2.0.2, pgbson.control
pgbson adds a BSON data type plus BSON-aware accessors and operators. Upstream documents the package release as 2.0.2, while the extension control file still exposes SQL default version 2.0; this matches the packaging note that the dist version is ahead of the extension SQL version.
CREATE EXTENSION pgbson;Core Access Patterns
Typed dotpath accessors walk the BSON structure directly and are the upstream-recommended fast path:
SELECT bson_get_datetime(bson_column, 'msg.header.event.ts') FROM my_table;
SELECT bson_get_bson(bson_column, 'msg.header.event') FROM my_table;
SELECT bson_get_string(bson_column, 'data.payload.product.definition.id') FROM my_table;JSON-style operators are also supported:
SELECT (bson_column->'msg'->'header'->'event'->>'ts')::timestamp
FROM my_table;Main Functions and Operators
- Typed getters such as
bson_get_string,bson_get_int32,bson_get_int64,bson_get_double,bson_get_decimal,bson_get_datetime,bson_get_binary, andbson_get_boolean. bson_get_bsonto return a BSON subdocument.bson_get_jsonb_arraywhen a path resolves to an array and you want nativejsonbarray operators afterward.- Arrow operators
->and->>similar to PostgreSQL JSON types. - Casts to
json/jsonbusing Extended JSON so type fidelity is preserved.
Interop and Indexing
Cast BSON to jsonb when you want PostgreSQL JSON operators:
SELECT (bson_get_bson(bson_column, 'msg.header.event')::jsonb) ?& ARRAY['id', 'type']
FROM my_table;Build expression indexes on extracted paths:
CREATE INDEX ON data_collection (bson_get_string(data, 'd.recordId'));The README also notes BSON values can round-trip byte-for-byte through bytea casts.
Caveats
- Dotpath accessors are usually faster and more memory-efficient than long
->chains because they avoid materializing intermediate substructures. bson_get_bson()returnsNULLfor scalar endpoints because simple scalars are not BSON documents.- Upstream explicitly calls out array handling and wrong-type accessor behavior as rough edges that still need better ergonomics.