Skip to content

pg_automerge

Automerge CRDT documents with JSONB access, merging and history

pg_automerge · Automerge CRDT documents with JSONB access, merging and history

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
102197pg_automergepg_automerge0.1.0TYPEMITRust

PostgreSQL · 18

Usage

Sources:

pg_automerge stores Automerge documents as a native PostgreSQL 18 type. Applications create changes and own actor IDs; the extension stores, merges and queries them. It does not edit documents or create changes itself.

Core Workflow

CREATE EXTENSION pg_automerge;
CREATE TABLE docs (id uuid PRIMARY KEY, doc automerge NOT NULL);
PREPARE store_document(uuid, bytea) AS
INSERT INTO docs (id, doc) VALUES ($1, $2)
ON CONFLICT (id) DO UPDATE SET doc = merge(docs.doc, EXCLUDED.doc);
SELECT doc->>'title' FROM docs WHERE doc @> '{"status":"open"}';

Operational Boundaries

Input must be valid Automerge binary saves, not JSON text. The implicit jsonb cast exposes the current document; merge() and merge_agg() combine histories. Incremental changes require an existing document, so an initial insert must use a full save. Use a distinct actor ID for each writer and compare heads rather than serialized bytes when identifying histories. pg_automerge.max_load_memory limits estimated per-document memory, but allocations bypass PostgreSQL memory accounting and do not bound concurrent sessions. A native load or merge is not promptly interruptible. Installation requires a superuser; keep writers trusted and bound input resource costs.

Table reader