Symptom
- A MERGE, UPDATE or DELETE runs much longer than usual. Query History shows status Blocked, or its time is mostly "transaction blocked" rather than execution.
- Downstream tasks and Airflow or dbt jobs pile up behind it. The warehouse is not busy, so this is not queuing for compute.
- Sometimes a statement fails after a long wait with a lock timeout error.
The default LOCK_TIMEOUT is 43200 seconds (12 hours). That is why a blocked load can sit silently for a whole night.
How Snowflake locks standard tables
| Statement | Lock behaviour | Runs in parallel with |
|---|---|---|
| UPDATE, DELETE, MERGE | Locks that generally prevent other UPDATE/DELETE/MERGE on the same table | Usually not with each other |
| INSERT, COPY INTO | Mostly write new micro-partitions | Often with other INSERT/COPY, sometimes with UPDATE/DELETE/MERGE |
Explicit transaction (BEGIN ... COMMIT) | Holds every lock it acquired until COMMIT or ROLLBACK | Whatever its locks allow |
| Hybrid tables | Row-level locks | Conflicting rows only |
Two causes account for most incidents:
- Several writers doing MERGE or UPDATE on the same table at the same time, for example per-source loads that all MERGE into one fact table.
- An open transaction left behind. A client with autocommit off ran DML and never committed, or a script ran
BEGIN, a statement failed, and the session kept going with the transaction still open.
Step 1: confirm it is a lock wait, not warehouse queuing
SELECT query_id, user_name, warehouse_name, execution_status,
start_time, DATEDIFF('minute', start_time, CURRENT_TIMESTAMP()) AS mins,
LEFT(query_text, 120) AS sql
FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY(RESULT_LIMIT => 1000))
WHERE execution_status IN ('BLOCKED', 'QUEUED', 'RUNNING')
ORDER BY start_time;
BLOCKED means a lock wait. QUEUED means the warehouse is full, which is a different problem (see the queuing and spilling post).
Step 2: find the holder now
SHOW LOCKS IN ACCOUNT;
SHOW TRANSACTIONS IN ACCOUNT;
SHOW LOCKS lists each lock with status = HOLDING or WAITING, the resource (table), and the transaction ID. Match a WAITING row's resource to the HOLDING row on the same resource. Without IN ACCOUNT you only see your own sessions, and the role needs privileges to see other users' locks.
Turn the output into something you can filter:
SHOW LOCKS IN ACCOUNT;
SELECT "resource", "type", "transaction", "transaction_started_on",
"status", "acquired_on", "query_id"
FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()))
ORDER BY "resource", "status";
Then look at the holder's session and its last statement:
SELECT query_id, session_id, user_name, role_name, execution_status,
start_time, end_time, LEFT(query_text, 200) AS sql
FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY_BY_SESSION(SESSION_ID => <session_id>))
ORDER BY start_time DESC
LIMIT 20;
A holder whose last statement finished minutes or hours ago is an abandoned open transaction. A holder that is still RUNNING is a long MERGE, and you have to decide whether to wait for it.
Step 3: release the lock
Caution
SYSTEM$ABORT_TRANSACTIONrolls back all work in that transaction, not just the last statement. If the holder is a multi-statement load, you lose its whole batch and must rerun it. Confirm with the owner of the job first, and note the transaction ID and SQL in the incident record.
-- Transaction ID from SHOW LOCKS / SHOW TRANSACTIONS
SELECT SYSTEM$ABORT_TRANSACTION(1700000000123456789);
-- Alternatively, cancel a single running statement but keep the session
SELECT SYSTEM$CANCEL_QUERY('<query_id>');
Cancelling a statement inside an explicit transaction does not end the transaction, so its locks can stay. For an abandoned transaction, abort the transaction.
Once the holder goes, waiters acquire the lock in order. Watch the BLOCKED list drain before you declare recovery.
Step 4: work out what happened afterwards
ACCOUNT_USAGE has the history, with some latency (up to a few hours for LOCK_WAIT_HISTORY):
SELECT l.object_name,
l.query_id AS waiting_query,
b.value:query_id::STRING AS blocker_query,
b.value:transaction_id::NUMBER AS blocker_txn,
b.value:is_snowflake::BOOLEAN AS blocker_is_snowflake,
l.requested_at, l.acquired_at,
DATEDIFF('second', l.requested_at, COALESCE(l.acquired_at, CURRENT_TIMESTAMP())) AS wait_s
FROM SNOWFLAKE.ACCOUNT_USAGE.LOCK_WAIT_HISTORY l,
LATERAL FLATTEN(input => l.blocker_queries) b
WHERE l.requested_at > DATEADD('day', -7, CURRENT_TIMESTAMP())
ORDER BY wait_s DESC
LIMIT 50;
is_snowflake = TRUE means the blocker was a Snowflake background process rather than one of your queries.
Find which jobs lose the most time to blocking:
SELECT user_name, warehouse_name,
COUNT(*) AS statements,
ROUND(SUM(transaction_blocked_time) / 1000 / 60, 1) AS blocked_minutes
FROM SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
WHERE start_time > DATEADD('day', -7, CURRENT_TIMESTAMP())
AND transaction_blocked_time > 0
GROUP BY 1, 2
ORDER BY blocked_minutes DESC;
Cause and fix
| Finding | Cause | Fix |
|---|---|---|
| Holder's last query finished long ago | Client autocommit off, or BEGIN without COMMIT after an error | Abort the transaction. Fix the client: commit explicitly, or turn autocommit on. Set TRANSACTION_ABORT_ON_ERROR = TRUE for that service user |
| Several MERGE jobs on one table at the same time | Parallel writers serialize on the table | Land each source in its own staging table, then one MERGE per table per cycle. Or serialize the writers in the orchestrator |
| Many small single-row UPDATEs | Row-at-a-time application writes | Batch the changes into a staging table and run one set-based MERGE |
| Long MERGE holding the lock for an hour | Large unpruned MERGE | Add a date or key predicate on the target so it prunes. Cluster the target on the merge key |
Blocker flagged is_snowflake | Background maintenance on the table | Usually short. If it recurs, schedule loads around it and contact support with the query IDs |
| Reporting reads blocked | Not expected: reads don't block on DML locks | Look again. It is probably warehouse queuing, not locks |
Set timeouts so blocking fails fast
Twelve hours of silent waiting is rarely what a pipeline wants. Set limits on the service users and warehouses that run loads:
ALTER USER svc_loader SET LOCK_TIMEOUT = 900; -- 15 min, then fail
ALTER USER svc_loader SET TRANSACTION_ABORT_ON_ERROR = TRUE; -- a failed statement aborts the transaction
ALTER USER svc_loader SET STATEMENT_TIMEOUT_IN_SECONDS = 7200;
-- Check what is in effect
SHOW PARAMETERS LIKE '%TIMEOUT%' FOR USER svc_loader;
Tip
A lock timeout turns a silent overnight stall into a failed task, with an alert, a retry and a clear error. Pair it with orchestrator retries and a backoff so the retry runs after the competing writer has finished.
For Python service code, make transactions explicit and short:
import snowflake.connector
conn = snowflake.connector.connect(**params) # autocommit is on by default
try:
conn.autocommit(False)
cur = conn.cursor()
cur.execute("ALTER SESSION SET LOCK_TIMEOUT = 600")
cur.execute("DELETE FROM sales.fact_orders WHERE load_date = %s", (d,))
cur.execute("INSERT INTO sales.fact_orders SELECT * FROM stage.orders WHERE load_date = %s", (d,))
conn.commit()
except Exception:
conn.rollback()
raise
finally:
conn.close()
Prevent it next time
- One writer per target table per cycle. Model this explicitly: an Airflow pool of size 1 per table, a single task graph in Snowflake Tasks, or a dbt model owning the table.
- Service users get
LOCK_TIMEOUT,TRANSACTION_ABORT_ON_ERRORandSTATEMENT_TIMEOUT_IN_SECONDSset at creation, as part of your account baseline. - Alert on any query in
BLOCKEDfor more than 15 minutes, and add a weekly report oftransaction_blocked_timeby user. - Review client code for
autocommit=Falsewithout afinallythat commits or rolls back.
Get the weekly HubDatAI email
One email a week with everything new: posts, learning sessions and videos. Free, no spam, unsubscribe in one click. What you get