ivy_mooncake Technical Design
ivy_mooncake adapts pg_mooncake to IvorySQL and uses pg_mooncake as its database extension name. It uses PostgreSQL logical replication to maintain Apache Iceberg columnstore mirrors of transactional row-store tables. Applications write to the IvorySQL source tables. Moonlink applies committed changes asynchronously, and DuckDB queries the mirrors through a dedicated scan extension. This design covers IvorySQL 6.0 and pg_mooncake 0.3.0.
1. Components and data flow
| Component | Responsibility |
|---|---|
|
Registers the |
|
Runs in the Moonlink background worker. It reads source snapshots and logical replication streams and manages mirror changes, Iceberg snapshots, and recovery progress. |
|
Identifies queries against Mooncake mirrors and routes them to DuckDB for planning and execution. |
|
Provides the Mooncake scan extension for DuckDB. It requests the table schema and scan state from Moonlink over a Unix socket, then reads data files and deletion information. |
The mirror table in IvorySQL stores column definitions, the relation name, and the access method for analytical SQL. Moonlink manages the mirror data, and the DuckDB scan extension reads it. Applications run INSERT, UPDATE, and DELETE on the source table. See the ivy_duckdb technical design for query routing.
-
Writes and replication. Applications write to the IvorySQL row-store table. Moonlink reads the initial snapshot, then applies committed changes through WAL-based logical replication and persists the data as Iceberg snapshots.
-
Analytical queries. DuckDB queries the mirror through the Mooncake scan extension. The scanner requests scan state from Moonlink over a Unix socket, reads the Iceberg snapshot files and temporary Parquet files generated for the scan, and applies deletion information.
2. Mirror creation and incremental replication
CALL mooncake.create_table('mirror_table', 'source_table') reads the source column definitions, creates a USING mooncake table in the target database, and sends a CreateTable request to Moonlink. It creates the table using a separate autocommit connection. If the calling transaction rolls back, a successfully created mirror must be dropped explicitly. If registration fails, the extension checks the relation OID and attempts cleanup once it confirms that the relation is the one created by this call.
Moonlink manages the publication and logical replication slot on the source. The publication is named moonlink_pub, and the slot is usually named moonlink_slot_<database>. Moonlink sets REPLICA IDENTITY FULL on the source table and its relevant partitions to capture the old row for updates and deletes. Account for the additional WAL these operations can generate.
The initial copy uses a consistent source snapshot. Its WAL position (LSN) marks where incremental replay picks up, avoiding duplicate application of changes. mooncake.create_table returns successfully after the initial data persists as an Iceberg snapshot. Moonlink then applies committed inserts, updates, and deletes. An update writes a new row version and marks the old one as deleted. Scans apply deletes using row positions and deletion vectors.
3. ivy_moonlink internals
The Moonlink worker receives source changes through a logical replication connection and serves mirror management and read requests from pg_mooncake and the DuckDB scan extension over moonlink.sock. See the ivy_moonlink technical design for the Rust modules, transaction visibility, and Iceberg persistence.
4. How DuckDB reads mirrors
The Mooncake scan extension uses GetTableSchema to retrieve the Arrow schema and ScanTableBegin to obtain the files and deletion information for the scan. A single scan can read both kinds of data.
-
Persisted data. The scan extension reads the Parquet files referenced by the Iceberg snapshot and applies deletion vectors.
-
Applied changes pending publication to Iceberg. Moonlink materializes these changes in temporary Parquet files as needed and provides positional deletes for the scan.
Each scan uses its own connection to Moonlink. Moonlink retains the read state and file references while the scan is active. It releases them on ScanTableEnd or when the connection closes. The scan therefore continues to use its initial file set as background snapshots, compaction, and cleanup progress.
5. Data visibility and external reads
Source commits, application of changes by Moonlink, visibility to mirror queries, and persistence to Iceberg snapshots each have their own progress. Diagnose read visibility and Iceberg persistence separately.
| Read scenario | Visibility rule |
|---|---|
Writing session reads the mirror after commit |
The scan carries the LSN of the session’s last commit. Moonlink waits for the target mirror to reach that lower bound before providing scan state. |
Separate reporting session reads the mirror |
Changes become visible after Moonlink applies them. Lag depends on transaction size, replication backlog, and current load. |
External engine reads through Iceberg |
The published Iceberg snapshot determines what is visible. The engine must have access to the filesystem catalog and data files and support the Iceberg metadata and deletion vectors the mirror uses. |
mooncake.list_tables() reports commit_lsn, flush_lsn, and iceberg_warehouse_location for each mirror in the current database. Use commit_lsn to track applied changes and flush_lsn to track persistence. To wait for the target mirror to persist the current session’s committed changes to Iceberg, run CALL mooncake.create_snapshot('mirror_table') after committing.
6. Storage, WAL retention, and restart recovery
mooncake.data_directory sets the mirror storage directory and defaults to $PGDATA/pg_mooncake. It contains the SQLite mirror registry, Iceberg data and metadata, indexes, staging files for the initial copy, and temporary scan files. Budget storage for the source tables, source WAL, persistent mirror data, and temporary space during initial copies and compaction.
Moonlink 0.3 recovers mirrors from Iceberg snapshots and source WAL. For healthy mirrors that share a replication stream, the slowest Iceberg persistence position determines how far the slot advances its confirmed position. The source thus retains WAL for changes that still need replay. On restart, Moonlink reads the mirror registry from SQLite, loads the latest Iceberg snapshot for each mirror, and resumes replaying changes from the logical replication slot.
To use pg_basebackup, configure mooncake.data_directory outside PGDATA and restart before creating mirrors. Give each instance its own directory. The base backup covers the source database. Manage the external mirror directory separately. If you restore with an empty mirror directory, drop the restored mirror table definitions and recreate the mirrors from the restored source tables. On a standby, Moonlink starts after promotion, when recovery ends. A promoted instance can reuse existing mirrors if their files and recovery state are available in its mirror directory.
7. Oracle compatibility mode and mirror limits
Moonlink uses internal type names in the sys schema and column type modifiers to map Oracle-compatible columns to Arrow during the initial copy and WAL-based logical replication. The scan extension obtains the Arrow schema and scan state over a Unix socket, then reads the Parquet files referenced by the Iceberg snapshot and temporary Parquet files generated for the scan.
NUMBER(p,s) maps to Decimal128(p,s) when the precision and scale are supported. NUMBER without a precision maps to Decimal128(38,10). Oracle DATE includes a time of day and maps to a microsecond-resolution timestamp without a time zone. The Arrow field carries ivory_original_type=DATE so downstream readers can distinguish it from an ordinary timestamp. See the ivy_duckdb technical design for type mappings when querying the row-store source table directly.
Compare mirror and source results using representative monetary values, timestamps, and character data. Initial copying and incremental updates have been verified in Oracle mode for ordinary unquoted table names. Quoted mixed-case identifiers have compatibility limits.
A source TRUNCATE, certain schema changes, or values that cannot be stored can put a mirror into an error state. Queries against that mirror then fail. Resolve the source schema or data issue, then drop and recreate the mirror. Mirror reads are intended for trusted analytical roles. The DuckDB execution role and server filesystem permissions govern access. This version skips per-table privilege checks when reading mirrors, so control mirror access through roles.
8. Operational diagnostics
Run these queries in the database that contains the target mirror.
SELECT mooncake.bgworker_status();
SELECT * FROM mooncake.list_tables();
SELECT slot_name, active, restart_lsn, confirmed_flush_lsn
FROM pg_replication_slots
WHERE slot_type = 'logical'
AND database = current_database();
Check socket_listening to confirm that the worker accepts connections. Compare commit_lsn and flush_lsn for the target mirror to see how far changes have been applied and persisted. On the corresponding replication slot, restart_lsn is the earliest WAL position still needed, and confirmed_flush_lsn is the position that the replication consumer has acknowledged. To confirm visibility to a reporting session, query a known committed change from a separate analytics connection.
See the ivy_mooncake user guide for installation and examples for PG and Oracle modes.