diff options
author | Alvaro Herrera | 2022-03-28 14:45:58 +0000 |
---|---|---|
committer | Alvaro Herrera | 2022-03-28 14:47:48 +0000 |
commit | 7103ebb7aae8ab8076b7e85f335ceb8fe799097c (patch) | |
tree | 0bc2faf176b58d2546de40c3c36d93a4cdf1aafe /doc | |
parent | ae63017bdb316b16a9f201b10f1221598111d6c5 (diff) |
Add support for MERGE SQL command
MERGE performs actions that modify rows in the target table using a
source table or query. MERGE provides a single SQL statement that can
conditionally INSERT/UPDATE/DELETE rows -- a task that would otherwise
require multiple PL statements. For example,
MERGE INTO target AS t
USING source AS s
ON t.tid = s.sid
WHEN MATCHED AND t.balance > s.delta THEN
UPDATE SET balance = t.balance - s.delta
WHEN MATCHED THEN
DELETE
WHEN NOT MATCHED AND s.delta > 0 THEN
INSERT VALUES (s.sid, s.delta)
WHEN NOT MATCHED THEN
DO NOTHING;
MERGE works with regular tables, partitioned tables and inheritance
hierarchies, including column and row security enforcement, as well as
support for row and statement triggers and transition tables therein.
MERGE is optimized for OLTP and is parameterizable, though also useful
for large scale ETL/ELT. MERGE is not intended to be used in preference
to existing single SQL commands for INSERT, UPDATE or DELETE since there
is some overhead. MERGE can be used from PL/pgSQL.
MERGE does not support targetting updatable views or foreign tables, and
RETURNING clauses are not allowed either. These limitations are likely
fixable with sufficient effort. Rewrite rules are also not supported,
but it's not clear that we'd want to support them.
Author: Pavan Deolasee <pavan.deolasee@gmail.com>
Author: Álvaro Herrera <alvherre@alvh.no-ip.org>
Author: Amit Langote <amitlangote09@gmail.com>
Author: Simon Riggs <simon.riggs@enterprisedb.com>
Reviewed-by: Peter Eisentraut <peter.eisentraut@enterprisedb.com>
Reviewed-by: Andres Freund <andres@anarazel.de> (earlier versions)
Reviewed-by: Peter Geoghegan <pg@bowt.ie> (earlier versions)
Reviewed-by: Robert Haas <robertmhaas@gmail.com> (earlier versions)
Reviewed-by: Japin Li <japinli@hotmail.com>
Reviewed-by: Justin Pryzby <pryzby@telsasoft.com>
Reviewed-by: Tomas Vondra <tomas.vondra@enterprisedb.com>
Reviewed-by: Zhihong Yu <zyu@yugabyte.com>
Discussion: https://postgr.es/m/CANP8+jKitBSrB7oTgT9CY2i1ObfOt36z0XMraQc+Xrz8QB0nXA@mail.gmail.com
Discussion: https://postgr.es/m/CAH2-WzkJdBuxj9PO=2QaO9-3h3xGbQPZ34kJH=HukRekwM-GZg@mail.gmail.com
Discussion: https://postgr.es/m/20201231134736.GA25392@alvherre.pgsql
Diffstat (limited to 'doc')
-rw-r--r-- | doc/src/sgml/libpq.sgml | 8 | ||||
-rw-r--r-- | doc/src/sgml/mvcc.sgml | 34 | ||||
-rw-r--r-- | doc/src/sgml/plpgsql.sgml | 5 | ||||
-rw-r--r-- | doc/src/sgml/ref/allfiles.sgml | 1 | ||||
-rw-r--r-- | doc/src/sgml/ref/create_policy.sgml | 23 | ||||
-rw-r--r-- | doc/src/sgml/ref/insert.sgml | 11 | ||||
-rw-r--r-- | doc/src/sgml/ref/merge.sgml | 620 | ||||
-rw-r--r-- | doc/src/sgml/reference.sgml | 1 | ||||
-rw-r--r-- | doc/src/sgml/trigger.sgml | 22 |
9 files changed, 713 insertions, 12 deletions
diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 3998b1781b9..70233aa872e 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -4125,9 +4125,11 @@ char *PQcmdTuples(PGresult *res); <structname>PGresult</structname>. This function can only be used following the execution of a <command>SELECT</command>, <command>CREATE TABLE AS</command>, <command>INSERT</command>, <command>UPDATE</command>, <command>DELETE</command>, - <command>MOVE</command>, <command>FETCH</command>, or <command>COPY</command> statement, - or an <command>EXECUTE</command> of a prepared query that contains an - <command>INSERT</command>, <command>UPDATE</command>, or <command>DELETE</command> statement. + <command>MERGE</command>, <command>MOVE</command>, <command>FETCH</command>, + or <command>COPY</command> statement, or an <command>EXECUTE</command> of a + prepared query that contains an <command>INSERT</command>, + <command>UPDATE</command>, <command>DELETE</command>, + or <command>MERGE</command> statement. If the command that generated the <structname>PGresult</structname> was anything else, <xref linkend="libpq-PQcmdTuples"/> returns an empty string. The caller should not free the return value directly. It will be freed when diff --git a/doc/src/sgml/mvcc.sgml b/doc/src/sgml/mvcc.sgml index b4d1e571705..905460723c5 100644 --- a/doc/src/sgml/mvcc.sgml +++ b/doc/src/sgml/mvcc.sgml @@ -423,6 +423,37 @@ COMMIT; </para> <para> + <command>MERGE</command> allows the user to specify various + combinations of <command>INSERT</command>, <command>UPDATE</command> + or <command>DELETE</command> subcommands. A <command>MERGE</command> + command with both <command>INSERT</command> and <command>UPDATE</command> + subcommands looks similar to <command>INSERT</command> with an + <literal>ON CONFLICT DO UPDATE</literal> clause but does not + guarantee that either <command>INSERT</command> or + <command>UPDATE</command> will occur. + If MERGE attempts an <command>UPDATE</command> or + <command>DELETE</command> and the row is concurrently updated but + the join condition still passes for the current target and the + current source tuple, then <command>MERGE</command> will behave + the same as the <command>UPDATE</command> or + <command>DELETE</command> commands and perform its action on the + updated version of the row. However, because <command>MERGE</command> + can specify several actions and they can be conditional, the + conditions for each action are re-evaluated on the updated version of + the row, starting from the first action, even if the action that had + originally matched appears later in the list of actions. + On the other hand, if the row is concurrently updated or deleted so + that the join condition fails, then <command>MERGE</command> will + evaluate the condition's <literal>NOT MATCHED</literal> actions next, + and execute the first one that succeeds. + If <command>MERGE</command> attempts an <command>INSERT</command> + and a unique index is present and a duplicate row is concurrently + inserted, then a uniqueness violation is raised. + <command>MERGE</command> does not attempt to avoid the + error by executing an <command>UPDATE</command>. + </para> + + <para> Because Read Committed mode starts each command with a new snapshot that includes all transactions committed up to that instant, subsequent commands in the same transaction will see the effects @@ -924,7 +955,8 @@ ERROR: could not serialize access due to read/write dependencies among transact <para> The commands <command>UPDATE</command>, - <command>DELETE</command>, and <command>INSERT</command> + <command>DELETE</command>, <command>INSERT</command>, and + <command>MERGE</command> acquire this lock mode on the target table (in addition to <literal>ACCESS SHARE</literal> locks on any other referenced tables). In general, this lock mode will be acquired by any diff --git a/doc/src/sgml/plpgsql.sgml b/doc/src/sgml/plpgsql.sgml index e5c1356d8c5..7ebc6593f10 100644 --- a/doc/src/sgml/plpgsql.sgml +++ b/doc/src/sgml/plpgsql.sgml @@ -1388,7 +1388,7 @@ EXECUTE format('SELECT count(*) FROM %I ' Another restriction on parameter symbols is that they only work in optimizable SQL commands (<command>SELECT</command>, <command>INSERT</command>, <command>UPDATE</command>, - <command>DELETE</command>, and certain commands containing one of these). + <command>DELETE</command>, <command>MERGE</command>, and certain commands containing one of these). In other statement types (generically called utility statements), you must insert values textually even if they are just data values. @@ -1666,7 +1666,8 @@ GET DIAGNOSTICS integer_var = ROW_COUNT; </listitem> <listitem> <para> - <command>UPDATE</command>, <command>INSERT</command>, and <command>DELETE</command> + <command>UPDATE</command>, <command>INSERT</command>, <command>DELETE</command>, + and <command>MERGE</command> statements set <literal>FOUND</literal> true if at least one row is affected, false if no row is affected. </para> diff --git a/doc/src/sgml/ref/allfiles.sgml b/doc/src/sgml/ref/allfiles.sgml index d67270ccc35..e90a0e1f837 100644 --- a/doc/src/sgml/ref/allfiles.sgml +++ b/doc/src/sgml/ref/allfiles.sgml @@ -158,6 +158,7 @@ Complete list of usable sgml source files in this directory. <!ENTITY listen SYSTEM "listen.sgml"> <!ENTITY load SYSTEM "load.sgml"> <!ENTITY lock SYSTEM "lock.sgml"> +<!ENTITY merge SYSTEM "merge.sgml"> <!ENTITY move SYSTEM "move.sgml"> <!ENTITY notify SYSTEM "notify.sgml"> <!ENTITY prepare SYSTEM "prepare.sgml"> diff --git a/doc/src/sgml/ref/create_policy.sgml b/doc/src/sgml/ref/create_policy.sgml index f898b7a2185..e76c342d3da 100644 --- a/doc/src/sgml/ref/create_policy.sgml +++ b/doc/src/sgml/ref/create_policy.sgml @@ -55,7 +55,8 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable </para> <para> - For <command>INSERT</command> and <command>UPDATE</command> statements, + For <command>INSERT</command>, <command>UPDATE</command>, and + <command>MERGE</command> statements, <literal>WITH CHECK</literal> expressions are enforced after <literal>BEFORE</literal> triggers are fired, and before any actual data modifications are made. Thus a <literal>BEFORE ROW</literal> trigger may @@ -281,7 +282,9 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable <listitem> <para> Using <literal>INSERT</literal> for a policy means that it will apply - to <literal>INSERT</literal> commands. Rows being inserted that do + to <literal>INSERT</literal> commands and <literal>MERGE</literal> + commands that contain <literal>INSERT</literal> actions. + Rows being inserted that do not pass this policy will result in a policy violation error, and the entire <literal>INSERT</literal> command will be aborted. An <literal>INSERT</literal> policy cannot have @@ -305,7 +308,9 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable to <literal>UPDATE</literal>, <literal>SELECT FOR UPDATE</literal> and <literal>SELECT FOR SHARE</literal> commands, as well as auxiliary <literal>ON CONFLICT DO UPDATE</literal> clauses of - <literal>INSERT</literal> commands. Since <literal>UPDATE</literal> + <literal>INSERT</literal> commands. + <literal>MERGE</literal> commands containing <literal>UPDATE</literal> + actions are affected as well. Since <literal>UPDATE</literal> involves pulling an existing record and replacing it with a new modified record, <literal>UPDATE</literal> policies accept both a <literal>USING</literal> expression and @@ -435,7 +440,7 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable <entry>—</entry> </row> <row> - <entry><command>INSERT</command></entry> + <entry><command>INSERT</command> / <command>MERGE ... THEN INSERT</command></entry> <entry>—</entry> <entry>New row</entry> <entry>—</entry> @@ -459,7 +464,7 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable <entry>—</entry> </row> <row> - <entry><command>UPDATE</command></entry> + <entry><command>UPDATE</command> / <command>MERGE ... THEN UPDATE</command></entry> <entry> Existing & new rows <footnoteref linkend="rls-select-priv"/> </entry> @@ -614,6 +619,14 @@ AND </para> <para> + No separate policy exists for <command>MERGE</command>. Instead, the policies + defined for <command>SELECT</command>, <command>INSERT</command>, + <command>UPDATE</command>, and <command>DELETE</command> are applied + while executing <command>MERGE</command>, depending on the actions that are + performed. + </para> + + <para> Additional discussion and practical examples can be found in <xref linkend="ddl-rowsecurity"/>. </para> diff --git a/doc/src/sgml/ref/insert.sgml b/doc/src/sgml/ref/insert.sgml index 2973b72b815..a9af9959c08 100644 --- a/doc/src/sgml/ref/insert.sgml +++ b/doc/src/sgml/ref/insert.sgml @@ -589,6 +589,13 @@ INSERT <replaceable>oid</replaceable> <replaceable class="parameter">count</repl is a partition, an error will occur if one of the input rows violates the partition constraint. </para> + + <para> + You may also wish to consider using <command>MERGE</command>, since that + allows mixing <command>INSERT</command>, <command>UPDATE</command>, and + <command>DELETE</command> within a single statement. + See <xref linkend="sql-merge"/>. + </para> </refsect1> <refsect1> @@ -759,7 +766,9 @@ INSERT INTO distributors (did, dname) VALUES (10, 'Conrad International') Also, the case in which a column name list is omitted, but not all the columns are filled from the <literal>VALUES</literal> clause or <replaceable>query</replaceable>, - is disallowed by the standard. + is disallowed by the standard. If you prefer a more SQL standard + conforming statement than <literal>ON CONFLICT</literal>, see + <xref linkend="sql-merge"/>. </para> <para> diff --git a/doc/src/sgml/ref/merge.sgml b/doc/src/sgml/ref/merge.sgml new file mode 100644 index 00000000000..c547122c9bb --- /dev/null +++ b/doc/src/sgml/ref/merge.sgml @@ -0,0 +1,620 @@ +<!-- +doc/src/sgml/ref/merge.sgml +PostgreSQL documentation +--> + +<refentry id="sql-merge"> + + <refmeta> + <refentrytitle>MERGE</refentrytitle> + <manvolnum>7</manvolnum> + <refmiscinfo>SQL - Language Statements</refmiscinfo> + </refmeta> + + <refnamediv> + <refname>MERGE</refname> + <refpurpose>conditionally insert, update, or delete rows of a table</refpurpose> + </refnamediv> + + <refsynopsisdiv> +<synopsis> +[ WITH <replaceable class="parameter">with_query</replaceable> [, ...] ] +MERGE INTO <replaceable class="parameter">target_table_name</replaceable> [ [ AS ] <replaceable class="parameter">target_alias</replaceable> ] +USING <replaceable class="parameter">data_source</replaceable> ON <replaceable class="parameter">join_condition</replaceable> +<replaceable class="parameter">when_clause</replaceable> [...] + +<phrase>where <replaceable class="parameter">data_source</replaceable> is</phrase> + +{ <replaceable class="parameter">source_table_name</replaceable> | ( <replaceable class="parameter">source_query</replaceable> ) } [ [ AS ] <replaceable class="parameter">source_alias</replaceable> ] + +<phrase>and <replaceable class="parameter">when_clause</replaceable> is</phrase> + +{ WHEN MATCHED [ AND <replaceable class="parameter">condition</replaceable> ] THEN { <replaceable class="parameter">merge_update</replaceable> | <replaceable class="parameter">merge_delete</replaceable> | DO NOTHING } | + WHEN NOT MATCHED [ AND <replaceable class="parameter">condition</replaceable> ] THEN { <replaceable class="parameter">merge_insert</replaceable> | DO NOTHING } } + +<phrase>and <replaceable class="parameter">merge_insert</replaceable> is</phrase> + +INSERT [( <replaceable class="parameter">column_name</replaceable> [, ...] )] +[ OVERRIDING { SYSTEM | USER } VALUE ] +{ VALUES ( { <replaceable class="parameter">expression</replaceable> | DEFAULT } [, ...] ) | DEFAULT VALUES } + +<phrase>and <replaceable class="parameter">merge_update</replaceable> is</phrase> + +UPDATE SET { <replaceable class="parameter">column_name</replaceable> = { <replaceable class="parameter">expression</replaceable> | DEFAULT } | + ( <replaceable class="parameter">column_name</replaceable> [, ...] ) = ( { <replaceable class="parameter">expression</replaceable> | DEFAULT } [, ...] ) } [, ...] + +<phrase>and <replaceable class="parameter">merge_delete</replaceable> is</phrase> + +DELETE +</synopsis> + </refsynopsisdiv> + + <refsect1> + <title>Description</title> + + <para> + <command>MERGE</command> performs actions that modify rows in the + <replaceable class="parameter">target_table_name</replaceable>, + using the <replaceable class="parameter">data_source</replaceable>. + <command>MERGE</command> provides a single <acronym>SQL</acronym> + statement that can conditionally <command>INSERT</command>, + <command>UPDATE</command> or <command>DELETE</command> rows, a task + that would otherwise require multiple procedural language statements. + </para> + + <para> + First, the <command>MERGE</command> command performs a join + from <replaceable class="parameter">data_source</replaceable> to + <replaceable class="parameter">target_table_name</replaceable> + producing zero or more candidate change rows. For each candidate change + row, the status of <literal>MATCHED</literal> or <literal>NOT MATCHED</literal> + is set just once, after which <literal>WHEN</literal> clauses are evaluated + in the order specified. For each candidate change row, the first clause to + evaluate as true is executed. No more than one <literal>WHEN</literal> + clause is executed for any candidate change row. + </para> + + <para> + <command>MERGE</command> actions have the same effect as + regular <command>UPDATE</command>, <command>INSERT</command>, or + <command>DELETE</command> commands of the same names. The syntax of + those commands is different, notably that there is no <literal>WHERE</literal> + clause and no table name is specified. All actions refer to the + <replaceable class="parameter">target_table_name</replaceable>, + though modifications to other tables may be made using triggers. + </para> + + <para> + When <literal>DO NOTHING</literal> is specified, the source row is + skipped. Since actions are evaluated in their specified order, <literal>DO + NOTHING</literal> can be handy to skip non-interesting source rows before + more fine-grained handling. + </para> + + <para> + There is no separate <literal>MERGE</literal> privilege. + If you specify an update action, you must have the + <literal>UPDATE</literal> privilege on the column(s) + of the <replaceable class="parameter">target_table_name</replaceable> + that are referred to in the <literal>SET</literal> clause. + If you specify an insert action, you must have the <literal>INSERT</literal> + privilege on the <replaceable class="parameter">target_table_name</replaceable>. + If you specify an delete action, you must have the <literal>DELETE</literal> + privilege on the <replaceable class="parameter">target_table_name</replaceable>. + Privileges are tested once at statement start and are checked + whether or not particular <literal>WHEN</literal> clauses are executed. + You will require the <literal>SELECT</literal> privilege on the + <replaceable class="parameter">data_source</replaceable> and any column(s) + of the <replaceable class="parameter">target_table_name</replaceable> + referred to in a <literal>condition</literal>. + </para> + + <para> + <command>MERGE</command> is not supported if the + <replaceable class="parameter">target_table_name</replaceable> is a + materialized view, foreign table, or if it has any + rules defined on it. + </para> + </refsect1> + + <refsect1> + <title>Parameters</title> + + <variablelist> + <varlistentry> + <term><replaceable class="parameter">target_table_name</replaceable></term> + <listitem> + <para> + The name (optionally schema-qualified) of the target table to merge into. + </para> + </listitem> + </varlistentry> + + <varlistentry> + <term><replaceable class="parameter">target_alias</replaceable></term> + <listitem> + <para> + A substitute name for the target table. When an alias is + provided, it completely hides the actual name of the table. For + example, given <literal>MERGE INTO foo AS f</literal>, the remainder of the + <command>MERGE</command> statement must refer to this table as + <literal>f</literal> not <literal>foo</literal>. + </para> + </listitem> + </varlistentry> + + <varlistentry> + <term><replaceable class="parameter">source_table_name</replaceable></term> + <listitem> + <para> + The name (optionally schema-qualified) of the source table, view, or + transition table. + </para> + </listitem> + </varlistentry> + + <varlistentry> + <term><replaceable class="parameter">source_query</replaceable></term> + <listitem> + <para> + A query (<command>SELECT</command> statement or <command>VALUES</command> + statement) that supplies the rows to be merged into the + <replaceable class="parameter">target_table_name</replaceable>. + Refer to the <xref linkend="sql-select"/> + statement or <xref linkend="sql-values"/> + statement for a description of the syntax. + </para> + </listitem> + </varlistentry> + + <varlistentry> + <term><replaceable class="parameter">source_alias</replaceable></term> + <listitem> + <para> + A substitute name for the data source. When an alias is + provided, it completely hides the actual name of the table or the fact + that a query was issued. + </para> + </listitem> + </varlistentry> + + <varlistentry> + <term><replaceable class="parameter">join_condition</replaceable></term> + <listitem> + <para> + <replaceable class="parameter">join_condition</replaceable> is + an expression resulting in a value of type + <type>boolean</type> (similar to a <literal>WHERE</literal> + clause) that specifies which rows in the + <replaceable class="parameter">data_source</replaceable> + match rows in the + <replaceable class="parameter">target_table_name</replaceable>. + </para> + <warning> + <para> + Only columns from <replaceable class="parameter">target_table_name</replaceable> + that attempt to match <replaceable class="parameter">data_source</replaceable> + rows should appear in <replaceable class="parameter">join_condition</replaceable>. + <replaceable class="parameter">join_condition</replaceable> subexpressions that + only reference <replaceable class="parameter">target_table_name</replaceable> + columns can affect which action is taken, often in surprising ways. + </para> + </warning> + </listitem> + </varlistentry> + + <varlistentry> + <term><replaceable class="parameter">when_clause</replaceable></term> + <listitem> + <para> + At least one <literal>WHEN</literal> clause is required. + </para> + <para> + If the <literal>WHEN</literal> clause specifies <literal>WHEN MATCHED</literal> + and the candidate change row matches a row in the + <replaceable class="parameter">target_table_name</replaceable>, + the <literal>WHEN</literal> clause is executed if the + <replaceable class="parameter">condition</replaceable> is + absent or it evaluates to <literal>true</literal>. + </para> + <para> + Conversely, if the <literal>WHEN</literal> clause specifies + <literal>WHEN NOT MATCHED</literal> + and the candidate change row does not match a row in the + <replaceable class="parameter">target_table_name</replaceable>, + the <literal>WHEN</literal> clause is executed if the + <replaceable class="parameter">condition</replaceable> is + absent or it evaluates to <literal>true</literal>. + </para> + </listitem> + </varlistentry> + + <varlistentry> + <term><replaceable class="parameter">condition</replaceable></term> + <listitem> + <para> + An expression that returns a value of type <type>boolean</type>. + If this expression for a <literal>WHEN</literal> clause + returns <literal>true</literal>, then the action for that clause + is executed for that row. + </para> + <para> + A condition on a <literal>WHEN MATCHED</literal> clause can refer to columns + in both the source and the target relations. A condition on a + <literal>WHEN NOT MATCHED</literal> clause can only refer to columns from + the source relation, since by definition there is no matching target row. + Only the system attributes from the target table are accessible. + </para> + </listitem> + </varlistentry> + + <varlistentry> + <term><replaceable class="parameter">merge_insert</replaceable></term> + <listitem> + <para> + The specification of an <literal>INSERT</literal> action that inserts + one row into the target table. + The target column names can be listed in any order. If no list of + column names is given at all, the default is all the columns of the + table in their declared order. + </para> + <para> + Each column not present in the explicit or implicit column list will be + filled with a default value, either its declared default value + or null if there is none. + </para> + <para> + If the expression for any column is not of the correct data type, + automatic type conversion will be attempted. + </para> + <para> + If <replaceable class="parameter">target_table_name</replaceable> + is a partitioned table, each row is routed to the appropriate partition + and inserted into it. + If <replaceable class="parameter">target_table_name</replaceable> + is a partition, an error will occur if any input row violates the + partition constraint. + </para> + <para> + Column names may not be specified more than once. + <command>INSERT</command> actions cannot contain sub-selects. + </para> + <para> + Only one <literal>VALUES</literal> clause can be specified. + The <literal>VALUES</literal> clause can only refer to columns from + the source relation, since by definition there is no matching target row. + </para> + </listitem> + </varlistentry> + + <varlistentry> + <term><replaceable class="parameter">merge_update</replaceable></term> + <listitem> + <para> + The specification of an <literal>UPDATE</literal> action that updates + the current row of the <replaceable class="parameter">target_table_name</replaceable>. + Column names may not be specified more than once. + </para> + <para> + Neither a table name nor a <literal>WHERE</literal> clause are allowed. + </para> + </listitem> + </varlistentry> + + <varlistentry> + <term><replaceable class="parameter">merge_delete</replaceable></term> + <listitem> + <para> + Specifies a <literal>DELETE</literal> action that deletes the current row + of the <replaceable class="parameter">target_table_name</replaceable>. + Do not include the table name or any other clauses, as you would normally + do with a <xref linkend="sql-delete"/> command. + </para> + </listitem> + </varlistentry> + + <varlistentry> + <term><replaceable class="parameter">column_name</replaceable></term> + <listitem> + <para> + The name of a column in the <replaceable + class="parameter">target_table_name</replaceable>. The column name + can be qualified with a subfield name or array subscript, if + needed. (Inserting into only some fields of a composite + column leaves the other fields null.) + Do not include the table's name in the specification + of a target column. + </para> + </listitem> + </varlistentry> + + <varlistentry> + <term><literal>OVERRIDING SYSTEM VALUE</literal></term> + <listitem> + <para> + Without this clause, it is an error to specify an explicit value + (other than <literal>DEFAULT</literal>) for an identity column defined + as <literal>GENERATED ALWAYS</literal>. This clause overrides that + restriction. + </para> + </listitem> + </varlistentry> + + <varlistentry> + <term><literal>OVERRIDING USER VALUE</literal></term> + <listitem> + <para> + If this clause is specified, then any values supplied for identity + columns defined as <literal>GENERATED BY DEFAULT</literal> are ignored + and the default sequence-generated values are applied. + </para> + </listitem> + </varlistentry> + + <varlistentry> + <term><literal>DEFAULT VALUES</literal></term> + <listitem> + <para> + All columns will be filled with their default values. + (An <literal>OVERRIDING</literal> clause is not permitted in this + form.) + </para> + </listitem> + </varlistentry> + + <varlistentry> + <term><replaceable class="parameter">expression</replaceable></term> + <listitem> + <para> + An expression to assign to the column. If used in a + <literal>WHEN MATCHED</literal> clause, the expression can use values + from the original row in the target table, and values from the + <literal>data_source</literal> row. + If used in a <literal>WHEN NOT MATCHED</literal> clause, the + expression can use values from the <literal>data_source</literal>. + </para> + </listitem> + </varlistentry> + + <varlistentry> + <term><literal>DEFAULT</literal></term> + <listitem> + <para> + Set the column to its default value (which will be <literal>NULL</literal> + if no specific default expression has been assigned to it). + </para> + </listitem> + </varlistentry> + + <varlistentry> + <term><replaceable class="parameter">with_query</replaceable></term> + <listitem> + <para> + The <literal>WITH</literal> clause allows you to specify one or more + subqueries that can be referenced by name in the <command>MERGE</command> + query. See <xref linkend="queries-with"/> and <xref linkend="sql-select"/> + for details. + </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 deleted). + If <replaceable class="parameter">total_count</replaceable> is 0, no rows + were changed in any way. + </para> + + </refsect1> + + <refsect1> + <title>Notes</title> + + <para> + The following steps take place during the execution of + <command>MERGE</command>. + <orderedlist> + <listitem> + <para> + Perform any <literal>BEFORE STATEMENT</literal> triggers for all + actions specified, whether or not their <literal>WHEN</literal> + clauses match. + </para> + </listitem> + <listitem> + <para> + Perform a join from source to target table. + The resulting query will be optimized normally and will produce + a set of candidate change rows. For each candidate change row, + <orderedlist> + <listitem> + <para> + Evaluate whether each row is <literal>MATCHED</literal> or + <literal>NOT MATCHED</literal>. + </para> + </listitem> + <listitem> + <para> + Test each <literal>WHEN</literal> condition in the order + specified until one returns true. + </para> + </listitem> + <listitem> + <para> + When a condition returns true, perform the following actions: + <orderedlist> + <listitem> + <para> + Perform any <literal>BEFORE ROW</literal> triggers that fire + for the action's event type. + </para> + </listitem> + <listitem> + <para> + Perform the specified action, invoking any check constraints on the + target table. + </para> + </listitem> + <listitem> + <para> + Perform any <literal>AFTER ROW</literal> triggers that fire for + the action's event type. + </para> + </listitem> + </orderedlist> + </para> + </listitem> + </orderedlist> + </para> + </listitem> + <listitem> + <para> + Perform any <literal>AFTER STATEMENT</literal> triggers for actions + specified, whether or not they actually occur. This is similar to the + behavior of an <command>UPDATE</command> statement that modifies no rows. + </para> + </listitem> + </orderedlist> + In summary, statement triggers for an event type (say, + <command>INSERT</command>) will be fired whenever we + <emphasis>specify</emphasis> an action of that kind. + In contrast, row-level triggers will fire only for the specific event type + being <emphasis>executed</emphasis>. + So a <command>MERGE</command> command might fire statement triggers for both + <command>UPDATE</command> and <command>INSERT</command>, even though only + <command>UPDATE</command> row triggers were fired. + </para> + + <para> + You should ensure that the join produces at most one candidate change row + for each target row. In other words, a target row shouldn't join to more + than one data source row. If it does, then only one of the candidate change + rows will be used to modify the target row; later attempts to modify the + row will cause an error. + This can also occur if row triggers make changes to the target table + and the rows so modified are then subsequently also modified by + <command>MERGE</command>. + If the repeated action is an <command>INSERT</command>, this will + cause a uniqueness violation, while a repeated <command>UPDATE</command> + or <command>DELETE</command> will cause a cardinality violation; the + latter behavior is required by the <acronym>SQL</acronym> standard. + This differs from historical <productname>PostgreSQL</productname> + behavior of joins in <command>UPDATE</command> and + <command>DELETE</command> statements where second and subsequent + attempts to modify the same row are simply ignored. + </para> + + <para> + If a <literal>WHEN</literal> clause omits an <literal>AND</literal> + sub-clause, it becomes the final reachable clause of that + kind (<literal>MATCHED</literal> or <literal>NOT MATCHED</literal>). + If a later <literal>WHEN</literal> clause of that kind + is specified it would be provably unreachable and an error is raised. + If no final reachable clause is specified of either kind, it is + possible that no action will be taken for a candidate change row. + </para> + + <para> + The order in which rows are generated from the data source is + indeterminate by default. + A <replaceable class="parameter">source_query</replaceable> can be + used to specify a consistent ordering, if required, which might be + needed to avoid deadlocks between concurrent transactions. + </para> + + <para> + There is no <literal>RETURNING</literal> clause with + <command>MERGE</command>. Actions of <command>INSERT</command>, + <command>UPDATE</command> and <command>DELETE</command> cannot contain + <literal>RETURNING</literal> or <literal>WITH</literal> clauses. + </para> + + <para> + 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>CustomerAccounts</literal> based + upon new <literal>Transactions</literal>. + +<programlisting> +MERGE INTO CustomerAccount CA +USING RecentTransactions T +ON T.CustomerId = CA.CustomerId +WHEN MATCHED THEN + UPDATE SET Balance = Balance + TransactionValue +WHEN NOT MATCHED THEN + INSERT (CustomerId, Balance) + VALUES (T.CustomerId, T.TransactionValue); +</programlisting> + </para> + + <para> + Notice that this would be exactly equivalent to the following + statement because the <literal>MATCHED</literal> result does not change + during execution. + +<programlisting> +MERGE INTO CustomerAccount CA +USING (Select CustomerId, TransactionValue From RecentTransactions) AS T +ON CA.CustomerId = T.CustomerId +WHEN NOT MATCHED THEN + INSERT (CustomerId, Balance) + VALUES (T.CustomerId, T.TransactionValue) +WHEN MATCHED THEN + UPDATE SET Balance = Balance + TransactionValue; +</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. +<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; +</programlisting> + + The <literal>wine_stock_changes</literal> table might be, for example, a + temporary table recently loaded into the database. + </para> + + </refsect1> + + <refsect1> + <title>Compatibility</title> + <para> + This command conforms to the <acronym>SQL</acronym> standard. + </para> + <para> + The WITH clause and <literal>DO NOTHING</literal> action are extensions to + the <acronym>SQL</acronym> standard. + </para> + </refsect1> +</refentry> diff --git a/doc/src/sgml/reference.sgml b/doc/src/sgml/reference.sgml index da421ff24e2..a3b743e8c1e 100644 --- a/doc/src/sgml/reference.sgml +++ b/doc/src/sgml/reference.sgml @@ -186,6 +186,7 @@ &listen; &load; &lock; + &merge; &move; ¬ify; &prepare; diff --git a/doc/src/sgml/trigger.sgml b/doc/src/sgml/trigger.sgml index 7e2654493bb..04e702a7956 100644 --- a/doc/src/sgml/trigger.sgml +++ b/doc/src/sgml/trigger.sgml @@ -193,6 +193,28 @@ </para> <para> + No separate triggers are defined for <command>MERGE</command>. Instead, + statement-level or row-level <command>UPDATE</command>, + <command>DELETE</command>, and <command>INSERT</command> triggers are fired + depending on (for statement-level triggers) what actions are specified in + the <command>MERGE</command> query and (for row-level triggers) what + actions are performed. + </para> + + <para> + While running a <command>MERGE</command> command, statement-level + <literal>BEFORE</literal> and <literal>AFTER</literal> triggers are + fired for events specified in the actions of the <command>MERGE</command> + command, irrespective of whether or not the action is ultimately performed. + This is the same as an <command>UPDATE</command> statement that updates + no rows, yet statement-level triggers are fired. + The row-level triggers are fired only when a row is actually updated, + inserted or deleted. So it's perfectly legal that while statement-level + triggers are fired for certain types of action, no row-level triggers + are fired for the same kind of action. + </para> + + <para> Trigger functions invoked by per-statement triggers should always return <symbol>NULL</symbol>. Trigger functions invoked by per-row triggers can return a table row (a value of |