Skip to content

Metric plugins - mackerel-plugin-postgres

mackerel-plugin-postgres is a plugin that posts PostgreSQL statistical status as a metric. Depending on the version of PostgreSQL being monitored, additional roles may be required for users connecting to PostgreSQL. For more information, see User roles required to run the plugin.

Monitorable metrics

This plugin posts PostgreSQL statistics as a metric. For statistics, please refer to the PostgreSQL documentation.

[https://www.postgresql.org/docs/current/:embed:cite]

Postgres Connections

Post pg_stat_activity information as a metric.

Metric Display NameMetric NameDiffStackedDescription
Activepostgres.connections.activeNumber of active connections
Active waitingpostgres.connections.active_waitingNumber of connections with active and waiting events
Idlepostgres.connections.idleNumber of idle connections
Idle in transactionpostgres.connections.idle_in_transactionNumber of connections in "Idle in transaction"
Idle in transaction (aborted)postgres.connections.idle_in_transaction_aborted_Number of connections in "Idle in transaction" state with errors during the transaction
fast-path function callpostgres.connections.fastpath_function_callNumber of connections running the fast-path function
Disabledpostgres.connections.disabledNumber of connections in disabled state
  • Disabled will be posted if track_activities is disabled.

Postgres Commits

Post pg_stat_database information as a metric.

Metric Display NameMetric NameDiffStackedDescription
Xact Commitpostgres.commits.xact_commitNumber of transactions committed in the database per minute
Xact Rollbackpostgres.commits.xact_rollbackNumber of transactions rolled back in the database per minute

Postgres Blocks

Post pg_stat_database information as a metric.

Metric Display NameMetric NameDiffStackedDescription
Blocks Readpostgres.blocks.blks_readNumber of blocks read from disk per minute
Blocks Hitpostgres.blocks.blks_hitNumber of blocks read from cache per minute

Postgres Rows

Post pg_stat_database information as a metric.

Metric Display NameMetric NameDiffStackedDescription
Returned Rowspostgres.rows.tup_returnedNumber of rows returned by a query in the database per minute
Fetched Rowspostgres.rows.tup_fetchedNumber of rows fetched by query in the database per minute
Inserted Rowspostgres.rows.tup_insertedNumber of rows inserted by a query in the database per minute
Updated Rowspostgres.rows.tup_updatedNumber of rows updated by a query in the database per minute
Deleted Rowspostgres.rows.tup_deletedNumber of rows deleted by a query in the database per minute

Postgres Data Size

Post the results of running SELECT sum(pg_database_size(datname)) from pg_database as a metric.

Metric Display NameMetric NameDiffStackedDescription
Total Sizepostgres.size.total_sizeTotal size of all databases, in bytes

Postgres Dead Locks

Post pg_stat_database information as a metric.

Metric Display NameMetric NameDiffStackedDescription
Deadlockspostgres.deadlocks.deadlocksNumber of deadlocks detected in the database per minute

Postgres Block I/O time

Post pg_stat_database information as a metric.

track_io_timing must be enabled to post this metric. If the setting is disabled, post 0.

Metric Display NameMetric NameDiffStackedDescription
Block Read Time (ms)postgres.iotime.blk_read_timeTime spent reading data file block per minute, in milliseconds
Block Write Time (ms)postgres.iotime.blk_write_timeTime spent writing data file block per minute, in milliseconds

Postgres Temporary file

Post pg_stat_database information as a metric.

Metric Display NameMetric NameDiffStackedDescription
Temporary file size (byte)postgres.tempfile.temp_bytesTotal size of data written to temporary files per minute, in bytes

Postgres Amount of Transaction location change

Post information about WAL as a metric. To post this metric, set wal_level in pg_settings to logical. Otherwise, post 0.

Metric Display NameMetric NameDiffStackedDescription
Amount of Transaction location change (byte)postgres.xlog_location.xlog_location_bytesSize of WAL per minute, in bytes

The value of the metric is calculated by the following query.

  • PostgreSQL version 10 or higher
    • SELECT pg_wal_lsn_diff(pg_current_wal_lsn(), '0/0')
  • PostgreSQL version 9.2 or higher
    • SELECT pg_xlog_location_diff(pg_current_xlog_location(), '0/0')

Configurable options

OptionRequiredDescriptionDefault
-hostnameTarget hostlocalhost
-portConnection port5432
-userPostgres User
-passwordPostgres Password
-databaseDatabase namepostgres
-metric-key-prefixSpecifies the prefix of the metric namepostgres
-sslmodeWhether or not to use SSLdisable
-connect_timeoutMaximum wait for connection, in seconds5 (seconds)
-tempfileSpecify the destination file path for tempfile
-h, -helpShow help
  • If you specify a user other than postgres for user, you must specify the database to connect to with the -database option.
    • The -database option is not an option that retrieves information only for the specified database.
  • The tempfile records the latest execution results. By default, it is created under /var/tmp/mackerel-agent as mackerel-plugin-postgres-<hash string>

Example configuration

toml
[plugin.metrics.postgres]
command = ["mackerel-plugin-postgres", "-user", "username", "-password", "password"]

User roles required to run the plugin

The Postgres user connecting to the database must have the following roles.

  • PostgreSQL version 10 or higher
    • pg_monitor
  • PostgreSQL version 9.6 or lower
    • Not require

Tips

How to write PostgreSQL connection information

If you want to avoid putting the connection information needed to run mackerel-plugin-postgres directly in the --password option, you can take the following approaches. Note that these approaches require that the contents of command be written as a string, not an array.

Use the command

If you specify a string, the contents of command will be executed via the shell, so you can use the command to output the password.

mackerel-agent.conf

toml
[plugin.metrics.postgres]
command = "mackerel-plugin-postgres -user username -password <command to output password>"

Use environment variables of check monitoring

If an environment variable is defined in the env of check monitoring, the password can be written to the environment variable. For more information on env, see Configuration items.

mackerel-agent.conf

toml
[plugin.metrics.postgres]
command = "mackerel-plugin-postgres -user username -password $PG_PASSWORD"
env = { "PG_PASSWORD" = "password" }

Use environment variables of mackerel-agent

Environment variables applied to mackerel-agent are also available.

The following is an example configuration in a Linux environment.

/etc/sysconfig/mackerel-agent

PG_PASSWORD=password

mackerel-agent.conf

toml
[plugin.metrics.postgres]
command = "mackerel-plugin-postgres -user username -password $PG_PASSWORD"

Repository

[https://github.com/mackerelio/mackerel-agent-plugins/tree/master/mackerel-plugin-postgres]