---
title: PostgreSQL Metrics
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: Docs > OpenTelemetry in Datadog > Integrations > PostgreSQL Metrics
---

# PostgreSQL Metrics

## Overview{% #overview %}

The [PostgreSQL receiver](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/postgresqlreceiver) allows for collection of PostgreSQL metrics and access to the [PostgreSQL Overview](https://app.datadoghq.com/dash/integration/235/postgres---overview) and [PostgreSQL Metrics](https://app.datadoghq.com/dash/integration/17/postgres---metrics) dashboards. Configure the receiver according to the specifications of the latest version of the `postgresqlreceiver`.

For more information, see the OpenTelemetry project documentation for the [PostgreSQL receiver](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/postgresqlreceiver).

## Setup{% #setup %}

To collect PostgreSQL metrics with OpenTelemetry for use with Datadog:

1. Configure the [PostgreSQL receiver](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/postgresqlreceiver) in your OpenTelemetry Collector configuration.
1. Optionally, configure the [host metrics receiver](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/hostmetricsreceiver) if your OpenTelemetry Collector is running on the same server as your PostgreSQL database.
1. Optionally, configure the [file log receiver](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/filelogreceiver) if your OpenTelemetry Collector is running on the same server as your PostgreSQL database.
1. Configure service pipelines.
1. Ensure the OpenTelemetry Collector is [configured to export to Datadog](https://docs.datadoghq.com/opentelemetry/setup/collector_exporter.md).

### PostgreSQL receiver{% #postgresql-receiver %}

```yaml
receivers:
  postgresql/pg-host-1:
    endpoint: "<HOST>:<PORT>"
    username: "<USERNAME>"
    password: "<PASSWORD>"
    collection_interval: 15s
    metrics:
      postgresql.blks_hit:
        enabled: true
      postgresql.blks_read:
        enabled: true
      postgresql.database.locks:
        enabled: true
      postgresql.deadlocks:
        enabled: true
      postgresql.function.calls:
        enabled: true
      postgresql.sequential_scans:
        enabled: true
      postgresql.temp_files:
        enabled: true
      postgresql.temp.io:
        enabled: true
      postgresql.tup_deleted:
        enabled: true
      postgresql.tup_fetched:
        enabled: true
      postgresql.tup_inserted:
        enabled: true
      postgresql.tup_returned:
        enabled: true
      postgresql.tup_updated:
        enabled: true
      postgresql.wal.delay:
        enabled: true

processors:
  resource/pg-host-1:
    attributes:
      - action: insert
        key: datadog.host.name
        value: <HOST>
  cumulativetodelta: {}
  deltatorate:
    metrics:
      - postgresql.tup_returned
      - postgresql.tup_fetched
      - postgresql.tup_inserted
      - postgresql.tup_updated
      - postgresql.tup_deleted
      - postgresql.operations
      - postgresql.commits
      - postgresql.rollbacks
      - postgresql.blks_hit
      - postgresql.blks_read
      - postgresql.temp_files
      - postgresql.temp.io
      - postgresql.function.calls
      - postgresql.index.scans
      - postgresql.sequential_scans
      - postgresql.blocks_read
```

See the [PostgreSQL receiver documentation](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/postgresqlreceiver) for detailed configuration options and requirements.

### Host metrics receiver{% #host-metrics-receiver %}

```yaml
receivers:
  hostmetrics:
    scrapers:
      load:
      cpu:
        metrics:
         system.cpu.utilization:
           enabled: true
      memory:
      network:
```

### File log receiver{% #file-log-receiver %}

This example assumes PostgreSQL 15+ with `log_destination = 'jsonlog'` configured. If you use the default `stderr` log format, adjust the operators to match your `log_line_prefix`.

```yaml
receivers:
  filelog:
    include:
      - <PATH_TO_YOUR_POSTGRESQL_LOG>
    operators:
      - type: json_parser
        parse_from: body
        timestamp:
          parse_from: attributes.timestamp
          layout: "%Y-%m-%d %H:%M:%S.%L %Z"

processors:
  transform/logs:
    log_statements:
      - context: resource
        statements:
          - set(attributes["datadog.host.name"], "<HOST>")
          - set(attributes["datadog.log.source"], "postgresql")

  batch: {}
```

### Service pipelines{% #service-pipelines %}

```yaml
service:
  pipelines:
    metrics/pg-host-1:
      receivers: [postgresql/pg-host-1]
      processors: [resource/pg-host-1, cumulativetodelta, deltatorate]
      exporters: [datadog/exporter]
```

If you configured the host metrics receiver, add it to a separate metrics pipeline:

```yaml
    metrics/host:
      receivers: [hostmetrics]
      processors: [cumulativetodelta]
      exporters: [datadog/exporter]
```

If you configured the file log receiver, add a logs pipeline:

```yaml
    logs:
      receivers: [filelog]
      processors: [transform/logs, batch]
      exporters: [datadog/exporter]
```

## Data collected{% #data-collected %}

| OTEL                                  | DATADOG                                   | DESCRIPTION                                                                                                                     | FILTER                    |
| ------------------------------------- | ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- | ------------------------- |
| postgresql.backends                   | postgresql.connections                    | The number of backends.                                                                                                         |
| postgresql.bgwriter.buffers.allocated | postgresql.bgwriter.buffers_alloc         | Number of buffers allocated.                                                                                                    |
| postgresql.bgwriter.buffers.writes    | postgresql.bgwriter.buffers_backend       | Number of buffers written.                                                                                                      | `source`: `backend`       |
| postgresql.bgwriter.buffers.writes    | postgresql.bgwriter.buffers_backend_fsync | Number of buffers written.                                                                                                      | `source`: `backend_fsync` |
| postgresql.bgwriter.buffers.writes    | postgresql.bgwriter.buffers_checkpoint    | Number of buffers written.                                                                                                      | `source`: `checkpoints`   |
| postgresql.bgwriter.buffers.writes    | postgresql.bgwriter.buffers_clean         | Number of buffers written.                                                                                                      | `source`: `bgwriter`      |
| postgresql.bgwriter.checkpoint.count  | postgresql.bgwriter.checkpoints_timed     | The number of checkpoints performed.                                                                                            | `type`: `scheduled`       |
| postgresql.bgwriter.checkpoint.count  | postgresql.bgwriter.checkpoints_requested | The number of checkpoints performed.                                                                                            | `type`: `requested`       |
| postgresql.bgwriter.duration          | postgresql.bgwriter.sync_time             | Total time spent writing and syncing files to disk by checkpoints.                                                              | `type`: `sync`            |
| postgresql.bgwriter.duration          | postgresql.bgwriter.write_time            | Total time spent writing and syncing files to disk by checkpoints.                                                              | `type`: `write`           |
| postgresql.bgwriter.maxwritten        | postgresql.bgwriter.maxwritten_clean      | Number of times the background writer stopped a cleaning scan because it had written too many buffers.                          |
| postgresql.blks_hit                   | postgresql.buffer_hit                     | Number of times disk blocks were found already in the buffer cache.                                                             |
| postgresql.blks_read                  | postgresql.disk_read                      | Number of disk blocks read in this database.                                                                                    |
| postgresql.blocks_read                | postgresql.heap_blocks_read               | The number of blocks read.                                                                                                      | `source`: `heap_read`     |
| postgresql.blocks_read                | postgresql.heap_blocks_hit                | The number of blocks read.                                                                                                      | `source`: `heap_hit`      |
| postgresql.blocks_read                | postgresql.index_blocks_read              | The number of blocks read.                                                                                                      | `source`: `idx_read`      |
| postgresql.blocks_read                | postgresql.index_blocks_hit               | The number of blocks read.                                                                                                      | `source`: `idx_hit`       |
| postgresql.blocks_read                | postgresql.toast_blocks_read              | The number of blocks read.                                                                                                      | `source`: `toast_read`    |
| postgresql.blocks_read                | postgresql.toast_blocks_hit               | The number of blocks read.                                                                                                      | `source`: `toast_hit`     |
| postgresql.blocks_read                | postgresql.toast_index_blocks_read        | The number of blocks read.                                                                                                      | `source`: `tidx_read`     |
| postgresql.blocks_read                | postgresql.toast_index_blocks_hit         | The number of blocks read.                                                                                                      | `source`: `tidx_hit`      |
| postgresql.commits                    | postgresql.commits                        | The number of commits.                                                                                                          |
| postgresql.connection.max             | postgresql.max_connections                | Configured maximum number of client connections allowed                                                                         |
| postgresql.database.count             | postgresql.db.count                       | Number of user databases.                                                                                                       |
| postgresql.database.locks             | postgresql.locks                          | The number of database locks.                                                                                                   |
| postgresql.db_size                    | postgresql.database_size                  | The database disk usage.                                                                                                        |
| postgresql.deadlocks                  | postgresql.deadlocks.count                | The number of deadlocks.                                                                                                        |
| postgresql.function.calls             | postgresql.function.calls                 | The number of calls made to a function. Requires `track_functions=pl|all` in Postgres config.                                   |
| postgresql.index.scans                | postgresql.index_scans                    | The number of index scans on a table.                                                                                           |
| postgresql.index.size                 | postgresql.individual_index_size          | The size of the index on disk.                                                                                                  |
| postgresql.operations                 | postgresql.rows_hot_updated               | The number of db row operations.                                                                                                | `operation`: `hot_upd`    |
| postgresql.replication.data_delay     | postgresql.replication_delay_bytes        | The amount of data delayed in replication.                                                                                      |
| postgresql.rollbacks                  | postgresql.rollbacks                      | The number of rollbacks.                                                                                                        |
| postgresql.rows                       | postgresql.dead_rows                      | The number of rows in the database.                                                                                             | `state`: `dead`           |
| postgresql.rows                       | postgresql.live_rows                      | The number of rows in the database.                                                                                             | `state`: `live`           |
| postgresql.sequential_scans           | postgresql.seq_scans                      | The number of sequential scans.                                                                                                 |
| postgresql.table.count                | postgresql.table.count                    | Number of user tables in a database.                                                                                            |
| postgresql.table.size                 | postgresql.table_size                     | Disk space used by a table.                                                                                                     |
| postgresql.table.vacuum.count         | postgresql.vacuumed                       | Number of times a table has manually been vacuumed.                                                                             |
| postgresql.temp.io                    | postgresql.temp_bytes                     | Total amount of data written to temporary files by queries.                                                                     |
| postgresql.temp_files                 | postgresql.temp_files                     | The number of temp files.                                                                                                       |
| postgresql.tup_deleted                | postgresql.rows_deleted                   | Number of rows deleted by queries in the database.                                                                              |
| postgresql.tup_fetched                | postgresql.rows_fetched                   | Number of rows fetched by queries in the database.                                                                              |
| postgresql.tup_inserted               | postgresql.rows_inserted                  | Number of rows inserted by queries in the database.                                                                             |
| postgresql.tup_returned               | postgresql.rows_returned                  | Number of rows returned by queries in the database.                                                                             |
| postgresql.tup_updated                | postgresql.rows_updated                   | Number of rows updated by queries in the database.                                                                              |
| postgresql.wal.age                    | postgresql.wal_age                        | Age of the oldest WAL file.                                                                                                     |
| postgresql.wal.delay                  | postgresql.replication.wal_write_lag      | Time between flushing recent WAL locally and receiving notification that the standby server has completed an operation with it. | `operation`: `write`      |
| postgresql.wal.delay                  | postgresql.replication.wal_flush_lag      | Time between flushing recent WAL locally and receiving notification that the standby server has completed an operation with it. | `operation`: `flush`      |
| postgresql.wal.delay                  | postgresql.replication.wal_replay_lag     | Time between flushing recent WAL locally and receiving notification that the standby server has completed an operation with it. | `operation`: `replay`     |

See [OpenTelemetry Metrics Mapping](https://docs.datadoghq.com/opentelemetry/guide/metrics_mapping.md) for more information.

## Further reading{% #further-reading %}

- [Setting Up the OpenTelemetry Collector](https://docs.datadoghq.com/opentelemetry/collector_exporter.md)
