~ubuntu-branches/ubuntu/oneiric/postgresql-9.1/oneiric-security

« back to all changes in this revision

Viewing changes to doc/src/sgml/dblink.sgml

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!-- doc/src/sgml/dblink.sgml -->
 
2
 
 
3
<sect1 id="dblink">
 
4
 <title>dblink</title>
 
5
 
 
6
 <indexterm zone="dblink">
 
7
  <primary>dblink</primary>
 
8
 </indexterm>
 
9
 
 
10
 <para>
 
11
  <filename>dblink</> is a module which supports connections to
 
12
  other <productname>PostgreSQL</> databases from within a database
 
13
  session.
 
14
 </para>
 
15
 
 
16
 <refentry id="CONTRIB-DBLINK-CONNECT">
 
17
  <refmeta>
 
18
   <refentrytitle>dblink_connect</refentrytitle>
 
19
   <manvolnum>3</manvolnum>
 
20
  </refmeta>
 
21
 
 
22
  <refnamediv>
 
23
   <refname>dblink_connect</refname>
 
24
   <refpurpose>opens a persistent connection to a remote database</refpurpose>
 
25
  </refnamediv>
 
26
 
 
27
  <refsynopsisdiv>
 
28
<synopsis>
 
29
dblink_connect(text connstr) returns text
 
30
dblink_connect(text connname, text connstr) returns text
 
31
</synopsis>
 
32
  </refsynopsisdiv>
 
33
 
 
34
  <refsect1>
 
35
   <title>Description</title>
 
36
 
 
37
   <para>
 
38
    <function>dblink_connect()</> establishes a connection to a remote
 
39
    <productname>PostgreSQL</> database.  The server and database to
 
40
    be contacted are identified through a standard <application>libpq</>
 
41
    connection string.  Optionally, a name can be assigned to the
 
42
    connection.  Multiple named connections can be open at once, but
 
43
    only one unnamed connection is permitted at a time.  The connection
 
44
    will persist until closed or until the database session is ended.
 
45
   </para>
 
46
 
 
47
   <para>
 
48
    The connection string may also be the name of an existing foreign
 
49
    server.  It is recommended to use
 
50
    the <function>postgresql_fdw_validator</function> when defining
 
51
    the corresponding foreign-data wrapper.  See the example below, as
 
52
    well as the following:
 
53
    <simplelist type="inline">
 
54
     <member><xref linkend="sql-createforeigndatawrapper"></member>
 
55
     <member><xref linkend="sql-createserver"></member>
 
56
     <member><xref linkend="sql-createusermapping"></member>
 
57
    </simplelist>
 
58
   </para>
 
59
 
 
60
  </refsect1>
 
61
 
 
62
  <refsect1>
 
63
   <title>Arguments</title>
 
64
 
 
65
   <variablelist>
 
66
    <varlistentry>
 
67
     <term><parameter>conname</parameter></term>
 
68
     <listitem>
 
69
      <para>
 
70
       The name to use for this connection; if omitted, an unnamed
 
71
       connection is opened, replacing any existing unnamed connection.
 
72
      </para>
 
73
     </listitem>
 
74
    </varlistentry>
 
75
 
 
76
    <varlistentry>
 
77
     <term><parameter>connstr</parameter></term>
 
78
     <listitem>
 
79
      <para>
 
80
       <application>libpq</>-style connection info string, for example
 
81
       <literal>hostaddr=127.0.0.1 port=5432 dbname=mydb user=postgres
 
82
       password=mypasswd</>.
 
83
       For details see <function>PQconnectdb</> in
 
84
       <xref linkend="libpq-connect">.
 
85
      </para>
 
86
     </listitem>
 
87
    </varlistentry>
 
88
   </variablelist>
 
89
  </refsect1>
 
90
 
 
91
  <refsect1>
 
92
   <title>Return Value</title>
 
93
 
 
94
   <para>
 
95
    Returns status, which is always <literal>OK</> (since any error
 
96
    causes the function to throw an error instead of returning).
 
97
   </para>
 
98
  </refsect1>
 
99
 
 
100
  <refsect1>
 
101
   <title>Notes</title>
 
102
 
 
103
   <para>
 
104
    Only superusers may use <function>dblink_connect</> to create
 
105
    non-password-authenticated connections.  If non-superusers need this
 
106
    capability, use <function>dblink_connect_u</> instead.
 
107
   </para>
 
108
 
 
109
   <para>
 
110
    It is unwise to choose connection names that contain equal signs,
 
111
    as this opens a risk of confusion with connection info strings
 
112
    in other <filename>dblink</> functions.
 
113
   </para>
 
114
  </refsect1>
 
115
 
 
116
  <refsect1>
 
117
   <title>Example</title>
 
118
 
 
119
<screen>
 
120
SELECT dblink_connect('dbname=postgres');
 
121
 dblink_connect
 
122
----------------
 
123
 OK
 
124
(1 row)
 
125
 
 
126
SELECT dblink_connect('myconn', 'dbname=postgres');
 
127
 dblink_connect
 
128
----------------
 
129
 OK
 
130
(1 row)
 
131
 
 
132
-- FOREIGN DATA WRAPPER functionality
 
133
-- Note: local connection must require password authentication for this to work properly
 
134
--       Otherwise, you will receive the following error from dblink_connect():
 
135
--       ----------------------------------------------------------------------
 
136
--       ERROR:  password is required
 
137
--       DETAIL:  Non-superuser cannot connect if the server does not request a password.
 
138
--       HINT:  Target server's authentication method must be changed.
 
139
CREATE USER dblink_regression_test WITH PASSWORD 'secret';
 
140
CREATE FOREIGN DATA WRAPPER postgresql VALIDATOR postgresql_fdw_validator;
 
141
CREATE SERVER fdtest FOREIGN DATA WRAPPER postgresql OPTIONS (hostaddr '127.0.0.1', dbname 'contrib_regression');
 
142
 
 
143
CREATE USER MAPPING FOR dblink_regression_test SERVER fdtest OPTIONS (user 'dblink_regression_test', password 'secret');
 
144
GRANT USAGE ON FOREIGN SERVER fdtest TO dblink_regression_test;
 
145
GRANT SELECT ON TABLE foo TO dblink_regression_test;
 
146
 
 
147
\set ORIGINAL_USER :USER
 
148
\c - dblink_regression_test
 
149
SELECT dblink_connect('myconn', 'fdtest');
 
150
 dblink_connect 
 
151
----------------
 
152
 OK
 
153
(1 row)
 
154
 
 
155
SELECT * FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[]);
 
156
 a  | b |       c       
 
157
----+---+---------------
 
158
  0 | a | {a0,b0,c0}
 
159
  1 | b | {a1,b1,c1}
 
160
  2 | c | {a2,b2,c2}
 
161
  3 | d | {a3,b3,c3}
 
162
  4 | e | {a4,b4,c4}
 
163
  5 | f | {a5,b5,c5}
 
164
  6 | g | {a6,b6,c6}
 
165
  7 | h | {a7,b7,c7}
 
166
  8 | i | {a8,b8,c8}
 
167
  9 | j | {a9,b9,c9}
 
168
 10 | k | {a10,b10,c10}
 
169
(11 rows)
 
170
 
 
171
\c - :ORIGINAL_USER
 
172
REVOKE USAGE ON FOREIGN SERVER fdtest FROM dblink_regression_test;
 
173
REVOKE SELECT ON TABLE foo FROM  dblink_regression_test;
 
174
DROP USER MAPPING FOR dblink_regression_test SERVER fdtest;
 
175
DROP USER dblink_regression_test;
 
176
DROP SERVER fdtest;
 
177
DROP FOREIGN DATA WRAPPER postgresql;
 
178
</screen>
 
179
  </refsect1>
 
180
 </refentry>
 
181
 
 
182
 <refentry id="CONTRIB-DBLINK-CONNECT-U">
 
183
  <refmeta>
 
184
   <refentrytitle>dblink_connect_u</refentrytitle>
 
185
   <manvolnum>3</manvolnum>
 
186
  </refmeta>
 
187
 
 
188
  <refnamediv>
 
189
   <refname>dblink_connect_u</refname>
 
190
   <refpurpose>opens a persistent connection to a remote database, insecurely</refpurpose>
 
191
  </refnamediv>
 
192
 
 
193
  <refsynopsisdiv>
 
194
<synopsis>
 
195
dblink_connect_u(text connstr) returns text
 
196
dblink_connect_u(text connname, text connstr) returns text
 
197
</synopsis>
 
198
  </refsynopsisdiv>
 
199
 
 
200
  <refsect1>
 
201
   <title>Description</title>
 
202
 
 
203
   <para>
 
204
    <function>dblink_connect_u()</> is identical to
 
205
    <function>dblink_connect()</>, except that it will allow non-superusers
 
206
    to connect using any authentication method.
 
207
   </para>
 
208
 
 
209
   <para>
 
210
    If the remote server selects an authentication method that does not
 
211
    involve a password, then impersonation and subsequent escalation of
 
212
    privileges can occur, because the session will appear to have
 
213
    originated from the user as which the local <productname>PostgreSQL</>
 
214
    server runs.  Also, even if the remote server does demand a password,
 
215
    it is possible for the password to be supplied from the server
 
216
    environment, such as a <filename>~/.pgpass</> file belonging to the
 
217
    server's user.  This opens not only a risk of impersonation, but the
 
218
    possibility of exposing a password to an untrustworthy remote server.
 
219
    Therefore, <function>dblink_connect_u()</> is initially
 
220
    installed with all privileges revoked from <literal>PUBLIC</>,
 
221
    making it un-callable except by superusers.  In some situations
 
222
    it may be appropriate to grant <literal>EXECUTE</> permission for
 
223
    <function>dblink_connect_u()</> to specific users who are considered
 
224
    trustworthy, but this should be done with care.  It is also recommended
 
225
    that any <filename>~/.pgpass</> file belonging to the server's user
 
226
    <emphasis>not</> contain any records specifying a wildcard host name.
 
