Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-updates > by-pkgid > 5fea23694c765462b86d6ddf74461eab > files > 881

postgresql9.6-docs-9.6.22-1.mga7.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>INSERT</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.6.22 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="SQL Commands"
HREF="sql-commands.html"><LINK
REL="PREVIOUS"
TITLE="IMPORT FOREIGN SCHEMA"
HREF="sql-importforeignschema.html"><LINK
REL="NEXT"
TITLE="LISTEN"
HREF="sql-listen.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2021-05-18T09:16:10"></HEAD
><BODY
CLASS="REFENTRY"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="4"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.6.22 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="IMPORT FOREIGN SCHEMA"
HREF="sql-importforeignschema.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-commands.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="LISTEN"
HREF="sql-listen.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="SQL-INSERT"
></A
>INSERT</H1
><DIV
CLASS="REFNAMEDIV"
><A
NAME="AEN89277"
></A
><H2
>Name</H2
>INSERT&nbsp;--&nbsp;create new rows in a table</DIV
><DIV
CLASS="REFSYNOPSISDIV"
><A
NAME="AEN89280"
></A
><H2
>Synopsis</H2
><PRE
CLASS="SYNOPSIS"
>[ WITH [ RECURSIVE ] <TT
CLASS="REPLACEABLE"
><I
>with_query</I
></TT
> [, ...] ]
INSERT INTO <TT
CLASS="REPLACEABLE"
><I
>table_name</I
></TT
> [ AS <TT
CLASS="REPLACEABLE"
><I
>alias</I
></TT
> ] [ ( <TT
CLASS="REPLACEABLE"
><I
>column_name</I
></TT
> [, ...] ) ]
    { DEFAULT VALUES | VALUES ( { <TT
CLASS="REPLACEABLE"
><I
>expression</I
></TT
> | DEFAULT } [, ...] ) [, ...] | <TT
CLASS="REPLACEABLE"
><I
>query</I
></TT
> }
    [ ON CONFLICT [ <TT
CLASS="REPLACEABLE"
><I
>conflict_target</I
></TT
> ] <TT
CLASS="REPLACEABLE"
><I
>conflict_action</I
></TT
> ]
    [ RETURNING * | <TT
CLASS="REPLACEABLE"
><I
>output_expression</I
></TT
> [ [ AS ] <TT
CLASS="REPLACEABLE"
><I
>output_name</I
></TT
> ] [, ...] ]

<SPAN
CLASS="phrase"
><SPAN
CLASS="PHRASE"
>where <TT
CLASS="REPLACEABLE"
><I
>conflict_target</I
></TT
> can be one of:</SPAN
></SPAN
>

    ( { <TT
CLASS="REPLACEABLE"
><I
>index_column_name</I
></TT
> | ( <TT
CLASS="REPLACEABLE"
><I
>index_expression</I
></TT
> ) } [ COLLATE <TT
CLASS="REPLACEABLE"
><I
>collation</I
></TT
> ] [ <TT
CLASS="REPLACEABLE"
><I
>opclass</I
></TT
> ] [, ...] ) [ WHERE <TT
CLASS="REPLACEABLE"
><I
>index_predicate</I
></TT
> ]
    ON CONSTRAINT <TT
CLASS="REPLACEABLE"
><I
>constraint_name</I
></TT
>

<SPAN
CLASS="phrase"
><SPAN
CLASS="PHRASE"
>and <TT
CLASS="REPLACEABLE"
><I
>conflict_action</I
></TT
> is one of:</SPAN
></SPAN
>

    DO NOTHING
    DO UPDATE SET { <TT
CLASS="REPLACEABLE"
><I
>column_name</I
></TT
> = { <TT
CLASS="REPLACEABLE"
><I
>expression</I
></TT
> | DEFAULT } |
                    ( <TT
CLASS="REPLACEABLE"
><I
>column_name</I
></TT
> [, ...] ) = ( { <TT
CLASS="REPLACEABLE"
><I
>expression</I
></TT
> | DEFAULT } [, ...] ) |
                    ( <TT
CLASS="REPLACEABLE"
><I
>column_name</I
></TT
> [, ...] ) = ( <TT
CLASS="REPLACEABLE"
><I
>sub-SELECT</I
></TT
> )
                  } [, ...]
              [ WHERE <TT
CLASS="REPLACEABLE"
><I
>condition</I
></TT
> ]</PRE
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN89309"
></A
><H2
>Description</H2
><P
>   <TT
CLASS="COMMAND"
>INSERT</TT
> inserts new rows into a table.
   One can insert one or more rows specified by value expressions,
   or zero or more rows resulting from a query.
  </P
