Skip to main content
Enterprise only
MERGE INTO applies the existing merge-insert operation from SQL. It matches source rows against target keys and commits the selected target changes atomically. The source can be a table or an aliased subquery.

Supported clauses

Specify at least one action, with at most one clause of each kind. NOT MATCHED BY TARGET is also accepted for insert. Qualify both sides of every ON equality with the target or source name or alias. Multiple key equalities can be joined with AND; other join conditions are refused. Merge keys cannot change. An update changes only the supplied columns. Assignments and inserted values can contain source expressions, evaluated before matching, but cannot read the target or contain subqueries. Put source subqueries in USING instead. When both update and insert are present, they must write the same columns and source expressions. The update can omit unchanged merge keys; the insert must explicitly include every key with its source value from ON. For example, both arms can use s.value + 1, but using s.value in one arm and 0 in the other is refused. Update predicates can read qualified target columns and source columns carried unchanged through that shared projection. Delete predicates can read only qualified target columns. Conditional inserts, matched deletes, multiple clauses of the same kind, and OUTPUT are unsupported.
The delete clause applies to target rows with no matching source key. With an empty source it can delete every target row satisfying its predicate.

INSERT ON CONFLICT

INSERT ... ON CONFLICT uses the same atomic merge-insert operation. Specify one or more conflict key columns, then update matching rows or leave them unchanged. Unmatched rows are inserted in either case.
The column list is optional; omitting it uses the table’s non-computed columns in schema order. Source values are converted to their destination types before matching conflict keys. Multiple key columns match together, as with MERGE. The update and insert actions share one projection. Assign every inserted non-key column using SET col = EXCLUDED.col; unchanged key columns can be omitted from SET. Columns outside the INSERT column list remain unchanged on matched rows. Existing-row expressions, different source columns in an assignment, and DO UPDATE ... WHERE are unsupported. A bare ON CONFLICT DO NOTHING, ON CONFLICT ON CONSTRAINT, and RETURNING are also unsupported. There are no unique constraints from which to infer conflict keys. Duplicate source keys follow MERGE behavior. On tables without a WAL, multiple source rows updating the same target row fail atomically. Duplicate keys absent from the target can insert multiple rows, including with DO NOTHING; the operation does not enforce uniqueness or deduplicate the source. A NULL key does not match another NULL key. On WAL tables, the existing read-on-write merge path determines duplicate handling, just as for MERGE. These forms require the same INSERT and UPDATE privileges as MERGE, including DO NOTHING. They use the same WAL routing and computed-column protections described below. The result reports operation MERGE, affected rows, and the resulting table version; rows skipped by DO NOTHING are not affected rows.

Types and identifiers

Merge keys and source columns read by a predicate must have the same data type as their target projection columns. String representations (Utf8, LargeUtf8, Utf8View) can convert between one another, as can binary representations. Other implicit conversions on these columns are refused before writing because casting could change a match or predicate result. Write-only values use the normal write casts. Choose a conversion explicitly in USING when needed. For example, casting a floating-point key to BIGINT there makes the converted key part of the SQL comparison:
Names follow the session’s SQL identifier rules: by default, unquoted names are lowercased and quoted names preserve case. Thus t.ID reads id, while t."ID" reads the distinct uppercase column. Disabling datafusion.sql_parser.enable_ident_normalization preserves unquoted case too. Computed columns, including Function outputs, cannot be written by MERGE. A Function output cannot be a merge key. Fill those columns through REFRESH COLUMN.

WAL tables

On a table with a write-ahead log (WAL) write specification, MERGE uses the existing bounded read-on-write merge path. Its ON keys must be exactly the table’s stored, indexed primary key. Predicates and NOT MATCHED BY SOURCE DELETE are refused. Source data must fit the WAL request body limit; an oversized request fails instead of falling back to a direct table write.

Result

The DML result reports operation MERGE, the total number of updated, inserted, and deleted rows, and the resulting table version.