Skip to content

Pgmq: notify consumers from pgmq_dispatch_v1() instead of pgmq's insert trigger - #9

Open
iGrog wants to merge 3 commits into
thesis-php:0.5.xfrom
iGrog:fix/pgmq-notify-without-throttle-row-lock
Open

iGrog wants to merge 3 commits into
thesis-php:0.5.xfrom
iGrog:fix/pgmq-notify-without-throttle-row-lock

Conversation

@iGrog

@iGrog iGrog commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Closes #4. Closes #3.

pgmq's insert trigger updates the queue's row in pgmq.notify_insert_throttle on every insert, and that row stays locked until the sender commits. Senders to one queue wait for each other, and two transactions sending to two queues in opposite order deadlock (details and a reproduction in #4).

This PR stops relying on the trigger:

  • pgmq_dispatch_v1() calls pg_notify() itself, on the channel the trigger used (pgmq.q_<queue>.INSERT), so consumers listen exactly as before. For publish it notifies every queue send_topic() routed the message to. Postgres delivers the notification on commit, never for a rolled-back send, and folds identical notifications within one transaction.
  • Delayed messages aren't announced: they're invisible until the delay passes, and the poll picks them up.
  • createQueue() undoes an earlier setup without stalling a live queue: it deletes the throttle row first (without it the trigger locks nothing), then drops the trigger with a 2 s lock_timeout. A busy queue keeps its trigger until the next setup, with a warning. With nothing to undo, it only reads the catalog.
  • The consumer no longer touches the trigger on start (PgmqTransport: every consumer start recreates the notify trigger (table lock, blocked producers, deadlocks) #3) and listens on the lower-cased channel name, matching the table name pgmq uses in the channel.

Trade-offs:

  • A message inserted with plain pgmq.send(), bypassing the bus, no longer wakes the consumer; it's picked up by the next poll.
  • Postgres serializes commits that issued a NOTIFY across the cluster, so the ceiling is roughly one sending transaction per WAL fsync. The old trigger hits the same ceiling; with 32 pgbench clients p95/p99 commit latency is 2.5–4× lower than with the trigger.

Replaces the trigger handling in #7 and doesn't need a new thesis/pgmq release. Integration tests come in a separate PR on top of #8.

Recent PHPStan 2.2.x infers the nested property assignment correctly, so the ignore
is unmatched and phpstan fails on every cell of the CI matrix (8.4/8.5 x lowest/highest).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant