Sign inSign up

postgresai/pgwatch2

By postgresai

•Updated about 1 year ago

pgwatch2 - Postgres.ai Edition

Image
Monitoring & observability
4

4.5K

postgresai/pgwatch2 repository overview

⁠pgwatch2 Postgres.ai Edition (a.k.a. pgaiwatch)

A modified version of pgwatch2⁠, a popular monitoring tool developed by Cybertec⁠.

Modifications:

⁠Demo: https://pgwatch.dblab.dev⁠
  • username: demo
  • password: demo
⁠Repository: https://gitlab.com/postgres-ai/pgwatch2⁠

⁠How to configure and run

⁠Extensions

For most monitored databases it’s extremely beneficial (to troubleshooting performance issues) to also activate the pg_stat_statements extension:

  • Adjust postgresql.conf
shared_preload_libraries = pg_stat_statements
  • Restart postgres

  • Create extension

create extension if not exists pg_stat_statements;

Or optional (can be omitted; should be omitted if these extensions are N/A, e.g., in case of RDS):

PG_VERSION=16; sudo apt install -y \
  postgresql-$PG_VERSION-pg-stat-kcache \
  postgresql-$PG_VERSION-pg-wait-sampling
  • Adjust postgresql.conf (do not include pg_stat_kcache and pg_wait_sampling if they are N/A, e.g., on RDS)
shared_preload_libraries = pg_stat_statements,pg_stat_kcache,pg_wait_sampling
  • Restart postgres

  • Create extensions

create extension if not exists pg_stat_statements;
create extension if not exists pg_stat_kcache; -- N/A on managed services such as RDS
create extension if not exists pg_wait_sampling; -- N/A on managed services such as RDS (available on CloudSQL!)

⁠Role and permissions

On Postgres hosts we want to observe:

  • DB user that will be used by pgwatch2 + permissions it needs
begin;

create role pgai_monitoring with login password '{{secure_password}}';

create schema pgai_monitoring;

create view pgai_monitoring.pg_statistic as
 select pg_statistic.stawidth,
    pg_statistic.stanullfrac,
    pg_statistic.starelid,
    pg_statistic.staattnum
   from pg_catalog.pg_statistic;

grant connect on database {{PROD_DB_NAME}} to pgai_monitoring;
grant pg_monitor to pgai_monitoring;
grant usage on schema public to pgai_monitoring;
grant usage on schema pgai_monitoring to pgai_monitoring;
grant select on pgai_monitoring.pg_statistic to pgai_monitoring;

alter user pgai_monitoring set search_path = pgai_monitoring, pg_catalog, public;

commit;

-- Environment-dependent steps
grant execute on function pg_ls_dir(text) to pgai_monitoring; -- skip it if you're on a managed Postgres such as RDS
grant execute on function pg_wait_sampling_reset_profile() to pgai_monitoring; -- if pg_wait_sampling is installed
  • Add pgwatch2 host to pg_hba.conf
⁠Instances

Copy and edit the instances.yaml configuration file (specify hosts you want to monitor):

sudo mkdir -p /etc/pgwatch2/config
sudo curl https://gitlab.com/postgres-ai/pgwatch2/-/raw/master/config/instances.yaml \
  --output /etc/pgwatch2/config/instances.yaml

sudo nano /etc/pgwatch2/config/instances.yaml
⁠Docker
sudo docker run -d --name pgwatch2-postgresai \
  -p 3000:3000 -p 8081:8081 \
  -v /etc/pgwatch2/config:/etc/pgwatch2/config:ro \
  -v pgwatch2:/etc/pgwatch2/persistent-config \
  -v pgwatch2_postgres:/var/lib/postgresql \
  -v pgwatch2_grafana:/var/lib/grafana \
  -e PW2_GRAFANANOANONYMOUS=true \
  -e PW2_GRAFANAUSER="admin" \
  -e PW2_GRAFANAPASSWORD="MY_SECRET_PASS" \
  -e PW2_DATASTORE="postgres" \
  -e PW2_PG_SCHEMA_TYPE="timescale" \
  -e PW2_PG_RETENTION_DAYS=14 \
  -e PW2_TIMESCALE_CHUNK_HOURS=1 \
  -e PW2_TIMESCALE_COMPRESS_HOURS=1 \
  --restart=unless-stopped \
  --shm-size=2g \
  postgresai/pgwatch2:latest
⁠Dashboards

To view the dashboards, navigate to http://<server-ip>:3000

⁠Troubleshooting

Log into the container and look at log files - they’re situated under /var/log/supervisor/

Example:

sudo docker exec pgwatch2-postgresai ls -lh /var/log/supervisor/
sudo docker exec pgwatch2-postgresai tail -n 50 /var/log/supervisor/pgwatch2-stderr---supervisor-21oji6p3.log

⁠Compatibility with RDS/Aurora

Most metrics are compatible with AWS RDS and Aurora, with the exception of wal_count⁠ and archiver_pending_count⁠ metrics, because these PostgreSQL versions restrict access to the pg_ls_dir function. This means that charts such as, WAL count and WAL Archive (pending_count) will not be available.
In addition, the wal⁠ metric (required for WAL rate chart) works in Aurora only if the wal_level parameter is set to logical. This is because the pg_current_wal_lsn function does not work in Aurora otherwise.

Tag summary

Content type

Image

Digest

sha256:bc46b9c9a…

Size

636.4 MB

Last updated

about 1 year ago

docker pull postgresai/pgwatch2