Remote tables infrastructure.
Remote tables infrastructure.
От:
"Dawid Kuroczko" <qnex42@gmail.com>
Дата:
Hello.
One nice feature of PostgreSQL's CREATE FUNCTION/RULE/VIEW system
is ability to create objects which "may appear closer than they really are".
Hence, all the dbi-link-like modules.
The usual method is to create a view on a set returning function (if
reasonable),
create RULEs for managing INSERT/UPDATE/DELETE, and voila.
This approach has some limitations. First of all if you add WHERE restrictions
to such a view, your PostgreSQL will first slurp all the data from SRF, and then
do the hard work of limiting/ordering. To circumvent that you may want to
pass actual query string at the application level.
Methinks it would be good idea to discuss a more general approach, i.e.
don't "create" links using existing infrastructure but rather provide something
conceptually similar to CREATE AGGREGATE statements. In other words,
make a remote link a first class object inside PostgreSQL.
The first thing, a connection:
CREATE DBLINK name (
connection = "some connection string",
start_transaction = foo_start_transaction_func,
rollback = foo_rollback_func,
before_commit = foo_prepare_transaction_func,
after_commit = foo_commit_prepared_func,
...
);
A start_transaction_func should receive ISOLATION LEVEL of course.
A 2PC infrastructure could be used to ensure that both transactions
will either commit or rollback. I think the remote transaction should
start (initialize connection) on first access to the remote table (as not
to cause connection storms upon backend process startup). If it
fails to initialize it, then our transaction should rollback.
I think it may be reasonable to provide timeout on IDLE (not IDLE in
transaction) remote link. Of course "nontransactional" remote links
should be possible without inconveniences. And we would need
to take (and keep) connection string which will allow us to reuse
the library functions with different connections.
Normally one would prototype using PLperlU/PLpythonU PLs and
if one wants more robust interface -- one would made a .so library
providing more robust interface.
Then we should have ability to access remote tables. I think PostgreSQL
should not assume that it knows what's on the other side of the link, and
just blindly follow user's request for a remote table (and throw an error
if table does not exist). I'll elaborate further on access functions later on.
For instance accessing table bar on foo DBLINK would be possible through:
SELECT * FROM foo.public.bar WHERE baz = 123;
foo -- being connection name,
public -- being schema name,
bar -- being table name.
You see now where am I cruising to: this way we're using
..Re: Remote tables infrastructure.
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
"Dawid Kuroczko" writes: > Methinks it would be good idea to discuss a more general approach, > i.e. don't "create" links using existing infrastructure but rather > provide something conceptually similar to CREATE AGGREGATE statements. > In other words, make a remote link a first class object inside PostgreSQL. Please see the archives, particularly past discussions of the SQL-MED standard. We do not need to go inventing our own syntax for this. regards, tom lane
Re: Remote tables infrastructure.
От:
"Dawid Kuroczko" <qnex42@gmail.com>
Дата:
On 11/10/06, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Great! I was not aware of that part of SQL:2003. Anyway I managed
to find September 2003 draft of the standard (Wikipedia has a link)
and skimmed through it (I didn't find much discussion in the archives,
just one thread).
While skimming through the draft I made quick notes on the
commands they propose to provide the functionality. Here is
the short list (I've ommited ALTER and DROP statements,
and also DATALINK which I don't quite grok at this late hour).
First there is a command:
CREATE FOREIGN DATA WRAPPER wrapper LIBRARY ...;
which establishes machinery for accessing remote data.
I would think of it as CREATE LANGUAGE of remote links.
Then there is a command to create a server definition,
the "instance" of connection.
CREATE SEVER servername [ TYPE type ] [ VERSION ver ]
FOREIGN DATA WRAPPER wrapper;
And finally, when we have a SERVER, we can get some
foregin data:
CREATE FOREIGN TABLE tabname ( ...) SERVER servername;
Or do it in bulk:
IMPORT FOREIGN SCHEMA foreignschema
[ LIMIT TO (tab1,tab2,...) | EXCEPT (tab3,tab4,...) ]
FROM SERVER server INTO localschema;
And these, I am not sure how they should be used. User mapping
seems straightforward, but routine mapping seems cryptic to me.
CREATE ROUTINE MAPPING rmapname FOR routine
SERVER server;
CREATE USER MAPPING umapname FOR userident SERVER server;
Regards,
Dawid
PS: So, to create connection to "foreign" postgresql, one would need to:
CREATE FOREIGN DATA WRAPPER which probably should be backed
by a bunch of our plain old FUNCTIONs or a specialized dynamically
linked library.
Then CREATE SERVER giving it an URI to our database, say
psql://host:port/dbname (is there an "official" uri syntax for PostgreSQL?)
And then either CREATE FOREIGN TABLEs or simply IMPORT FOREIGN
SCHEMA somewhere.
This would be a "minimum" I guess.
"Dawid Kuroczko" <qnex42@gmail.com> writes:
> Methinks it would be good idea to discuss a more general approach,
> i.e. don't "create" links using existing infrastructure but rather
> provide something conceptually similar to CREATE AGGREGATE statements.
> In other words, make a remote link a first class object inside PostgreSQL.
Please see the archives, particularly past discussions of the SQL-MED
standard. We do not need to go inventing our own syntax for this.
Great! I was not aware of that part of SQL:2003. Anyway I managed
to find September 2003 draft of the standard (Wikipedia has a link)
and skimmed through it (I didn't find much discussion in the archives,
just one thread).
While skimming through the draft I made quick notes on the
commands they propose to provide the functionality. Here is
the short list (I've ommited ALTER and DROP statements,
and also DATALINK which I don't quite grok at this late hour).
First there is a command:
CREATE FOREIGN DATA WRAPPER wrapper LIBRARY ...;
which establishes machinery for accessing remote data.
I would think of it as CREATE LANGUAGE of remote links.
Then there is a command to create a server definition,
the "instance" of connection.
CREATE SEVER servername [ TYPE type ] [ VERSION ver ]
FOREIGN DATA WRAPPER wrapper;
And finally, when we have a SERVER, we can get some
foregin data:
CREATE FOREIGN TABLE tabname ( ...) SERVER servername;
Or do it in bulk:
IMPORT FOREIGN SCHEMA foreignschema
[ LIMIT TO (tab1,tab2,...) | EXCEPT (tab3,tab4,...) ]
FROM SERVER server INTO localschema;
And these, I am not sure how they should be used. User mapping
seems straightforward, but routine mapping seems cryptic to me.
CREATE ROUTINE MAPPING rmapname FOR routine
SERVER server;
CREATE USER MAPPING umapname FOR userident SERVER server;
Regards,
Dawid
PS: So, to create connection to "foreign" postgresql, one would need to:
CREATE FOREIGN DATA WRAPPER which probably should be backed
by a bunch of our plain old FUNCTIONs or a specialized dynamically
linked library.
Then CREATE SERVER giving it an URI to our database, say
psql://host:port/dbname (is there an "official" uri syntax for PostgreSQL?)
And then either CREATE FOREIGN TABLEs or simply IMPORT FOREIGN
SCHEMA somewhere.
This would be a "minimum" I guess.