><P
>   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; or the first <TT
CLASS="REPLACEABLE"
><I
>N</I
></TT
> column
   names, if there are only <TT
CLASS="REPLACEABLE"
><I
>N</I
></TT
> columns supplied by the
   <TT
CLASS="LITERAL"
>VALUES</TT
> clause or <TT
CLASS="REPLACEABLE"
><I
>query</I
></TT
>.  The values
   supplied by the <TT
CLASS="LITERAL"
>VALUES</TT
> clause or <TT
CLASS="REPLACEABLE"
><I
>query</I
></TT
> are
   associated with the explicit or implicit column list left-to-right.
  </P
><P
>   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.
  </P
><P
>   If the expression for any column is not of the correct data type,
   automatic type conversion will be attempted.
  </P
><P
>   <TT
CLASS="LITERAL"
>ON CONFLICT</TT
> can be used to specify an alternative
   action to raising a unique constraint or exclusion constraint
   violation error. (See <A
HREF="sql-insert.html#SQL-ON-CONFLICT"
><I
><I
>ON CONFLICT</I
> Clause</I
></A
> below.)
  </P
><P
>   The optional <TT
CLASS="LITERAL"
>RETURNING</TT
> clause causes <TT
CLASS="COMMAND"
>INSERT</TT
>
   to compute and return value(s) based on each row actually inserted
   (or updated, if an <TT
CLASS="LITERAL"
>ON CONFLICT DO UPDATE</TT
> clause was
   used).  This is primarily useful for obtaining values that were
   supplied by defaults, such as a serial sequence number.  However,
   any expression using the table's columns is allowed.  The syntax of
   the <TT
CLASS="LITERAL"
>RETURNING</TT
> list is identical to that of the output
   list of <TT
CLASS="COMMAND"
>SELECT</TT
>.  Only rows that were successfully
   inserted or updated will be returned.  For example, if a row was
   locked but not updated because an <TT
CLASS="LITERAL"
>ON CONFLICT DO UPDATE
   ... WHERE</TT
> clause <TT
CLASS="REPLACEABLE"
><I
>condition</I
></TT
> was not satisfied, the
   row will not be returned.
  </P
><P
>   You must have <TT
CLASS="LITERAL"
>INSERT</TT
> privilege on a table in
   order to insert into it.  If <TT
CLASS="LITERAL"
>ON CONFLICT DO UPDATE</TT
> is
   present, <TT
CLASS="LITERAL"
>UPDATE</TT
> privilege on the table is also
   required.
  </P
><P
>   If a column list is specified, you only need
   <TT
CLASS="LITERAL"
>INSERT</TT
> privilege on the listed columns.
   Similarly, when <TT
CLASS="LITERAL"
>ON CONFLICT DO UPDATE</TT
> is specified, you
   only need <TT
CLASS="LITERAL"
>UPDATE</TT
> privilege on the column(s) that are
   listed to be updated.  However, <TT
CLASS="LITERAL"
>ON CONFLICT DO UPDATE</TT
>
   also requires <TT
CLASS="LITERAL"
>SELECT</TT
> privilege on any column whose
   values are read in the <TT
CLASS="LITERAL"
>ON CONFLICT DO UPDATE</TT
>
   expressions or <TT
CLASS="REPLACEABLE"
><I
>condition</I
></TT
>.
  </P
