# DocumentDB and the gateway

> How DocumentDB stores BSON in PostgreSQL and how a gateway translates the MongoDB wire protocol.

---

LLMS index: [llms.txt](/llms.txt)

---

Unlike SQL Server or MySQL, MongoDB support on PostgreSQL is a **service API protocol** (see [Architecture](/docs/concepts/architecture/#protocol-classes)): it needs no kernel patch and no PostgreSQL fork. A stateless gateway listens on TCP port `27017`, speaks the MongoDB wire protocol to drivers and `mongosh`, and translates each operation into SQL function calls against the **DocumentDB** extensions running inside stock PostgreSQL.

This architecture describes the Pigsty stack used in the earlier PG18 study. The Debian 13 amd64 r4 candidate exercised DocumentDB’s own gateway on a single node. [r4 verification scope](/docs/concepts/architecture/#documentdb)

## Two-layer architecture {#layers}

The stack separates protocol translation from storage and query execution:

| Layer | Component | Responsibility |
| --- | --- | --- |
| Wire-protocol gateway (port `27017`) | **FerretDB** (`FerretDB/FerretDB`, Apache-2.0, Go) or **DocumentDB gateway** (`documentdb/documentdb`, MIT, Rust) | Accepts MongoDB wire-protocol connections (`OP_MSG`), multiplexes client sessions over a PostgreSQL connection pool, and translates commands into `documentdb_api` SQL calls |
| Storage and execution engine (port `5432`) | PostgreSQL 18 + **DocumentDB** extensions (`documentdb/documentdb`, MIT, C) | Stores BSON documents in PostgreSQL tables, indexes BSON paths with RUM/GIN/B-tree/HNSW/GiST, and executes CRUD, aggregation pipelines, geospatial, full-text and vector operators inside PostgreSQL |
{.wide}

Because the gateway holds no persistent state, PostgreSQL owns durability, ACID transactions, write-ahead logging, point-in-time recovery (pgBackRest), high availability (Patroni) and monitoring. In a multi-node cluster, each node can run a local stateless gateway instance behind HAProxy.

## The DocumentDB extensions {#extensions}

DocumentDB (`1.0` / `v1.0-0`, MIT License, packaged for PostgreSQL 15–18 by PGDG and Pigsty) consists of four PostgreSQL extensions:

| Extension | Shared library (`shared_preload_libraries`) | Schema | Role |
| --- | --- | --- | --- |
| `documentdb_core` | `pg_documentdb_core` | `documentdb_core` | Low-level BSON data type (`documentdb_core.bson`, `bsonquery`, `bsonsequence`), path extraction (`->`, `->>`, `bson_get_value`), serialization (`bson_from_bytea`, `bson_json_to_bson`) and B-tree / hash operator classes |
| `documentdb` | `pg_documentdb` | `documentdb_api` | Public PostgreSQL API for collections, CRUD, aggregation, geospatial, full-text and vector queries. Depends on `documentdb_core`, `pg_cron`, `postgis`, `tsm_system_rows` and `vector` (`pgvector`) |
| `documentdb_extended_rum` | `pg_documentdb_extended_rum` | `documentdb_extended_rum_catalog` | Extended RUM index access method for BSON documents and paths; selected by default via `documentdb.rum_library_load_option = 'require_documentdb_extended_rum'` |
| `documentdb_distributed` | `pg_documentdb_distributed` | — | Internal multi-node execution layer integrating `documentdb` with Citus (`citus` required); retired from standard single-node packages |
{.wide}

Enabling the single-node DocumentDB stack in `postgresql.conf` requires preloading the three libraries alongside `pg_cron` and restarting PostgreSQL:

```ini
shared_preload_libraries = 'pg_cron, pg_documentdb_core, pg_documentdb, pg_documentdb_extended_rum'
cron.database_name = 'postgres'
```

Then install the extensions as a superuser in the target database:

```sql
CREATE EXTENSION documentdb CASCADE;
CREATE EXTENSION documentdb_extended_rum;
```

`CREATE EXTENSION documentdb CASCADE` automatically installs `documentdb_core`, `pg_cron`, `postgis`, `tsm_system_rows` and `vector` when their packages are present. Both extensions are superuser-only and non-relocatable.

## SQL API under the hood {#sql-api}

Installing `documentdb` without a gateway exposes its SQL surface inside PostgreSQL, but does not open a listener on port `27017`. When a gateway is running, it translates MongoDB wire commands into calls to `documentdb_api` and `documentdb_api_catalog` functions. The same functions can be called directly from `psql`:

```sql
SELECT documentdb_api.create_collection('appdb', 'people');

SELECT documentdb_api.insert_one(
  'appdb',
  'people',
  '{"_id": 1, "name": "Ada", "team": "storage"}',
  NULL
);

SELECT document
FROM documentdb_api_catalog.bson_aggregation_find(
  'appdb',
  '{"find": "people", "filter": {"team": "storage"}}'
);
```

`documentdb_core.bson` follows MongoDB BSON comparison, numeric and indexing rules rather than PostgreSQL `jsonb` semantics. Indexes on document collections should be created and managed through the DocumentDB APIs or a MongoDB client connected to the gateway, rather than by building indexes directly on internal operator classes.

## Questions from the PG18 study {#open-questions}

The November 2026 PG18 study recorded the following four questions. Its gateway and binary-compatibility assumptions refer to that historical environment.

1. **Default gateway selection**: Pigsty's `mongo` template currently deploys **FerretDB** (`ghcr.io/ferretdb/ferretdb:2.7.0`) as a Docker application connected to `documentdb`, whereas upstream DocumentDB also maintains its own Rust gateway. Mimus must choose which gateway to ship and supervise alongside `pgredis`, `pgetcd` and `pgsilo`.
2. **Verification on the Mimus 1 kernel**: `pg_documentdb_core`, `pg_documentdb` and `pg_documentdb_extended_rum` were not part of the 11-extension smoke matrix run on 2026-11-07 (see [Extension matrix](/docs/guides/extensions/)). Because Mimus 1 keeps Level A binary compatibility with stock PostgreSQL 18 (see [ABI compatibility](/docs/guides/extensions/#abi)), stock PGDG and Pigsty `documentdb` packages are expected to load without recompilation, but this must be verified in CI.
3. **Authentication and role mapping**: FerretDB authenticates users against PostgreSQL credentials (`FERRETDB_AUTH: true`), but does not implement MongoDB role-based authorization; in DocumentDB `1.0-0`, custom role CRUD is also disabled by default. How MongoDB users map onto PostgreSQL roles and `pg_hba.conf` rules in the [unified authentication](/docs/guides/instance-planning/#auth) model is undecided.
4. **Telemetry defaults**: FerretDB's Pigsty template sets `FERRETDB_TELEMETRY: disabled`, and upstream DocumentDB's container image enables telemetry unless `DOCUMENTDB_USAGE_TELEMETRY=false` (or `DO_NOT_TRACK=1`) is set (native RPM/DEB extension packages are unaffected). Mimus packaging must keep telemetry disabled by default.
