Skip to content

pgexporter Metrics

The main task of pgexporter is to gather metrics from your database and format it according to Prometheus metrics standards. pgexporter provides some pre-defined metrics that are necessary/useful, and also provides the capability of providing custom metrics to the user.

Internal Metrics

pgexporter defines a lot of metrics out of the box. By default, all of them are enabled, and will be reflected in the output. However, if you need only specific metrics, you can enable only them as shown here (and thus, disable other metrics).

Listing them (for a wider view, visit here):

CategoryNameSupportTypeLabelsCollectorSortServerDescription
Generalpgexporter_statev10-16counterN/AN/AN/AState of pgexporter.
pgexporter_versioncounterpgexporter_versionN/AN/AN/AVersion of pgexporter.
pgexporter_postgresql_activegaugeserverN/AnamebothStatus of PostgreSQL servers.
pgexporter_postgresql_versioncounterserver, versionN/AnamebothPostgreSQL Versions on servers.
pgexporter_uptimecounterserverN/AnamebothThe PostgreSQL uptime in seconds.
pgexporter_postgresql_primarygaugeserverprimarynamebothIs the PostgreSQL instance the primary
pg_settingspgexporter_pg_settings_name_of_settingv10-16gaugeserversettingsnamebothSettings.
pg_databasepgexporter_pg_database_sizev10-16gaugeserver, databasedbdatabothSize of the database
pg_lockspgexporter_pg_locks_countv10-16gaugeserver, database, modelocksdatabothLock count of a database
pg_replication_slotspgexporter_pg_replication_slots_activev10-16gaugeserver, slot_name, slot_type, databasereplicationdatabothIs the replication active
pgexporter_pg_replication_slots_temporarygaugeIs the replication temporary
pg_stat_bgwriterpgexporter_pg_stat_bgwriter_buffers_allocv10-16gaugeserver, database, modestat_bgwriternamebothbuffers_alloc
pgexporter_pg_stat_bgwriter_buffers_backendgaugebuffers_backend
pgexporter_pg_stat_bgwriter_buffers_backend_fsyncgaugebuffers_backend_fsync
pgexporter_pg_stat_bgwriter_buffers_checkpointgaugebuffers_checkpoint
pgexporter_pg_stat_bgwriter_buffers_cleangaugebuffers_clean
pgexporter_pg_stat_bgwriter_checkpoint_sync_timecountercheckpoint_sync_time
pgexporter_pg_stat_bgwriter_checkpoint_write_timecountercheckpoint_write_time
pgexporter_pg_stat_bgwriter_checkpoints_reqcountercheckpoints_req
pgexporter_pg_stat_bgwriter_checkpoints_timedcountercheckpoints_timed
pgexporter_pg_stat_bgwriter_maxwritten_cleancountermaxwritten_clean
pg_process_idle_secondspgexporter_pg_process_idle_secondsv10-16histogramserver, application_nameidle_procsnamebothHistogram of idle processes
pg_available_extensionspgexporter_pg_available_extensionsv10-16gaugeserveravailable_extensionsnamebothNumber of available extensions for installation.
pg_extensionpgexporter_pg_installed_extensionsv10-16gaugeserver, extensionsinstalled_extensionsnamebothNumber of installed extensions.
pg_file_settingspgexporter_pg_file_settingsv10-16gaugeserver, sourcefile, appliedfile_settingsdatabothSettings that are applied.
pg_indexespgexporter_pg_indexesv10-16gaugeserver, schemaname, tablenameindexesdatabothIndexes for each schemaname for each tablename.
pg_matviewspgexporter_pg_matviewsv10-16gaugeservermatviewsnamebothNumber of applied Materialized views.
pg_rulespgexporter_pg_rulesv10-16gaugeserver, tablenamerulesdatabothNumber of rules in table.
pg_shadowpgexporter_pg_shadowv10-16gaugeserver, tablenameauth_typedatabothNumber of users with authentication type.
pg_usr_evt_triggerpgexporter_pg_usr_evt_triggerv10-16gaugeserverusr_evt_triggernamebothNumber of user defined event triggers.
pg_db_connpgexporter_pg_db_connv10-16gaugeserver, databaseconnectionsnamebothNumber of database connections.
pgexporter_pg_db_conn_sslgaugeserver, databasedatabothNumber of DB connections with SSL.
pg_statio_all_tablespgexporter_pg_statio_all_tables_heap_blks_readv10-16counterserverstatio_all_tablesnamebothNumber of disk blocks read in postgres db.
pgexporter_pg_statio_all_tables_heap_blks_hitcounterNumber of buffer hits read in postgres db.
pgexporter_pg_statio_all_tables_idx_blks_readcounterNumber of disk blocks reads from all indexes in postgres db.
pgexporter_pg_statio_all_tables_idx_blks_hitcounterNumber of buffer hits read from all indexes in postgres db.
pgexporter_pg_statio_all_tables_toast_blks_readcounterNumber of disk blocks reads from postgres db's TOAST tables.
pgexporter_pg_statio_all_tables_toast_blks_hitcounterNumber of buffer hits read from postgres db's TOAST tables.
pgexporter_pg_statio_all_tables_tidx_blks_readcounterNumber of disk blocks reads from postgres db's TOAST table indexes.
pgexporter_pg_statio_all_tables_tidx_blks_hitcounterNumber of buffer hits read from postgres db's TOAST table indexes.
pg_statio_all_sequencespgexporter_pg_statio_all_sequences_blks_readv10-16counterserverstatio_all_sequencesnamebothNumber of disk blocks read from sequences in postgres db.
pgexporter_pg_statio_all_sequences_blks_hitcounterNumber of buffer hits read from sequences in postgres db.
pg_stat_user_functionspgexporter_pg_stat_user_functions_callsv10-16counterserver, funcnamestat_user_functionsdatabothNumber of times function is called.
pgexporter_pg_stat_user_functions_self_timecounterTotal time spent in milliseconds on the function itself.
pgexporter_pg_stat_user_functions_total_timecounterTotal time spent in milliseconds by the function and any other functions called by it.
pg_stat_statements_callspgexporter_pg_stat_statements_callsv10-16counterserver, querystat_statements_callsdatabothNumber of times the SQL query is executed.
pg_stat_statements_rowspgexporter_pg_stat_statements_rowsv10-16counterserver, querystat_statements_rowsdatabothNumber of rows the SQL query affects.
pg_stat_replicationpgexporter_pg_stat_replicationv10-16gaugeserver, application_namestat_replicationdatabothNumber of streaming WAL connections per application.
pg_stat_archiverpgexporter_pg_stat_archiver_archived_countv10-16counterserverstat_archiverdatabothNumber of successful archived WAL files.
pgexporter_pg_stat_archiver_success_time_elapsed_mscounterMilliseconds since successful archived WAL file.
pgexporter_pg_stat_archiver_archived_countcounterNumber of failed archival operation on WAL files.
pgexporter_pg_stat_archiver_archived_countcounterMilliseconds since last failed archival operation on WAL files.
pg_wal_last_receivedpgexporter_pg_wal_last_receivedv11-16counterserver, senderwal_last_receivednamereplicaTime since last message received from WAL sender.
pg_gss_authpgexporter_pg_gss_authv12-16gaugeservergssapinamebothNumber of GSSAPI authenticated DB connections.
pg_encrypted_connpgexporter_pg_encrypted_conngaugeserverencryted_connsnamebothNumber of encrypted DB connections.
pg_shmem_allocationspgexporter_pg_shmem_allocations_sizev13-16histogramservershmem_sizenamebothHistogram of shared memory sizes.
pg_stat_statements_total_exec_timepgexporter_pg_stat_statements_total_exec_timegaugeserver, querystat_statements_total_exec_timenamebothMilliseconds taken by the sql query to execute.
pg_stat_statements_planspgexporter_pg_stat_statements_planscounterserver, querystat_statements_plansdatabothNumber of times the sql query is planned.
pg_stat_statements_wal_bytespgexporter_pg_stat_statements_wal_bytescounterserver, querystat_statements_wal_bytesdatabothBytes occupied in WAL.
pg_mem_ctxpgexporter_pg_mem_ctx_contextsv14-16gaugeserver, parentmem_ctxnamebothNumber of memory contexts per parent.
pgexporter_pg_mem_ctx_free_bytesgaugeFree bytes per memory context.
pgexporter_pg_mem_ctx_used_bytesgaugeUsed bytes per memory context.
pgexporter_pg_mem_ctx_total_bytesgaugeTotal bytes per memory context.
pg_stat_walpgexporter_pg_stat_wal_wal_recordsv14-16counterserverstat_walnamebothNumber of WAL records generated.
pgexporter_pg_stat_wal_wal_fpicounterNumber of WAL full page images generated.
pgexporter_pg_stat_wal_wal_bytescounterTotal bytes of generated WAL.
pgexporter_pg_stat_wal_wal_buffers_fullcounterNumber of disk writes due to WAL buffers being full.
pgexporter_pg_stat_wal_wal_writecounterNumber of times WAL files were written to disk.
pgexporter_pg_stat_wal_wal_synccounterNumber of times WAL files were synced to disk.
pgexporter_pg_stat_wal_wal_write_timecounterTime taken for WAL files to be written to disk.
pgexporter_pg_stat_wal_wal_sync_timecounterTime taken for WAL files to be synced to disk.
pg_stat_databasepgexporter_pg_stat_database_blk_read_timev10-16counterserver, databasestat_dbnamebothpg_stat_database_blk_read_time
pgexporter_pg_stat_database_blk_write_timecounterpg_stat_database_blk_write_time
pgexporter_pg_stat_database_blks_hitcounterpg_stat_database_blks_hit
pgexporter_pg_stat_database_blks_readcounterpg_stat_database_blks_read
pgexporter_pg_stat_database_deadlockscounterpg_stat_database_deadlocks
pgexporter_pg_stat_database_temp_filesgaugepg_stat_database_temp_files
pgexporter_pg_stat_database_temp_bytesgaugepg_stat_database_temp_bytes
pgexporter_pg_stat_database_tup_returnedcounterpg_stat_database_tup_returned
pgexporter_pg_stat_database_tup_fetchedcounterpg_stat_database_tup_fetched
pgexporter_pg_stat_database_tup_insertedcounterpg_stat_database_tup_inserted
pgexporter_pg_stat_database_tup_updatedcounterpg_stat_database_tup_updated
pgexporter_pg_stat_database_tup_deletedcounterpg_stat_database_tup_deleted
pgexporter_pg_stat_database_xact_commitcounterpg_stat_database_xact_commit
pgexporter_pg_stat_database_xact_rollbackcounterpg_stat_database_xact_rollback
pgexporter_pg_stat_database_conflictscounterpg_stat_database_conflicts
pgexporter_pg_stat_database_numbackendsgaugepg_stat_database_numbackends
pgexporter_pg_stat_database_checksum_failuresv12-16gaugepg_stat_database_checksum_failures
pgexporter_pg_stat_database_session_timev14-16gaugepg_stat_database_session_time
pgexporter_pg_stat_database_active_timegaugepg_stat_database_active_time
pgexporter_pg_stat_database_idle_in_transaction_timegaugepg_stat_database_idle_in_transaction_time
pgexporter_pg_stat_database_sessionsgaugepg_stat_database_sessions
pgexporter_pg_stat_database_sessions_abandonedgaugepg_stat_database_sessions_abandoned
pgexporter_pg_stat_database_sessions_fatalgaugepg_stat_database_sessions_fatal
pgexporter_pg_stat_database_sessions_killedgaugepg_stat_database_sessions_killed
pg_wal_prefetch_resetpgexporter_pg_wal_prefetch_resetv15-16counterserverwal_prefetch_resetnamebothSeconds from last WAL prefetch stats reset.
pg_gssapi_credentials_delegatedpgexporter_pg_gssapi_credentials_delegatedv16gaugeservergssapi_creds_delegatednamebothNumber of DB connections with delegated GSSAPI credentials.
pg_stat_iopgexporter_pg_stat_io_readsv16counterserver, backend_typestat_ionamebothNumber of read operations.
pgexporter_pg_stat_io_read_timecounterTotal time spent on read operations in milliseconds.
pgexporter_pg_stat_io_writescounterNumber of write operations.
pgexporter_pg_stat_io_write_timecounterTotal time spent on write operations in milliseconds.
pgexporter_pg_stat_io_writebackscounterNumber of writeback to permanent storage requests sent to kernel.
pgexporter_pg_stat_io_writeback_timecounterTotal time spent on writeback operations in milliseconds.
pgexporter_pg_stat_io_extendscounterNumber of relation extend operations.
pgexporter_pg_stat_io_extend_timecounterTotal time spent on relation extend operations in milliseconds.
pgexporter_pg_stat_io_op_bytesgaugeBytes per unit of I/O read, written or extended.
pgexporter_pg_stat_io_hitscounterThe number of times a desired block was found in shared buffer.
pgexporter_pg_stat_io_evictionscounterThe number of times a block has been written out from shared or local buffer in order to make it available for another use.
pgexporter_pg_stat_io_reusescounterThe number of times an existing buffer in a size-limited ring buffer outside of shared buffers was reused as part of an I/O operation.
pgexporter_pg_stat_io_fsyncscounterNumber of fsync calls.
pgexporter_pg_stat_io_fsync_timecounterTotal time spent on fsync operations in milliseconds.
pg_stat_database_conflictspgexporter_pg_stat_database_conflicts_confl_tablespacev16counterserver, databasestat_conflictsdatabothpg_stat_database_conflicts_confl_tablespace
pgexporter_pg_stat_database_conflicts_confl_lockcounterpg_stat_database_conflicts_confl_lock
pgexporter_pg_stat_database_conflicts_confl_snapshotcounterpg_stat_database_conflicts_confl_snapshot
pgexporter_pg_stat_database_conflicts_confl_bufferpincounterpg_stat_database_conflicts_confl_bufferpin
pgexporter_pg_stat_database_conflicts_confl_deadcounterpg_stat_database_conflicts_confl_dead
pgexporter_pg_stat_database_conflicts_confl_active_logicalslotcounterpg_stat_database_conflicts_confl_active_logicalslot
pg_stat_all_indexespgexporter_pg_stat_all_indexes_idx_scansv10-16counterserver, relnamestat_all_indexesdatabothNumber of index scans on the table's indexes.
pgexporter_pg_stat_all_indexes_idx_tup_readscounterNumber of index entries returned by scans on the table's indexes.
pgexporter_pg_stat_all_indexes_idx_tup_fetchscounterNumber of rows fetched by simple index scans on the table's indexes.
pgexporter_pg_stat_all_indexes_time_elapsed_msv16counterMilliseconds since last scan of an index in the table.