><P
>   Use of the <TT
CLASS="LITERAL"
>RETURNING</TT
> clause requires <TT
CLASS="LITERAL"
>SELECT</TT
>
   privilege on all columns mentioned in <TT
CLASS="LITERAL"
>RETURNING</TT
>.
   If you use the <TT
CLASS="REPLACEABLE"
><I
>query</I
></TT
> clause to insert rows from a
   query, you of course need to have <TT
CLASS="LITERAL"
>SELECT</TT
> privilege on
   any table or column used in the query.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN89351"
></A
><H2
>Parameters</H2
><DIV
CLASS="REFSECT2"
><A
NAME="SQL-INSERTING-PARAMS"
></A
><H3
>Inserting</H3
><P
>    This section covers parameters that may be used when only
    inserting new rows.  Parameters <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>exclusively</I
></SPAN
>
    used with the <TT
CLASS="LITERAL"
>ON CONFLICT</TT
> clause are described
    separately.
   </P
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="REPLACEABLE"
><I
>with_query</I
></TT
></DT
><DD
><P
>        The <TT
CLASS="LITERAL"
>WITH</TT
> clause allows you to specify one or more
        subqueries that can be referenced by name in the <TT
CLASS="COMMAND"
>INSERT</TT
>
        query. See <A
HREF="queries-with.html"
>Section 7.8</A
> and <A
HREF="sql-select.html"
>SELECT</A
>
        for details.
       </P
><P
>        It is possible for the <TT
CLASS="REPLACEABLE"
><I
>query</I
></TT
>
        (<TT
CLASS="COMMAND"
>SELECT</TT
> statement)
        to also contain a <TT
CLASS="LITERAL"
>WITH</TT
> clause.  In such a case both
        sets of <TT
CLASS="REPLACEABLE"
><I
>with_query</I
></TT
> can be referenced within
        the <TT
CLASS="REPLACEABLE"
><I
>query</I
></TT
>, but the
        second one takes precedence since it is more closely nested.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>table_name</I
></TT
></DT
><DD
><P
>        The name (optionally schema-qualified) of an existing table.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>alias</I
></TT
></DT
><DD
><P
>        A substitute name for <TT
CLASS="REPLACEABLE"
><I
>table_name</I
></TT
>.  When an alias is
        provided, it completely hides the actual name of the table.
        This is particularly useful when <TT
CLASS="LITERAL"
>ON CONFLICT DO
        UPDATE</TT
> targets a table named excluded, since that's
        also the name of the special table representing rows proposed
        for insertion.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>column_name</I
></TT
></DT
><DD
><P
>        The name of a column in the table named by <TT
CLASS="REPLACEABLE"
><I
>table_name</I
></TT
>.  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.)  When referencing a
        column with <TT
CLASS="LITERAL"
>ON CONFLICT DO UPDATE</TT
>, do not include
        the table's name in the specification of a target column.  For
        example, <TT
CLASS="LITERAL"
>INSERT INTO table_name ... ON CONFLICT DO UPDATE
        SET table_name.col = 1</TT
> is invalid (this follows the general
        behavior for <TT
CLASS="COMMAND"
>UPDATE</TT
>).
       </P
></DD
><DT
><TT
CLASS="LITERAL"
>DEFAULT VALUES</TT
></DT
><DD
><P
>        All columns will be filled with their default values.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>expression</I
></TT
></DT
><DD
><P
>        An expression or value to assign to the corresponding column.
       </P
></DD
><DT
><TT
CLASS="LITERAL"
>DEFAULT</TT
></DT
><DD
><P
>        The corresponding column will be filled with
        its default value.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>query</I
></TT
></DT
><DD
><P
>        A query (<TT
CLASS="COMMAND"
>SELECT</TT
> statement) that supplies the
        rows to be inserted.  Refer to the
        <A
HREF="sql-select.html"
>SELECT</A
>
        statement for a description of the syntax.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>output_expression</I
