跳转到主要内容

DocumentDB 与网关

DocumentDB 如何把 BSON 存进 PostgreSQL,网关如何翻译 MongoDB 线协议。

与 SQL Server 或 MySQL 不同,PostgreSQL 上的 MongoDB 支持属于服务型 API 协议(见架构):不需要内核补丁,也不需要分叉 PostgreSQL。无状态网关在 TCP 27017 端口监听,向客户端驱动和 mongosh 提供 MongoDB 线协议,并将请求翻译为对原生 PostgreSQL 内 DocumentDB 扩展的 SQL 函数调用。

以下架构介绍沿用早期 PG18 研究中的 Pigsty 组合。19.0.1-r4 在 Debian 13 amd64 上选用 DocumentDB 自带的单节点网关。r4 验证范围

两层架构

整套方案将线协议转换与底层存储、查询执行解耦:

层次组件职责
线协议网关(27017 端口)FerretDB(FerretDB/FerretDB,Apache-2.0,Go)或 DocumentDB 自带网关(documentdb/documentdb,MIT,Rust)接收 MongoDB 线协议连接(OP_MSG),通过 PostgreSQL 连接池多路复用客户端会话,将命令翻译为 documentdb_api SQL 调用
存储与执行引擎(5432 端口)PostgreSQL 18 + DocumentDB 扩展集(documentdb/documentdb,MIT,C)在 PostgreSQL 表中存储 BSON 文档,利用 RUM / GIN / B-tree / HNSW / GiST 为文档路径建索引,并在数据库内执行 CRUD、聚合管道、地理空间、全文检索与向量运算

由于网关本身不保存持久化状态,持久化、ACID 事务、WAL、时间点恢复(pgBackRest)、高可用(Patroni)和监控全部由 PostgreSQL 承担。在多节点集群中,每个节点可各运行一个无状态网关实例,由 HAProxy 统一对外暴露。

DocumentDB 扩展组件

DocumentDB(1.0 / v1.0-0,MIT 许可证,PGDG 与 Pigsty 已为 PostgreSQL 15–18 打包)包含四个 PostgreSQL 扩展:

扩展名预加载共享库(shared_preload_libraries)所属 Schema作用
documentdb_corepg_documentdb_coredocumentdb_core底层 BSON 类型(documentdb_core.bson、bsonquery、bsonsequence)、路径提取(->、->>、bson_get_value)、序列化转换(bson_from_bytea、bson_json_to_bson)及 B-tree / hash 运算符族
documentdbpg_documentdbdocumentdb_api面向集合管理、CRUD、聚合、地理空间、全文与向量查询的公共 PostgreSQL API。依赖 documentdb_core、pg_cron、postgis、tsm_system_rows 与 vector(pgvector)
documentdb_extended_rumpg_documentdb_extended_rumdocumentdb_extended_rum_catalog面向 BSON 文档与路径的扩展 RUM 索引访问方法,默认通过 documentdb.rum_library_load_option = 'require_documentdb_extended_rum' 启用
documentdb_distributedpg_documentdb_distributed—将 documentdb 与 Citus 集成的内部多节点执行层(依赖 citus);标准单节点软件包不打包该组件

在单节点 PostgreSQL 的 postgresql.conf 中启用 DocumentDB 需要预加载三个共享库及 pg_cron,并重启实例:

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

随后以超级用户在目标数据库中安装扩展:

CREATE EXTENSION documentdb CASCADE;
CREATE EXTENSION documentdb_extended_rum;

当依赖包已安装时,CREATE EXTENSION documentdb CASCADE 会自动一并创建 documentdb_core、pg_cron、postgis、tsm_system_rows 与 vector。上述扩展均要求超级用户权限且不可重定位(non-relocatable)。

底层 SQL 接口

仅安装 documentdb 扩展会在 PostgreSQL 内暴露 SQL 接口,但不会监听 27017 端口。运行网关时,网关代表 MongoDB 客户端调用 documentdb_api 与 documentdb_api_catalog 下的函数;在 psql 中也可以直接调用同一套函数:

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 的比较、数值与索引语义遵循 MongoDB BSON 规范,不等同于 PostgreSQL 原生 jsonb。文档集合上的索引应通过 DocumentDB API 或连接网关的 MongoDB 客户端创建和管理,不要直接基于内部运算符类手工建索引。

早期研究提出的问题

以下是 2026 年 11 月 PG18 研究记录中的四个待定事项。当时的网关和二进制兼容性设想仅适用于该记录。

  1. 默认网关选型:Pigsty 的 mongo 模板目前以 Docker 应用形式部署 FerretDB(ghcr.io/ferretdb/ferretdb:2.7.0)对接 documentdb,而 DocumentDB 上游也维护了自己的 Rust 网关。Mimus 需要决定默认随发行物提供哪一个网关,以及如何与 pgredis、pgetcd、pgsilo 统一编排和监控。
  2. Mimus 1 内核兼容性验证:2026-11-07 的 11 扩展冒烟矩阵未包含 pg_documentdb_core、pg_documentdb 与 pg_documentdb_extended_rum(见扩展兼容矩阵)。由于 Mimus 1 与官方 PostgreSQL 18 保持等级 A 二进制兼容(见 ABI 兼容性),PGDG 与 Pigsty 现成的 documentdb 软件包预期可直接加载,但需纳入 CI 实测确认。
  3. 认证与角色映射:FerretDB 通过 PostgreSQL 校验口令(FERRETDB_AUTH: true),但尚未实现 MongoDB 基于角色的授权体系;DocumentDB 1.0-0 的自定义角色 CRUD 默认也处于关闭状态。在统一认证设计中如何把 MongoDB 用户映射到 PostgreSQL 角色与 pg_hba.conf 仍待确定。
  4. 遥测默认开关:Pigsty 的 FerretDB 模板默认设置 FERRETDB_TELEMETRY: disabled;DocumentDB 官方容器镜像默认开启遥测(需设 DOCUMENTDB_USAGE_TELEMETRY=false 或 DO_NOT_TRACK=1 关闭,原生 RPM/DEB 扩展包不受影响)。Mimus 打包时需确保默认关闭遥测。
扫码打开此页
https://mimus.pgsty.com/zh/docs/mongo/architecture/