Files
portal/app/database
Дмитрий ba3dbbd9be fix(audit): add pg_advisory_xact_lock to audit_chain_hash trigger
Closes race condition where concurrent INSERT workers (webhook handlers)
all read the same prev_hash before any commits, causing hash chain to
branch. Advisory lock key is derived from the partition OID (TG_RELID):
  lock_key := ('x' || lpad(to_hex(TG_RELID::int), 16, '0'))::bit(64)::bigint

This serializes INSERTs to the SAME partition without blocking concurrent
INSERTs to DIFFERENT partitions (distinct OIDs → distinct lock keys).

Hash formula: verbatim unchanged from db/schema.sql:3107-3127:
  digest(COALESCE(prev_hash, ''::bytea) || NEW::text::bytea, 'sha256')

Tested: pg_locks advisory lock presence test passes (pg_advisory_xact_lock
visible in pg_locks during INSERT transaction).

Refs: docs/superpowers/plans/2026-05-29-audit-chain-race-fix.md Task 2

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-29 09:20:56 +03:00
..