Custom Metrics

Custom metrics are either defined in a single YAML file, or inside multiple YAML files inside a single directory.

NOTE

Examples of custom YAML files can be found here.

The overall schema for the custom metrics are:

YAML = {
  version : number,
  metrics : Metric[]
};

Metric = {
  queries : Query[],
  tag : string,
  collector : string,
  sort : "name" (default) | "data",
  server : "both" (default) | "primary" | "replica",
};

Query = {
  query : string,
  columns : Column[],
  version ?: number
};

Column = Label | Counter | Gauge;

Label = {
  name: string,
  type: "label"
};

Counter = {
  name ?: string,
  type: "counter",
  description: string
};

Gauge = {
  name ?: string,
  type: "gauge",
  description: string
};
YAML = {
  version : number,
  metrics : Metric[]
};

Metric = {
  queries : Query[],
  tag : string,
  collector : string,
  sort : "name" (default) | "data",
  server : "both" (default) | "primary" | "replica",
};

Query = {
  query : string,
  columns : Column[],
  version ?: number
};

Column = Label | Counter | Gauge;

Label = {
  name: string,
  type: "label"
};

Counter = {
  name ?: string,
  type: "counter",
  description: string
};

Gauge = {
  name ?: string,
  type: "gauge",
  description: string
};

