multicorn

用Python编写自定义的外部数据源包装器

概览

扩展包名版本分类许可证语言
multicorn3.2FDWPostgreSQLC
ID扩展名BinLibLoadCreateTrustReloc模式
8510multicorn-
相关扩展wrappers odbc_fdw jdbc_fdw pgspider_ext mysql_fdw db2_fdw mongo_fdw redis_fdw

版本

类型仓库版本PG 大版本包名依赖
EXTPGDG3.21817161514multicorn-
RPMPGDG3.21817161514multicorn2_$v-
DEBPIGSTY3.21817161514postgresql-$v-multicornpython3-multicorn
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2
d13.x86_64
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2
d13.aarch64
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2
u22.x86_64
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2
u22.aarch64
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2
u24.x86_64
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2
u24.aarch64
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2
u26.x86_64
u26.aarch64
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2
PIGSTY 3.2

安装

您可以直接安装 multicorn 扩展包的预置二进制包,首先确保 PGDG 仓库已经添加并启用:

pig repo add pgdg -u          # 添加 PGDG 仓库并更新缓存

使用 pig 或者是 apt/yum/dnf 安装扩展:

pig install multicorn;          # 当前活跃 PG 版本安装
pig ext install -y multicorn -v 18  # PG 18
pig ext install -y multicorn -v 17  # PG 17
pig ext install -y multicorn -v 16  # PG 16
pig ext install -y multicorn -v 15  # PG 15
pig ext install -y multicorn -v 14  # PG 14
dnf install -y multicorn2_18       # PG 18
dnf install -y multicorn2_17       # PG 17
dnf install -y multicorn2_16       # PG 16
dnf install -y multicorn2_15       # PG 15
dnf install -y multicorn2_14       # PG 14
apt install -y postgresql-18-multicorn   # PG 18
apt install -y postgresql-17-multicorn   # PG 17
apt install -y postgresql-16-multicorn   # PG 16
apt install -y postgresql-15-multicorn   # PG 15
apt install -y postgresql-14-multicorn   # PG 14

创建扩展

CREATE EXTENSION multicorn;

用法

来源:READMECHANGELOG

Multicorn2 允许您用 Python 编写外部数据包装器。您需要实现一个继承自 multicorn.ForeignDataWrapper 的 Python 类,Multicorn 负责将其桥接到 PostgreSQL 的 FDW 接口。版本 3.2 已使用 PostgreSQL 14-18 和 Python 3.9-3.13 测试,不过上游建议近期待发行版兼容性优先使用 Python 3.10-3.12。

定义 Python FDW 类

创建一个 PostgreSQL 进程可访问的 Python 模块(例如 myfdw.py):

from multicorn import ForeignDataWrapper

class MyFDW(ForeignDataWrapper):
    def __init__(self, options, columns):
        super().__init__(options, columns)
        self.options = options
        self.columns = columns

    def execute(self, quals, columns):
        """以字典形式返回行。quals 包含 WHERE 下推信息。"""
        yield {"id": 1, "name": "example"}

    def insert(self, new_values):
        """处理 INSERT 操作。"""
        pass

    def update(self, old_values, new_values):
        """处理 UPDATE 操作。"""
        pass

    def delete(self, old_values):
        """处理 DELETE 操作。"""
        pass

创建服务器和外部表

CREATE EXTENSION multicorn;

CREATE SERVER multicorn_srv FOREIGN DATA WRAPPER multicorn
  OPTIONS (wrapper 'myfdw.MyFDW');

CREATE FOREIGN TABLE my_table (
  id integer,
  name text
)
SERVER multicorn_srv
OPTIONS (
  option1 'value1'
);

SELECT * FROM my_table;

wrapper 选项指定完全限定的 Python 类名。任何额外选项都会传递给类构造函数的 options 参数。

内置 FDW 示例

Multicorn 附带了几个可以直接使用或作为参考的 FDW 实现:

  • CsvFdw – 读取 CSV 文件
  • ProcessFdw – 执行系统命令并解析输出
  • GCalFdw – 访问 Google 日历
  • ImapFdw – 查询 IMAP 邮箱
  • RssFdw – 读取 RSS/Atom 订阅
CREATE SERVER csv_srv FOREIGN DATA WRAPPER multicorn
  OPTIONS (wrapper 'multicorn.csvfdw.CsvFdw');

CREATE FOREIGN TABLE csvtest (
  col1 text,
  col2 text
)
SERVER csv_srv
OPTIONS (
  filename '/tmp/data.csv',
  skip_header '1',
  delimiter ','
);

版本说明

Multicorn 3.2 增加基本 OFFSET/LIMIT pushdown 和 LDAP paging support,并修复 LDAP right-parenthesis escaping。上游 3.1 增加 PostgreSQL 18 和 Python 3.13 支持,同时停止支持 PostgreSQL 14 之前的版本。

注意事项

由于 CPython 限制,Multicorn2 和 PL/Python 在 Python 3.12 上不能在同一个 PostgreSQL 数据库中共存。它们可以安装在同一系统上,但避免在同一个数据库里同时启用。


最后修改 2026-07-02: extension update 2026-07-02 (d4da20c)