]> begriffs open source - ai-pg/blob - full-docs/man7/MERGE.7
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / man7 / MERGE.7
1 '\" t
2 .\"     Title: MERGE
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 "MERGE" "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 MERGE \- conditionally insert, update, or delete rows of a table
32 .SH "SYNOPSIS"
33 .sp
34 .nf
35 [ WITH \fIwith_query\fR [, \&.\&.\&.] ]
36 MERGE INTO [ ONLY ] \fItarget_table_name\fR [ * ] [ [ AS ] \fItarget_alias\fR ]
37     USING \fIdata_source\fR ON \fIjoin_condition\fR
38     \fIwhen_clause\fR [\&.\&.\&.]
39     [ RETURNING [ WITH ( { OLD | NEW } AS \fIoutput_alias\fR [, \&.\&.\&.] ) ]
40                 { * | \fIoutput_expression\fR [ [ AS ] \fIoutput_name\fR ] } [, \&.\&.\&.] ]
41
42 where \fIdata_source\fR is:
43
44     { [ ONLY ] \fIsource_table_name\fR [ * ] | ( \fIsource_query\fR ) } [ [ AS ] \fIsource_alias\fR ]
45
46 and \fIwhen_clause\fR is:
47
48     { WHEN MATCHED [ AND \fIcondition\fR ] THEN { \fImerge_update\fR | \fImerge_delete\fR | DO NOTHING } |
49       WHEN NOT MATCHED BY SOURCE [ AND \fIcondition\fR ] THEN { \fImerge_update\fR | \fImerge_delete\fR | DO NOTHING } |
50       WHEN NOT MATCHED [ BY TARGET ] [ AND \fIcondition\fR ] THEN { \fImerge_insert\fR | DO NOTHING } }
51
52 and \fImerge_insert\fR is:
53
54     INSERT [( \fIcolumn_name\fR [, \&.\&.\&.] )]
55         [ OVERRIDING { SYSTEM | USER } VALUE ]
56         { VALUES ( { \fIexpression\fR | DEFAULT } [, \&.\&.\&.] ) | DEFAULT VALUES }
57
58 and \fImerge_update\fR is:
59
60     UPDATE SET { \fIcolumn_name\fR = { \fIexpression\fR | DEFAULT } |
61                  ( \fIcolumn_name\fR [, \&.\&.\&.] ) = [ ROW ] ( { \fIexpression\fR | DEFAULT } [, \&.\&.\&.] ) |
62                  ( \fIcolumn_name\fR [, \&.\&.\&.] ) = ( \fIsub\-SELECT\fR )
63                } [, \&.\&.\&.]
64
65 and \fImerge_delete\fR is:
66
67     DELETE
68 .fi
69 .SH "DESCRIPTION"
70 .PP
71 \fBMERGE\fR
72 performs actions that modify rows in the target table identified as
73 \fItarget_table_name\fR, using the
74 \fIdata_source\fR\&.
75 \fBMERGE\fR
76 provides a single
77 SQL
78 statement that can conditionally
79 \fBINSERT\fR,
80 \fBUPDATE\fR
81 or
82 \fBDELETE\fR
83 rows, a task that would otherwise require multiple procedural language statements\&.
84 .PP
85 First, the
86 \fBMERGE\fR
87 command performs a join from
88 \fIdata_source\fR
89 to the target table producing zero or more candidate change rows\&. For each candidate change row, the status of
90 MATCHED,
91 NOT MATCHED BY SOURCE, or
92 NOT MATCHED [BY TARGET]
93 is set just once, after which
94 WHEN
95 clauses are evaluated in the order specified\&. For each candidate change row, the first clause to evaluate as true is executed\&. No more than one
96 WHEN
97 clause is executed for any candidate change row\&.
98 .PP
99 \fBMERGE\fR
100 actions have the same effect as regular
101 \fBUPDATE\fR,
102 \fBINSERT\fR, or
103 \fBDELETE\fR
104 commands of the same names\&. The syntax of those commands is different, notably that there is no
105 WHERE
106 clause and no table name is specified\&. All actions refer to the target table, though modifications to other tables may be made using triggers\&.
107 .PP
108 When
109 DO NOTHING
110 is specified, the source row is skipped\&. Since actions are evaluated in their specified order,
111 DO NOTHING
112 can be handy to skip non\-interesting source rows before more fine\-grained handling\&.
113 .PP
114 The optional
115 RETURNING
116 clause causes
117 \fBMERGE\fR
118 to compute and return value(s) based on each row inserted, updated, or deleted\&. Any expression using the source or target table\*(Aqs columns, or the
119 \fBmerge_action()\fR
120 function can be computed\&. By default, when an
121 \fBINSERT\fR
122 or
123 \fBUPDATE\fR
124 action is performed, the new values of the target table\*(Aqs columns are used, and when a
125 \fBDELETE\fR
126 is performed, the old values of the target table\*(Aqs columns are used, but it is also possible to explicitly request old and new values\&. The syntax of the
127 RETURNING
128 list is identical to that of the output list of
129 \fBSELECT\fR\&.
130 .PP
131 There is no separate
132 MERGE
133 privilege\&. If you specify an update action, you must have the
134 UPDATE
135 privilege on the column(s) of the target table that are referred to in the
136 SET
137 clause\&. If you specify an insert action, you must have the
138 INSERT
139 privilege on the target table\&. If you specify a delete action, you must have the
140 DELETE
141 privilege on the target table\&. If you specify a
142 DO NOTHING
143 action, you must have the
144 SELECT
145 privilege on at least one column of the target table\&. You will also need
146 SELECT
147 privilege on any column(s) of the
148 \fIdata_source\fR
149 and of the target table referred to in any
150 condition
151 (including
152 join_condition) or
153 expression\&. Privileges are tested once at statement start and are checked whether or not particular
154 WHEN
155 clauses are executed\&.
156 .PP
157 \fBMERGE\fR
158 is not supported if the target table is a materialized view, foreign table, or if it has any rules defined on it\&.
159 .SH "PARAMETERS"
160 .PP
161 \fIwith_query\fR
162 .RS 4
163 The
164 WITH
165 clause allows you to specify one or more subqueries that can be referenced by name in the
166 \fBMERGE\fR
167 query\&. See
168 Section\ \&7.8
169 and
170 \fBSELECT\fR(7)
171 for details\&. Note that
172 WITH RECURSIVE
173 is not supported by
174 \fBMERGE\fR\&.
175 .RE
176 .PP
177 \fItarget_table_name\fR
178 .RS 4
179 The name (optionally schema\-qualified) of the target table or view to merge into\&. If
180 ONLY
181 is specified before a table name, matching rows are updated or deleted in the named table only\&. If
182 ONLY
183 is not specified, matching rows are also updated or deleted in any tables inheriting from the named table\&. Optionally,
184 *
185 can be specified after the table name to explicitly indicate that descendant tables are included\&. The
186 ONLY
187 keyword and
188 *
189 option do not affect insert actions, which always insert into the named table only\&.
190 .sp
191 If
192 \fItarget_table_name\fR
193 is a view, it must either be automatically updatable with no
194 INSTEAD OF
195 triggers, or it must have
196 INSTEAD OF
197 triggers for every type of action (INSERT,
198 UPDATE, and
199 DELETE) specified in the
200 WHEN
201 clauses\&. Views with rules are not supported\&.
202 .RE
203 .PP
204 \fItarget_alias\fR
205 .RS 4
206 A substitute name for the target table\&. When an alias is provided, it completely hides the actual name of the table\&. For example, given
207 MERGE INTO foo AS f, the remainder of the
208 \fBMERGE\fR
209 statement must refer to this table as
210 f
211 not
212 foo\&.
213 .RE
214 .PP
215 \fIsource_table_name\fR
216 .RS 4
217 The name (optionally schema\-qualified) of the source table, view, or transition table\&. If
218 ONLY
219 is specified before the table name, matching rows are included from the named table only\&. If
220 ONLY
221 is not specified, matching rows are also included from any tables inheriting from the named table\&. Optionally,
222 *
223 can be specified after the table name to explicitly indicate that descendant tables are included\&.
224 .RE
225 .PP
226 \fIsource_query\fR
227 .RS 4
228 A query (\fBSELECT\fR
229 statement or
230 \fBVALUES\fR
231 statement) that supplies the rows to be merged into the target table\&. Refer to the
232 \fBSELECT\fR(7)
233 statement or
234 \fBVALUES\fR(7)
235 statement for a description of the syntax\&.
236 .RE
237 .PP
238 \fIsource_alias\fR
239 .RS 4
240 A substitute name for the data source\&. When an alias is provided, it completely hides the actual name of the table or the fact that a query was issued\&.
241 .RE
242 .PP
243 \fIjoin_condition\fR
244 .RS 4
245 \fIjoin_condition\fR
246 is an expression resulting in a value of type
247 boolean
248 (similar to a
249 WHERE
250 clause) that specifies which rows in the
251 \fIdata_source\fR
252 match rows in the target table\&.
253 .if n \{\
254 .sp
255 .\}
256 .RS 4
257 .it 1 an-trap
258 .nr an-no-space-flag 1
259 .nr an-break-flag 1
260 .br
261 .ps +1
262 \fBWarning\fR
263 .ps -1
264 .br
265 Only columns from the target table that attempt to match
266 \fIdata_source\fR
267 rows should appear in
268 \fIjoin_condition\fR\&.
269 \fIjoin_condition\fR
270 subexpressions that only reference the target table\*(Aqs columns can affect which action is taken, often in surprising ways\&.
271 .sp
272 If both
273 WHEN NOT MATCHED BY SOURCE
274 and
275 WHEN NOT MATCHED [BY TARGET]
276 clauses are specified, the
277 \fBMERGE\fR
278 command will perform a
279 FULL
280 join between
281 \fIdata_source\fR
282 and the target table\&. For this to work, at least one
283 \fIjoin_condition\fR
284 subexpression must use an operator that can support a hash join, or all of the subexpressions must use operators that can support a merge join\&.
285 .sp .5v
286 .RE
287 .RE
288 .PP
289 \fIwhen_clause\fR
290 .RS 4
291 At least one
292 WHEN
293 clause is required\&.
294 .sp
295 The
296 WHEN
297 clause may specify
298 WHEN MATCHED,
299 WHEN NOT MATCHED BY SOURCE, or
300 WHEN NOT MATCHED [BY TARGET]\&. Note that the
301 SQL
302 standard only defines
303 WHEN MATCHED
304 and
305 WHEN NOT MATCHED
306 (which is defined to mean no matching target row)\&.
307 WHEN NOT MATCHED BY SOURCE
308 is an extension to the
309 SQL
310 standard, as is the option to append
311 BY TARGET
312 to
313 WHEN NOT MATCHED, to make its meaning more explicit\&.
314 .sp
315 If the
316 WHEN
317 clause specifies
318 WHEN MATCHED
319 and the candidate change row matches a row in the
320 \fIdata_source\fR
321 to a row in the target table, the
322 WHEN
323 clause is executed if the
324 \fIcondition\fR
325 is absent or it evaluates to
326 true\&.
327 .sp
328 If the
329 WHEN
330 clause specifies
331 WHEN NOT MATCHED BY SOURCE
332 and the candidate change row represents a row in the target table that does not match a row in the
333 \fIdata_source\fR, the
334 WHEN
335 clause is executed if the
336 \fIcondition\fR
337 is absent or it evaluates to
338 true\&.
339 .sp
340 If the
341 WHEN
342 clause specifies
343 WHEN NOT MATCHED [BY TARGET]
344 and the candidate change row represents a row in the
345 \fIdata_source\fR
346 that does not match a row in the target table, the
347 WHEN
348 clause is executed if the
349 \fIcondition\fR
350 is absent or it evaluates to
351 true\&.
352 .RE
353 .PP
354 \fIcondition\fR
355 .RS 4
356 An expression that returns a value of type
357 boolean\&. If this expression for a
358 WHEN
359 clause returns
360 true, then the action for that clause is executed for that row\&.
361 .sp
362 A condition on a
363 WHEN MATCHED
364 clause can refer to columns in both the source and the target relations\&. A condition on a
365 WHEN NOT MATCHED BY SOURCE
366 clause can only refer to columns from the target relation, since by definition there is no matching source row\&. A condition on a
367 WHEN NOT MATCHED [BY TARGET]
368 clause can only refer to columns from the source relation, since by definition there is no matching target row\&. Only the system attributes from the target table are accessible\&.
369 .RE
370 .PP
371 \fImerge_insert\fR
372 .RS 4
373 The specification of an
374 INSERT
375 action that inserts one row into the target table\&. 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\&.
376 .sp
377 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\&.
378 .sp
379 If the target table is a partitioned table, each row is routed to the appropriate partition and inserted into it\&. If the target table is a partition, an error will occur if any input row violates the partition constraint\&.
380 .sp
381 Column names may not be specified more than once\&.
382 \fBINSERT\fR
383 actions cannot contain sub\-selects\&.
384 .sp
385 Only one
386 VALUES
387 clause can be specified\&. The
388 VALUES
389 clause can only refer to columns from the source relation, since by definition there is no matching target row\&.
390 .RE
391 .PP
392 \fImerge_update\fR
393 .RS 4
394 The specification of an
395 UPDATE
396 action that updates the current row of the target table\&. Column names may not be specified more than once\&.
397 .sp
398 Neither a table name nor a
399 WHERE
400 clause are allowed\&.
401 .RE
402 .PP
403 \fImerge_delete\fR
404 .RS 4
405 Specifies a
406 DELETE
407 action that deletes the current row of the target table\&. Do not include the table name or any other clauses, as you would normally do with a
408 \fBDELETE\fR(7)
409 command\&.
410 .RE
411 .PP
412 \fIcolumn_name\fR
413 .RS 4
414 The name of a column in the target table\&. 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\&.) Do not include the table\*(Aqs name in the specification of a target column\&.
415 .RE
416 .PP
417 OVERRIDING SYSTEM VALUE
418 .RS 4
419 Without this clause, it is an error to specify an explicit value (other than
420 DEFAULT) for an identity column defined as
421 GENERATED ALWAYS\&. This clause overrides that restriction\&.
422 .RE
423 .PP
424 OVERRIDING USER VALUE
425 .RS 4
426 If this clause is specified, then any values supplied for identity columns defined as
427 GENERATED BY DEFAULT
428 are ignored and the default sequence\-generated values are applied\&.
429 .RE
430 .PP
431 DEFAULT VALUES
432 .RS 4
433 All columns will be filled with their default values\&. (An
434 OVERRIDING
435 clause is not permitted in this form\&.)
436 .RE
437 .PP
438 \fIexpression\fR
439 .RS 4
440 An expression to assign to the column\&. If used in a
441 WHEN MATCHED
442 clause, the expression can use values from the original row in the target table, and values from the
443 \fIdata_source\fR
444 row\&. If used in a
445 WHEN NOT MATCHED BY SOURCE
446 clause, the expression can only use values from the original row in the target table\&. If used in a
447 WHEN NOT MATCHED [BY TARGET]
448 clause, the expression can only use values from the
449 \fIdata_source\fR
450 row\&.
451 .RE
452 .PP
453 DEFAULT
454 .RS 4
455 Set the column to its default value (which will be
456 NULL
457 if no specific default expression has been assigned to it)\&.
458 .RE
459 .PP
460 \fIsub\-SELECT\fR
461 .RS 4
462 A
463 SELECT
464 sub\-query that produces as many output columns as are listed in the parenthesized column list preceding it\&. The sub\-query must yield no more than one row when executed\&. If it yields one row, its column values are assigned to the target columns; if it yields no rows, NULL values are assigned to the target columns\&. If used in a
465 WHEN MATCHED
466 clause, the sub\-query can refer to values from the original row in the target table, and values from the
467 \fIdata_source\fR
468 row\&. If used in a
469 WHEN NOT MATCHED BY SOURCE
470 clause, the sub\-query can only refer to values from the original row in the target table\&.
471 .RE
472 .PP
473 \fIoutput_alias\fR
474 .RS 4
475 An optional substitute name for
476 OLD
477 or
478 NEW
479 rows in the
480 RETURNING
481 list\&.
482 .sp
483 By default, old values from the target table can be returned by writing
484 OLD\&.\fIcolumn_name\fR
485 or
486 OLD\&.*, and new values can be returned by writing
487 NEW\&.\fIcolumn_name\fR
488 or
489 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
490 RETURNING WITH (OLD AS o, NEW AS n) o\&.*, n\&.*\&.
491 .RE
492 .PP
493 \fIoutput_expression\fR
494 .RS 4
495 An expression to be computed and returned by the
496 \fBMERGE\fR
497 command after each row is changed (whether inserted, updated, or deleted)\&. The expression can use any columns of the source or target tables, or the
498 \fBmerge_action()\fR
499 function to return additional information about the action executed\&.
500 .sp
501 Writing
502 *
503 will return all columns from the source table, followed by all columns from the target table\&. Often this will lead to a lot of duplication, since it is common for the source and target tables to have a lot of the same columns\&. This can be avoided by qualifying the
504 *
505 with the name or alias of the source or target table\&.
506 .sp
507 A column name or
508 *
509 may also be qualified using
510 OLD
511 or
512 NEW, or the corresponding
513 \fIoutput_alias\fR
514 for
515 OLD
516 or
517 NEW, to cause old or new values from the target table to be returned\&. An unqualified column name from the target table, or a column name or
518 *
519 qualified using the target table name or alias will return new values for
520 INSERT
521 and
522 UPDATE
523 actions, and old values for
524 DELETE
525 actions\&.
526 .RE
527 .PP
528 \fIoutput_name\fR
529 .RS 4
530 A name to use for a returned column\&.
531 .RE
532 .SH "OUTPUTS"
533 .PP
534 On successful completion, a
535 \fBMERGE\fR
536 command returns a command tag of the form
537 .sp
538 .if n \{\
539 .RS 4
540 .\}
541 .nf
542 MERGE \fItotal_count\fR
543 .fi
544 .if n \{\
545 .RE
546 .\}
547 .sp
548 The
549 \fItotal_count\fR
550 is the total number of rows changed (whether inserted, updated, or deleted)\&. If
551 \fItotal_count\fR
552 is 0, no rows were changed in any way\&.
553 .PP
554 If the
555 \fBMERGE\fR
556 command contains a
557 RETURNING
558 clause, the result will be similar to that of a
559 \fBSELECT\fR
560 statement containing the columns and values defined in the
561 RETURNING
562 list, computed over the row(s) inserted, updated, or deleted by the command\&.
563 .SH "NOTES"
564 .PP
565 The following steps take place during the execution of
566 \fBMERGE\fR\&.
567 .sp
568 .RS 4
569 .ie n \{\
570 \h'-04' 1.\h'+01'\c
571 .\}
572 .el \{\
573 .sp -1
574 .IP "  1." 4.2
575 .\}
576 Perform any
577 BEFORE STATEMENT
578 triggers for all actions specified, whether or not their
579 WHEN
580 clauses match\&.
581 .RE
582 .sp
583 .RS 4
584 .ie n \{\
585 \h'-04' 2.\h'+01'\c
586 .\}
587 .el \{\
588 .sp -1
589 .IP "  2." 4.2
590 .\}
591 Perform a join from source to target table\&. The resulting query will be optimized normally and will produce a set of candidate change rows\&. For each candidate change row,
592 .sp
593 .RS 4
594 .ie n \{\
595 \h'-04' 1.\h'+01'\c
596 .\}
597 .el \{\
598 .sp -1
599 .IP "  1." 4.2
600 .\}
601 Evaluate whether each row is
602 MATCHED,
603 NOT MATCHED BY SOURCE, or
604 NOT MATCHED [BY TARGET]\&.
605 .RE
606 .sp
607 .RS 4
608 .ie n \{\
609 \h'-04' 2.\h'+01'\c
610 .\}
611 .el \{\
612 .sp -1
613 .IP "  2." 4.2
614 .\}
615 Test each
616 WHEN
617 condition in the order specified until one returns true\&.
618 .RE
619 .sp
620 .RS 4
621 .ie n \{\
622 \h'-04' 3.\h'+01'\c
623 .\}
624 .el \{\
625 .sp -1
626 .IP "  3." 4.2
627 .\}
628 When a condition returns true, perform the following actions:
629 .sp
630 .RS 4
631 .ie n \{\
632 \h'-04' 1.\h'+01'\c
633 .\}
634 .el \{\
635 .sp -1
636 .IP "  1." 4.2
637 .\}
638 Perform any
639 BEFORE ROW
640 triggers that fire for the action\*(Aqs event type\&.
641 .RE
642 .sp
643 .RS 4
644 .ie n \{\
645 \h'-04' 2.\h'+01'\c
646 .\}
647 .el \{\
648 .sp -1
649 .IP "  2." 4.2
650 .\}
651 Perform the specified action, invoking any check constraints on the target table\&.
652 .RE
653 .sp
654 .RS 4
655 .ie n \{\
656 \h'-04' 3.\h'+01'\c
657 .\}
658 .el \{\
659 .sp -1
660 .IP "  3." 4.2
661 .\}
662 Perform any
663 AFTER ROW
664 triggers that fire for the action\*(Aqs event type\&.
665 .RE
666 .sp
667 If the target relation is a view with
668 INSTEAD OF ROW
669 triggers for the action\*(Aqs event type, they are used to perform the action instead\&.
670 .RE
671 .RE
672 .sp
673 .RS 4
674 .ie n \{\
675 \h'-04' 3.\h'+01'\c
676 .\}
677 .el \{\
678 .sp -1
679 .IP "  3." 4.2
680 .\}
681 Perform any
682 AFTER STATEMENT
683 triggers for actions specified, whether or not they actually occur\&. This is similar to the behavior of an
684 \fBUPDATE\fR
685 statement that modifies no rows\&.
686 .RE
687 .sp
688 In summary, statement triggers for an event type (say,
689 \fBINSERT\fR) will be fired whenever we
690 \fIspecify\fR
691 an action of that kind\&. In contrast, row\-level triggers will fire only for the specific event type being
692 \fIexecuted\fR\&. So a
693 \fBMERGE\fR
694 command might fire statement triggers for both
695 \fBUPDATE\fR
696 and
697 \fBINSERT\fR, even though only
698 \fBUPDATE\fR
699 row triggers were fired\&.
700 .PP
701 You should ensure that the join produces at most one candidate change row for each target row\&. In other words, a target row shouldn\*(Aqt join to more than one data source row\&. If it does, then only one of the candidate change rows will be used to modify the target row; later attempts to modify the row will cause an error\&. This can also occur if row triggers make changes to the target table and the rows so modified are then subsequently also modified by
702 \fBMERGE\fR\&. If the repeated action is an
703 \fBINSERT\fR, this will cause a uniqueness violation, while a repeated
704 \fBUPDATE\fR
705 or
706 \fBDELETE\fR
707 will cause a cardinality violation; the latter behavior is required by the
708 SQL
709 standard\&. This differs from historical
710 PostgreSQL
711 behavior of joins in
712 \fBUPDATE\fR
713 and
714 \fBDELETE\fR
715 statements where second and subsequent attempts to modify the same row are simply ignored\&.
716 .PP
717 If a
718 WHEN
719 clause omits an
720 AND
721 sub\-clause, it becomes the final reachable clause of that kind (MATCHED,
722 NOT MATCHED BY SOURCE, or
723 NOT MATCHED [BY TARGET])\&. If a later
724 WHEN
725 clause of that kind is specified it would be provably unreachable and an error is raised\&. If no final reachable clause is specified of either kind, it is possible that no action will be taken for a candidate change row\&.
726 .PP
727 The order in which rows are generated from the data source is indeterminate by default\&. A
728 \fIsource_query\fR
729 can be used to specify a consistent ordering, if required, which might be needed to avoid deadlocks between concurrent transactions\&.
730 .PP
731 When
732 \fBMERGE\fR
733 is run concurrently with other commands that modify the target table, the usual transaction isolation rules apply; see
734 Section\ \&13.2
735 for an explanation on the behavior at each isolation level\&. You may also wish to consider using
736 \fBINSERT \&.\&.\&. ON CONFLICT\fR
737 as an alternative statement which offers the ability to run an
738 \fBUPDATE\fR
739 if a concurrent
740 \fBINSERT\fR
741 occurs\&. There are a variety of differences and restrictions between the two statement types and they are not interchangeable\&.
742 .SH "EXAMPLES"
743 .PP
744 Perform maintenance on
745 customer_accounts
746 based upon new
747 recent_transactions\&.
748 .sp
749 .if n \{\
750 .RS 4
751 .\}
752 .nf
753 MERGE INTO customer_account ca
754 USING recent_transactions t
755 ON t\&.customer_id = ca\&.customer_id
756 WHEN MATCHED THEN
757   UPDATE SET balance = balance + transaction_value
758 WHEN NOT MATCHED THEN
759   INSERT (customer_id, balance)
760   VALUES (t\&.customer_id, t\&.transaction_value);
761 .fi
762 .if n \{\
763 .RE
764 .\}
765 .PP
766 Attempt to insert a new stock item along with the quantity of stock\&. If the item already exists, instead update the stock count of the existing item\&. Don\*(Aqt allow entries that have zero stock\&. Return details of all changes made\&.
767 .sp
768 .if n \{\
769 .RS 4
770 .\}
771 .nf
772 MERGE INTO wines w
773 USING wine_stock_changes s
774 ON s\&.winename = w\&.winename
775 WHEN NOT MATCHED AND s\&.stock_delta > 0 THEN
776   INSERT VALUES(s\&.winename, s\&.stock_delta)
777 WHEN MATCHED AND w\&.stock + s\&.stock_delta > 0 THEN
778   UPDATE SET stock = w\&.stock + s\&.stock_delta
779 WHEN MATCHED THEN
780   DELETE
781 RETURNING merge_action(), w\&.winename, old\&.stock AS old_stock, new\&.stock AS new_stock;
782 .fi
783 .if n \{\
784 .RE
785 .\}
786 .sp
787 The
788 wine_stock_changes
789 table might be, for example, a temporary table recently loaded into the database\&.
790 .PP
791 Update
792 wines
793 based on a replacement wine list, inserting rows for any new stock, updating modified stock entries, and deleting any wines not present in the new list\&.
794 .sp
795 .if n \{\
796 .RS 4
797 .\}
798 .nf
799 MERGE INTO wines w
800 USING new_wine_list s
801 ON s\&.winename = w\&.winename
802 WHEN NOT MATCHED BY TARGET THEN
803   INSERT VALUES(s\&.winename, s\&.stock)
804 WHEN MATCHED AND w\&.stock != s\&.stock THEN
805   UPDATE SET stock = s\&.stock
806 WHEN NOT MATCHED BY SOURCE THEN
807   DELETE;
808 .fi
809 .if n \{\
810 .RE
811 .\}
812 .sp
813 .SH "COMPATIBILITY"
814 .PP
815 This command conforms to the
816 SQL
817 standard\&.
818 .PP
819 The
820 WITH
821 clause,
822 BY SOURCE
823 and
824 BY TARGET
825 qualifiers to
826 WHEN NOT MATCHED,
827 DO NOTHING
828 action, and
829 RETURNING
830 clause are extensions to the
831 SQL
832 standard\&.