pg_mooncake Documentation

repository·main·Indexed 24 days ago

https://github.com/mooncake-labs/pg_mooncake

A Postgres extension (version 0.2.0) that provides real-time analytics by mirroring Postgres tables into an Apache Iceberg columnstore. It utilizes DuckDB for high-performance queries and moonlink for low-latency data ingestion, allowing users to query columnstore tables via a Postgres-native interface.

Tokens
1.2K
Snippets
5
Records
6
Agent score
34%

What's inside pg_mooncake

  1. What is pg_mooncake?

    main

    pg_mooncake is a Postgres extension that creates a columnstore mirror of your Postgres tables in Apache Iceberg. It enables fast analytics queries with sub-second freshness by combining:

    • Real-time ingestion: Powered by moonlink for streaming and batched INSERT/UPDATE/DELETE.
    • Fast analytics: Accelerated by DuckDB.
    • Postgres-native interface: Query columnstore tables just like regular Postgres tables.
    • Iceberg-native storage: Data is stored in Iceberg format, making it accessible to other query engines.
  2. Install pg_mooncake from source

    main

    To build from source, ensure you have [Rust], [pgrx], and [DuckDB build tools] installed.

    1. Clone the repository with submodules:
    git clone --recurse-submodules https://github.com/Mooncake-Labs/pg_mooncake.git
    1. Build and install (example for Postgres 18):
    cargo pgrx init --pg18=$(which pg_config)
    make pg_duckdb
    make install PG_VERSION=pg18
    1. Configure postgresql.conf to load the libraries and enable logical replication:
    duckdb.allow_community_extensions = true
    shared_preload_libraries = 'pg_duckdb,pg_mooncake'
    wal_level = logical
    git clone --recurse-submodules https://github.com/Mooncake-Labs/pg_mooncake.git
    
    # For Postgres 18
    cargo pgrx init --pg18=$(which pg_config)
    make pg_duckdb
    make install PG_VERSION=pg18
  3. Install pg_mooncake using Docker

    main

    For a quick start, use the official Docker image which comes with pg_mooncake preinstalled.

    1. Run the container:
    docker run --name mooncake --rm -e POSTGRES_PASSWORD=password mooncakelabs/pg_mooncake
    1. Connect to the instance using psql as the postgres user:
    docker exec -it mooncake psql -U postgres
  4. Quick Start: Create a columnstore mirror

    main

    Follow these steps to set up a real-time analytics mirror of a Postgres table using pg_mooncake.

    1. Create the extension:
    CREATE EXTENSION pg_mooncake CASCADE;
    1. Create a source table:
    CREATE TABLE trades(
      id bigint PRIMARY KEY,
      symbol text,
      time timestamp,
      price real
    );
    1. Create the Iceberg columnstore mirror: Use mooncake.create_table to create a mirror that stays in sync with the source table:
    CALL mooncake.create_table('trades_iceberg', 'trades');
    1. Verify synchronization: Insert data into the source table and query the mirror:
    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);
    
    SELECT avg(price) FROM trades_iceberg WHERE symbol = 'AMZN';
    CREATE EXTENSION pg_mooncake CASCADE;
    
    CREATE TABLE trades(
      id bigint PRIMARY KEY,
      symbol text,
      time timestamp,
      price real
    );
    
    CALL mooncake.create_table('trades_iceberg', '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);
    
    SELECT avg(price) FROM trades_iceberg WHERE symbol = 'AMZN';
  5. Create the mooncake access method in SQL

    main

    To use Mooncake as a table storage engine, you must first define the access method using the mooncake_am_handler function. This is typically done during the extension installation or setup phase. The following SQL commands define the handler and the mooncake access method type.

    CREATE FUNCTION mooncake_am_handler(internal) RETURNS table_am_handler LANGUAGE c AS 'MODULE_PATHNAME', '@FUNCTION_NAME@';
    CREATE ACCESS METHOD mooncake TYPE TABLE HANDLER mooncake_am_handler;
  6. Register the mooncake access method

    main

    The mooncake access method (AM) is registered within PostgreSQL via a handler function. This allows PostgreSQL to use Mooncake's storage engine for table operations. The registration is performed by calling RegisterDuckdbTableAm with the name mooncake and the MOONCAKE_AM routine structure.

    CREATE FUNCTION mooncake_am_handler(internal) RETURNS table_am_handler LANGUAGE c AS 'MODULE_PATHNAME', '@FUNCTION_NAME@';
    CREATE ACCESS METHOD mooncake TYPE TABLE HANDLER mooncake_am_handler;