---
source: https://iinventedlight.com/library/notes/postgres-merge.md
title: "MERGE in PostgreSQL inserts, updates, or deletes based on a match"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PostgreSQL License (PostgreSQL Global Development Group); PostgreSQL License (PostgreSQL Global Development Group); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: PostgreSQL Global Development Group, MERGE; PostgreSQL Global Development Group, parse_merge.c. PostgreSQL License; PostgreSQL License."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-20
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-20
as_of: 2026-11-20
confidence: medium
untested: "Not reproduced (2026-11-20). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: PostgreSQL MERGE"
tags: [postgresql, sql, merge, databases]
client: Librarian
sources:
  - id: S1
    title: "MERGE"
    publisher: PostgreSQL Global Development Group
    url: https://www.postgresql.org/docs/current/sql-merge.html
    accessed: 2026-11-20
    license: "PostgreSQL License"
  - id: S2
    title: "parse_merge.c"
    publisher: PostgreSQL Global Development Group
    url: https://raw.githubusercontent.com/postgres/postgres/master/src/backend/parser/parse_merge.c
    accessed: 2026-11-20
    license: "PostgreSQL License"
---
# 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 MATCHED` then merge_update, merge_delete, or DO NOTHING
- `WHEN NOT MATCHED BY SOURCE` then merge_update, merge_delete, or DO NOTHING
- `WHEN NOT MATCHED [ BY TARGET ]` then merge_insert or DO NOTHING
- `AND condition` a 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

- [postgres-upsert-on-conflict](https://iinventedlight.com/library/notes/postgres-upsert-on-conflict/)
- [postgres-transactions](https://iinventedlight.com/library/notes/postgres-transactions/)
- [postgres-views](https://iinventedlight.com/library/notes/postgres-views/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
