Home Explore Blog CI



postgresql

12th chunk of `doc/src/sgml/ref/merge.sgml`
b2fcd44ac38261c96b66fc6d6a119d16017e2335102febc50000000100000be7
 be
   used to specify a consistent ordering, if required, which might be
   needed to avoid deadlocks between concurrent transactions.
  </para>

  <para>
   When <command>MERGE</command> is run concurrently with other commands
   that modify the target table, the usual transaction isolation rules
   apply; see <xref linkend="transaction-iso"/> for an explanation
   on the behavior at each isolation level.
   You may also wish to consider using <command>INSERT ... ON CONFLICT</command>
   as an alternative statement which offers the ability to run an
   <command>UPDATE</command> if a concurrent <command>INSERT</command>
   occurs.  There are a variety of differences and restrictions between
   the two statement types and they are not interchangeable.
  </para>
 </refsect1>

 <refsect1>
  <title>Examples</title>

  <para>
   Perform maintenance on <literal>customer_accounts</literal> based
   upon new <literal>recent_transactions</literal>.

<programlisting>
MERGE INTO customer_account ca
USING recent_transactions t
ON t.customer_id = ca.customer_id
WHEN MATCHED THEN
  UPDATE SET balance = balance + transaction_value
WHEN NOT MATCHED THEN
  INSERT (customer_id, balance)
  VALUES (t.customer_id, t.transaction_value);
</programlisting>
  </para>

  <para>
   Attempt to insert a new stock item along with the quantity of stock. If
   the item already exists, instead update the stock count of the existing
   item. Don't allow entries that have zero stock. Return details of all
   changes made.
<programlisting>
MERGE INTO wines w
USING wine_stock_changes s
ON s.winename = w.winename
WHEN NOT MATCHED AND s.stock_delta > 0 THEN
  INSERT VALUES(s.winename, s.stock_delta)
WHEN MATCHED AND w.stock + s.stock_delta > 0 THEN
  UPDATE SET stock = w.stock + s.stock_delta
WHEN MATCHED THEN
  DELETE
RETURNING merge_action(), w.winename, old.stock AS old_stock, new.stock AS new_stock;
</programlisting>

   The <literal>wine_stock_changes</literal> table might be, for example, a
   temporary table recently loaded into the database.
  </para>

  <para>
   Update <literal>wines</literal> based on a replacement wine list, inserting
   rows for any new stock, updating modified stock entries, and deleting any
   wines not present in the new list.
<programlisting>
MERGE INTO wines w
USING new_wine_list s
ON s.winename = w.winename
WHEN NOT MATCHED BY TARGET THEN
  INSERT VALUES(s.winename, s.stock)
WHEN MATCHED AND w.stock != s.stock THEN
  UPDATE SET stock = s.stock
WHEN NOT MATCHED BY SOURCE THEN
  DELETE;
</programlisting>
  </para>

 </refsect1>

 <refsect1>
  <title>Compatibility</title>
   <para>
    This command conforms to the <acronym>SQL</acronym> standard.
  </para>
   <para>
    The <literal>WITH</literal> clause, <literal>BY SOURCE</literal> and
    <literal>BY TARGET</literal> qualifiers to
    <literal>WHEN NOT MATCHED</literal>, <literal>DO NOTHING</literal> action,
    and <literal>RETURNING</literal> clause are extensions to the
    <acronym>SQL</acronym> standard.
  </para>
 </refsect1>
</refentry>

Title: MERGE Statement Examples and Compatibility
Summary
This section provides examples of the MERGE statement, demonstrating how to maintain a customer accounts table based on recent transactions, insert or update stock items, and update a wine list based on a replacement list. It showcases the use of WHEN MATCHED and WHEN NOT MATCHED clauses for updating, inserting, and deleting data. The examples include the use of RETURNING to display changes made. The section concludes with a note on compatibility with the SQL standard, highlighting extensions like the WITH clause, BY SOURCE/TARGET qualifiers, DO NOTHING action, and RETURNING clause.