Hello, The attached patch is just a proof-of-concept of writable foreign table feature; thus, I don't expect it getting merged at the upcoming commit fest. The purpose of this patch is to find out the best way to support "write stuff" in FDW.
Basic idea of this patch is to utilize "ctid" field to track an identifier of a particular foreign-row; to be updated or deleted. It shall be moved to the modify-stage from the scan-stage as regular table doing. Then, newly added methods being invoked at ExecUpdate or ExecDelete with the "pseudo ctid", so FDW shall be able to modify the target foreign-row. It is a responsibility of FDW extension (and DBA) to ensure each foreign-row has a unique identifier that has 48-bits width integer data type in maximum. In case of pgsql_fdw, "ctid" of remote table can perform as "pseudo ctid", as is. For other RDBMS, DBA will need to indicate which column should perform. INSERT is simple enough; all we need to do it to carry every field of new tuple into the remote side. This patch adds five new methods of FdwRoutine structure. The BeginForeignModify and EndForeignModify give a chance to initialize / destruct a private state that can be allocated on ResultRelInfo. As literal, ExecForeignInsert, ExecForeignDelete and ExecForeignUpdate are invoked for each new tuple, instead of heap_*() for regular tables. If NULL was set on them, it means this FDW does not support these operations. I intend FDW to set up a prepared statement that modifies a particular remote-row being identified with pseudo-ctid, at the BeginForeignModify(). Then, ExecForeign*() kicks the prepared statement with given pseudo-ctid. The patched portion at contrib/file_fdw.c does not make sense actually. It just prints messages for each invocation. It is just a proof-of-concept to show possibility of implementation based on real RDBMS. In case when file_fdw performs behalf on "ftbl". -------------------------------- postgres=# SELECT ctid, * FROM ftbl; ctid | a | b --------+-----+----- (0,1) | 101 | aaa (0,2) | 102 | bbb (0,3) | 103 | ccc (0,4) | 104 | ddd (0,5) | 105 | eee (0,6) | 106 | fff (0,7) | 107 | ggg (0,8) | 108 | hhh (0,9) | 109 | iii (0,10) | 110 | jjj (10 rows) ==> ctid is used to carray identifier of row; line number in this example. postgres=# UPDATE ftbl SET a = a + 1 WHERE a > 107; INFO: ftbl is the target relation of UPDATE INFO: fdw_file: BeginForeignModify method INFO: fdw_file: UPDATE (lineno = 8) INFO: fdw_file: UPDATE (lineno = 9) INFO: fdw_file: UPDATE (lineno = 10) INFO: fdw_file: EndForeignModify method UPDATE 3 postgres=# DELETE FROM ftbl WHERE a BETWEEN 103 AND 106; INFO: ftbl is the target relation of DELETE INFO: fdw_file: BeginForeignModify method INFO: fdw_file: DELETE (lineno = 3) INFO: fdw_file: DELETE (lineno = 4) INFO: fdw_file: DELETE (lineno = 5) INFO: fdw_file: DELETE (lineno = 6) INFO: fdw_file: EndForeignModify method DELETE 4 -------------------------------- This patch does not care about transaction control anyway. According to the discussion in developer meeting at Ottawa, I didn't include such a complex stuff in the first step. (Probably, we can implement using XactCallback...) Thanks, -- KaiGai Kohei <kai...@kaigai.gr.jp>
pgsql-v9.3-writable-fdw-poc.v1.patch
Description: Binary data
-- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers