Delayed messages
A delayed message is committed to its subscribed queues when it is published, with an earliest delivery time. Consumers cannot claim it before that time. The publisher does not need to stay connected or publish it again when the time arrives.
The delivery time is an eligibility boundary, not an appointment: processing can begin later because there are no available consumers, an earlier FIFO message is blocking, or the database is unavailable.
Publish for later delivery
Create the topic and subscriptions using Publishing messages. The following examples make a message eligible one minute after publication. Go uses encoding/json and time; the connection variables come from the Quick start.
- Go
- Rust
- TypeScript
- SQL
id, err := conn.Publish(ctx, "events", json.RawMessage(`{"number":1}`),
postgremq.WithDeliverAfter(time.Now().Add(time.Minute)))
if err != nil {
return err
}
fmt.Printf("Scheduled message %d\n", id)
let id = conn.publish(
"events", &postgremq::serde_json::json!({"number": 1}),
postgremq::PublishOptions::default().deliver_after(
std::time::SystemTime::now() + std::time::Duration::from_secs(60),
),
).await?;
println!("Scheduled message {id}");
const id = await client.publish('events', { number: 1 }, {
deliverAfter: new Date(Date.now() + 60_000),
});
console.log(`Scheduled message ${id}`);
SELECT postgremq.publish_message(
p_topic => 'events',
p_payload => '{"number":1}'::jsonb,
p_deliver_after => clock_timestamp() + interval '1 minute'
);
The SDK examples calculate an absolute timestamp from the publisher's clock; SQL above uses the database's clock. Eligibility is checked against the database clock. Keep the clocks synchronized so that a requested delay has the intended meaning.
What successful publication guarantees
The normal publication guarantees apply. On commit, the payload and one delivery for each selected live subscription are stored atomically. All those deliveries start with the same earliest delivery time. There is no later redistribution to queues when the delay elapses.
| Event | Result |
|---|---|
| Publication commits, then the publisher disconnects | The scheduled deliveries remain stored. |
| A new queue subscribes before the delivery time but after publication | It does not receive this message. |
| A Durable queue has no consumers at the delivery time | The message waits until a consumer can claim it. |
| An Exclusive queue expires before the delivery time | Its subscription is lost; it cannot deliver the message later. |
| A queue is deleted, or its topic’s messages are explicitly cleaned up, before delivery | The publication guarantee does not preserve the deleted subscription's delivery. |
| Publication occurs inside an uncommitted transaction | No consumer can receive it until the transaction commits. Rollback removes it. |
If the delivery time passes while the transaction is still open, commit makes the message eligible immediately, subject to ordering and consumer availability. A timestamp already in the past also makes it immediately eligible after commit.
A delayed message can appear in queue inspection and pending counts before it is eligible for consumption. Pending does not necessarily mean ready now.
Interaction with FIFO delivery
For messages in the same group, commit order takes precedence over delivery time. If message 1 is delayed until tomorrow and message 2 in the same group is eligible now, message 2 waits for message 1 to complete, be removed, or be dead-lettered. It cannot bypass message 1 merely because its delivery time is earlier.
Different groups progress independently. Use separate group keys when messages should not block one another. Changing the delivery time does not create ordering for ungrouped messages; see group keys.
Redelivery and recovery
The initial delay applies to the first opportunity to claim the message. After a consumer claims it, visibility timeout, acknowledgement, and retry rules control the delivery. A failed attempt does not automatically repeat the original publication delay. Choose a retry delay when negatively acknowledging if the next attempt should wait.
An unavailable database or absent consumers can postpone delivery beyond the requested time. Once service resumes, stored messages become claimable when their time and ordering conditions permit; a missed notification does not remove the stored delivery.
A connection failure during publication can leave the commit outcome unknown, just as for immediate publication. Retrying may create another scheduled message. Use stable application event IDs and idempotent consumers when duplicates must not repeat effects.