ivy_duckdb Technical Design
ivy_duckdb adapts pg_duckdb to IvorySQL. The build for IvorySQL 6.0 is based on pg_duckdb 1.2.2 and DuckDB 2.0.1. Its extension name in the database is pg_duckdb. Applications submit SQL through their existing IvorySQL connections. IvorySQL parses statements, resolves tables and column types, and establishes the transaction context. DuckDB runs eligible analytical operations. The extension can analyze IvorySQL row-store tables directly, query external files, and write query results to ordinary IvorySQL tables.
1. Query routing and execution plans
After IvorySQL parses and rewrites the SQL, the ivy_duckdb planner hook checks the tables and functions in the query tree to decide whether to attempt a DuckDB plan.
| Query type | Routing rule |
|---|---|
Queries on ordinary tables |
IvorySQL plans these queries by default. With |
Queries that require DuckDB |
Queries that call |
The extension turns the IvorySQL query tree into SQL that DuckDB can parse. DuckDB determines the output column names and types when it prepares the statement. The extension maps the output types back to IvorySQL and builds a Custom Scan (DuckDBScan) node. At execution time, the node passes DuckDB results back to IvorySQL, which returns them over the existing connection. See the planner hook and plan node construction.
2. Row-store scans and data visibility
DuckDB uses PostgresScan to read source tables. The extension builds a scan query from the columns it needs and the filters it can push down, then hands that query to the native IvorySQL planner. Applying the filters during the row-store scan can reduce the amount of data passed between engines. The scan uses the active transaction snapshot for the statement and reads rows that IvorySQL considers visible to that snapshot. See scan query construction and the native scan and active snapshot.
The extension converts batches of rows into DuckDB column vectors for operators such as aggregation, sorting, and joins. The row-store scan, type conversion, and vectorized execution all contribute to query time. To assess performance, compare broad scans and grouped reports as well as selective queries where the native planner can locate a few rows efficiently with an index.
For a continuously maintained Iceberg mirror, ivy_mooncake creates the mirror, ivy_moonlink replicates source changes, and ivy_duckdb reads the mirror through the scan extension. The transaction snapshot determines which source rows are visible. Mirror queries reflect the changes the mirror has applied. See the ivy_mooncake technical design.
3. External file queries and writeback
Use read_parquet(), read_csv(), and read_json() to filter or aggregate data from external files, or join it with IvorySQL tables. The database server resolves file paths. The DuckDB execution role, server filesystem permissions, and extension settings such as duckdb.enable_external_access and duckdb.allowed_directories govern external access. See the file reader functions and permissions and external access settings.
For an eligible INSERT INTO … SELECT, DuckDB executes the SELECT and IvorySQL keeps the native ModifyTable node. IvorySQL writes the rows produced by DuckDB and handles constraint checks, index maintenance, and the transaction commit. When selecting a dynamic column with r['column_name'], explicitly cast it in the SELECT to the type required by the target column. See the write plan and the writeback regression tests.
4. Oracle compatibility mode and type conversion
In Oracle compatibility mode, IvorySQL parses Oracle-compatible syntax. ivy_duckdb maps types using type identifiers and type modifiers such as numeric precision, scale, and binary column length. See the Oracle type mapping.
| Type | Execution and validation notes |
|---|---|
|
Maps to |
|
Maps to |
|
Oracle |
|
Map to DuckDB string types. Compare both execution paths for byte and character lengths, trailing spaces, sorting, and implicit casts. |
|
Use binary values. The regression cases for creating a table from query results check type identity and the |
Validate type compatibility with application SQL. Check the results when the same type is used in filters, joins, aggregates, and writeback. See the Oracle type regression tests.
5. Concurrent resource use and validation
Each IvorySQL backend session that uses DuckDB initializes its own DuckDB instance. duckdb.memory_limit and duckdb.threads set the memory budget and thread limit for that instance. duckdb.max_workers_per_postgres_scan and duckdb.threads_for_postgres_scan limit the PostgreSQL parallel workers and DuckDB threads used for a single source-table scan, respectively. Assess CPU, memory, temporary files, and row-store I/O against the number of analytics sessions. Measure transaction latency under a workload that combines transactions and analytics. See the resource settings.
Compare both execution paths for application SQL that can run on either.
-
Execution path. Set
duckdb.force_executiontooffandonand runEXPLAINunder each setting to check forCustom Scan (DuckDBScan). Enableduckdb.log_pg_explainto see the underlying IvorySQL scan plan. -
Result consistency. Compare row counts, aggregate values, numeric precision, timestamps, and null handling. Specify
ORDER BYwhen the result order matters. -
Query time. Use
\timinginpsqlto time aSELECTthat returns its results. Keep the data and other session settings fixed, alternate between the two paths for several runs, and record the median for each path. Also measure transaction latency and resource use while analytics run concurrently when assessing deployment performance.