agentleFS
Mooncake-LabsMooncake-Labs/pg_mooncake/AGENTS.md

pg_mooncake

AGENTS.md2k starsChanged 13 months agoMITView on GitHub

The contributor guide: the invariants a change must keep, where things live, how to build and test, and common pitfalls. The architecture and the wire format are described in docs/ARCHITECTURE.md, and the tests in TESTING.md. Statements tagged [pins] hold at the submodule pins listed at the top of docs/ARCHITECTURE.md. Re-verify them whenever a submodule pin moves. Users get duckdb_mooncake as a prebuilt binary: CREATE EXTENSION runs duckdb.install_extension('mooncake', 'community') (src/sql/bootstrap.sql). That binary was built against moonlink_rpc at the commit locked in…

  • Deletes or force-pushes
  • Installs packages
  • Where files are headingNew AGENTS.md files have more rules about keeping secrets out of the code, and fewer about code style.

    Of the AGENTS.md files we read, the 33,398 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.

  • In this repositoryOne other file it gives its agents.

The file

We split this AGENTS.md into the rules it gives an agent

8 rules. About 0 are common, 1 is distinctive, and 7 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 7

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    No version is negotiated at runtime, so anything below that changes breaks every user on the default install, even when your local tests …

    line 15
  2. Very specific:

    If DropTable fails (moonlink down, erroring, or silent), the DROP still commits and emits a WARNING per mirror.

    line 75
  3. Very specific:

    All drops of a transaction share one 30 s deadline, and once moonlink is unreachable the remaining ones are not attempted.

    line 76
  4. Very specific:

    Never .expect()/.unwrap() an RPC result.

    line 87
  5. Very specific:

    Add this to the cluster's postgresql.conf (pgrx clusters live in ~/.pgrx/data-NN):

    line 134
  6. Very specific:

    Call moonlink with rpc!(moonlink_rpc::<fn>, args..).unwrap_or_else(|e| error!("mooncake.x failed: {e}")).

    line 185
  7. Very specific:

    Use only existing RPCs (section 1: the wire format must not change).

    line 186
  8. Very specific:

    Add a regress test (pitfall 6) and run make test for every supported major.

    line 190

≈ “Common” is an estimate: at least 1 in 5 of the 43,423 AGENTS.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
AGENTS.md

Contributing to pg_mooncake

The contributor guide: the invariants a change must keep, where things live, how to build and test, and common pitfalls. The architecture and the wire format are described in docs/ARCHITECTURE.md, and the tests in TESTING.md.

Statements tagged [pins] hold at the submodule pins listed at the top of docs/ARCHITECTURE.md. Re-verify them whenever a submodule pin moves.

1. Contract with duckdb_mooncake: never change these

Users get duckdb_mooncake as a prebuilt binary: CREATE EXTENSION runs duckdb.install_extension('mooncake', 'community') (src/sql/bootstrap.sql). That binary was built against moonlink_rpc at the commit locked in duckdb_mooncake/moonlink_ffi/Cargo.lock (git dependency on the moonlink repo). No version is negotiated at runtime, so anything below that changes breaks every user on the default install, even when your local tests (with a locally built extension) pass.

  • Do not change the wire format (described in docs/ARCHITECTURE.md). The rpcs!{} list in moonlink/src/moonlink_rpc defines it: bincode config::standard(), where the enum variant index is encoded, so order matters. The list happens to be alphabetical, so adding a variant "in order" silently renumbers the ones after it.
  • Framing (moonlink_rpc/src/lib.rs write/read): u32 native-endian length prefix, then the bincode payload.
  • Responses are RpcResult<T> = Result<T, moonlink_error::ErrorStruct>. Do not change ErrorStruct (moonlink/src/moonlink_error/src/error.rs: message, status: ErrorStatus, source with custom serde, location) or ErrorStatus. The prebuilt binary decodes every error reply through them.
  • GetTableSchema returns an Arrow IPC stream with only the schema (arrow_ipc::writer::StreamWriter in moonlink_service/src/rpc_server.rs), which duckdb_mooncake decodes with nanoarrow.
  • ScanTableBegin payload (moonlink/src/moonlink_table_metadata/src/table_metadata.rs Encode): u32 native-endian arrays that duckdb_mooncake/src/storage/mooncake_table_metadata.cpp reinterprets in place. Puffin deletion-vector blobs are read as portable CRoaring at offset+20, length size-24. Every file named in the payload must stay readable until ScanTableEnd.
  • C symbols that duckdb_mooncake dlsyms from pg_mooncake.so (duckdb_mooncake/src/include/pgmooncake.hpp): pgmooncake_get_init_query, pgmooncake_get_lsn, pgmooncake_drop_cstring (src/duckdb_mooncake.rs). pgmooncake_drop_cstring has no Rust caller, but it is not dead code: removing it makes the extension call a NULL function pointer.
  • ATTACH string and naming: ATTACH DATABASE 'mooncake' (TYPE mooncake, URI '<mooncake.data_directory>/moonlink.sock', DATABASE '<db>') (pgmooncake_get_init_query). With a relative mooncake.data_directory, such as the default pg_mooncake, the socket path is relative to the backend's cwd ($PGDATA). Tables are addressed as unquoted schema.table (format!("{}.{}", schema, table) in duckdb_mooncake/moonlink_ffi/src/lib.rs), so pg_mooncake must register mirrors with moonlink under that exact string.

