Skip to content

cbor

Concise Binary Object Representation data type for PostgreSQL.

cbor · Concise Binary Object Representation data type for PostgreSQL.

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
100250cborcbor0.1.0TYPEPostgreSQLC

PostgreSQL · 10, 11, 12, 13, 14, 15, 16, 17, 18

Usage

Sources:

cbor adds a Concise Binary Object Representation type based on RFC 7049. Use it when a database must store and exchange CBOR bytes while retaining a textual SQL representation; it is not a replacement for jsonb query and indexing features.

Encode and Decode

CREATE EXTENSION cbor;

SELECT '{"sensor":"alpha","values":[1,2,3]}'::cbor;

WITH encoded AS (
  SELECT cbor_encode('{"ok":true,"count":3}'::cbor) AS payload
)
SELECT cbor_decode(payload)
FROM encoded;

cbor_encode(cbor) returns the binary representation as bytea; cbor_decode(bytea) parses binary CBOR. The type’s text input and output cover integers, byte and text strings, arrays, maps, tags, floating-point values, and simple values.

Operators and Indexes

The extension supplies =, <>, <, <=, >, and >=, plus default B-tree and hash operator classes. It also exposes cbor_contains(cbor, cbor) and cbor_contained(cbor, cbor) functions, but version 0.1.0 does not define containment operators.

CREATE TABLE messages (id bigint GENERATED ALWAYS AS IDENTITY, body cbor);
CREATE INDEX messages_body_btree ON messages (body);

This old release targets the original RFC 7049 rather than the later RFC 8949 specification. Before exchanging data with other implementations, test tags, floating-point values, duplicate map keys, ordering, canonical encoding, and equality semantics. Preserve the original bytea if byte-for-byte identity matters, because a decode/encode round trip need not preserve an external encoder’s exact representation.

Table reader