Skip to content
masterclass Buddy

PostgreSQL · Command

MERGE

MERGE conditionally inserts, updates, deletes, or skips rows in a target table using source data. It joins source and target rows, then runs the first matching WHEN action for each candidate row.

Last updated

Syntax

[ WITH with_query [, ...] ] MERGE INTO [ ONLY ] target_table_name [ * ] [ [ AS ] target_alias ] USING data_source ON join_condition when_clause [...] [ RETURNING [ WITH ( { OLD | NEW } AS output_alias [, ...] ) ] { * | output_expression [ [ AS ] output_name ] } [, ...] ]

In-depth API docs are only for subscribers: parameters, examples, common mistakes and interview practice.

Subscribe
  • INSERT
  • UPDATE
  • DELETE
  • RETURNING
  • merge_action()
  • CREATE POLICY
  • ALTER TABLE ... ENABLE ROW LEVEL SECURITY

Source: PostgreSQL: Documentation: 18: MERGE. Checked 2026-11-03.