Skip to content
pgbson

pgbson

pgbson : BSON data type and accessor functions for PostgreSQL

Overview

ID Extension Package Version Category License Language
3910
pgbson
pgbson
2.0.4
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

PGXN distribution name is bson; CREATE EXTENSION name is pgbson; package release 2.0.4 still installs extension SQL version 2.0; RPM package root is postgresbson and requires libbson.

Packages

Type Repo Version PG Major Compatibility Package Pattern Dependencies
EXT
PIGSTY
2.0.4
18
17
16
15
14
pgbson -
RPM
PIGSTY
2.0.4
18
17
16
15
14
postgresbson_$v libbson
DEB
PIGSTY
2.0.4
18
17
16
15
14
postgresql-$v-pgbson -
Linux / PG PG18 PG17 PG16 PG15 PG14
el8.x86_64
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
el8.aarch64
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
el9.x86_64
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
el9.aarch64
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
el10.x86_64
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
el10.aarch64
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
d12.x86_64
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
d12.aarch64
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
d13.x86_64
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
d13.aarch64
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
u22.x86_64
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
u22.aarch64
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
u24.x86_64
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
u24.aarch64
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
u26.x86_64
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
u26.aarch64
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
PIGSTY 2.0.4
Package Version OS ORG SIZE File URL
postgresbson_18 2.0.4 el8.x86_64 pigsty 30.4 KiB postgresbson_18-2.0.4-1PIGSTY.el8.x86_64.rpm
postgresbson_18 2.0.4 el8.aarch64 pigsty 30.0 KiB postgresbson_18-2.0.4-1PIGSTY.el8.aarch64.rpm
postgresbson_18 2.0.4 el9.x86_64 pigsty 29.9 KiB postgresbson_18-2.0.4-1PIGSTY.el9.x86_64.rpm
postgresbson_18 2.0.4 el9.aarch64 pigsty 29.6 KiB postgresbson_18-2.0.4-1PIGSTY.el9.aarch64.rpm
postgresbson_18 2.0.4 el10.x86_64 pigsty 30.0 KiB postgresbson_18-2.0.4-1PIGSTY.el10.x86_64.rpm
postgresbson_18 2.0.4 el10.aarch64 pigsty 29.8 KiB postgresbson_18-2.0.4-1PIGSTY.el10.aarch64.rpm
postgresql-18-pgbson 2.0.4 d12.x86_64 pigsty 37.7 KiB postgresql-18-pgbson_2.0.4-1PIGSTY~bookworm_amd64.deb
postgresql-18-pgbson 2.0.4 d12.aarch64 pigsty 37.4 KiB postgresql-18-pgbson_2.0.4-1PIGSTY~bookworm_arm64.deb
postgresql-18-pgbson 2.0.4 d13.x86_64 pigsty 37.8 KiB postgresql-18-pgbson_2.0.4-1PIGSTY~trixie_amd64.deb
postgresql-18-pgbson 2.0.4 d13.aarch64 pigsty 37.5 KiB postgresql-18-pgbson_2.0.4-1PIGSTY~trixie_arm64.deb
postgresql-18-pgbson 2.0.4 u22.x86_64 pigsty 39.4 KiB postgresql-18-pgbson_2.0.4-1PIGSTY~jammy_amd64.deb
postgresql-18-pgbson 2.0.4 u22.aarch64 pigsty 39.0 KiB postgresql-18-pgbson_2.0.4-1PIGSTY~jammy_arm64.deb
postgresql-18-pgbson 2.0.4 u24.x86_64 pigsty 38.7 KiB postgresql-18-pgbson_2.0.4-1PIGSTY~noble_amd64.deb
postgresql-18-pgbson 2.0.4 u24.aarch64 pigsty 38.7 KiB postgresql-18-pgbson_2.0.4-1PIGSTY~noble_arm64.deb
postgresql-18-pgbson 2.0.4 u26.x86_64 pigsty 38.6 KiB postgresql-18-pgbson_2.0.4-1PIGSTY~resolute_amd64.deb
postgresql-18-pgbson 2.0.4 u26.aarch64 pigsty 38.5 KiB postgresql-18-pgbson_2.0.4-1PIGSTY~resolute_arm64.deb

Source

pig build pkg pgbson;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

pig repo add pgsql -u   # add both repo and update cache

Install 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 14

Create this extension with:

CREATE EXTENSION pgbson;

Usage

Sources:

pgbson adds a BSON data type, typed path accessors, JSON-style operators, casts, and expression-index support. Use it when binary BSON must be stored without first converting every value to JSONB, especially when BSON type fidelity or byte-level round trips matter.

The distribution release is 2.0.4 while the extension control and SQL API version remain 2.0.

Create the Extension

CREATE EXTENSION pgbson;

The native module uses libbson. Install a package built against compatible PostgreSQL and libbson versions.

Store and Validate BSON

The bytea-to-bson cast validates input when a value is written. Version 2.0.4 documents that reads can then assume the stored BSON is valid. Do not bypass the type’s input or cast path with unsafe low-level writes.

Extract Values

Typed dot-path accessors avoid materializing every intermediate object:

SELECT bson_get_datetime(payload, 'msg.header.event.ts'),
       bson_get_string(payload, 'data.customer.name')
FROM events;

Use bson_get_bson for a subdocument:

SELECT bson_get_bson(payload, 'msg.header.event')
FROM events;

JSON-style navigation is also available:

SELECT payload->'msg'->'header'->'event'->>'ts'
FROM events;

Function and Operator Index

  • bson_get_string, bson_get_int32, bson_get_int64, bson_get_double, bson_get_decimal: typed scalar accessors.
  • bson_get_datetime, bson_get_binary, bson_get_boolean: accessors for additional BSON types.
  • bson_get_bson: return an embedded BSON document.
  • bson_get_jsonb_array: convert an array endpoint to a PostgreSQL jsonb array.
  • -> and -»: navigate values with JSON-like syntax.
  • bson casts to json and jsonb: expose Extended JSON for PostgreSQL JSON processing.
  • bson and bytea casts: preserve the BSON binary representation.

Index and Interoperate

Create expression indexes on frequently queried paths:

CREATE INDEX events_customer_id_idx
ON events (bson_get_string(payload, 'data.customer.id'));

Cast a subdocument to jsonb when PostgreSQL’s JSON operators are more convenient:

SELECT bson_get_bson(payload, 'msg.header')::jsonb ? 'event'
FROM events;

Caveats

  • A typed getter returns useful data only when the endpoint has the expected BSON type. Make type expectations explicit in ingestion code.
  • bson_get_bson returns NULL for scalar endpoints because a scalar is not a BSON document.
  • Dot-path accessors are generally preferable to long operator chains for repeated extraction because they avoid intermediate BSON values.
  • BSON and JSONB have different type and ordering semantics. A cast can be useful but is not a lossless replacement for every BSON workflow.
Last updated on