2024/04/22

PostgreSQL: Foreign data wrappers

In 2003, a new specification called SQL/MED ("SQL Management of External Data") was added to the SQL standard. It is a standardized way of handling access to remote objects from SQL databases. In 2011, PostgreSQL 9.1 was released with read-only support of this standard, and in 2013 write support was added with PostgreSQL 9.3.

在使用 FDW 的情況下,PostgreSQL 具有存取外部資料源的能力,並且可以作為一個 SQL query engine 使用。 也就是在 FDW 的處理範圍內,PostgreSQL 也有讀取 Hadoop/HBase big data store 的能力(看 FDW 所提供的能力而定)。

PostgreSQL FDW 的 Column-Oriented Wrappers 有二個選擇, 一個是 monetdb_fdw, 一個是 cstore_fdw

monetdb_fdw

使用 monetdb_fdw 來進行 PostgreSQL Foreign_data_wrappers 的測試。

如果不從 PostgreSQL source code 一起編譯,使用下列的方式:
make USE_PGXS=1
如果 MonetDB 的 MAPI lib 與 include 目錄不正確,需要修改 Makefile。

下面是 Makefile 修改的範例:
SHLIB_LINK = -L/usr/local/lib64 -lmapi
PG_CPPFLAGS = -I/usr/local/include/monetdb

再來是安裝:
sudo make USE_PGXS=1 install
需要修改 /var/lib/pgsql/data/postgresql.conf,加入下列的設定:
shared_preload_libraries = 'monetdb_fdw'

要注意的是,重新啟動 PostgreSQL 前要確定 monetdb_fdw 可以正確的尋找到 MonetDB MAPI library 的路徑位置, 否則要設定才行,不然 PostgreSQL 會無法正確啟動

下面是一個例子:
export LD_LIBRARY_PATH=/usr/local/lib64:$LD_LIBRARY_PATH
然後重新啟動 PostgreSQL。


下面是使用 psql 來進行是否可以使用的測試:
CREATE EXTENSION monetdb_fdw;
再來是建立 server object:
CREATE SERVER monetdb_server FOREIGN DATA WRAPPER monetdb_fdw;
然後建立與 MonetDB table 對應的 foreign table。假設 MonetDB 有一個 person 的 table, 我們可以對應過來以後,就可以對這個表格進行操作。

下面是一個例子:
CREATE FOREIGN TABLE person (id integer, name varchar(40)) SERVER monetdb_server
OPTIONS (host 'localhost', port '50000', user 'monetdb', passwd 'monetdb', dbname 'demo', table 'person');
然後就可以查詢 MonetDB person 表格:
SELECT * FROM person;

如果要刪除不用的 FDW 並且完整的移除,要先刪除全部的 Foreign table:
DROP FOREIGN TABLE person;
Then drop the monetdb server and extension:
DROP SERVER monetdb_server;
DROP EXTENSION monetdb_fdw;
Then remove monetdb_fdw from shared_preload_libraries in your postgresql.conf:
#shared_preload_libraries = 'monetdb_fdw'
Finally remove the monetdb_fdw files, into the monetdb_fdw development folder, execute:
sudo make USE_PGXS=1 uninstall

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。