MERGE(7)
okay bestie, this is MERGE(7) from the PostgreSQL 17.11 Documentation, and itβs literally the single-statement queen of conditionally doing INSERT, UPDATE, and DELETE on a table. Manual name: MERGE. Periodt. π
β¨
π NAME π
MERGE β conditionally insert, update, or delete rows of a table πΈ
π¦ SYNOPSIS π¦
[ WITH with_query [, ...] ]
MERGE INTO [ ONLY ] target_table_name [ * ] [ [ AS ] target_alias ]
USING data_source ON join_condition
when_clause [...]
[ RETURNING { * | output_expression [ [ AS ] output_name ] } [, ...] ]
where data_source is:
{ [ ONLY ] source_table_name [ * ] | ( source_query ) } [ [ AS ] source_alias ]
and when_clause is:
{ WHEN MATCHED [ AND condition ] THEN { merge_update | merge_delete | DO NOTHING } |
WHEN NOT MATCHED BY SOURCE [ AND condition ] THEN { merge_update | merge_delete | DO NOTHING } |
WHEN NOT MATCHED [ BY TARGET ] [ AND condition ] THEN { merge_insert | DO NOTHING } }
and merge_insert is:
INSERT [( column_name [, ...] )]
[ OVERRIDING { SYSTEM | USER } VALUE ]
{ VALUES ( { expression | DEFAULT } [, ...] ) | DEFAULT VALUES }
and merge_update is:
UPDATE SET { column_name = { expression | DEFAULT } |
( column_name [, ...] ) = [ ROW ] ( { expression | DEFAULT } [, ...] ) |
( column_name [, ...] ) = ( sub-SELECT )
} [, ...]
and merge_delete is:
DELETE
π DESCRIPTION π
okay so MERGE performs actions that modify rows in the target table identified as target_table_name, using the data_source. It gives you a single SQL statement that can conditionally INSERT, UPDATE, or DELETE rows β a whole task that would otherwise require multiple procedural language statements. iconic behavior, honestly π«Ά
First, the MERGE command performs a join from data_source to the target table, producing zero or more candidate change rows. For each candidate change row, the status of MATCHED, NOT MATCHED BY SOURCE, or NOT MATCHED [BY TARGET] is set just once, after which WHEN 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 WHEN clause is executed for any candidate change row. no cap. π
MERGE actions have the same effect as regular UPDATE, INSERT, or DELETE commands of the same names. The syntax of those commands is different, notably that there is no WHERE clause and no table name is specified. All actions refer to the target table, though modifications to other tables may be made using triggers. triggers stay sneaky like that π
When DO NOTHING is specified, the source row is skipped. Since actions are evaluated in their specified order, DO NOTHING can be handy to skip non-interesting source rows before more fine-grained handling. itβs giving βfilter out the NPCs firstβ energy β¨
The optional RETURNING clause causes MERGE to compute and return value(s) based on each row inserted, updated, or deleted. Any expression using the source or target tableβs columns, or the merge_action() function can be computed. When an INSERT or UPDATE action is performed, the new values of the target tableβs columns are used. When a DELETE is performed, the old values of the target tableβs columns are used. The syntax of the RETURNING list is identical to that of the output list of SELECT. love that consistency π
There is no separate MERGE privilege. Privilege vibes are very specific here:
- If you specify an update action, you must have the
UPDATEprivilege on the column(s) of the target table that are referred to in theSETclause. π - If you specify an insert action, you must have the
INSERTprivilege on the target table. πΈ - If you specify a delete action, you must have the
DELETEprivilege on the target table. π¦ - If you specify a
DO NOTHINGaction, you must have theSELECTprivilege on at least one column of the target table. β
You will also need SELECT privilege on any column(s) of the data_source and of the target table referred to in any condition (including join_condition) or expression. Privileges are tested once at statement start and are checked whether or not particular WHEN clauses are executed. yes bestie, even the clauses that ghost the execution still get privilege-checked π
π¨ Heads up: MERGE is not supported if the target table is a materialized view, foreign table, or if it has any rules defined on it. thatβs a hard no, not a vibe π
π PARAMETERS π
Okay bestie, this is the big one β every single knob and dial on MERGE. Buckle up, we're going through them all, no skipping. π
with_query
The WITH clause lets you specify one or more subqueries that can be referenced by name in the MERGE query. See Section 7.8 and SELECT(7) for all the deets. Heads up though: WITH RECURSIVE is not supported by MERGE. Not today, recursion. π
target_table_name
The name (optionally schema-qualified) of the target table or view you're merging into. If ONLY is specified before a table name, matching rows are updated or deleted in the named table only. If ONLY is not specified, matching rows are also updated or deleted in any tables inheriting from the named table. Optionally, * can be specified after the table name to explicitly indicate that descendant tables are included. Important little caveat: the ONLY keyword and * option do not affect insert actions β those always insert into the named table only. Periodt. π
If target_table_name is a view, it must either be automatically updatable with no INSTEAD OF triggers, or it must have INSTEAD OF triggers for every type of action (INSERT, UPDATE, and DELETE) specified in the WHEN clauses. Views with rules are not supported, sorry babe. π
target_alias
A substitute name for the target table. The way this works: when an alias is provided, it completely hides the actual name of the table. So like, given MERGE INTO foo AS f, the remainder of the MERGE statement must refer to this table as f not foo. Don't be out here using the government name. π
source_table_name
The name (optionally schema-qualified) of the source table, view, or transition table. If ONLY is specified before the table name, matching rows are included from the named table only. If ONLY is not specified, matching rows are also included from any tables inheriting from the named table. Optionally, * can be specified after the table name to explicitly indicate that descendant tables are included.
source_query
A query (SELECT statement or VALUES statement) that supplies the rows to be merged into the target table. Refer to the SELECT(7) statement or VALUES(7) statement for a description of the syntax. β¨
source_alias
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 at all. Sneaky sneaky. π
join_condition
join_condition is an expression resulting in a value of type boolean (similar to a WHERE clause) that specifies which rows in the data_source match rows in the target table.
β οΈ Warning: Only columns from the target table that attempt to matchdata_sourcerows should appear injoin_condition.join_conditionsubexpressions that only reference the target table's columns can affect which action is taken, often in surprising ways. Like, genuinely surprising. Do not play with this. π
If both WHEN NOT MATCHED BY SOURCE and WHEN NOT MATCHED [BY TARGET] clauses are specified, the MERGE command will perform a FULL join between data_source and the target table. For this to work, at least one join_condition 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. Them's the rules, bestie. π«Ά
when_clause
At least one WHEN clause is required. Non-negotiable, it's giving mandatory. β¨
The WHEN clause may specify WHEN MATCHED, WHEN NOT MATCHED BY SOURCE, or WHEN NOT MATCHED [BY TARGET]. Note that the SQL standard only defines WHEN MATCHED and WHEN NOT MATCHED (which is defined to mean no matching target row). WHEN NOT MATCHED BY SOURCE is an extension to the SQL standard β as is the option to append BY TARGET to WHEN NOT MATCHED, to make its meaning more explicit. We love an extension that spells things out. π¦
If the WHEN clause specifies WHEN MATCHED and the candidate change row matches a row in the data_source to a row in the target table, the WHEN clause is executed if the condition is absent or it evaluates to true.
If the WHEN clause specifies WHEN NOT MATCHED BY SOURCE and the candidate change row represents a row in the target table that does not match a row in the data_source, the WHEN clause is executed if the condition is absent or it evaluates to true.
If the WHEN clause specifies WHEN NOT MATCHED [BY TARGET] and the candidate change row represents a row in the data_source that does not match a row in the target table, the WHEN clause is executed if the condition is absent or it evaluates to true. Simple as that. π
condition
An expression that returns a value of type boolean. If this expression for a WHEN clause returns true, then the action for that clause is executed for that row. π―
A condition on a WHEN MATCHED clause can refer to columns in both the source and the target relations. A condition on a WHEN NOT MATCHED BY SOURCE clause can only refer to columns from the target relation, since by definition there is no matching source row. A condition on a WHEN NOT MATCHED [BY TARGET] 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. That's the boundary, respect it. πΈ
merge_insert
The specification of an INSERT action that inserts one row into the target table. The target column names can be listed in any order β go off, alphabetize if you want. If no list of column names is given at all, the default is all the columns of the table in their declared order.
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. π
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. Watch out for that one. π
Column names may not be specified more than once. INSERT actions cannot contain sub-selects. Only one VALUES clause can be specified. The VALUES clause can only refer to columns from the source relation, since by definition there is no matching target row. No cap, that's just how it is. π₯
merge_update
The specification of an UPDATE action that updates the current row of the target table. Column names may not be specified more than once. π
Neither a table name nor a WHERE clause are allowed. Like, at all. Don't try it. π
merge_delete
Specifies a DELETE 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 DELETE(7) command. It's giving minimalist. π
column_name
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's name in the specification of a target column. π«Ά
OVERRIDING SYSTEM VALUE
Without this clause, it is an error to specify an explicit value (other than DEFAULT) for an identity column defined as GENERATED ALWAYS. This clause overrides that restriction. Slay, override. π
OVERRIDING USER VALUE
If this clause is specified, then any values supplied for identity columns defined as GENERATED BY DEFAULT are ignored and the default sequence-generated values are applied. Bye bye, your input. π¦
DEFAULT VALUES
All columns will be filled with their default values. (An OVERRIDING clause is not permitted in this form.) Nope, not even then. β¨
expression
An expression to assign to the column. If used in a WHEN MATCHED clause, the expression can use values from the original row in the target table, and values from the data_source row. If used in a WHEN NOT MATCHED BY SOURCE clause, the expression can only use values from the original row in the target table. If used in a WHEN NOT MATCHED [BY TARGET] clause, the expression can only use values from the data_source row. Scope matters, bestie. π―
DEFAULT
Set the column to its default value (which will be NULL if no specific default expression has been assigned to it). Classic. π
sub-SELECT
A SELECT 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 WHEN MATCHED clause, the sub-query can refer to values from the original row in the target table, and values from the data_source row. If used in a WHEN NOT MATCHED BY SOURCE clause, the sub-query can only refer to values from the original row in the target table. One row max, no exceptions. π
output_expression
An expression to be computed and returned by the MERGE 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 merge_action() function to return additional information about the action executed. Iconic little function, honestly. π
Writing * 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 * with the name or alias of the source or target table. Pro tip energy, but it's literally in the manual so it's canon. π
output_name
A name to use for a returned column. That's it, that's the whole thing. π
β¨ OUTPUTS β¨
On successful completion, a MERGE command returns a command tag of the form
MERGE total_count
The total_count is the total number of rows changed (whether inserted, updated, or deleted). If total_count is 0, no rows were changed in any way. Zero, zilch, nothing happened. π«Ά
If the MERGE command contains a RETURNING clause, the result will be similar to that of a SELECT statement containing the columns and values defined in the RETURNING list, computed over the row(s) inserted, updated, or deleted by the command. πΈ
πΈ NOTES πΈ
okay so bestie, here's the literal backstage tour of what happens while MERGE is running. take notes π
- Perform any
BEFORE STATEMENTtriggers for all actions specified, whether or not theirWHENclauses match. - 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:
- Evaluate whether each row is
MATCHED,NOT MATCHED BY SOURCE, orNOT MATCHED [BY TARGET]. - Test each
WHENcondition in the order specified until one returns true. - When a condition returns true, perform the following actions:
- Perform any
BEFORE ROWtriggers that fire for the action's event type. - Perform the specified action, invoking any check constraints on the target table.
- Perform any
AFTER ROWtriggers that fire for the action's event type.
- Perform any
- Evaluate whether each row is
If the target relation is a view with INSTEAD OF ROW triggers for the action's event type, they are used to perform the action instead.
- Perform any
AFTER STATEMENTtriggers for actions specified, whether or not they actually occur. This is similar to the behavior of anUPDATEstatement that modifies no rows.
So yeah β in summary, statement triggers for an event type (say, INSERT) will be fired whenever we specify an action of that kind. In contrast, row-level triggers will fire only for the specific event type being executed. So a MERGE command might fire statement triggers for both UPDATE and INSERT, even though only UPDATE row triggers were fired. the way that works is a little unhinged but it's giving exactly that πβ¨
π¨ bestie, pay attention here: you should ensure that the join produces at most one candidate change row for each target row. In other words, a target row shouldn't 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 byMERGE. If the repeated action is anINSERT, this will cause a uniqueness violation, while a repeatedUPDATEorDELETEwill cause a cardinality violation; the latter behavior is required by the SQL standard. This differs from historical PostgreSQL behavior of joins inUPDATEandDELETEstatements where second and subsequent attempts to modify the same row are simply ignored. no cap, that difference is real π«Ά
If a WHEN clause omits an AND sub-clause, it becomes the final reachable clause of that kind (MATCHED, NOT MATCHED BY SOURCE, or NOT MATCHED [BY TARGET]). If a later WHEN 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. lowkey important π
The order in which rows are generated from the data source is indeterminate by default. A source_query can be used to specify a consistent ordering, if required, which might be needed to avoid deadlocks between concurrent transactions. bestie, ordering is not guaranteed unless you say so π¦
When MERGE is run concurrently with other commands that modify the target table, the usual transaction isolation rules apply; see Section 13.2 for an explanation on the behavior at each isolation level. You may also wish to consider using INSERT ... ON CONFLICT as an alternative statement which offers the ability to run an UPDATE if a concurrent INSERT occurs. There are a variety of differences and restrictions between the two statement types and they are not interchangeable. periodt β they are not the same thing, don't swap them like they're dupes π
π EXAMPLES π
Perform maintenance on customer_accounts based upon new recent_transactions. slay:
MERGE INTO customer_account ca
USING recent_transactions t
ON t.customer_id = ca.customer_id
WHEN MATCHED THEN
UPDATE SET balance = balance + transaction_value
WHEN NOT MATCHED THEN
INSERT (customer_id, balance)
VALUES (t.customer_id, t.transaction_value);
Notice that this would be exactly equivalent to the following statement because the MATCHED result does not change during execution. iykyk β¨
MERGE INTO customer_account ca
USING (SELECT customer_id, transaction_value FROM recent_transactions) AS t
ON t.customer_id = ca.customer_id
WHEN MATCHED THEN
UPDATE SET balance = balance + transaction_value
WHEN NOT MATCHED THEN
INSERT (customer_id, balance)
VALUES (t.customer_id, t.transaction_value);
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't allow entries that have zero stock. Return details of all changes made. this one is honestly iconic π
MERGE INTO wines w
USING wine_stock_changes s
ON s.winename = w.winename
WHEN NOT MATCHED AND s.stock_delta > 0 THEN
INSERT VALUES(s.winename, s.stock_delta)
WHEN MATCHED AND w.stock + s.stock_delta > 0 THEN
UPDATE SET stock = w.stock + s.stock_delta
WHEN MATCHED THEN
DELETE
RETURNING merge_action(), w.*;
The wine_stock_changes table might be, for example, a temporary table recently loaded into the database. keeping it cute and temp π
Update wines 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. full glow-up energy π
MERGE INTO wines w
USING new_wine_list s
ON s.winename = w.winename
WHEN NOT MATCHED BY TARGET THEN
INSERT VALUES(s.winename, s.stock)
WHEN MATCHED AND w.stock != s.stock THEN
UPDATE SET stock = s.stock
WHEN NOT MATCHED BY SOURCE THEN
DELETE;
β¨ COMPATIBILITY β¨
This command conforms to the SQL standard. π
The WITH clause, BY SOURCE and BY TARGET qualifiers to WHEN NOT MATCHED, DO NOTHING action, and RETURNING clause are extensions to the SQL standard. so those bits are the little extras, bestie π
π that's a wrap on this iconic lil' page β it's from PostgreSQL 17.11, dated 2026. go forth and merge responsibly, and remember: one candidate row per target row, mwah π«Άβ¨πΈ
π¦ where this page comes from
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms