pgwatch2 Documentation

repository·master·Indexed 23 days ago

https://github.com/cybertec-postgresql/pgwatch2

A flexible, self-contained PostgreSQL metrics monitoring and dashboarding solution supporting PostgreSQL versions 9.0 through 16. It provides pre-configured Grafana dashboards and supports multiple metric storage schemas, including standard PostgreSQL tables, partitioned tables, and TimescaleDB. The tool includes capabilities for collecting OS-level metrics via PL/Python, deploying metric fetching helpers via rollout_helper.py, and importing external data such as vmstat logs.

Tokens
31.7K
Snippets
60
Records
150
Agent score
82%

What's inside pgwatch2

  1. Overview of pgwatch2 main features

    master

    pgwatch2 is a monitoring solution for PostgreSQL that offers several key capabilities:

    • Non-invasive PostgreSQL setup: Base functionality requires no extensions or superuser rights.
    • Extensive Metric Coverage: Includes preset configurations for PostgreSQL Statistics Collector data and supports custom business-domain metrics defined in pure SQL.
    • Flexible Storage: Supports PostgreSQL, PostgreSQL with TimescaleDB, InfluxDB, Graphite, or Prometheus scraping.
    • Deployment Models: Supports "push" and "pull" models using PostgreSQL configuration DB, YAML, or ENV configuration.
    • Scalable Monitoring: Can monitor all, single, or a subset (via list or regex) of databases within a PostgreSQL instance.
    • Advanced Integrations: Supports PgBouncer, Pgpool2, AWS RDS, and Patroni with automatic member discovery.
    • Observability & Security: Includes an internal health-check API (default port 8081), SSL connection support, and password encryption for connection strings.
    • Extended Monitoring: Supports log parsing for error detection and OS-level metrics via PL/Python helper stored procedures.
    • Connectivity Testing: Includes a Ping mode to verify connectivity to all monitored databases.
  2. Define custom SQL metrics

    master

    Metrics in pgwatch2 are SQL queries. They can be version-specific and can account for the database's recovery state (primary vs standby).

    Data Storage:

    • Query output is automatically stored in the metrics database.
    • If a column name is prefixed with tag_, that column is also indexed as a tag.

    Example Custom Metric:

    SELECT
      (extract(epoch from now()) * 1e9)::int8 as epoch_ns,
      extract(epoch from (now() - pg_postmaster_start_time()))::int8 as postmaster_uptime_s,
      case when pg_is_in_recovery() then 1 else 0 end as in_recovery_int;
  3. Understand the pgwatch2 architecture and components

    master

    pgwatch2 is a monitoring solution designed to glue together proven software for metrics storage and dashboarding. The core components are:

    • Metrics gathering daemon (Collector): The mandatory Go-based component. It reads configuration, fetches metrics from target databases via SQL queries, and stores them in a destination (DB or Prometheus endpoint).
    • Configuration store: Defines which databases to monitor, the frequency, and the specific metrics. Supported modes include:
      • PostgreSQL: A schema with 5 tables.
      • File-based: YAML config files and SQL metric definition files.
      • ENV-based: Ad-hoc configuration using a connection string (JDBC or Libpq), ideal for containers.
    • Metrics storage DB: Where gathered data is kept. Options include InfluxDB, PostgreSQL (requires JSONB, v9.4+), TimescaleDB, Prometheus, Graphite, or plain JSON files.
    • Web UI (Optional): Used for administering monitoring configuration and basic data analysis. Note: The Web UI requires the configuration to be stored in a PostgreSQL database.
    • Metrics representation: Typically handled by Grafana, using predefined dashboards for Postgres and InfluxDB.
  4. Dynamic configuration updates in pgwatch2

    master

    pgwatch2 supports dynamic management of monitored databases, metrics, and intervals without requiring a restart or redeployment. The system scans the Configuration DB or YAML/SQL files every 2 minutes by default to apply changes.

    You can adjust the scanning frequency using the --servers-refresh-loop-seconds parameter.

  5. Choose a metric storage schema type

    master

    pgwatch2 supports several schema architectures depending on your scale and PostgreSQL version:

    • metric: A single/separate table for each distinct metric in the public schema. No partitioning. Works on all PG versions. Suitable for up to ~25 monitored DBs.
    • metric-time: A single top-level table per metric in public + weekly partitions in the subpartitions schema. Requires PG 11+. Suitable for up to ~50 monitored DBs. Reduces IO by dropping old partitions instead of deleting rows. This is the default for the pgwatch2-postgres Docker image.
    • metric-dbname-time: A single top-level table per metric in public + two levels of subpartitions (dbname + weekly time) in subpartitions. Requires PG 11+. Best for 50+ monitored DBs or when you need fast queries for specific databases on slow disks.
    • custom: All data goes into public.metrics. You are responsible for all partition management, table creation, and data cleanup.
    • timescale: Uses the TimescaleDB extension (v1.7+). Best for hundreds of databases or long retention periods due to 3x-10x compression. Note: Realtime metrics still use the metric-time schema because TimescaleDB does not support unlogged tables.
  6. Operate pgwatch2 using a Config DB (Central Pull Mode)

    master
    In this mode, pgwatch2 uses a central PostgreSQL database as its configuration source. This database holds connection strings, metric definition SQL, and preset configurations. This is the default approach used by the standard Docker images. It requires a small schema to be rolled out on any Postgres database accessible to the metrics gathering daemon.
  7. Use Prometheus mode for metrics gathering

    master

    Starting from v1.6.0, pgwatch2 supports Prometheus as a datastore. In this mode, the pgwatch2 metrics collector does not perform interval-based fetching. Instead, it acts as a target that listens for scrape requests (defaulting to port 9187, which is configurable) initiated by a Prometheus server.

    Deployment Pattern: When using Prometheus mode, pgwatch2 agents should be deployed on each database host separately rather than running a single central collector. This aligns with Prometheus best practices and prevents scrape timeouts.

    To use this mode, set the datastore parameter to prometheus and use the prometheus Preset Config.

  8. Monitor managed cloud databases with pgwatch2

    master

    When monitoring managed cloud services (Google Cloud SQL, Amazon RDS, Azure, Aiven), certain functionalities like file system access and untrusted PL-languages (e.g., Python) are disallowed. This results in the loss of some metrics and 'helper functions' compared to a standard on-site setup.

    To handle these limitations:

    • Use 'lighter' dashboards: Instead of standard dashboards like "System Stats" or "DB overview" (which may display errors due to missing permissions), use dashboards specifically tailored for unprivileged environments, such as "DB overview Unprivileged".
    • Integrate OS metrics: Since OS-level helpers are unavailable, you can integrate OS metrics into Grafana using the provider's specific data source (e.g., Stackdriver, CloudWatch, or Azure Monitor).
    • Add custom metrics: You can still add custom business metrics using plain SQL (e.g., tracking sales orders).
  9. Understand when metric fetching helpers are required

    master

    Metric fetching helpers (PL/Python wrappers) are primarily used to provide unprivileged users with access to statistics that otherwise require superuser permissions.

    Key considerations:

    • PostgreSQL v10+: Most helpers are unnecessary because you can use the built-in pg_monitor role, which provides access to Statistics Collector views without superuser privileges. However, some out-of-the-box metrics still rely on helpers as a first attempt before falling back to direct access.
    • OS Statistics: To gather CPU, IO, and disk metrics, helpers based on the psutil Python package are used. This requires a minimum Kernel version of 3.3. Note that SQL helpers are defined for Python 3 (LANGUAGE plpython3u); if using Python 2, you must manually change the language declaration.
    • Push Model (Local Gatherer): If running the gatherer locally in a "push" configuration, helpers are mostly unnecessary because the superuser can be used safely. You can also use the --direct-os-stats parameter (available since v1.8.4) to fetch psutil_* metrics directly from OS counters, falling back to PL/Python only if direct fetching fails.
    • Upgrades: If you perform a binary upgrade via pg_upgrade on a cluster that has helpers installed, you may encounter errors. To fix this, drop the failing helpers on the old cluster and re-create them after the upgrade.
  10. What are metrics in pgwatch2

    master

    Metrics are named SQL queries that return a timestamp and other useful data points. pgwatch2 automatically selects the correct version of a metric definition by checking the target database's PostgreSQL version, recovery state (primary vs. replica), and whether the monitoring user is a superuser.

    Special Built-in Metrics

    • change_events: Tracks DDL and configuration changes. It relies on internal *_hashes metrics which should not be removed.
    • recommendations: When enabled (interval > 0), it executes all metrics starting with reco_* to identify performance or security best practices violations.
    • server_log_event_counts: Enables Postgres server log
    -- a sample metric
    SELECT
      (extract(epoch from now()) * 1e9)::int8 as epoch_ns,
      extract(epoch from (now() - pg_postmaster_start_time()))::int8 as postmaster_uptime_s,
      case when pg_is_in_recovery() then 1 else 0 end as in_recovery_int;
  11. Understand pgwatch2 deployment architectures

    master

    pgwatch2 supports two primary deployment models depending on your environment:

    Typical "pull" architecture

    In a standard setup, a central pgwatch2 instance fetches metrics from a set of PostgreSQL databases. These databases are configured via a central Configuration DB which holds the connection strings and metric definitions.

    Typical "push" architecture

    For highly dynamic or Cloud environments, a de-centralized "push" approach is often a better fit. In this model, you only need the pgwatch2 metrics collection daemon to push metrics to a storage backend or expose them over a port for remote scraping.