multicorn
multicorn : Fetch foreign data in Python in your PostgreSQL server.
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 8510 | multicorn
|
multicorn
|
3.2 |
FDW
|
PostgreSQL
|
C
|
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
--s-d--
|
No
|
Yes
|
No
|
Yes
|
no
|
no
|
| Relationships | |
|---|---|
| See Also | wrappers
odbc_fdw
jdbc_fdw
pgspider_ext
mysql_fdw
db2_fdw
mongo_fdw
redis_fdw
|
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PGDG
|
3.2 |
18
17
16
15
14
|
multicorn |
- |
| RPM | PGDG
|
3.2 |
18
17
16
15
14
|
multicorn2_$v |
- |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
el8.x86_64
|
PGDG 3.2
|
PGDG 3.2
|
PGDG 3.2
|
PGDG 3.2
|
PGDG 3.2
|
el8.aarch64
|
PGDG 3.2
|
PGDG 3.2
|
PGDG 3.2
|
PGDG 3.2
|
PGDG 3.2
|
el9.x86_64
|
PGDG 3.2
|
PGDG 3.2
|
PGDG 3.2
|
PGDG 3.2
|
PGDG 3.2
|
el9.aarch64
|
PGDG 3.2
|
PGDG 3.2
|
PGDG 3.2
|
PGDG 3.2
|
PGDG 3.2
|
el10.x86_64
|
PGDG 3.2
|
PGDG 3.2
|
PGDG 3.2
|
PGDG 3.2
|
PGDG 3.2
|
el10.aarch64
|
PGDG 3.2
|
PGDG 3.2
|
PGDG 3.2
|
PGDG 3.2
|
PGDG 3.2
|
d12.x86_64
|
MISS
|
MISS
|
MISS
|
MISS
|
MISS
|
d12.aarch64
|
MISS
|
MISS
|
MISS
|
MISS
|
MISS
|
d13.x86_64
|
MISS
|
MISS
|
MISS
|
MISS
|
MISS
|
d13.aarch64
|
MISS
|
MISS
|
MISS
|
MISS
|
MISS
|
u22.x86_64
|
MISS
|
MISS
|
MISS
|
MISS
|
MISS
|
u22.aarch64
|
MISS
|
MISS
|
MISS
|
MISS
|
MISS
|
u24.x86_64
|
MISS
|
MISS
|
MISS
|
MISS
|
MISS
|
u24.aarch64
|
MISS
|
MISS
|
MISS
|
MISS
|
MISS
|
Source
Install
Make sure PGDG repo available:
pig repo add pgdg -u # add pgdg repo and update cacheInstall this extension with pig:
pig install multicorn; # install via package name, for the active PG version
pig install multicorn -v 18; # install for PG 18
pig install multicorn -v 17; # install for PG 17
pig install multicorn -v 16; # install for PG 16
pig install multicorn -v 15; # install for PG 15
pig install multicorn -v 14; # install for PG 14Create this extension with:
CREATE EXTENSION multicorn;Usage
multicorn: Fetch foreign data in Python in your PostgreSQL server
Multicorn2 allows you to write Foreign Data Wrappers in Python. You implement a Python class that inherits from multicorn.ForeignDataWrapper, and Multicorn handles bridging it to PostgreSQL’s FDW interface.
Define a Python FDW Class
Create a Python module (e.g., myfdw.py) accessible to the PostgreSQL process:
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):
"""Yield rows as dictionaries. quals contains WHERE pushdown info."""
yield {"id": 1, "name": "example"}
def insert(self, new_values):
"""Handle INSERT operations."""
pass
def update(self, old_values, new_values):
"""Handle UPDATE operations."""
pass
def delete(self, old_values):
"""Handle DELETE operations."""
passCreate Server and Foreign Table
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;The wrapper option specifies the fully qualified Python class name. Any additional options are passed to the class constructor’s options parameter.
Built-in FDW Examples
Multicorn ships with several example FDW implementations that can be used directly or as reference:
- CsvFdw – read CSV files
- ProcessFdw – execute system commands and parse output
- GCalFdw – access Google Calendar
- ImapFdw – query IMAP mailboxes
- RssFdw – read RSS/Atom feeds
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 ','
);