A deduplication key must name one intent#
A deterministic ID exists so that a retry converges. A worker that crashes after enqueueing a follow-up job and then runs again must produce the same job ID, so the second enqueue finds the first and does nothing. That is the contract of a unique key with ON CONFLICT DO NOTHING: the PostgreSQL INSERT documentation says the clause "simply avoids inserting a row as its alternative action", and the command tag counts only rows actually inserted.
So the key has two jobs. A repeat of the same intent must produce the same key, and two different intents must produce different keys. The first job is easy to test and usually gets tested. The second fails quietly, because the database cannot tell a duplicate from a collision. Both arrive as a conflict on the same key.
A timestamp looks like a good ingredient because it changes between events. It changes only as fast as its precision, though. recheck:42:after:1796196503 is the same string for every recheck of item 42 decided in that second. Two decisions in the same second are two intents with one key.
When a seconds bucket holds and when it breaks#
A seconds-based key holds as long as nothing can produce two intents for the same subject within one second. A scheduler can satisfy that condition if its enqueue path enforces five minutes between distinct intents for the same subject. A five-minute due time alone leaves concurrent and manual enqueue paths to check. The assumption breaks wherever the spacing is not enforced:
- A test that makes a scheduled job due at once and drains the queue, so two decisions land in the same second.
- A catch-up after downtime, when a backlog of overdue jobs runs back to back.
- A fast path added later, such as a manual "check now" button, that reuses the same key builder.
- Clock adjustments that move wall time backwards, which can also repeat a second.
None of these raises an error. The lost job appears later as something that "never ran".
The lab#
The lab uses Docker and takes a minute. It schedules a recheck for item 42, then has the recheck pause again within the same second, as a fast test or a backlog would.
docker run -d --rm --name pl-c-sameid -e POSTGRES_PASSWORD=x postgres:18
docker exec -it pl-c-sameid psql -U postgres
CREATE TABLE runs (
id text PRIMARY KEY,
filing_id int NOT NULL,
due_at timestamptz NOT NULL
);
-- Use one fixture timestamp for two separate events in the same second.
SELECT timestamptz '2026-12-02 07:28:23.298322+00' AS observed_at \gset
-- First pause of item 42: schedule a recheck.
INSERT INTO runs
SELECT 'recheck:42:after:' || floor(extract(epoch FROM :'observed_at'::timestamptz))::bigint,
42, :'observed_at'::timestamptz + interval '5 minutes'
ON CONFLICT (id) DO NOTHING;
-- The recheck runs at once and pauses again, still in the same second.
INSERT INTO runs
SELECT 'recheck:42:after:' || floor(extract(epoch FROM :'observed_at'::timestamptz))::bigint,
42, :'observed_at'::timestamptz + interval '5 minutes'
ON CONFLICT (id) DO NOTHING;
SELECT id, due_at FROM runs;
On PostgreSQL 18.7:
INSERT 0 1
INSERT 0 0
id | due_at
-----------------------------+-------------------------------
recheck:42:after:1796196503 | 2026-12-02 07:33:23.298322+00
(1 row)
The second pause scheduled nothing. Anything waiting for the second recheck now waits forever.
Now key the recheck by the run that decided to pause. A retry of the same decision reuses the deciding run's identity and still converges, while a new decision comes from a different run and gets a new key.
TRUNCATE runs;
ALTER TABLE runs ADD COLUMN seq bigserial UNIQUE;
INSERT INTO runs (id, filing_id, due_at) VALUES ('filing:42', 42, now());
CREATE FUNCTION pause(deciding_run text) RETURNS text
LANGUAGE sql AS $$
INSERT INTO runs (id, filing_id, due_at)
SELECT 'recheck:42:after-run:' || seq, 42, clock_timestamp() + interval '5 minutes'
FROM runs WHERE id = deciding_run
ON CONFLICT (id) DO NOTHING
RETURNING id
$$;
SELECT pause('filing:42'); -- first pause
SELECT pause('filing:42'); -- the same decision, retried
SELECT pause('recheck:42:after-run:1'); -- the recheck pauses again
The three calls return recheck:42:after-run:1, an empty result for the retry, and recheck:42:after-run:2. The retry converged, and the second pause got its own job regardless of the clock. Remove the container with docker rm -f pl-c-sameid.
Build the key from the cause, not the clock#
Pick the ingredient that differs between intents and repeats within one intent:
- The deciding run's ID, as in the lab. Every follow-up a run schedules is keyed by that run, so its retries converge and the next run's follow-ups are new.
- A counter on the subject, incremented in the same transaction that schedules the job. A rolled-back attempt rolls back the counter too, so its retry reuses the number.
- The source event's own ID, such as a webhook delivery ID or a message ID, when one event causes exactly one job.
- A random ID created once and stored, the approach Stripe recommends for idempotency keys: "V4 UUIDs, or another random string with enough entropy to avoid collisions." It works only if the caller stores the key before the first attempt and reuses it on retry; a fresh UUID per attempt deduplicates nothing.
Keep the scheduled time as a column. If the time must be part of the key, it has to be the time the intent was decided and stored, not a fresh clock read on each attempt, and its precision must be finer than the fastest legitimate repeat.
Make a collision visible#
ON CONFLICT DO NOTHING treats every conflict as a harmless duplicate. Check that it really is one. Return the inserted row with RETURNING, and when nothing comes back, read the existing row and compare what matters: the subject, the payload or a hash of it. Stripe's API does the same: its idempotency layer "compares incoming parameters to those of the original request and errors if they're not the same". A mismatch is a bug in the key, and it should fail loudly instead of passing for a retry. The webhook deduplication guide covers keys that come from the sender's delivery ID.
What the regression test showed#
A scheduled filing paused while its target was unreachable and scheduled a recheck five minutes later, keyed ...:after:{unix seconds}. The regression test exercised that same code with a shorter interval. A new test made the paused run due at once and drained the queue again in the same second. The second pause produced the same run ID, the enqueue converged on the first run, and the test's query for the newly scheduled run failed with RowNotFound. The change retained the production key and rebuilt the tests so each subject pauses on its first run. The test established the collision under the accelerated schedule.
Where this came from#
Proofline found this in a test of its paused-filing rechecks and included the change in a production release tagged 30 November 2026. The RowNotFound in What the regression test showed came from the test run. Proofline reviews GitHub pull requests; new organizations get one calendar month of hosted code review without a cloud connection. Start a free GitHub review ยท Eligibility and fair-use terms
Questions#
Would milliseconds instead of seconds fix the key?
It narrows the window without closing it, and it breaks the retry case: a retry reads the clock again and gets a new key, so it no longer converges. Use an identity that a retry repeats, such as the deciding run's ID.
How do I find which jobs a timestamp key dropped?
Count conflicts. Log or count every ON CONFLICT DO NOTHING that inserted zero rows, with the key, and compare the conflicting row's payload with the new one. Real duplicates match; collisions do not.
Is a seconds-based key ever acceptable?
Yes, when the system enforces spacing longer than a second between intents for the same subject, including when work is retried or triggered manually. Write that assumption next to the key builder, so the next fast path or test does not break it silently.