3
.\" Author: The PostgreSQL Global Development Group
4
.\" Generator: DocBook XSL Stylesheets v1.79.1 <http://docbook.sf.net/>
6
.\" Manual: PostgreSQL 11beta1 Documentation
7
.\" Source: PostgreSQL 11beta1
10
.TH "INSERT" "7" "2018" "PostgreSQL 11beta1" "PostgreSQL 11beta1 Documentation"
11
.\" -----------------------------------------------------------------
12
.\" * Define some portability stuff
13
.\" -----------------------------------------------------------------
14
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15
.\" http://bugs.debian.org/507673
16
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20
.\" -----------------------------------------------------------------
21
.\" * set default formatting
22
.\" -----------------------------------------------------------------
23
.\" disable hyphenation
25
.\" disable justification (adjust text to left margin only)
27
.\" -----------------------------------------------------------------
28
.\" * MAIN CONTENT STARTS HERE *
29
.\" -----------------------------------------------------------------
31
INSERT \- create new rows in a table
35
[ WITH [ RECURSIVE ] \fIwith_query\fR [, \&.\&.\&.] ]
36
INSERT INTO \fItable_name\fR [ AS \fIalias\fR ] [ ( \fIcolumn_name\fR [, \&.\&.\&.] ) ]
37
[ OVERRIDING { SYSTEM | USER} VALUE ]
38
{ DEFAULT VALUES | VALUES ( { \fIexpression\fR | DEFAULT } [, \&.\&.\&.] ) [, \&.\&.\&.] | \fIquery\fR }
39
[ ON CONFLICT [ \fIconflict_target\fR ] \fIconflict_action\fR ]
40
[ RETURNING * | \fIoutput_expression\fR [ [ AS ] \fIoutput_name\fR ] [, \&.\&.\&.] ]
42
where \fIconflict_target\fR can be one of:
44
( { \fIindex_column_name\fR | ( \fIindex_expression\fR ) } [ COLLATE \fIcollation\fR ] [ \fIopclass\fR ] [, \&.\&.\&.] ) [ WHERE \fIindex_predicate\fR ]
45
ON CONSTRAINT \fIconstraint_name\fR
47
and \fIconflict_action\fR is one of:
50
DO UPDATE SET { \fIcolumn_name\fR = { \fIexpression\fR | DEFAULT } |
51
( \fIcolumn_name\fR [, \&.\&.\&.] ) = [ ROW ] ( { \fIexpression\fR | DEFAULT } [, \&.\&.\&.] ) |
52
( \fIcolumn_name\fR [, \&.\&.\&.] ) = ( \fIsub\-SELECT\fR )
54
[ WHERE \fIcondition\fR ]
59
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\&.
61
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
63
column names, if there are only
65
columns supplied by the
68
\fIquery\fR\&. The values supplied by the
72
are associated with the explicit or implicit column list left\-to\-right\&.
74
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\&.
76
If the expression for any column is not of the correct data type, automatic type conversion will be attempted\&.
79
can be used to specify an alternative action to raising a unique constraint or exclusion constraint violation error\&. (See
87
to compute and return value(s) based on each row actually inserted (or updated, if an
89
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\*(Aqs columns is allowed\&. The syntax of the
91
list is identical to that of the output list of
92
\fBSELECT\fR\&. Only rows that were successfully inserted or updated will be returned\&. For example, if a row was locked but not updated because an
93
ON CONFLICT DO UPDATE \&.\&.\&. WHERE
96
was not satisfied, the row will not be returned\&.
100
privilege on a table in order to insert into it\&. If
101
ON CONFLICT DO UPDATE
104
privilege on the table is also required\&.
106
If a column list is specified, you only need
108
privilege on the listed columns\&. Similarly, when
109
ON CONFLICT DO UPDATE
110
is specified, you only need
112
privilege on the column(s) that are listed to be updated\&. However,
113
ON CONFLICT DO UPDATE
116
privilege on any column whose values are read in the
117
ON CONFLICT DO UPDATE
125
privilege on all columns mentioned in
126
RETURNING\&. If you use the
128
clause to insert rows from a query, you of course need to have
130
privilege on any table or column used in the query\&.
134
This section covers parameters that may be used when only inserting new rows\&. Parameters
138
clause are described separately\&.
144
clause allows you to specify one or more subqueries that can be referenced by name in the
152
It is possible for the
155
statement) to also contain a
157
clause\&. In such a case both sets of
159
can be referenced within the
160
\fIquery\fR, but the second one takes precedence since it is more closely nested\&.
165
The name (optionally schema\-qualified) of an existing table\&.
170
A substitute name for
171
\fItable_name\fR\&. When an alias is provided, it completely hides the actual name of the table\&. This is particularly useful when
172
ON CONFLICT DO UPDATE
173
targets a table named
174
\fIexcluded\fR, since that will otherwise be taken as the name of the special table representing rows proposed for insertion\&.
179
The name of a column in the table named by
180
\fItable_name\fR\&. 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
181
ON CONFLICT DO UPDATE, do not include the table\*(Aqs name in the specification of a target column\&. For example,
182
INSERT INTO table_name \&.\&.\&. ON CONFLICT DO UPDATE SET table_name\&.col = 1
183
is invalid (this follows the general behavior for
187
OVERRIDING SYSTEM VALUE
189
Without this clause, it is an error to specify an explicit value (other than
190
DEFAULT) for an identity column defined as
191
GENERATED ALWAYS\&. This clause overrides that restriction\&.
194
OVERRIDING USER VALUE
196
If this clause is specified, then any values supplied for identity columns defined as
198
are ignored and the default sequence\-generated values are applied\&.
200
This clause is useful for example when copying values between tables\&. Writing
201
INSERT INTO tbl2 OVERRIDING USER VALUE SELECT * FROM tbl1
204
all columns that are not identity columns in
206
while values for the identity columns in
208
will be generated by the sequences associated with
214
All columns will be filled with their default values\&. (An
216
clause is not permitted in this form\&.)
221
An expression or value to assign to the corresponding column\&.
226
The corresponding column will be filled with its default value\&.
231
A query (\fBSELECT\fR
232
statement) that supplies the rows to be inserted\&. Refer to the
234
statement for a description of the syntax\&.
237
\fIoutput_expression\fR
239
An expression to be computed and returned by the
241
command after each row is inserted or updated\&. The expression can use any column names of the table named by
242
\fItable_name\fR\&. Write
244
to return all columns of the inserted or updated row(s)\&.
249
A name to use for a returned column\&.
251
.SS "ON CONFLICT Clause"
255
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
257
constraint or index specified by
258
\fIconflict_target\fR
259
is violated, the alternative
260
\fIconflict_action\fR
262
ON CONFLICT DO NOTHING
263
simply avoids inserting a row as its alternative action\&.
264
ON CONFLICT DO UPDATE
265
updates the existing row that conflicts with the row proposed for insertion as its alternative action\&.
267
\fIconflict_target\fR
269
\fIunique index inference\fR\&. When performing inference, it consists of one or more
270
\fIindex_column_name\fR
272
\fIindex_expression\fR
273
expressions, and an optional
274
\fIindex_predicate\fR\&. All
276
unique indexes that, without regard to order, contain exactly the
277
\fIconflict_target\fR\-specified columns/expressions are inferred (chosen) as arbiter indexes\&. If an
278
\fIindex_predicate\fR
279
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
280
ON CONFLICT) if such an index satisfying every other criteria is available\&. If an attempt at inference is unsuccessful, an error is raised\&.
282
ON CONFLICT DO UPDATE
287
outcome; provided there is no independent error, one of those two outcomes is guaranteed, even under high concurrency\&. This is also known as
290
\(lqUPDATE or INSERT\(rq\&.
292
\fIconflict_target\fR
294
Specifies which conflicts
296
takes the alternative action on by choosing
297
arbiter indexes\&. Either performs
298
\fIunique index inference\fR, or names a constraint explicitly\&. For
299
ON CONFLICT DO NOTHING, it is optional to specify a
300
\fIconflict_target\fR; when omitted, conflicts with all usable constraints (and unique indexes) are handled\&. For
301
ON CONFLICT DO UPDATE, a
302
\fIconflict_target\fR
307
\fIconflict_action\fR
309
\fIconflict_action\fR
310
specifies an alternative
312
action\&. It can be either
315
clause specifying the exact details of the
317
action to be performed in case of a conflict\&. The
322
ON CONFLICT DO UPDATE
323
have access to the existing row using the table\*(Aqs name (or an alias), and to rows proposed for insertion using the special
327
privilege is required on any column in the target table where corresponding
331
Note that the effects of all per\-row
333
triggers are reflected in
335
values, since those effects may have contributed to the row being excluded from insertion\&.
338
\fIindex_column_name\fR
342
column\&. Used to infer arbiter indexes\&. Follows
347
\fIindex_column_name\fR
351
\fIindex_expression\fR
354
\fIindex_column_name\fR, but used to infer expressions on
356
columns appearing within index definitions (not simple columns)\&. Follows
360
privilege on any column appearing within
361
\fIindex_expression\fR
367
When specified, mandates that corresponding
368
\fIindex_column_name\fR
370
\fIindex_expression\fR
371
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
378
When specified, mandates that corresponding
379
\fIindex_column_name\fR
381
\fIindex_expression\fR
382
use particular operator class in order to be matched during inference\&. Typically this is omitted, as the
384
semantics are often equivalent across a type\*(Aqs operator classes anyway, or because it\*(Aqs sufficient to trust that the defined unique indexes have the pertinent definition of equality\&. Follows
389
\fIindex_predicate\fR
391
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
395
privilege on any column appearing within
396
\fIindex_predicate\fR
400
\fIconstraint_name\fR
402
Explicitly specifies an arbiter
404
by name, rather than inferring a constraint or index\&.
409
An expression that returns a value of type
410
boolean\&. Only rows for which this expression returns
412
will be updated, although all rows will be locked when the
413
ON CONFLICT DO UPDATE
414
action is taken\&. Note that
416
is evaluated last, after a conflict has been identified as a candidate to update\&.
419
Note that exclusion constraints are not supported as arbiters with
420
ON CONFLICT DO UPDATE\&. In all cases, only
422
constraints and unique indexes are supported as arbiters\&.
426
ON CONFLICT DO UPDATE
428
\(lqdeterministic\(rq
429
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\&.
431
Note that it is currently not supported for the
432
ON CONFLICT DO UPDATE
435
applied to a partitioned table to update the partition key of a conflicting row such that it requires the row be moved to a new partition\&.
441
.nr an-no-space-flag 1
449
It is often preferable to use unique index inference rather than naming a constraint directly using
450
ON CONFLICT ON CONSTRAINT
451
\fI constraint_name\fR\&. 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
452
CREATE UNIQUE INDEX \&.\&.\&. CONCURRENTLY
453
before dropping the index being replaced\&.
458
On successful completion, an
460
command returns a command tag of the form
466
INSERT \fIoid\fR \fIcount\fR
474
is the number of rows inserted or updated\&. If
476
is exactly one, and the target table has OIDs, then
480
assigned to the inserted row\&. The single row must have been inserted rather than updated\&. Otherwise
488
clause, the result will be similar to that of a
490
statement containing the columns and values defined in the
492
list, computed over the row(s) inserted or updated by the command\&.
495
If the specified table is a partitioned table, each row is routed to the appropriate partition and inserted into it\&. If the specified table is a partition, an error will occur if one of the input rows violates the partition constraint\&.
498
Insert a single row into table
505
INSERT INTO films VALUES
506
(\*(AqUA502\*(Aq, \*(AqBananas\*(Aq, 105, \*(Aq1971\-07\-13\*(Aq, \*(AqComedy\*(Aq, \*(Aq82 minutes\*(Aq);
514
column is omitted and therefore it will have the default value:
520
INSERT INTO films (code, title, did, date_prod, kind)
521
VALUES (\*(AqT_601\*(Aq, \*(AqYojimbo\*(Aq, 106, \*(Aq1961\-06\-16\*(Aq, \*(AqDrama\*(Aq);
527
This example uses the
529
clause for the date columns rather than specifying a value:
535
INSERT INTO films VALUES
536
(\*(AqUA502\*(Aq, \*(AqBananas\*(Aq, 105, DEFAULT, \*(AqComedy\*(Aq, \*(Aq82 minutes\*(Aq);
537
INSERT INTO films (code, title, did, date_prod, kind)
538
VALUES (\*(AqT_601\*(Aq, \*(AqYojimbo\*(Aq, 106, DEFAULT, \*(AqDrama\*(Aq);
544
To insert a row consisting entirely of default values:
550
INSERT INTO films DEFAULT VALUES;
556
To insert multiple rows using the multirow
564
INSERT INTO films (code, title, did, date_prod, kind) VALUES
565
(\*(AqB6717\*(Aq, \*(AqTampopo\*(Aq, 110, \*(Aq1985\-02\-10\*(Aq, \*(AqComedy\*(Aq),
566
(\*(AqHG120\*(Aq, \*(AqThe Dinner Game\*(Aq, 140, DEFAULT, \*(AqComedy\*(Aq);
572
This example inserts some rows into table
576
with the same column layout as
583
INSERT INTO films SELECT * FROM tmp_films WHERE date_prod < \*(Aq2004\-05\-07\*(Aq;
589
This example inserts into array columns:
595
\-\- Create an empty 3x3 gameboard for noughts\-and\-crosses
596
INSERT INTO tictactoe (game, board[1:3][1:3])
597
VALUES (1, \*(Aq{{" "," "," "},{" "," "," "},{" "," "," "}}\*(Aq);
598
\-\- The subscripts in the above example aren\*(Aqt really needed
599
INSERT INTO tictactoe (game, board)
600
VALUES (2, \*(Aq{{X," "," "},{" ",O," "},{" ",X," "}}\*(Aq);
606
Insert a single row into table
607
distributors, returning the sequence number generated by the
615
INSERT INTO distributors (did, dname) VALUES (DEFAULT, \*(AqXYZ Widgets\*(Aq)
622
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:
629
UPDATE employees SET sales_count = sales_count + 1 WHERE id =
630
(SELECT sales_person FROM accounts WHERE name = \*(AqAcme Corporation\*(Aq)
633
INSERT INTO employees_log SELECT *, current_timestamp FROM upd;
639
Insert or update new distributors as appropriate\&. Assumes a unique index has been defined that constrains values appearing in the
641
column\&. Note that the special
643
table is used to reference values originally proposed for insertion:
649
INSERT INTO distributors (did, dname)
650
VALUES (5, \*(AqGizmo Transglobal\*(Aq), (6, \*(AqAssociated Computing, Inc\*(Aq)
651
ON CONFLICT (did) DO UPDATE SET dname = EXCLUDED\&.dname;
657
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
665
INSERT INTO distributors (did, dname) VALUES (7, \*(AqRedline GmbH\*(Aq)
666
ON CONFLICT (did) DO NOTHING;
672
Insert or update new distributors as appropriate\&. Example assumes a unique index has been defined that constrains values appearing in the
676
clause is used to limit the rows actually updated (any existing row not updated will still be locked, though):
682
\-\- Don\*(Aqt update existing distributors based in a certain ZIP code
683
INSERT INTO distributors AS d (did, dname) VALUES (8, \*(AqAnvil Distribution\*(Aq)
684
ON CONFLICT (did) DO UPDATE
685
SET dname = EXCLUDED\&.dname || \*(Aq (formerly \*(Aq || d\&.dname || \*(Aq)\*(Aq
686
WHERE d\&.zipcode <> \*(Aq21201\*(Aq;
688
\-\- Name a constraint directly in the statement (uses associated
689
\-\- index to arbitrate taking the DO NOTHING action)
690
INSERT INTO distributors (did, dname) VALUES (9, \*(AqAntwerp Design\*(Aq)
691
ON CONFLICT ON CONSTRAINT distributors_pkey DO NOTHING;
697
Insert new distributor if possible; otherwise
698
DO NOTHING\&. Example assumes a unique index has been defined that constrains values appearing in the
700
column on a subset of rows where the
702
Boolean column evaluates to
709
\-\- This statement could infer a partial unique index on "did"
710
\-\- with a predicate of "WHERE is_active", but it could also
711
\-\- just use a regular unique constraint on "did"
712
INSERT INTO distributors (did, dname) VALUES (10, \*(AqConrad International\*(Aq)
713
ON CONFLICT (did) WHERE is_active DO NOTHING;
721
conforms to the SQL standard, except that the
725
extension, as is the ability to use
728
\fBINSERT\fR, and the ability to specify an alternative action with
729
ON CONFLICT\&. Also, the case in which a column name list is omitted, but not all the columns are filled from the
732
\fIquery\fR, is disallowed by the standard\&.
734
The SQL standard specifies that
735
OVERRIDING SYSTEM VALUE
736
can only be specified if an identity column that is generated always exists\&. PostgreSQL allows the clause in any case and ignores it if it is not applicable\&.
738
Possible limitations of the
740
clause are documented under