Exclusive queues
An Exclusive queue provides a subscription whose lifetime is tied to the application using it. The SDK maintains the queue while the declaring connection is active. If that connection can no longer maintain it, the queue expires one configured lifetime window after its last successful renewal. The remaining window can therefore be shorter when renewal stops.
Use Exclusive queues for publish/subscribe when a subscriber needs events during its session and can start with new events when it returns. Use a Durable queue when the subscriber must retain its backlog across downtime.
Publish to connected subscribers
Give each independent subscriber its own Exclusive queue on the same topic:
→ session-a queue → subscriber A
Topic → each message
→ session-b queue → subscriber B
While both queues are live, a publication creates a delivery in each. Each subscriber acknowledges its own delivery. Creating a new session queue starts a new subscription; it does not replay events published before that queue existed.
PostgreMQ's exclusive option controls queue lifetime. It does not restrict the queue to a single connection or consumer. If two consumers use the same queue, they share work. Use different queue names when both must receive every publication.
Exclusive queues still store messages in PostgreSQL and require acknowledgements. They can buffer messages and redeliver failed attempts while live. The lifetime setting determines how long that stored subscription remains available.
Create an Exclusive queue
These snippets use the SDK 0.4.0 connections from the Quick start, with SQL 0.1.1 installed. Each example creates the events topic before declaring the queue.
Choose a valid, unique queueName/queue_name in your application, such as session- followed by a UUID. Queue names accept ASCII letters, digits, _, :, ., and -, up to 57 bytes.
- Go
- Rust
- TypeScript
- SQL
if err := conn.CreateTopic(ctx, "events"); err != nil { return err }
if err := conn.CreateQueue(ctx, queueName, "events", true,
postgremq.WithMaxDeliveryAttempts(5),
postgremq.WithKeepAliveInterval(60*time.Second)); err != nil {
return err
}
consumer, err := conn.Consume(queueName, postgremq.WithBatchSize(1))
if err != nil {
return err
}
conn.create_topic("events").await?;
conn.create_queue(
&queue_name, "events",
postgremq::QueueOptions::default()
.exclusive(true)
.max_delivery_attempts(5)
.keep_alive_interval(std::time::Duration::from_secs(60)),
).await?;
let mut consumer = conn.consume(
&queue_name, postgremq::ConsumeOptions::default(),
).await?;
await client.createTopic('events');
await client.createQueue(queueName, 'events', true, {
maxDeliveryAttempts: 5,
keepAliveInterval: 60, // Seconds.
});
const consumer = client.consume(queueName, { batchSize: 1 });
SELECT postgremq.create_topic('events');
SELECT postgremq.create_queue('session-sql-demo', 'events', 5, true, interval '60 seconds');
Choose a distinct name for each SQL subscriber too. This SQL declares a queue but does not maintain it automatically; a raw SQL client must implement lifetime renewal. The SDKs manage that responsibility for their declaring connections.
Receive and acknowledge deliveries using the loop from the Quick start; keep receiving instead of stopping after the first message. In TypeScript, consumption begins when you call consumer.messages(); iteration reads the claimed deliveries. Create the subscription before the events you want to see are published.
The SDK maintains the subscription lifetime automatically through the connection that declared it. Keep that connection open while the subscriber is active. The lifetime option allows a window for interruptions; it is not a message-processing timeout, and receiving messages through another connection does not by itself maintain the subscription.
With the 60-second setting above, expiry occurs 60 seconds after the last successful lifetime extension. Closing the connection does not promise an immediate deletion or a new full 60-second window. On expiry, the subscription stops receiving new publications and its consumers can no longer continue. Physical cleanup happens through scheduled maintenance.
Disconnects and expiry
The connection that declared the queue manages its lifetime, independently of whether a consumer is currently receiving. Stopping a consumer while keeping that connection open does not by itself expire the queue.
| Event | Effect on the subscription |
|---|---|
| Consumer temporarily stops; the declaring connection still maintains the queue | New publications continue to accumulate in the queue. |
| Declaring connection closes or loses contact | The queue remains live until its last confirmed expiry time. Immediate deletion is not guaranteed. |
| Queue expires | New publications no longer reach it, and consumers cannot fetch its backlog. Reconnecting does not revive it. |
| Application creates a new queue | The new subscription receives subsequent publications. It does not recover the expired queue's backlog or events missed between subscriptions. |
A short lifetime window releases abandoned subscriptions sooner, but also makes brief disruptions more likely to lose the subscription. Choose it according to how much interruption the application should tolerate. It is separate from a delivery's visibility timeout, which controls recovery of unacknowledged work.
Handle a lost subscription
Observe consumer termination and the connection's queue-loss callback:
| Language | Connection option | Consumer signal |
|---|---|---|
| Go | WithQueueFatalHandler | NotifyClose and the closed message channel |
| Rust | ConnectionOptions::on_queue_fatal | An error followed by the end of the stream; handler consumers expose closed() |
| TypeScript | onQueueFatal | onClose and the end of the iterator |
After an Exclusive queue expires, create a new subscription with a new name and receive future events. Events published while no live subscription existed are not caught up. If the application must recover everything after downtime, use a Durable queue.
Deleting and recreating a queue with the same name also replaces the subscription. Existing consumers do not silently switch to the replacement; start new consumers deliberately. An expired Exclusive queue cannot be revived by redeclaring it.
Delivery and cleanup
While the queue is live, acknowledgements, retries, and group ordering follow the same rules as for Durable queues. A failed or timed-out attempt can be redelivered, so subscribers must tolerate duplicates. A group key enables FIFO delivery within that group; Exclusive queues do not order ungrouped messages strictly.
Expiry ends access to the subscription even if messages remain unprocessed. Physical cleanup runs later through scheduled maintenance. Dead-letter entries can prevent physical deletion until an operator resolves them; queue expiry should not be treated as a way to discard dead letters automatically.
Use Graceful shutdown to finish or return active deliveries when stopping the application. Closing the connection and waiting for handlers does not change the queue's expiry rule.