Offsets, consuming DML and rollback
SELECT inspects a stream. A committed transaction that consumes it through DML advances its offset; rollback preserves the unconsumed changes.
Understand the idea
Think of the offset as a bookmark, not a delete button on the source. Advancing it acknowledges the change interval for that stream. Source rows remain in the source table.
| Concept | What it means |
|---|---|
| SELECT | Observe changes without acknowledging them. |
| Consuming DML | Use stream data in a write operation. |
| COMMIT | Accept writes and the stream advancement together. |
| ROLLBACK | Undo writes and preserve the previous offset. |
A concrete example
After the preceding lesson, counts are 0 audit rows and 4 stream rows. SELECT can be repeated without consuming.
Remember: Filtering is a routing decision, not selective acknowledgement.
Explain it step by step · full theory
Understand the concept
Think of the offset as a bookmark, not a delete button on the source. Advancing it acknowledges the change interval for that stream. Source rows remain in the source table.
A consuming statement can advance the stream even when its WHERE condition discards change rows. You cannot keep unselected rows waiting simply by filtering them out. Route all needed changes deliberately before commit.
Walk through the behavior
Within an explicit transaction, reads of the stream use a consistent transaction boundary. New source changes committed after that boundary belong to a later interval. Keep processing short enough that history retention is not threatened.
This demonstration stores the four change records in an audit table. The table is created before BEGIN so DDL does not interfere with the transaction.
Words used in this lesson
- SELECT
- Observe changes without acknowledging them.
- Consuming DML
- Use stream data in a write operation.
- COMMIT
- Accept writes and the stream advancement together.
- ROLLBACK
- Undo writes and preserve the previous offset.
See how it works
Choose a step to explore the idea at your own pace.
Open the interactive explanation
Offsets, consuming DML and rollback
Observe
SELECT sees four change rows; the bookmark stays put.
Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.
Read the complete diagram explanation
- Observe
SELECT sees four change rows; the bookmark stays put.
- Rollback
The audit insert is undone and the bookmark stays at A.
- Commit
Audit rows persist and the stream advances together.
Try the SQL or command
Read → predict → run in your learning environment → compare. You can also learn by reading without an account.
New to SQL? Where to run it and how to read it
A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.
- No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
- For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
- Read the entire example, including comments beginning with
--. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor. - Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
- SELECT / FROM
- Choose the values to return / identify their input.
- VALUES / COLUMN1
- VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
- AS / WITH
- AS gives a result a readable name. WITH names a temporary query step within one statement.
- NULL / ::
- NULL means missing or unknown. The :: notation converts a value to a named data type.
- Session
- One connection and its settings. Keep related transaction steps in the same session.
- Database.schema.table
- A full address for stored data. It is separate from the warehouse that processes queries.
Need the L2_OBJECTS_LAB setup? Open the first database-object lesson →
Help with common practice errors
- No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
- Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
- Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
- Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.
Before running this lesson
Manual learning lab; nothing runs on this website. Complete lesson 1 setup in L2_OBJECTS_LAB.M11_LAB and follow the lesson order. Use the same authorized role and warehouse for manual SQL. Task templates use <lab_warehouse>: replace it with an approved warehouse name. Do not activate several consumers against the same stream. Each task starts suspended; running or resuming it can consume compute credits.
Prove rollback does not consume
CREATE TABLE CHANGE_AUDIT (ORDER_ID NUMBER,AMOUNT NUMBER(12,2),ACTION VARCHAR,IS_UPDATE BOOLEAN,ROW_ID VARCHAR);
BEGIN TRANSACTION;
INSERT INTO CHANGE_AUDIT SELECT ORDER_ID,AMOUNT,METADATA$ACTION,
METADATA$ISUPDATE,METADATA$ROW_ID FROM SOURCE_CHANGES;
ROLLBACK;
SELECT COUNT(*) FROM CHANGE_AUDIT;
SELECT COUNT(*) FROM SOURCE_CHANGES;What to expect and why
After the preceding lesson, counts are 0 audit rows and 4 stream rows. SELECT can be repeated without consuming.
Commit the same processing
BEGIN TRANSACTION;
INSERT INTO CHANGE_AUDIT SELECT ORDER_ID,AMOUNT,METADATA$ACTION,
METADATA$ISUPDATE,METADATA$ROW_ID FROM SOURCE_CHANGES;
COMMIT;
SELECT COUNT(*) FROM CHANGE_AUDIT;
SELECT COUNT(*) FROM SOURCE_CHANGES;What to expect and why
Expect 4 audit rows and an empty stream if no further source changes occurred. The source still contains current orders 101 and 103.
Check your understanding
Choose an answer, then check your reasoning. This is practice, not a scored test.
Try it yourself
Why create the audit table before BEGIN?
Compare your reasoning
DDL can affect transaction boundaries. Prepare objects first, then keep the intended consuming DML inside one explicit transaction.