pgbson
Overview
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 3910 | pgbson | No | Yes | No | Yes | No | Yes | - |
| Related | pgjq jsquery pg_jsonschema jsonschema pg_projection hstore jsonb_plperl documentdb jsonb_plpython3u jsonb_plperlu |
|---|
PGXN distribution name is bson, CREATE EXTENSION name is pgbson, source archive and RPM root are postgresbson, and the control default_version is 2.1 while the package release is 2.1.0.
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | PIGSTY | 2.1.0 | 1817161514 | pgbson | - |
| RPM | PIGSTY | 2.1.0 | 1817161514 | postgresbson_$v | libbson |
| DEB | PIGSTY | 2.1.0 | 1817161514 | postgresql-$v-pgbson | - |
Build
You can build the RPM / DEB packages for pgbson using pig build:
Install
You can install pgbson directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:
Install the extension using pig or apt/yum/dnf:
Create Extension:
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.
Was this page helpful?
Thanks—your feedback helps us improve this page.
What got in the way? (optional)