Partial indexes for PostgreSQL job queues
Too long, didn’t read
The worker asked for one pending pickup. Postgres followed pickup numbers and discarded 100,000 completed jobs along the way. With an index that includes only pending jobs, the same request found its candidate without working through that completed stretch.
The primary-key scan starts at pickup 1
The warehouse keeps its records. Pickups 1 through 100,000 are still in the table, all marked done. The ten after them are pending.
The worker asks for the lowest-numbered pending pickup it can lock. The primary-key index gives Postgres the pickup numbers in order. To find a pending job, it still has to look up the status on the corresponding row.
The first number that passes that test is 100001. Once Postgres locks that pickup, the worker has what it asked for.
Give the worker a list of pending jobs
Create an index on the pickup number and include only rows whose status is pending. Postgres calls this a partial index. It still puts pickup numbers in order, and the first entry in this fixture is 100001. Partial indexes
The two runs used the same 100,010 rows and the same query on PostgreSQL 18.7.
| Index used | Rows discarded by filter | Rows returned by query |
|---|---|---|
| Primary key | 100,000 | 1 |
| Pending only | None reported | 1 |
The worker gets one pickup either way. The difference is buried one level down in the plan. Limit reports one row in both runs, while the first scan reports 100,000 discarded rows. A separate query found that the first pending pickup was 100001.
Postgres can consider the partial index when it knows the query can only match pending rows. A plan reused for an unknown status may have to work for other rows. Among the usable plans, Postgres chooses by cost.
Both plans had a LockRows step. The index supplies candidates and the query locks the row it takes. A complete claim also changes the status before committing.
Suppose another worker already holds a conflicting lock on 100001. This query can skip that row and try the next pending pickup. The recorded run used one worker. PostgreSQL row locking
Move the first pending job to the front
Make pickup 1 pending and mark 100001 done. You still have 100,000 completed pickups and ten pending ones. Where would the same primary-key scan stop this time?
At pickup 1 the scan already has a candidate. Two status changes bring the first pending job to the start of the scan.
When your worker looks for one pending job, check where pending rows sit in the order it reads. A long completed stretch gives the partial index work to save. The recorded plan shows that work under Rows Removed by Filter, even though both queries returned one row. Using EXPLAIN
Replay the two query plans
Use a fresh database on a disposable Postgres instance. Create and analyze the fixture below.
CREATE TABLE pickup_jobs (
id bigint PRIMARY KEY,
status text NOT NULL CHECK (status IN ('pending', 'done'))
);
INSERT INTO pickup_jobs (id, status)
SELECT id, CASE WHEN id <= 100000 THEN 'done' ELSE 'pending' END
FROM generate_series(1, 100010) AS rows(id);
ANALYZE pickup_jobs; Run this plan before adding the partial index. EXPLAIN ANALYZE executes the query and takes a row lock. Finish the transaction to release the lock if autocommit is off.
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF)
SELECT id
FROM pickup_jobs
WHERE status = 'pending'
ORDER BY id
LIMIT 1
FOR UPDATE SKIP LOCKED; Limit (actual rows=1.00 loops=1)
Buffers: shared hit=817
-> LockRows (actual rows=1.00 loops=1)
Buffers: shared hit=817
-> Index Scan using pickup_jobs_pkey on pickup_jobs (actual rows=1.00 loops=1)
Filter: (status = 'pending'::text)
Rows Removed by Filter: 100000 Add the partial index and run the same plan again.
CREATE INDEX pickup_jobs_pending_id
ON pickup_jobs (id)
WHERE status = 'pending'; Limit (actual rows=1.00 loops=1)
Buffers: shared hit=2 read=1
-> LockRows (actual rows=1.00 loops=1)
Buffers: shared hit=2 read=1
-> Index Scan using pickup_jobs_pending_id on pickup_jobs (actual rows=1.00 loops=1)
Filter: (status = 'pending'::text) Run SELECT min(id) FROM pickup_jobs WHERE status = 'pending'; to confirm the smallest pending pickup number separately.
Check the query your worker sends
For a partial index to be eligible, Postgres has to know that the query only asks for rows the index covers. Use the literal status = 'pending' in the first comparison. A generic prepared plan for status = $1 has to work for every status. A custom plan for the value pending can consider the index. The planner chooses among eligible plans by cost. Partial indexes · Prepared statements
Rows Removed by Filter counts rows discarded at one plan node, reported as a per-loop average when that node runs more than once. Buffer counters show page hits and reads. Try both on the pickup distribution and query your worker uses. If workers run at the same time, check row locks in a separate test. Using EXPLAIN
A queue reader that asks for a variety of statuses could also test a B-tree index on (status, id). It includes every status. An equality condition on status locates one group and id orders its pickups. Compare the plans and maintenance costs on the actual queue. Multicolumn indexes · Indexes and ORDER BY