Versioned upserts¶
Part of Streaming Transformations. See all pages in this section.
Versioned upserts keep the highest-version row for each logical key. A pipe appends complete row versions streamed to a table; versioned upsert merges row versions in the background. Queries can read the latest deduplicated state (LATEST_DEDUP), all currently stored row versions, including those not yet merged (LATEST_ALL), or the state covered by the last background merge pass (LAST_DEDUP). Versioned upserts enable the application of change streams produced by a CDC solution and are currently supported for Interactive tables only.
Example¶
The following example creates an Interactive table as the target for order revisions and a matching pipe:
Stream these complete records through order_state_pipe in the order shown:
After the client confirms that the records have been committed, the latest state can be queried:
The result is (9001, DELIVERED, 4). The highest version wins, not the last arrival. Replaying an identical row with the same key and version still returns one logical row.
Prepare a table for versioned upserts¶
Versioned upserts are configured on the target table, not in the pipe definition.
(1) A PRIMARY KEY … INITIALLY DEFERRED constraint defines the logical identity of rows. Ingestion accepts repeated keys.
(2) VERSION BY determines which complete row to retain for rows sharing the same key. Use a version that reliably orders changes for each key, such as a source sequence number or timestamp. When multiple rows have the same VERSION BY value, the merge process picks one row at random.
(3) CLUSTER BY sets the clustering key. It must include the primary key columns and can include further columns used in your queries.
Stream data into the table¶
Stream complete row images into the table, including unchanged columns. These can be change streams produced by a CDC solution. Use non-NULL keys and version values as configured for the table.
Read data¶
(4) Use DEDUP_OPTION on the table reference to choose the read behavior:
LATEST_DEDUPreturns the highest-version row per key, including data not yet merged. It is more expensive thanLAST_DEDUPas it needs to merge the latest ingested data at query time.LAST_DEDUPreturns the highest-version row per key covered by the last merge pass. It can return no rows for a populated table before the first merge pass. Merge passes are triggered by ongoing commits and are spaced at least several minutes apart. UseLATEST_DEDUPwhen results must reflect all committed data.LATEST_ALLreturns all currently stored row versions, including those not yet merged. The background merge process removes superseded rows, so this mode is not a permanent version history.
Default read mode¶
The default read mode is LATEST_ALL. To change the default read mode:
This affects only read statements; it does not trigger a merge pass. A query-level DEDUP_OPTION overrides the effective table default, which can inherit schema, database, or account settings. Specify the mode rather than assuming a default.
Sizing¶
Choose the read mode for the result you need, then measure latency with your data:
- Query-time work:
LATEST_DEDUPadds deduplication work that grows with scanned and unconsolidated data. - Freshness:
LAST_DEDUPavoids that work but lags ingestion. - Serving: cluster for your predicates and attach the table to an interactive warehouse with
ALTER WAREHOUSE ... ADD TABLESto warm arriving data into its cache. - Validation: compare equivalent populated results. An empty
LAST_DEDUPquery is not a latency comparison with a populatedLATEST_DEDUPquery.
Limitations¶
- Standard Snowflake tables and managed Iceberg tables are not currently supported.
- Only Snowpipe Streaming with high-performance architecture is supported. Batch loading and direct
INSERT,UPDATE,DELETE, andMERGEinto the target are not supported. - A table with versioned upserts can’t be used as the source for an insert-triggered materialized view.
- A table with versioned upserts can’t be used as the source for dynamic tables or table streams.
- Versioned upserts do not preserve the history of all versions.
LATEST_ALLreturns only the currently stored row versions, including superseded versions that haven’t been merged yet. - Merge passes run as ingestion continues. If streaming stops, the most recent commits might stay invisible to
LAST_DEDUP; useLATEST_DEDUPto read them.
See also¶
The CDC scenario shows versioned upserts with filtering.
Previous: Insert-triggered materialized views · Next: Reference