Postgresql Receiver
contrib
Maintainers: @antonblock, @ishleenk17, @Caleb-Hurshman
Source: opentelemetry-collector-contrib
Supported Telemetry
Overview
Prerequisites
See PostgreSQL documentation for supported versions. The monitoring user must be grantedSELECT on pg_stat_database.
[!NOTE] The feature gatereceiver.postgresql.separateSchemaAttraddresses an inconsistency in how schema names are reported across different metric types. When enabled, schema names are consistently reported in a dedicatedpostgresql.schema.nameresource attribute. Status: Alpha (disabled by default) When disabled (default behavior):When enabled (recommended for consistency):
- Table metrics:
postgresql.table.name = "schema_name.table_name"(schema included)- Index metrics:
postgresql.table.name = "table_name"(schema missing)- Function metrics: Schema reported separately in some cases
This ensures reliable correlation of metrics when tables with identical names exist across different schemas. To enable:
- All metrics consistently use:
postgresql.schema.name = "schema_name"postgresql.table.name = "table_name"See https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/29559 for more details.
Configuration
The following settings are required to create a database connection:usernamepassword
-
endpoint(default =localhost:5432): The endpoint of the PostgreSQL server. Whether using TCP or Unix sockets, this value should behost:port. Iftransportis set tounix, the endpoint will internally be translated fromhost:portto/host.s.PGSQL.port -
transport(default =tcp): The transport protocol being used to connect to PostgreSQL. Available options aretcpandunix. -
databases(default =[]): The list of databases for which the receiver will attempt to collect statistics. If an empty list is provided, the receiver will attempt to collect statistics for all non-template databases. -
exclude_databases(default =[]): List of databases which will be excluded when collecting statistics.
tls to help configure client transport security
-
insecure(default =false): Whether to enable client transport security for the PostgreSQL connection. -
insecure_skip_verify(default =true): Whether to validate server name and certificate if client transport security is enabled. -
cert_file(default =$HOME/.postgresql/postgresql.crt): A certificate used for client authentication, if necessary. -
key_file(default =$HOME/.postgresql/postgresql.key): An SSL key used for client authentication, if necessary. -
ca_file(default = ""): A set of certificate authorities used to validate the database serverβs SSL certificate. -
collection_interval(default =10s): This receiver collects metrics on an interval. This value must be a string readable by Golangβs time.ParseDuration. Valid time units arens,us(orΒ΅s),ms,s,m,h. -
initial_delay(default =1s): defines how long this receiver waits before starting.
Query Sample Collection
We provide functionality to collect the query sample from PostgreSQL. It will get historical query frompg_stat_activity. To enable it, you will need the following configuration
pg_monitor. Take the example from testdata/integration/init.sql
max_rows_per_query: (optional, default=1000) The max number of rows would return from the query againstpg_stat_activity.
Top Query Collection
We provide functionality to collect the most executed queries from PostgreSQL. It will get data frompg_stat_statements and report incremental value of total_exec_time, total_plan_time, calls, rows, shared_blks_dirtied, shared_blks_hit, shared_blks_read, shared_blks_written, temp_blks_read, temp_blks_written. To enable it, you will need the following configuration
testdata/integration/02-create-extension.sh
max_rows_per_query: (optional, default=1000) The max number of rows would return from the query againstpg_stat_statements.top_n_query: (optional, default=200) The maximum number of active queries to report (to the next consumer) in a single run.max_explain_each_interval: (optional, default=1000). The maximum number of explain query to be sent in each scrape interval. The top query collection would not get the query plan directly. Instead, we need to mimic the query in the database and get the query plan from database separately. This could lead some resources usage and limit this will reduce the impact on your database.query_plan_cache_size: (optional, default=1000). The query plan cache size. Once we got explain for one query, we will store it in the cache. This defines the cacheβs size for query plan.query_plan_cache_ttl: (optional, default=1h). How long before the query plan cache got expired. Example values:1m,1h.collection_interval: (optional, default=60s). This receiver can collect top_query metrics on an interval. If not provided then the global collection_interval takes effect. This value must be a string readable by Golangβs time.ParseDuration. Valid time units arens,us(orΒ΅s),ms,s,m,h.
Example Configuration
Connection pool feature
The feature gatereceiver.postgresql.connectionPool allows to enable the creation & reuse of a pool per database for the connections instead of creating & closing on each scrape.
This is generally a useful optimization but also alleviates the volume of generated audit logs when the PostgreSQL instance is configured with log_connections=on and/or log_disconnections=on.
When this feature gate is enabled, the following optional settings are available nested under connection_pool to help configure the connection pools:
max_idle_time: The maximum amount of time a connection may be idle before being closed.max_lifetime: The maximum amount of time a connection may be reused.max_idle: The maximum number of connections in the idle connection pool.max_open: The maximum number of open connections to the database.
sql/database package.
Example Configuration
Metrics
Details about the metrics produced by this receiver can be found in metadata.yamlMetrics
| Metric Name | Description | Unit | Type | Attributes |
|---|---|---|---|---|
β
postgresql.backends | The number of backends. | 1 | UpDownCounter | |
β
postgresql.bgwriter.buffers.allocated | Number of buffers allocated. | {buffers} | Counter | |
β
postgresql.bgwriter.buffers.writes | Number of buffers written. | {buffers} | Counter | bg_buffer_source |
β
postgresql.bgwriter.checkpoint.count | The number of checkpoints performed. | {checkpoints} | Counter | bg_checkpoint_type |
β
postgresql.bgwriter.duration | Total time spent writing and syncing files to disk by checkpoints. | ms | Counter | bg_duration_type |
β
postgresql.bgwriter.maxwritten | Number of times the background writer stopped a cleaning scan because it had written too many buffers. | 1 | Counter | |
β postgresql.blks_hit | Number of times disk blocks were found already in the buffer cache. | {blks_hit} | Counter | |
β postgresql.blks_read | Number of disk blocks read in this database. | {blks_read} | Counter | |
β
postgresql.blocks_read | The number of blocks read. | 1 | Counter | source |
β
postgresql.commits | The number of commits. | 1 | Counter | |
β
postgresql.connection.max | Configured maximum number of client connections allowed | {connections} | Gauge | |
β
postgresql.database.count | Number of user databases. | {databases} | UpDownCounter | |
β postgresql.database.locks | The number of database locks. | {lock} | Gauge | relation, mode, lock_type |
β
postgresql.db_size | The database disk usage. | By | UpDownCounter | |
β postgresql.deadlocks | The number of deadlocks. | {deadlock} | Counter | |
β postgresql.function.calls | The number of calls made to a function. Requires track_functions=pl|all in Postgres config. | {call} | Counter | function |
β
postgresql.index.scans | The number of index scans on a table. | {scans} | Counter | |
β
postgresql.index.size | The size of the index on disk. | By | Gauge | |
β
postgresql.operations | The number of db row operations. | 1 | Counter | operation |
β
postgresql.replication.data_delay | The amount of data delayed in replication. | By | Gauge | replication_client |
β
postgresql.rollbacks | The number of rollbacks. | 1 | Counter | |
β
postgresql.rows | The number of rows in the database. | 1 | UpDownCounter | state |
β postgresql.sequential_scans | The number of sequential scans. | {sequential_scan} | Counter | |
β
postgresql.table.count | Number of user tables in a database. | {table} | UpDownCounter | |
β
postgresql.table.size | Disk space used by a table. | By | UpDownCounter | |
β
postgresql.table.vacuum.count | Number of times a table has manually been vacuumed. | {vacuum} | Counter | |
β postgresql.temp.io | Total amount of data written to temporary files by queries. | By | Counter | |
β postgresql.temp_files | The number of temp files. | {temp_file} | Counter | |
β postgresql.tup_deleted | Number of rows deleted by queries in the database. | {tup_deleted} | Counter | |
β postgresql.tup_fetched | Number of rows fetched by queries in the database. | {tup_fetched} | Counter | |
β postgresql.tup_inserted | Number of rows inserted by queries in the database. | {tup_inserted} | Counter | |
β postgresql.tup_returned | Number of rows returned by queries in the database. | {tup_returned} | Counter | |
β postgresql.tup_updated | Number of rows updated by queries in the database. | {tup_updated} | Counter | |
β
postgresql.wal.age | Age of the oldest WAL file. | s | Gauge | |
β postgresql.wal.delay | Time between flushing recent WAL locally and receiving notification that the standby server has completed an operation with it. | s | Gauge | wal_operation_lag, replication_client |
β
postgresql.wal.lag | Time between flushing recent WAL locally and receiving notification that the standby server has completed an operation with it. | s | Gauge | wal_operation_lag, replication_client |
Attributes
| Attribute Name | Description | Type | Values |
|---|---|---|---|
source | The source of a buffer write. | string | backend, backend_fsync, checkpoints, bgwriter |
type | The type of checkpoint state. | string | requested, scheduled |
type | The type of time spent during the checkpoint. | string | sync, write |
db.namespace | The namespace or schema of the database where the query is executed. | string | |
db.query.text | The text of the database query being executed. | string | |
db.system.name | The database management system (DBMS) product as identified by the client instrumentation. | string | postgresql |
function | The name of the function. | string | |
lock_type | Type of the lockable object. | string | |
mode | Name of the lock mode held or desired by the process. | string | |
network.peer.address | IP address of the client connected to this backend. | string | |
network.peer.port | TCP port number that the client is using for communication with this backend. | int | |
operation | The database operation. | string | ins, upd, del, hot_upd |
postgresql.application_name | Name of the application that is connected to this backend. | string | |
postgresql.calls | Number of times the statement was executed, reported in delta value. | int | |
postgresql.client_hostname | Host name of the connected client, as reported by a reverse DNS lookup of client_addr. | string | |
postgresql.pid | Process ID of this backend. | int | |
postgresql.query_id | Identifier of this backendβs most recent query. If state is active this field shows the identifier of the currently executing query. In all other states, it shows the identifier of last query that was executed. | string | |
postgresql.query_plan | The execution plan used by PostgreSQL for the query. | string | |
postgresql.query_start | Time when the currently active query was started, or if state is not active, when the last query was started. | string | |
postgresql.queryid | Hash code to identify identical normalized queries. | string | |
postgresql.rolname | The name of the PostgreSQL role that executed the query. | string | |
postgresql.rows | Total number of rows retrieved or affected by the statement, reported in delta value. | int | |
postgresql.shared_blks_dirtied | Total number of shared blocks dirtied by the statement, reported in delta value. | int | |
postgresql.shared_blks_hit | Total number of shared block cache hits by the statement, reported in delta value. | int | |
postgresql.shared_blks_read | Total number of shared blocks read by the statement, reported in delta value. | int | |
postgresql.shared_blks_written | Total number of shared blocks written by the statement, reported in delta value. | int | |
postgresql.state | Current overall state of this backend | string | |
postgresql.temp_blks_read | Total number of temp blocks read by the statement, reported in delta value. | int | |
postgresql.temp_blks_written | Total number of temp blocks written by the statement, reported in delta value. | int | |
postgresql.total_exec_time | Total time spent executing the statement, in delta milliseconds. | double | |
postgresql.total_plan_time | Total time spent planning the statement, in delta milliseconds. | double | |
postgresql.wait_event | Wait event name if backend is currently waiting, otherwise NULL. | string | |
postgresql.wait_event_type | The type of event for which the backend is waiting, if any; otherwise NULL. | string | |
relation | OID of the relation targeted by the lock, or null if the target is not a relation or part of a relation. | string | |
replication_client | The IP address of the client connected to this backend. If this field is βunixβ, it indicates either that the client is connected via a Unix socket. | string | |
source | The block read source type. | string | heap_read, heap_hit, idx_read, idx_hit, toast_read, toast_hit, tidx_read, tidx_hit |
state | The tuple (row) state. | string | dead, live |
user.name | Name of the user logged into this backend. | string | |
operation | The operation which is responsible for the lag. | string | flush, replay, write |
Resource Attributes
| Attribute Name | Description | Type | Enabled |
|---|---|---|---|
postgresql.database.name | The name of the database. | string | β |
postgresql.index.name | The name of the index on a table. | string | β |
postgresql.schema.name | The schema name. | string | β |
postgresql.table.name | The table name. | string | β |
service.instance.id | A unique identifier of the PostgreSQL instance in the format host:port (defaults to βunknown:5432β in case of error in generating this value). | string | β |
Configuration
Example Configuration
Last generated: 2026-04-13