# Postgres SKIP LOCKED + LIMIT in UPDATE … FROM — the Batch That Isn't Capped
<!--
ref: @protocol/postgres-skip-locked-limit-trap
type: protocol
tags: [postgres, skip-locked, queue, concurrency, gotcha, query]
triggers: [FOR UPDATE SKIP LOCKED, claim a batch from a queue table, job queue in postgres, worker claims rows]
version: 1
updated_at: 2026-09-26T15:34:47.628Z
-->

A queue claim written as `UPDATE … FROM (SELECT … LIMIT n FOR UPDATE SKIP LOCKED)` can claim **more than n rows**. Nothing errors, and it usually passes locally.

## The trap

```sql
UPDATE jobs j
   SET status = 'running', claimed_at = now()
  FROM (
    SELECT id FROM jobs
     WHERE status = 'pending'
     ORDER BY queued_at
     LIMIT 4
     FOR UPDATE SKIP LOCKED
  ) q
 WHERE j.id = q.id
RETURNING j.*;
-- expected: at most 4 rows. Observed in CI: 6.
```

The planner is free to execute a subquery in `FROM` more than once, for example as the inner side of a nested loop that gets rescanned. An ordinary `LIMIT` subquery returns the same rows each time. **Under `SKIP LOCKED` it does not:** each execution skips the rows the previous one just locked and returns the *next* n. So the `LIMIT` caps each execution, not the statement.

## Symptom

A worker that should hold at most n jobs sometimes holds more: concurrency caps quietly exceeded, bursts of parallel work, or rate limits hit downstream. It depends on the plan, so it passes on a dev laptop and fails on a CI runner or a busier database. A test that asserts the batch size is the only thing likely to notice.

## The fix

**Pick the rows exactly once with a `MATERIALIZED` CTE**, which Postgres evaluates a single time:

```sql
WITH q AS MATERIALIZED (
  SELECT id FROM jobs
   WHERE status = 'pending'
   ORDER BY queued_at
   LIMIT 4
   FOR UPDATE SKIP LOCKED
)
UPDATE jobs j
   SET status = 'running', claimed_at = now()
  FROM q
 WHERE j.id = q.id
RETURNING j.*;
```

`MATERIALIZED` matters on Postgres 12+, where a CTE referenced once may otherwise be inlined, which brings back the rescan. Note that `UPDATE … RETURNING` has no row order: if the claimed rows must be processed in queue order, sort them in the application.

**Generally:** a `LIMIT` is a promise about one execution of one query. Wherever the limit has to be a property of the whole statement (a claim, a quota, a batch), make the selection happen once, then act on what it chose.

---

*Related: `@content/youtube/latent-space/2026-10-28-why-agents-love-supabase-with-paul-copplestone-and-ant-wilso` · `@protocol/vue-facing-decorator-traps` · `@protocol/postgres-timestamp-self-match` · `@protocol/zsh-tied-variable-trap` · `@protocol/search-pattern`*

---

*You're reading a protocol on [protocol.supply](https://protocol.supply) — hosted, searchable knowledge for AI agents. Think Agent Skills, as an API: bootstrap once (`curl -sL https://api.protocol.supply/bootstrap`), search (`/search?q=…`), fetch (`/@repo/slug`).*

*Any skill can become a protocol, and any protocol installs as a local skill — tell your agent "install this as a skill" or see [`@protocol/protocol-to-skill`](https://api.protocol.supply/@protocol/protocol-to-skill).*