// dialect comparison

Advisory and application locks in every SQL dialect

Application locks let code coordinate across servers through the database: lock a name, do the work, release.

Filed under Sessions & admin

The verdict

Four facilities, four APIs, and SQLite with none. PostgreSQL's advisory locks are the closest to a real API, MySQL's GET_LOCK is the simplest, and SQL Server and Oracle both wrap the feature in procedural code.

Where they differ

Four engines have one, each with a different shape: named locks with timeouts in MySQL, 64-bit-key locks in PostgreSQL, a stored procedure in SQL Server and a package in Oracle. SQLite has none.

How each engine writes it

SQL Server

BEGIN TRANSACTION;
DECLARE @r INT;
EXEC @r = sp_getapplock @Resource = 'report_job', @LockMode = 'Exclusive', @LockOwner = 'Transaction', @LockTimeout = 10000;
-- @r >= 0 means acquired; do the work here
COMMIT; -- releases the lock

Locks are per database. Owner 'Transaction' (the default) needs an open transaction, or the call fails with -999, and releases at COMMIT; 'Session' lasts until sp_releaseapplock or disconnect. Return codes: 0 granted, 1 granted after waiting, -1 timeout, -2 cancelled, -3 deadlock victim.

PostgreSQL

SELECT pg_advisory_lock(918273);
-- ... serialized work ...
SELECT pg_advisory_unlock(918273);

Locks on a bigint (or two ints) key, held per session until unlocked or disconnect. Variants: pg_try_advisory_lock (non-blocking), xact variants released at commit, shared mode.

MySQL

SELECT GET_LOCK('report_job', 10);
-- returns 1 = acquired, 0 = timeout
SELECT RELEASE_LOCK('report_job');

Named locks at connection scope; IS_FREE_LOCK and IS_USED_LOCK check state. Releasing happens implicitly when the connection drops.

SQLite

-- no application locks; coordinate in application code or via a lock table

No named-lock facility. A common trick: INSERT into a jobs/locks table with a UNIQUE column; the successful insert owns the work.

Oracle

DECLARE
  h VARCHAR2(128);
BEGIN
  DBMS_LOCK.ALLOCATE_UNIQUE('report_job', h);
  IF DBMS_LOCK.REQUEST(h, DBMS_LOCK.X_MODE, 10) = 0 THEN
    -- ... serialized work ...
    IF DBMS_LOCK.RELEASE(h) = 0 THEN NULL; END IF;
  END IF;
END;
/

Needs EXECUTE on DBMS_LOCK. ALLOCATE_UNIQUE maps a name to a handle (and commits); REQUEST takes a mode and a timeout in seconds and returns 0 on success, 1 on timeout. Without RELEASE the lock lasts until the session ends.

Questions that come up

Why use these instead of a table lock?

They serialize application work without touching data: only one node runs the nightly job, without a scheduler column or polling races.

What happens if my session dies holding one?

All four engines release the lock when the owning session ends, so crashes do not leave permanent locks behind. Transaction-scoped locks (SQL Server's default owner, PostgreSQL's pg_advisory_xact_lock) go even earlier, at commit or rollback.

PostgreSQL lock key in two ints?

pg_advisory_lock takes one bigint or two int4 values; the two-int form is handy for namespaces like (app_id, job_id).

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.