PgQue icon indicating copy to clipboard operation
PgQue copied to clipboard

fix(idempotency): base dedup TTL on send time, not transaction start

Open NikolayS opened this issue 4 months ago • 1 comments

Severity

Medium-high — producer idempotency correctness.

Problem

pgque.send_idem() uses transaction-stable now() for both the claim expiry and conflict predicate. In a transaction older than the requested TTL, a newly created claim can already be expired when it commits, so an immediate retry inserts a duplicate event.

Reproduction

begin;
select pg_sleep(1.5);

select event_id, deduped
from pgque.send_idem('q', 'job', '{}', 'same', '1 second');
-- event_id=1, deduped=false, but expires_at is already in the past
commit;

select event_id, deduped
from pgque.send_idem('q', 'job', '{}', 'same', '1 second');
-- event_id=2, deduped=false immediately

Affected implementation: devel/sql/pgque-api/send_idem.sql.

PostgreSQL 19 also accepts interval 'infinity'; the current positive-only check permits a permanently live dedup row, contradicting bounded-state/GC expectations.

Expected fix

Capture wall-clock time for each call (using clock_timestamp()) and use it consistently for claim qualification and expiry. Ensure a contender that waits on a conflicting row receives a fresh expiry when it wins. Reject non-finite TTLs.

Add regression coverage for:

  • transaction age greater than TTL before the first send;
  • immediate retry after commit still deduplicates;
  • non-finite TTL rejection on versions that support it;
  • existing concurrent single-insert guarantee.

NikolayS avatar Jul 10 '26 09:07 NikolayS

Shipped in v0.3.0-rc.2 (merged to main via #395 at f4c7457): https://github.com/NikolayS/PgQue/releases/tag/v0.3.0-rc.2

NikolayS avatar Nov 21 '26 16:11 NikolayS