227
   </para>
 
228
 
 
229
   <para>
 
230
    For further details see <function>dblink_connect()</>.
 
231
   </para>
 
232
  </refsect1>
 
233
 </refentry>
 
234
 
 
235
 <refentry id="CONTRIB-DBLINK-DISCONNECT">
 
236
  <refmeta>
 
237
   <refentrytitle>dblink_disconnect</refentrytitle>
 
238
   <manvolnum>3</manvolnum>
 
239
  </refmeta>
 
240
 
 
241
  <refnamediv>
 
242
   <refname>dblink_disconnect</refname>
 
243
   <refpurpose>closes a persistent connection to a remote database</refpurpose>
 
244
  </refnamediv>
 
245
 
 
246
  <refsynopsisdiv>
 
247
<synopsis>
 
248
dblink_disconnect() returns text
 
249
dblink_disconnect(text connname) returns text
 
250
</synopsis>
 
251
  </refsynopsisdiv>
 
252
 
 
253
  <refsect1>
 
254
   <title>Description</title>
 
255
 
 
256
   <para>
 
257
    <function>dblink_disconnect()</> closes a connection previously opened
 
258
    by <function>dblink_connect()</>.  The form with no arguments closes
 
259
    an unnamed connection.
 
260
   </para>
 
261
  </refsect1>
 
262
 
 
263
  <refsect1>
 
264
   <title>Arguments</title>
 
265
 
 
266
   <variablelist>
 
267
    <varlistentry>
 
268
     <term><parameter>conname</parameter></term>
 
269
     <listitem>
 
270
      <para>
 
271
       The name of a named connection to be closed.
 
272
      </para>
 
273
     </listitem>
 
274
    </varlistentry>
 
275
   </variablelist>
 
276
  </refsect1>
 
277
 
 
278
  <refsect1>
 
279
   <title>Return Value</title>
 
280
 
 
281
   <para>
 
282
    Returns status, which is always <literal>OK</> (since any error
 
283
    causes the function to throw an error instead of returning).
 
284
   </para>
 
285
  </refsect1>
 
286
 
 
287
  <refsect1>
 
288
   <title>Example</title>
 
289
 
 
290
<screen>
 
291
SELECT dblink_disconnect();
 
292
 dblink_disconnect
 
293
-------------------
 
294
 OK
 
295
(1 row)
 
296
 
 
297
SELECT dblink_disconnect('myconn');
 
298
 dblink_disconnect
 
299
-------------------
 
300
 OK
 
301
(1 row)
 
302
</screen>
 
303
  </refsect1>
 
304
 </refentry>
 
305
 
 
306
 <refentry id="CONTRIB-DBLINK-FUNCTION">
 
307
  <refmeta>
 
308
   <refentrytitle>dblink</refentrytitle>
 
309
   <manvolnum>3</manvolnum>
 
310
  </refmeta>
 
311
 
 
312
  <refnamediv>
 
313
   <refname>dblink</refname>
 
314
   <refpurpose>executes a query in a remote database</refpurpose>
 
315
  </refnamediv>
 
316
 
 
317
  <refsynopsisdiv>
 
318
<synopsis>
 
319
dblink(text connname, text sql [, bool fail_on_error]) returns setof record
 
320
dblink(text connstr, text sql [, bool fail_on_error]) returns setof record
 
321
dblink(text sql [, bool fail_on_error]) returns setof record
 
322
</synopsis>
 
323
  </refsynopsisdiv>
 
324
 
 
325
  <refsect1>
 
326
   <title>Description</title>
 
327
 
 
328
   <para>
 
329
    <function>dblink</> executes a query (usually a <command>SELECT</>,
 
330
    but it can be any SQL statement that returns rows) in a remote database.
 
331
   </para>
 
332
 
 
333
   <para>
 
334
    When two <type>text</> arguments are given, the first one is first
 
335
    looked up as a persistent connection's name; if found, the command
 
336
    is executed on that connection.  If not found, the first argument
 
337
    is treated as a connection info string as for <function>dblink_connect</>,
 
338
    and the indicated connection is made just for the duration of this command.
 
339
   </para>
 
340
  </refsect1>
 
341
 
 
342
  <refsect1>
 
343
   <title>Arguments</title>
 
344
 
 
345
   <variablelist>
 
346
    <varlistentry>
 
347
     <term><parameter>conname</parameter></term>
 
348
     <listitem>
 
349
      <para>
 
350
       Name of the connection to use; omit this parameter to use the
 
351
       unnamed connection.
 
352
      </para>
 
353
     </listitem>
 
354
    </varlistentry>
 
355
 
 
356
    <varlistentry>
 
357
     <term><parameter>connstr</parameter></term>
 
358
     <listitem>
 
359
      <para>
 
360
       A connection info string, as previously described for
 
361
       <function>dblink_connect</>.
 
362
      </para>
 
363
     </listitem>
 
364
    </varlistentry>
 
365
 
 
366
    <varlistentry>
 
367
     <term><parameter>sql</parameter></term>
 
368
     <listitem>
 
369
      <para>
 
370
       The SQL query that you wish to execute in the remote database,
 
371
       for example <literal>select * from foo</>.
 
372
      </para>
 
373
     </listitem>
 
374
    </varlistentry>
 
375
 
 
376
    <varlistentry>
 
377
     <term><parameter>fail_on_error</parameter></term>
 
378
     <listitem>
 
379
      <para>
 
380
       If true (the default when omitted) then an error thrown on the
 
381
       remote side of the connection causes an error to also be thrown
 
382
       locally. If false, the remote error is locally reported as a NOTICE,
 
383
       and the function returns no rows.
 
384
      </para>
 
385
     </listitem>
 
386
    </varlistentry>
 
387
   </variablelist>
 
388
  </refsect1>
 
389
 
 
390
  <refsect1>
 
391
   <title>Return Value</title>
 
392
 
 
393
   <para>
 
394
    The function returns the row(s) produced by the query.  Since
 
395
    <function>dblink</> can be used with any query, it is declared
 
396
    to return <type>record</>, rather than specifying any particular
 
397
    set of columns.  This means that you must specify the expected
 
398
    set of columns in the calling query &mdash; otherwise
 
399
    <productname>PostgreSQL</> would not know what to expect.
 
400
    Here is an example:
 
401
 
 
402
<programlisting>
 
403
SELECT *
 
404
    FROM dblink('dbname=mydb', 'select proname, prosrc from pg_proc')
 
405
      AS t1(proname name, prosrc text)
 
406
    WHERE proname LIKE 'bytea%';
 
407
</programlisting>
 
408
 
 
409
    The <quote>alias</> part of the <literal>FROM</> clause must
 
410
    specify the column names and types that the function will return.
 
411
    (Specifying column names in an alias is actually standard SQL
 
412
    syntax, but specifying column types is a <productname>PostgreSQL</>
 
413
    extension.)  This allows the system to understand what
 
414
    <literal>*</> should expand to, and what <structname>proname</>
 
415
    in the <literal>WHERE</> clause refers to, in advance of trying
 
416
    to execute the function.  At run time, an error will be thrown
 
417
    if the actual query result from the remote database does not
 
418
    have the same number of columns shown in the <literal>FROM</> clause.
 
419
    The column names need not match, however, and <function>dblink</>
 
420
    does not insist on exact type matches either.  It will succeed
 
421
    so long as the returned data strings are valid input for the
 
422
    column type declared in the <literal>FROM</> clause.
 
423
   </para>
 
424
  </refsect1>
 
425
 
 
426
  <refsect1>
 
427
   <title>Notes</title>
 
428
 
 
429
   <para>
 
430
    <function>dblink</> fetches the entire remote query result before
 
431
    returning any of it to the local system.  If the query is expected
 
432
    to return a large number of rows, it's better to open it as a cursor
 
433
    with <function>dblink_open</> and then fetch a manageable number
 
434
    of rows at a time.
 
435
   </para>
 
436
 
 
437
   <para>
 
438
    A convenient way to use <function>dblink</> with predetermined
 
439
    queries is to create a view.
 
440
    This allows the column type information to be buried in the view,
 
441
    instead of having to spell it out in every query.  For example,
 
442
 
 
443
<programlisting>
 
444
CREATE VIEW myremote_pg_proc AS
 
445
  SELECT *
 
446
    FROM dblink('dbname=postgres', 'select proname, prosrc from pg_proc')
 
447
    AS t1(proname name, prosrc text);
 
448
 
 
449
SELECT * FROM myremote_pg_proc WHERE proname LIKE 'bytea%';
 
450
</programlisting>
 
451
   </para>
 
452
  </refsect1>
 
453
 
 
454
  <refsect1>
 
455
   <title>Example</title>
 
456
 
 
457
<screen>
 
458
SELECT * FROM dblink('dbname=postgres', 'select proname, prosrc from pg_proc')
 
459
  AS t1(proname name, prosrc text) WHERE proname LIKE 'bytea%';
 
460
  proname   |   prosrc
 
461
------------+------------
 
462
 byteacat   | byteacat
 
463
 byteaeq    | byteaeq
 
464
 bytealt    | bytealt
 
465
 byteale    | byteale
 
466
 byteagt    | byteagt
 
467
 byteage    | byteage
 
468
 byteane    | byteane
 
469
 byteacmp   | byteacmp
 
470
 bytealike  | bytealike
 
471
 byteanlike | byteanlike
 
472
 byteain    | byteain
 
473
 byteaout   | byteaout
 
474
(12 rows)
 
475
 
 
476
SELECT dblink_connect('dbname=postgres');
 
477
 dblink_connect
 
478
----------------
 
479
 OK
 
480
(1 row)
 
481
 
 
482
SELECT * FROM dblink('select proname, prosrc from pg_proc')
 
483
  AS t1(proname name, prosrc text) WHERE proname LIKE 'bytea%';
 
484
  proname   |   prosrc
 
485
------------+------------
 
486
 byteacat   | byteacat
 
487
 byteaeq    | byteaeq
 
