Concepts
Write strategies
append, merge, delete_insert and replace: how each writes rows and what it needs.
write_strategy says how a load writes rows into the destination table. Set it on a source for all
its tables, or on a table to override the source. Every table needs one.
| Strategy | Writes | Needs |
|---|---|---|
append |
Inserts the rows read. | Nothing. |
merge |
Updates rows whose unique_key matches, inserts the rest. |
A unique_key: columns, or "{primary_key}". |
delete_insert |
Deletes the cursor range it read, or the rows holding a replace_key value it read, then inserts the rows. |
A cursor_column or a replace_key, not both. |
replace |
Builds a new table from the whole source, and swaps it in at the end. | No cursor_column. |
[sources.erp]kind = "odbc"connection = "erp"include = ["pub.*"]write_strategy = "replace"
[sources.erp.tables."pub.stock_item"]write_strategy = "merge"cursor_column = "updated_at"unique_key = ["company_no", "item_no"]plan refuses a table with no write_strategy, or a merge without a unique_key:
$ sqlload plan…Errors (1)└── raw.app__page_views no write_strategy ├── reason public.page_views (kind = "postgres"): no write_strategy is set on the table or its │ source, and SQLLoad doesn't choose one └── fix add write_strategy = "merge" with unique_key = "{primary_key}" (or the key columns) under [sources.app] for all its tables, or under [sources.app.tables."public.page_views"] for this one; or choose "delete_insert" or "append", which can load a row updated since an earlier load again beside its old versionplan shows each table’s strategy with its key or cursor, such as
merge (unique_key: order_no, line_no ← {primary_key}).
append
Section titled “append”Each run inserts what it reads. With a cursor_column, it reads strictly after the destination’s
maximum (the first run from cursor_start, inclusive), so a row arriving later at or below the
maximum is never read. When that can happen, use delete_insert or merge with a lookback.
[sources.app.tables."public.events"]description = "Events, one row per event."write_strategy = "append"cursor_column = "event_id"append_cursor_inclusive = truereads from the maximum itself, inserting its rows again on every run.planwarns.appendrefuses a date cursor, which would lose rows added later to the last day; usedelete_insert. Files sources are exempt.appendtakes nolookback, which would insert the window again on every run.- A files source loads each file once.
merge upserts on unique_key: a column, a list such as ["order_id", "line_no"], or
"{primary_key}", the primary key the source reports. Without a cursor_column it reads the
whole table on every run.
"{primary_key}"takes only a reported primary key, never a unique constraint, and can’t be listed with other columns. A table without one failsplan. Python, files and Google Sheets sources report none: name the columns.- A table that doesn’t merge ignores its source’s
"{primary_key}". - Files targets don’t take
merge. A files source merges one file at a time; see Merging files.
Repeated keys
Section titled “Repeated keys”When one commit holds a unique_key value more than once, merge keeps one row for it. A NULL in
a key column counts as a value.
| Setting | Which row wins |
|---|---|
| Default | The row read last; of files, the last row of the newest file holding it. |
newest_by = "updated_at" |
The highest value; a NULL loses, and a tie goes to the row read later. |
newest_by = ["mod_date", "mod_time"] |
Compared in order: the next column breaks a tie. |
on_duplicate_keys = "fail" |
None: the commit fails before it writes, naming examples. Earlier commits stay. |
newest_by also applies against the table: a row overwrites its key’s row only when it isn’t
older, so late, older data never replaces newer data. A tie overwrites; a NULL never overwrites a
value. The load reports what it left alone, as in (12 older rows skipped: newest_by updated_at).
[sources.app.tables."public.readings"]write_strategy = "merge"unique_key = ["sensor_id"]newest_by = "read_at"- Higher
newest_byvalues are newer. Its columns can’t be key columns, masked, JSON, arrays or repeated, or a native type the target can’t order, such as a variant: pin a portable type incolumns. - Set on a source,
newest_byandon_duplicate_keysapply to itsmergetables only. A table that sets one drops the other from its source.
NULLs in keys
Section titled “NULLs in keys”merge matches a NULL in a key column with a NULL. A row keyed ("acme", "bolt", NULL) on
unique_key = ["company", "item", "site"] updates the table’s row of that key, and plan shows
merge (unique_key: company, item, site; NULLs in keys match).
null_keys = "fail" refuses a commit holding a NULL in a key column instead, before it writes;
earlier commits stay. Set on a source, it applies to its merge tables only.
[sources.erp.tables."pub.stock"]write_strategy = "merge"unique_key = ["company", "item", "site"]null_keys = "fail"plan refuses a "{primary_key}" key with a column that can be NULL, naming it, since such rows
may be different rows: name the unique_key columns instead. This refuses most BigQuery tables.
A Postgres target needs Postgres 15 or later to match NULLs.
Removing copies of a key
Section titled “Removing copies of a key”A table another tool wrote may hold several copies of a key with a NULL; each load updates them
all. Remove them once with sqlload load app__stock --reload (not on a
Postgres target), or keep only the newest copy by _sqlload_loaded_at. On DuckDB:
DELETE FROM raw.app__stockWHERE rowid IN ( SELECT rowid FROM ( SELECT rowid, row_number() OVER ( PARTITION BY company, item, site ORDER BY _sqlload_loaded_at DESC ) AS copy FROM raw.app__stock ) WHERE copy > 1);Rows deleted at the source
Section titled “Rows deleted at the source”A merge never sees a deleted row: its key just stops arriving. deletes makes a merge table mark
or drop the rows whose key the source no longer holds.
[sources.erp.tables."pub.stock_item"]write_strategy = "merge"cursor_column = "updated_at"unique_key = ["company_no", "item_no"]deletes = "soft" # or "hard"checks = { max_row_drop = "10%" }A plain load only merges. sqlload load --sync-deletes then reads every key of the source table
(or its sql query) and marks the rows of keys it didn’t read; run it as often as you need, such
as nightly.
softsets_sqlload_deleted_atto when the scan started, and a merge clears it when the key comes back.hardremoves the row.- A scan marks at most
checks.max_row_dropof the table, 5% by default. One that would mark more, as an empty view or a changed permission would, fails the table and marks nothing. - Files, Python and Google Sheets sources can’t list every key, so they don’t take
deletes. deletesloads into DuckDB, MotherDuck, Postgres, Snowflake, BigQuery and Databricks, not ClickHouse or files targets.- A table with
unnest, or sharing its destination throughconstant_columns, can’t take it.
$ sqlload plan -v --sync-deletes…└── raw.erp__stock_item merge (unique_key: company_no, item_no), then a full key scan of pub.stock_item soft-deletes the rows of keys it no longer holds (at most 10% of the table)…$ sqlload load --sync-deletes…loaded raw.erp__stock_item 1,204 rows (key scan of 48,112 keys: 37 rows soft-deleted) 6.3sDeletes from a tombstone table
Section titled “Deletes from a tombstone table”A table too big to scan can take its deletes from a source table that records each deleted key with the time, such as one a trigger writes:
[sources.erp.tables."pub.stock_item"]write_strategy = "merge"cursor_column = "updated_at"unique_key = ["company_no", "item_no"]deletes = "soft"deletes_from = { table = "audit.deleted_items", cursor_column = "deleted_at" }Every load first loads new tombstones into <table>__sqlload_deletes, by delete_insert. After the
merge commits, it marks the rows whose key has a tombstone at or after the row’s own cursor value,
so a key deleted and created again stays. Every tombstone is checked on every load.
$ sqlload plan -v…├── raw.erp__stock_item merge (unique_key: company_no, item_no), then soft-deletes the rows whose key audit.deleted_items lists deleted at or after the row's updated_at (its tombstones load first)└── raw.erp__stock_item__sqlload_deletes delete_insert (cursor: deleted_at)…$ sqlload load…loaded raw.erp__stock_item 1,204 rows (tombstones: 12 rows soft-deleted) 2.1s- A tombstone committed late with an earlier time, such as by a long transaction, is read only with
a
lookbackindeletes_fromthat reaches it, such aslookback = "1h". deletes_fromneedsdeletesand acursor_column. Undersoft,_sqlload_deleted_atis the latest matching tombstone’s time.- The tombstone table must hold every
unique_keycolumn under the same names, and can’t also be listed undertables. Its copy keeps every tombstone: trim it yourself. deleted_atmust compare exactly with the table’scursor_column: both timestamps with a time zone, both dates or timestamps without, both numbers, or both text. Undersoft, a timestamp.
delete_insert
Section titled “delete_insert”delete_insert deletes the cursor range it read, [start, end), then inserts the rows. It needs no
key, so it suits keyless tables and lookback windows. A row updated after it loaded is loaded
again beside its old version unless the old one is in the range.
[sources.app.tables."public.page_views"]description = "Page views, one row per view."write_strategy = "delete_insert"cursor_column = "viewed_at"lookback = "2h"To replace a fixed window around today, see Reload a window. On a files source partitioned by the cursor, it replaces whole partitions; see Which files load.
Replacing rows by a key
Section titled “Replacing rows by a key”With replace_key, each commit deletes every row holding a key value its rows hold, then inserts
its rows, in one transaction. Rows of other values stay.
[sources.finance.tables."le"]write_strategy = "delete_insert"replace_key = ["le_version"]Loading LE-FY27-P01 with lines A and B, into a table holding P01 with lines A, B and C and P02,
leaves P01 with A and B at the new amounts, and P02 as it was.
- A database or Python source reads every row on each run. A files source reads only new or changed files; see Replacing rows by a key.
- A NULL in a
replace_keycolumn matches a NULL;null_keys = "fail"refuses one instead. load --jsonlists the values replaced inreplaced_keys.
plan refuses replace_key:
| With | Why |
|---|---|
A cursor_column, including one set on the source |
The read returns part of a value’s rows, and the commit deletes all of them. Drop the cursor, or use merge. |
| A files target | It replaces partition folders. |
unnest |
Child tables aren’t replaced with their rows. |
replace
Section titled “replace”replace rebuilds the table from the whole source on every run, and swaps the new table in at the
end, so readers see the old one until then.
- A Python table that yields no rows into a table that has rows fails rather than emptying it. Set
allow_empty=Truewhen an empty result is real. - A Postgres target empties and refills the table in one transaction instead, keeping its grants, indexes and views.
Reloading a table
Section titled “Reloading a table”--reload rebuilds the selected tables from the whole source, whatever their strategy, after
confirmation or with --yes. Postgres targets and postgres_cdc tables refuse it. See
Reloading.
Several tenants in one table
Section titled “Several tenants in one table”constant_columns gives every row of a table the same values,
so several sources can load into one table, such as one database per customer. Put the constant in
the unique_key:
[sources.acme]kind = "postgres"connection = "acme_db"include = ["public.orders"]table_name = "{table}"constant_columns = { tenant_id = "acme" }write_strategy = "merge"unique_key = ["tenant_id", "order_id"]cursor_column = "updated_at"
[sources.globex]kind = "postgres"connection = "globex_db"include = ["public.orders"]table_name = "{table}"constant_columns = { tenant_id = "globex" }write_strategy = "merge"unique_key = ["tenant_id", "order_id"]cursor_column = "updated_at"Both load into orders. Each tenant’s merge matches only its own rows, and its cursor reads from
the maximum of its own rows.
- Tables sharing a destination must differ in at least one constant column.
planrefusesreplace,delete_insert,--reload,deletes,state = "logged", and amergewhoseunique_keyholds no constant column. - Separate runs into a shared table don’t wait for each other: don’t overlap them.
- Rows loaded before a constant was added hold
NULLin it, so the next run starts again fromcursor_start. - A constant can’t be the
cursor_column. Files tenants sharing a destination need their files in distinct locations.
See Cursors for how a run decides what to read.