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:

CREATE INTERACTIVE TABLE order_state (
  order_id NUMBER NOT NULL,
  status VARCHAR,
  record_version NUMBER NOT NULL,
  CONSTRAINT pk_order_id
    PRIMARY KEY (order_id) INITIALLY DEFERRED                  -- (1)
)
VERSION BY record_version                                      -- (2)
CLUSTER BY (order_id);                                         -- (3)

CREATE PIPE order_state_pipe
  AS COPY INTO order_state (order_id, status, record_version)
  FROM (
    SELECT s.$1:order_id::NUMBER,
           s.$1:status::VARCHAR,
           s.$1:record_version::NUMBER
    FROM TABLE(DATA_SOURCE(TYPE => 'STREAMING')) s
  );

Stream these complete records through order_state_pipe in the order shown:

{"order_id": 9001, "status": "PENDING", "record_version": 1}
{"order_id": 9001, "status": "DELIVERED", "record_version": 4}
{"order_id": 9001, "status": "SHIPPED", "record_version": 3}

After the client confirms that the records have been committed, the latest state can be queried:

SELECT order_id, status, record_version
  FROM order_state DEDUP_OPTION = LATEST_DEDUP                       -- (4)
  WHERE order_id = 9001;

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:

  1. LATEST_DEDUP returns the highest-version row per key, including data not yet merged. It is more expensive than LAST_DEDUP as it needs to merge the latest ingested data at query time.
  2. LAST_DEDUP returns 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. Use LATEST_DEDUP when results must reflect all committed data.
  3. LATEST_ALL returns 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.
SELECT * FROM order_state DEDUP_OPTION = LAST_DEDUP;
SELECT * FROM order_state DEDUP_OPTION = LATEST_DEDUP;
SELECT * FROM order_state DEDUP_OPTION = LATEST_ALL;

Default read mode

The default read mode is LATEST_ALL. To change the default read mode:

ALTER TABLE order_state
  SET DEFAULT_INTERACTIVE_TABLE_SCAN_VERSION = LATEST_DEDUP;

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:

  1. Query-time work: LATEST_DEDUP adds deduplication work that grows with scanned and unconsolidated data.
  2. Freshness: LAST_DEDUP avoids that work but lags ingestion.
  3. Serving: cluster for your predicates and attach the table to an interactive warehouse with ALTER WAREHOUSE ... ADD TABLES to warm arriving data into its cache.
  4. Validation: compare equivalent populated results. An empty LAST_DEDUP query is not a latency comparison with a populated LATEST_DEDUP query.

Limitations

  1. Standard Snowflake tables and managed Iceberg tables are not currently supported.
  2. Only Snowpipe Streaming with high-performance architecture is supported. Batch loading and direct INSERT, UPDATE, DELETE, and MERGE into the target are not supported.
  3. A table with versioned upserts can’t be used as the source for an insert-triggered materialized view.
  4. A table with versioned upserts can’t be used as the source for dynamic tables or table streams.
  5. Versioned upserts do not preserve the history of all versions. LATEST_ALL returns only the currently stored row versions, including superseded versions that haven’t been merged yet.
  6. Merge passes run as ingestion continues. If streaming stops, the most recent commits might stay invisible to LAST_DEDUP; use LATEST_DEDUP to read them.

See also

The CDC scenario shows versioned upserts with filtering.


Previous: Insert-triggered materialized views · Next: Reference