Custom metrics have to be defined in yaml files. There may be a single file or multiple of them (within a single directory).

TIP

Some examples of user-defined metrics can be found here and can be used as a reference while going through this guide.

The structure of these custom yaml files can be approached in a top-down manner as follows.

Top Level

At the top-most level of the yaml file, there are two keys version and metrics:

yml
version: ...
metrics: ...
version: ...
metrics: ...
  • version specifies a default minimum PostgreSQL support version for the queries (more on this below).
  • metrics is a list of user defined metrics.

Metrics

The metrics key is a list of metrics and has the following structure:

yml
...
metrics:

  - queries: ...
    tag: ...
    collector: ...
    sort: ...
    server: ...

  - queries: ...
    tag: ...
    collector: ...
    sort: ...
    server: ...

  - queries: ...
    tag: ...
    collector: ...
    sort: ...
    server: ...
  ...
...
metrics:

  - queries: ...
    tag: ...
    collector: ...
    sort: ...
    server: ...

  - queries: ...
    tag: ...
    collector: ...
    sort: ...
    server: ...

  - queries: ...
    tag: ...
    collector: ...
    sort: ...
    server: ...
  ...
  • queries are a list of query alternatives (more on this below).
  • tag: This specifies the tag of the metics (more on this below).
  • collector: This specifies a collector name for the metric. This allows you to disable a collector by using the -C flag (see here for details).
  • sort (optional): This specifies how the output of each metric will be sorted. Currently there are two supported values:
    • name (default): This will sort the output according to server's name.
    • data: This will sort the output according to the data of the first column of the SQL query that will be run on the server (more on this below).
  • server (optional): This specifies on which type of server the metric should query. There are three possible values to this:
    • primary: This means that this metric is only for primary servers.
    • replica: This means that this metric is only for replica servers.
    • both (default): This means that this metric is for both types of servers.

