From 4234fc6a837d98aadc3176e4863d1f6c9f41029f Mon Sep 17 00:00:00 2001 From: max Date: Mon, 9 Nov 2026 13:39:11 +0200 Subject: [PATCH 1/3] Pgmq: notify consumers from pgmq_dispatch_v1() instead of the insert trigger --- src/MessageBus/Pgmq/PgmqTransport.php | 53 ++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/src/MessageBus/Pgmq/PgmqTransport.php b/src/MessageBus/Pgmq/PgmqTransport.php index 66bdca6..717c963 100644 --- a/src/MessageBus/Pgmq/PgmqTransport.php +++ b/src/MessageBus/Pgmq/PgmqTransport.php @@ -60,7 +60,41 @@ public function createQueue(string $name): void Pgmq\createExtension($this->postgres); Pgmq\createQueue($this->postgres, $name); + // Consumers are woken up by pgmq_dispatch_v1() itself (see below), not by pgmq's insert trigger. The trigger + // updates the queue's row in pgmq.notify_insert_throttle on every insert, and that row lock is held until the + // sender commits: concurrent senders to one queue wait for each other, and two transactions sending to two + // queues in opposite order deadlock. Drop it if an earlier version set it up. The check reads the catalog only; + // the DDL (and its table lock) runs once per queue, under the lock pgmq takes to create and drop queues. + $this->postgres->query(\sprintf( + <<<'SQL' + DO $disable$ + BEGIN + PERFORM pgmq.acquire_queue_lock(%1$s); + + IF EXISTS ( + SELECT 1 + FROM pg_trigger tg + JOIN pg_class c ON c.oid = tg.tgrelid + JOIN pg_namespace n ON n.oid = c.relnamespace + WHERE n.nspname = 'pgmq' + AND c.relname = pgmq.format_table_name(%1$s, 'q') + AND tg.tgname = 'trigger_notify_queue_insert_listeners' + ) OR EXISTS ( + SELECT 1 FROM pgmq.notify_insert_throttle t WHERE t.queue_name = %1$s + ) THEN + PERFORM pgmq.disable_notify_insert(%1$s); + END IF; + END + $disable$ + SQL, + $this->postgres->quoteLiteral($name), + )); + $this->postgres->query('create schema if not exists thesis_message_bus;'); + // Wakes consumers with pg_notify() on the channel pgmq's insert trigger used, so listeners need no change. + // No row is locked: a notification is delivered on commit (never for a rolled-back send), and identical + // notifications of one transaction are folded into one — a natural throttle per queue and transaction. + // Delayed messages are not announced: they are invisible until the delay passes, and the poll picks them up. $this->postgres->query( <<<'SQL' create or replace function thesis_message_bus.pgmq_dispatch_v1( @@ -75,12 +109,28 @@ public function createQueue(string $name): void as $$ declare index integer; + target text; begin for index in 1..coalesce(array_length(operations, 1), 0) loop if operations[index] = 'send' then return query select id from pgmq.send(addresses[index], payloads[index], headers[index], delays[index]) as id; + + if delays[index] <= 0 then + perform pg_notify('pgmq.' || pgmq.format_table_name(addresses[index], 'q') || '.INSERT', null); + end if; elsif operations[index] = 'publish' then return next pgmq.send_topic(addresses[index], payloads[index], headers[index], delays[index])::bigint; + + if delays[index] <= 0 then + -- The same queues pgmq.send_topic() has just sent to. + for target in + select distinct tb.queue_name + from pgmq.topic_bindings tb + where addresses[index] ~ tb.compiled_regex + loop + perform pg_notify('pgmq.' || pgmq.format_table_name(target, 'q') || '.INSERT', null); + end loop; + end if; else raise exception 'unknown message bus operation: %', operations[index]; end if; @@ -189,7 +239,8 @@ public function startConsumer(string $queue, ConsumerHandler $handler): PgmqCons $timeoutWatcher, new ChannelWatcher( $polls, - $this->postgres->listen($queue->enableNotifyInsert()), + // pgmq_dispatch_v1() notifies this channel; see createQueue() for why the insert trigger is not used. + $this->postgres->listen(Pgmq\channelName($queue->name)), $timeoutWatcher, ), ]); From beedc4e2b6a913fc63e17436b3f674f05c231194 Mon Sep 17 00:00:00 2001 From: max Date: Mon, 9 Nov 2026 14:09:12 +0200 Subject: [PATCH 2/3] Pgmq: undo an earlier notify-trigger setup without stalling a busy queue; listen on the lower-case channel --- src/MessageBus/Pgmq/PgmqTransport.php | 43 ++++++++++++++++++++------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/src/MessageBus/Pgmq/PgmqTransport.php b/src/MessageBus/Pgmq/PgmqTransport.php index 717c963..49a6fdd 100644 --- a/src/MessageBus/Pgmq/PgmqTransport.php +++ b/src/MessageBus/Pgmq/PgmqTransport.php @@ -63,13 +63,29 @@ public function createQueue(string $name): void // Consumers are woken up by pgmq_dispatch_v1() itself (see below), not by pgmq's insert trigger. The trigger // updates the queue's row in pgmq.notify_insert_throttle on every insert, and that row lock is held until the // sender commits: concurrent senders to one queue wait for each other, and two transactions sending to two - // queues in opposite order deadlock. Drop it if an earlier version set it up. The check reads the catalog only; - // the DDL (and its table lock) runs once per queue, under the lock pgmq takes to create and drop queues. + // queues in opposite order deadlock. Undo an earlier setup, without stalling a live queue: + // + // * the throttle row goes first: without it the trigger updates nothing and locks nothing. Deleting it waits + // only for senders holding that row, not for the whole table; + // * the trigger itself is dropped with a short lock_timeout: DROP TRIGGER takes an ACCESS EXCLUSIVE lock that + // waits for every open transaction on the queue and blocks its readers and senders meanwhile. If the queue is + // busy, it is skipped with a warning; the trigger is harmless without its row, and the next setup retries. + // + // Both steps only read the catalog when there is nothing to undo, so repeated setups run no DDL. $this->postgres->query(\sprintf( <<<'SQL' - DO $disable$ + DO $undo$ + DECLARE + v_queue text := %s; + v_lock_timeout text := current_setting('lock_timeout'); BEGIN - PERFORM pgmq.acquire_queue_lock(%1$s); + PERFORM set_config('lock_timeout', '2s', true); + + BEGIN + DELETE FROM pgmq.notify_insert_throttle WHERE queue_name = v_queue; + EXCEPTION WHEN lock_not_available THEN + RAISE WARNING 'pgmq queue "%%" is busy: its insert-notify throttle row is left for the next setup', v_queue; + END; IF EXISTS ( SELECT 1 @@ -77,15 +93,19 @@ public function createQueue(string $name): void JOIN pg_class c ON c.oid = tg.tgrelid JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = 'pgmq' - AND c.relname = pgmq.format_table_name(%1$s, 'q') + AND c.relname = pgmq.format_table_name(v_queue, 'q') AND tg.tgname = 'trigger_notify_queue_insert_listeners' - ) OR EXISTS ( - SELECT 1 FROM pgmq.notify_insert_throttle t WHERE t.queue_name = %1$s ) THEN - PERFORM pgmq.disable_notify_insert(%1$s); + BEGIN + PERFORM pgmq.disable_notify_insert(v_queue); + EXCEPTION WHEN lock_not_available THEN + RAISE WARNING 'pgmq queue "%%" is busy: its insert-notify trigger is left for the next setup', v_queue; + END; END IF; + + PERFORM set_config('lock_timeout', v_lock_timeout, true); END - $disable$ + $undo$ SQL, $this->postgres->quoteLiteral($name), )); @@ -239,8 +259,9 @@ public function startConsumer(string $queue, ConsumerHandler $handler): PgmqCons $timeoutWatcher, new ChannelWatcher( $polls, - // pgmq_dispatch_v1() notifies this channel; see createQueue() for why the insert trigger is not used. - $this->postgres->listen(Pgmq\channelName($queue->name)), + // pgmq_dispatch_v1() notifies this channel; see createQueue() for why the insert trigger is not used. Lower case: + // the SQL side names the channel after the queue table, and pgmq lower-cases table names. + $this->postgres->listen(Pgmq\channelName(strtolower($queue->name))), $timeoutWatcher, ), ]); From 0763081fb19ad0976813947f492a879e14bb6a53 Mon Sep 17 00:00:00 2001 From: max Date: Wed, 7 Oct 2026 12:42:21 +0300 Subject: [PATCH 3/3] Remove the @phpstan-ignore that no longer matches 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). --- src/MessageBus/Handling/Handlers.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/MessageBus/Handling/Handlers.php b/src/MessageBus/Handling/Handlers.php index b55ba5f..9aacd48 100644 --- a/src/MessageBus/Handling/Handlers.php +++ b/src/MessageBus/Handling/Handlers.php @@ -54,7 +54,6 @@ public function with(string $messageClass, callable $handler, string $qualifier } $copy = clone $this; - /** @phpstan-ignore assign.propertyType */ $copy->handlers[$messageClass][$qualifier] = $handler; return $copy;