// dialect comparison

Lock wait and lock timeouts in every SQL dialect

A lock timeout keeps one blocked statement from freezing a queue behind it. Units differ: milliseconds in SQL Server and SQLite (and PostgreSQL without a unit), seconds in MySQL and Oracle.

Filed under Sessions & admin

The verdict

Five different mechanisms with different granularity and scope. Statement-level control (Oracle's WAIT) is the most precise; session variables (SQL Server, PostgreSQL, MySQL) are the practical default; SQLite's busy_timeout solves a different, file-level problem.

Where they differ

No shared syntax: SQL Server uses a session SET, MySQL and PostgreSQL use session variables, Oracle puts WAIT on the statement, and SQLite's pragma is about the database file lock.

How each engine writes it

SQL Server

SET LOCK_TIMEOUT 5000;
-- next statements wait at most 5s for a lock, then error 1222

Milliseconds, per session, sticky until changed. A timeout raises error 1222; handle it in the client rather than retrying blindly.

PostgreSQL

SET lock_timeout = '5s';
-- also: SET statement_timeout, SET idle_in_transaction_session_timeout

Takes a unit ('5s', '500ms'); a bare number means milliseconds. Set it per session, role or database; a statement that waits longer fails with SQLSTATE 55P03.

MySQL

SET SESSION innodb_lock_wait_timeout = 5;
-- seconds; error 1205 on timeout

InnoDB row-lock waits in seconds (default 50). SELECT ... FOR UPDATE NOWAIT / SKIP LOCKED skip the wait entirely per statement.

SQLite

PRAGMA busy_timeout = 5000;
-- milliseconds; SQLITE_BUSY becomes an error after this

Controls how long a connection waits for the database file lock (SQLITE_BUSY), not row locks, since SQLite has none.

Oracle

SELECT * FROM accounts WHERE id = 1 FOR UPDATE WAIT 5;
-- ORA-30006 after 5 seconds (or NOWAIT to fail at once)

No session-wide lock timeout; the WAIT n clause is per statement. ddl_lock_timeout applies to DDL waiting for locks.

Questions that come up

Lock timeout vs deadlock?

A lock timeout is you giving up while waiting. A deadlock is the engine detecting a cycle and killing one victim; they are unrelated mechanisms.

Should my app retry on a lock timeout?

Usually yes, with jitter and a bounded count. Timeouts often mean contention spikes; immediate blind retries make them worse.

What is the default?

SQL Server waits forever (-1), MySQL 50s, PostgreSQL no lock_timeout (but statement_timeout may apply), SQLite 0ms by default via busy_timeout, Oracle waits forever without WAIT.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.