]> begriffs open source - ai-pg/blob - full-docs/man7/INSERT.7
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / man7 / INSERT.7
1 '\" t
2 .\"     Title: INSERT
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
10 .TH "INSERT" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 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 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 INSERT \- create new rows in a table
32 .SH "SYNOPSIS"
33 .sp
34 .nf
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 [ WITH ( { OLD | NEW } AS \fIoutput_alias\fR [, \&.\&.\&.] ) ]
41                 { * | \fIoutput_expression\fR [ [ AS ] \fIoutput_name\fR ] } [, \&.\&.\&.] ]
42
43 where \fIconflict_target\fR can be one of:
44
45     ( { \fIindex_column_name\fR | ( \fIindex_expression\fR ) } [ COLLATE \fIcollation\fR ] [ \fIopclass\fR ] [, \&.\&.\&.] ) [ WHERE \fIindex_predicate\fR ]
46     ON CONSTRAINT \fIconstraint_name\fR
47
48 and \fIconflict_action\fR is one of:
49
50     DO NOTHING
51     DO UPDATE SET { \fIcolumn_name\fR = { \fIexpression\fR | DEFAULT } |
52                     ( \fIcolumn_name\fR [, \&.\&.\&.] ) = [ ROW ] ( { \fIexpression\fR | DEFAULT } [, \&.\&.\&.] ) |
53                     ( \fIcolumn_name\fR [, \&.\&.\&.] ) = ( \fIsub\-SELECT\fR )
54                   } [, \&.\&.\&.]
55               [ WHERE \fIcondition\fR ]
56 .fi
57 .SH "DESCRIPTION"
58 .PP
59 \fBINSERT\fR
60 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 .PP
62 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 \fIN\fR
64 column names, if there are only
65 \fIN\fR
66 columns supplied by the
67 VALUES
68 clause or
69 \fIquery\fR\&. The values supplied by the
70 VALUES
71 clause or
72 \fIquery\fR
73 are associated with the explicit or implicit column list left\-to\-right\&.
74 .PP
75 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 .PP
77 If the expression for any column is not of the correct data type, automatic type conversion will be attempted\&.
78 .PP
79 \fBINSERT\fR
80 into tables that lack unique indexes will not be blocked by concurrent activity\&. Tables with unique indexes might block if concurrent sessions perform actions that lock or modify rows matching the unique index values being inserted; the details are covered in
81 Section\ \&63.5\&.
82 ON CONFLICT
83 can be used to specify an alternative action to raising a unique constraint or exclusion constraint violation error\&. (See
84 ON CONFLICT Clause
85 below\&.)
86 .PP
87 The optional
88 RETURNING
89 clause causes
90 \fBINSERT\fR
91 to compute and return value(s) based on each row actually inserted (or updated, if an
92 ON CONFLICT DO UPDATE
93 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
94 RETURNING
95 list is identical to that of the output list of
96 \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
97 ON CONFLICT DO UPDATE \&.\&.\&. WHERE
98 clause
99 \fIcondition\fR
100 was not satisfied, the row will not be returned\&.
101 .PP
102 You must have
103 INSERT
104 privilege on a table in order to insert into it\&. If
105 ON CONFLICT DO UPDATE
106 is present,
107 UPDATE
108 privilege on the table is also required\&.
109 .PP
110 If a column list is specified, you only need
111 INSERT
112 privilege on the listed columns\&. Similarly, when
113 ON CONFLICT DO UPDATE
114 is specified, you only need
115 UPDATE
116 privilege on the column(s) that are listed to be updated\&. However,
117 ON CONFLICT DO UPDATE
118 also requires
119 SELECT
120 privilege on any column whose values are read in the
121 ON CONFLICT DO UPDATE
122 expressions or
123 \fIcondition\fR\&.
124 .PP
125 Use of the
126 RETURNING
127 clause requires
128 SELECT
129 privilege on all columns mentioned in
130 RETURNING\&. If you use the
131 \fIquery\fR
132 clause to insert rows from a query, you of course need to have
133 SELECT
134 privilege on any table or column used in the query\&.
135 .SH "PARAMETERS"
136 .SS "Inserting"
137 .PP
138 This section covers parameters that may be used when only inserting new rows\&. Parameters
139 \fIexclusively\fR
140 used with the
141 ON CONFLICT
142 clause are described separately\&.
143 .PP
144 \fIwith_query\fR
145 .RS 4
146 The
147 WITH
148 clause allows you to specify one or more subqueries that can be referenced by name in the
149 \fBINSERT\fR
150 query\&. See
151 Section\ \&7.8
152 and
153 \fBSELECT\fR(7)
154 for details\&.
155 .sp
156 It is possible for the
157 \fIquery\fR
158 (\fBSELECT\fR
159 statement) to also contain a
160 WITH
161 clause\&. In such a case both sets of
162 \fIwith_query\fR
163 can be referenced within the
164 \fIquery\fR, but the second one takes precedence since it is more closely nested\&.
165 .RE
166 .PP
167 \fItable_name\fR
168 .RS 4
169 The name (optionally schema\-qualified) of an existing table\&.
170 .RE
171 .PP
172 \fIalias\fR
173 .RS 4
174 A substitute name for
175 \fItable_name\fR\&. When an alias is provided, it completely hides the actual name of the table\&. This is particularly useful when
176 ON CONFLICT DO UPDATE
177 targets a table named
178 \fIexcluded\fR, since that will otherwise be taken as the name of the special table representing the row proposed for insertion\&.
179 .RE
180 .PP
181 \fIcolumn_name\fR
182 .RS 4
183 The name of a column in the table named by
184 \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
185 ON CONFLICT DO UPDATE, do not include the table\*(Aqs name in the specification of a target column\&. For example,
186 INSERT INTO table_name \&.\&.\&. ON CONFLICT DO UPDATE SET table_name\&.col = 1
187 is invalid (this follows the general behavior for
188 \fBUPDATE\fR)\&.
189 .RE
190 .PP
191 OVERRIDING SYSTEM VALUE
192 .RS 4
193 If this clause is specified, then any values supplied for identity columns will override the default sequence\-generated values\&.
194 .sp
195 For an identity column defined as
196 GENERATED ALWAYS, it is an error to insert an explicit value (other than
197 DEFAULT) without specifying either
198 OVERRIDING SYSTEM VALUE
199 or
200 OVERRIDING USER VALUE\&. (For an identity column defined as
201 GENERATED BY DEFAULT,
202 OVERRIDING SYSTEM VALUE
203 is the normal behavior and specifying it does nothing, but
204 PostgreSQL
205 allows it as an extension\&.)
206 .RE
207 .PP
208 OVERRIDING USER VALUE
209 .RS 4
210 If this clause is specified, then any values supplied for identity columns are ignored and the default sequence\-generated values are applied\&.
211 .sp
212 This clause is useful for example when copying values between tables\&. Writing
213 INSERT INTO tbl2 OVERRIDING USER VALUE SELECT * FROM tbl1
214 will copy from
215 tbl1
216 all columns that are not identity columns in
217 tbl2
218 while values for the identity columns in
219 tbl2
220 will be generated by the sequences associated with
221 tbl2\&.
222 .RE
223 .PP
224 DEFAULT VALUES
225 .RS 4
226 All columns will be filled with their default values, as if
227 DEFAULT
228 were explicitly specified for each column\&. (An
229 OVERRIDING
230 clause is not permitted in this form\&.)
231 .RE
232 .PP
233 \fIexpression\fR
234 .RS 4
235 An expression or value to assign to the corresponding column\&.
236 .RE
237 .PP
238 DEFAULT
239 .RS 4
240 The corresponding column will be filled with its default value\&. An identity column will be filled with a new value generated by the associated sequence\&. For a generated column, specifying this is permitted but merely specifies the normal behavior of computing the column from its generation expression\&.
241 .RE
242 .PP
243 \fIquery\fR
244 .RS 4
245 A query (\fBSELECT\fR
246 statement) that supplies the rows to be inserted\&. Refer to the
247 \fBSELECT\fR(7)
248 statement for a description of the syntax\&.
249 .RE
250 .PP
251 \fIoutput_alias\fR
252 .RS 4
253 An optional substitute name for
254 OLD
255 or
256 NEW
257 rows in the
258 RETURNING
259 list\&.
260 .sp
261 By default, old values from the target table can be returned by writing
262 OLD\&.\fIcolumn_name\fR
263 or
264 OLD\&.*, and new values can be returned by writing
265 NEW\&.\fIcolumn_name\fR
266 or
267 NEW\&.*\&. When an alias is provided, these names are hidden and the old or new rows must be referred to using the alias\&. For example
268 RETURNING WITH (OLD AS o, NEW AS n) o\&.*, n\&.*\&.
269 .RE
270 .PP
271 \fIoutput_expression\fR
272 .RS 4
273 An expression to be computed and returned by the
274 \fBINSERT\fR
275 command after each row is inserted or updated\&. The expression can use any column names of the table named by
276 \fItable_name\fR\&. Write
277 *
278 to return all columns of the inserted or updated row(s)\&.
279 .sp
280 A column name or
281 *
282 may be qualified using
283 OLD
284 or
285 NEW, or the corresponding
286 \fIoutput_alias\fR
287 for
288 OLD
289 or
290 NEW, to cause old or new values to be returned\&. An unqualified column name, or
291 *, or a column name or
292 *
293 qualified using the target table name or alias will return new values\&.
294 .sp
295 For a simple
296 \fBINSERT\fR, all old values will be
297 NULL\&. However, for an
298 \fBINSERT\fR
299 with an
300 ON CONFLICT DO UPDATE
301 clause, the old values may be non\-NULL\&.
302 .RE
303 .PP
304 \fIoutput_name\fR
305 .RS 4
306 A name to use for a returned column\&.
307 .RE
308 .SS "ON CONFLICT Clause"
309 .PP
310 The optional
311 ON CONFLICT
312 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
313 \fIarbiter\fR
314 constraint or index specified by
315 \fIconflict_target\fR
316 is violated, the alternative
317 \fIconflict_action\fR
318 is taken\&.
319 ON CONFLICT DO NOTHING
320 simply avoids inserting a row as its alternative action\&.
321 ON CONFLICT DO UPDATE
322 updates the existing row that conflicts with the row proposed for insertion as its alternative action\&.
323 .PP
324 \fIconflict_target\fR
325 can perform
326 \fIunique index inference\fR\&. When performing inference, it consists of one or more
327 \fIindex_column_name\fR
328 columns and/or
329 \fIindex_expression\fR
330 expressions, and an optional
331 \fIindex_predicate\fR\&. All
332 \fItable_name\fR
333 unique indexes that, without regard to order, contain exactly the
334 \fIconflict_target\fR\-specified columns/expressions are inferred (chosen) as arbiter indexes\&. If an
335 \fIindex_predicate\fR
336 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
337 ON CONFLICT) if such an index satisfying every other criteria is available\&. If an attempt at inference is unsuccessful, an error is raised\&.
338 .PP
339 ON CONFLICT DO UPDATE
340 guarantees an atomic
341 \fBINSERT\fR
342 or
343 \fBUPDATE\fR
344 outcome; provided there is no independent error, one of those two outcomes is guaranteed, even under high concurrency\&. This is also known as
345 UPSERT
346 \(em
347 \(lqUPDATE or INSERT\(rq\&.
348 .PP
349 \fIconflict_target\fR
350 .RS 4
351 Specifies which conflicts
352 ON CONFLICT
353 takes the alternative action on by choosing
354 arbiter indexes\&. Either performs
355 \fIunique index inference\fR, or names a constraint explicitly\&. For
356 ON CONFLICT DO NOTHING, it is optional to specify a
357 \fIconflict_target\fR; when omitted, conflicts with all usable constraints (and unique indexes) are handled\&. For
358 ON CONFLICT DO UPDATE, a
359 \fIconflict_target\fR
360 \fImust\fR
361 be provided\&.
362 .RE
363 .PP
364 \fIconflict_action\fR
365 .RS 4
366 \fIconflict_action\fR
367 specifies an alternative
368 ON CONFLICT
369 action\&. It can be either
370 DO NOTHING, or a
371 DO UPDATE
372 clause specifying the exact details of the
373 UPDATE
374 action to be performed in case of a conflict\&. The
375 SET
376 and
377 WHERE
378 clauses in
379 ON CONFLICT DO UPDATE
380 have access to the existing row using the table\*(Aqs name (or an alias), and to the row proposed for insertion using the special
381 \fIexcluded\fR
382 table\&.
383 SELECT
384 privilege is required on any column in the target table where corresponding
385 \fIexcluded\fR
386 columns are read\&.
387 .sp
388 Note that the effects of all per\-row
389 BEFORE INSERT
390 triggers are reflected in
391 \fIexcluded\fR
392 values, since those effects may have contributed to the row being excluded from insertion\&.
393 .RE
394 .PP
395 \fIindex_column_name\fR
396 .RS 4
397 The name of a
398 \fItable_name\fR
399 column\&. Used to infer arbiter indexes\&. Follows
400 \fBCREATE INDEX\fR
401 format\&.
402 SELECT
403 privilege on
404 \fIindex_column_name\fR
405 is required\&.
406 .RE
407 .PP
408 \fIindex_expression\fR
409 .RS 4
410 Similar to
411 \fIindex_column_name\fR, but used to infer expressions on
412 \fItable_name\fR
413 columns appearing within index definitions (not simple columns)\&. Follows
414 \fBCREATE INDEX\fR
415 format\&.
416 SELECT
417 privilege on any column appearing within
418 \fIindex_expression\fR
419 is required\&.
420 .RE
421 .PP
422 \fIcollation\fR
423 .RS 4
424 When specified, mandates that corresponding
425 \fIindex_column_name\fR
426 or
427 \fIindex_expression\fR
428 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
429 \fBCREATE INDEX\fR
430 format\&.
431 .RE
432 .PP
433 \fIopclass\fR
434 .RS 4
435 When specified, mandates that corresponding
436 \fIindex_column_name\fR
437 or
438 \fIindex_expression\fR
439 use particular operator class in order to be matched during inference\&. Typically this is omitted, as the
440 \fIequality\fR
441 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
442 \fBCREATE INDEX\fR
443 format\&.
444 .RE
445 .PP
446 \fIindex_predicate\fR
447 .RS 4
448 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
449 \fBCREATE INDEX\fR
450 format\&.
451 SELECT
452 privilege on any column appearing within
453 \fIindex_predicate\fR
454 is required\&.
455 .RE
456 .PP
457 \fIconstraint_name\fR
458 .RS 4
459 Explicitly specifies an arbiter
460 \fIconstraint\fR
461 by name, rather than inferring a constraint or index\&.
462 .RE
463 .PP
464 \fIcondition\fR
465 .RS 4
466 An expression that returns a value of type
467 boolean\&. Only rows for which this expression returns
468 true
469 will be updated, although all rows will be locked when the
470 ON CONFLICT DO UPDATE
471 action is taken\&. Note that
472 \fIcondition\fR
473 is evaluated last, after a conflict has been identified as a candidate to update\&.
474 .RE
475 .PP
476 Note that exclusion constraints are not supported as arbiters with
477 ON CONFLICT DO UPDATE\&. In all cases, only
478 NOT DEFERRABLE
479 constraints and unique indexes are supported as arbiters\&.
480 .PP
481 \fBINSERT\fR
482 with an
483 ON CONFLICT DO UPDATE
484 clause is a
485 \(lqdeterministic\(rq
486 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\&.
487 .PP
488 Note that it is currently not supported for the
489 ON CONFLICT DO UPDATE
490 clause of an
491 \fBINSERT\fR
492 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\&.
493 .if n \{\
494 .sp
495 .\}
496 .RS 4
497 .it 1 an-trap
498 .nr an-no-space-flag 1
499 .nr an-break-flag 1
500 .br
501 .ps +1
502 \fBTip\fR
503 .ps -1
504 .br
505 .PP
506 It is often preferable to use unique index inference rather than naming a constraint directly using
507 ON CONFLICT ON CONSTRAINT
508 \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
509 CREATE UNIQUE INDEX \&.\&.\&. CONCURRENTLY
510 before dropping the index being replaced\&.
511 .sp .5v
512 .RE
513 .SH "OUTPUTS"
514 .PP
515 On successful completion, an
516 \fBINSERT\fR
517 command returns a command tag of the form
518 .sp
519 .if n \{\
520 .RS 4
521 .\}
522 .nf
523 INSERT \fIoid\fR \fIcount\fR
524 .fi
525 .if n \{\
526 .RE
527 .\}
528 .sp
529 The
530 \fIcount\fR
531 is the number of rows inserted or updated\&.
532 \fIoid\fR
533 is always 0 (it used to be the
534 OID
535 assigned to the inserted row if
536 \fIcount\fR
537 was exactly one and the target table was declared
538 WITH OIDS
539 and 0 otherwise, but creating a table
540 WITH OIDS
541 is not supported anymore)\&.
542 .PP
543 If the
544 \fBINSERT\fR
545 command contains a
546 RETURNING
547 clause, the result will be similar to that of a
548 \fBSELECT\fR
549 statement containing the columns and values defined in the
550 RETURNING
551 list, computed over the row(s) inserted or updated by the command\&.
552 .SH "NOTES"
553 .PP
554 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\&.
555 .PP
556 You may also wish to consider using
557 \fBMERGE\fR, since that allows mixing
558 \fBINSERT\fR,
559 \fBUPDATE\fR, and
560 \fBDELETE\fR
561 within a single statement\&. See
562 \fBMERGE\fR(7)\&.
563 .SH "EXAMPLES"
564 .PP
565 Insert a single row into table
566 films:
567 .sp
568 .if n \{\
569 .RS 4
570 .\}
571 .nf
572 INSERT INTO films VALUES
573     (\*(AqUA502\*(Aq, \*(AqBananas\*(Aq, 105, \*(Aq1971\-07\-13\*(Aq, \*(AqComedy\*(Aq, \*(Aq82 minutes\*(Aq);
574 .fi
575 .if n \{\
576 .RE
577 .\}
578 .PP
579 In this example, the
580 len
581 column is omitted and therefore it will have the default value:
582 .sp
583 .if n \{\
584 .RS 4
585 .\}
586 .nf
587 INSERT INTO films (code, title, did, date_prod, kind)
588     VALUES (\*(AqT_601\*(Aq, \*(AqYojimbo\*(Aq, 106, \*(Aq1961\-06\-16\*(Aq, \*(AqDrama\*(Aq);
589 .fi
590 .if n \{\
591 .RE
592 .\}
593 .PP
594 This example uses the
595 DEFAULT
596 clause for the date columns rather than specifying a value:
597 .sp
598 .if n \{\
599 .RS 4
600 .\}
601 .nf
602 INSERT INTO films VALUES
603     (\*(AqUA502\*(Aq, \*(AqBananas\*(Aq, 105, DEFAULT, \*(AqComedy\*(Aq, \*(Aq82 minutes\*(Aq);
604 INSERT INTO films (code, title, did, date_prod, kind)
605     VALUES (\*(AqT_601\*(Aq, \*(AqYojimbo\*(Aq, 106, DEFAULT, \*(AqDrama\*(Aq);
606 .fi
607 .if n \{\
608 .RE
609 .\}
610 .PP
611 To insert a row consisting entirely of default values:
612 .sp
613 .if n \{\
614 .RS 4
615 .\}
616 .nf
617 INSERT INTO films DEFAULT VALUES;
618 .fi
619 .if n \{\
620 .RE
621 .\}
622 .PP
623 To insert multiple rows using the multirow
624 \fBVALUES\fR
625 syntax:
626 .sp
627 .if n \{\
628 .RS 4
629 .\}
630 .nf
631 INSERT INTO films (code, title, did, date_prod, kind) VALUES
632     (\*(AqB6717\*(Aq, \*(AqTampopo\*(Aq, 110, \*(Aq1985\-02\-10\*(Aq, \*(AqComedy\*(Aq),
633     (\*(AqHG120\*(Aq, \*(AqThe Dinner Game\*(Aq, 140, DEFAULT, \*(AqComedy\*(Aq);
634 .fi
635 .if n \{\
636 .RE
637 .\}
638 .PP
639 This example inserts some rows into table
640 films
641 from a table
642 tmp_films
643 with the same column layout as
644 films:
645 .sp
646 .if n \{\
647 .RS 4
648 .\}
649 .nf
650 INSERT INTO films SELECT * FROM tmp_films WHERE date_prod < \*(Aq2004\-05\-07\*(Aq;
651 .fi
652 .if n \{\
653 .RE
654 .\}
655 .PP
656 This example inserts into array columns:
657 .sp
658 .if n \{\
659 .RS 4
660 .\}
661 .nf
662 \-\- Create an empty 3x3 gameboard for noughts\-and\-crosses
663 INSERT INTO tictactoe (game, board[1:3][1:3])
664     VALUES (1, \*(Aq{{" "," "," "},{" "," "," "},{" "," "," "}}\*(Aq);
665 \-\- The subscripts in the above example aren\*(Aqt really needed
666 INSERT INTO tictactoe (game, board)
667     VALUES (2, \*(Aq{{X," "," "},{" ",O," "},{" ",X," "}}\*(Aq);
668 .fi
669 .if n \{\
670 .RE
671 .\}
672 .PP
673 Insert a single row into table
674 distributors, returning the sequence number generated by the
675 DEFAULT
676 clause:
677 .sp
678 .if n \{\
679 .RS 4
680 .\}
681 .nf
682 INSERT INTO distributors (did, dname) VALUES (DEFAULT, \*(AqXYZ Widgets\*(Aq)
683    RETURNING did;
684 .fi
685 .if n \{\
686 .RE
687 .\}
688 .PP
689 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:
690 .sp
691 .if n \{\
692 .RS 4
693 .\}
694 .nf
695 WITH upd AS (
696   UPDATE employees SET sales_count = sales_count + 1 WHERE id =
697     (SELECT sales_person FROM accounts WHERE name = \*(AqAcme Corporation\*(Aq)
698     RETURNING *
699 )
700 INSERT INTO employees_log SELECT *, current_timestamp FROM upd;
701 .fi
702 .if n \{\
703 .RE
704 .\}
705 .PP
706 Insert or update new distributors as appropriate\&. Assumes a unique index has been defined that constrains values appearing in the
707 did
708 column\&. Note that the special
709 \fIexcluded\fR
710 table is used to reference values originally proposed for insertion:
711 .sp
712 .if n \{\
713 .RS 4
714 .\}
715 .nf
716 INSERT INTO distributors (did, dname)
717     VALUES (5, \*(AqGizmo Transglobal\*(Aq), (6, \*(AqAssociated Computing, Inc\*(Aq)
718     ON CONFLICT (did) DO UPDATE SET dname = EXCLUDED\&.dname;
719 .fi
720 .if n \{\
721 .RE
722 .\}
723 .PP
724 Insert or update new distributors as above, returning information about any existing values that were updated, together with the new data inserted\&. Note that the returned values for
725 old_did
726 and
727 old_dname
728 will be
729 NULL
730 for non\-conflicting rows:
731 .sp
732 .if n \{\
733 .RS 4
734 .\}
735 .nf
736 INSERT INTO distributors (did, dname)
737     VALUES (5, \*(AqGizmo Transglobal\*(Aq), (6, \*(AqAssociated Computing, Inc\*(Aq)
738     ON CONFLICT (did) DO UPDATE SET dname = EXCLUDED\&.dname
739     RETURNING old\&.did AS old_did, old\&.dname AS old_dname,
740               new\&.did AS new_did, new\&.dname AS new_dname;
741 .fi
742 .if n \{\
743 .RE
744 .\}
745 .PP
746 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
747 did
748 column:
749 .sp
750 .if n \{\
751 .RS 4
752 .\}
753 .nf
754 INSERT INTO distributors (did, dname) VALUES (7, \*(AqRedline GmbH\*(Aq)
755     ON CONFLICT (did) DO NOTHING;
756 .fi
757 .if n \{\
758 .RE
759 .\}
760 .PP
761 Insert or update new distributors as appropriate\&. Example assumes a unique index has been defined that constrains values appearing in the
762 did
763 column\&.
764 WHERE
765 clause is used to limit the rows actually updated (any existing row not updated will still be locked, though):
766 .sp
767 .if n \{\
768 .RS 4
769 .\}
770 .nf
771 \-\- Don\*(Aqt update existing distributors based in a certain ZIP code
772 INSERT INTO distributors AS d (did, dname) VALUES (8, \*(AqAnvil Distribution\*(Aq)
773     ON CONFLICT (did) DO UPDATE
774     SET dname = EXCLUDED\&.dname || \*(Aq (formerly \*(Aq || d\&.dname || \*(Aq)\*(Aq
775     WHERE d\&.zipcode <> \*(Aq21201\*(Aq;
776
777 \-\- Name a constraint directly in the statement (uses associated
778 \-\- index to arbitrate taking the DO NOTHING action)
779 INSERT INTO distributors (did, dname) VALUES (9, \*(AqAntwerp Design\*(Aq)
780     ON CONFLICT ON CONSTRAINT distributors_pkey DO NOTHING;
781 .fi
782 .if n \{\
783 .RE
784 .\}
785 .PP
786 Insert new distributor if possible; otherwise
787 DO NOTHING\&. Example assumes a unique index has been defined that constrains values appearing in the
788 did
789 column on a subset of rows where the
790 is_active
791 Boolean column evaluates to
792 true:
793 .sp
794 .if n \{\
795 .RS 4
796 .\}
797 .nf
798 \-\- This statement could infer a partial unique index on "did"
799 \-\- with a predicate of "WHERE is_active", but it could also
800 \-\- just use a regular unique constraint on "did"
801 INSERT INTO distributors (did, dname) VALUES (10, \*(AqConrad International\*(Aq)
802     ON CONFLICT (did) WHERE is_active DO NOTHING;
803 .fi
804 .if n \{\
805 .RE
806 .\}
807 .SH "COMPATIBILITY"
808 .PP
809 \fBINSERT\fR
810 conforms to the SQL standard, except that the
811 RETURNING
812 clause is a
813 PostgreSQL
814 extension, as is the ability to use
815 WITH
816 with
817 \fBINSERT\fR, and the ability to specify an alternative action with
818 ON CONFLICT\&. Also, the case in which a column name list is omitted, but not all the columns are filled from the
819 VALUES
820 clause or
821 \fIquery\fR, is disallowed by the standard\&. If you prefer a more SQL standard conforming statement than
822 ON CONFLICT, see
823 \fBMERGE\fR(7)\&.
824 .PP
825 The SQL standard specifies that
826 OVERRIDING SYSTEM VALUE
827 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\&.
828 .PP
829 Possible limitations of the
830 \fIquery\fR
831 clause are documented under
832 \fBSELECT\fR(7)\&.