Mooncake-Labs/pg_mooncake
حول
تحليلات آنية لجداول Postgres
البيانات والتحليلاتالنمو المقاس للنجوم
- خلال 24 ساعة
- ٠
- خلال 7 أيام
- ٠
- خلال 15 يومًا
- -٢
- خلال شهر
- +١
تغيّر صافي النجوم خلال فترات متداخلة، وليس مجموعًا يوميًا. تبقى القياسات المفقودة فارغة. النجوم تقيس الاهتمام لا جودة البرمجيات.
README.md
الأصلOverview
pg_mooncake is a Postgres extension that creates a columnstore mirror of your Postgres tables in Iceberg, enabling fast analytics queries on fresh data: changes are visible about half a second later from another session (immediately in the writing session):
- Real-time ingestion powered by moonlink for streaming and batched INSERT/UPDATE/DELETE.
- Fast analytics accelerated by DuckDB.
- Postgres-native allowing you to query a columnstore table just like a regular Postgres table.
- Iceberg-native making your data readily accessible by other query engines.
Installation
Option 1: Docker
For new users, we recommend using the Docker image to get started quickly:
docker run --name mooncake --rm -e POSTGRES_PASSWORD=password mooncakelabs/pg_mooncake
This will start Postgres with pg_mooncake preinstalled. You can then connect to it using psql with the default user postgres:
docker exec -it mooncake psql -U postgres
The image keeps mirrors in /var/lib/postgresql/18/mooncake (mooncake.data_directory), inside its volume but
outside the data directory, so pg_basebackup works in it (see Backups and standbys). If you
bind-mount a host directory on /var/lib/postgresql, or run the container with --user, create 18/mooncake in it
first, owned by the user the server runs as (the image's postgres user by default) with mode 0700; otherwise the
server doesn't start (could not create mooncake.data_directory ...: Permission denied).
Option 2: From Source
pg_mooncake supports Postgres 14 to 18 and is built with Rust and pgrx. It depends on
pg_duckdb, which is included as a submodule and built from source along with DuckDB. The submodule is
the v1.0.0-mooncake branch of Mooncake-Labs/pg_duckdb (tag v1.0.0-mooncake.1): pg_duckdb
1.0.0 on DuckDB v1.4.1 plus the upstream fix for its TimeZone hook (duckdb/pg_duckdb#981), without which a
configuration reload crashes every session that has run a DuckDB query. Use this pg_duckdb rather than a separately
installed one.
Prerequisites.
- Postgres 14 to 18, including the server development files (
pg_configand headers). - rustup. The Rust version is pinned in
rust-toolchain.toml, and rustup installs it on first use. - The build tools for DuckDB (a C++ compiler, CMake, Ninja) and libclang, which pgrx uses to generate bindings.
On Debian or Ubuntu, for example:
sudo apt install build-essential cmake ninja-build pkg-config clang libclang-dev \
libssl-dev libcurl4-openssl-dev liblz4-dev postgresql-server-dev-18
postgresql-server-dev-18 (and the other majors' packages) comes from the PostgreSQL project's apt repository
(PGDG, apt.postgresql.org); distribution repositories usually carry only one Postgres major.
Build and install. Clone the repository with its submodules:
git clone --recurse-submodules https://github.com/Mooncake-Labs/pg_mooncake.git
cd pg_mooncake
Install pgrx and point it at your Postgres:
cargo install --locked [email protected]
cargo pgrx init --pg18=$(which pg_config) # Replace with your Postgres version
Then build and install pg_duckdb and pg_mooncake for that version:
make pg_duckdb PG_VERSION=pg18 # Skip only if this bundled pg_duckdb (v1.0.0-mooncake.1) is installed
make install PG_VERSION=pg18
make install and make pg_duckdb install into the Postgres you passed to cargo pgrx init for that version. To
install into another one, pass its pg_config: make install PG_VERSION=pg18 PG_CONFIG=/path/to/pg_config (the major
version must match PG_VERSION). If that Postgres is owned by root, install pg_mooncake with
cargo pgrx install --release --sudo --pg-config /path/to/pg_config in place of make install, and install pg_duckdb
with root privileges as described in its build instructions.
Configure Postgres. Add pg_mooncake to shared_preload_libraries in your postgresql.conf file and enable logical
replication, then restart Postgres:
duckdb.allow_community_extensions = true
shared_preload_libraries = 'pg_duckdb,pg_mooncake'
wal_level = logical
pg_duckdbmust come beforepg_mooncake. pg_mooncake works only when preloaded: otherwiseCREATE EXTENSIONandLOADfail withpg_mooncake must be loaded via shared_preload_libraries.pg_upgradeloads extension libraries to check them, so the new cluster must preloadpg_duckdb,pg_mooncaketoo.CREATE EXTENSION pg_mooncakedownloads DuckDB'smooncakeextension from the DuckDB community extensions repository, which is whyduckdb.allow_community_extensionsis needed.
For a complete walkthrough, refer to our Dockerfile.
Quick Start
First, create the pg_mooncake extension:
CREATE EXTENSION pg_mooncake CASCADE;
Next, create a regular Postgres table trades:
CREATE TABLE trades(
id bigint PRIMARY KEY,
symbol text,
time timestamp,
price real
);
Then, create a columnstore mirror trades_iceberg that stays in sync with trades:
CALL mooncake.create_table('trades_iceberg', 'trades');
Now, insert some data into trades:
INSERT INTO trades VALUES
(1, 'AMD', '2024-06-05 10:00:00', 119),
(2, 'AMZN', '2024-06-05 10:05:00', 207),
(3, 'AAPL', '2024-06-05 10:10:00', 203),
(4, 'AMZN', '2024-06-05 10:15:00', 210);
Finally, in the same session, query trades_iceberg to see that it reflects the up-to-date state of trades:
SELECT avg(price) FROM trades_iceberg WHERE symbol = 'AMZN';
A query on a mirror waits until moonlink has applied the session's own last commit, so the writing session always sees its changes. Other sessions see a change once moonlink applies it, about half a second later (longer after large transactions).
Self-Hosting
Roles. Creating the extension requires a superuser. mooncake.create_table needs, in practice, a superuser too: it
creates the mirror table, creates a replication slot and adds the source to a publication (the REPLICATION attribute,
and CREATE on the database for the first mirror), sets REPLICA IDENTITY FULL on the source (ownership of it), and
reads the source (SELECT). Reading mirrors needs, in practice, a superuser as well:
- Mirrors are read by DuckDB through pg_duckdb, so a role needs pg_duckdb's
duckdb.postgres_role(set it inpostgresql.conf, restart, thenGRANT <that role> TO <user>).GRANT SELECTon the mirror alone is not enough: queries fail withDuckDB execution is not allowed because you have not been granted the duckdb.postgres_role. - Mirrors are local Parquet files, and pg_duckdb blocks DuckDB's local file access for roles that are not members of
both
pg_read_server_filesandpg_write_server_files. Without them, reads fail withFile system LocalFileSystem has been disabled by configuration.
Run queries on mirrors as a superuser or as one trusted role that has all three, not as application roles that should see only some of the data. See Security.
Where data lives. Mirrors are stored on the local filesystem, by default under $PGDATA/pg_mooncake/ (the
mooncake.data_directory setting, see Backups and standbys). This includes the Iceberg tables
(filesystem catalog; mooncake.list_tables() shows each table's location), moonlink's metadata, caches, and the Unix
socket moonlink.sock. Plan disk space for it alongside your Postgres data.
The moonlink worker. Moonlink runs inside Postgres as a background worker named moonlink and logs to the
Postgres server log. Postgres restarts it 15 seconds after a crash.
Local connections. mooncake.create_table connects back to Postgres over its Unix socket as the current user,
without a password, to create the mirror table and replicate from the source. pg_hba.conf must accept that local
connection (for example with trust or peer). moonlink's logical replication connection is matched by the same
per-database entries; replication lines in pg_hba.conf are only for physical replication and are not needed.
Replication slots and WAL. Each database with mirrors uses one logical replication slot (usually named
moonlink_slot_<database>) and the publication moonlink_pub. Both are created by the first mooncake.create_table
and dropped with the last mirror. Changes stay in pg_wal until they are persisted to Iceberg. To see how much WAL a
slot retains:
SELECT slot_name,
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS retained_wal,
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), confirmed_flush_lsn)) AS not_yet_persisted
FROM pg_replication_slots WHERE slot_name LIKE 'moonlink_slot%';
retained_wal is what the slot keeps in pg_wal; not_yet_persisted is how far moonlink's Iceberg tables are behind.
If you stop using pg_mooncake, drop all mirrors so that the slot is dropped and stops retaining WAL.
Mirroring from another server. mooncake.create_table takes an optional connection string for the source:
CALL mooncake.create_table('trades_iceberg', 'trades', 'postgresql://user@host/db'). The source server needs
wal_level = logical, and the user needs permission to replicate and to alter the source table. moonlink stores the
connection string in its metadata store (moonlink_metadata_store.sqlite in mooncake.data_directory), and some of
its errors repeat it, so use one without a password: let .pgpass, peer or certificate authentication supply the
credentials. pg_mooncake's own connection errors show only the host, port, database and user.
Column types. Mirrors store booleans, character and text types, integers, floating-point numbers, numeric (up to
38 digits), bytea, dates, times and timestamps (not timetz), uuid, json/jsonb, oid and enums, and
one-dimensional arrays, composites and domains of these, with a few exceptions; mooncake.create_table refuses a source
with any other column type. Some values read back differently from a mirror (unconstrained numeric with 10
fractional digits, char(n) without trailing blanks, composites as DuckDB STRUCT text), enums sort as text, and queries
on mirrors follow DuckDB's semantics in places, such as integer division. The CHANGELOG's
Known limitations has the full list.
Renames and schema changes. moonlink keeps each mirror under the database, schema and table name it was created
with. pg_mooncake rejects renaming a mirror, moving it to another schema, and renaming a schema that contains mirrors.
ALTER DATABASE ... RENAME TO fails while the database has mirrors, because moonlink's replication connections use
it: drop that database's mirrors first and re-create them afterwards. Column changes on a source table (for example
ALTER TABLE ... ADD COLUMN or DROP COLUMN) put its mirrors into the errored state; drop and re-create them (see
Durability and Recovery).
Table settings. The optional fourth argument of mooncake.create_table is a JSON object of moonlink settings for
the mirror (default '{}'). The supported keys are under "mooncake": skip_index_merge and skip_data_compaction
(booleans, default false) turn off the background index merges and data compaction, and mem_slice_bytes (default
16 MiB) is how many bytes of changes are buffered in memory before they are written to a data file. For example
CALL mooncake.create_table('trades_iceberg', 'trades', NULL, '{"mooncake": {"skip_data_compaction": true}}').
Unknown keys are ignored. Other moonlink settings, such as "iceberg" storage, are not supported in this release.
Monitoring. SELECT * FROM mooncake.list_tables(); lists the mirrors with their commit and flush LSNs.
Removing pg_mooncake. Drop all mirrors first (DROP TABLE <mirror>;): DROP EXTENSION pg_mooncake CASCADE would
drop the mirror tables without telling moonlink, which keeps replicating them. Then DROP EXTENSION pg_mooncake;, remove it
from shared_preload_libraries, restart Postgres, and delete the mooncake.data_directory directory
($PGDATA/pg_mooncake/ by default).
Backups and standbys
moonlink keeps all its files, including its RPC socket moonlink.sock, in the directory set by
mooncake.data_directory. The setting is read at server start, so changing it needs a restart. The default,
pg_mooncake, is relative to the data directory, so mirrors live in $PGDATA/pg_mooncake/; an absolute path puts them
elsewhere; ./pg_mooncake and the absolute path of $PGDATA/pg_mooncake count as the default. At startup Postgres
creates the directory (mode 0700) if it is missing, and checks it like a data directory: it must be owned by the
server's user, writable, have mode 0700 or 0750, not be a symbolic link, and not be the data directory or contain it,
and the socket path must be at most 107 bytes. Otherwise the server refuses to start with a FATAL that says why.
One server per directory: the server locks moonlink.lock in the directory while it runs, and refuses to start if
another server holds that lock (the error names that server's data directory and postmaster PID) or if a moonlink socket
there accepts connections. The lock file stays after a shutdown; that is harmless, and the next start locks it again.
pg_basebackup. With the default directory, pg_basebackup fails while any mirror exists
(file name too long for tar format): moonlink's file names are longer than the 99 characters Postgres' tar stream
allows. To use pg_basebackup, also to create a standby, set mooncake.data_directory to a directory outside the data
directory, for example mooncake.data_directory = '/var/lib/pg_mooncake', and restart Postgres before creating
mirrors. A non-default directory inside the data directory gets a startup WARNING for the same reason. Mirror data
is then not part of a base backup. A copy started on the same host inherits the setting from postgresql.conf: give
it its own mooncake.data_directory before starting it, or it refuses to start while the original server runs.
Restoring. A restored copy starts with an empty moonlink directory: mooncake.list_tables() is empty, and reading a
mirror fails with an error (Table <database>.<schema>.<mirror> not found). Drop and re-create each mirror to take a
new snapshot of its source.
Standbys. moonlink runs only once recovery finishes, so not on a standby: there, mirrors can't be read (queries on
them fail with an error) and mooncake.list_tables() fails with failed to connect to moonlink. moonlink starts when
the standby is promoted. A promoted standby keeps the mirrors whose files are in its mooncake.data_directory; drop and
re-create the others.
Security
- Grant
pg_read_server_filesandpg_write_server_filesonly to roles you trust like a superuser. Reading mirrors requires them (see Roles), and they let a role read and write any file the Postgres server can, through DuckDB as well as throughCOPY. That includes every mirror's data files. - Mirrors don't honour
GRANTandREVOKE. Privileges on a mirror table are not checked when it is read, so a role that can read one mirror can read all of them. Limit access by controlling who has the roles above, not with privileges on individual mirrors. - The privileges of the source table still apply to the source table itself; only reads of mirrors bypass them.
The CHANGELOG lists the other known limitations.
Durability and Recovery
Moonlink acknowledges the replication slot only up to what is persisted in Iceberg and replays the rest from the slot
after a restart. Idle mirrors are persisted at least every 5 minutes, which bounds how much pg_wal they hold back.
Errored mirrors don't hold the slot back.
If a mirror can no longer be kept in sync (for example after TRUNCATE, a schema change of its source table, or a
value it cannot store), it enters an errored state. Queries on it fail with a message that says why. Drop it and
re-create it:
DROP TABLE trades_iceberg;
CALL mooncake.create_table('trades_iceberg', 'trades');
Dropping a mirror always succeeds, even while moonlink is down. Moonlink then keeps its copy until the mirror is created again under the same name, which removes it. After a crash, if a mirror looks wrong, do the same.
Coming from 0.1.x
pg_mooncake 0.3.0 is a new architecture. 0.1.x stored data in its own columnstore tables; 0.3.0 keeps your data in
regular Postgres tables and mirrors them into Iceberg through logical replication. Nothing is migrated from 0.1.x, and
ALTER EXTENSION pg_mooncake UPDATE cannot upgrade it.
- Copy any data you want to keep into regular tables, for example
CREATE TABLE t_heap AS SELECT * FROM t;.CREATE TABLE ASdoesn't copy the primary key or other constraints, so add them back (ALTER TABLE t_heap ADD PRIMARY KEY (id);); a partitioned source needs a primary key to be mirrored. - Drop the 0.1 extension and its columnstore tables:
DROP EXTENSION pg_mooncake CASCADE;. - Delete 0.1's data files, which
DROP EXTENSIONleaves behind:$PGDATA/mooncake_local_tables/and$PGDATA/mooncake_local_cache/, and, if you setmooncake.default_bucket, themooncake_<database>_<table>_<oid>/prefixes in that bucket. - Install 0.3.0 (see Installation). Add
pg_duckdb,pg_mooncaketoshared_preload_libraries, setwal_level = logicalandduckdb.allow_community_extensions = true, and restart Postgres beforeCREATE EXTENSION pg_mooncake CASCADE.
Then follow the Quick Start. See the CHANGELOG for what 0.3.0 does and its known limitations.
Contributing
pg_mooncake is an open-source project maintained by Mooncake Labs and licensed under the MIT License. We'd love your help to make it even better! Join our Slack, participate in discussions, open issues to report bugs or suggest features, contribute code and documentation, or help us improve the project in any way. All contributions are welcome! 🥮
To build and test a change, see AGENTS.md and TESTING.md.
استكشف مشاريع مشابهة ومرتبطة
جارٍ التحميل…