Queries

Each queries key is an object of the following structure:

yml
- queries:
  - query: ...
    columns: ...
    version: ...
  - query: ...
    columns: ...
    version: ...
    ...
- queries:
  - query: ...
    columns: ...
    version: ...
  - query: ...
    columns: ...
    version: ...
    ...
  • query: Query String for the query alternative (explained below).
  • version (optional): Minimum PostgreSQL version required to run the query (explained below). If this value is not provided, then the default is taken from the top level version.
  • columns: List of columns (explained below)

There are multiple queries for each metrics. They exist because not all queries are supported across all versions of PostgreSQL. The solution is to provide a query, as well as the minimum version of PostgreSQL it will run on.

Thus queries contains multiple entries, each containing a query and a minimum PostgreSQL version required to run it.

Query Selection Based On Server Version

Depending on the version of the server, a suitable query is picked. It is picked according to the following rule:

If your server has a version v, then it will select the query with the maximum value of version that it can find which is also less than or equal to v.

For example:

txt
|Server mininum supported PostgreSQL version|Query|
|-------------------------------------------|-----|
|                  10                       |  Q1 |
|                  12                       |  Q2 |
|                  14                       |  Q3 |
|                  16                       |  Q4 |
|                  18                       |  Q5 |
|                  20                       |  Q6 |
|                  22                       |  Q7 |
|Server mininum supported PostgreSQL version|Query|
|-------------------------------------------|-----|
|                  10                       |  Q1 |
|                  12                       |  Q2 |
|                  14                       |  Q3 |
|                  16                       |  Q4 |
|                  18                       |  Q5 |
|                  20                       |  Q6 |
|                  22                       |  Q7 |
txt
Server is v9: Not Supported (No query is sent)
Server is v10: Q1 sent
Server is v11: Q1 sent
Server is v12: Q2 sent
Server is v13: Q2 sent
Server is v14: Q3 sent
Server is v15: Q3 sent
Server is v16: Q4 sent
Server is v17: Q4 sent
Server is v18: Q5 sent
Server is v19: Q5 sent
Server is v21: Q6 sent
Server is v25: Q7 sent
Server is v9: Not Supported (No query is sent)
Server is v10: Q1 sent
Server is v11: Q1 sent
Server is v12: Q2 sent
Server is v13: Q2 sent
Server is v14: Q3 sent
Server is v15: Q3 sent
Server is v16: Q4 sent
Server is v17: Q4 sent
Server is v18: Q5 sent
Server is v19: Q5 sent
Server is v21: Q6 sent
Server is v25: Q7 sent

