multicorn
用Python编写自定义的外部数据源包装器
仓库
pgsql-io/multicorn2
https://github.com/pgsql-io/multicorn2
源码
multicorn2-3.2.tar.gz
multicorn2-3.2.tar.gz
概览
| 扩展包名 | 版本 | 分类 | 许可证 | 语言 |
|---|---|---|---|---|
multicorn | 3.2 | FDW | PostgreSQL | C |
| ID | 扩展名 | Bin | Lib | Load | Create | Trust | Reloc | 模式 |
|---|---|---|---|---|---|---|---|---|
| 8510 | multicorn | 否 | 是 | 否 | 是 | 否 | 否 | - |
| 相关扩展 | wrappers odbc_fdw jdbc_fdw pgspider_ext mysql_fdw db2_fdw mongo_fdw redis_fdw |
|---|
版本
| 类型 | 仓库 | 版本 | PG 大版本 | 包名 | 依赖 |
|---|---|---|---|---|---|
| EXT | PGDG | 3.2 | 1817161514 | multicorn | - |
| RPM | PGDG | 3.2 | 1817161514 | multicorn2_$v | - |
| DEB | PIGSTY | 3.2 | 1817161514 | postgresql-$v-multicorn | python3-multicorn |
安装
您可以直接安装 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;
用法
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 数据库中共存。它们可以安装在同一系统上,但避免在同一个数据库里同时启用。