Skip to content

Commit d38d666

Browse files
authored
[+] improve development experience with docker compose (#927)
* [+] improve development experience with docker compose - add "demo_pgbouncer" and "demo_pgpool" test monitored sources - add "debug" preset with all built-in - fix "pgpool" and "pgbouncer" compose service - fix "postgres-standby" service init * [*] remove `pgbouncer` and `pgpool` metrics from debug preset * [-] fix `buffercache_by_db`, `buffercache_by_type` metrics, closes #928 * [-] fix `pgstattuple` dependent metrics, closes #929 * [-] fix pgpool dashboard layout
1 parent b38f62d commit d38d666

File tree

7 files changed

+159
-43
lines changed

7 files changed

+159
-43
lines changed

README.md

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ For the fastest development and deployment experience the Docker compose files a
5050
docker compose -f ./docker/docker-compose.yml build
5151
5252
docker compose -f ./docker/docker-compose.yml up --detach
53+
54+
docker/compose.add-test-db.sh
5355
```
5456

5557
These commands will build and start services listed in the compose file:
@@ -70,22 +72,7 @@ These commands will build and start services listed in the compose file:
7072
After start, you could open the [monitoring dashboard](http://localhost:3000/) and start
7173
looking at metrics.
7274

73-
To add a test database under monitoring, you can use [built-in WebUI](http://localhost:8080/). Or simply
74-
execute from command line:
75-
76-
```shell
77-
docker/compose.add-test-db.sh
78-
```
79-
80-
```console
81-
CREATE EXTENSION
82-
CREATE EXTENSION
83-
CREATE FUNCTION
84-
GRANT
85-
GRANT
86-
GRANT
87-
INSERT 0 1
88-
```
75+
To add a test database under monitoring, you can use [built-in WebUI](http://localhost:8080/).
8976

9077
## Produce Workload
9178

docker/compose.add-test-db.sh

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@ cd "$(dirname "${BASH_SOURCE[0]}")"
55
export MSYS_NO_PATHCONV=1
66

77
# We want to pipe the output of the `pgwatch metric print-init` command to the `psql` command
8-
docker compose exec -T pgwatch /pgwatch/pgwatch metric print-init full | \
8+
docker compose exec -T pgwatch /pgwatch/pgwatch metric print-init debug | \
99
docker compose exec -T -i postgres psql -d pgwatch -v ON_ERROR_STOP=1
1010

11-
docker compose exec -T postgres psql -d pgwatch -v ON_ERROR_STOP=1 -c \
12-
"INSERT INTO pgwatch.source (name, preset_config, connstr) VALUES
13-
('demo', 'full', 'postgresql://pgwatch:pgwatchadmin@postgres/pgwatch'),
14-
('demo_standby', 'full', 'postgresql://pgwatch:pgwatchadmin@postgres-standby/pgwatch')"
15-
16-
11+
docker compose exec -T pgwatch /pgwatch/pgwatch metric print-init full | \
12+
docker compose exec -T -i postgres psql -d pgwatch_metrics -v ON_ERROR_STOP=1
1713

14+
docker compose exec -T postgres psql -d pgwatch -v ON_ERROR_STOP=1 -c \
15+
"TRUNCATE pgwatch.source CASCADE;
16+
INSERT INTO pgwatch.source
17+
(name, dbtype, preset_config, connstr)
18+
VALUES
19+
('demo', 'postgres', 'debug', 'postgresql://pgwatch:pgwatchadmin@postgres/pgwatch'),
20+
('demo_metrics', 'postgres', 'full', 'postgresql://pgwatch:pgwatchadmin@postgres/pgwatch_metrics'),
21+
('demo_standby', 'postgres', 'full', 'postgresql://pgwatch:pgwatchadmin@postgres-standby/pgwatch'),
22+
('demo_pgbouncer', 'pgbouncer', 'pgbouncer', 'postgresql://pgwatch:pgwatchadmin@pgbouncer/pgbouncer'),
23+
('demo_pgpool', 'pgpool', 'pgpool', 'postgresql://pgwatch:pgwatchadmin@pgpool/pgwatch');"

docker/compose.pgbouncer.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ services:
33
image: bitnami/pgbouncer:latest
44
container_name: pgbouncer
55
ports:
6-
- "6432:6432"
6+
- "6432:5432"
77
environment:
88
POSTGRESQL_HOST: postgres
9+
POSTGRESQL_PORT: 5432
10+
POSTGRESQL_USERNAME: pgwatch
11+
POSTGRESQL_PASSWORD: pgwatchadmin
12+
POSTGRESQL_DATABASE: pgwatch
913
PGBOUNCER_AUTH_TYPE: trust
14+
PGBOUNCER_POOL_MODE: session
15+
PGBOUNCER_PORT: 5432
1016
depends_on:
1117
postgres:
1218
condition: service_healthy

docker/compose.pgpool.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ services:
99
PGPOOL_SR_CHECK_USER: postgres
1010
PGPOOL_SR_CHECK_PASSWORD: standbypass
1111
PGPOOL_ENABLE_LDAP: no
12-
PGPOOL_POSTGRES_USERNAME: postgres
13-
PGPOOL_POSTGRES_PASSWORD: adminpassword
14-
PGPOOL_ADMIN_USERNAME: admin
15-
PGPOOL_ADMIN_PASSWORD: adminpassword
12+
PGPOOL_POSTGRES_USERNAME: pgwatch
13+
PGPOOL_POSTGRES_PASSWORD: pgwatchadmin
14+
PGPOOL_ADMIN_USERNAME: pgwatch
15+
PGPOOL_ADMIN_PASSWORD: pgwatchadmin
1616
depends_on:
1717
postgres:
1818
condition: service_healthy

docker/compose.postgres.yml

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,36 @@ services:
4141
depends_on:
4242
postgres:
4343
condition: service_healthy
44-
command: >
45-
bash -c "psql -h postgres -c \"SELECT * FROM pg_create_physical_replication_slot('standby_slot')\" ||
46-
rm -rf /var/lib/postgresql/data/* &&
47-
pg_basebackup -h postgres --pgdata=/var/lib/postgresql/data --wal-method=stream --progress --write-recovery-conf --slot=standby_slot &&
48-
chmod 0700 /var/lib/postgresql/data &&
49-
postgres -cshared_preload_libraries=pg_stat_statements -cpg_stat_statements.track=all -ctrack_io_timing=on -ctrack_functions=pl"
44+
command: |
45+
bash -c "
46+
echo 'Setting up standby server...'
47+
export PGDATA=/var/lib/postgresql/standby
48+
49+
# Check if data directory already exists and is initialized
50+
if [ -f /var/lib/postgresql/standby/PG_VERSION ]; then
51+
echo 'Data directory already exists and is initialized, starting PostgreSQL...'
52+
else
53+
echo 'Initializing new standby server...'
54+
55+
# Create replication slot if it doesn't exist
56+
psql -h postgres -U postgres -c \"SELECT * FROM pg_create_physical_replication_slot('standby_slot');\" 2>/dev/null || true
57+
58+
# Create custom data directory with correct permissions
59+
mkdir -p /var/lib/postgresql/standby
60+
chmod 0700 /var/lib/postgresql/standby
61+
62+
# Remove any existing files to ensure clean backup
63+
rm -rf /var/lib/postgresql/standby/* || true
64+
65+
# Create base backup directly to our custom directory
66+
pg_basebackup -h postgres -U postgres -D /var/lib/postgresql/standby -Xs -P -R -S standby_slot
67+
68+
# Ensure correct permissions after backup
69+
chmod 0700 /var/lib/postgresql/standby
70+
fi
71+
72+
# Start PostgreSQL with custom data directory
73+
exec postgres -D /var/lib/postgresql/standby -c shared_preload_libraries=pg_stat_statements -c pg_stat_statements.track=all -c track_io_timing=on -c track_functions=pl
74+
"
5075
links:
5176
- postgres

grafana/postgres/v12/pgpool-stats.json

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"editable": true,
1919
"fiscalYearStartMonth": 0,
2020
"graphTooltip": 0,
21-
"id": 33,
21+
"id": 10,
2222
"links": [],
2323
"panels": [
2424
{
@@ -44,7 +44,8 @@
4444
"mode": "absolute",
4545
"steps": [
4646
{
47-
"color": "green"
47+
"color": "green",
48+
"value": 0
4849
},
4950
{
5051
"color": "red",
@@ -79,7 +80,8 @@
7980
"mode": "absolute",
8081
"steps": [
8182
{
82-
"color": "rgba(245, 54, 54, 0.9)"
83+
"color": "rgba(245, 54, 54, 0.9)",
84+
"value": 0
8385
},
8486
{
8587
"color": "rgba(237, 129, 40, 0.89)",
@@ -130,7 +132,7 @@
130132
},
131133
"showHeader": true
132134
},
133-
"pluginVersion": "12.0.0",
135+
"pluginVersion": "12.1.0",
134136
"targets": [
135137
{
136138
"datasource": {
@@ -223,7 +225,8 @@
223225
"mode": "absolute",
224226
"steps": [
225227
{
226-
"color": "green"
228+
"color": "green",
229+
"value": 0
227230
},
228231
{
229232
"color": "red",
@@ -257,7 +260,7 @@
257260
"sort": "desc"
258261
}
259262
},
260-
"pluginVersion": "12.0.0",
263+
"pluginVersion": "12.1.0",
261264
"targets": [
262265
{
263266
"datasource": {
@@ -299,8 +302,8 @@
299302
"overrides": []
300303
},
301304
"gridPos": {
302-
"h": 6,
303-
"w": 24,
305+
"h": 4,
306+
"w": 10,
304307
"x": 0,
305308
"y": 19
306309
},
@@ -314,7 +317,7 @@
314317
"content": "### Brought to you by\n\n[![Cybertec – The PostgreSQL Database Company](https://www.cybertec-postgresql.com/wp-content/uploads/2025/02/cybertec-logo-white-blue.svg)](https://www.cybertec-postgresql.com/en/)\n",
315318
"mode": "markdown"
316319
},
317-
"pluginVersion": "12.0.0",
320+
"pluginVersion": "12.1.0",
318321
"title": "",
319322
"transparent": true,
320323
"type": "text"

internal/metrics/metrics.yaml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ metrics:
294294
Retrieves buffer cache statistics grouped by database, providing insights into the size of buffers used by each database.
295295
It calculates the total size of buffers in bytes for each database.
296296
This metric helps administrators monitor buffer usage across different databases in the PostgreSQL instance.
297+
init_sql: |-
298+
create extension if not exists pg_buffercache;
297299
sqls:
298300
11: |-
299301
select /* pgwatch_generated */
@@ -316,6 +318,8 @@ metrics:
316318
by different relation kinds. It calculates the total size of buffers in bytes for each relation kind
317319
(e.g., Table, Index, Toast, Materialized view). This metric helps administrators monitor buffer usage across
318320
different relation types in the PostgreSQL instance.
321+
init_sql: |-
322+
create extension if not exists pg_buffercache;
319323
sqls:
320324
11: |-
321325
select /* pgwatch_generated */
@@ -3222,6 +3226,8 @@ metrics:
32223226
description: >
32233227
This metric collects approximate table bloat statistics using the `pgstattuple_approx` function.
32243228
It provides insights into the amount of free space and dead tuples in tables, which can help in identifying bloat issues.
3229+
init_sql: |-
3230+
create extension if not exists pgstattuple;
32253231
sqls:
32263232
11: |-
32273233
select /* pgwatch_generated */
@@ -3247,6 +3253,8 @@ metrics:
32473253
description: >
32483254
This metric provides a summary of approximate table bloat statistics, including the total bloat size and percentage for the current database.
32493255
It aggregates data from multiple tables to give an overview of bloat across the database.
3256+
init_sql: |-
3257+
create extension if not exists pgstattuple;
32503258
sqls:
32513259
11: |-
32523260
/* accessing pgstattuple_approx directly requires superuser or pg_stat_scan_tables/pg_monitor builtin roles or
@@ -3284,6 +3292,8 @@ metrics:
32843292
description: >
32853293
This metric provides a summary of approximate table bloat statistics, including the total bloat size and percentage for the current database.
32863294
It aggregates data from multiple tables to give an overview of bloat across the database.
3295+
init_sql: |-
3296+
create extension if not exists pgstattuple;
32873297
sqls:
32883298
11: |
32893299
WITH q_bloat AS (
@@ -4912,3 +4922,82 @@ presets:
49124922
table_io_stats: 600
49134923
table_stats: 300
49144924
wal: 60
4925+
debug:
4926+
description: all available metrics with 30 second intervals for debugging and development
4927+
metrics:
4928+
archiver: 30
4929+
archiver_pending_count: 30
4930+
backends: 30
4931+
backup_age_pgbackrest: 30
4932+
backup_age_walg: 30
4933+
bgwriter: 30
4934+
blocking_locks: 30
4935+
buffercache_by_db: 30
4936+
buffercache_by_type: 30
4937+
change_events: 30
4938+
checkpointer: 30
4939+
configuration_hashes: 30
4940+
cpu_load: 30
4941+
database_conflicts: 30
4942+
datfrozenxid: 30
4943+
db_size: 30
4944+
db_size_approx: 30
4945+
db_stats: 30
4946+
db_stats_aurora: 30
4947+
index_hashes: 30
4948+
index_stats: 30
4949+
instance_up: 30
4950+
invalid_indexes: 30
4951+
kpi: 30
4952+
locks: 30
4953+
locks_mode: 30
4954+
logical_subscriptions: 30
4955+
postgres_role: 30
4956+
privilege_changes: 30
4957+
psutil_cpu: 30
4958+
psutil_disk: 30
4959+
psutil_disk_io_total: 30
4960+
psutil_mem: 30
4961+
reco_add_index: 30
4962+
reco_default_public_schema: 30
4963+
reco_disabled_triggers: 30
4964+
reco_drop_index: 30
4965+
reco_nested_views: 30
4966+
reco_partial_index_candidates: 30
4967+
reco_sprocs_wo_search_path: 30
4968+
reco_superusers: 30
4969+
replication: 30
4970+
replication_slot_stats: 30
4971+
replication_slots: 30
4972+
sequence_health: 30
4973+
server_log_event_counts: 30
4974+
settings: 30
4975+
smart_health_per_disk: 30
4976+
sproc_hashes: 30
4977+
sproc_stats: 30
4978+
stat_activity: 30
4979+
stat_io: 30
4980+
stat_ssl: 30
4981+
stat_statements: 30
4982+
stat_statements_calls: 30
4983+
stat_statements_no_query_text: 30
4984+
subscription_stats: 30
4985+
table_bloat_approx_stattuple: 30
4986+
table_bloat_approx_summary: 30
4987+
table_bloat_approx_summary_sql: 30
4988+
table_hashes: 30
4989+
table_io_stats: 30
4990+
table_stats: 30
4991+
table_stats_approx: 30
4992+
unused_indexes: 30
4993+
vmstat: 30
4994+
wait_events: 30
4995+
wal: 30
4996+
wal_receiver: 30
4997+
wal_size: 30
4998+
wal_stats: 30
4999+
# pgbouncer and pgpool metrics need separate source settings
5000+
# pgbouncer_clients: 30
5001+
# pgbouncer_stats: 30
5002+
# pgpool_processes: 30
5003+
# pgpool_stats: 30

0 commit comments

Comments
 (0)