What is an upsert? INSERT or UPDATE in one statement
An upsert writes a row by key: if no row with that key exists it inserts one, and if one exists it updates it instead. Databases spell it differently: INSERT ... ON CONFLICT, ON DUPLICATE KEY UPDATE, MERGE or INSERT OR REPLACE.
Why upserts exist
Syncing data means applying a batch of rows where some are new and some changed. Doing it as SELECT, then INSERT or UPDATE, takes two round trips per row and races with other writers: two sessions can both see no row and both insert. An upsert makes the decision inside the database, in one statement, using a primary key or unique constraint to detect the existing row.
INSERT ... ON CONFLICT (PostgreSQL, DuckDB, SQLite)
CREATE TABLE inventory (sku VARCHAR PRIMARY KEY, qty INTEGER, updated_at TIMESTAMP);
INSERT INTO inventory VALUES
('A-1', 10, TIMESTAMP '2026-11-01 09:00'),
('B-2', 5, TIMESTAMP '2026-11-01 09:00');
INSERT INTO inventory VALUES
('A-1', 7, TIMESTAMP '2026-11-20 12:00'),
('C-3', 3, TIMESTAMP '2026-11-20 12:00')
ON CONFLICT (sku) DO UPDATE
SET qty = excluded.qty, updated_at = excluded.updated_at;
-- Shorthand: replace the whole row
INSERT OR REPLACE INTO inventory VALUES ('B-2', 8, TIMESTAMP '2026-11-22 08:00');After both statements A-1 holds 7, B-2 holds 8 and C-3 is new. excluded refers to the row that was proposed for insertion. ON CONFLICT DO NOTHING skips duplicates instead of updating them. The conflict target must be backed by a primary key or unique constraint; on a table without one, DuckDB refuses the statement with a binder error.
MySQL and MERGE
INSERT INTO inventory (sku, qty) VALUES ('A-1', 7)
ON DUPLICATE KEY UPDATE qty = VALUES(qty);MySQL fires the update on a clash with any unique index, not one named column. Recent MySQL versions prefer a row alias (VALUES (...) AS new ... qty = new.qty) over the VALUES() function. MERGE is the SQL-standard form, used by SQL Server and Oracle and supported by PostgreSQL 15 and later. It joins a source to a target and can update, insert and delete in one statement.
MERGE INTO inventory AS t USING stock_updates AS s ON t.sku = s.sku WHEN MATCHED THEN UPDATE SET qty = s.qty WHEN NOT MATCHED THEN INSERT (sku, qty) VALUES (s.sku, s.qty);
Delete-then-insert and the trade-offs
Where no upsert syntax or unique key exists, as in most data lake tables, the usual substitute is to delete the keys in the incoming batch and insert the batch. It gives the same end state when the batch has one row per key, and it can be re-run safely.
CREATE TABLE stock_updates (sku VARCHAR, qty INTEGER);
INSERT INTO stock_updates VALUES ('A-1', 0), ('D-4', 12);
DELETE FROM inventory WHERE sku IN (SELECT sku FROM stock_updates);
INSERT INTO inventory SELECT sku, qty, now()::TIMESTAMP FROM stock_updates;- Upserts need a unique key. Without one, every form either fails or silently inserts duplicates.
- A batch with the same key twice is an error or ambiguous in most engines. Deduplicate the batch first, for example with QUALIFY.
- Replace-style upserts (INSERT OR REPLACE, delete-then-insert) overwrite every column, including ones the batch did not mean to change.
In Thunderduck
Thunderduck accepts INSERT, UPDATE, DELETE, CREATE, DROP and ALTER from callers who can write. Iceberg tables have no upsert, so a re-run load duplicates its rows; the docs recommend loading each batch once and clearing a batch before retrying it. Oracle and SQL Server catalogs are read-only and refuse MERGE along with every other write.
For a table that stays in sync with a source, a pipeline runs a dbt incremental model with incremental_strategy='delete+insert' and a unique_key. Each run reads a small overlapping window, for example two days, and replaces those keys, which absorbs late and changed rows.
Docs: https://docs.thunderduck.ai/data/pipelines/