></TT
></DT
><DD
><P
>        An expression to be computed and returned by the
        <TT
CLASS="COMMAND"
>INSERT</TT
> command after each row is inserted or
        updated. The expression can use any column names of the table
        named by <TT
CLASS="REPLACEABLE"
><I
>table_name</I
></TT
>.  Write
        <TT
CLASS="LITERAL"
>*</TT
> to return all columns of the inserted or updated
        row(s).
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>output_name</I
></TT
></DT
><DD
><P
>        A name to use for a returned column.
       </P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="REFSECT2"
><A
NAME="SQL-ON-CONFLICT"
></A
><H3
><TT
CLASS="LITERAL"
>ON CONFLICT</TT
> Clause</H3
><P
>    The optional <TT
CLASS="LITERAL"
>ON CONFLICT</TT
> 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
    <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>arbiter</I
></SPAN
> constraint or index specified by
    <TT
CLASS="PARAMETER"
>conflict_target</TT
> is violated, the
    alternative <TT
CLASS="PARAMETER"
>conflict_action</TT
> is taken.
    <TT
CLASS="LITERAL"
>ON CONFLICT DO NOTHING</TT
> simply avoids inserting
    a row as its alternative action.  <TT
CLASS="LITERAL"
>ON CONFLICT DO
    UPDATE</TT
> updates the existing row that conflicts with the
    row proposed for insertion as its alternative action.
   </P
><P
>    <TT
CLASS="PARAMETER"
>conflict_target</TT
> can perform
    <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>unique index inference</I
></SPAN
>.  When performing
    inference, it consists of one or more <TT
CLASS="REPLACEABLE"
><I
>index_column_name</I
></TT
> columns and/or
    <TT
CLASS="REPLACEABLE"
><I
>index_expression</I
></TT
>
    expressions, and an optional <TT
CLASS="REPLACEABLE"
><I
>index_predicate</I
></TT
>.  All <TT
CLASS="REPLACEABLE"
><I
>table_name</I
></TT
> unique indexes that,
    without regard to order, contain exactly the
    <TT
CLASS="PARAMETER"
>conflict_target</TT
>-specified
    columns/expressions are inferred (chosen) as arbiter indexes.  If
    an <TT
CLASS="REPLACEABLE"
><I
>index_predicate</I
></TT
> 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 <TT
CLASS="LITERAL"
>ON CONFLICT</TT
>) if such an index
    satisfying every other criteria is available.  If an attempt at
    inference is unsuccessful, an error is raised.
   </P
><P
>    <TT
CLASS="LITERAL"
>ON CONFLICT DO UPDATE</TT
> guarantees an atomic
    <TT
CLASS="COMMAND"
>INSERT</TT
> or <TT
CLASS="COMMAND"
>UPDATE</TT
> outcome;
    provided there is no independent error, one of those two outcomes
    is guaranteed, even under high concurrency.  This is also known as
    <I
CLASS="FIRSTTERM"
>UPSERT</I
> &mdash; <SPAN
CLASS="QUOTE"
>"UPDATE or
    INSERT"</SPAN
>.
   </P
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="REPLACEABLE"
><I
>conflict_target</I
></TT
></DT
><DD
><P
>        Specifies which conflicts <TT
CLASS="LITERAL"
>ON CONFLICT</TT
> takes
        the alternative action on by choosing <I
CLASS="FIRSTTERM"
>arbiter
        indexes</I
>.  Either performs <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>unique index
        inference</I
></SPAN
>, or names a constraint explicitly.  For
        <TT
CLASS="LITERAL"
>ON CONFLICT DO NOTHING</TT
>, it is optional to
        specify a <TT
CLASS="PARAMETER"
>conflict_target</TT
>; when
        omitted, conflicts with all usable constraints (and unique
        indexes) are handled.  For <TT
CLASS="LITERAL"
>ON CONFLICT DO
        UPDATE</TT
>, a <TT
CLASS="PARAMETER"
>conflict_target</TT
>
        <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>must</I