488
 bytealt    | bytealt
 
489
 byteale    | byteale
 
490
 byteagt    | byteagt
 
491
 byteage    | byteage
 
492
 byteane    | byteane
 
493
 byteacmp   | byteacmp
 
494
 bytealike  | bytealike
 
495
 byteanlike | byteanlike
 
496
 byteain    | byteain
 
497
 byteaout   | byteaout
 
498
(12 rows)
 
499
 
 
500
SELECT dblink_connect('myconn', 'dbname=regression');
 
501
 dblink_connect
 
502
----------------
 
503
 OK
 
504
(1 row)
 
505
 
 
506
SELECT * FROM dblink('myconn', 'select proname, prosrc from pg_proc')
 
507
  AS t1(proname name, prosrc text) WHERE proname LIKE 'bytea%';
 
508
  proname   |   prosrc
 
509
------------+------------
 
510
 bytearecv  | bytearecv
 
511
 byteasend  | byteasend
 
512
 byteale    | byteale
 
513
 byteagt    | byteagt
 
514
 byteage    | byteage
 
515
 byteane    | byteane
 
516
 byteacmp   | byteacmp
 
517
 bytealike  | bytealike
 
518
 byteanlike | byteanlike
 
519
 byteacat   | byteacat
 
520
 byteaeq    | byteaeq
 
521
 bytealt    | bytealt
 
522
 byteain    | byteain
 
523
 byteaout   | byteaout
 
524
(14 rows)
 
525
</screen>
 
526
  </refsect1>
 
527
 </refentry>
 
528
 
 
529
 <refentry id="CONTRIB-DBLINK-EXEC">
 
530
  <refmeta>
 
531
   <refentrytitle>dblink_exec</refentrytitle>
 
532
   <manvolnum>3</manvolnum>
 
533
  </refmeta>
 
534
 
 
535
  <refnamediv>
 
536
   <refname>dblink_exec</refname>
 
537
   <refpurpose>executes a command in a remote database</refpurpose>
 
538
  </refnamediv>
 
539
 
 
540
  <refsynopsisdiv>
 
541
<synopsis>
 
542
dblink_exec(text connname, text sql [, bool fail_on_error]) returns text
 
543
dblink_exec(text connstr, text sql [, bool fail_on_error]) returns text
 
544
dblink_exec(text sql [, bool fail_on_error]) returns text
 
545
</synopsis>
 
546
  </refsynopsisdiv>
 
547
 
 
548
  <refsect1>
 
549
   <title>Description</title>
 
550
 
 
551
   <para>
 
