pgbson
pgbson : BSON data type and accessor functions for PostgreSQL
Overview
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
| --s-d-r | No | Yes | No | Yes | yes | no |
| Relationships | |
|---|---|
| See Also | pgjq jsquery pg_jsonschema jsonschema pg_projection hstore jsonb_plperl documentdb jsonb_plpython3u jsonb_plperlu |
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PIGSTY | 2.1.0 |
18 17 16 15 14 | pgbson |
- |
| RPM | PIGSTY | 2.1.0 |
18 17 16 15 14 | postgresbson_$v |
libbson |
| DEB | PIGSTY | 2.1.0 |
18 17 16 15 14 | postgresql-$v-pgbson |
- |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
| el8.x86_64 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 |
| el8.aarch64 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 |
| el9.x86_64 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 |
| el9.aarch64 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 |
| el10.x86_64 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 |
| el10.aarch64 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 |
| d12.x86_64 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 |
| d12.aarch64 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 |
| d13.x86_64 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 |
| d13.aarch64 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 |
| u22.x86_64 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 |
| u22.aarch64 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 |
| u24.x86_64 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 |
| u24.aarch64 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 |
| u26.x86_64 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 |
| u26.aarch64 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 | PIGSTY 2.1.0 |
Source
github.com/buzzm/postgresbson
postgresbson-2.1.0.tar.gz
Install
Make sure PGDG and PIGSTY repo available:
Install this extension with pig:
Create this extension with:
Usage
Sources:
pgbson adds a BSON data type, typed dot-path accessors, JSON-style navigation, casts, comparison operators, and btree/hash indexing. The PGXN distribution release is 2.1.0, while the SQL extension version is 2.1. Use BSON when binary round-trip fidelity or BSON-specific scalar types matter; use jsonb when PostgreSQL-native JSON indexing is the primary requirement.
Install and Store BSON
The native module depends on libbson. The implicit bytea-to-bson cast validates BSON input, while the reverse cast preserves the binary representation.
Extract Values
Typed accessors avoid materializing each intermediate document:
Other typed getters cover 64-bit integers, doubles, decimals, datetimes, binary values, booleans, embedded BSON documents, and JSONB arrays. A missing path or a type mismatch returns NULL, so validate the expected BSON schema at ingestion when those cases must be distinguished.
Version 2.1 adds a type-agnostic terminal extractor:
bson_get_value always wraps the selected scalar, array, or document under the key _. Remove exactly that one wrapper in the caller. It intentionally has no chainable -> equivalent.
Navigate, Compare, and Index
Version 2.1 provides logical comparison operators =, <>, <, <=, >, and >=; == and <<>> perform binary equality and inequality. The default btree operator class uses logical BSON comparison, while the hash operator class uses binary equality. Choose intentionally when field order or byte identity matters.
Upgrade and Caveats
- Installing a 2.1 shared library does not update an existing 2.0 extension’s SQL objects; run the extension update after installing the files.
- The 2.1 shared library fixes a backend crash when
bson_get_bson()or->resolves to a scalar endpoint. Earlier binaries should be replaced even when an application does not yet use the new 2.1 SQL function. - BSON-to-JSON/JSONB casts use Extended JSON. BSON and JSONB have different type, equality, and ordering semantics, so conversion is not lossless for every workflow.
- In 2.1,
->>on a BSON datetime includes the trailingZ;bson_get_datetime()is unchanged. Check clients that compare the old text form. - BSON top-level values are documents, not bare arrays or scalars.
bson_get_valueuses its_wrapper to return any nested shape within that restriction.