Online ALTER COLUMN final Sch-M lock duration bounds under concurrent DML in SQL Server 2019
0 reputation · 12 Apr 2024, 01:47 UTC
0 reputation · 12 Apr 2024, 01:47 UTC
SQL Server 2016 introduced online ALTER COLUMN for certain data type changes, allowing concurrent reads and writes during the operation. For incompatible conversions (e.g., int to bigint), a data-copy phase occurs followed by a brief schema modification (Sch-M) lock to switch metadata.
The documentation does not specify a maximum duration for the final Sch-M lock acquisition. The wait depends on transaction log flush, checkpoint activity, and any long-running transactions holding shared locks. Under heavy write load, this lock can extend to seconds, causing application timeouts — a gap for strict zero-downtime requirements.
MAX_DURATION or timeout option exists for ALTER COLUMN (unlike resumable index rebuild).ALTER COLUMN?35275 reputation · 12 Apr 2024, 08:00 UTC
There is no published upper bound for the final schema‑modification (Sch‑M) lock taken during an online ALTER COLUMN in SQL Server 2019. In practice the lock is held only for the time needed to flush the log and acquire the metadata lock – usually a few milliseconds – but it can extend to several seconds when the transaction log is being flushed, a checkpoint is in progress, or any session holds a shared lock on the table for a long time.
LOCK_TIMEOUT; if the wait exceeds the timeout the statement aborts with error 1222.The final Sch‑M lock is taken after the online data‑copy phase completes. Its duration depends on:
You can bound the wait by issuing SET LOCK_TIMEOUT 5000; (or any value in milliseconds) in the session that runs the ALTER COLUMN. If the lock cannot be acquired within the timeout, the statement is cancelled and error 1222 is returned. This does not require third‑party tools. However, aborting an online ALTER COLUMN after it has started the data‑copy phase may leave the table in a partially altered state; you must be prepared to roll back or restore from a backup.
UPDATE with a WHERE clause that limits rows, switch application logic to read/write the new column, then drop the old column.ALTER COLUMN during a maintenance window with no concurrent DML, or use a secondary replica (Always On/Log Shipping) to apply the change and then failover.SELECT request_session_id, resource_type, request_mode
FROM sys.dm_tran_locks
WHERE resource_associated_entity_id = OBJECT_ID('dbo.YourTable')
AND request_mode IN ('S','U');
SET LOCK_TIMEOUT 2000; -- 2 seconds
ALTER TABLE dbo.YourTable ALTER COLUMN ColName BIGINT;
If the statement fails with Msg 1222, the lock wait exceeded the timeout.SELECT *
FROM sys.dm_db_log_stats(DB_ID(), OBJECT_ID('dbo.YourTable'));
High log I/O correlates with longer Sch‑M waits.If you observe long‑running shared locks, consider resolving them (e.g., by completing or killing the blocking sessions) before attempting the ALTER COLUMN with a bounded LOCK_TIMEOUT.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 12 Apr 2024, 07:28 UTC
The shadow-table swap using sp_rename still requires a brief Sch-M lock on both the original and shadow tables for the metadata switch. While this lock is typically shorter than the online ALTER COLUMN Sch-M wait (no data copy occurs), it faces identical blocking from long-running transactions holding shared locks.
SWITCH — requires aligned partitioning; online index operations need Enterprise edition.Also note: sys.dm_exec_requests reports no percent_complete for ALTER COLUMN, so you cannot poll progress to time the Sch-M attempt.