Home Explore Blog CI



postgresql

8th chunk of `doc/src/sgml/dblink.sgml`
1a7aa868f080b6c7cd27efcd5d679bcf264259db65f42d790000000100000fa8
  <title>Examples</title>

<screen>
SELECT dblink_connect('dbname=dblink_test_standby');
 dblink_connect
----------------
 OK
(1 row)

SELECT dblink_exec('insert into foo values(21, ''z'', ''{"a0","b0","c0"}'');');
   dblink_exec
-----------------
 INSERT 943366 1
(1 row)

SELECT dblink_connect('myconn', 'dbname=regression');
 dblink_connect
----------------
 OK
(1 row)

SELECT dblink_exec('myconn', 'insert into foo values(21, ''z'', ''{"a0","b0","c0"}'');');
   dblink_exec
------------------
 INSERT 6432584 1
(1 row)

SELECT dblink_exec('myconn', 'insert into pg_class values (''foo'')',false);
NOTICE:  sql error
DETAIL:  ERROR:  null value in column "relnamespace" violates not-null constraint

 dblink_exec
-------------
 ERROR
(1 row)
</screen>
  </refsect1>
 </refentry>

 <refentry id="contrib-dblink-open">
  <indexterm>
   <primary>dblink_open</primary>
  </indexterm>

  <refmeta>
   <refentrytitle>dblink_open</refentrytitle>
   <manvolnum>3</manvolnum>
  </refmeta>

  <refnamediv>
   <refname>dblink_open</refname>
   <refpurpose>opens a cursor in a remote database</refpurpose>
  </refnamediv>

  <refsynopsisdiv>
<synopsis>
dblink_open(text cursorname, text sql [, bool fail_on_error]) returns text
dblink_open(text connname, text cursorname, text sql [, bool fail_on_error]) returns text
</synopsis>
  </refsynopsisdiv>

  <refsect1>
   <title>Description</title>

   <para>
    <function>dblink_open()</function> opens a cursor in a remote database.
    The cursor can subsequently be manipulated with
    <function>dblink_fetch()</function> and <function>dblink_close()</function>.
   </para>
  </refsect1>

  <refsect1>
   <title>Arguments</title>

   <variablelist>
    <varlistentry>
     <term><parameter>connname</parameter></term>
     <listitem>
      <para>
       Name of the connection to use; omit this parameter to use the
       unnamed connection.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry>
     <term><parameter>cursorname</parameter></term>
     <listitem>
      <para>
       The name to assign to this cursor.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry>
     <term><parameter>sql</parameter></term>
     <listitem>
      <para>
       The <command>SELECT</command> statement that you wish to execute in the remote
       database, for example <literal>select * from pg_class</literal>.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry>
     <term><parameter>fail_on_error</parameter></term>
     <listitem>
      <para>
       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's return value is set to <literal>ERROR</literal>.
      </para>
     </listitem>
    </varlistentry>
   </variablelist>
  </refsect1>

  <refsect1>
   <title>Return Value</title>

   <para>
    Returns status, either <literal>OK</literal> or <literal>ERROR</literal>.
   </para>
  </refsect1>

  <refsect1>
   <title>Notes</title>

   <para>
    Since a cursor can only persist within a transaction,
    <function>dblink_open</function> starts an explicit transaction block
    (<command>BEGIN</command>) on the remote side, if the remote side was
    not already within a transaction.  This transaction will be
    closed again when the matching <function>dblink_close</function> is
    executed.  Note that if
    you use <function>dblink_exec</function> to change data between
    <function>dblink_open</function> and <function>dblink_close</function>,
    and then an error occurs or you use <function>dblink_disconnect</function> before
    <function>dblink_close</function>, your change <emphasis>will be
    lost</emphasis> because the transaction will be aborted.
   </para>
  </refsect1>

  <refsect1>
   <title>Examples</title>

<screen>
SELECT dblink_connect('dbname=postgres options=-csearch_path=');
 dblink_connect

Title: dblink_open: Opening a Cursor in a Remote Database
Summary
This section details the `dblink_open` function, which is used to open a cursor in a remote database. The cursor can then be used by `dblink_fetch()` and `dblink_close()`. The syntax, arguments (`connname`, `cursorname`, `sql`, and `fail_on_error`), and return value are explained. It also includes important notes on how `dblink_open` starts a transaction block on the remote side and the implications for data changes made between `dblink_open` and `dblink_close`. It explains that if disconnection happens before closing a transaction, the changes will be lost.