Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk5 min

How PostgreSQL Row Locking Works in a Concurrent Job Queue

PostgreSQL workers can claim jobs concurrently with FOR UPDATE SKIP LOCKED, provided the claim is made atomically and the queue design accounts for ordering, transaction scope, and crash recovery.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Multiple PostgreSQL workers can claim different jobs without waiting on one another by selecting eligible rows with FOR UPDATE SKIP LOCKED, changing those rows to a claimed state in the same short transaction, and committing. The row locks coordinate workers while that transaction is open; the committed state change records the claim after the locks are released.

What a PostgreSQL row lock does

A locking clause on SELECT locks the rows returned by the query. For example, FOR UPDATE blocks other transactions from making conflicting updates, deletes, or row-locking operations on those rows until the transaction ends. Ordinary readers are not blocked by these row locks.

If another transaction tries to lock a row already held by a conflicting lock, it normally waits. When the lock becomes available, PostgreSQL can lock and return the updated row if it still exists; if the competing transaction deleted it, the waiting query may return no row. Locks are normally released at transaction end, or when rolling back to a savepoint that predates the lock.

Choosing a lock strength

PostgreSQL offers FOR UPDATE, FOR NO KEY UPDATE, FOR SHARE, and FOR KEY SHARE. They have different conflict behavior: FOR UPDATE is the strongest of these row-lock modes, while the others are weaker or shared modes. For a queue worker that will update a job’s status, FOR UPDATE is a clear default. Use a weaker mode when it provides the protection the operation actually needs; the strongest lock is not required for every query. See the PostgreSQL 16 SELECT documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How workers use SKIP LOCKED to claim jobs

SKIP LOCKED tells PostgreSQL not to wait for rows that cannot be locked immediately. Those rows are left out of that query’s result, allowing another worker to claim other eligible jobs. PostgreSQL specifically identifies queue-like consumers as a useful case, while warning that skipping locked rows provides an inconsistent view and is not suitable for general-purpose reads.

Here is an illustrative pattern for claiming up to 10 pending jobs. It assumes a table named jobs with id, status, priority, and created_at columns; adapt it to the actual schema and PostgreSQL version.

BEGIN;

WITH picked AS (
    SELECT id
    FROM jobs
    WHERE status = 'pending'
    ORDER BY priority DESC, created_at, id
    LIMIT 10
    FOR UPDATE SKIP LOCKED
)
UPDATE jobs AS j
SET status = 'running'
FROM picked
WHERE j.id = picked.id
RETURNING j.*;

COMMIT;

The selection, row locks, and status change are part of one transaction. Concurrent workers skip rows another claim transaction has locked. After commit, the changed status remains visible to other transactions even though the row locks have been released. The returned rows are the jobs that this worker claimed.

  1. Begin a short transaction.
  2. Select a bounded batch of eligible jobs in the intended order, using FOR UPDATE SKIP LOCKED.
  3. Update the selected rows to a claimed or running state and return them.
  4. Commit the claim transaction.
  5. Perform the potentially slow job work after commit, rather than holding database row locks while calling external services or doing long-running work.

The SQL illustrates the locking pattern, not a complete production queue. Confirm its syntax, indexes, state transitions, and behavior against the deployed PostgreSQL release and table definition.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

NOWAIT, waiting, and skipping are different choices

Behavior What happens on a locked row Typical fit
Ordinary row locking The statement waits for a conflicting lock to end. When waiting for that particular row is acceptable.
NOWAIT The statement errors instead of waiting. When the caller should fail or choose another action immediately.
SKIP LOCKED The locked row is omitted from the result; the query can return other eligible rows. Queue consumers distributing available work among concurrent workers.

These options affect row-lock behavior. PostgreSQL still takes the required table-level lock in the ordinary way. For exact syntax and semantics, consult the PostgreSQL 16 SELECT reference.

Ordering, priority, and fairness

SQL does not promise a predictable row order without ORDER BY. If the queue is intended to process older jobs first, an ordering such as ORDER BY created_at, id makes that policy explicit. For priority followed by age, ORDER BY priority DESC, created_at, id expresses the tie-breaking rule; including a unique ID makes ordering deterministic when the other values match.

SKIP LOCKED is designed to let workers make progress despite contention, not to guarantee strict FIFO ordering or starvation freedom. A worker can skip an in-use high-priority job and claim a later one; repeated contention can mean some rows are bypassed multiple times. Treat ordering and fairness as queue-design choices rather than properties guaranteed by the lock clause.

There is also a documented ordering caveat at READ COMMITTED: if the query waits on a row and an ordering column changes during that wait, rows can be returned out of order relative to the original sort. If strict ordering matters, prevent sort-key changes during claims or serialize priority changes through an appropriate application rule, then validate that approach for the workload. See PostgreSQL 17 SELECT documentation.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Keep lock time short; plan for recovery separately

The lock protects the claim transaction, not the entire lifetime of the job. A committed running status is a durable record of the claim, but it does not by itself recover a job if its worker crashes after commit. Systems that need crash recovery commonly add a lease or timeout and a process that reclaims expired work; the exact recovery policy is an application design decision, not a guarantee provided by row locking.

Batch size is a practical trade-off rather than a universal number: a larger batch can reduce claim round trips but holds more rows locked during the transaction, while a smaller batch limits lock exposure but can require more coordination. Keep the transaction limited to claiming and updating rows, not job execution.

Isolation levels and retry behavior

At READ COMMITTED, PostgreSQL may wait for a concurrent updater and then apply the locking-query behavior described above. The changing-sort-key caveat applies when ordering matters.

At REPEATABLE READ or SERIALIZABLE, a locking attempt can fail if the target row changed since the transaction snapshot. Applications using these isolation levels need to handle transaction errors and use an appropriate retry strategy. Row locks also protect selected rows; they do not automatically enforce arbitrary business rules involving multiple rows. Review PostgreSQL’s transaction isolation documentation when choosing broader consistency rules.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The practical boundary of SKIP LOCKED

PostgreSQL’s documentation describes the key trade-off directly: “Skipping locked rows provides an inconsistent view of the data, so this is not suitable for general purpose work, but can be used to avoid lock contention with multiple consumers accessing a queue-like table.” That makes SKIP LOCKED a targeted work-distribution tool: use it to find claimable jobs, not as a substitute for a consistent general-purpose read.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.