Skip to content

Commit 6c8e8ae

Browse files
committed
fix: use postgres_ai schema for pg_statistic view
The target-db/init.sql was creating pg_statistic view in public schema, but verify step checks for it in postgres_ai schema. This also updates the pgwatch metrics to use the correct schema reference.
1 parent 938dddf commit 6c8e8ae

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

config/pgwatch-prometheus/metrics.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,7 +1399,7 @@ metrics:
13991399
and a2.attnum = ic.attpos
14001400
) i
14011401
join pg_catalog.pg_namespace n on n.oid = i.relnamespace
1402-
join public.pg_statistic s on s.schemaname = n.nspname
1402+
join postgres_ai.pg_statistic s on s.schemaname = n.nspname
14031403
and s.tablename = i.attrelname
14041404
and s.attname = i.attname
14051405
group by 1,2,3,4,5,6,7,8,9,10,11,12
@@ -1474,7 +1474,7 @@ metrics:
14741474
from pg_attribute as att
14751475
join pg_class as tbl on att.attrelid = tbl.oid
14761476
join pg_namespace as ns on ns.oid = tbl.relnamespace
1477-
left join public.pg_statistic as s on s.schemaname=ns.nspname
1477+
left join postgres_ai.pg_statistic as s on s.schemaname=ns.nspname
14781478
and s.tablename = tbl.relname and s.inherited=false and s.attname=att.attname
14791479
left join pg_class as toast on tbl.reltoastrelid = toast.oid
14801480
where not att.attisdropped

config/target-db/init.sql

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,26 @@ create user monitor with password 'monitor_pass';
2020
grant connect on database target_database to monitor;
2121
grant usage on schema public to monitor;
2222

23-
-- Create a public view for pg_statistic access
24-
create or replace view public.pg_statistic as
25-
select
23+
-- Create postgres_ai schema and pg_statistic view (matches cli/sql/02.permissions.sql)
24+
create schema if not exists postgres_ai;
25+
grant usage on schema postgres_ai to pg_monitor;
26+
27+
create or replace view postgres_ai.pg_statistic as
28+
select
2629
n.nspname as schemaname,
2730
c.relname as tablename,
2831
a.attname,
2932
s.stanullfrac as null_frac,
3033
s.stawidth as avg_width,
3134
false as inherited
32-
from pg_statistic s
33-
join pg_class c on c.oid = s.starelid
34-
join pg_namespace n on n.oid = c.relnamespace
35-
join pg_attribute a on a.attrelid = s.starelid and a.attnum = s.staattnum
35+
from pg_catalog.pg_statistic s
36+
join pg_catalog.pg_class c on c.oid = s.starelid
37+
join pg_catalog.pg_namespace n on n.oid = c.relnamespace
38+
join pg_catalog.pg_attribute a on a.attrelid = s.starelid and a.attnum = s.staattnum
3639
where a.attnum > 0 and not a.attisdropped;
3740

3841
-- Grant specific access instead of all tables
39-
grant select on public.pg_statistic to pg_monitor;
42+
grant select on postgres_ai.pg_statistic to pg_monitor;
4043

4144
-- Grant access to monitoring views
4245
grant select on pg_stat_statements to monitor;

0 commit comments

Comments
 (0)