ducksync

Intelligentes Caching von Abfrageergebnissen zwischen DuckDB und Snowflake mit TTL und intelligentem Refresh

Maintainer: danjsiegel

Installation und Laden

INSTALL ducksync FROM community;
LOAD ducksync;

Beispiel

-- Install and load the extension
INSTALL ducksync FROM community;
LOAD ducksync;
-- Option A: Use an existing DuckLake catalog
ATTACH 'ducklake:postgres:host=localhost dbname=ducklake user=postgres password=secret' AS my_lake (DATA_PATH '/data');
SELECT * FROM ducksync_init('my_lake');
-- Option B: Full setup (attaches DuckLake automatically)
SELECT * FROM ducksync_setup_storage(
'host=localhost port=5432 dbname=ducklake user=postgres password=secret',
'/data/ducksync'
);
-- Register Snowflake source (requires existing DuckDB secret)
SELECT * FROM ducksync_add_source('prod', 'snowflake', 'my_snowflake_secret');
-- Create a cache with 1-hour TTL
SELECT * FROM ducksync_create_cache(
'customers_cache',
'prod',
'SELECT * FROM PROD.PUBLIC.CUSTOMERS',
['PROD.PUBLIC.CUSTOMERS'],
3600
);
-- Query via smart routing (cache hit or passthrough)
SELECT * FROM ducksync_refresh('customers_cache');
-- Query via smart routing: cache hit → DuckLake, miss → Snowflake passthrough
SELECT * FROM ducksync_query(
'SELECT * FROM PROD.PUBLIC.CUSTOMERS WHERE region = ''US''',
'prod'
);
-- Or query the DuckLake table directly
SELECT * FROM ducksync.prod.customers_cache;

Über ducksync

DuckSync bietet intelligentes Caching von Abfrageergebnissen zwischen DuckDB und Snowflake. Es nutzt DuckLake zur Speicherung (PostgreSQL-Katalog + Parquet-Dateien) und bietet transparente Abfrage- Weiterleitung, TTL-basiertes Verfallen und intelligentes Refresh anhand der Metadaten der Quelltabellen.

Kernablauf:

  1. Eine Snowflake-Quelle mit ducksync_add_source registrieren
  2. Einen Cache mit ducksync_create_cache definieren (SQL-Abfrage + überwachte Tabellen + TTL)
  3. Mit ducksync_refresh befüllen (holt Daten aus Snowflake, speichert sie in DuckLake)
  4. Mit ducksync_query abfragen — leitet automatisch an den Cache oder Snowflake weiter

Funktionen:

  • Intelligente Abfrageweiterleitung: ducksync_query() leitet automatisch an den Cache oder Snowflake weiter
  • Benannte Abfragen: Komplexe Abfragen unter sprechenden Namen cachen
  • TTL-Unterstützung: Konfigurierbares Cache-Ablaufen mit automatischem Refresh
  • Intelligentes Refresh: Aktualisiert nur, wenn sich Quelltabellen geändert haben (prüft last_altered)
  • Direktzugriff: Gecachte Daten als normale DuckLake-Tabellen abfragen
  • AST-basiertes Umschreiben: Sichere Abfragetransformation, die nur Tabellenverweise ändert
  • Konfigurierbares Metadatenschema: Optionalen schema_name an ducksync_init oder ducksync_setup_storage übergeben für Mandantenumgebungen

Voraussetzungen:

  • PostgreSQL-Datenbank (für den DuckLake-Katalog)
  • Snowflake-Konto mit konfiguriertem DuckDB-Secret
  • ADBC-Snowflake-Treiber (siehe duckdb-snowflake-Einrichtung)

Logik der Abfrageweiterleitung:

  1. Parst SQL mit dem DuckDB-Parser, um Tabellenverweise zu extrahieren
  2. Prüft, ob jede Tabelle gecacht ist (nach Cachenamen oder überwachter Tabelle)
  3. Wenn ALLE Tabellen gecacht sind → schreibt die Abfrage auf lokale DuckLake-Tabellen um
  4. Wenn EINE Tabelle nicht gecacht ist → leitet die gesamte Abfrage an Snowflake weiter

