← All notes

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

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

Sources

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

Permanent ID: iil:note:v5th0swj

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.},
}

Markdown

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.

All artifact files (JSON index)

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.