pg_duckdb

在PostgreSQL中的嵌入式DuckDB扩展

扩展总览

PIGSTY 第三方扩展: pg_duckdb : 在PostgreSQL中的嵌入式DuckDB扩展

基本信息

  • 扩展编号: 2430
  • 扩展名称: pg_duckdb
  • 标准包名: pg_duckdb
  • 扩展类目: OLAP
  • 开源协议: MIT
  • 官方网站: https://github.com/duckdb/pg_duckdb
  • 编程语言: C++
  • 其他标签: duckdb
  • 备注信息:

元数据

  • 默认版本: 0.3.1
  • PG大版本: 17,16,15,14
  • 动态加载: 需要显式加载
  • 需要DDL: 需要执行 CREATE EXTENSION DDL
  • 可重定位: 可以重定位安装至其他模式下
  • 信任程度: 未受信任,创建扩展需要超级用户权限
  • 所需模式: 无
  • 所需扩展: 无

软件包

  • RPM仓库:PIGSTY
  • RPM包名:pg_duckdb_$v*
  • RPM版本:0.3.1
  • RPM依赖:无
  • DEB仓库:PIGSTY
  • DEB包名:postgresql-$v-pg-duckdb
  • DEB版本:0.3.1
  • DEB依赖:无

最新版本

系统 架构 PG17 PG16 PG15 PG14 PG13
el8 x86_64
el8 aarch64
el9 x86_64 pg_duckdb_17
PIGSTY 0.3.1
pg_duckdb_16
PIGSTY 0.3.1
pg_duckdb_15
PIGSTY 0.3.1
pg_duckdb_14
PIGSTY 0.3.1
el9 aarch64 pg_duckdb_17
PIGSTY 0.3.1
pg_duckdb_16
PIGSTY 0.3.1
pg_duckdb_15
PIGSTY 0.3.1
pg_duckdb_14
PIGSTY 0.3.1
d12 x86_64 postgresql-17-pg-duckdb
PIGSTY 0.3.1
postgresql-16-pg-duckdb
PIGSTY 0.3.1
postgresql-15-pg-duckdb
PIGSTY 0.3.1
postgresql-14-pg-duckdb
PIGSTY 0.3.1
d12 aarch64 postgresql-17-pg-duckdb
PIGSTY 0.3.1
postgresql-16-pg-duckdb
PIGSTY 0.3.1
postgresql-15-pg-duckdb
PIGSTY 0.3.1
postgresql-14-pg-duckdb
PIGSTY 0.3.1
u22 x86_64 postgresql-17-pg-duckdb
PIGSTY 0.3.1
postgresql-16-pg-duckdb
PIGSTY 0.3.1
postgresql-15-pg-duckdb
PIGSTY 0.3.1
postgresql-14-pg-duckdb
PIGSTY 0.3.1
u22 aarch64 postgresql-17-pg-duckdb
PIGSTY 0.3.1
postgresql-16-pg-duckdb
PIGSTY 0.3.1
postgresql-15-pg-duckdb
PIGSTY 0.3.1
postgresql-14-pg-duckdb
PIGSTY 0.3.1
u24 x86_64 postgresql-17-pg-duckdb
PIGSTY 0.3.1
postgresql-16-pg-duckdb
PIGSTY 0.3.1
postgresql-15-pg-duckdb
PIGSTY 0.3.1
postgresql-14-pg-duckdb
PIGSTY 0.3.1
u24 aarch64 postgresql-17-pg-duckdb
PIGSTY 0.3.1
postgresql-16-pg-duckdb
PIGSTY 0.3.1
postgresql-15-pg-duckdb
PIGSTY 0.3.1
postgresql-14-pg-duckdb
PIGSTY 0.3.1

扩展安装

使用 pig 命令行工具安装 pg_duckdb 扩展:

pig ext install pg_duckdb

使用 Pigsty剧本 安装 pg_duckdb 扩展:

./pgsql.yml -t pg_extension -e '{"pg_extensions": ["pg_duckdb"]}' # -l <集群名>

YUM仓库 手工安装 pg_duckdb RPM 包:

dnf install pg_duckdb_17*;
dnf install pg_duckdb_16*;
dnf install pg_duckdb_15*;
dnf install pg_duckdb_14*;

APT仓库 手工安装 pg_duckdb DEB 包:

apt install postgresql-17-pg-duckdb;
apt install postgresql-16-pg-duckdb;
apt install postgresql-15-pg-duckdb;
apt install postgresql-14-pg-duckdb;

扩展 pg_duckdb 需要通过 shared_preload_libraries 进行 动态加载

shared_preload_libraries = 'pg_duckdb'; # 修改 PG 集群配置

使用以下 SQL 命令在已经安装此扩展插件的 PG 集群上 启用 pg_duckdb 扩展:

CREATE EXTENSION pg_duckdb;

使用方法

Add pg_duckdb to shared_preload_libraries via patronictl

pg edit-config --force -p shared_preload_libraries='pg_duckdb, pg_stat_statements, auto_explain'
pg restart --force pg-meta

Create Extension

CREATE EXTENSION pg_duckdb;

Generate some data

pgbench -is100
\timing on

SELECT count(*) FROM pgbench_accounts;
-- 3268.023ms

# use the duckdb execution engine
SET duckdb.force_execution = true;


postgres@el8:5432/postgres=# explain SELECT count(*) FROM pgbench_accounts;
                                   QUERY PLAN
---------------------------------------------------------------------------------
Custom Scan (DuckDBScan)  (cost=0.00..0.00 rows=0 width=0)
DuckDB Execution Plan:

┌───────────────────────────┐
│    UNGROUPED_AGGREGATE    │
│    ────────────────────   │
│        Aggregates:        │
│        count_star()└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│     POSTGRES_SEQ_SCAN     │
│    ────────────────────   │
│         Function:         │
│     POSTGRES_SEQ_SCAN     │
│                           │
│       ~10000000 Rows      │
└───────────────────────────┘


JIT:
Functions: 1
Options: Inlining false, Optimization false, Expressions true, Deforming true
(22 rows)


postgres@el8:5432/postgres=# SELECT count(*) FROM pgbench_accounts;
count
----------
10000000
(1 row)

Time: 696.801 ms

According some user feedbacks, the duckdb engine can achieve 100x - 1000x speed up on certain queries.

Check more details @ https://github.com/duckdb/pg_duckdb





最后修改 2025-03-20: update extension version (52133a3)