diff options
author | Simon Riggs <simon@2ndQuadrant.com> | 2018-04-03 09:28:16 +0100 |
---|---|---|
committer | Simon Riggs <simon@2ndQuadrant.com> | 2018-04-03 09:28:16 +0100 |
commit | d204ef63776b8a00ca220adec23979091564e465 (patch) | |
tree | 5fa3d403db1d0377d85d20b9afb949c58855a37f /doc/src/sgml/mvcc.sgml | |
parent | aa5877bb26347c58a34aee4e460eb1e1123bb096 (diff) | |
download | postgresql-d204ef63776b8a00ca220adec23979091564e465.tar.gz |
MERGE SQL Command following SQL:2016
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 other require multiple PL statements.
e.g.
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 and partitioned tables, including
column and row security enforcement, as well as support for
row, statement and transition triggers.
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 statically from PL/pgSQL.
MERGE does not yet support inheritance, write rules,
RETURNING clauses, updatable views or foreign tables.
MERGE follows SQL Standard per the most recent SQL:2016.
Includes full tests and documentation, including full
isolation tests to demonstrate the concurrent behavior.
This version written from scratch in 2017 by Simon Riggs,
using docs and tests originally written in 2009. Later work
from Pavan Deolasee has been both complex and deep, leaving
the lead author credit now in his hands.
Extensive discussion of concurrency from Peter Geoghegan,
with thanks for the time and effort contributed.
Various issues reported via sqlsmith by Andreas Seltenreich
Authors: Pavan Deolasee, Simon Riggs
Reviewer: Peter Geoghegan, Amit Langote, Tomas Vondra, Simon Riggs
Discussion:
https://postgr.es/m/CANP8+jKitBSrB7oTgT9CY2i1ObfOt36z0XMraQc+Xrz8QB0nXA@mail.gmail.com
https://postgr.es/m/CAH2-WzkJdBuxj9PO=2QaO9-3h3xGbQPZ34kJH=HukRekwM-GZg@mail.gmail.com
Diffstat (limited to 'doc/src/sgml/mvcc.sgml')
-rw-r--r-- | doc/src/sgml/mvcc.sgml | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/doc/src/sgml/mvcc.sgml b/doc/src/sgml/mvcc.sgml index 24613e3c75..0e3e89af56 100644 --- a/doc/src/sgml/mvcc.sgml +++ b/doc/src/sgml/mvcc.sgml @@ -423,6 +423,31 @@ COMMIT; </para> <para> + The <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> and <command>UPDATE</command> will occur. + + If MERGE attempts an UPDATE or DELETE and the row is concurrently updated + but the join condition still passes for the current target and the current + source tuple, then MERGE will behave the same as the UPDATE or DELETE commands + and perform its action on the latest version of the row, using standard + EvalPlanQual. MERGE actions can be conditional, so conditions must be + re-evaluated on the latest row, starting from the first action. + + On the other hand, if the row is concurrently updated or deleted so that + the join condition fails, then MERGE will execute a NOT MATCHED action, if it + exists and the AND WHEN qual evaluates to true. + + If MERGE attempts an INSERT and a unique index is present and a duplicate + row is concurrently inserted then a uniqueness violation is raised. MERGE + does not attempt to avoid the ERROR by attempting an UPDATE. + </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 @@ -900,7 +925,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 |