Managing topics and queues
Topics and queues are named database resources. Repeating a topic declaration preserves it. An identical queue declaration preserves a Durable queue or a live Exclusive queue; for a live Exclusive queue it also renews the lifetime. An expired Exclusive queue rejects redeclaration with PMQ02 until it is deleted. Redeclaring a queue with changed settings fails. Deleting and recreating a queue replaces its subscription and discards its delivery history.
The CLI manages schema installation and upgrades. Use SQL for topology inspection and changes, with a login authorized to operate on the affected data.
Inspect before changingโ
Run these queries in psql connected to the intended database:
SELECT * FROM postgremq.list_topics();
SELECT * FROM postgremq.list_queues();
SELECT * FROM postgremq.get_queue_statistics('jobs');
SELECT * FROM postgremq.list_messages('jobs');
SELECT * FROM postgremq.list_dlq_messages() WHERE queue_name = 'jobs';
Queue listing includes expired Exclusive queues awaiting physical cleanup. Raw statistics include pending, processing, and completed delivery rows but exclude dead letters. Inspect both before deciding a queue has no remaining work.
For an ordinary declaration:
SELECT postgremq.create_topic('events');
SELECT postgremq.create_queue('jobs', 'events', 5, false);
These settings select a Durable queue with five allowed attempts. A changed topic, attempt limit, or lifetime is not an in-place update. See Durable queues and Exclusive queues for equivalent SDK declarations.
Replace a queue deliberatelyโ
If a declaration must change, choose between a new queue name and replacement of the existing name. A new name permits explicit consumer rollout but starts a new subscription: it receives only subsequent publications. During overlap, both queues receive their own deliveries.
For same-name replacement, stop the affected consumers, decide how to handle their backlog and DLQ entries, and coordinate publishers if a missed-subscription interval is unacceptable. Deleting the queue removes its delivery rows, including in-flight work. Creating a replacement does not recover them. Existing consumers do not silently adopt the new generation; start new consumers after declaration.
Use Dead-letter recovery before deleting a queue with DLQ entries. Do not globally purge dead letters merely to make one queue deletion succeed.
Understand destructive scopeโ
| Operation | Deletes or changes | What remains |
|---|---|---|
delete_queue_message(queue, id) | One queue delivery, in any state. | Other queues' deliveries and payload; any separate DLQ entry. |
clean_up_queue(queue) | All delivery rows in one queue, including in-flight work. | Queue identity, payloads, and DLQ entries. |
delete_queue(queue) | Queue identity and delivery rows; refused while it has DLQ entries. | Other subscriptions and unreferenced payloads awaiting collection. |
clean_up_topic(topic) | Topic payloads and their delivery rows; refused while its messages have DLQ references. | Topic and its queues. |
delete_topic(topic) | Topic and its queues; the snapshot check refuses visible payloads. Stop and drain publishers first to prevent concurrent publications being cascade-deleted. | Other topics. |
purge_dlq() | Every DLQ entry across all queues. | Payloads until unreferenced collection. |
purge_all_messages() | Every payload, delivery, and DLQ entry. | Topic and queue declarations. |
These are administrative deletion operations. They do not wait for business processing to finish and do not undo effects already performed by consumers. Removing an unsettled FIFO head can permit its successor to proceed.
For example, after confirming that all work in jobs is intentionally being discarded and stopping its consumers:
SELECT postgremq.clean_up_queue('jobs');
SELECT * FROM postgremq.get_queue_statistics('jobs');
SELECT * FROM postgremq.list_dlq_messages() WHERE queue_name = 'jobs';
The statistics should be zero unless concurrent publication has added new work. Dead letters are a separate check. Coordinate publishers when the expected postcondition requires an empty queue.
Use retention maintenance for ordinary completed-history collection. A purge is never required just because completed rows remain visible.