Columns

Labels, Gauges, Counters and Histograms are the types of Prometheus metrics currently supported by pgexporter. Histogram metric type is a bit different from the rest:

Labels, Gauges and Counters

For labels, gauges and counters, columns contains a list of columns, which can be either one of the following:

yml
- name: ...
  type: label
- name: ...
  type: label

or,

yml
- name: ...
  type: gauge
  description: ...
- name: ...
  type: gauge
  description: ...

or,

yml
- name: ...
  type: counter
  description: ...
- name: ...
  type: counter
  description: ...

Gauges or Counters do not strictly need names. It will, by default, take the name from the tag of the metric like pgexporter_tag. However, any names provided will be appended to the tag like: pgexporter_tag_name, and hence names should be provided when dealing with multiple metrics derived from a single query.

NOTE

Names are required for distinction when the same metric has more than one counter/gauge as if not provided, all of these will have the same metric name.

Histograms

How to Use Custom Metrics

Suppose the user defines their metrics in custom.yml, having the path /location/to/custom.yml. To run pgepxorter with the metrics defined in this file:

sh
$ pgexporter -c pgexporter.conf -u pgexporter_users.conf -Y /location/to/custom.yml
$ pgexporter -c pgexporter.conf -u pgexporter_users.conf -Y /location/to/custom.yml

or, if all the custom yaml files are kept in a directory having path /location/to/custom/dir, pgexporter is run as:

sh
$ pgexporter -c pgexporter.conf -u pgexporter_users.conf -Y /location/to/custom/dir
$ pgexporter -c pgexporter.conf -u pgexporter_users.conf -Y /location/to/custom/dir

Examples can be refered from here.