></SPAN
> be provided.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>conflict_action</I
></TT
></DT
><DD
><P
>        <TT
CLASS="PARAMETER"
>conflict_action</TT
> specifies an
        alternative <TT
CLASS="LITERAL"
>ON CONFLICT</TT
> action.  It can be
        either <TT
CLASS="LITERAL"
>DO NOTHING</TT
>, or a <TT
CLASS="LITERAL"
>DO
        UPDATE</TT
> clause specifying the exact details of the
        <TT
CLASS="LITERAL"
>UPDATE</TT
> action to be performed in case of a
        conflict.  The <TT
CLASS="LITERAL"
>SET</TT
> and
        <TT
CLASS="LITERAL"
>WHERE</TT
> clauses in <TT
CLASS="LITERAL"
>ON CONFLICT DO
        UPDATE</TT
> have access to the existing row using the
        table's name (or an alias), and to rows proposed for insertion
        using the special <TT
CLASS="VARNAME"
>excluded</TT
> table.
        <TT
CLASS="LITERAL"
>SELECT</TT
> privilege is required on any column in the
        target table where corresponding <TT
CLASS="VARNAME"
>excluded</TT
>
        columns are read.
       </P
><P
>        Note that the effects of all per-row <TT
CLASS="LITERAL"
>BEFORE
        INSERT</TT
> triggers are reflected in
        <TT
CLASS="VARNAME"
>excluded</TT
> values, since those effects may
        have contributed to the row being excluded from insertion.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>index_column_name</I
></TT
></DT
><DD
><P
>        The name of a <TT
CLASS="REPLACEABLE"
><I
>table_name</I
></TT
> column.  Used to
        infer arbiter indexes.  Follows <TT
CLASS="COMMAND"
>CREATE
        INDEX</TT
> format.  <TT
CLASS="LITERAL"
>SELECT</TT
> privilege on
        <TT
CLASS="REPLACEABLE"
><I
>index_column_name</I
></TT
>
        is required.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>index_expression</I
></TT
></DT
><DD
><P
>        Similar to <TT
CLASS="REPLACEABLE"
><I
>index_column_name</I
></TT
>, but used to
        infer expressions on <TT
CLASS="REPLACEABLE"
><I
>table_name</I
></TT
> columns appearing
        within index definitions (not simple columns).  Follows
        <TT
CLASS="COMMAND"
>CREATE INDEX</TT
> format.  <TT
CLASS="LITERAL"
>SELECT</TT
>
        privilege on any column appearing within <TT
CLASS="REPLACEABLE"
><I
>index_expression</I
></TT
> is required.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>collation</I
></TT
></DT
><DD
><P
>        When specified, mandates that corresponding <TT
CLASS="REPLACEABLE"
><I
>index_column_name</I
></TT
> or
        <TT
CLASS="REPLACEABLE"
><I
>index_expression</I
></TT
>
        use a particular collation in order to be matched during
        inference.  Typically this is omitted, as collations usually
        do not affect whether or not a constraint violation occurs.
        Follows <TT
CLASS="COMMAND"
>CREATE INDEX</TT
> format.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>opclass</I
></TT
></DT
><DD
><P
>        When specified, mandates that corresponding <TT
CLASS="REPLACEABLE"
><I
>index_column_name</I
></TT
> or
        <TT
CLASS="REPLACEABLE"
><I
>index_expression</I
></TT
>
        use particular operator class in order to be matched during
        inference.  Typically this is omitted,  as the
        <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>equality</I
></SPAN
> semantics are often equivalent
        across a type's operator classes anyway, or because it's
        sufficient to trust that the defined unique indexes have the
        pertinent definition of equality.  Follows <TT
CLASS="COMMAND"
>CREATE
        INDEX</TT
> format.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>index_predicate</I
></TT
></DT
><DD
><P
>        Used to allow inference of partial unique indexes.  Any
        indexes that satisfy the predicate (which need not actually be
        partial indexes) can be inferred.  Follows <TT