552
    <function>dblink_exec</> executes a command (that is, any SQL statement
 
553
    that doesn't return rows) in a remote database.
 
554
   </para>
 
555
 
 
556
   <para>
 
557
    When two <type>text</> arguments are given, the first one is first
 
558
    looked up as a persistent connection's name; if found, the command
 
559
    is executed on that connection.  If not found, the first argument
 
560
    is treated as a connection info string as for <function>dblink_connect</>,
 
561
    and the indicated connection is made just for the duration of this command.
 
562
   </para>
 
563
  </refsect1>
 
564
 
 
565
  <refsect1>
 
566
   <title>Arguments</title>
 
567
 
 
568
   <variablelist>
 
569
    <varlistentry>
 
570
     <term><parameter>conname</parameter></term>
 
571
     <listitem>
 
572
      <para>
 
573
       Name of the connection to use; omit this parameter to use the
 
574
       unnamed connection.
 
575
      </para>
 
576
     </listitem>
 
577
    </varlistentry>
 
578
 
 
579
    <varlistentry>
 
580
     <term><parameter>connstr</parameter></term>
 
581
     <listitem>
 
582
      <para>
 
583
       A connection info string, as previously described for
 
584
       <function>dblink_connect</>.
 
585
      </para>
 
586
     </listitem>
 
587
    </varlistentry>
 
588
 
 
589
    <varlistentry>
 
590
     <term><parameter>sql</parameter></term>
 
591
     <listitem>
 
592
      <para>
 
593
       The SQL command that you wish to execute in the remote database,
 
594
       for example
 
595
       <literal>insert into foo values(0,'a','{"a0","b0","c0"}')</>.
 
596
      </para>
 
597
     </listitem>
 
598
    </varlistentry>
 
599
 
 
600
    <varlistentry>
 
601
     <term><parameter>fail_on_error</parameter></term>
 
602
     <listitem>
 
603
      <para>
 
604
       If true (the default when omitted) then an error thrown on the
 
605
       remote side of the connection causes an error to also be thrown
 
606
       locally. If false, the remote error is locally reported as a NOTICE,
 
607
       and the function's return value is set to <literal>ERROR</>.
 
608
      </para>
 
609
     </listitem>
 
610
    </varlistentry>
 
611
   </variablelist>
 
612
  </refsect1>
 
613
 
 
614
  <refsect1>
 
615
   <title>Return Value</title>
 
616
 
 
617
   <para>
 
618
    Returns status, either the command's status string or <literal>ERROR</>.
 
619
   </para>
 
620
  </refsect1>
 
621
 
 
622
  <refsect1>
 
623
   <title>Example</title>
 
624
 
 
625
<screen>
 
626
SELECT dblink_connect('dbname=dblink_test_standby');
 
627
 dblink_connect
 
628
----------------
 
629
 OK
 
630
(1 row)
 
631
 
 
632
SELECT dblink_exec('insert into foo values(21,''z'',''{"a0","b0","c0"}'');');
 
633
   dblink_exec
 
634
-----------------
 
635
 INSERT 943366 1
 
636
(1 row)
 
637
 
 
638
SELECT dblink_connect('myconn', 'dbname=regression');
 
639
 dblink_connect
 
640
----------------
 
641
 OK
 
642
(1 row)
 
643
 
 
644
SELECT dblink_exec('myconn', 'insert into foo values(21,''z'',''{"a0","b0","c0"}'');');
 
645
   dblink_exec
 
646
------------------
 
647
 INSERT 6432584 1
 
648
(1 row)
 
649
 
 
650
SELECT dblink_exec('myconn', 'insert into pg_class values (''foo'')',false);
 
651
NOTICE:  sql error
 
652
DETAIL:  ERROR:  null value in column "relnamespace" violates not-null constraint
 
653
 
 
654
 dblink_exec
 
655
-------------
 
656
 ERROR
 
657
(1 row)
 
658
</screen>
 
659
  </refsect1>
 
660
 </refentry>
 
661
 
 
662
 <refentry id="CONTRIB-DBLINK-OPEN">
 
663
  <refmeta>
 
664
   <refentrytitle>dblink_open</refentrytitle>
 
665
   <manvolnum>3</manvolnum>
 
666
  </refmeta>
 
667
 
 
668
  <refnamediv>
 
669
   <refname>dblink_open</refname>
 
670
   <refpurpose>opens a cursor in a remote database</refpurpose>
 
671
  </refnamediv>
 
672
 
 
673
  <refsynopsisdiv>
 
674
<synopsis>
 
675
dblink_open(text cursorname, text sql [, bool fail_on_error]) returns text
 
676
dblink_open(text connname, text cursorname, text sql [, bool fail_on_error]) returns text
 
677
</synopsis>
 
678
  </refsynopsisdiv>
 
679
 
 
680
  <refsect1>
 
681
   <title>Description</title>
 
682
 
 
683
   <para>
 
684
    <function>dblink_open()</> opens a cursor in a remote database.
 
685
    The cursor can subsequently be manipulated with
 
686
    <function>dblink_fetch()</> and <function>dblink_close()</>.
 
687
   </para>
 
688
  </refsect1>
 
689
 
 
690
  <refsect1>
 
691
   <title>Arguments</title>
 
692
 
 
693
   <variablelist>
 
694
    <varlistentry>
 
695
     <term><parameter>conname</parameter></term>
 
696
     <listitem>
 
697
      <para>
 
698
       Name of the connection to use; omit this parameter to use the
 
699
       unnamed connection.
 
700
      </para>
 
701
     </listitem>
 
702
    </varlistentry>
 
703
 
 
704
    <varlistentry>
 
705
     <term><parameter>cursorname</parameter></term>
 
706
     <listitem>
 
707
      <para>
 
708
       The name to assign to this cursor.
 
709
      </para>
 
710
     </listitem>
 
711
    </varlistentry>
 
712
 
 
713
    <varlistentry>
 
714
     <term><parameter>sql</parameter></term>
 
715
     <listitem>
 
716
      <para>
 
717
       The <command>SELECT</> statement that you wish to execute in the remote
 
718
       database, for example <literal>select * from pg_class</>.
 
719
      </para>
 
720
     </listitem>
 
721
    </varlistentry>
 
722
 
 
723
    <varlistentry>
 
724
     <term><parameter>fail_on_error</parameter></term>
 
725
     <listitem>
 
726
      <para>
 
727
       If true (the default when omitted) then an error thrown on the
 
728
       remote side of the connection causes an error to also be thrown
 
729
       locally. If false, the remote error is locally reported as a NOTICE,
 
730
       and the function's return value is set to <literal>ERROR</>.
 
731
      </para>
 
732
     </listitem>
 
733
    </varlistentry>
 
734
   </variablelist>
 
735
  </refsect1>
 
736
 
 
737
  <refsect1>
 
738
   <title>Return Value</title>
 
739
 
 
740
   <para>
 
741
    Returns status, either <literal>OK</> or <literal>ERROR</>.
 
742
   </para>
 
743
  </refsect1>
 
744
 
 
745
  <refsect1>
 
746
   <title>Notes</title>
 
747
 
 
748
   <para>
 
749
    Since a cursor can only persist within a transaction,
 
750
    <function>dblink_open</> starts an explicit transaction block
 
751
    (<command>BEGIN</>) on the remote side, if the remote side was
 
752
    not already within a transaction.  This transaction will be
 
753
    closed again when the matching <function>dblink_close</> is
 
754
    executed.  Note that if
 
755
    you use <function>dblink_exec</> to change data between
 
756
    <function>dblink_open</> and <function>dblink_close</>,
 
757
    and then an error occurs or you use <function>dblink_disconnect</> before
 
758
    <function>dblink_close</>, your change <emphasis>will be
 
759
    lost</> because the transaction will be aborted.
 
760
   </para>
 
761
  </refsect1>
 
762
 
 
763
  <refsect1>
 
764
   <title>Example</title>
 
765
 
 
766
<screen>
 
767
SELECT dblink_connect('dbname=postgres');
 
768
 dblink_connect
 
769
----------------
 
770
 OK
 
771
(1 row)
 
772
 
 
773
SELECT dblink_open('foo', 'select proname, prosrc from pg_proc');
 
774
 dblink_open
 
775
-------------
 
776
 OK
 
777
(1 row)
 
778
</screen>
 
779
  </refsect1>
 
780
 </refentry>
 
781
 
 
782
 <refentry id="CONTRIB-DBLINK-FETCH">
 
783
  <refmeta>
 
784
   <refentrytitle>dblink_fetch</refentrytitle>
 
785
   <manvolnum>3</manvolnum>
 
786
  </refmeta>
 
787
 
 
788
  <refnamediv>
 
789
   <refname>dblink_fetch</refname>
 
790
   <refpurpose>returns rows from an open cursor in a remote database</refpurpose>
 
791
  </refnamediv>
 
792
 
 
793
  <refsynopsisdiv>
 
794
<synopsis>
 
795
dblink_fetch(text cursorname, int howmany [, bool fail_on_error]) returns setof record
 
796
dblink_fetch(text connname, text cursorname, int howmany [, bool fail_on_error]) returns setof record
 
797
</synopsis>
 
798
  </refsynopsisdiv>
 
799
 
 
800
  <refsect1>
 
801
   <title>Description</title>
 
802
 
 
803
   <para>
 
804
    <function>dblink_fetch</> fetches rows from a cursor previously
 
805
    established by <function>dblink_open</>.
 
806
   </para>
 
807
  </refsect1>
 
808
 
 
809
  <refsect1>
 
810
   <title>Arguments</title>
 
811
 
 
812
   <variablelist>
 
813
    <varlistentry>
 
814
     <term><parameter>conname</parameter></term>
 
815
     <listitem>
 
816
      <para>
 
817
       Name of the connection to use; omit this parameter to use the
 
818
       unnamed connection.
 
819
      </para>
 
820
     </listitem>
 
821
    </varlistentry>
 
822
 
 
823
    <varlistentry>
 
824
     <term><parameter>cursorname</parameter></term>
 
825
     <listitem>
 
826
      <para>
 
827
       The name of the cursor to fetch from.
 
828
      </para>
 
829
     </listitem>
 
830
    </varlistentry>
 
831
 
 
832
    <varlistentry>
 
833
     <term><parameter>howmany</parameter></term>
 
834
     <listitem>
 
835
      <para>
 
836
       The maximum number of rows to retrieve. The next <parameter>howmany</>
 
837
       rows are fetched, starting at the current cursor position, moving
 
838
       forward. Once the cursor has reached its end, no more rows are produced.
 
839
      </para>
 
840
     </listitem>
 
841
    </varlistentry>
 
842
 
 
843
    <varlistentry>
 
844
     <term><parameter>fail_on_error</parameter></term>
 
845
     <listitem>
 
846
      <para>
 
847
       If true (the default when omitted) then an error thrown on the
 
848
       remote side of the connection causes an error to also be thrown
 
849
       locally. If false, the remote error is locally reported as a NOTICE,
 
850
       and the function returns no rows.
 
851
      </para>
 
852
     </listitem>
 
853
    </varlistentry>
 
854
   </variablelist>
 
855
  </refsect1>
 
856
 
 
857
  <refsect1>
 
858
   <title>Return Value</title>
 
859
 
 
860
   <para>
 
861
    The function returns the row(s) fetched from the cursor.  To use this
 
862
    function, you will need to specify the expected set of columns,
 
863
    as previously discussed for <function>dblink</>.
 
864
   </para>
 
865
  </refsect1>
 
866
 
 
867
  <refsect1>
 
868
   <title>Notes</title>
 
869
 
 
870
   <para>
 
871
    On a mismatch between the number of return columns specified in the
 
872
    <literal>FROM</> clause, and the actual number of columns returned by the
 
873
    remote cursor, an error will be thrown. In this event, the remote cursor
 
874
    is still advanced by as many rows as it would have been if the error had
 
875
    not occurred.  The same is true for any other error occurring in the local
 
876
    query after the remote <command>FETCH</> has been done.
 
877
   </para>
 
878
  </refsect1>
 
879
 
 
880
  <refsect1>
 
881
   <title>Example</title>
 
882
 
 
883
<screen>
 
884
SELECT dblink_connect('dbname=postgres');
 
885
 dblink_connect
 
886
----------------
 
887
 OK
 
888
(1 row)
 
889
 
 
890
SELECT dblink_open('foo', 'select proname, prosrc from pg_proc where proname like ''bytea%''');
 
891
 dblink_open
 
892
-------------
 
893
 OK
 
894
(1 row)
 
895
 
 
896
SELECT * FROM dblink_fetch('foo', 5) AS (funcname name, source text);
 
897
 funcname |  source
 
898
----------+----------
 
899
 byteacat | byteacat
 
900
 byteacmp | byteacmp
 
901
 byteaeq  | byteaeq
 
902
 byteage  | byteage
 
903
 byteagt  | byteagt
 
904
(5 rows)
 
905
 
 
906
SELECT * FROM dblink_fetch('foo', 5) AS (funcname name, source text);
 
907
 funcname  |  source
 
908
-----------+-----------
 
909
 byteain   | byteain
 
910
 byteale   | byteale
 
911
 bytealike | bytealike
 
912
 bytealt   | bytealt
 
913
 byteane   | byteane
 
914
(5 rows)
 
915
 
 
916
SELECT * FROM dblink_fetch('foo', 5) AS (funcname name, source text);
 
917
  funcname  |   source
 
918
------------+------------
 
919
 byteanlike | byteanlike
 
920
 byteaout   | byteaout
 
921
(2 rows)
 
922
 
 
923
SELECT * FROM dblink_fetch('foo', 5) AS (funcname name, source text);
 
924
 funcname | source
 
925
----------+--------
 
926
(0 rows)
 
927
</screen>
 
928
  </refsect1>
 
929
 </refentry>
 
930
 
 
931
 <refentry id="CONTRIB-DBLINK-CLOSE">
 
932
  <refmeta>
 
933
   <refentrytitle>dblink_close</refentrytitle>
 
934
   <manvolnum>3</manvolnum>
 
935
  </refmeta>
 
936
 
 
937
  <refnamediv>
 
938
   <refname>dblink_close</refname>
 
939
   <refpurpose>closes a cursor in a remote database</refpurpose>
 
940
  </refnamediv>
 
941
 
 
942
  <refsynopsisdiv>
 
943
<synopsis>
 
944
dblink_close(text cursorname [, bool fail_on_error]) returns text
 
945
dblink_close(text connname, text cursorname [, bool fail_on_error]) returns text
 
946
</synopsis>
 
947
  </refsynopsisdiv>
 
948
 
 
949
  <refsect1>
 
950
   <title>Description</title>
 
951
 
 
952
   <para>
 
953
    <function>dblink_close</> closes a cursor previously opened with
 
954
    <function>dblink_open</>.
 
955
   </para>
 
956
  </refsect1>
 
957
 
 
958
  <refsect1>
 
959
   <title>Arguments</title>
 
960
 
 
961
   <variablelist>
 
962
    <varlistentry>
 
963
     <term><parameter>conname</parameter></term>
 
964
     <listitem>
 
965
      <para>
 
966
       Name of the connection to use; omit this parameter to use the
 
967
       unnamed connection.
 
968
      </para>
 
969
     </listitem>
 
970
    </varlistentry>
 
971
 
 
972
    <varlistentry>
 
973
     <term><parameter>cursorname</parameter></term>
 
974
     <listitem>
 
975
      <para>
 
976
       The name of the cursor to close.
 
977
      </para>
 
978
     </listitem>
 
979
    </varlistentry>
 
980
 
 
981
    <varlistentry>
 
982
     <term><parameter>fail_on_error</parameter></term>
 
983
     <listitem>
 
984
      <para>
 
985
       If true (the default when omitted) then an error thrown on the
 
986
       remote side of the connection causes an error to also be thrown
 
987
       locally. If false, the remote error is locally reported as a NOTICE,
 
988
       and the function's return value is set to <literal>ERROR</>.
 
989
      </para>
 
990
     </listitem>
 
991
    </varlistentry>
 
992
   </variablelist>
 
993
  </refsect1>
 
994
 
 
995
  <refsect1>
 
996
   <title>Return Value</title>
 
997
 
 
998
   <para>
 
999
    Returns status, either <literal>OK</> or <literal>ERROR</>.
 
1000
   </para>
 
1001
  </refsect1>
 
1002
 
 
1003
  <refsect1>
 
1004
   <title>Notes</title>
 
1005
 
 
1006
   <para>
 
1007
    If <function>dblink_open</> started an explicit transaction block,
 
1008
    and this is the last remaining open cursor in this connection,
 
1009
    <function>dblink_close</> will issue the matching <command>COMMIT</>.
 
1010
   </para>
 
1011
  </refsect1>
 
1012
 
 
1013
  <refsect1>
 
1014
   <title>Example</title>
 
1015
 
 
1016
<screen>
 
1017
SELECT dblink_connect('dbname=postgres');
 
1018
 dblink_connect
 
1019
----------------
 
1020
 OK
 
1021
(1 row)
 
1022
 
 
1023
SELECT dblink_open('foo', 'select proname, prosrc from pg_proc');
 
1024
 dblink_open
 
1025
-------------
 
1026
 OK
 
1027
(1 row)
 
1028
 
 
1029
SELECT dblink_close('foo');
 
1030
 dblink_close
 
1031
--------------
 
1032
 OK
 
1033
(1 row)
 
1034
</screen>
 
1035
  </refsect1>
 
1036
 </refentry>
 
1037
 
 
1038
 <refentry id="CONTRIB-DBLINK-GET-CONNECTIONS">
 
1039
  <refmeta>
 
1040
   <refentrytitle>dblink_get_connections</refentrytitle>
 
1041
   <manvolnum>3</manvolnum>
 
1042
  </refmeta>
 
1043
 
 
1044
  <refnamediv>
 
1045
   <refname>dblink_get_connections</refname>
 
1046
   <refpurpose>returns the names of all open named dblink connections</refpurpose>
 
1047
  </refnamediv>
 
1048
 
 
1049
  <refsynopsisdiv>
 
1050
<synopsis>
 
1051
dblink_get_connections() returns text[]
 
1052
</synopsis>
 
1053
  </refsynopsisdiv>
 
1054
 
 
1055
  <refsect1>
 
1056
   <title>Description</title>
 
1057
 
 
1058
   <para>
 
1059
    <function>dblink_get_connections</> returns an array of the names
 
1060
    of all open named <filename>dblink</> connections.
 
1061
   </para>
 
1062
  </refsect1>
 
1063
 
 
1064
  <refsect1>
 
1065
   <title>Return Value</title>
 
1066
 
 
1067
   <para>Returns a text array of connection names, or NULL if none.</para>
 
1068
  </refsect1>
 
1069
 
 
1070
  <refsect1>
 
1071
   <title>Example</title>
 
1072
 
 
1073
<programlisting>
 
1074
SELECT dblink_get_connections();
 
1075
</programlisting>
 
1076
  </refsect1>
 
1077
 </refentry>
 
1078
 
 
1079
 <refentry id="CONTRIB-DBLINK-ERROR-MESSAGE">
 
1080
  <refmeta>
 
1081
   <refentrytitle>dblink_error_message</refentrytitle>
 
1082
   <manvolnum>3</manvolnum>
 
1083
  </refmeta>
 
1084
 
 
1085
  <refnamediv>
 
1086
   <refname>dblink_error_message</refname>
 
1087
   <refpurpose>gets last error message on the named connection</refpurpose>
 
1088
  </refnamediv>
 
1089
 
 
1090
  <refsynopsisdiv>
 
1091
<synopsis>
 
1092
dblink_error_message(text connname) returns text
 
1093
</synopsis>
 
1094
  </refsynopsisdiv>
 
1095
 
 
1096
  <refsect1>
 
1097
   <title>Description</title>
 
1098
 
 
1099
   <para>
 
1100
    <function>dblink_error_message</> fetches the most recent remote
 
1101
    error message for a given connection.
 
1102
   </para>
 
1103
  </refsect1>
 
1104
 
 
1105
  <refsect1>
 
1106
   <title>Arguments</title>
 
1107
 
 
1108
   <variablelist>
 
1109
    <varlistentry>
 
1110
     <term><parameter>conname</parameter></term>
 
1111
     <listitem>
 
1112
      <para>
 
1113
       Name of the connection to use.
 
1114
      </para>
 
1115
     </listitem>
 
1116
    </varlistentry>
 
1117
   </variablelist>
 
1118
  </refsect1>
 
1119
 
 
1120
  <refsect1>
 
1121
   <title>Return Value</title>
 
1122
 
 
1123
   <para>
 
1124
    Returns last error message, or an empty string if there has been
 
1125
    no error in this connection.
 
1126
   </para>
 
1127
  </refsect1>
 
1128
 
 
1129
  <refsect1>
 
1130
   <title>Example</title>
 
1131
 
 
1132
<programlisting>
 
1133
SELECT dblink_error_message('dtest1');
 
1134
</programlisting>
 
1135
  </refsect1>
 
1136
 </refentry>
 
1137
 
 
1138
 <refentry id="CONTRIB-DBLINK-SEND-QUERY">
 
1139
  <refmeta>
 
1140
   <refentrytitle>dblink_send_query</refentrytitle>
 
1141
   <manvolnum>3</manvolnum>
 
1142
  </refmeta>
 
1143
 
 
1144
  <refnamediv>
 
1145
   <refname>dblink_send_query</refname>
 
1146
   <refpurpose>sends an async query to a remote database</refpurpose>
 
1147
  </refnamediv>
 
1148
 
 
1149
  <refsynopsisdiv>
 
1150
<synopsis>
 
1151
dblink_send_query(text connname, text sql) returns int
 
1152
</synopsis>
 
1153
  </refsynopsisdiv>
 
1154
 
 
1155
  <refsect1>
 
1156
   <title>Description</title>
 
1157
 
 
1158
   <para>
 
1159
    <function>dblink_send_query</> sends a query to be executed
 
1160
    asynchronously, that is, without immediately waiting for the result.
 
1161
    There must not be an async query already in progress on the
 
1162
    connection.
 
1163
   </para>
 
1164
 
 
1165
   <para>
 
1166
    After successfully dispatching an async query, completion status
 
1167
    can be checked with <function>dblink_is_busy</>, and the results
 
1168
    are ultimately collected with <function>dblink_get_result</>.
 
1169
    It is also possible to attempt to cancel an active async query
 
1170
    using <function>dblink_cancel_query</>.
 
1171
   </para>
 
1172
  </refsect1>
 
1173
 
 
1174
  <refsect1>
 
1175
   <title>Arguments</title>
 
1176
 
 
1177
   <variablelist>
 
1178
    <varlistentry>
 
1179
     <term><parameter>conname</parameter></term>
 
1180
     <listitem>
 
1181
      <para>
 
1182
       Name of the connection to use.
 
1183
      </para>
 
1184
     </listitem>
 
1185
    </varlistentry>
 
1186
 
 
1187
    <varlistentry>
 
1188
     <term><parameter>sql</parameter></term>
 
1189
     <listitem>
 
1190
      <para>
 
1191
       The SQL statement that you wish to execute in the remote database,
 
1192
       for example <literal>select * from pg_class</>.
 
1193
      </para>
 
1194
     </listitem>
 
1195
    </varlistentry>
 
1196
   </variablelist>
 
1197
  </refsect1>
 
1198
 
 
1199
  <refsect1>
 
1200
   <title>Return Value</title>
 
1201
 
 
1202
   <para>
 
1203
    Returns 1 if the query was successfully dispatched, 0 otherwise.
 
1204
   </para>
 
1205
  </refsect1>
 
1206
 
 
1207
  <refsect1>
 
1208
   <title>Example</title>
 
1209
 
 
1210
<programlisting>
 
1211
SELECT dblink_send_query('dtest1', 'SELECT * FROM foo WHERE f1 &lt; 3');
 
1212
</programlisting>
 
1213
  </refsect1>
 
1214
 </refentry>
 
1215
 
 
1216
 <refentry id="CONTRIB-DBLINK-IS-BUSY">
 
1217
  <refmeta>
 
1218
   <refentrytitle>dblink_is_busy</refentrytitle>
 
1219
   <manvolnum>3</manvolnum>
 
1220
  </refmeta>
 
1221
 
 
1222
  <refnamediv>
 
1223
   <refname>dblink_is_busy</refname>
 
1224
   <refpurpose>checks if connection is busy with an async query</refpurpose>
 
1225
  </refnamediv>
 
1226
 
 
1227
  <refsynopsisdiv>
 
1228
<synopsis>
 
1229
dblink_is_busy(text connname) returns int
 
1230
</synopsis>
 
1231
  </refsynopsisdiv>
 
1232
 
 
1233
  <refsect1>
 
1234
   <title>Description</title>
 
1235
 
 
1236
   <para>
 
1237
    <function>dblink_is_busy</> tests whether an async query is in progress.
 
1238
   </para>
 
1239
  </refsect1>
 
1240
 
 
1241
  <refsect1>
 
1242
   <title>Arguments</title>
 
1243
 
 
1244
   <variablelist>
 
1245
    <varlistentry>
 
1246
     <term><parameter>conname</parameter></term>
 
1247
     <listitem>
 
1248
      <para>
 
1249
       Name of the connection to check.
 
1250
      </para>
 
1251
     </listitem>
 
1252
    </varlistentry>
 
1253
   </variablelist>
 
1254
  </refsect1>
 
1255
 
 
1256
  <refsect1>
 
1257
   <title>Return Value</title>
 
1258
 
 
1259
   <para>
 
1260
    Returns 1 if connection is busy, 0 if it is not busy.
 
1261
    If this function returns 0, it is guaranteed that
 
1262
    <function>dblink_get_result</> will not block.
 
1263
   </para>
 
1264
  </refsect1>
 
1265
 
 
1266
  <refsect1>
 
1267
   <title>Example</title>
 
1268
 
 
1269
<programlisting>
 
1270
SELECT dblink_is_busy('dtest1');
 
1271
</programlisting>
 
1272
  </refsect1>
 
1273
 </refentry>
 
1274
 
 
1275
 <refentry id="CONTRIB-DBLINK-GET-NOTIFY">
 
1276
  <refmeta>
 
1277
   <refentrytitle>dblink_get_notify</refentrytitle>
 
1278
   <manvolnum>3</manvolnum>
 
1279
  </refmeta>
 
1280
 
 
1281
  <refnamediv>
 
1282
   <refname>dblink_get_notify</refname>
 
1283
   <refpurpose>retrieve async notifications on a connection</refpurpose>
 
1284
  </refnamediv>
 
1285
 
 
1286
  <refsynopsisdiv>
 
1287
<synopsis>
 
1288
dblink_get_notify() returns setof (notify_name text, be_pid int, extra text)
 
1289
dblink_get_notify(text connname) returns setof (notify_name text, be_pid int, extra text)
 
1290
</synopsis>
 
1291
  </refsynopsisdiv>
 
1292
 
 
1293
  <refsect1>
 
1294
   <title>Description</title>
 
1295
 
 
1296
   <para>
 
1297
    <function>dblink_get_notify</> retrieves notifications on either
 
1298
    the unnamed connection, or on a named connection if specified.
 
1299
    To receive notifications via dblink, <function>LISTEN</> must
 
1300
    first be issued, using <function>dblink_exec</>.
 
1301
    For details see <xref linkend="sql-listen"> and <xref linkend="sql-notify">.
 
1302
   </para>
 
1303
 
 
1304
  </refsect1>
 
1305
 
 
1306
  <refsect1>
 
1307
   <title>Arguments</title>
 
1308
 
 
1309
   <variablelist>
 
1310
    <varlistentry>
 
1311
     <term><parameter>conname</parameter></term>
 
1312
     <listitem>
 
1313
      <para>
 
1314
       The name of a named connection to get notifications on.
 
1315
      </para>
 
1316
     </listitem>
 
1317
    </varlistentry>
 
1318
   </variablelist>
 
1319
  </refsect1>
 
1320
 
 
1321
  <refsect1>
 
1322
   <title>Return Value</title>
 
1323
    <para>Returns <type>setof (notify_name text, be_pid int, extra text)</type>, or an empty set if none.</para>
 
1324
  </refsect1>
 
1325
 
 
1326
  <refsect1>
 
1327
   <title>Example</title>
 
1328
 
 
1329
<screen>
 
1330
SELECT dblink_exec('LISTEN virtual');
 
1331
 dblink_exec 
 
1332
-------------
 
1333
 LISTEN
 
1334
(1 row)
 
1335
 
 
1336
SELECT * FROM dblink_get_notify();
 
1337
 notify_name | be_pid | extra
 
1338
-------------+--------+-------
 
1339
(0 rows)
 
1340
 
 
1341
NOTIFY virtual;
 
1342
NOTIFY
 
1343
 
 
1344
SELECT * FROM dblink_get_notify();
 
1345
 notify_name | be_pid | extra
 
1346
-------------+--------+-------
 
1347
 virtual     |   1229 |
 
1348
(1 row)
 
1349
</screen>
 
1350
  </refsect1>
 
1351
 </refentry>
 
1352
 
 
1353
 <refentry id="CONTRIB-DBLINK-GET-RESULT">
 
1354
  <refmeta>
 
1355
   <refentrytitle>dblink_get_result</refentrytitle>
 
1356
   <manvolnum>3</manvolnum>
 
1357
  </refmeta>
 
1358
 
 
1359
  <refnamediv>
 
1360
   <refname>dblink_get_result</refname>
 
1361
   <refpurpose>gets an async query result</refpurpose>
 
1362
  </refnamediv>
 
1363
 
 
1364
  <refsynopsisdiv>
 
1365
<synopsis>
 
1366
dblink_get_result(text connname [, bool fail_on_error]) returns setof record
 
1367
</synopsis>
 
1368
  </refsynopsisdiv>
 
1369
 
 
1370
  <refsect1>
 
1371
   <title>Description</title>
 
1372
 
 
1373
   <para>
 
1374
    <function>dblink_get_result</> collects the results of an
 
1375
    asynchronous query previously sent with <function>dblink_send_query</>.
 
1376
    If the query is not already completed, <function>dblink_get_result</>
 
1377
    will wait until it is.
 
1378
   </para>
 
1379
  </refsect1>
 
1380
 
 
1381
  <refsect1>
 
1382
   <title>Arguments</title>
 
1383
 
 
1384
   <variablelist>
 
1385
    <varlistentry>
 
1386
     <term><parameter>conname</parameter></term>
 
1387
     <listitem>
 
1388
      <para>
 
1389
       Name of the connection to use.
 
1390
      </para>
 
1391
     </listitem>
 
1392
    </varlistentry>
 
1393
 
 
1394
    <varlistentry>
 
1395
     <term><parameter>fail_on_error</parameter></term>
 
1396
     <listitem>
 
1397
      <para>
 
1398
       If true (the default when omitted) then an error thrown on the
 
1399
       remote side of the connection causes an error to also be thrown
 
1400
       locally. If false, the remote error is locally reported as a NOTICE,
 
1401
       and the function returns no rows.
 
1402
      </para>
 
1403
     </listitem>
 
1404
    </varlistentry>
 
1405
   </variablelist>
 
1406
  </refsect1>
 
1407
 
 
1408
  <refsect1>
 
1409
   <title>Return Value</title>
 
1410
 
 
1411
   <para>
 
1412
    For an async query (that is, a SQL statement returning rows),
 
1413
    the function returns the row(s) produced by the query.  To use this
 
1414
    function, you will need to specify the expected set of columns,
 
1415
    as previously discussed for <function>dblink</>.
 
1416
   </para>
 
1417
 
 
1418
   <para>
 
1419
    For an async command (that is, a SQL statement not returning rows),
 
1420
    the function returns a single row with a single text column containing
 
1421
    the command's status string.  It is still necessary to specify that
 
1422
    the result will have a single text column in the calling <literal>FROM</>
 
1423
    clause.
 
1424
   </para>
 
1425
  </refsect1>
 
1426
 
 
1427
  <refsect1>
 
1428
   <title>Notes</title>
 
1429
 
 
1430
   <para>
 
1431
    This function <emphasis>must</> be called if
 
1432
    <function>dblink_send_query</> returned 1.
 
1433
    It must be called once for each query
 
1434
    sent, and one additional time to obtain an empty set result,
 
1435
    before the connection can be used again.
 
1436
   </para>
 
1437
  </refsect1>
 
1438
 
 
1439
  <refsect1>
 
1440
   <title>Example</title>
 
1441
 
 
1442
<screen>
 
1443
contrib_regression=# SELECT dblink_connect('dtest1', 'dbname=contrib_regression');
 
1444
 dblink_connect
 
1445
----------------
 
1446
 OK
 
1447
(1 row)
 
1448
 
 
1449
contrib_regression=# SELECT * FROM
 
1450
contrib_regression-# dblink_send_query('dtest1', 'select * from foo where f1 &lt; 3') AS t1;
 
1451
 t1
 
1452
----
 
1453
  1
 
1454
(1 row)
 
1455
 
 
1456
contrib_regression=# SELECT * FROM dblink_get_result('dtest1') AS t1(f1 int, f2 text, f3 text[]);
 
1457
 f1 | f2 |     f3
 
1458
----+----+------------
 
1459
  0 | a  | {a0,b0,c0}
 
1460
  1 | b  | {a1,b1,c1}
 
1461
  2 | c  | {a2,b2,c2}
 
1462
(3 rows)
 
1463
 
 
1464
contrib_regression=# SELECT * FROM dblink_get_result('dtest1') AS t1(f1 int, f2 text, f3 text[]);
 
1465
 f1 | f2 | f3
 
1466
----+----+----
 
1467
(0 rows)
 
1468
 
 
1469
contrib_regression=# SELECT * FROM
 
1470
contrib_regression-# dblink_send_query('dtest1', 'select * from foo where f1 &lt; 3; select * from foo where f1 &gt; 6') AS t1;
 
1471
 t1
 
1472
----
 
1473
  1
 
1474
(1 row)
 
1475
 
 
1476
contrib_regression=# SELECT * FROM dblink_get_result('dtest1') AS t1(f1 int, f2 text, f3 text[]);
 
1477
 f1 | f2 |     f3
 
1478
----+----+------------
 
1479
  0 | a  | {a0,b0,c0}
 
1480
  1 | b  | {a1,b1,c1}
 
1481
  2 | c  | {a2,b2,c2}
 
1482
(3 rows)
 
1483
 
 
1484
contrib_regression=# SELECT * FROM dblink_get_result('dtest1') AS t1(f1 int, f2 text, f3 text[]);
 
1485
 f1 | f2 |      f3
 
1486
----+----+---------------
 
1487
  7 | h  | {a7,b7,c7}
 
1488
  8 | i  | {a8,b8,c8}
 
1489
  9 | j  | {a9,b9,c9}
 
1490
 10 | k  | {a10,b10,c10}
 
1491
(4 rows)
 
1492
 
 
1493
contrib_regression=# SELECT * FROM dblink_get_result('dtest1') AS t1(f1 int, f2 text, f3 text[]);
 
1494
 f1 | f2 | f3
 
1495
----+----+----
 
1496
(0 rows)
 
1497
</screen>
 
1498
  </refsect1>
 
1499
 </refentry>
 
1500
 
 
1501
 <refentry id="CONTRIB-DBLINK-CANCEL-QUERY">
 
1502
  <refmeta>
 
1503
   <refentrytitle>dblink_cancel_query</refentrytitle>
 
1504
   <manvolnum>3</manvolnum>
 
1505
  </refmeta>
 
1506
 
 
1507
  <refnamediv>
 
1508
   <refname>dblink_cancel_query</refname>
 
1509
   <refpurpose>cancels any active query on the named connection</refpurpose>
 
1510
  </refnamediv>
 
1511
 
 
1512
  <refsynopsisdiv>
 
1513
<synopsis>
 
1514
dblink_cancel_query(text connname) returns text
 
1515
</synopsis>
 
1516
  </refsynopsisdiv>
 
1517
 
 
1518
  <refsect1>
 
1519
   <title>Description</title>
 
1520
 
 
1521
   <para>
 
1522
    <function>dblink_cancel_query</> attempts to cancel any query that
 
1523
    is in progress on the named connection.  Note that this is not
 
1524
    certain to succeed (since, for example, the remote query might
 
1525
    already have finished).  A cancel request simply improves the
 
1526
    odds that the query will fail soon.  You must still complete the
 
1527
    normal query protocol, for example by calling
 
1528
    <function>dblink_get_result</>.
 
1529
   </para>
 
1530
  </refsect1>
 
1531
 
 
1532
  <refsect1>
 
1533
   <title>Arguments</title>
 
1534
 
 
1535
   <variablelist>
 
1536
    <varlistentry>
 
1537
     <term><parameter>conname</parameter></term>
 
1538
     <listitem>
 
1539
      <para>
 
1540
       Name of the connection to use.
 
1541
      </para>
 
1542
     </listitem>
 
1543
    </varlistentry>
 
1544
   </variablelist>
 
1545
  </refsect1>
 
1546
 
 
1547
  <refsect1>
 
1548
   <title>Return Value</title>
 
1549
 
 
1550
   <para>
 
1551
    Returns <literal>OK</> if the cancel request has been sent, or
 
1552
    the text of an error message on failure.
 
1553
   </para>
 
1554
  </refsect1>
 
1555
 
 
1556
  <refsect1>
 
1557
   <title>Example</title>
 
1558
 
 
1559
<programlisting>
 
1560
SELECT dblink_cancel_query('dtest1');
 
1561
</programlisting>
 
1562
  </refsect1>
 
1563
 </refentry>
 
1564
 
 
1565
 <refentry id="CONTRIB-DBLINK-GET-PKEY">
 
1566
  <refmeta>
 
1567
   <refentrytitle>dblink_get_pkey</refentrytitle>
 
1568
   <manvolnum>3</manvolnum>
 
1569
  </refmeta>
 
1570
 
 
1571
  <refnamediv>
 
1572
   <refname>dblink_get_pkey</refname>
 
1573
   <refpurpose>returns the positions and field names of a relation's
 
1574
    primary key fields
 
1575
   </refpurpose>
 
1576
  </refnamediv>
 
1577
 
 
1578
  <refsynopsisdiv>
 
1579
<synopsis>
 
1580
dblink_get_pkey(text relname) returns setof dblink_pkey_results
 
1581
</synopsis>
 
1582
  </refsynopsisdiv>
 
1583
 
 
1584
  <refsect1>
 
1585
   <title>Description</title>
 
1586
 
 
1587
   <para>
 
1588
    <function>dblink_get_pkey</> provides information about the primary
 
1589
    key of a relation in the local database.  This is sometimes useful
 
1590
    in generating queries to be sent to remote databases.
 
1591
   </para>
 
1592
  </refsect1>
 
1593
 
 
1594
  <refsect1>
 
1595
   <title>Arguments</title>
 
1596
 
 
1597
   <variablelist>
 
1598
    <varlistentry>
 
1599
     <term><parameter>relname</parameter></term>
 
1600
     <listitem>
 
1601
      <para>
 
1602
       Name of a local relation, for example <literal>foo</> or
 
1603
       <literal>myschema.mytab</>.  Include double quotes if the
 
1604
       name is mixed-case or contains special characters, for
 
1605
       example <literal>"FooBar"</>; without quotes, the string
 
1606
       will be folded to lower case.
 
1607
      </para>
 
1608
     </listitem>
 
1609
    </varlistentry>
 
1610
   </variablelist>
 
1611
  </refsect1>
 
1612
 
 
1613
  <refsect1>
 
1614
   <title>Return Value</title>
 
1615
 
 
1616
   <para>
 
1617
    Returns one row for each primary key field, or no rows if the relation
 
1618
    has no primary key.  The result row type is defined as
 
1619
 
 
1620
<programlisting>
 
1621
CREATE TYPE dblink_pkey_results AS (position int, colname text);
 
1622
</programlisting>
 
1623
 
 
1624
    The <literal>position</> column simply runs from 1 to <replaceable>N</>;
 
1625
    it is the number of the field within the primary key, not the number
 
1626
    within the table's columns.
 
1627
   </para>
 
1628
  </refsect1>
 
1629
 
 
1630
  <refsect1>
 
1631
   <title>Example</title>
 
1632
 
 
1633
<screen>
 
1634
CREATE TABLE foobar (
 
1635
    f1 int,
 
1636
    f2 int,
 
1637
    f3 int,
 
1638
    PRIMARY KEY (f1, f2, f3)
 
1639
);
 
1640
CREATE TABLE
 
1641
 
 
1642
SELECT * FROM dblink_get_pkey('foobar');
 
1643
 position | colname
 
1644
----------+---------
 
1645
        1 | f1
 
1646
        2 | f2
 
1647
        3 | f3
 
1648
(3 rows)
 
1649
</screen>
 
1650
  </refsect1>
 
1651
 </refentry>
 
1652
 
 
1653
 <refentry id="CONTRIB-DBLINK-BUILD-SQL-INSERT">
 
1654
  <refmeta>
 
1655
   <refentrytitle>dblink_build_sql_insert</refentrytitle>
 
1656
   <manvolnum>3</manvolnum>
 
1657
  </refmeta>
 
1658
 
 
1659
  <refnamediv>
 
1660
   <refname>dblink_build_sql_insert</refname>
 
1661
   <refpurpose>
 
1662
    builds an INSERT statement using a local tuple, replacing the
 
1663
    primary key field values with alternative supplied values
 
1664
   </refpurpose>
 
1665
  </refnamediv>
 
1666
 
 
1667
  <refsynopsisdiv>
 
1668
<synopsis>
 
1669
dblink_build_sql_insert(text relname,
 
1670
                        int2vector primary_key_attnums,
 
1671
                        integer num_primary_key_atts,
 
1672
                        text[] src_pk_att_vals_array,
 
1673
                        text[] tgt_pk_att_vals_array) returns text
 
1674
</synopsis>
 
1675
  </refsynopsisdiv>
 
1676
 
 
1677
  <refsect1>
 
1678
   <title>Description</title>
 
1679
 
 
1680
   <para>
 
1681
    <function>dblink_build_sql_insert</> can be useful in doing selective
 
1682
    replication of a local table to a remote database.  It selects a row
 
1683
    from the local table based on primary key, and then builds a SQL
 
1684
    <command>INSERT</> command that will duplicate that row, but with
 
1685
    the primary key values replaced by the values in the last argument.
 
1686
    (To make an exact copy of the row, just specify the same values for
 
1687
    the last two arguments.)
 
1688
   </para>
 
1689
  </refsect1>
 
1690
 
 
1691
  <refsect1>
 
1692
   <title>Arguments</title>
 
1693
 
 
1694
   <variablelist>
 
1695
    <varlistentry>
 
1696
     <term><parameter>relname</parameter></term>
 
1697
     <listitem>
 
1698
      <para>
 
1699
       Name of a local relation, for example <literal>foo</> or
 
1700
       <literal>myschema.mytab</>.  Include double quotes if the
 
1701
       name is mixed-case or contains special characters, for
 
1702
       example <literal>"FooBar"</>; without quotes, the string
 
1703
       will be folded to lower case.
 
1704
      </para>
 
1705
     </listitem>
 
1706
    </varlistentry>
 
1707
 
 
1708
    <varlistentry>
 
1709
     <term><parameter>primary_key_attnums</parameter></term>
 
1710
     <listitem>
 
1711
      <para>
 
1712
       Attribute numbers (1-based) of the primary key fields,
 
1713
       for example <literal>1 2</>.
 
1714
      </para>
 
1715
     </listitem>
 
1716
    </varlistentry>
 
1717
 
 
1718
    <varlistentry>
 
1719
     <term><parameter>num_primary_key_atts</parameter></term>
 
1720
     <listitem>
 
1721
      <para>
 
1722
       The number of primary key fields.
 
1723
      </para>
 
1724
     </listitem>
 
1725
    </varlistentry>
 
1726
 
 
1727
    <varlistentry>
 
1728
     <term><parameter>src_pk_att_vals_array</parameter></term>
 
1729
     <listitem>
 
1730
      <para>
 
1731
       Values of the primary key fields to be used to look up the
 
1732
       local tuple.  Each field is represented in text form.
 
1733
       An error is thrown if there is no local row with these
 
1734
       primary key values.
 
1735
      </para>
 
1736
     </listitem>
 
1737
    </varlistentry>
 
1738
 
 
1739
    <varlistentry>
 
1740
     <term><parameter>tgt_pk_att_vals_array</parameter></term>
 
1741
     <listitem>
 
1742
      <para>
 
1743
       Values of the primary key fields to be placed in the resulting
 
1744
       <command>INSERT</> command.  Each field is represented in text form.
 
1745
      </para>
 
1746
     </listitem>
 
1747
    </varlistentry>
 
1748
   </variablelist>
 
1749
  </refsect1>
 
1750
 
 
1751
  <refsect1>
 
1752
   <title>Return Value</title>
 
1753
 
 
1754
   <para>Returns the requested SQL statement as text.</para>
 
1755
  </refsect1>
 
1756
 
 
1757
  <refsect1>
 
1758
   <title>Notes</title>
 
1759
 
 
1760
   <para>
 
1761
    As of <productname>PostgreSQL</> 9.0, the attribute numbers in
 
1762
    <parameter>primary_key_attnums</parameter> are interpreted as logical
 
1763
    column numbers, corresponding to the column's position in
 
1764
    <literal>SELECT * FROM relname</>.  Previous versions interpreted the
 
1765
    numbers as physical column positions.  There is a difference if any
 
1766
    column(s) to the left of the indicated column have been dropped during
 
1767
    the lifetime of the table.
 
1768
   </para>
 
1769
  </refsect1>
 
1770
 
 
1771
  <refsect1>
 
1772
   <title>Example</title>
 
1773
 
 
1774
<screen>
 
1775
SELECT dblink_build_sql_insert('foo', '1 2', 2, '{"1", "a"}', '{"1", "b''a"}');
 
1776
             dblink_build_sql_insert
 
1777
--------------------------------------------------
 
1778
 INSERT INTO foo(f1,f2,f3) VALUES('1','b''a','1')
 
1779
(1 row)
 
1780
</screen>
 
1781
  </refsect1>
 
1782
 </refentry>
 
1783
 
 
1784
 <refentry id="CONTRIB-DBLINK-BUILD-SQL-DELETE">
 
1785
  <refmeta>
 
1786
   <refentrytitle>dblink_build_sql_delete</refentrytitle>
 
1787
   <manvolnum>3</manvolnum>
 
1788
  </refmeta>
 
1789
 
 
1790
  <refnamediv>
 
1791
   <refname>dblink_build_sql_delete</refname>
 
1792
   <refpurpose>builds a DELETE statement using supplied values for primary
 
1793
    key field values
 
1794
   </refpurpose>
 
1795
  </refnamediv>
 
1796
 
 
1797
  <refsynopsisdiv>
 
1798
<synopsis>
 
1799
dblink_build_sql_delete(text relname,
 
1800
                        int2vector primary_key_attnums,
 
1801
                        integer num_primary_key_atts,
 
1802
                        text[] tgt_pk_att_vals_array) returns text
 
1803
</synopsis>
 
1804
  </refsynopsisdiv>
 
1805
 
 
1806
  <refsect1>
 
1807
   <title>Description</title>
 
1808
 
 
1809
   <para>
 
1810
    <function>dblink_build_sql_delete</> can be useful in doing selective
 
1811
    replication of a local table to a remote database.  It builds a SQL
 
1812
    <command>DELETE</> command that will delete the row with the given
 
1813
    primary key values.
 
1814
   </para>
 
1815
  </refsect1>
 
1816
 
 
1817
  <refsect1>
 
1818
   <title>Arguments</title>
 
1819
 
 
1820
   <variablelist>
 
1821
    <varlistentry>
 
1822
     <term><parameter>relname</parameter></term>
 
1823
     <listitem>
 
1824
      <para>
 
1825
       Name of a local relation, for example <literal>foo</> or
 
1826
       <literal>myschema.mytab</>.  Include double quotes if the
 
1827
       name is mixed-case or contains special characters, for
 
1828
       example <literal>"FooBar"</>; without quotes, the string
 
1829
       will be folded to lower case.
 
1830
      </para>
 
1831
     </listitem>
 
1832
    </varlistentry>
 
1833
 
 
1834
    <varlistentry>
 
1835
     <term><parameter>primary_key_attnums</parameter></term>
 
1836
     <listitem>
 
1837
      <para>
 
1838
       Attribute numbers (1-based) of the primary key fields,
 
1839
       for example <literal>1 2</>.
 
1840
      </para>
 
1841
     </listitem>
 
1842
    </varlistentry>
 
1843
 
 
1844
    <varlistentry>
 
1845
     <term><parameter>num_primary_key_atts</parameter></term>
 
1846
     <listitem>
 
1847
      <para>
 
1848
       The number of primary key fields.
 
1849
      </para>
 
1850
     </listitem>
 
1851
    </varlistentry>
 
1852
 
 
1853
    <varlistentry>
 
1854
     <term><parameter>tgt_pk_att_vals_array</parameter></term>
 
1855
     <listitem>
 
1856
      <para>
 
1857
       Values of the primary key fields to be used in the resulting
 
1858
       <command>DELETE</> command.  Each field is represented in text form.
 
1859
      </para>
 
1860
     </listitem>
 
1861
    </varlistentry>
 
1862
   </variablelist>
 
1863
  </refsect1>
 
1864
 
 
1865
  <refsect1>
 
1866
   <title>Return Value</title>
 
1867
 
 
1868
   <para>Returns the requested SQL statement as text.</para>
 
1869
  </refsect1>
 
1870
 
 
1871
  <refsect1>
 
1872
   <title>Notes</title>
 
1873
 
 
1874
   <para>
 
1875
    As of <productname>PostgreSQL</> 9.0, the attribute numbers in
 
1876
    <parameter>primary_key_attnums</parameter> are interpreted as logical
 
1877
    column numbers, corresponding to the column's position in
 
1878
    <literal>SELECT * FROM relname</>.  Previous versions interpreted the
 
1879
    numbers as physical column positions.  There is a difference if any
 
1880
    column(s) to the left of the indicated column have been dropped during
 
1881
    the lifetime of the table.
 
1882
   </para>
 
1883
  </refsect1>
 
1884
 
 
1885
  <refsect1>
 
1886
   <title>Example</title>
 
1887
 
 
1888
<screen>
 
1889
SELECT dblink_build_sql_delete('"MyFoo"', '1 2', 2, '{"1", "b"}');
 
1890
           dblink_build_sql_delete
 
1891
---------------------------------------------
 
1892
 DELETE FROM "MyFoo" WHERE f1='1' AND f2='b'
 
1893
(1 row)
 
1894
</screen>
 
1895
  </refsect1>
 
1896
 </refentry>
 
1897
 
 
1898
 <refentry id="CONTRIB-DBLINK-BUILD-SQL-UPDATE">
 
1899
  <refmeta>
 
1900
   <refentrytitle>dblink_build_sql_update</refentrytitle>
 
1901
   <manvolnum>3</manvolnum>
 
1902
  </refmeta>
 
1903
 
 
1904
  <refnamediv>
 
1905
   <refname>dblink_build_sql_update</refname>
 
1906
   <refpurpose>builds an UPDATE statement using a local tuple, replacing
 
1907
    the primary key field values with alternative supplied values
 
1908
   </refpurpose>
 
1909
  </refnamediv>
 
1910
 
 
1911
  <refsynopsisdiv>
 
1912
<synopsis>
 
1913
dblink_build_sql_update(text relname,
 
1914
                        int2vector primary_key_attnums,
 
1915
                        integer num_primary_key_atts,
 
1916
                        text[] src_pk_att_vals_array,
 
1917
                        text[] tgt_pk_att_vals_array) returns text
 
1918
</synopsis>
 
1919
  </refsynopsisdiv>
 
1920
 
 
1921
  <refsect1>
 
1922
   <title>Description</title>
 
1923
 
 
1924
   <para>
 
1925
    <function>dblink_build_sql_update</> can be useful in doing selective
 
1926
    replication of a local table to a remote database.  It selects a row
 
1927
    from the local table based on primary key, and then builds a SQL
 
1928
    <command>UPDATE</> command that will duplicate that row, but with
 
1929
    the primary key values replaced by the values in the last argument.
 
1930
    (To make an exact copy of the row, just specify the same values for
 
1931
    the last two arguments.)  The <command>UPDATE</> command always assigns
 
1932
    all fields of the row &mdash; the main difference between this and
 
1933
    <function>dblink_build_sql_insert</> is that it's assumed that
 
1934
    the target row already exists in the remote table.
 
1935
   </para>
 
1936
  </refsect1>
 
1937
 
 
1938
  <refsect1>
 
1939
   <title>Arguments</title>
 
1940
 
 
1941
   <variablelist>
 
1942
    <varlistentry>
 
1943
     <term><parameter>relname</parameter></term>
 
1944
     <listitem>
 
1945
      <para>
 
1946
       Name of a local relation, for example <literal>foo</> or
 
1947
       <literal>myschema.mytab</>.  Include double quotes if the
 
1948
       name is mixed-case or contains special characters, for
 
1949
       example <literal>"FooBar"</>; without quotes, the string
 
1950
       will be folded to lower case.
 
1951
      </para>
 
1952
     </listitem>
 
1953
    </varlistentry>
 
1954
 
 
1955
    <varlistentry>
 
1956
     <term><parameter>primary_key_attnums</parameter></term>
 
1957
     <listitem>
 
1958
      <para>
 
1959
       Attribute numbers (1-based) of the primary key fields,
 
1960
       for example <literal>1 2</>.
 
1961
      </para>
 
1962
     </listitem>
 
1963
    </varlistentry>
 
1964
 
 
1965
    <varlistentry>
 
1966
     <term><parameter>num_primary_key_atts</parameter></term>
 
1967
     <listitem>
 
1968
      <para>
 
1969
       The number of primary key fields.
 
1970
      </para>
 
1971
     </listitem>
 
1972
    </varlistentry>
 
1973
 
 
1974
    <varlistentry>
 
1975
     <term><parameter>src_pk_att_vals_array</parameter></term>
 
1976
     <listitem>
 
1977
      <para>
 
1978
       Values of the primary key fields to be used to look up the
 
1979
       local tuple.  Each field is represented in text form.
 
1980
       An error is thrown if there is no local row with these
 
1981
       primary key values.
 
1982
      </para>
 
1983
     </listitem>
 
1984
    </varlistentry>
 
1985
 
 
1986
    <varlistentry>
 
1987
     <term><parameter>tgt_pk_att_vals_array</parameter></term>
 
1988
     <listitem>
 
1989
      <para>
 
1990
       Values of the primary key fields to be placed in the resulting
 
1991
       <command>UPDATE</> command.  Each field is represented in text form.
 
1992
      </para>
 
1993
     </listitem>
 
1994
    </varlistentry>
 
1995
   </variablelist>
 
1996
  </refsect1>
 
1997
 
 
1998
  <refsect1>
 
1999
   <title>Return Value</title>
 
2000
 
 
2001
   <para>Returns the requested SQL statement as text.</para>
 
2002
  </refsect1>
 
2003
 
 
2004
  <refsect1>
 
2005
   <title>Notes</title>
 
2006
 
 
2007
   <para>
 
2008
    As of <productname>PostgreSQL</> 9.0, the attribute numbers in
 
2009
    <parameter>primary_key_attnums</parameter> are interpreted as logical
 
2010
    column numbers, corresponding to the column's position in
 
2011
    <literal>SELECT * FROM relname</>.  Previous versions interpreted the
 
2012
    numbers as physical column positions.  There is a difference if any
 
2013
    column(s) to the left of the indicated column have been dropped during
 
2014
    the lifetime of the table.
 
2015
   </para>
 
2016
  </refsect1>
 
2017
 
 
2018
  <refsect1>
 
2019
   <title>Example</title>
 
2020
 
 
2021
<screen>
 
2022
SELECT dblink_build_sql_update('foo', '1 2', 2, '{"1", "a"}', '{"1", "b"}');
 
2023
                   dblink_build_sql_update
 
2024
-------------------------------------------------------------
 
2025
 UPDATE foo SET f1='1',f2='b',f3='1' WHERE f1='1' AND f2='b'
 
2026
(1 row)
 
2027
</screen>
 
2028
  </refsect1>
 
2029
 </refentry>
 
2030
 
 
2031
</sect1>