6 <indexterm zone=
"dblink">
7 <primary>dblink
</primary>
11 <filename>dblink<
/> is a module which supports connections to
12 other
<productname>PostgreSQL<
/> databases from within a database
16 <refentry id=
"CONTRIB-DBLINK-CONNECT">
18 <refentrytitle>dblink_connect
</refentrytitle>
22 <refname>dblink_connect
</refname>
23 <refpurpose>opens a persistent connection to a remote database
</refpurpose>
28 dblink_connect(text connstr) returns text
29 dblink_connect(text connname, text connstr) returns text
34 <title>Description
</title>
37 <function>dblink_connect()<
/> establishes a connection to a remote
38 <productname>PostgreSQL<
/> database. The server and database to
39 be contacted are identified through a standard
<application>libpq<
/>
40 connection string. Optionally, a name can be assigned to the
41 connection. Multiple named connections can be open at once, but
42 only one unnamed connection is permitted at a time. The connection
43 will persist until closed or until the database session is ended.
47 The connection string may also be the name of an existing foreign
48 server. It is recommended to use
49 the
<function>postgresql_fdw_validator
</function> when defining
50 the corresponding foreign-data wrapper. See the example below, as
51 well as the following:
52 <simplelist type=
"inline">
53 <member><xref linkend=
"sql-createforeigndatawrapper" endterm=
"sql-createforeigndatawrapper-title"></member>
54 <member><xref linkend=
"sql-createserver" endterm=
"sql-createserver-title"></member>
55 <member><xref linkend=
"sql-createusermapping" endterm=
"sql-createusermapping-title"></member>
62 <title>Arguments
</title>
66 <term><parameter>conname
</parameter></term>
69 The name to use for this connection; if omitted, an unnamed
70 connection is opened, replacing any existing unnamed connection.
76 <term><parameter>connstr
</parameter></term>
79 <application>libpq<
/>-style connection info string, for example
80 <literal>hostaddr=
127.0.0.1 port=
5432 dbname=mydb user=postgres
82 For details see
<function>PQconnectdb<
/> in
83 <xref linkend=
"libpq-connect">.
91 <title>Return Value
</title>
94 Returns status, which is always
<literal>OK<
/> (since any error
95 causes the function to throw an error instead of returning).
103 Only superusers may use
<function>dblink_connect<
/> to create
104 non-password-authenticated connections. If non-superusers need this
105 capability, use
<function>dblink_connect_u<
/> instead.
109 It is unwise to choose connection names that contain equal signs,
110 as this opens a risk of confusion with connection info strings
111 in other
<filename>dblink<
/> functions.
116 <title>Example
</title>
119 select dblink_connect('dbname=postgres');
125 select dblink_connect('myconn', 'dbname=postgres');
131 -- FOREIGN DATA WRAPPER functionality
132 -- Note: local connection must require password authentication for this to work properly
133 -- Otherwise, you will receive the following error from dblink_connect():
134 -- ----------------------------------------------------------------------
135 -- ERROR: password is required
136 -- DETAIL: Non-superuser cannot connect if the server does not request a password.
137 -- HINT: Target server's authentication method must be changed.
138 CREATE USER dblink_regression_test WITH PASSWORD 'secret';
139 CREATE FOREIGN DATA WRAPPER postgresql VALIDATOR postgresql_fdw_validator;
140 CREATE SERVER fdtest FOREIGN DATA WRAPPER postgresql OPTIONS (hostaddr '
127.0.0.1', dbname 'contrib_regression');
142 CREATE USER MAPPING FOR dblink_regression_test SERVER fdtest OPTIONS (user 'dblink_regression_test', password 'secret');
143 GRANT USAGE ON FOREIGN SERVER fdtest TO dblink_regression_test;
144 GRANT SELECT ON TABLE foo TO dblink_regression_test;
146 \set ORIGINAL_USER :USER
147 \c - dblink_regression_test
148 SELECT dblink_connect('myconn', 'fdtest');
154 SELECT * FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[]);
156 ----+---+---------------
167 10 | k | {a10,b10,c10}
171 REVOKE USAGE ON FOREIGN SERVER fdtest FROM dblink_regression_test;
172 REVOKE SELECT ON TABLE foo FROM dblink_regression_test;
173 DROP USER MAPPING FOR dblink_regression_test SERVER fdtest;
174 DROP USER dblink_regression_test;
176 DROP FOREIGN DATA WRAPPER postgresql;
181 <refentry id=
"CONTRIB-DBLINK-CONNECT-U">
183 <refentrytitle>dblink_connect_u
</refentrytitle>
187 <refname>dblink_connect_u
</refname>
188 <refpurpose>opens a persistent connection to a remote database, insecurely
</refpurpose>
193 dblink_connect_u(text connstr) returns text
194 dblink_connect_u(text connname, text connstr) returns text
199 <title>Description
</title>
202 <function>dblink_connect_u()<
/> is identical to
203 <function>dblink_connect()<
/>, except that it will allow non-superusers
204 to connect using any authentication method.
208 If the remote server selects an authentication method that does not
209 involve a password, then impersonation and subsequent escalation of
210 privileges can occur, because the session will appear to have
211 originated from the user as which the local
<productname>PostgreSQL<
/>
212 server runs. Also, even if the remote server does demand a password,
213 it is possible for the password to be supplied from the server
214 environment, such as a
<filename>~/.pgpass<
/> file belonging to the
215 server's user. This opens not only a risk of impersonation, but the
216 possibility of exposing a password to an untrustworthy remote server.
217 Therefore,
<function>dblink_connect_u()<
/> is initially
218 installed with all privileges revoked from
<literal>PUBLIC<
/>,
219 making it un-callable except by superusers. In some situations
220 it may be appropriate to grant
<literal>EXECUTE<
/> permission for
221 <function>dblink_connect_u()<
/> to specific users who are considered
222 trustworthy, but this should be done with care. It is also recommended
223 that any
<filename>~/.pgpass<
/> file belonging to the server's user
224 <emphasis>not<
/> contain any records specifying a wildcard host name.
228 For further details see
<function>dblink_connect()<
/>.
233 <refentry id=
"CONTRIB-DBLINK-DISCONNECT">
235 <refentrytitle>dblink_disconnect
</refentrytitle>
239 <refname>dblink_disconnect
</refname>
240 <refpurpose>closes a persistent connection to a remote database
</refpurpose>
245 dblink_disconnect() returns text
246 dblink_disconnect(text connname) returns text
251 <title>Description
</title>
254 <function>dblink_disconnect()<
/> closes a connection previously opened
255 by
<function>dblink_connect()<
/>. The form with no arguments closes
256 an unnamed connection.
261 <title>Arguments
</title>
265 <term><parameter>conname
</parameter></term>
268 The name of a named connection to be closed.
276 <title>Return Value
</title>
279 Returns status, which is always
<literal>OK<
/> (since any error
280 causes the function to throw an error instead of returning).
285 <title>Example
</title>
288 test=# select dblink_disconnect();
294 select dblink_disconnect('myconn');
303 <refentry id=
"CONTRIB-DBLINK">
305 <refentrytitle>dblink
</refentrytitle>
309 <refname>dblink
</refname>
310 <refpurpose>executes a query in a remote database
</refpurpose>
315 dblink(text connname, text sql [, bool fail_on_error]) returns setof record
316 dblink(text connstr, text sql [, bool fail_on_error]) returns setof record
317 dblink(text sql [, bool fail_on_error]) returns setof record
322 <title>Description
</title>
325 <function>dblink<
/> executes a query (usually a
<command>SELECT<
/>,
326 but it can be any SQL statement that returns rows) in a remote database.
330 When two
<type>text<
/> arguments are given, the first one is first
331 looked up as a persistent connection's name; if found, the command
332 is executed on that connection. If not found, the first argument
333 is treated as a connection info string as for
<function>dblink_connect<
/>,
334 and the indicated connection is made just for the duration of this command.
339 <title>Arguments
</title>
343 <term><parameter>conname
</parameter></term>
346 Name of the connection to use; omit this parameter to use the
353 <term><parameter>connstr
</parameter></term>
356 A connection info string, as previously described for
357 <function>dblink_connect<
/>.
363 <term><parameter>sql
</parameter></term>
366 The SQL query that you wish to execute in the remote database,
367 for example
<literal>select * from foo<
/>.
373 <term><parameter>fail_on_error
</parameter></term>
376 If true (the default when omitted) then an error thrown on the
377 remote side of the connection causes an error to also be thrown
378 locally. If false, the remote error is locally reported as a NOTICE,
379 and the function returns no rows.
387 <title>Return Value
</title>
390 The function returns the row(s) produced by the query. Since
391 <function>dblink<
/> can be used with any query, it is declared
392 to return
<type>record<
/>, rather than specifying any particular
393 set of columns. This means that you must specify the expected
394 set of columns in the calling query
— otherwise
395 <productname>PostgreSQL<
/> would not know what to expect.
400 FROM dblink('dbname=mydb', 'select proname, prosrc from pg_proc')
401 AS t1(proname name, prosrc text)
402 WHERE proname LIKE 'bytea%';
405 The
<quote>alias<
/> part of the
<literal>FROM<
/> clause must
406 specify the column names and types that the function will return.
407 (Specifying column names in an alias is actually standard SQL
408 syntax, but specifying column types is a
<productname>PostgreSQL<
/>
409 extension.) This allows the system to understand what
410 <literal>*<
/> should expand to, and what
<structname>proname<
/>
411 in the
<literal>WHERE<
/> clause refers to, in advance of trying
412 to execute the function. At runtime, an error will be thrown
413 if the actual query result from the remote database does not
414 have the same number of columns shown in the
<literal>FROM<
/> clause.
415 The column names need not match, however, and
<function>dblink<
/>
416 does not insist on exact type matches either. It will succeed
417 so long as the returned data strings are valid input for the
418 column type declared in the
<literal>FROM<
/> clause.
426 <function>dblink<
/> fetches the entire remote query result before
427 returning any of it to the local system. If the query is expected
428 to return a large number of rows, it's better to open it as a cursor
429 with
<function>dblink_open<
/> and then fetch a manageable number
434 A convenient way to use
<function>dblink<
/> with predetermined
435 queries is to create a view.
436 This allows the column type information to be buried in the view,
437 instead of having to spell it out in every query. For example,
440 create view myremote_pg_proc as
442 from dblink('dbname=postgres', 'select proname, prosrc from pg_proc')
443 as t1(proname name, prosrc text);
445 select * from myremote_pg_proc where proname like 'bytea%';
451 <title>Example
</title>
454 select * from dblink('dbname=postgres', 'select proname, prosrc from pg_proc')
455 as t1(proname name, prosrc text) where proname like 'bytea%';
457 ------------+------------
466 bytealike | bytealike
467 byteanlike | byteanlike
472 select dblink_connect('dbname=postgres');
478 select * from dblink('select proname, prosrc from pg_proc')
479 as t1(proname name, prosrc text) where proname like 'bytea%';
481 ------------+------------
490 bytealike | bytealike
491 byteanlike | byteanlike
496 select dblink_connect('myconn', 'dbname=regression');
502 select * from dblink('myconn', 'select proname, prosrc from pg_proc')
503 as t1(proname name, prosrc text) where proname like 'bytea%';
505 ------------+------------
506 bytearecv | bytearecv
507 byteasend | byteasend
513 bytealike | bytealike
514 byteanlike | byteanlike
525 <refentry id=
"CONTRIB-DBLINK-EXEC">
527 <refentrytitle>dblink_exec
</refentrytitle>
531 <refname>dblink_exec
</refname>
532 <refpurpose>executes a command in a remote database
</refpurpose>
537 dblink_exec(text connname, text sql [, bool fail_on_error]) returns text
538 dblink_exec(text connstr, text sql [, bool fail_on_error]) returns text
539 dblink_exec(text sql [, bool fail_on_error]) returns text
544 <title>Description
</title>
547 <function>dblink_exec<
/> executes a command (that is, any SQL statement
548 that doesn't return rows) in a remote database.
552 When two
<type>text<
/> arguments are given, the first one is first
553 looked up as a persistent connection's name; if found, the command
554 is executed on that connection. If not found, the first argument
555 is treated as a connection info string as for
<function>dblink_connect<
/>,
556 and the indicated connection is made just for the duration of this command.
561 <title>Arguments
</title>
565 <term><parameter>conname
</parameter></term>
568 Name of the connection to use; omit this parameter to use the
575 <term><parameter>connstr
</parameter></term>
578 A connection info string, as previously described for
579 <function>dblink_connect<
/>.
585 <term><parameter>sql
</parameter></term>
588 The SQL command that you wish to execute in the remote database,
590 <literal>insert into foo values(
0,'a','{
"a0",
"b0",
"c0"}')<
/>.
596 <term><parameter>fail_on_error
</parameter></term>
599 If true (the default when omitted) then an error thrown on the
600 remote side of the connection causes an error to also be thrown
601 locally. If false, the remote error is locally reported as a NOTICE,
602 and the function's return value is set to
<literal>ERROR<
/>.
610 <title>Return Value
</title>
613 Returns status, either the command's status string or
<literal>ERROR<
/>.
618 <title>Example
</title>
621 select dblink_connect('dbname=dblink_test_slave');
627 select dblink_exec('insert into foo values(
21,''z'',''{
"a0",
"b0",
"c0"}'');');
633 select dblink_connect('myconn', 'dbname=regression');
639 select dblink_exec('myconn', 'insert into foo values(
21,''z'',''{
"a0",
"b0",
"c0"}'');');
645 select dblink_exec('myconn', 'insert into pg_class values (''foo'')',false);
647 DETAIL: ERROR: null value in column
"relnamespace" violates not-null constraint
657 <refentry id=
"CONTRIB-DBLINK-OPEN">
659 <refentrytitle>dblink_open
</refentrytitle>
663 <refname>dblink_open
</refname>
664 <refpurpose>opens a cursor in a remote database
</refpurpose>
669 dblink_open(text cursorname, text sql [, bool fail_on_error]) returns text
670 dblink_open(text connname, text cursorname, text sql [, bool fail_on_error]) returns text
675 <title>Description
</title>
678 <function>dblink_open()<
/> opens a cursor in a remote database.
679 The cursor can subsequently be manipulated with
680 <function>dblink_fetch()<
/> and
<function>dblink_close()<
/>.
685 <title>Arguments
</title>
689 <term><parameter>conname
</parameter></term>
692 Name of the connection to use; omit this parameter to use the
699 <term><parameter>cursorname
</parameter></term>
702 The name to assign to this cursor.
708 <term><parameter>sql
</parameter></term>
711 The
<command>SELECT<
/> statement that you wish to execute in the remote
712 database, for example
<literal>select * from pg_class<
/>.
718 <term><parameter>fail_on_error
</parameter></term>
721 If true (the default when omitted) then an error thrown on the
722 remote side of the connection causes an error to also be thrown
723 locally. If false, the remote error is locally reported as a NOTICE,
724 and the function's return value is set to
<literal>ERROR<
/>.
732 <title>Return Value
</title>
735 Returns status, either
<literal>OK<
/> or
<literal>ERROR<
/>.
743 Since a cursor can only persist within a transaction,
744 <function>dblink_open<
/> starts an explicit transaction block
745 (
<command>BEGIN<
/>) on the remote side, if the remote side was
746 not already within a transaction. This transaction will be
747 closed again when the matching
<function>dblink_close<
/> is
748 executed. Note that if
749 you use
<function>dblink_exec<
/> to change data between
750 <function>dblink_open<
/> and
<function>dblink_close<
/>,
751 and then an error occurs or you use
<function>dblink_disconnect<
/> before
752 <function>dblink_close<
/>, your change
<emphasis>will be
753 lost<
/> because the transaction will be aborted.
758 <title>Example
</title>
761 test=# select dblink_connect('dbname=postgres');
767 test=# select dblink_open('foo', 'select proname, prosrc from pg_proc');
776 <refentry id=
"CONTRIB-DBLINK-FETCH">
778 <refentrytitle>dblink_fetch
</refentrytitle>
782 <refname>dblink_fetch
</refname>
783 <refpurpose>returns rows from an open cursor in a remote database
</refpurpose>
788 dblink_fetch(text cursorname, int howmany [, bool fail_on_error]) returns setof record
789 dblink_fetch(text connname, text cursorname, int howmany [, bool fail_on_error]) returns setof record
794 <title>Description
</title>
797 <function>dblink_fetch<
/> fetches rows from a cursor previously
798 established by
<function>dblink_open<
/>.
803 <title>Arguments
</title>
807 <term><parameter>conname
</parameter></term>
810 Name of the connection to use; omit this parameter to use the
817 <term><parameter>cursorname
</parameter></term>
820 The name of the cursor to fetch from.
826 <term><parameter>howmany
</parameter></term>
829 The maximum number of rows to retrieve. The next
<parameter>howmany<
/>
830 rows are fetched, starting at the current cursor position, moving
831 forward. Once the cursor has reached its end, no more rows are produced.
837 <term><parameter>fail_on_error
</parameter></term>
840 If true (the default when omitted) then an error thrown on the
841 remote side of the connection causes an error to also be thrown
842 locally. If false, the remote error is locally reported as a NOTICE,
843 and the function returns no rows.
851 <title>Return Value
</title>
854 The function returns the row(s) fetched from the cursor. To use this
855 function, you will need to specify the expected set of columns,
856 as previously discussed for
<function>dblink<
/>.
864 On a mismatch between the number of return columns specified in the
865 <literal>FROM<
/> clause, and the actual number of columns returned by the
866 remote cursor, an error will be thrown. In this event, the remote cursor
867 is still advanced by as many rows as it would have been if the error had
868 not occurred. The same is true for any other error occurring in the local
869 query after the remote
<command>FETCH<
/> has been done.
874 <title>Example
</title>
877 test=# select dblink_connect('dbname=postgres');
883 test=# select dblink_open('foo', 'select proname, prosrc from pg_proc where proname like ''bytea%''');
889 test=# select * from dblink_fetch('foo',
5) as (funcname name, source text);
891 ----------+----------
899 test=# select * from dblink_fetch('foo',
5) as (funcname name, source text);
901 -----------+-----------
904 bytealike | bytealike
909 test=# select * from dblink_fetch('foo',
5) as (funcname name, source text);
911 ------------+------------
912 byteanlike | byteanlike
916 test=# select * from dblink_fetch('foo',
5) as (funcname name, source text);
924 <refentry id=
"CONTRIB-DBLINK-CLOSE">
926 <refentrytitle>dblink_close
</refentrytitle>
930 <refname>dblink_close
</refname>
931 <refpurpose>closes a cursor in a remote database
</refpurpose>
936 dblink_close(text cursorname [, bool fail_on_error]) returns text
937 dblink_close(text connname, text cursorname [, bool fail_on_error]) returns text
942 <title>Description
</title>
945 <function>dblink_close<
/> closes a cursor previously opened with
946 <function>dblink_open<
/>.
951 <title>Arguments
</title>
955 <term><parameter>conname
</parameter></term>
958 Name of the connection to use; omit this parameter to use the
965 <term><parameter>cursorname
</parameter></term>
968 The name of the cursor to close.
974 <term><parameter>fail_on_error
</parameter></term>
977 If true (the default when omitted) then an error thrown on the
978 remote side of the connection causes an error to also be thrown
979 locally. If false, the remote error is locally reported as a NOTICE,
980 and the function's return value is set to
<literal>ERROR<
/>.
988 <title>Return Value
</title>
991 Returns status, either
<literal>OK<
/> or
<literal>ERROR<
/>.
999 If
<function>dblink_open<
/> started an explicit transaction block,
1000 and this is the last remaining open cursor in this connection,
1001 <function>dblink_close<
/> will issue the matching
<command>COMMIT<
/>.
1006 <title>Example
</title>
1009 test=# select dblink_connect('dbname=postgres');
1015 test=# select dblink_open('foo', 'select proname, prosrc from pg_proc');
1021 test=# select dblink_close('foo');
1030 <refentry id=
"CONTRIB-DBLINK-GET-CONNECTIONS">
1032 <refentrytitle>dblink_get_connections
</refentrytitle>
1036 <refname>dblink_get_connections
</refname>
1037 <refpurpose>returns the names of all open named dblink connections
</refpurpose>
1042 dblink_get_connections() returns text[]
1047 <title>Description
</title>
1050 <function>dblink_get_connections<
/> returns an array of the names
1051 of all open named
<filename>dblink<
/> connections.
1056 <title>Return Value
</title>
1058 <para>Returns a text array of connection names, or NULL if none.
</para>
1062 <title>Example
</title>
1065 SELECT dblink_get_connections();
1070 <refentry id=
"CONTRIB-DBLINK-ERROR-MESSAGE">
1072 <refentrytitle>dblink_error_message
</refentrytitle>
1076 <refname>dblink_error_message
</refname>
1077 <refpurpose>gets last error message on the named connection
</refpurpose>
1082 dblink_error_message(text connname) returns text
1087 <title>Description
</title>
1090 <function>dblink_error_message<
/> fetches the most recent remote
1091 error message for a given connection.
1096 <title>Arguments
</title>
1100 <term><parameter>conname
</parameter></term>
1103 Name of the connection to use.
1111 <title>Return Value
</title>
1114 Returns last error message, or an empty string if there has been
1115 no error in this connection.
1120 <title>Example
</title>
1123 SELECT dblink_error_message('dtest1');
1128 <refentry id=
"CONTRIB-DBLINK-SEND-QUERY">
1130 <refentrytitle>dblink_send_query
</refentrytitle>
1134 <refname>dblink_send_query
</refname>
1135 <refpurpose>sends an async query to a remote database
</refpurpose>
1140 dblink_send_query(text connname, text sql) returns int
1145 <title>Description
</title>
1148 <function>dblink_send_query<
/> sends a query to be executed
1149 asynchronously, that is, without immediately waiting for the result.
1150 There must not be an async query already in progress on the
1155 After successfully dispatching an async query, completion status
1156 can be checked with
<function>dblink_is_busy<
/>, and the results
1157 are ultimately collected with
<function>dblink_get_result<
/>.
1158 It is also possible to attempt to cancel an active async query
1159 using
<function>dblink_cancel_query<
/>.
1164 <title>Arguments
</title>
1168 <term><parameter>conname
</parameter></term>
1171 Name of the connection to use.
1177 <term><parameter>sql
</parameter></term>
1180 The SQL statement that you wish to execute in the remote database,
1181 for example
<literal>select * from pg_class<
/>.
1189 <title>Return Value
</title>
1192 Returns
1 if the query was successfully dispatched,
0 otherwise.
1197 <title>Example
</title>
1200 SELECT dblink_send_query('dtest1', 'SELECT * FROM foo WHERE f1
< 3');
1205 <refentry id=
"CONTRIB-DBLINK-IS-BUSY">
1207 <refentrytitle>dblink_is_busy
</refentrytitle>
1211 <refname>dblink_is_busy
</refname>
1212 <refpurpose>checks if connection is busy with an async query
</refpurpose>
1217 dblink_is_busy(text connname) returns int
1222 <title>Description
</title>
1225 <function>dblink_is_busy<
/> tests whether an async query is in progress.
1230 <title>Arguments
</title>
1234 <term><parameter>conname
</parameter></term>
1237 Name of the connection to check.
1245 <title>Return Value
</title>
1248 Returns
1 if connection is busy,
0 if it is not busy.
1249 If this function returns
0, it is guaranteed that
1250 <function>dblink_get_result<
/> will not block.
1255 <title>Example
</title>
1258 SELECT dblink_is_busy('dtest1');
1263 <refentry id=
"CONTRIB-DBLINK-GET-RESULT">
1265 <refentrytitle>dblink_get_result
</refentrytitle>
1269 <refname>dblink_get_result
</refname>
1270 <refpurpose>gets an async query result
</refpurpose>
1275 dblink_get_result(text connname [, bool fail_on_error]) returns setof record
1280 <title>Description
</title>
1283 <function>dblink_get_result<
/> collects the results of an
1284 asynchronous query previously sent with
<function>dblink_send_query<
/>.
1285 If the query is not already completed,
<function>dblink_get_result<
/>
1286 will wait until it is.
1291 <title>Arguments
</title>
1295 <term><parameter>conname
</parameter></term>
1298 Name of the connection to use.
1304 <term><parameter>fail_on_error
</parameter></term>
1307 If true (the default when omitted) then an error thrown on the
1308 remote side of the connection causes an error to also be thrown
1309 locally. If false, the remote error is locally reported as a NOTICE,
1310 and the function returns no rows.
1318 <title>Return Value
</title>
1321 For an async query (that is, a SQL statement returning rows),
1322 the function returns the row(s) produced by the query. To use this
1323 function, you will need to specify the expected set of columns,
1324 as previously discussed for
<function>dblink<
/>.
1328 For an async command (that is, a SQL statement not returning rows),
1329 the function returns a single row with a single text column containing
1330 the command's status string. It is still necessary to specify that
1331 the result will have a single text column in the calling
<literal>FROM<
/>
1337 <title>Notes
</title>
1340 This function
<emphasis>must<
/> be called if
1341 <function>dblink_send_query<
/> returned
1.
1342 It must be called once for each query
1343 sent, and one additional time to obtain an empty set result,
1344 before the connection can be used again.
1349 <title>Example
</title>
1352 contrib_regression=# SELECT dblink_connect('dtest1', 'dbname=contrib_regression');
1358 contrib_regression=# SELECT * from
1359 contrib_regression-# dblink_send_query('dtest1', 'select * from foo where f1
< 3') as t1;
1365 contrib_regression=# SELECT * from dblink_get_result('dtest1') as t1(f1 int, f2 text, f3 text[]);
1367 ----+----+------------
1373 contrib_regression=# SELECT * from dblink_get_result('dtest1') as t1(f1 int, f2 text, f3 text[]);
1378 contrib_regression=# SELECT * from
1379 dblink_send_query('dtest1', 'select * from foo where f1
< 3; select * from foo where f1
> 6') as t1;
1385 contrib_regression=# SELECT * from dblink_get_result('dtest1') as t1(f1 int, f2 text, f3 text[]);
1387 ----+----+------------
1393 contrib_regression=# SELECT * from dblink_get_result('dtest1') as t1(f1 int, f2 text, f3 text[]);
1395 ----+----+---------------
1399 10 | k | {a10,b10,c10}
1402 contrib_regression=# SELECT * from dblink_get_result('dtest1') as t1(f1 int, f2 text, f3 text[]);
1410 <refentry id=
"CONTRIB-DBLINK-CANCEL-QUERY">
1412 <refentrytitle>dblink_cancel_query
</refentrytitle>
1416 <refname>dblink_cancel_query
</refname>
1417 <refpurpose>cancels any active query on the named connection
</refpurpose>
1422 dblink_cancel_query(text connname) returns text
1427 <title>Description
</title>
1430 <function>dblink_cancel_query<
/> attempts to cancel any query that
1431 is in progress on the named connection. Note that this is not
1432 certain to succeed (since, for example, the remote query might
1433 already have finished). A cancel request simply improves the
1434 odds that the query will fail soon. You must still complete the
1435 normal query protocol, for example by calling
1436 <function>dblink_get_result<
/>.
1441 <title>Arguments
</title>
1445 <term><parameter>conname
</parameter></term>
1448 Name of the connection to use.
1456 <title>Return Value
</title>
1459 Returns
<literal>OK<
/> if the cancel request has been sent, or
1460 the text of an error message on failure.
1465 <title>Example
</title>
1468 SELECT dblink_cancel_query('dtest1');
1473 <refentry id=
"CONTRIB-DBLINK-GET-PKEY">
1475 <refentrytitle>dblink_get_pkey
</refentrytitle>
1479 <refname>dblink_get_pkey
</refname>
1480 <refpurpose>returns the positions and field names of a relation's
1487 dblink_get_pkey(text relname) returns setof dblink_pkey_results
1492 <title>Description
</title>
1495 <function>dblink_get_pkey<
/> provides information about the primary
1496 key of a relation in the local database. This is sometimes useful
1497 in generating queries to be sent to remote databases.
1502 <title>Arguments
</title>
1506 <term><parameter>relname
</parameter></term>
1509 Name of a local relation, for example
<literal>foo<
/> or
1510 <literal>myschema.mytab<
/>. Include double quotes if the
1511 name is mixed-case or contains special characters, for
1512 example
<literal>"FooBar"<
/>; without quotes, the string
1513 will be folded to lower case.
1521 <title>Return Value
</title>
1524 Returns one row for each primary key field, or no rows if the relation
1525 has no primary key. The result rowtype is defined as
1528 CREATE TYPE dblink_pkey_results AS (position int, colname text);
1534 <title>Example
</title>
1537 test=# create table foobar(f1 int, f2 int, f3 int,
1538 test(# primary key(f1,f2,f3));
1540 test=# select * from dblink_get_pkey('foobar');
1542 ----------+---------
1551 <refentry id=
"CONTRIB-DBLINK-BUILD-SQL-INSERT">
1553 <refentrytitle>dblink_build_sql_insert
</refentrytitle>
1557 <refname>dblink_build_sql_insert
</refname>
1559 builds an INSERT statement using a local tuple, replacing the
1560 primary key field values with alternative supplied values
1566 dblink_build_sql_insert(text relname,
1567 int2vector primary_key_attnums,
1568 int2 num_primary_key_atts,
1569 text[] src_pk_att_vals_array,
1570 text[] tgt_pk_att_vals_array) returns text
1575 <title>Description
</title>
1578 <function>dblink_build_sql_insert<
/> can be useful in doing selective
1579 replication of a local table to a remote database. It selects a row
1580 from the local table based on primary key, and then builds a SQL
1581 <command>INSERT<
/> command that will duplicate that row, but with
1582 the primary key values replaced by the values in the last argument.
1583 (To make an exact copy of the row, just specify the same values for
1584 the last two arguments.)
1589 <title>Arguments
</title>
1593 <term><parameter>relname
</parameter></term>
1596 Name of a local relation, for example
<literal>foo<
/> or
1597 <literal>myschema.mytab<
/>. Include double quotes if the
1598 name is mixed-case or contains special characters, for
1599 example
<literal>"FooBar"<
/>; without quotes, the string
1600 will be folded to lower case.
1606 <term><parameter>primary_key_attnums
</parameter></term>
1609 Attribute numbers (
1-based) of the primary key fields,
1610 for example
<literal>1 2<
/>.
1616 <term><parameter>num_primary_key_atts
</parameter></term>
1619 The number of primary key fields.
1625 <term><parameter>src_pk_att_vals_array
</parameter></term>
1628 Values of the primary key fields to be used to look up the
1629 local tuple. Each field is represented in text form.
1630 An error is thrown if there is no local row with these
1637 <term><parameter>tgt_pk_att_vals_array
</parameter></term>
1640 Values of the primary key fields to be placed in the resulting
1641 <command>INSERT<
/> command. Each field is represented in text form.
1649 <title>Return Value
</title>
1651 <para>Returns the requested SQL statement as text.
</para>
1655 <title>Example
</title>
1658 test=# select dblink_build_sql_insert('foo', '
1 2',
2, '{
"1",
"a"}', '{
"1",
"b''a"}');
1659 dblink_build_sql_insert
1660 --------------------------------------------------
1661 INSERT INTO foo(f1,f2,f3) VALUES('
1','b''a','
1')
1667 <refentry id=
"CONTRIB-DBLINK-BUILD-SQL-DELETE">
1669 <refentrytitle>dblink_build_sql_delete
</refentrytitle>
1673 <refname>dblink_build_sql_delete
</refname>
1674 <refpurpose>builds a DELETE statement using supplied values for primary
1681 dblink_build_sql_delete(text relname,
1682 int2vector primary_key_attnums,
1683 int2 num_primary_key_atts,
1684 text[] tgt_pk_att_vals_array) returns text
1689 <title>Description
</title>
1692 <function>dblink_build_sql_delete<
/> can be useful in doing selective
1693 replication of a local table to a remote database. It builds a SQL
1694 <command>DELETE<
/> command that will delete the row with the given
1700 <title>Arguments
</title>
1704 <term><parameter>relname
</parameter></term>
1707 Name of a local relation, for example
<literal>foo<
/> or
1708 <literal>myschema.mytab<
/>. Include double quotes if the
1709 name is mixed-case or contains special characters, for
1710 example
<literal>"FooBar"<
/>; without quotes, the string
1711 will be folded to lower case.
1717 <term><parameter>primary_key_attnums
</parameter></term>
1720 Attribute numbers (
1-based) of the primary key fields,
1721 for example
<literal>1 2<
/>.
1727 <term><parameter>num_primary_key_atts
</parameter></term>
1730 The number of primary key fields.
1736 <term><parameter>tgt_pk_att_vals_array
</parameter></term>
1739 Values of the primary key fields to be used in the resulting
1740 <command>DELETE<
/> command. Each field is represented in text form.
1748 <title>Return Value
</title>
1750 <para>Returns the requested SQL statement as text.
</para>
1754 <title>Example
</title>
1757 test=# select dblink_build_sql_delete('
"MyFoo"', '
1 2',
2, '{
"1",
"b"}');
1758 dblink_build_sql_delete
1759 ---------------------------------------------
1760 DELETE FROM
"MyFoo" WHERE f1='
1' AND f2='b'
1766 <refentry id=
"CONTRIB-DBLINK-BUILD-SQL-UPDATE">
1768 <refentrytitle>dblink_build_sql_update
</refentrytitle>
1772 <refname>dblink_build_sql_update
</refname>
1773 <refpurpose>builds an UPDATE statement using a local tuple, replacing
1774 the primary key field values with alternative supplied values
1780 dblink_build_sql_update(text relname,
1781 int2vector primary_key_attnums,
1782 int2 num_primary_key_atts,
1783 text[] src_pk_att_vals_array,
1784 text[] tgt_pk_att_vals_array) returns text
1789 <title>Description
</title>
1792 <function>dblink_build_sql_update<
/> can be useful in doing selective
1793 replication of a local table to a remote database. It selects a row
1794 from the local table based on primary key, and then builds a SQL
1795 <command>UPDATE<
/> command that will duplicate that row, but with
1796 the primary key values replaced by the values in the last argument.
1797 (To make an exact copy of the row, just specify the same values for
1798 the last two arguments.) The
<command>UPDATE<
/> command always assigns
1799 all fields of the row
— the main difference between this and
1800 <function>dblink_build_sql_insert<
/> is that it's assumed that
1801 the target row already exists in the remote table.
1806 <title>Arguments
</title>
1810 <term><parameter>relname
</parameter></term>
1813 Name of a local relation, for example
<literal>foo<
/> or
1814 <literal>myschema.mytab<
/>. Include double quotes if the
1815 name is mixed-case or contains special characters, for
1816 example
<literal>"FooBar"<
/>; without quotes, the string
1817 will be folded to lower case.
1823 <term><parameter>primary_key_attnums
</parameter></term>
1826 Attribute numbers (
1-based) of the primary key fields,
1827 for example
<literal>1 2<
/>.
1833 <term><parameter>num_primary_key_atts
</parameter></term>
1836 The number of primary key fields.
1842 <term><parameter>src_pk_att_vals_array
</parameter></term>
1845 Values of the primary key fields to be used to look up the
1846 local tuple. Each field is represented in text form.
1847 An error is thrown if there is no local row with these
1854 <term><parameter>tgt_pk_att_vals_array
</parameter></term>
1857 Values of the primary key fields to be placed in the resulting
1858 <command>UPDATE<
/> command. Each field is represented in text form.
1866 <title>Return Value
</title>
1868 <para>Returns the requested SQL statement as text.
</para>
1872 <title>Example
</title>
1875 test=# select dblink_build_sql_update('foo', '
1 2',
2, '{
"1",
"a"}', '{
"1",
"b"}');
1876 dblink_build_sql_update
1877 -------------------------------------------------------------
1878 UPDATE foo SET f1='
1',f2='b',f3='
1' WHERE f1='
1' AND f2='b'