CLASS="COMMAND"
>CREATE
        INDEX</TT
> format.  <TT
CLASS="LITERAL"
>SELECT</TT
> privilege on any
        column appearing within <TT
CLASS="REPLACEABLE"
><I
>index_predicate</I
></TT
> is required.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>constraint_name</I
></TT
></DT
><DD
><P
>        Explicitly specifies an arbiter
        <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>constraint</I
></SPAN
> by name, rather than inferring
        a constraint or index.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>condition</I
></TT
></DT
><DD
><P
>        An expression that returns a value of type
        <TT
CLASS="TYPE"
>boolean</TT
>.  Only rows for which this expression
        returns <TT
CLASS="LITERAL"
>true</TT
> will be updated, although all
        rows will be locked when the <TT
CLASS="LITERAL"
>ON CONFLICT DO UPDATE</TT
>
        action is taken.  Note that
        <TT
CLASS="REPLACEABLE"
><I
>condition</I
></TT
> is evaluated last, after
        a conflict has been identified as a candidate to update.
       </P
></DD
></DL
></DIV
><P
>    Note that exclusion constraints are not supported as arbiters with
    <TT
CLASS="LITERAL"
>ON CONFLICT DO UPDATE</TT
>. In all cases, only
    <TT
CLASS="LITERAL"
>NOT DEFERRABLE</TT
> constraints and unique indexes
    are supported as arbiters.
   </P
><P
>    <TT
CLASS="COMMAND"
>INSERT</TT
> with an <TT
CLASS="LITERAL"
>ON CONFLICT DO UPDATE</TT
>
    clause is a <SPAN
CLASS="QUOTE"
>"deterministic"</SPAN
> 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 an
    arbiter index or constraint.
   </P
><DIV
CLASS="TIP"
><BLOCKQUOTE
CLASS="TIP"
><P
><B
>Tip: </B
>     It is often preferable to use unique index inference rather than
     naming a constraint directly using <TT
CLASS="LITERAL"
>ON CONFLICT ON
     CONSTRAINT</TT
> <TT
CLASS="REPLACEABLE"
><I
>     constraint_name</I
></TT
>.  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 <TT
CLASS="LITERAL"
>CREATE UNIQUE INDEX ...  CONCURRENTLY</TT
>
     before dropping the index being replaced.
    </P
></BLOCKQUOTE
></DIV
></DIV
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN89564"
></A
><H2
>Outputs</H2
><P
>   On successful completion, an <TT
CLASS="COMMAND"
>INSERT</TT
> command returns a command
   tag of the form
</P><PRE
CLASS="SCREEN"
>INSERT <TT
CLASS="REPLACEABLE"
><I
>oid</I
></TT
> <TT
CLASS="REPLACEABLE"
><I
>count</I
></TT
></PRE
><P>
   The <TT
CLASS="REPLACEABLE"
><I
>count</I
></TT
> is the
   number of rows inserted or updated.  If <TT
CLASS="REPLACEABLE"
><I
>count</I
></TT
> is exactly one, and the
   target table has OIDs, then <TT
CLASS="REPLACEABLE"
><I
>oid</I
></TT
> is the <ACRONYM
CLASS="ACRONYM"
>OID</ACRONYM
>
   assigned to the inserted row.  The single row must have been
   inserted rather than updated.  Otherwise <TT
CLASS="REPLACEABLE"
><I
>oid</I
></TT
> is zero.
  </P
><P
>   If the <TT
CLASS="COMMAND"
>INSERT</TT
> command contains a <TT
CLASS="LITERAL"
>RETURNING</TT
>
   clause, the result will be similar to that of a <TT
CLASS="COMMAND"
>SELECT</TT
>
   statement containing the columns and values defined in the
   <TT
CLASS="LITERAL"
>RETURNING</TT
> list, computed over the row(s) inserted or
   updated by the command.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN89581"
></A
><H2
>Examples</H2
><P
>   Insert a single row into table <TT
CLASS="LITERAL"
>films</TT
>:

