Maintenance and message retention
Maintenance is part of running PostgreMQ. Installing the schema or connecting an SDK does not start these jobs. Without them, abandoned final attempts can wait outside the DLQ, expired Exclusive queues occupy storage, and completed deliveries accumulate.
Run fast maintenance separately from retention so recovery work does not depend on the lifetime chosen for completed history.
Schedule the required jobs
Use a dedicated scheduler with database access and an owner for failures. Start with these operations:
SELECT * FROM postgremq.pmq_maintenance_fast();
SELECT postgremq.cleanup_completed_messages(24, 1000);
Fast maintenance returns counts named retired_to_dlq and inactive_queues_dropped. It retires expired final attempts and reaps expired Exclusive queues where deletion is permitted. Schedule it every 1–10 seconds for prompt recovery; run it at least once per minute. The interval adds to the wait after an abandoned final attempt expires and can delay the next message in its FIFO group.
Queue expiry itself does not wait for maintenance. An expired queue has already stopped accepting publications and serving consumers. Reaping releases its stored delivery state later; dead-letter entries can prevent that deletion.
Retention returns the number of completed delivery rows deleted, not a combined count of deliveries and payloads. The example keeps completed deliveries for 24 hours after completion and processes at most 1,000 eligible delivery rows per call. It then separately attempts a bounded collection of unreferenced payloads using the same arguments.
Use an external scheduler
A scheduler can invoke this command every five seconds, recording its exit status and returned counts:
psql "$MAINTENANCE_DATABASE_URL" -X -v ON_ERROR_STOP=1 -c 'SELECT * FROM postgremq.pmq_maintenance_fast();'
Invoke retention every 10–60 seconds according to workload:
psql "$MAINTENANCE_DATABASE_URL" -X -v ON_ERROR_STOP=1 -c 'SELECT postgremq.cleanup_completed_messages(24, 1000);'
Configure non-overlapping runs and a database query deadline appropriate to observed execution time. Persist failures and retry the next scheduled run; these jobs do not need to be considered completed when a connection response is lost. Track last successful execution separately from the deletion count: returning zero can be a healthy result.
If the deployment already uses pg_cron with second-level scheduling support, the equivalent jobs are:
SELECT cron.schedule('postgremq-maintenance', '5 seconds',
'SELECT postgremq.pmq_maintenance_fast()');
SELECT cron.schedule('postgremq-retention', '10 seconds',
'SELECT postgremq.cleanup_completed_messages(24, 1000)');
These require pg_cron 1.5 or later and installation/configuration for the intended database. If its scheduler database differs, use the extension's documented schedule_in_database facility. PostgreMQ does not install pg_cron. Inspect job execution records and test the job role, database, and schedule before relying on it.
Retention is not a message TTL
Completed-delivery retention uses completion time. Payload collection uses publication time and requires that no delivery or DLQ entry references the payload. A slow queue, pending retry, or retained dead letter can therefore keep a payload indefinitely. Setting retention to zero does not expire pending work.
Call cleanup_unreferenced_messages(hours, batch) separately only when a different payload-collection budget is needed. Cleanup already invokes it. Group metadata pruning is handled by cleanup; it does not require another regular scheduler.
Keep non-null positive batch limits. The cleanup functions skip locked candidate rows; they do not establish a hard wall-clock deadline or eliminate every possible database lock wait. Fast maintenance is not governed by the retention batch size.
Keep collection ahead of accumulation
At 10 publications per second and five queues per topic, fully consumed traffic produces about 3,000 completed delivery rows per minute. A 1,000-row batch every 10 seconds offers up to 6,000 deletions per minute if calls finish on schedule and rows are eligible and unlocked. This is a sizing calculation, not a throughput promise.
Measure returned counts, old completed rows, DLQ size, table growth, and vacuum activity. Repeatedly full batches indicate that more eligible work may remain. Increase capacity while observing database load. Use Maintenance troubleshooting for errors and Performance for storage and vacuum behavior.