A <literal>SELECT</literal> sub-query that produces as many output columns
as are listed in the parenthesized column list preceding it. The
sub-query must yield no more than one row when executed. If it
yields one row, its column values are assigned to the target columns;
if it yields no rows, NULL values are assigned to the target columns.
If used in a <literal>WHEN MATCHED</literal> clause, the sub-query can
refer to values from the original row in the target table, and values
from the <replaceable class="parameter">data_source</replaceable> row.
If used in a <literal>WHEN NOT MATCHED BY SOURCE</literal> clause, the
sub-query can only refer to values from the original row in the target
table.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">output_alias</replaceable></term>
<listitem>
<para>
An optional substitute name for <literal>OLD</literal> or
<literal>NEW</literal> rows in the <literal>RETURNING</literal> list.
</para>
<para>
By default, old values from the target table can be returned by writing
<literal>OLD.<replaceable class="parameter">column_name</replaceable></literal>
or <literal>OLD.*</literal>, and new values can be returned by writing
<literal>NEW.<replaceable class="parameter">column_name</replaceable></literal>
or <literal>NEW.*</literal>. When an alias is provided, these names are
hidden and the old or new rows must be referred to using the alias.
For example <literal>RETURNING WITH (OLD AS o, NEW AS n) o.*, n.*</literal>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">output_expression</replaceable></term>
<listitem>
<para>
An expression to be computed and returned by the <command>MERGE</command>
command after each row is changed (whether inserted, updated, or deleted).
The expression can use any columns of the source or target tables, or the
<link linkend="merge-action"><function>merge_action()</function></link>
function to return additional information about the action executed.
</para>
<para>
Writing <literal>*</literal> will return all columns from the source
table, followed by all columns from the target table. Often this will
lead to a lot of duplication, since it is common for the source and
target tables to have a lot of the same columns. This can be avoided by
qualifying the <literal>*</literal> with the name or alias of the source
or target table.
</para>
<para>
A column name or <literal>*</literal> may also be qualified using
<literal>OLD</literal> or <literal>NEW</literal>, or the corresponding
<replaceable class="parameter">output_alias</replaceable> for
<literal>OLD</literal> or <literal>NEW</literal>, to cause old or new
values from the target table to be returned. An unqualified column
name from the target table, or a column name or <literal>*</literal>
qualified using the target table name or alias will return new values
for <literal>INSERT</literal> and <literal>UPDATE</literal> actions, and
old values for <literal>DELETE</literal> actions.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">output_name</replaceable></term>
<listitem>
<para>
A name to use for a returned column.
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1>
<title>Outputs</title>
<para>
On successful completion, a <command>MERGE</command> command returns a command
tag of the form
<screen>
MERGE <replaceable class="parameter">total_count</replaceable>
</screen>
The <replaceable class="parameter">total_count</replaceable> is the total
number of rows changed (whether inserted, updated, or