ivy_duckdb Use Cases and User Guide

Use ivy_duckdb for reporting, aggregations, and ad hoc analysis on existing IvorySQL row-store tables. Applications continue writing to those tables while analytics sessions can send supported queries to DuckDB. If you need a continuously synchronized Iceberg columnstore mirror, see the ivy_mooncake user guide.

1. Set up the extension

These steps target IvorySQL 6.0 (PostgreSQL 19.0). After building and installing from source or starting the Docker container, create pg_duckdb in the test database. The examples query IvorySQL row-store tables directly.

1.1. Build and install from source

Set up an IvorySQL 6.0 development environment with a C++17 compiler, GNU Make, CMake, and Ninja. The default build also requires development libraries for OpenSSL, libcurl 7.85.0 or later, and LZ4. Put the target IvorySQL installation’s bin directory first on PATH and check that pg_config --version identifies the instance you use.

IVORYSQL_PG_CONFIG="$(command -v pg_config)"
"$IVORYSQL_PG_CONFIG" --version
git clone https://github.com/IvorySQL/ivy_duckdb.git
cd ivy_duckdb
git submodule update --init --recursive
make -j"$(getconf _NPROCESSORS_ONLN)" PG_CONFIG="$IVORYSQL_PG_CONFIG"
make install PG_CONFIG="$IVORYSQL_PG_CONFIG"

Add pg_duckdb to the existing shared_preload_libraries list in the target instance’s postgresql.conf, then restart the instance. Use the following setting when this is the only preloaded library.

shared_preload_libraries = 'pg_duckdb'

1.2. Run the Docker image

The ivy_mooncake:0.3 image is based on IvorySQL 6.0, comes with ivy_duckdb and ivy_mooncake preinstalled, and preloads pg_duckdb. For this guide, only create pg_duckdb in the test database and run the examples on ordinary row-store tables.

read -rsp 'IvorySQL test password: ' IVORYSQL_PASSWORD
printf '\n'
export IVORYSQL_PASSWORD
docker run -d --name ivy_duckdb_03 \
  -e IVORYSQL_PASSWORD \
  registry.highgo.com/mooncake/ivy_mooncake:0.3
unset IVORYSQL_PASSWORD

docker exec -it ivy_duckdb_03 bash

If the container is already running, enter it with docker exec -it ivy_duckdb_03 bash. For the Docker setup, run the remaining psql commands inside the container.

1.3. Create the extension in the test database

The examples use Oracle mode. The image uses port 1521 for Oracle mode. For a source installation, use your Oracle-mode instance’s port, user, and database.

psql -X -p 1521 -U ivorysql -d postgres -v ON_ERROR_STOP=1
CREATE EXTENSION IF NOT EXISTS pg_duckdb;

SELECT extname, extversion
FROM pg_extension
WHERE extname = 'pg_duckdb';

Create the extension in the database where you will run reports. To authorize an analytics role to use DuckDB, configure duckdb.postgres_role as appropriate for your deployment and grant the role access to the required tables.

2. Verify DuckDB execution on a row-store query

In the same Oracle-mode session, create the test table and run EXPLAIN twice for the same aggregation. Use a test database where htap_ora_source has yet to be created.

SHOW ivorysql.compatible_mode;
SELECT CAST(42 AS NUMBER(10,0)) FROM dual;

CREATE TABLE htap_ora_source (
    id NUMBER(10,0) PRIMARY KEY,
    amount NUMBER(10,2),
    channel VARCHAR2(12)
);

INSERT INTO htap_ora_source VALUES (1, 10.50, 'online');
INSERT INTO htap_ora_source VALUES (2, 20.00, 'store');

SET duckdb.force_execution = off;
EXPLAIN (COSTS OFF)
SELECT channel, SUM(amount) FROM htap_ora_source GROUP BY channel;

SET duckdb.force_execution = on;
EXPLAIN (COSTS OFF)
SELECT channel, SUM(amount) FROM htap_ora_source GROUP BY channel;

SELECT channel, SUM(amount)
FROM htap_ora_source
GROUP BY channel
ORDER BY channel;

SET duckdb.force_execution = off;

With forced execution off, IvorySQL plans this row-store query. The original test showed HashAggregate and Seq Scan. With it on, eligible queries run through DuckDB and the plan shows Custom Scan (DuckDBScan). This query returns online = 10.50 and store = 20.00.

3. Compare Query Times on Two Million Rows

Stay in Oracle mode. Count distinct customers by region on the same source table.

CREATE TABLE htap_bench_orders (
    region_id INT,
    customer_id INT,
    amount NUMBER(10,2)
);

INSERT INTO htap_bench_orders
SELECT MOD(n, 32), MOD(n, 180013), MOD(n, 10000) / 100.0
FROM pg_catalog.generate_series(1, 2000000) AS g(n);

ANALYZE htap_bench_orders;

In psql, enable \timing and record the Time after each SELECT. Alternate between the two settings for several runs and take the median for each setting.

\timing on

SET duckdb.force_execution = off;

SELECT region_id, COUNT(DISTINCT customer_id) AS unique_customers
FROM htap_bench_orders
GROUP BY region_id
ORDER BY region_id;

SET duckdb.force_execution = on;

SELECT region_id, COUNT(DISTINCT customer_id) AS unique_customers
FROM htap_bench_orders
GROUP BY region_id
ORDER BY region_id;

EXPLAIN (COSTS OFF)
SELECT region_id, COUNT(DISTINCT customer_id) AS unique_customers
FROM htap_bench_orders
GROUP BY region_id
ORDER BY region_id;

SET duckdb.force_execution = off;
\timing off

The timings below come from the original linux/amd64 image test, which queried the row-store source. Each setting was tested three times and returned the same results for all 32 regions. For a standalone pg_duckdb setup, record your own times.

duckdb.force_execution Three query times Median

off

2.23 s, 1.83 s, 1.71 s

1.83 s

on

0.52 s, 0.30 s, 0.45 s

0.45 s

This query ran about 4.1 times as fast with DuckDB forced execution on. Use Custom Scan (DuckDBScan) in EXPLAIN to check the execution path and the measured SELECT times to compare performance.

4. Run a DuckDB query with forced execution off

With duckdb.force_execution off, ordinary IvorySQL table queries follow the default planning path. Use duckdb.query() to send an explicit DuckDB SQL query to the enabled pg_duckdb extension. This example uses DuckDB’s range and returns a sum of 6.

SET duckdb.force_execution = off;
SELECT * FROM duckdb.query('SELECT sum(i) AS total FROM range(1, 4) AS t(i)');