Skip to main content

Monitoring and alerting

Monitor database queue state, application processing, and PostgreSQL health together. Queue counts establish what is retained; client metrics explain attempted operations and handler activity. Neither alone establishes that useful work is completing on time.

Run one active SQL scraper per database. Each application replica can export its own client metrics, with a distinct service instance identity.

Inspect queue state​

Use a metrics-only role:

SELECT * FROM postgremq.queue_metrics();

The result includes every queue, even empty queues and expired Exclusive queues. active distinguishes a live queue from an expired subscription. Expired queues have zero ready and oldest_ready_age_seconds, while other state counts can remain nonzero. ready, delayed, processing, exhausted, and dead_letter describe delivery state; completed-history retention is excluded.

ready is not a promise that every counted row can be claimed immediately. It includes FIFO successors blocked behind a group head and rows that another transaction currently locks. oldest_ready_age_seconds measures time since the current visibility deadline, not age since original publication. A delayed retry changes that deadline.

Use get_queue_statistics() when investigating raw pending, processing, and completed row counts. Those counts answer a different question and exclude DLQ entries.

Configure collection​

The supplied Collector configuration uses the OpenTelemetry SQL Query receiver and separate pipelines for SQL and client metrics. It is a complete starting configuration, not a requirement to adopt a particular telemetry backend.

Supply POSTGREMQ_METRICS_DSN for the restricted login and POSTGREMQ_DATABASE_ID for a stable identity unique among the databases sharing the telemetry destination. Set connection and statement deadlines in that DSN, such as connect_timeout=5&statement_timeout=5000 for a URI connection string (seconds and milliseconds, respectively). The Collector configuration alone does not set them. Configure your database TLS settings and pin the Collector version. The source example uses Collector Contrib 0.147.0; rehearse configuration changes with the version deployed.

Preserve each application's own service.name and service.instance.id. Applying the SQL database's identity to every application replica obscures where failures occur. Avoid duplicate SQL scrapers reporting the same gauges.

The query reads delivery and DLQ state without reading payloads. Its cost still grows with retained backlog. Measure scrape duration under a representative backlog before choosing a high collection frequency.

Build dashboards around progress​

QuestionEvidence
Is work waiting too long?Ready age, ready depth, delayed work, and application completion latency.
Are workers active?Handler activity, consume/settlement durations, and connection errors.
Is the retry policy recovering?Redelivered attempts, DLQ growth, exhausted rows, and application errors.
Is maintenance keeping up?Job success time, returned counts, exhausted rows, retained table size.
Is the database constrained?Pool waits, server connections, locks, CPU/I/O, WAL, and vacuum activity.

Application instrumentation is opt-in; Application metrics and logging shows setup. Stream consumers need their own processing duration measurement because the library cannot identify that application's work boundary.

Choose actionable alerts​

Alert on sustained ready age above the queue's processing objective, growing dead letters, exhausted rows persisting beyond the maintenance interval, and missing maintenance successes. Investigate increasing renewal loss alongside pool and database latency. Alert when an expected Durable queue disappears; expected Exclusive expiry often needs a different policy.

Use thresholds derived from the workload and retry schedule. A single delayed message, a briefly nonzero retry count, or an intentionally idle worker need not be an incident. Include the database and queue identity, observed duration, and a link to the matching runbook.

Do not use sent-message counters as committed publication counts. They record attempted queries, including failed and transactional attempts that later roll back. See the Metrics reference for precise recording boundaries.