Die vollständige Dokumentation finden Sie im Erweiterungs-Repository.

Hinzugefügte Funktionen

function_name function_type description comment examples
ducksync_init table Initialize DuckSync with an existing DuckLake catalog. Use when you already have DuckLake attached. Recommended for existing DuckLake users. Optional second arg schema_name (default ‘ducksync’) for multi-tenant environments. [SELECT * FROM ducksync_init(‘my_lake’);]
ducksync_init table Initialize DuckSync with an existing DuckLake catalog and a custom metadata schema name. Use a custom schema_name to isolate DuckSync metadata in shared DuckLake environments (e.g. GizmoSQL). [SELECT * FROM ducksync_init(‘my_lake’, ‘my_ducksync_schema’);]
ducksync_setup_storage table Full setup — attaches DuckLake with a PostgreSQL catalog and initializes DuckSync. Use when you don’t have DuckLake configured yet. Optional third arg schema_name (default ‘ducksync’). [SELECT * FROM ducksync_setup_storage(‘host=localhost dbname=ducklake user=postgres’, ‘/data/ducksync’);]
ducksync_setup_storage table Full setup with a custom metadata schema name for multi-tenant environments. Data tables always live in catalog.source_name.cache_name regardless of schema_name. [SELECT * FROM ducksync_setup_storage(‘host=localhost dbname=ducklake user=postgres’, ‘/data/ducksync’, ‘my_schema’);]
ducksync_add_source table Register a Snowflake data source using an existing DuckDB secret. Currently supports Snowflake. Optional passthrough_enabled=true to allow direct Snowflake passthrough. [SELECT * FROM ducksync_add_source(‘prod’, ‘snowflake’, ‘my_snowflake_secret’);]
ducksync_create_cache table Define a cached query result with a source query, monitor tables for smart refresh, and optional TTL in seconds. monitor_tables are checked for last_altered changes to determine if refresh is needed. TTL=0 means no expiry. [SELECT * FROM ducksync_create_cache(‘customers’, ‘prod’, ‘SELECT * FROM PROD.PUBLIC.CUSTOMERS’, [‘PROD.PUBLIC.CUSTOMERS’], 3600);]
ducksync_refresh table Refresh a cache by fetching from Snowflake and storing in DuckLake. Skips if source data unchanged (smart refresh). Returns result=REFRESHED, SKIPPED, or ERROR with rows_refreshed and duration_ms. Use force=true to bypass smart check. [SELECT * FROM ducksync_refresh(‘customers’);]
ducksync_query table Smart query routing — rewrites SQL to use cached DuckLake tables when all referenced tables are cached; passes through to Snowflake otherwise. Returns actual query results. Main query interface. Automatically refreshes expired caches before executing. Supports SELECT with JOINs, UNIONs, subqueries. [SELECT * FROM ducksync_query(’SELECT * FROM PROD.PUBLIC.CUSTOMERS WHERE region = ’‘US’‘’, ‘prod’);]
ducksync_serve table Start a Quack listener so DuckDB-native clients can connect to this DuckSync instance over the Quack remote-protocol. Requires initialization. Warns if not PostgreSQL-backed. Named params: token, allow_other_hostname, disable_ssl. Never auto-starts on init. [SELECT * FROM ducksync_serve(‘quack:localhost’, token := ‘mytoken’);]
ducksync_stop table Stop a previously started Quack listener. Requires initialization. Returns a one-row status result. [SELECT * FROM ducksync_stop(‘quack:localhost’);]

Überladene Funktionen

Diese Erweiterung fügt keine Funktionsüberladungen hinzu.

Hinzugefügte Typen

Diese Erweiterung fügt keine Typen hinzu.

Hinzugefügte Einstellungen

Diese Erweiterung fügt keine Einstellungen hinzu.