</P><PRE
CLASS="PROGRAMLISTING"
>INSERT INTO films VALUES
    ('UA502', 'Bananas', 105, '1971-07-13', 'Comedy', '82 minutes');</PRE
><P>
  </P
><P
>   In this example, the <TT
CLASS="LITERAL"
>len</TT
> column is
   omitted and therefore it will have the default value:

</P><PRE
CLASS="PROGRAMLISTING"
>INSERT INTO films (code, title, did, date_prod, kind)
    VALUES ('T_601', 'Yojimbo', 106, '1961-06-16', 'Drama');</PRE
><P>
  </P
><P
>   This example uses the <TT
CLASS="LITERAL"
>DEFAULT</TT
> clause for
   the date columns rather than specifying a value:

</P><PRE
CLASS="PROGRAMLISTING"
>INSERT INTO films VALUES
    ('UA502', 'Bananas', 105, DEFAULT, 'Comedy', '82 minutes');
INSERT INTO films (code, title, did, date_prod, kind)
    VALUES ('T_601', 'Yojimbo', 106, DEFAULT, 'Drama');</PRE
><P>
  </P
><P
>   To insert a row consisting entirely of default values:

</P><PRE
CLASS="PROGRAMLISTING"
>INSERT INTO films DEFAULT VALUES;</PRE
><P>
  </P
><P
>   To insert multiple rows using the multirow <TT
CLASS="COMMAND"
>VALUES</TT
> syntax:

</P><PRE
CLASS="PROGRAMLISTING"
>INSERT INTO films (code, title, did, date_prod, kind) VALUES
    ('B6717', 'Tampopo', 110, '1985-02-10', 'Comedy'),
    ('HG120', 'The Dinner Game', 140, DEFAULT, 'Comedy');</PRE
><P>
  </P
><P
>   This example inserts some rows into table
   <TT
CLASS="LITERAL"
>films</TT
> from a table <TT
CLASS="LITERAL"
>tmp_films</TT
>
   with the same column layout as <TT
CLASS="LITERAL"
>films</TT
>:

</P><PRE
CLASS="PROGRAMLISTING"
>INSERT INTO films SELECT * FROM tmp_films WHERE date_prod &lt; '2004-05-07';</PRE
><P>
  </P
><P
>   This example inserts into array columns:

</P><PRE
CLASS="PROGRAMLISTING"
>-- Create an empty 3x3 gameboard for noughts-and-crosses
INSERT INTO tictactoe (game, board[1:3][1:3])
    VALUES (1, '{{" "," "," "},{" "," "," "},{" "," "," "}}');
-- The subscripts in the above example aren't really needed
INSERT INTO tictactoe (game, board)
    VALUES (2, '{{X," "," "},{" ",O," "},{" ",X," "}}');</PRE
><P>
  </P
><P
>   Insert a single row into table <TT
CLASS="LITERAL"
>distributors</TT
>, returning
   the sequence number generated by the <TT
CLASS="LITERAL"
>DEFAULT</TT
> clause:

</P><PRE
CLASS="PROGRAMLISTING"
>INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets')
   RETURNING did;</PRE
><P>
  </P
><P
>   Increment the sales count of the salesperson who manages the
   account for Acme Corporation, and record the whole updated row
   along with current time in a log table:
</P><PRE
CLASS="PROGRAMLISTING"
>WITH upd AS (
  UPDATE employees SET sales_count = sales_count + 1 WHERE id =
    (SELECT sales_person FROM accounts WHERE name = 'Acme Corporation')
    RETURNING *
)
INSERT INTO employees_log SELECT *, current_timestamp FROM upd;</PRE
><P>
  </P
><P
>   Insert or update new distributors as appropriate.  Assumes a unique
   index has been defined that constrains values appearing in the
   <TT
CLASS="LITERAL"
>did</TT
> column.  Note that the special
   <TT
CLASS="VARNAME"
>excluded</TT
> table is used to reference values originally
   proposed for insertion:
