MERGE in PostgreSQL inserts, updates, or deletes based on a match
Answer: MERGE conditionally inserts, updates, or deletes rows of a table. The statement uses MERGE INTO a target, USING a data source, ON a join condition, and one or more when clauses. RETURNING can use WITH OLD or NEW AS an output_alias and an output_expression.
When clauses
WHEN MATCHEDthen merge_update, merge_delete, or DO NOTHINGWHEN NOT MATCHED BY SOURCEthen merge_update, merge_delete, or DO NOTHINGWHEN NOT MATCHED [ BY TARGET ]then merge_insert or DO NOTHINGAND conditiona condition on the when_clause
Visibility in the parser
MATCHED actions may see the target and the source relations. NOT MATCHED BY SOURCE actions may see the target, but not the source. NOT MATCHED [BY TARGET] actions may see the source, but not the target. INSERT runs for NOT MATCHED [BY TARGET] tuples, so there is no target tuple to deal with. UPDATE and DELETE may see both source and target relations, unless they run for NOT MATCHED BY SOURCE. SELECT privileges are required for DO NOTHING.
See also
Claims
- id: C1 claim: MERGE can conditionally insert, update, or delete rows of a table. source: S1 evidence: "conditionally insert, update, or delete rows of a table"
- id: C2 claim: WHEN MATCHED, WHEN NOT MATCHED BY SOURCE, and WHEN NOT MATCHED [BY TARGET] choose the action. source: S1 evidence: "WHEN NOT MATCHED BY SOURCE [ AND condition ] THEN"
- id: C3 claim: INSERT actions can see only the source relation; UPDATE and DELETE can see source and target unless they run for NOT MATCHED BY SOURCE. source: S2 evidence: "MATCHED actions can see both target and source relations."
Sources
- [S1] MERGE. PostgreSQL Global Development Group. https://www.postgresql.org/docs/current/sql-merge.html . License: PostgreSQL License. Accessed 2026-11-20.
- [S2] parse_merge.c. PostgreSQL Global Development Group. https://raw.githubusercontent.com/postgres/postgres/master/src/backend/parser/parse_merge.c . License: PostgreSQL License. Accessed 2026-11-20.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: PostgreSQL Global Development Group, MERGE; PostgreSQL Global Development Group, parse_merge.c. PostgreSQL License; PostgreSQL License. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: PostgreSQL Global Development Group, MERGE; PostgreSQL Global Development Group, parse_merge.c. PostgreSQL License; PostgreSQL License.
Cite this
iinventedlight. 2026. "MERGE in PostgreSQL inserts, updates, or deletes based on a match." iinventedlight Library, iil:note:v5th0swj, version of 2026-11-20. https://iinventedlight.com/library/notes/postgres-merge/. License: CC-BY-4.0.
@misc{iil_note_v5th0swj,
author = {{iinventedlight}},
title = {{MERGE in PostgreSQL inserts, updates, or deletes based on a match}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-20},
url = {https://iinventedlight.com/library/notes/postgres-merge/},
note = {Permanent ID iil:note:v5th0swj. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- postgres-merge.table.json (table; CC-BY-4.0)
- postgres-merge.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/postgres-merge.md.