Skip to main content
Databases & applications

PostgreSQL Query

Run your own SELECT query on a PostgreSQL server and get alerted on its row count or execution time.

The PostgreSQL Query check runs your own SQL query on a PostgreSQL server and evaluates either the number of rows returned or the execution time. Use it, for example, to watch for failed jobs, stuck orders or slow queries. The check runs on the agent device you assign it to and connects from there over TCP to the host you enter. It runs on Windows, Linux and macOS (each in x64 and Arm64), every 5 minutes by default. The database server itself does not need to be an agent device; any agent device that can reach it over the network is enough.

Prerequisites

  • A login role and a host line in pg_hba.conf that admits this role from the agent device's address.
  • CONNECT on the database, USAGE on the schema and SELECT on the tables your query reads.

To create the monitoring role, its read rights and the pg_hba.conf entry, see Set up the database monitoring account.

Steps

  1. Go to Devices and open the device.
  2. Click the Checks tab → Add Check → PostgreSQL Query.

Settings

FieldTypeRequiredDefaultDescription
HostTextYes—Hostname or IP address of the PostgreSQL server, as seen from the agent device
PortIntegerNo5432TCP port of the server. The check replaces values outside 1–65535 with 5432
DatabaseTextNopostgresDatabase the query runs in. An empty field is treated as postgres
UsernameTextYes—Role the check signs in with
PasswordTextNo—Password of the role
SSL modeSelectNoPreferPrefer encrypts when the server offers it; Require insists on an encrypted connection; Disable connects unencrypted
Query (read-only SELECT)TextYes—The statement to run. It runs inside a READ ONLY transaction that is rolled back afterwards
Alert onSelectNoRow countRow count evaluates the number of rows returned, Execution time (ms) the query duration in milliseconds
Alert when below thresholdYes/NoNoNoReverses the comparison: alert when the value reaches or falls below the thresholds
Warning thresholdNumberNo1Value at which a warning is reported
Error thresholdNumberNo1Value at which a critical result is reported

The check counts rows, not values. SELECT count(*) FROM jobs WHERE failed always returns exactly one row and is therefore always evaluated as 1, no matter how many jobs failed. Write the query so that it returns one row per problem, for example SELECT id FROM jobs WHERE failed.

Results

The check compares the chosen value (row count or execution time) with both thresholds. Thresholds also trigger on equality.

SituationResult
Value at or above Error threshold (with Alert when below threshold: at or below)Critical
Value at or above Warning threshold (with Alert when below threshold: at or below)Warning
OtherwiseOK
Sign-in rejected, server unreachable, query error or timeoutCritical
No host or no query enteredWarning
Unexpected error in the checkFailure

With the defaults (both thresholds 1, mode Row count), the check is therefore Critical as soon as the query returns at least one row, and OK otherwise. If the two thresholds are in the wrong order, the check swaps them before evaluation. Connection setup and query have a fixed 15-second timeout.

Metrics collected

  • Number of rows returned
  • Query execution time in milliseconds (excluding connection setup)

Notes

  • The READ ONLY transaction is a safety net, not a replacement for a read-only role. Grant the monitoring role SELECT only.
  • The host is part of the check configuration. If you assign the check to many devices through a config package, each of them runs the same query against the same server. Assign it to a single device instead.