Isolation, concurrent DML and lock waits
Concurrent sessions can read and write at the same time, but conflicting writes may wait or fail. Isolation defines which changes a statement can see.
Understand the idea
Use two separate sessions, not two tabs that share one connection. A read in session B does not see an uncommitted write in session A. Later statements can see newly committed data; a transaction does not guarantee one frozen snapshot across all its statements.
| Concept | What it means |
|---|---|
| READ COMMITTED | A statement reads previously committed data plus its own transaction changes. |
| Lock wait | A write waits for a conflicting transaction to release a resource. |
| Conflict handling | Inspect the failure, roll back when appropriate and retry a well-defined operation. |
A concrete example
A sees 110 in its own transaction. Other sessions still see the committed 100. Setup assumes AUTOCOMMIT enabled; confirm it before starting.
Remember: Each statement can observe newer committed work. Design retries around the complete business operation and avoid leaving transactions open.
Explain it step by step · full theory
Let us understand the idea
Use two separate sessions, not two tabs that share one connection. A read in session B does not see an uncommitted write in session A. Later statements can see newly committed data; a transaction does not guarantee one frozen snapshot across all its statements.
Conflicting updates on standard tables can block each other. Hybrid tables have different row-lock behavior. A timeout or deadlock requires error handling; do not assume an entire transaction has disappeared.
Reason through the SQL
The exercise uses a transient table because both sessions must see it. Temporary tables are session-local and unsuitable here. Use the same authorized learning role in both sessions and confirm CURRENT_SESSION() differs. Keep a warehouse selected in each.
Run the labeled blocks in order. Do not paste the entire lesson into one worksheet. Session A keeps an update open while B observes the old committed value and attempts a conflicting update. B uses a zero-second lock timeout to avoid a long wait. After the intentional error, explicitly run B cleanup before releasing A.
Words used in this lesson
- READ COMMITTED
- A statement reads previously committed data plus its own transaction changes.
- Lock wait
- A write waits for a conflicting transaction to release a resource.
- Conflict handling
- Inspect the failure, roll back when appropriate and retry a well-defined operation.
See how it works
Choose a step to explore the idea at your own pace.
Open the interactive explanation
Isolation, concurrent DML and lock waits
Begin with the question
Concurrent sessions can read and write at the same time, but conflicting writes may wait or fail. Isolation defines which changes a statement can see.
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
- Begin with the question
Concurrent sessions can read and write at the same time, but conflicting writes may wait or fail. Isolation defines which changes a statement can see.
- Choose the operation
Use two separate sessions, not two tabs that share one connection. A read in session B does not see an uncommitted write in session A. Later statements can see newly committed data; a transaction does not guarantee one frozen snapshot across all its statements.
- Check the meaning
Each statement can observe newer committed work. Design retries around the complete business operation and avoid leaving transactions open.
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
Optional two-session lab on a disposable standard table. Requires Module 03 schema setup, AUTOCOMMIT enabled during setup, CREATE TABLE and matching role access in both sessions. Never use production tables. The intentional error needs a separate ROLLBACK; session timeout settings must be restored.
1. Session A — setup and hold an update
USE DATABASE L2_OBJECTS_LAB; USE SCHEMA SALES;
SELECT CURRENT_SESSION();
CREATE TRANSIENT TABLE M4_LOCK_LAB (ID NUMBER, AMOUNT NUMBER);
INSERT INTO M4_LOCK_LAB VALUES (1,100);
BEGIN TRANSACTION;
UPDATE M4_LOCK_LAB SET AMOUNT=110 WHERE ID=1;
SELECT * FROM M4_LOCK_LAB;
-- Pause here. Keep this session open; do not COMMIT yet.What to expect and why
A sees 110 in its own transaction. Other sessions still see the committed 100. Setup assumes AUTOCOMMIT enabled; confirm it before starting.
2. Session B — read and deliberately conflict
USE DATABASE L2_OBJECTS_LAB; USE SCHEMA SALES;
SELECT CURRENT_SESSION();
SELECT * FROM M4_LOCK_LAB;
-- Record the current LOCK_TIMEOUT value and level first.
SHOW PARAMETERS LIKE 'LOCK_TIMEOUT' IN SESSION;
ALTER SESSION SET LOCK_TIMEOUT=0;
BEGIN TRANSACTION;
UPDATE M4_LOCK_LAB SET AMOUNT=120 WHERE ID=1;
-- Expected lock error while A holds its write.
-- Execute the following separately after that error:
ROLLBACK;
-- Restore the recorded session override if there was one.
-- Otherwise remove the exercise override:
ALTER SESSION UNSET LOCK_TIMEOUT;What to expect and why
B initially reads 100. Its UPDATE should fail to acquire the conflicting lock while A remains open. The client may stop at that error, so execute ROLLBACK separately. Restore a previous session-level timeout with ALTER SESSION SET LOCK_TIMEOUT = your_recorded_number instead of UNSET.
3. Session A — commit; then session B — reread
-- Session A:
COMMIT;
-- Then Session B:
SELECT * FROM L2_OBJECTS_LAB.SALES.M4_LOCK_LAB;
-- Finally Session A, after B has rolled back and finished:
DROP TABLE L2_OBJECTS_LAB.SALES.M4_LOCK_LAB;What to expect and why
After A commits, B reads 110. The failed B update must not become 120. Drop the lab object only after both sessions have ended their transactions.
Check your understanding
Choose an answer, then check your reasoning. This is practice, not a scored test.
Try it yourself
Explain why the shared exercise table is transient rather than temporary.
Compare your reasoning
The two sessions need the same stored object. A temporary table belongs to its creating session. A transient table is shared through normal privileges and remains until dropped.
Official sources and curriculum
Module 04 · See module scope