3
.\" Author: The PostgreSQL Global Development Group
4
.\" Generator: DocBook XSL Stylesheets v1.75.1 <http://docbook.sf.net/>
6
.\" Manual: PostgreSQL 9.1beta1 Documentation
7
.\" Source: PostgreSQL 9.1beta1
10
.TH "DBLINK" "3" "2011-04-27" "PostgreSQL 9.1beta1" "PostgreSQL 9.1beta1 Documentation"
11
.\" -----------------------------------------------------------------
12
.\" * set default formatting
13
.\" -----------------------------------------------------------------
14
.\" disable hyphenation
16
.\" disable justification (adjust text to left margin only)
18
.\" -----------------------------------------------------------------
19
.\" * MAIN CONTENT STARTS HERE *
20
.\" -----------------------------------------------------------------
22
dblink \- executes a query in a remote database
26
dblink(text connname, text sql [, bool fail_on_error]) returns setof record
27
dblink(text connstr, text sql [, bool fail_on_error]) returns setof record
28
dblink(text sql [, bool fail_on_error]) returns setof record
33
executes a query (usually a
34
SELECT, but it can be any SQL statement that returns rows) in a remote database\&.
38
arguments are given, the first one is first looked up as a persistent connection\(aqs name; if found, the command is executed on that connection\&. If not found, the first argument is treated as a connection info string as for
39
\fBdblink_connect\fR, and the indicated connection is made just for the duration of this command\&.
44
Name of the connection to use; omit this parameter to use the unnamed connection\&.
49
A connection info string, as previously described for
50
\fBdblink_connect\fR\&.
55
The SQL query that you wish to execute in the remote database, for example
61
If true (the default when omitted) then an error thrown on the remote side of the connection causes an error to also be thrown locally\&. If false, the remote error is locally reported as a NOTICE, and the function returns no rows\&.
65
The function returns the row(s) produced by the query\&. Since
67
can be used with any query, it is declared to return
68
record, rather than specifying any particular set of columns\&. This means that you must specify the expected set of columns in the calling query \(em otherwise
70
would not know what to expect\&. Here is an example:
77
FROM dblink(\(aqdbname=mydb\(aq, \(aqselect proname, prosrc from pg_proc\(aq)
78
AS t1(proname name, prosrc text)
79
WHERE proname LIKE \(aqbytea%\(aq;
89
clause must specify the column names and types that the function will return\&. (Specifying column names in an alias is actually standard SQL syntax, but specifying column types is a
91
extension\&.) This allows the system to understand what
93
should expand to, and what
97
clause refers to, in advance of trying to execute the function\&. At run time, an error will be thrown if the actual query result from the remote database does not have the same number of columns shown in the
99
clause\&. The column names need not match, however, and
101
does not insist on exact type matches either\&. It will succeed so long as the returned data strings are valid input for the column type declared in the
107
fetches the entire remote query result before returning any of it to the local system\&. If the query is expected to return a large number of rows, it\(aqs better to open it as a cursor with
109
and then fetch a manageable number of rows at a time\&.
111
A convenient way to use
113
with predetermined queries is to create a view\&. This allows the column type information to be buried in the view, instead of having to spell it out in every query\&. For example,
119
CREATE VIEW myremote_pg_proc AS
121
FROM dblink(\(aqdbname=postgres\(aq, \(aqselect proname, prosrc from pg_proc\(aq)
122
AS t1(proname name, prosrc text);
124
SELECT * FROM myremote_pg_proc WHERE proname LIKE \(aqbytea%\(aq;
135
SELECT * FROM dblink(\(aqdbname=postgres\(aq, \(aqselect proname, prosrc from pg_proc\(aq)
136
AS t1(proname name, prosrc text) WHERE proname LIKE \(aqbytea%\(aq;
138
\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-
147
bytealike | bytealike
148
byteanlike | byteanlike
153
SELECT dblink_connect(\(aqdbname=postgres\(aq);
155
\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
159
SELECT * FROM dblink(\(aqselect proname, prosrc from pg_proc\(aq)
160
AS t1(proname name, prosrc text) WHERE proname LIKE \(aqbytea%\(aq;
162
\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-
171
bytealike | bytealike
172
byteanlike | byteanlike
177
SELECT dblink_connect(\(aqmyconn\(aq, \(aqdbname=regression\(aq);
179
\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
183
SELECT * FROM dblink(\(aqmyconn\(aq, \(aqselect proname, prosrc from pg_proc\(aq)
184
AS t1(proname name, prosrc text) WHERE proname LIKE \(aqbytea%\(aq;
186
\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-
187
bytearecv | bytearecv
188
byteasend | byteasend
194
bytealike | bytealike
195
byteanlike | byteanlike