Home Explore Blog CI



postgresql

17th chunk of `doc/src/sgml/dblink.sgml`
5b5e2f422c4aff81ef5f7c3a3c358343ecff9de8da91d3020000000100000fa3
 number of the field within the primary key, not the number
    within the table's columns.
   </para>
  </refsect1>

  <refsect1>
   <title>Examples</title>

<screen>
CREATE TABLE foobar (
    f1 int,
    f2 int,
    f3 int,
    PRIMARY KEY (f1, f2, f3)
);
CREATE TABLE

SELECT * FROM dblink_get_pkey('foobar');
 position | colname
----------+---------
        1 | f1
        2 | f2
        3 | f3
(3 rows)
</screen>
  </refsect1>
 </refentry>

 <refentry id="contrib-dblink-build-sql-insert">
  <indexterm>
   <primary>dblink_build_sql_insert</primary>
  </indexterm>

  <refmeta>
   <refentrytitle>dblink_build_sql_insert</refentrytitle>
   <manvolnum>3</manvolnum>
  </refmeta>

  <refnamediv>
   <refname>dblink_build_sql_insert</refname>
   <refpurpose>
    builds an INSERT statement using a local tuple, replacing the
    primary key field values with alternative supplied values
   </refpurpose>
  </refnamediv>

  <refsynopsisdiv>
<synopsis>
dblink_build_sql_insert(text relname,
                        int2vector primary_key_attnums,
                        integer num_primary_key_atts,
                        text[] src_pk_att_vals_array,
                        text[] tgt_pk_att_vals_array) returns text
</synopsis>
  </refsynopsisdiv>

  <refsect1>
   <title>Description</title>

   <para>
    <function>dblink_build_sql_insert</function> can be useful in doing selective
    replication of a local table to a remote database.  It selects a row
    from the local table based on primary key, and then builds an SQL
    <command>INSERT</command> command that will duplicate that row, but with
    the primary key values replaced by the values in the last argument.
    (To make an exact copy of the row, just specify the same values for
    the last two arguments.)
   </para>
  </refsect1>

  <refsect1>
   <title>Arguments</title>

   <variablelist>
    <varlistentry>
     <term><parameter>relname</parameter></term>
     <listitem>
      <para>
       Name of a local relation, for example <literal>foo</literal> or
       <literal>myschema.mytab</literal>.  Include double quotes if the
       name is mixed-case or contains special characters, for
       example <literal>"FooBar"</literal>; without quotes, the string
       will be folded to lower case.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry>
     <term><parameter>primary_key_attnums</parameter></term>
     <listitem>
      <para>
       Attribute numbers (1-based) of the primary key fields,
       for example <literal>1 2</literal>.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry>
     <term><parameter>num_primary_key_atts</parameter></term>
     <listitem>
      <para>
       The number of primary key fields.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry>
     <term><parameter>src_pk_att_vals_array</parameter></term>
     <listitem>
      <para>
       Values of the primary key fields to be used to look up the
       local tuple.  Each field is represented in text form.
       An error is thrown if there is no local row with these
       primary key values.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry>
     <term><parameter>tgt_pk_att_vals_array</parameter></term>
     <listitem>
      <para>
       Values of the primary key fields to be placed in the resulting
       <command>INSERT</command> command.  Each field is represented in text form.
      </para>
     </listitem>
    </varlistentry>
   </variablelist>
  </refsect1>

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

   <para>Returns the requested SQL statement as text.</para>
  </refsect1>

  <refsect1>
   <title>Notes</title>

   <para>
    As of <productname>PostgreSQL</productname> 9.0, the attribute numbers in
    <parameter>primary_key_attnums</parameter> are interpreted as logical
    column numbers, corresponding to the column's position in
    <literal>SELECT * FROM relname</literal>.  Previous versions interpreted the
    numbers

Title: dblink_build_sql_insert Function
Summary
This section describes the `dblink_build_sql_insert` function, designed to assist in replicating data from a local table to a remote database. It constructs an SQL INSERT statement based on a row selected from the local table, allowing for the replacement of primary key values with alternative values. The function takes the relation name, primary key attribute numbers, the number of primary key attributes, and arrays of source and target primary key values as input, and returns the generated SQL INSERT statement as text.