2. Other invariants

  1. The moonlink background worker must not panic or error out. [pins] Restart behavior:
    • A panic in a per-table handler puts that mirror into the errored state (a fallback loop keeps answering for it and still performs its DROP); other mirrors keep replicating. A panic in a per-connection RPC handler surfaces as early eof on that connection.
    • A mirror that fails recovery at startup is errored (a transient failure is retried) instead of failing the worker.
    • An error or panic on the main path exits with status 1 and the worker restarts after 15 s (src/bgworker.rs unwraps start_with_config's result on the main thread).
    • moonlink returns only after SIGTERM. During a server shutdown (stopping in postmaster.pid) the worker exits with status 0. Any other SIGTERM (a kill of the worker; pg_terminate_backend can't reach it, since it has no database connection) exits with status 1, so Postgres restarts it after 15 s. Keep shutdowns at status 0: the e2e gate's stop checks count abnormal worker exits.
  2. Shutdown signals are handled only on the worker's main thread (src/bgworker.rs). moonlink's tokio::signal handler chains to pgrx's SIGTERM handler, which calls into Postgres and panics on any other thread, so SIGTERM, SIGHUP and SIGINT are blocked in every moonlink thread: the main thread blocks them while it creates the runtime and the moonlink thread (which inherit its mask), on_thread_start covers threads created later, and the main thread then restores its own mask and only waits. Keep that if you change how the runtime is built, and don't call Postgres from moonlink threads.
  3. ServiceConfig is built with a struct literal in src/bgworker.rs. Adding or removing a field in moonlink_service needs a matching edit there. [pins]
  4. On this path moonlink uses only the sqlite metadata store (<mooncake.data_directory>/moonlink_metadata_store.sqlite, the recovery source of truth) and the filesystem Iceberg catalog. It keeps no write-ahead log: the replication slot is acknowledged only up to what every healthy mirror has persisted to Iceberg, and the rest is replayed from the slot after a restart (docs/ARCHITECTURE.md, "Consistency model"). [pins]
  5. User-level recovery is "drop and re-create the mirror" (DROP TABLE <mirror>; CALL mooncake.create_table(...)). DROP must succeed no matter what state moonlink is in (the flow: docs/ARCHITECTURE.md, "Processes and data flow", step 6):
    • An object_access_hook (src/drop_tracking.rs) records which dropped relations are mooncake mirrors (by relam, before the catalog rows go away; this covers cascades and DROP SCHEMA). The sql_drop trigger queues those, and a pre-commit transaction callback sends DropTable for each one that is still gone at top-level commit, so a drop undone by ROLLBACK TO SAVEPOINT or an exception block keeps the mirror. PREPARE TRANSACTION after dropping a mirror is an error. Dropping any other table never talks to moonlink.
    • If DropTable fails (moonlink down, erroring, or silent), the DROP still commits and emits a WARNING per mirror. All drops of a transaction share one 30 s deadline, and once moonlink is unreachable the remaining ones are not attempted. moonlink may then still hold the mirror (it shows up in mooncake.list_tables()).
    • moonlink also keeps a mirror whose table was dropped without the sql_drop trigger firing: by DROP EXTENSION pg_mooncake CASCADE (the trigger is dropped by the same command) or with session_replication_role = replica (event triggers don't fire). mooncake.create_table therefore drops whatever moonlink holds under the new mirror's name before registering it (the name was free in Postgres, so that table is an orphan), and drops the new Postgres table again if moonlink refuses it. So re-creating a mirror cleans up its orphan; to clean one up without re-creating: CREATE TABLE <same name> (id int) USING mooncake; DROP TABLE <same name>;.
    • RPC errors become normal Postgres ERRORs raised after the connection lock is released (rpc! in src/utils.rs). The lock is poison-tolerant. A connection that saw an IO or decode error is discarded, and a cached connection is probed (MSG_PEEK) before reuse, so the first call after a moonlink restart reconnects instead of failing. Never .expect()/.unwrap() an RPC result.

3. Components and where things live

PathLanguageRole
src/Rust (pgrx 0.16.1)The extension: mooncake.* SQL API, mooncake table AM, the moonlink background worker
moonlink/ (submodule)RustIngestion engine inside the worker: CDC from a logical slot, Iceberg writer, Unix-socket RPC server
pg_duckdb/ (submodule)C++Embeds DuckDB in each backend. Routes queries on USING mooncake tables to DuckDB
duckdb_mooncake/ (submodule)C++ + Rust FFIDuckDB extension mooncake: gets a snapshot from moonlink, then reads the parquet/puffin files directly
WhatWhere
_PG_init, bootstrap SQLsrc/lib.rs, src/sql/bootstrap.sql
mooncake.create_table/create_snapshot/optimize_table/list_tables, drop event trigger, identifier parsingsrc/functions.rs
Which dropped relations are mirrors (object_access_hook)src/drop_tracking.rs
Rejecting writes, renames and attaching on mirrors (planner_hook and ProcessUtility_hook, run before pg_duckdb's)src/mirror_writes.rs
mooncake table AM (mostly unimplemented!()) and its registration with pg_duckdbsrc/table.rs
Background worker (ServiceConfig, start_with_config)src/bgworker.rs
Per-backend RPC connection (rpc!), tokio runtime, DATABASEsrc/utils.rs
C symbols for duckdb_mooncakesrc/duckdb_mooncake.rs
RPC definitions and framing / server / service startupmoonlink/src/moonlink_rpc/src/lib.rs, moonlink/src/moonlink_service/src/{rpc_server.rs,lib.rs}
Backend API, table_config JSON, recoverymoonlink/src/moonlink_backend/src/{lib.rs,table_config.rs,config_utils.rs,recovery_utils.rs}
Postgres CDC (slot, publication, confirmed-LSN feedback)moonlink/src/moonlink_connectors/src/pg_replicate*.rs, pg_replicate/
Table engine, Iceberg, union read, errored state [pins]moonlink/src/moonlink/src/{table_handler.rs,storage/,union_read/}
DuckDB side: catalog, scan, payload decoding, dlsyms, RPC clientduckdb_mooncake/src/storage/*.cpp, src/include/pgmooncake.hpp, src/moonlink/moonlink.cpp, moonlink_ffi/src/lib.rs
Regression teststests/pg_regress/{sql,expected}

4. Setup, build, test (standard toolchain)

The checks are make test (pg_regress) and the end-to-end gate scripts/e2e-smoke.sh, which covers what pg_regress cannot: moonlink down or hung, restarts, crashes, DDL and TRUNCATE on sources. See TESTING.md for both and for CI (.github/workflows/ci.yml: make test on PostgreSQL 14-18 for pull requests, plus the gate on PostgreSQL 17 and 18 for pushes). Run the gate yourself before changing the worker, the RPC client or DROP handling.

git clone --recurse-submodules https://github.com/Mooncake-Labs/pg_mooncake.git && cd pg_mooncake
# Rust: rustup picks up rust-toolchain.toml (1.88.0, clippy, rustfmt). pgrx needs rustfmt at build time.
cargo install --locked cargo-pgrx@0.16.1
cargo pgrx init --pg17 download --pg18 download    # or --pgNN "$(which pg_config)" for an existing install

Build and install for one PG major (repeat with pg17):

export PG_VERSION=pg18
make pg_duckdb PG_VERSION=$PG_VERSION    # pg_duckdb + libduckdb into that PG
make install PG_VERSION=$PG_VERSION      # release build; PG_CONFIG=<pg_config> installs into another PG

Add this to the cluster's postgresql.conf (pgrx clusters live in ~/.pgrx/data-NN):

shared_preload_libraries = 'pg_duckdb,pg_mooncake'   # this order: pg_mooncake needs pg_duckdb's symbols
wal_level = logical
duckdb.allow_community_extensions = true

Tests:

make test PG_VERSION=pg18     # cargo pgrx regress against ~/.pgrx/data-18; PATH does not matter here
make test PG_VERSION=pg17
cargo pgrx run pg18           # interactive psql on the pgrx cluster (`make run` = pg_duckdb + this, PG18)

make test installs a debug build of pg_mooncake. Reinstall with make install before measuring performance.

Using a locally built duckdb_mooncake

Needed only when you change duckdb_mooncake/. Build it with make duckdb_mooncake. CRoaring ≥ 1.x must be findable by CMake: the devcontainer uses vcpkg via VCPKG_TOOLCHAIN_PATH, and distro packages such as Ubuntu 24.04's libroaring-dev 0.2.66 are too old. Corrosion and nanoarrow are fetched by CMake. Then copy duckdb_mooncake/build/release/extension/mooncake/mooncake.duckdb_extension into <duckdb.extension_directory>/v1.4.1/linux_amd64/ without a .info file, which makes the bootstrap INSTALL a no-op, and set duckdb.allow_unsigned_extensions = true and duckdb.autoinstall_known_extensions = false. Both DuckDB trees (pg_duckdb/third_party/duckdb, duckdb_mooncake/duckdb) must be the same version (v1.4.1).

5. Pitfalls

  1. cargo pgrx install without --pg-config uses the first pg_config on PATH. With another Postgres on PATH it compiles for the right major but installs into the wrong prefix. make install and make package pass --pg-config "$PG_CONFIG"; call cargo pgrx install directly only with --pg-config.
  2. The bgworker cargo feature (default) is what starts moonlink. A --no-default-features build has no worker, and every call fails with mooncake.<procedure> failed: failed to connect to moonlink: ....
  3. mooncake.create_table creates the mirror table on a separate, autocommitted libpq connection (loopback by default, or src_uri). It is not part of the caller's transaction, so a ROLLBACK does not undo a create_table that returned. If the CreateTable RPC fails, create_table drops the new table again on that connection.
  4. Replication connections are keyed by src_uri (moonlink_connectors/src/replication_manager.rs). The slot moonlink_slot_<db> and publication moonlink_pub are dropped when the last mirror of that src_uri is dropped.
  5. No committed Cargo.lock (/Cargo.lock is gitignored): the moonlink dependency graph resolves fresh. postgres/postgres-native-tls are pinned to the rust-postgres revision moonlink uses (make check-rust-postgres checks it). .cargo/config.toml makes the resolver prefer versions that support rust-version in Cargo.toml; keep that equal to the rust-toolchain.toml channel, or a fresh resolution picks crates the pinned compiler cannot build.
  6. New regress test without an expected file: cargo pgrx regress prompts "Accept [Y, n]?" and panics without a terminal. --auto accepts new output and overwrites failing expected files. Prefer: create an empty expected/<name>.out, run make test, review tests/pg_regress/results/<name>.out, then copy it. Diffs land in tests/pg_regress/regression.diffs.

6. Recipe: adding a mooncake.* procedure

  1. In src/functions.rs, follow the existing pattern: #[pg_extern(sql = "CREATE PROCEDURE mooncake.x(...) LANGUAGE c AS 'MODULE_PATHNAME', '@FUNCTION_NAME@';")]. Parse table arguments with parse_table (Postgres identifier rules). Send TableName::moonlink_name() (unquoted schema.table) over RPC; use TableName::quoted() only in SQL you run yourself. Call moonlink with rpc!(moonlink_rpc::<fn>, args..).unwrap_or_else(|e| error!("mooncake.x failed: {e}")).
  2. Use only existing RPCs (section 1: the wire format must not change). A feature that needs a new RPC is a coordinated moonlink + duckdb_mooncake release, not a pg_mooncake change. If you do touch moonlink, the change has to land in the moonlink repo and the submodule pin has to move. An exhaustive match in rpc_server.rs::handle_stream and a MoonlinkBackend method go with every RPC.
  3. Add a regress test (pitfall 6) and run make test for every supported major.

7. Known limitations

The user-visible limitations are listed once, in CHANGELOG.md. Fix them with a test; don't paper over them. The end-to-end gate's expected failures and their failure signatures are in scripts/e2e-known-bugs.txt (TESTING.md).

8. Conventions

  • Rust: make format (cargo fmt + cargo clippy). duckdb_mooncake C++: make -C duckdb_mooncake format-all.
  • Reads from another session are eventually consistent (about 0.5 s locally). Only the writing session gets read-your-writes (a scan waits for that backend's XactLastCommitEnd). Tests must poll, or read in the writing session.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.