</P><PRE
CLASS="PROGRAMLISTING"
>INSERT INTO distributors (did, dname)
    VALUES (5, 'Gizmo Transglobal'), (6, 'Associated Computing, Inc')
    ON CONFLICT (did) DO UPDATE SET dname = EXCLUDED.dname;</PRE
><P>
  </P
><P
>   Insert a distributor, or do nothing for rows proposed for insertion
   when an existing, excluded row (a row with a matching constrained
   column or columns after before row insert triggers fire) exists.
   Example assumes a unique index has been defined that constrains
   values appearing in the <TT
CLASS="LITERAL"
>did</TT
> column:
</P><PRE
CLASS="PROGRAMLISTING"
>INSERT INTO distributors (did, dname) VALUES (7, 'Redline GmbH')
    ON CONFLICT (did) DO NOTHING;</PRE
><P>
  </P
><P
>   Insert or update new distributors as appropriate.  Example assumes
   a unique index has been defined that constrains values appearing in
   the <TT
CLASS="LITERAL"
>did</TT
> column.  <TT
CLASS="LITERAL"
>WHERE</TT
> clause is
   used to limit the rows actually updated (any existing row not
   updated will still be locked, though):
</P><PRE
CLASS="PROGRAMLISTING"
>-- Don't update existing distributors based in a certain ZIP code
INSERT INTO distributors AS d (did, dname) VALUES (8, 'Anvil Distribution')
    ON CONFLICT (did) DO UPDATE
    SET dname = EXCLUDED.dname || ' (formerly ' || d.dname || ')'
    WHERE d.zipcode &lt;&gt; '21201';

-- Name a constraint directly in the statement (uses associated
-- index to arbitrate taking the DO NOTHING action)
INSERT INTO distributors (did, dname) VALUES (9, 'Antwerp Design')
    ON CONFLICT ON CONSTRAINT distributors_pkey DO NOTHING;</PRE
><P>
  </P
><P
>   Insert new distributor if possible;  otherwise
   <TT
CLASS="LITERAL"
>DO NOTHING</TT
>.  Example assumes a unique index has been
   defined that constrains values appearing in the
   <TT
CLASS="LITERAL"
>did</TT
> column on a subset of rows where the
   <TT
CLASS="LITERAL"
>is_active</TT
> Boolean column evaluates to
   <TT
CLASS="LITERAL"
>true</TT
>:
</P><PRE
CLASS="PROGRAMLISTING"
>-- This statement could infer a partial unique index on "did"
-- with a predicate of "WHERE is_active", but it could also
-- just use a regular unique constraint on "did"
INSERT INTO distributors (did, dname) VALUES (10, 'Conrad International')
    ON CONFLICT (did) WHERE is_active DO NOTHING;</PRE
><P>
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN89627"
></A
><H2
>Compatibility</H2
><P
>   <TT
CLASS="COMMAND"
>INSERT</TT
> conforms to the SQL standard, except that
   the <TT
CLASS="LITERAL"
>RETURNING</TT
> clause is a
   <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> extension, as is the ability
   to use <TT
CLASS="LITERAL"
>WITH</TT
> with <TT
CLASS="COMMAND"
>INSERT</TT
>, and the ability to
   specify an alternative action with <TT
CLASS="LITERAL"
>ON CONFLICT</TT
>.
   Also, the case in
   which a column name list is omitted, but not all the columns are
   filled from the <TT
CLASS="LITERAL"
>VALUES</TT
> clause or <TT
CLASS="REPLACEABLE"
><I
>query</I
></TT
>,
   is disallowed by the standard.
  </P
><P
>   Possible limitations of the <TT
CLASS="REPLACEABLE"
><I
>query</I
></TT
> clause are documented under
   <A
HREF="sql-select.html"
>SELECT</A
>.
  </P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-importforeignschema.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="sql-listen.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>IMPORT FOREIGN SCHEMA</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="sql-commands.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>LISTEN</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>