
# PostgreSQL by ODBC

## Overview

This template is designed for the effortless deployment of PostgreSQL monitoring by Zabbix via ODBC and doesn't require any external scripts.

## Requirements

Zabbix version: 7.0 and higher.

## Tested versions

This template has been tested on:
- PostgreSQL 11-17

## Configuration

> Zabbix should be configured according to the instructions in the [Templates out of the box](https://www.zabbix.com/documentation/7.0/manual/config/templates_out_of_the_box) section.

## Setup

1. Create the PostgreSQL user for monitoring (`<password>` at your discretion) and inherit permissions from the default role `pg_monitor`:

```sql
CREATE USER zbx_monitor WITH PASSWORD '<PASSWORD>' INHERIT;
GRANT pg_monitor TO zbx_monitor;
```

2. Edit the `pg_hba.conf` configuration file to allow TCP connections for the user `zbx_monitor`. For example, you could add one of the following rows to allow local connections from the same host:

```bash
# TYPE  DATABASE        USER            ADDRESS                 METHOD
  host       all        zbx_monitor     localhost               trust
  host       all        zbx_monitor     127.0.0.1/32            md5
  host       all        zbx_monitor     ::1/128                 scram-sha-256
```

For more information please read the PostgreSQL documentation `https://www.postgresql.org/docs/current/auth-pg-hba-conf.html`.

3. Install the PostgreSQL ODBC driver. Check the [`Zabbix documentation`](https://www.zabbix.com/documentation/7.0/manual/config/items/itemtypes/odbc_checks/) for details about ODBC checks and [`recommended parameters page`](https://www.zabbix.com/documentation/7.0/manual/config/items/itemtypes/odbc_checks/unixodbc_postgresql).

4. Set up the connection string with the `{$PG.CONNSTRING.ODBC}` macro. The minimum required parameters are:

- `Driver=` - set the name of the driver which will be used for monitoring (from the `odbcinst.ini` file) or specify the path to the driver file (for example `/usr/lib64/psqlodbcw.so`);
- `Servername=` - set the host name or IP address of the PostgreSQL instance;
- `Port=` - adjust the port number if needed.

**Note:** if you want to use SSL/TLS encryption to protect communications with the remote PostgreSQL instance, you can also specify encryption parameters here.

It is assumed that you set up the PostgreSQL instance to work in the desired encryption mode. Check the [`PostgreSQL documentation`](https://www.postgresql.org/docs/current/ssl-tcp.html) for details.

For example, to enable required encryption in transport mode without identity checks, the connection string could look like this (replace `<instanceip>` with the address of the PostgreSQL instance):

```
Servername=<instanceip>;Port=5432;Driver=/usr/lib64/psqlodbcw.so;SSLmode=require
```

5. Set the password that you specified in step 1 in the macro `{$PG.PASSWORD}`.

### Macros used

|Name|Description|Default|
|----|-----------|-------|
|{$PG.PASSWORD}|
PostgreSQL user password.
|`<Put the password here>`|
|{$PG.USER}|
PostgreSQL username.
|`zbx_monitor`|
|{$PG.CONNSTRING.ODBC}|
Connection string for the PostgreSQL instance.
|`Macro too long. Please see the template.`|
|{$PG.LLD.FILTER.DBNAME}|
Filter of discoverable databases.
|`.+`|
|{$PG.CONN_TOTAL_PCT.MAX.WARN}|
Maximum percentage of current connections for trigger expression.
|`90`|
|{$PG.DATABASE}|
Default PostgreSQL database for the connection.
|`postgres`|
|{$PG.DEADLOCKS.MAX.WARN}|
Maximum number of detected deadlocks for trigger expression.
|`0`|
|{$PG.LLD.FILTER.APPLICATION}|
Filter of discoverable applications.
|`.+`|
|{$PG.CONFLICTS.MAX.WARN}|
Maximum number of recovery conflicts for trigger expression.
|`0`|
|{$PG.QUERY_ETIME.MAX.WARN}|
Execution time limit for count of slow queries.
|`30`|
|{$PG.SLOW_QUERIES.MAX.WARN}|
Slow queries count threshold for a trigger.
|`5`|

### Items

|Name|Description|Type|Key and additional info|
|----|-----------|----|-----------------------|
|Get archive|
Collect archive status metrics.
|Database monitor|db.odbc.select[pgsql.archive,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]|
|Get dbstat|
Collect all metrics from pg_stat_database per database:

https://www.postgresql.org/docs/current/monitoring-stats.html#PG-STAT-DATABASE-VIEW
|Database monitor|db.odbc.select[pgsql.dbstat,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]|
|Get dbstat sum|
Collect all metrics from pg_stat_database as sums for all databases:

https://www.postgresql.org/docs/current/monitoring-stats.html#PG-STAT-DATABASE-VIEW
|Database monitor|db.odbc.select[pgsql.dbstat.sum,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]|
|Get connections sum|
Collect all metrics from pg_stat_activity:

https://www.postgresql.org/docs/current/monitoring-stats.html#PG-STAT-ACTIVITY-VIEW
|Database monitor|db.odbc.select[pgsql.connections.sum,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]|
|Get WAL|
Collect write-ahead log (WAL) metrics.
|Database monitor|db.odbc.select[pgsql.wal.stat,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]|
|Get locks|
Collect all metrics from pg_locks per database:

https://www.postgresql.org/docs/current/explicit-locking.html#LOCKING-TABLES
|Database monitor|db.odbc.select[pgsql.locks,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]|
|Get replication|
Collect metrics from the pg_stat_replication, which contains information about the WAL sender process, showing statistics about replication to that sender's connected standby server.
|Database monitor|db.odbc.select[pgsql.replication.process,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]
**Preprocessing**

- 
Check for not supported value: `any error`

⛔️Custom on fail: Discard value

|
|Get queries|
Collect all metrics by query execution time.
|Database monitor|db.odbc.select[pgsql.queries,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]|
|Version|
PostgreSQL version.
|Database monitor|db.odbc.select[pgsql.version,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]
**Preprocessing**

- 
Discard unchanged with heartbeat: `1d`

|
|Version (numeric)|
Reports the version number of the server as an integer.
|Database monitor|db.odbc.select[pgsql.version.num,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]
**Preprocessing**

- 
Discard unchanged with heartbeat: `1d`

|
|Bgwriter LLD|
LLD data for "bgwriter" metrics discovery.
|Dependent item|pgsql.bgwriter.lld
**Preprocessing**

- 
JavaScript: `The text is too long. Please see the template.`

|
|WAL: Bytes written|
WAL write, in bytes.
|Dependent item|pgsql.wal.write
**Preprocessing**

- 
JSON Path: `$.write`

- Change per second
|
|WAL: Bytes received|
WAL receive, in bytes.
|Dependent item|pgsql.wal.receive
**Preprocessing**

- 
JSON Path: `$.receive`

- Change per second
|
|WAL: Segments count|
Number of WAL segments.
|Dependent item|pgsql.wal.count
**Preprocessing**

- 
JSON Path: `$.count`

|
|Archive: Count of archived files|
Count of archived files.
|Dependent item|pgsql.archive.count_archived_files
**Preprocessing**

- 
JSON Path: `$.archived_count`

|
|Archive: Count of failed attempts to archive files|
Count of failed attempts to archive files.
|Dependent item|pgsql.archive.failed_trying_to_archive
**Preprocessing**

- 
JSON Path: `$.failed_count`

|
|Archive: Count of files in archive_status need to archive|
Count of files to archive.
|Dependent item|pgsql.archive.count_files_to_archive
**Preprocessing**

- 
JSON Path: `$.count_files`

|
|Archive: Size of files need to archive|
Size of files to archive.
|Dependent item|pgsql.archive.size_files_to_archive
**Preprocessing**

- 
JSON Path: `$.size_files`

|
|Dbstat: Blocks read time|
Time spent reading data file blocks by backends.
|Dependent item|pgsql.dbstat.sum.blk_read_time
**Preprocessing**

- 
JSON Path: `$.blk_read_time`

- 
Custom multiplier: `0.001`

|
|Dbstat: Blocks write time|
Time spent writing data file blocks by backends.
|Dependent item|pgsql.dbstat.sum.blk_write_time
**Preprocessing**

- 
JSON Path: `$.blk_write_time`

- 
Custom multiplier: `0.001`

|
|Dbstat: Committed transactions per second|
Number of transactions that have been committed per second.
|Dependent item|pgsql.dbstat.sum.xact_commit.rate
**Preprocessing**

- 
JSON Path: `$.xact_commit`

- Change per second
|
|Dbstat: Conflicts per second|
Number of queries canceled per second due to conflicts with recovery (conflicts occur only on standby servers; see pg_stat_database_conflicts for details).
|Dependent item|pgsql.dbstat.sum.conflicts.rate
**Preprocessing**

- 
JSON Path: `$.conflicts`

- Change per second
|
|Dbstat: Deadlocks per second|
Number of deadlocks detected per second.
|Dependent item|pgsql.dbstat.sum.deadlocks.rate
**Preprocessing**

- 
JSON Path: `$.deadlocks`

- Change per second
|
|Dbstat: Disk blocks read per second|
Number of disk blocks read per second.
|Dependent item|pgsql.dbstat.sum.blks_read.rate
**Preprocessing**

- 
JSON Path: `$.blks_read`

- Change per second
|
|Dbstat: Hit blocks read per second|
Number of times per second disk blocks were found already in the buffer cache.
|Dependent item|pgsql.dbstat.sum.blks_hit.rate
**Preprocessing**

- 
JSON Path: `$.blks_hit`

- Change per second
|
|Dbstat: Number temp bytes per second|
Total amount of data written per second to temporary files by queries.
|Dependent item|pgsql.dbstat.sum.temp_bytes.rate
**Preprocessing**

- 
JSON Path: `$.temp_bytes`

- Change per second
|
|Dbstat: Number temp files per second|
Number of temporary files created by queries per second.
|Dependent item|pgsql.dbstat.sum.temp_files.rate
**Preprocessing**

- 
JSON Path: `$.temp_files`

- Change per second
|
|Dbstat: Roll backed transactions per second|
Number of transactions that have been rolled back per second.
|Dependent item|pgsql.dbstat.sum.xact_rollback.rate
**Preprocessing**

- 
JSON Path: `$.xact_rollback`

- Change per second
|
|Dbstat: Rows deleted per second|
Number of rows deleted by queries per second.
|Dependent item|pgsql.dbstat.sum.tup_deleted.rate
**Preprocessing**

- 
JSON Path: `$.tup_deleted`

- Change per second
|
|Dbstat: Rows fetched per second|
Number of rows fetched by queries per second.
|Dependent item|pgsql.dbstat.sum.tup_fetched.rate
**Preprocessing**

- 
JSON Path: `$.tup_fetched`

- Change per second
|
|Dbstat: Rows inserted per second|
Number of rows inserted by queries per second.
|Dependent item|pgsql.dbstat.sum.tup_inserted.rate
**Preprocessing**

- 
JSON Path: `$.tup_inserted`

- Change per second
|
|Dbstat: Rows returned per second|
Number of rows returned by queries per second.
|Dependent item|pgsql.dbstat.sum.tup_returned.rate
**Preprocessing**

- 
JSON Path: `$.tup_returned`

- Change per second
|
|Dbstat: Rows updated per second|
Number of rows updated by queries per second.
|Dependent item|pgsql.dbstat.sum.tup_updated.rate
**Preprocessing**

- 
JSON Path: `$.tup_updated`

- Change per second
|
|Dbstat: Backends connected|
Number of connected backends.
|Dependent item|pgsql.dbstat.sum.numbackends
**Preprocessing**

- 
JSON Path: `$.numbackends`

|
|Connections sum: Active|
Total number of connections executing a query.
|Dependent item|pgsql.connections.sum.active
**Preprocessing**

- 
JSON Path: `$.active`

|
|Connections sum: Fastpath function call|
Total number of connections executing a fast-path function.
|Dependent item|pgsql.connections.sum.fastpath_function_call
**Preprocessing**

- 
JSON Path: `$.fastpath_function_call`

|
|Connections sum: Idle|
Total number of connections waiting for a new client command.
|Dependent item|pgsql.connections.sum.idle
**Preprocessing**

- 
JSON Path: `$.idle`

|
|Connections sum: Idle in transaction|
Total number of connections in a transaction state but not executing a query.
|Dependent item|pgsql.connections.sum.idle_in_transaction
**Preprocessing**

- 
JSON Path: `$.idle_in_transaction`

|
|Connections sum: Prepared|
Total number of prepared transactions:

https://www.postgresql.org/docs/current/sql-prepare-transaction.html
|Dependent item|pgsql.connections.sum.prepared
**Preprocessing**

- 
JSON Path: `$.prepared`

|
|Connections sum: Total|
Total number of connections.
|Dependent item|pgsql.connections.sum.total
**Preprocessing**

- 
JSON Path: `$.total`

|
|Connections sum: Total, %|
Total number of connections, in percentage.
|Dependent item|pgsql.connections.sum.total_pct
**Preprocessing**

- 
JSON Path: `$.total_pct`

|
|Connections sum: Waiting|
Total number of waiting connections:

https://www.postgresql.org/docs/current/monitoring-stats.html#WAIT-EVENT-TABLE
|Dependent item|pgsql.connections.sum.waiting
**Preprocessing**

- 
JSON Path: `$.waiting`

|
|Connections sum: Idle in transaction (aborted)|
Total number of connections in a transaction state but not executing a query, and where one of the statements in the transaction caused an error.
|Dependent item|pgsql.connections.sum.idle_in_transaction_aborted
**Preprocessing**

- 
JSON Path: `$.idle_in_transaction_aborted`

|
|Connections sum: Disabled|
Total number of disabled connections.
|Dependent item|pgsql.connections.sum.disabled
**Preprocessing**

- 
JSON Path: `$.disabled`

|
|Age of oldest xid|
Age of oldest xid.
|Database monitor|db.odbc.select[pgsql.oldest.xid,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]|
|Count of autovacuum workers|
Number of autovacuum workers.
|Database monitor|db.odbc.select[pgsql.autovacuum.count,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]|
|Cache hit ratio, %|
Cache hit ratio.
|Calculated|pgsql.cache.hit|
|Uptime|
Time since the server started.
|Database monitor|db.odbc.select[pgsql.uptime,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]|
|Replication: Lag in bytes|
Replication lag with master, in bytes.
|Database monitor|db.odbc.select[pgsql.replication.lag.b,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]|
|Replication: Lag in seconds|
Replication lag with master, in seconds.
|Database monitor|db.odbc.select[pgsql.replication.lag.sec,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]|
|Replication: Recovery role|
Replication role: 1 — recovery is still in progress (standby mode), 0 — master mode.
|Database monitor|db.odbc.select[pgsql.replication.recovery_role,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]|
|Replication: Standby count|
Number of standby servers.
|Database monitor|db.odbc.select[pgsql.replication.count,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]|
|Replication: Status|
Replication status: 0 — streaming is down, 1 — streaming is up, 2 — master mode.
|Database monitor|db.odbc.select[pgsql.replication.status,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]|
|Ping|
Used to test a connection to see if it is alive. It is set to 0 if the query is unsuccessful.
|Database monitor|db.odbc.select[pgsql.ping,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]
**Preprocessing**

- 
Check for not supported value: `any error`

⛔️Custom on fail: Set value to: `0`

- 
Discard unchanged with heartbeat: `1h`

|

### Triggers

|Name|Description|Expression|Severity|Dependencies and additional info|
|----|-----------|----------|--------|--------------------------------|
|PostgreSQL: Version has changed||`last(/PostgreSQL by ODBC/db.odbc.select[pgsql.version,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"],#1)<>last(/PostgreSQL by ODBC/db.odbc.select[pgsql.version,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"],#2) and length(last(/PostgreSQL by ODBC/db.odbc.select[pgsql.version,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]))>0`|Info||
|PostgreSQL: Total number of connections is too high|
Total number of current connections exceeds the limit of {$PG.CONN_TOTAL_PCT.MAX.WARN}% out of the maximum number of concurrent connections to the database server (the "max_connections" setting).
|`min(/PostgreSQL by ODBC/pgsql.connections.sum.total_pct,5m) > {$PG.CONN_TOTAL_PCT.MAX.WARN}`|Average||
|PostgreSQL: Oldest xid is too big||`last(/PostgreSQL by ODBC/db.odbc.select[pgsql.oldest.xid,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]) > 18000000`|Average||
|PostgreSQL: Service has been restarted|
PostgreSQL uptime is less than 10 minutes.
|`last(/PostgreSQL by ODBC/db.odbc.select[pgsql.uptime,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]) < 10m`|Average||
|PostgreSQL: Service is down|
Last test of a connection was unsuccessful.
|`last(/PostgreSQL by ODBC/db.odbc.select[pgsql.ping,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"])=0`|High||

### LLD rule Replication discovery

|Name|Description|Type|Key and additional info|
|----|-----------|----|-----------------------|
|Replication discovery|
Discovers replication lag metrics.
|Database monitor|db.odbc.select[pgsql.replication.process.discovery,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]
**Preprocessing**

- 
Discard unchanged with heartbeat: `3h`

|

### Item prototypes for Replication discovery

|Name|Description|Type|Key and additional info|
|----|-----------|----|-----------------------|
|Application [{#APPLICATION_NAME}]: Get replication|
Collect metrics from the "pg_stat_replication" about the application "{#APPLICATION_NAME}" that is connected to this WAL sender, which contains information about the WAL sender process, showing statistics about replication to that sender's connected standby server.
|Dependent item|pgsql.replication.get_metrics["{#APPLICATION_NAME}"]
**Preprocessing**

- 
JSON Path: `$['{#APPLICATION_NAME}']`

⛔️Custom on fail: Discard value

|
|Application [{#APPLICATION_NAME}]: Replication flush lag|
Time elapsed between flushing recent WAL locally and receiving notification that this standby server has written and flushed it (but not yet applied it). This can be used to gauge the delay that synchronous_commit level on incurred while committing if this server was configured as a synchronous standby.
|Dependent item|pgsql.replication.process.flush_lag["{#APPLICATION_NAME}"]
**Preprocessing**

- 
JSON Path: `$.flush_lag`

|
|Application [{#APPLICATION_NAME}]: Replication replay lag|
Time elapsed between flushing recent WAL locally and receiving notification that this standby server has written, flushed and applied it. This can be used to gauge the delay that synchronous_commit level remote_apply incurred while committing if this server was configured as a synchronous standby.
|Dependent item|pgsql.replication.process.replay_lag["{#APPLICATION_NAME}"]
**Preprocessing**

- 
JSON Path: `$.replay_lag`

|
|Application [{#APPLICATION_NAME}]: Replication write lag|
Time elapsed between flushing recent WAL locally and receiving notification that this standby server has written it (but not yet flushed it or applied it). This can be used to gauge the delay that synchronous_commit level remote_write incurred while committing if this server was configured as a synchronous standby.
|Dependent item|pgsql.replication.process.write_lag["{#APPLICATION_NAME}"]
**Preprocessing**

- 
JSON Path: `$.write_lag`

|

### LLD rule Database discovery

|Name|Description|Type|Key and additional info|
|----|-----------|----|-----------------------|
|Database discovery|
Discovers databases (DB) in the database management system (DBMS), except:

- templates;

- default "postgres" DB;

- DBs that do not allow connections.
|Database monitor|db.odbc.select[pgsql.db.discovery,,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]|

### Item prototypes for Database discovery

|Name|Description|Type|Key and additional info|
|----|-----------|----|-----------------------|
|DB [{#DBNAME}]: Get dbstat|
Get dbstat metrics for database "{#DBNAME}".
|Dependent item|pgsql.dbstat.get_metrics["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$['{#DBNAME}']`

⛔️Custom on fail: Discard value

|
|DB [{#DBNAME}]: Get locks|
Get locks metrics for database "{#DBNAME}".
|Dependent item|pgsql.locks.get_metrics["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$['{#DBNAME}']`

⛔️Custom on fail: Discard value

|
|DB [{#DBNAME}]: Get queries|
Get queries metrics for database "{#DBNAME}".
|Dependent item|pgsql.queries.get_metrics["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$['{#DBNAME}']`

⛔️Custom on fail: Discard value

|
|DB [{#DBNAME}]: Database age|
Database age.
|Database monitor|db.odbc.select[pgsql.db.age,,"Database={#DBNAME};{$PG.CONNSTRING.ODBC}"]|
|DB [{#DBNAME}]: Bloating tables|
Number of bloating tables.
|Database monitor|db.odbc.select[pgsql.db.bloating_tables,,"Database={#DBNAME};{$PG.CONNSTRING.ODBC}"]|
|DB [{#DBNAME}]: Database size|
Database size.
|Database monitor|db.odbc.select[pgsql.db.size,,"Database={#DBNAME};{$PG.CONNSTRING.ODBC}"]|
|DB [{#DBNAME}]: Blocks hit per second|
Total number of times per second disk blocks were found already in the buffer cache, so that a read was not necessary.
|Dependent item|pgsql.dbstat.blks_hit.rate["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.blks_hit`

- Change per second
|
|DB [{#DBNAME}]: Disk blocks read per second|
Total number of disk blocks read per second in this database.
|Dependent item|pgsql.dbstat.blks_read.rate["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.blks_read`

- Change per second
|
|DB [{#DBNAME}]: Detected conflicts per second|
Total number of queries canceled due to conflicts with recovery in this database per second.
|Dependent item|pgsql.dbstat.conflicts.rate["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.conflicts`

- Change per second
|
|DB [{#DBNAME}]: Detected deadlocks per second|
Total number of detected deadlocks in this database per second.
|Dependent item|pgsql.dbstat.deadlocks.rate["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.deadlocks`

- Change per second
|
|DB [{#DBNAME}]: Temp_bytes written per second|
Total amount of data written to temporary files by queries in this database.
|Dependent item|pgsql.dbstat.temp_bytes.rate["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.temp_bytes`

- Change per second
|
|DB [{#DBNAME}]: Temp_files created per second|
Total number of temporary files created by queries in this database.
|Dependent item|pgsql.dbstat.temp_files.rate["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.temp_files`

- Change per second
|
|DB [{#DBNAME}]: Tuples deleted per second|
Total number of rows deleted by queries in this database per second.
|Dependent item|pgsql.dbstat.tup_deleted.rate["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.tup_deleted`

- Change per second
|
|DB [{#DBNAME}]: Tuples fetched per second|
Total number of rows fetched by queries in this database per second.
|Dependent item|pgsql.dbstat.tup_fetched.rate["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.tup_fetched`

- Change per second
|
|DB [{#DBNAME}]: Tuples inserted per second|
Total number of rows inserted by queries in this database per second.
|Dependent item|pgsql.dbstat.tup_inserted.rate["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.tup_inserted`

- Change per second
|
|DB [{#DBNAME}]: Tuples returned per second|
Number of rows returned by queries in this database per second.
|Dependent item|pgsql.dbstat.tup_returned.rate["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.tup_returned`

- Change per second
|
|DB [{#DBNAME}]: Tuples updated per second|
Total number of rows updated by queries in this database per second.
|Dependent item|pgsql.dbstat.tup_updated.rate["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.tup_updated`

- Change per second
|
|DB [{#DBNAME}]: Commits per second|
Number of transactions in this database that have been committed per second.
|Dependent item|pgsql.dbstat.xact_commit.rate["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.xact_commit`

- Change per second
|
|DB [{#DBNAME}]: Rollbacks per second|
Total number of transactions in this database that have been rolled back.
|Dependent item|pgsql.dbstat.xact_rollback.rate["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.xact_rollback`

- Change per second
|
|DB [{#DBNAME}]: Backends connected|
Number of backends currently connected to this database.
|Dependent item|pgsql.dbstat.numbackends["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.numbackends`

|
|DB [{#DBNAME}]: Disk blocks read time per second|
Time spent reading data file blocks by backends per second.
|Dependent item|pgsql.dbstat.blk_read_time.rate["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.blk_read_time`

- 
Custom multiplier: `0.001`

- Change per second
|
|DB [{#DBNAME}]: Disk blocks write time per second|
Time spent writing data file blocks by backends per second.
|Dependent item|pgsql.dbstat.blk_write_time.rate["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.blk_write_time`

- 
Custom multiplier: `0.001`

- Change per second
|
|DB [{#DBNAME}]: Num of accessexclusive locks|
Number of accessexclusive locks for this database.
|Dependent item|pgsql.locks.accessexclusive["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.accessexclusive`

|
|DB [{#DBNAME}]: Num of accessshare locks|
Number of accessshare locks for this database.
|Dependent item|pgsql.locks.accessshare["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.accessshare`

|
|DB [{#DBNAME}]: Num of exclusive locks|
Number of exclusive locks for this database.
|Dependent item|pgsql.locks.exclusive["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.exclusive`

|
|DB [{#DBNAME}]: Num of rowexclusive locks|
Number of rowexclusive locks for this database.
|Dependent item|pgsql.locks.rowexclusive["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.rowexclusive`

|
|DB [{#DBNAME}]: Num of rowshare locks|
Number of rowshare locks for this database.
|Dependent item|pgsql.locks.rowshare["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$['{#DBNAME}'].rowshare`

⛔️Custom on fail: Discard value

|
|DB [{#DBNAME}]: Num of sharerowexclusive locks|
Number of total sharerowexclusive for this database.
|Dependent item|pgsql.locks.sharerowexclusive["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.sharerowexclusive`

|
|DB [{#DBNAME}]: Num of shareupdateexclusive locks|
Number of shareupdateexclusive locks for this database.
|Dependent item|pgsql.locks.shareupdateexclusive["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.shareupdateexclusive`

|
|DB [{#DBNAME}]: Num of share locks|
Number of share locks for this database.
|Dependent item|pgsql.locks.share["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.share`

|
|DB [{#DBNAME}]: Num of locks total|
Total number of locks in this database.
|Dependent item|pgsql.locks.total["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.total`

|
|DB [{#DBNAME}]: Queries max maintenance time|
Max maintenance query time for this database.
|Dependent item|pgsql.queries.mro.time_max["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.mro_time_max`

|
|DB [{#DBNAME}]: Queries max query time|
Max query time for this database.
|Dependent item|pgsql.queries.query.time_max["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.query_time_max`

|
|DB [{#DBNAME}]: Queries max transaction time|
Max transaction query time for this database.
|Dependent item|pgsql.queries.tx.time_max["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.tx_time_max`

|
|DB [{#DBNAME}]: Queries slow maintenance count|
Slow maintenance query count for this database.
|Dependent item|pgsql.queries.mro.slow_count["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.mro_slow_count`

|
|DB [{#DBNAME}]: Queries slow query count|
Slow query count for this database.
|Dependent item|pgsql.queries.query.slow_count["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.query_slow_count`

|
|DB [{#DBNAME}]: Queries slow transaction count|
Slow transaction query count for this database.
|Dependent item|pgsql.queries.tx.slow_count["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.tx_slow_count`

|
|DB [{#DBNAME}]: Queries sum maintenance time|
Sum maintenance query time for this database.
|Dependent item|pgsql.queries.mro.time_sum["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.mro_time_sum`

|
|DB [{#DBNAME}]: Queries sum query time|
Sum query time for this database.
|Dependent item|pgsql.queries.query.time_sum["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.query_time_sum`

|
|DB [{#DBNAME}]: Queries sum transaction time|
Sum transaction query time for this database.
|Dependent item|pgsql.queries.tx.time_sum["{#DBNAME}"]
**Preprocessing**

- 
JSON Path: `$.tx_time_sum`

|

### Trigger prototypes for Database discovery

|Name|Description|Expression|Severity|Dependencies and additional info|
|----|-----------|----------|--------|--------------------------------|
|PostgreSQL: DB [{#DBNAME}]: Too many recovery conflicts|
The primary and standby servers are in many ways loosely connected. Actions on the primary will have an effect on the standby. As a result, there is potential for negative interactions or conflicts between them.
https://www.postgresql.org/docs/current/hot-standby.html#HOT-STANDBY-CONFLICT
|`min(/PostgreSQL by ODBC/pgsql.dbstat.conflicts.rate["{#DBNAME}"],5m) > {$PG.CONFLICTS.MAX.WARN:"{#DBNAME}"}`|Average||
|PostgreSQL: DB [{#DBNAME}]: Deadlock occurred|
Number of deadlocks detected per second exceeds {$PG.DEADLOCKS.MAX.WARN:"{#DBNAME}"} for 5m.
|`min(/PostgreSQL by ODBC/pgsql.dbstat.deadlocks.rate["{#DBNAME}"],5m) > {$PG.DEADLOCKS.MAX.WARN:"{#DBNAME}"}`|High||
|PostgreSQL: DB [{#DBNAME}]: Too many slow queries|
The number of detected slow queries exceeds the limit of {$PG.SLOW_QUERIES.MAX.WARN:"{#DBNAME}"}.
|`min(/PostgreSQL by ODBC/pgsql.queries.query.slow_count["{#DBNAME}"],5m)>{$PG.SLOW_QUERIES.MAX.WARN:"{#DBNAME}"}`|Warning||

### LLD rule Bgwriter metrics discovery (versions below 17)

|Name|Description|Type|Key and additional info|
|----|-----------|----|-----------------------|
|Bgwriter metrics discovery (versions below 17)|
Discovers "bgwriter" metrics for PostgreSQL below version 17.
|Dependent item|pgsql.bgwriter.below_17.discovery|

### Item prototypes for Bgwriter metrics discovery (versions below 17)

|Name|Description|Type|Key and additional info|
|----|-----------|----|-----------------------|
|Get bgwriter{#SINGLETON}|
Collect all metrics from pg_stat_bgwriter:

https://www.postgresql.org/docs/current/monitoring-stats.html#PG-STAT-BGWRITER-VIEW
|Database monitor|db.odbc.select[pgsql.bgwriter.below_17{#SINGLETON},,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]|
|Bgwriter: Buffers allocated per second{#SINGLETON}|
Number of buffers allocated per second.
|Dependent item|pgsql.bgwriter.buffers_alloc.rate.below_17[{#SINGLETON}]
**Preprocessing**

- 
JSON Path: `$.buffers_alloc`

- Change per second
|
|Bgwriter: Number of bgwriter cleaning scan stopped per second{#SINGLETON}|
Number of times the background writer stopped a cleaning scan because it had written too many buffers per second.
|Dependent item|pgsql.bgwriter.maxwritten_clean.rate.below_17[{#SINGLETON}]
**Preprocessing**

- 
JSON Path: `$.maxwritten_clean`

- Change per second
|
|Checkpoint: Buffers written by the background writer per second{#SINGLETON}|
Number of buffers written by the background writer per second.
|Dependent item|pgsql.bgwriter.buffers_clean.rate.below_17[{#SINGLETON}]
**Preprocessing**

- 
JSON Path: `$.buffers_clean`

- Change per second
|
|Checkpoint: Buffers written during checkpoints per second{#SINGLETON}|
Number of buffers written during checkpoints per second.
|Dependent item|pgsql.bgwriter.buffers_checkpoint.rate.below_17[{#SINGLETON}]
**Preprocessing**

- 
JSON Path: `$.buffers_checkpoint`

- Change per second
|
|Checkpoint: Scheduled per second{#SINGLETON}|
Number of scheduled checkpoints that have been performed per second.
|Dependent item|pgsql.bgwriter.checkpoints_timed.rate.below_17[{#SINGLETON}]
**Preprocessing**

- 
JSON Path: `$.checkpoints_timed`

- Change per second
|
|Checkpoint: Requested per second{#SINGLETON}|
Number of requested checkpoints that have been performed per second.
|Dependent item|pgsql.bgwriter.checkpoints_req.rate.below_17[{#SINGLETON}]
**Preprocessing**

- 
JSON Path: `$.checkpoints_req`

- Change per second
|
|Checkpoint: Checkpoint write time per second{#SINGLETON}|
Total amount of time per second that has been spent in the portion of checkpoint processing where files are written to disk.
|Dependent item|pgsql.bgwriter.checkpoint_write_time.rate.below_17[{#SINGLETON}]
**Preprocessing**

- 
JSON Path: `$.checkpoint_write_time`

- 
Custom multiplier: `0.001`

- Change per second
|
|Bgwriter: Checkpoint sync time per second{#SINGLETON}|
Total amount of time per second that has been spent in the portion of checkpoint processing where files are synchronized to disk.
|Dependent item|pgsql.bgwriter.checkpoint_sync_time.rate.below_17[{#SINGLETON}]
**Preprocessing**

- 
JSON Path: `$.checkpoint_sync_time`

- 
Custom multiplier: `0.001`

- Change per second
|
|Bgwriter: Buffers written directly by a backend per second{#SINGLETON}|
Number of buffers written directly by a backend per second.
|Dependent item|pgsql.bgwriter.buffers_backend.rate.below_17[{#SINGLETON}]
**Preprocessing**

- 
JSON Path: `$.buffers_backend`

- Change per second
|
|Bgwriter: Times a backend executed its own fsync per second{#SINGLETON}|
Number of times a backend had to execute its own fsync call per second (normally the background writer handles those even when the backend does its own write).
|Dependent item|pgsql.bgwriter.buffers_backend_fsync.rate.below_17[{#SINGLETON}]
**Preprocessing**

- 
JSON Path: `$.buffers_backend_fsync`

- Change per second
|

### LLD rule Bgwriter metrics discovery (versions above 17)

|Name|Description|Type|Key and additional info|
|----|-----------|----|-----------------------|
|Bgwriter metrics discovery (versions above 17)|
Discovers "bgwriter" metrics for PostgreSQL version 17 and above.
|Dependent item|pgsql.bgwriter.above_17.discovery|

### Item prototypes for Bgwriter metrics discovery (versions above 17)

|Name|Description|Type|Key and additional info|
|----|-----------|----|-----------------------|
|Get bgwriter{#SINGLETON}|
Collect all metrics from pg_stat_bgwriter:

https://www.postgresql.org/docs/current/monitoring-stats.html#PG-STAT-BGWRITER-VIEW
|Database monitor|db.odbc.select[pgsql.bgwriter.above_17{#SINGLETON},,"Database={$PG.DATABASE};{$PG.CONNSTRING.ODBC}"]|
|Bgwriter: Buffers allocated per second{#SINGLETON}|
Number of buffers allocated per second.
|Dependent item|pgsql.bgwriter.buffers_alloc.rate.above_17[{#SINGLETON}]
**Preprocessing**

- 
JSON Path: `$.buffers_alloc`

- Change per second
|
|Bgwriter: Number of bgwriter cleaning scan stopped per second{#SINGLETON}|
Number of times the background writer stopped a cleaning scan because it had written too many buffers per second.
|Dependent item|pgsql.bgwriter.maxwritten_clean.rate.above_17[{#SINGLETON}]
**Preprocessing**

- 
JSON Path: `$.maxwritten_clean`

- Change per second
|
|Checkpoint: Buffers written by the background writer per second{#SINGLETON}|
Number of buffers written by the background writer per second.
|Dependent item|pgsql.bgwriter.buffers_clean.rate.above_17[{#SINGLETON}]
**Preprocessing**

- 
JSON Path: `$.buffers_clean`

- Change per second
|
|Checkpoint: Buffers written during checkpoints per second{#SINGLETON}|
Number of buffers written during checkpoints per second.
|Dependent item|pgsql.bgwriter.buffers_checkpoint.rate.above_17[{#SINGLETON}]
**Preprocessing**

- 
JSON Path: `$.buffers_checkpoint`

- Change per second
|
|Checkpoint: Scheduled per second{#SINGLETON}|
Number of scheduled checkpoints that have been performed per second.
|Dependent item|pgsql.bgwriter.checkpoints_timed.rate.above_17[{#SINGLETON}]
**Preprocessing**

- 
JSON Path: `$.checkpoints_timed`

- Change per second
|
|Checkpoint: Requested per second{#SINGLETON}|
Number of requested checkpoints that have been performed per second.
|Dependent item|pgsql.bgwriter.checkpoints_req.rate.above_17[{#SINGLETON}]
**Preprocessing**

- 
JSON Path: `$.checkpoints_req`

- Change per second
|
|Checkpoint: Checkpoint write time per second{#SINGLETON}|
Total amount of time per second that has been spent in the portion of checkpoint processing where files are written to disk.
|Dependent item|pgsql.bgwriter.checkpoint_write_time.rate.above_17[{#SINGLETON}]
**Preprocessing**

- 
JSON Path: `$.checkpoint_write_time`

- 
Custom multiplier: `0.001`

- Change per second
|
|Bgwriter: Checkpoint sync time per second{#SINGLETON}|
Total amount of time per second that has been spent in the portion of checkpoint processing where files are synchronized to disk.
|Dependent item|pgsql.bgwriter.checkpoint_sync_time.rate.above_17[{#SINGLETON}]
**Preprocessing**

- 
JSON Path: `$.checkpoint_sync_time`

- 
Custom multiplier: `0.001`

- Change per second
|

## Feedback

Please report any issues with the template at [`https://support.zabbix.com`](https://support.zabbix.com)

You can also provide feedback, discuss the template, or ask for help at [`ZABBIX forums`](https://www.zabbix.com/forum/zabbix-suggestions-and-feedback)

