mirror of
https://github.com/postgres/postgres.git
synced 2025-05-08 07:21:33 +03:00
Improve ON CONFLICT documentation.
Author: Peter Geoghegan and Andres Freund Discussion: CAM3SWZScpWzQ-7EJC77vwqzZ1GO8GNmURQ1QqDQ3wRn7AbW1Cg@mail.gmail.com Backpatch: 9.5, where ON CONFLICT was introduced
This commit is contained in:
parent
6f8519d130
commit
af85779bf7
@ -99,7 +99,8 @@ INSERT INTO <replaceable class="PARAMETER">table_name</replaceable> [ AS <replac
|
|||||||
<para>
|
<para>
|
||||||
You must have <literal>INSERT</literal> privilege on a table in
|
You must have <literal>INSERT</literal> privilege on a table in
|
||||||
order to insert into it. If <literal>ON CONFLICT DO UPDATE</> is
|
order to insert into it. If <literal>ON CONFLICT DO UPDATE</> is
|
||||||
present the <literal>UPDATE</literal> privilege is also required.
|
present, <literal>UPDATE</literal> privilege on the table is also
|
||||||
|
required.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
@ -126,366 +127,378 @@ INSERT INTO <replaceable class="PARAMETER">table_name</replaceable> [ AS <replac
|
|||||||
<refsect1>
|
<refsect1>
|
||||||
<title>Parameters</title>
|
<title>Parameters</title>
|
||||||
|
|
||||||
<variablelist>
|
<refsect2 id="SQL-INSERTING-PARAMS">
|
||||||
<varlistentry>
|
<title id="sql-inserting-params-title">Inserting</title>
|
||||||
<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>INSERT</>
|
|
||||||
query. See <xref linkend="queries-with"> and <xref linkend="sql-select">
|
|
||||||
for details.
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
It is possible for the <replaceable class="parameter">query</replaceable>
|
|
||||||
(<command>SELECT</command> statement)
|
|
||||||
to also contain a <literal>WITH</literal> clause. In such a case both
|
|
||||||
sets of <replaceable>with_query</replaceable> can be referenced within
|
|
||||||
the <replaceable class="parameter">query</replaceable>, but the
|
|
||||||
second one takes precedence since it is more closely nested.
|
|
||||||
</para>
|
|
||||||
</listitem>
|
|
||||||
</varlistentry>
|
|
||||||
|
|
||||||
<varlistentry>
|
<para>
|
||||||
<term><replaceable class="PARAMETER">table_name</replaceable></term>
|
This section covers parameters that may be used when only
|
||||||
<listitem>
|
inserting new rows. Parameters <emphasis>exclusively</emphasis>
|
||||||
<para>
|
used with the <literal>ON CONFLICT</literal> clause are described
|
||||||
The name (optionally schema-qualified) of an existing table.
|
separately.
|
||||||
</para>
|
</para>
|
||||||
</listitem>
|
|
||||||
</varlistentry>
|
|
||||||
|
|
||||||
<varlistentry>
|
<variablelist>
|
||||||
<term><replaceable class="parameter">alias</replaceable></term>
|
<varlistentry>
|
||||||
<listitem>
|
<term><replaceable class="parameter">with_query</replaceable></term>
|
||||||
<para>
|
<listitem>
|
||||||
A substitute name for the target table. When an alias is provided, it
|
<para>
|
||||||
completely hides the actual name of the table. This is particularly
|
The <literal>WITH</literal> clause allows you to specify one or more
|
||||||
useful when using <literal>ON CONFLICT DO UPDATE</literal> into a table
|
subqueries that can be referenced by name in the <command>INSERT</>
|
||||||
named <literal>excluded</literal> as that's also the name of the
|
query. See <xref linkend="queries-with"> and <xref linkend="sql-select">
|
||||||
pseudo-relation containing the proposed row.
|
for details.
|
||||||
</para>
|
</para>
|
||||||
</listitem>
|
<para>
|
||||||
</varlistentry>
|
It is possible for the <replaceable class="parameter">query</replaceable>
|
||||||
|
(<command>SELECT</command> statement)
|
||||||
|
to also contain a <literal>WITH</literal> clause. In such a case both
|
||||||
|
sets of <replaceable>with_query</replaceable> can be referenced within
|
||||||
|
the <replaceable class="parameter">query</replaceable>, but the
|
||||||
|
second one takes precedence since it is more closely nested.
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
|
|
||||||
|
<varlistentry>
|
||||||
|
<term><replaceable class="PARAMETER">table_name</replaceable></term>
|
||||||
|
<listitem>
|
||||||
|
<para>
|
||||||
|
The name (optionally schema-qualified) of an existing table.
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
|
|
||||||
|
<varlistentry>
|
||||||
|
<term><replaceable class="parameter">alias</replaceable></term>
|
||||||
|
<listitem>
|
||||||
|
<para>
|
||||||
|
A substitute name for <replaceable
|
||||||
|
class="PARAMETER">table_name</replaceable>. When an alias is
|
||||||
|
provided, it completely hides the actual name of the table.
|
||||||
|
This is particularly useful when <literal>ON CONFLICT DO
|
||||||
|
UPDATE</literal> targets a table named excluded, since that's
|
||||||
|
also the name of the special table representing rows proposed
|
||||||
|
for insertion.
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
|
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><replaceable class="PARAMETER">column_name</replaceable></term>
|
<term><replaceable class="PARAMETER">column_name</replaceable></term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
The name of a column in the table named by <replaceable class="PARAMETER">table_name</replaceable>.
|
The name of a column in the table named by <replaceable
|
||||||
The column name can be qualified with a subfield name or array
|
class="PARAMETER">table_name</replaceable>. The column name
|
||||||
subscript, if needed. (Inserting into only some fields of a
|
can be qualified with a subfield name or array subscript, if
|
||||||
composite column leaves the other fields null.) When
|
needed. (Inserting into only some fields of a composite
|
||||||
referencing a column with <literal>ON CONFLICT DO UPDATE</>, do
|
column leaves the other fields null.) When referencing a
|
||||||
not include the table's name in the specification of a target
|
column with <literal>ON CONFLICT DO UPDATE</>, do not include
|
||||||
column. For example, <literal>INSERT ... ON CONFLICT DO UPDATE
|
the table's name in the specification of a target column. For
|
||||||
tab SET table_name.col = 1</> is invalid (this follows the general
|
example, <literal>INSERT ... ON CONFLICT DO UPDATE tab SET
|
||||||
behavior for <command>UPDATE</>).
|
table_name.col = 1</> is invalid (this follows the general
|
||||||
</para>
|
behavior for <command>UPDATE</>).
|
||||||
</listitem>
|
</para>
|
||||||
</varlistentry>
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><literal>DEFAULT VALUES</literal></term>
|
<term><literal>DEFAULT VALUES</literal></term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
All columns will be filled with their default values.
|
All columns will be filled with their default values.
|
||||||
</para>
|
</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><replaceable class="PARAMETER">expression</replaceable></term>
|
<term><replaceable class="PARAMETER">expression</replaceable></term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
An expression or value to assign to the corresponding column.
|
An expression or value to assign to the corresponding column.
|
||||||
</para>
|
</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><literal>DEFAULT</literal></term>
|
<term><literal>DEFAULT</literal></term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
The corresponding column will be filled with
|
The corresponding column will be filled with
|
||||||
its default value.
|
its default value.
|
||||||
</para>
|
</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><replaceable class="PARAMETER">query</replaceable></term>
|
<term><replaceable class="PARAMETER">query</replaceable></term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
A query (<command>SELECT</command> statement) that supplies the
|
A query (<command>SELECT</command> statement) that supplies the
|
||||||
rows to be inserted. Refer to the
|
rows to be inserted. Refer to the
|
||||||
<xref linkend="sql-select">
|
<xref linkend="sql-select">
|
||||||
statement for a description of the syntax.
|
statement for a description of the syntax.
|
||||||
</para>
|
</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><replaceable class="PARAMETER">output_expression</replaceable></term>
|
<term><replaceable class="PARAMETER">output_expression</replaceable></term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
An expression to be computed and returned by the <command>INSERT</>
|
An expression to be computed and returned by the
|
||||||
command after each row is inserted (not updated). The
|
<command>INSERT</> command after each row is inserted or
|
||||||
expression can use any column names of the table named by
|
updated. The expression can use any column names of the table
|
||||||
<replaceable class="PARAMETER">table_name</replaceable>.
|
named by <replaceable
|
||||||
Write <literal>*</> to return all columns of the inserted row(s).
|
class="PARAMETER">table_name</replaceable>. Write
|
||||||
</para>
|
<literal>*</> to return all columns of the inserted or updated
|
||||||
</listitem>
|
row(s).
|
||||||
</varlistentry>
|
</para>
|
||||||
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><literal>conflict_target</literal></term>
|
<term><replaceable class="PARAMETER">output_name</replaceable></term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
Specify which conflicts <literal>ON CONFLICT</literal> refers to.
|
A name to use for a returned column.
|
||||||
</para>
|
</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
</variablelist>
|
||||||
|
</refsect2>
|
||||||
|
|
||||||
<varlistentry>
|
<refsect2 id="sql-on-conflict">
|
||||||
<term><literal>conflict_action</literal></term>
|
<title id="sql-on-conflict-title"><literal>ON CONFLICT</literal> Clause</title>
|
||||||
<listitem>
|
<indexterm zone="SQL-INSERT">
|
||||||
<para>
|
<primary>UPSERT</primary>
|
||||||
<literal>DO NOTHING</literal> or <literal>DO UPDATE
|
</indexterm>
|
||||||
SET</literal> clause specifying the action to be performed in
|
<indexterm zone="SQL-INSERT">
|
||||||
case of a conflict.
|
<primary>ON CONFLICT</primary>
|
||||||
</para>
|
</indexterm>
|
||||||
</listitem>
|
<para>
|
||||||
</varlistentry>
|
The optional <literal>ON CONFLICT</literal> clause specifies an
|
||||||
|
alternative action to raising a unique violation or exclusion
|
||||||
|
constraint violation error. For each individual row proposed for
|
||||||
|
insertion, either the insertion proceeds, or, if an
|
||||||
|
<emphasis>arbiter</emphasis> constraint or index specified by
|
||||||
|
<parameter>conflict_target</parameter> is violated, the
|
||||||
|
alternative <parameter>conflict_action</parameter> is taken.
|
||||||
|
<literal>ON CONFLICT DO NOTHING</literal> simply avoids inserting
|
||||||
|
a row as its alternative action. <literal>ON CONFLICT DO
|
||||||
|
UPDATE</literal> updates the existing row that conflicts with the
|
||||||
|
row proposed for insertion as its alternative action.
|
||||||
|
</para>
|
||||||
|
|
||||||
<varlistentry>
|
<para>
|
||||||
<term><replaceable class="PARAMETER">output_name</replaceable></term>
|
<parameter>conflict_target</parameter> can perform
|
||||||
<listitem>
|
<emphasis>unique index inference</emphasis>. When performing
|
||||||
<para>
|
inference, it consists of one or more <replaceable
|
||||||
A name to use for a returned column.
|
class="PARAMETER">column_name_index</replaceable> columns and/or
|
||||||
</para>
|
<replaceable class="PARAMETER">expression_index</replaceable>
|
||||||
</listitem>
|
expressions, and an optional <replaceable class="PARAMETER">
|
||||||
</varlistentry>
|
index_predicate</replaceable>. All <replaceable
|
||||||
|
class="PARAMETER">table_name</replaceable> unique indexes that,
|
||||||
|
without regard to order, contain exactly the
|
||||||
|
<parameter>conflict_target</parameter>-specified
|
||||||
|
columns/expressions are inferred (chosen) as arbiter indexes. If
|
||||||
|
an <replaceable class="PARAMETER">index_predicate</replaceable> is
|
||||||
|
specified, it must, as a further requirement for inference,
|
||||||
|
satisfy arbiter indexes. Note that this means a non-partial
|
||||||
|
unique index (a unique index without a predicate) will be inferred
|
||||||
|
(and thus used by <literal>ON CONFLICT</literal>) if such an index
|
||||||
|
satisfying every other criteria is available. If an attempt at
|
||||||
|
inference is unsuccessful, an error is raised.
|
||||||
|
</para>
|
||||||
|
|
||||||
<varlistentry>
|
<para>
|
||||||
<term><replaceable class="PARAMETER">column_name_index</replaceable></term>
|
<literal>ON CONFLICT DO UPDATE</literal> guarantees an atomic
|
||||||
<listitem>
|
<command>INSERT</command> or <command>UPDATE</command> outcome;
|
||||||
<para>
|
provided there is no independent error, one of those two outcomes
|
||||||
The name of a <replaceable
|
is guaranteed, even under high concurrency. This is also known as
|
||||||
class="PARAMETER">table_name</replaceable> column. Part of a
|
<firstterm>UPSERT</firstterm> — <quote>UPDATE or
|
||||||
unique index inference clause. Follows <command>CREATE
|
INSERT</quote>.
|
||||||
INDEX</command> format. <literal>SELECT</> privilege on
|
</para>
|
||||||
<replaceable class="PARAMETER">column_name_index</replaceable>
|
|
||||||
is required.
|
|
||||||
</para>
|
|
||||||
</listitem>
|
|
||||||
</varlistentry>
|
|
||||||
|
|
||||||
<varlistentry>
|
<variablelist>
|
||||||
<term><replaceable class="PARAMETER">expression_index</replaceable></term>
|
<varlistentry>
|
||||||
<listitem>
|
<term><literal>conflict_target</literal></term>
|
||||||
<para>
|
<listitem>
|
||||||
Similar to <replaceable
|
<para>
|
||||||
class="PARAMETER">column_name_index</replaceable>, but used to
|
Specifies which conflicts <literal>ON CONFLICT</literal> takes
|
||||||
infer expressions on <replaceable
|
the alternative action on by choosing <firstterm>arbiter
|
||||||
class="PARAMETER">table_name</replaceable> columns appearing
|
indexes</firstterm>. Either performs <emphasis>unique index
|
||||||
within index definitions (not simple columns). Part of unique
|
inference</emphasis>, or names a constraint explicitly. For
|
||||||
index inference clause. Follows <command>CREATE INDEX</command>
|
<literal>ON CONFLICT DO NOTHING</literal>, it is optional to
|
||||||
format. <literal>SELECT</> privilege on any column appearing
|
specify a <parameter>conflict_target</parameter>; when
|
||||||
within <replaceable
|
omitted, conflicts with all usable constraints (and unique
|
||||||
class="PARAMETER">expression_index</replaceable> is required.
|
indexes) are handled. For <literal>ON CONFLICT DO
|
||||||
</para>
|
UPDATE</literal>, a <parameter>conflict_target</parameter>
|
||||||
</listitem>
|
<emphasis>must</emphasis> be provided.
|
||||||
</varlistentry>
|
</para>
|
||||||
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><replaceable class="PARAMETER">collation</replaceable></term>
|
<term><literal>conflict_action</literal></term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
When specified, mandates that corresponding <replaceable
|
<parameter>conflict_action</parameter> specifies an
|
||||||
class="PARAMETER">column_name_index</replaceable> or
|
alternative <literal>ON CONFLICT</literal> action. It can be
|
||||||
<replaceable class="PARAMETER">expression_index</replaceable> use a
|
either <literal>DO NOTHING</literal>, or a <literal>DO
|
||||||
particular collation in order to be matched in the inference clause.
|
UPDATE</literal> clause specifying the exact details of the
|
||||||
Typically this is omitted, as collations usually do not affect whether or
|
<literal>UPDATE</literal> action to be performed in case of a
|
||||||
not a constraint violation occurs. Follows <command>CREATE
|
conflict. The <literal>SET</literal> and
|
||||||
INDEX</command> format.
|
<literal>WHERE</literal> clauses in <literal>ON CONFLICT DO
|
||||||
</para>
|
UPDATE</literal> have access to the existing row using the
|
||||||
</listitem>
|
table's name (or an alias), and to rows proposed for insertion
|
||||||
</varlistentry>
|
using the special <varname>excluded</varname> table.
|
||||||
|
<literal>SELECT</> privilege is required on any column in the
|
||||||
|
target table where corresponding <varname>excluded</varname>
|
||||||
|
columns are read.
|
||||||
|
</para>
|
||||||
|
<para>
|
||||||
|
Note that the effects of all per-row <literal>BEFORE
|
||||||
|
INSERT</literal> triggers are reflected in
|
||||||
|
<varname>excluded</varname> values, since those effects may
|
||||||
|
have contributed to the row being excluded from insertion.
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><replaceable class="PARAMETER">opclass</replaceable></term>
|
<term><replaceable class="PARAMETER">column_name_index</replaceable></term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
When specified, mandates that corresponding <replaceable
|
The name of a <replaceable
|
||||||
class="PARAMETER">column_name_index</replaceable> or
|
class="PARAMETER">table_name</replaceable> column. Used to
|
||||||
<replaceable class="PARAMETER">expression_index</replaceable> use
|
infer arbiter indexes. Follows <command>CREATE
|
||||||
particular operator class in order to be matched by the inference
|
INDEX</command> format. <literal>SELECT</> privilege on
|
||||||
clause. Sometimes this is omitted because the
|
<replaceable class="PARAMETER">column_name_index</replaceable>
|
||||||
<emphasis>equality</emphasis> semantics are often equivalent across a
|
is required.
|
||||||
type's operator classes anyway, or because it's sufficient to trust that
|
</para>
|
||||||
the defined unique indexes have the pertinent definition of equality.
|
</listitem>
|
||||||
Follows <command>CREATE INDEX</command> format.
|
</varlistentry>
|
||||||
</para>
|
|
||||||
</listitem>
|
|
||||||
</varlistentry>
|
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><replaceable class="PARAMETER">index_predicate</replaceable></term>
|
<term><replaceable class="PARAMETER">expression_index</replaceable></term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
Used to allow inference of partial unique indexes. Any indexes
|
Similar to <replaceable
|
||||||
that satisfy the predicate (which need not actually be partial
|
class="PARAMETER">column_name_index</replaceable>, but used to
|
||||||
indexes) can be matched by the rest of the inference clause.
|
infer expressions on <replaceable
|
||||||
Follows <command>CREATE INDEX</command> format.
|
class="PARAMETER">table_name</replaceable> columns appearing
|
||||||
<literal>SELECT</> privilege on any column appearing within
|
within index definitions (not simple columns). Follows
|
||||||
<replaceable class="PARAMETER">index_predicate</replaceable> is
|
<command>CREATE INDEX</command> format. <literal>SELECT</>
|
||||||
required.
|
privilege on any column appearing within <replaceable
|
||||||
</para>
|
class="PARAMETER">expression_index</replaceable> is required.
|
||||||
</listitem>
|
</para>
|
||||||
</varlistentry>
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><replaceable class="PARAMETER">constraint_name</replaceable></term>
|
<term><replaceable class="PARAMETER">collation</replaceable></term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
Explicitly specifies an arbiter <emphasis>constraint</emphasis>
|
When specified, mandates that corresponding <replaceable
|
||||||
by name, rather than inferring a constraint or index. This is
|
class="PARAMETER">column_name_index</replaceable> or
|
||||||
mostly useful for exclusion constraints, that cannot be chosen
|
<replaceable class="PARAMETER">expression_index</replaceable>
|
||||||
in the conventional way (with an inference clause).
|
use a particular collation in order to be matched during
|
||||||
</para>
|
inference. Typically this is omitted, as collations usually
|
||||||
</listitem>
|
do not affect whether or not a constraint violation occurs.
|
||||||
</varlistentry>
|
Follows <command>CREATE INDEX</command> format.
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><replaceable class="PARAMETER">condition</replaceable></term>
|
<term><replaceable class="PARAMETER">opclass</replaceable></term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
An expression that returns a value of type <type>boolean</type>. Only
|
When specified, mandates that corresponding <replaceable
|
||||||
rows for which this expression returns <literal>true</literal> will be
|
class="PARAMETER">column_name_index</replaceable> or
|
||||||
updated, although all rows will be locked when the
|
<replaceable class="PARAMETER">expression_index</replaceable>
|
||||||
<literal>ON CONFLICT DO UPDATE</> action is taken.
|
use particular operator class in order to be matched during
|
||||||
</para>
|
inference. Typically this is omitted, as the
|
||||||
</listitem>
|
<emphasis>equality</emphasis> semantics are often equivalent
|
||||||
</varlistentry>
|
across a type's operator classes anyway, or because it's
|
||||||
</variablelist>
|
sufficient to trust that the defined unique indexes have the
|
||||||
</refsect1>
|
pertinent definition of equality. Follows <command>CREATE
|
||||||
|
INDEX</command> format.
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
|
|
||||||
<refsect1 id="sql-on-conflict">
|
<varlistentry>
|
||||||
<title id="sql-on-conflict-title"><literal>ON CONFLICT</literal> Clause</title>
|
<term><replaceable class="PARAMETER">index_predicate</replaceable></term>
|
||||||
<indexterm zone="SQL-INSERT">
|
<listitem>
|
||||||
<primary>UPSERT</primary>
|
<para>
|
||||||
</indexterm>
|
Used to allow inference of partial unique indexes. Any
|
||||||
<indexterm zone="SQL-INSERT">
|
indexes that satisfy the predicate (which need not actually be
|
||||||
<primary>ON CONFLICT</primary>
|
partial indexes) can be inferred. Follows <command>CREATE
|
||||||
</indexterm>
|
INDEX</command> format. <literal>SELECT</> privilege on any
|
||||||
<para>
|
column appearing within <replaceable
|
||||||
The optional <literal>ON CONFLICT</literal> clause specifies an
|
class="PARAMETER">index_predicate</replaceable> is required.
|
||||||
alternative action to raising a unique violation or exclusion
|
</para>
|
||||||
constraint violation error. For each individual row proposed for
|
</listitem>
|
||||||
insertion, either the insertion proceeds, or, if a constraint
|
</varlistentry>
|
||||||
specified by the <parameter>conflict_target</parameter> is
|
|
||||||
violated, the alternative <parameter>conflict_action</parameter> is
|
|
||||||
taken.
|
|
||||||
</para>
|
|
||||||
|
|
||||||
<para>
|
<varlistentry>
|
||||||
<parameter>conflict_target</parameter> describes which conflicts
|
<term><replaceable class="PARAMETER">constraint_name</replaceable></term>
|
||||||
are handled by the <literal>ON CONFLICT</literal> clause. Either a
|
<listitem>
|
||||||
<emphasis>unique index inference</emphasis> clause or an explicitly
|
<para>
|
||||||
named constraint can be used. For <literal>ON CONFLICT DO
|
Explicitly specifies an arbiter
|
||||||
NOTHING</literal>, it is optional to specify a
|
<emphasis>constraint</emphasis> by name, rather than inferring
|
||||||
<parameter>conflict_target</parameter>; when omitted, conflicts
|
a constraint or index.
|
||||||
with all usable constraints (and unique indexes) are handled. For
|
</para>
|
||||||
<literal>ON CONFLICT DO UPDATE</literal>, a conflict target
|
</listitem>
|
||||||
<emphasis>must</emphasis> be specified.
|
</varlistentry>
|
||||||
|
|
||||||
Every time an insertion without <literal>ON CONFLICT</literal>
|
<varlistentry>
|
||||||
would ordinarily raise an error due to violating one of the
|
<term><replaceable class="PARAMETER">condition</replaceable></term>
|
||||||
inferred (or explicitly named) constraints, a conflict (as in
|
<listitem>
|
||||||
<literal>ON CONFLICT</literal>) occurs, and the alternative action,
|
<para>
|
||||||
as specified by <parameter>conflict_action</parameter> is taken.
|
An expression that returns a value of type
|
||||||
This happens on a row-by-row basis.
|
<type>boolean</type>. Only rows for which this expression
|
||||||
</para>
|
returns <literal>true</literal> will be updated, although all
|
||||||
|
rows will be locked when the <literal>ON CONFLICT DO UPDATE</>
|
||||||
|
action is taken. Note that
|
||||||
|
<replaceable>condition</replaceable> is evaluated last, after
|
||||||
|
a conflict has been identified as a candidate to update.
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
|
</variablelist>
|
||||||
|
<para>
|
||||||
|
Note that exclusion constraints are not supported as arbiters with
|
||||||
|
<literal>ON CONFLICT DO UPDATE</literal>. In all cases, only
|
||||||
|
<literal>NOT DEFERRABLE</literal> constraints and unique indexes
|
||||||
|
are supported as arbiters.
|
||||||
|
</para>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
A <emphasis>unique index inference</emphasis> clause consists of
|
<command>INSERT</command> with an <literal>ON CONFLICT DO UPDATE</>
|
||||||
one or more <replaceable
|
clause is a <quote>deterministic</quote> statement. This means
|
||||||
class="PARAMETER">column_name_index</replaceable> columns and/or
|
that the command will not be allowed to affect any single existing
|
||||||
<replaceable class="PARAMETER">expression_index</replaceable>
|
row more than once; a cardinality violation error will be raised
|
||||||
expressions, and an optional <replaceable class="PARAMETER">
|
when this situation arises. Rows proposed for insertion should
|
||||||
index_predicate</replaceable>.
|
not duplicate each other in terms of attributes constrained by an
|
||||||
</para>
|
arbiter index or constraint. Note that exclusion constraints are
|
||||||
|
not supported with <literal>ON CONFLICT DO UPDATE</literal>.
|
||||||
|
</para>
|
||||||
|
<tip>
|
||||||
|
<para>
|
||||||
|
It is often preferable to use unique index inference rather than
|
||||||
|
naming a constraint directly using <literal>ON CONFLICT ON
|
||||||
|
CONSTRAINT</literal> <replaceable class="PARAMETER">
|
||||||
|
constraint_name</replaceable>. Inference will continue to work
|
||||||
|
correctly when the underlying index is replaced by another more
|
||||||
|
or less equivalent index in an overlapping way, for example when
|
||||||
|
using <literal>CREATE UNIQUE INDEX ... CONCURRENTLY</literal>
|
||||||
|
before dropping the index being replaced.
|
||||||
|
</para>
|
||||||
|
</tip>
|
||||||
|
|
||||||
<para>
|
</refsect2>
|
||||||
All the <replaceable class="PARAMETER">table_name</replaceable>
|
|
||||||
unique indexes that, without regard to order, contain exactly the
|
|
||||||
specified columns/expressions and, if specified, whose predicate
|
|
||||||
implies the <replaceable class="PARAMETER">
|
|
||||||
index_predicate</replaceable> are chosen as arbiter indexes. Note
|
|
||||||
that this means an index without a predicate will be used if a
|
|
||||||
non-partial index matching every other criteria happens to be
|
|
||||||
available.
|
|
||||||
</para>
|
|
||||||
|
|
||||||
<para>
|
|
||||||
If no index matches the inference clause (nor is there a constraint
|
|
||||||
explicitly named), an error is raised. Deferred constraints are
|
|
||||||
not supported as arbiters.
|
|
||||||
</para>
|
|
||||||
|
|
||||||
<para>
|
|
||||||
<parameter>conflict_action</parameter> defines the action to be
|
|
||||||
taken in case of conflict. <literal>ON CONFLICT DO
|
|
||||||
NOTHING</literal> simply avoids inserting a row as its alternative
|
|
||||||
action. <literal>ON CONFLICT DO UPDATE</literal> updates the
|
|
||||||
existing row that conflicts with the row proposed for insertion as
|
|
||||||
its alternative action.
|
|
||||||
|
|
||||||
<literal>ON CONFLICT DO UPDATE</literal> guarantees an atomic
|
|
||||||
<command>INSERT</command> or <command>UPDATE</command> outcome - provided
|
|
||||||
there is no independent error, one of those two outcomes is guaranteed,
|
|
||||||
even under high concurrency. This feature is also known as
|
|
||||||
<firstterm>UPSERT</firstterm>.
|
|
||||||
|
|
||||||
Note that exclusion constraints are not supported with
|
|
||||||
<literal>ON CONFLICT DO UPDATE</literal>.
|
|
||||||
</para>
|
|
||||||
|
|
||||||
<para>
|
|
||||||
<literal>ON CONFLICT DO UPDATE</literal> optionally accepts
|
|
||||||
a <literal>WHERE</literal> clause <replaceable>condition</replaceable>.
|
|
||||||
When provided, the statement only proceeds with updating if
|
|
||||||
the <replaceable>condition</replaceable> is satisfied. Otherwise, unlike a
|
|
||||||
conventional <command>UPDATE</command>, the row is still locked for update.
|
|
||||||
Note that the <replaceable>condition</replaceable> is evaluated last, after
|
|
||||||
a conflict has been identified as a candidate to update.
|
|
||||||
</para>
|
|
||||||
|
|
||||||
<para>
|
|
||||||
The <literal>SET</literal> and <literal>WHERE</literal> clauses in
|
|
||||||
<literal>ON CONFLICT UPDATE</literal> have access to the existing
|
|
||||||
row, using the table's name, and to the row
|
|
||||||
proposed for insertion, using the <varname>excluded</varname>
|
|
||||||
alias. The <varname>excluded</varname> alias requires
|
|
||||||
<literal>SELECT</> privilege on any column whose values are read.
|
|
||||||
|
|
||||||
Note that the effects of all per-row <literal>BEFORE INSERT</literal>
|
|
||||||
triggers are reflected in <varname>excluded</varname> values, since those
|
|
||||||
effects may have contributed to the row being excluded from insertion.
|
|
||||||
</para>
|
|
||||||
|
|
||||||
<para>
|
|
||||||
<command>INSERT</command> with an <literal>ON CONFLICT DO UPDATE</>
|
|
||||||
clause is a <quote>deterministic</quote> statement. This means
|
|
||||||
that the command will not be allowed to affect any single existing
|
|
||||||
row more than once; a cardinality violation error will be raised
|
|
||||||
when this situation arises. Rows proposed for insertion should not
|
|
||||||
duplicate each other in terms of attributes constrained by the
|
|
||||||
conflict-arbitrating unique index.
|
|
||||||
</para>
|
|
||||||
</refsect1>
|
</refsect1>
|
||||||
|
|
||||||
<refsect1>
|
<refsect1>
|
||||||
@ -617,12 +630,12 @@ INSERT INTO employees_log SELECT *, current_timestamp FROM upd;
|
|||||||
<para>
|
<para>
|
||||||
Insert or update new distributors as appropriate. Assumes a unique
|
Insert or update new distributors as appropriate. Assumes a unique
|
||||||
index has been defined that constrains values appearing in the
|
index has been defined that constrains values appearing in the
|
||||||
<literal>did</literal> column. Note that an <varname>EXCLUDED</>
|
<literal>did</literal> column. Note that the special
|
||||||
expression is used to reference values originally proposed for
|
<varname>excluded</> table is used to reference values originally
|
||||||
insertion:
|
proposed for insertion:
|
||||||
<programlisting>
|
<programlisting>
|
||||||
INSERT INTO distributors (did, dname)
|
INSERT INTO distributors (did, dname)
|
||||||
VALUES (5, 'Gizmo transglobal'), (6, 'Associated Computing, inc')
|
VALUES (5, 'Gizmo Transglobal'), (6, 'Associated Computing, Inc')
|
||||||
ON CONFLICT (did) DO UPDATE SET dname = EXCLUDED.dname;
|
ON CONFLICT (did) DO UPDATE SET dname = EXCLUDED.dname;
|
||||||
</programlisting>
|
</programlisting>
|
||||||
</para>
|
</para>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user