1- -- Initialize PostgreSQL sink database for storing pgwatch measurements
2- -- This database will store all the monitoring metrics collected by PGWatch
1+ -- Initialize Postgres sink database for storing pgwatch measurements
2+ -- This database will store all the monitoring metrics collected by pgwatch
33-- Based on https://pgwat.ch/latest/howto/metrics_db_bootstrap.html
44
55-- Create the pgwatch role for measurements database
6- CREATE ROLE ${PGWATCH_MONITOR_USER} WITH LOGIN PASSWORD ' ${PGWATCH_MONITOR_PASSWORD}' ;
6+ create role ${PGWATCH_MONITOR_USER} with login password ' ${PGWATCH_MONITOR_PASSWORD}' ;
77
88-- Create the measurements database owned by pgwatch
9- CREATE DATABASE measurements OWNER ${PGWATCH_MONITOR_USER};
9+ create database measurements owner ${PGWATCH_MONITOR_USER};
1010
1111-- Switch to the measurements database context
1212\c measurements;
1313
1414-- Create extensions that might be useful for metrics storage
15- CREATE EXTENSION IF NOT EXISTS btree_gist;
16- CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
15+ create extension if not exists btree_gist;
16+ create extension if not exists pg_stat_statements;
1717
1818-- Grant necessary permissions to pgwatch user
19- GRANT ALL PRIVILEGES ON DATABASE measurements TO ${PGWATCH_MONITOR_USER};
20- GRANT ALL PRIVILEGES ON SCHEMA public TO ${PGWATCH_MONITOR_USER};
19+ grant all privileges on database measurements to ${PGWATCH_MONITOR_USER};
20+ grant all privileges on schema public to ${PGWATCH_MONITOR_USER};
2121
22-
23-
24- -- Create a partitioned table for queryid-to-query mappings with LIST partitioning by dbname
25- CREATE TABLE IF NOT EXISTS public .pgss_queryid_queries (
26- time TIMESTAMPTZ NOT NULL ,
27- dbname TEXT NOT NULL ,
28- data JSONB NOT NULL ,
29- tag_data JSONB
30- ) PARTITION BY LIST (dbname);
22+ -- Create a partitioned table for queryid-to-query mappings with list partitioning by dbname
23+ create table if not exists public .pgss_queryid_queries (
24+ time timestamptz not null ,
25+ dbname text not null ,
26+ data jsonb not null ,
27+ tag_data jsonb
28+ ) partition by list (dbname);
3129
3230-- Create indexes for efficient lookups
33- CREATE INDEX IF NOT EXISTS pgss_queryid_queries_dbname_time_idx ON public .pgss_queryid_queries (dbname, time );
34-
35- -- Use existing subpartitions schema
36-
31+ create index if not exists pgss_queryid_queries_dbname_time_idx
32+ on public .pgss_queryid_queries (dbname, time );
3733
3834-- Set ownership and grant permissions to pgwatch
39- ALTER TABLE public .pgss_queryid_queries OWNER TO ${PGWATCH_MONITOR_USER};
40- GRANT ALL PRIVILEGES ON TABLE public .pgss_queryid_queries TO ${PGWATCH_MONITOR_USER};
41- -- Ensure pgwatch can use sequences (if any are created)
42- GRANT USAGE ON SCHEMA public TO ${PGWATCH_MONITOR_USER};
43- -- Grant permissions on all future tables in public schema
44- ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO ${PGWATCH_MONITOR_USER};
45-
46- CREATE OR REPLACE FUNCTION enforce_queryid_uniqueness ()
47- RETURNS TRIGGER AS $$
48- DECLARE
49- queryid_value TEXT ;
50- BEGIN
51- -- Extract queryid from the data JSONB
52- queryid_value := NEW .data - >> ' queryid' ;
53-
54- -- Allow NULL queryids through
55- IF queryid_value IS NULL THEN
56- RETURN NEW;
57- END IF;
58-
59- -- Silently skip if duplicate exists
60- IF EXISTS (
61- SELECT 1
62- FROM pgss_queryid_queries
63- WHERE dbname = NEW .dbname
64- AND data- >> ' queryid' = queryid_value
65- LIMIT 1
66- ) THEN
67- RETURN NULL ; -- Cancels INSERT silently
68- END IF;
69-
70- RETURN NEW;
71- END;
72- $$ LANGUAGE plpgsql;
35+ alter table public .pgss_queryid_queries owner to ${PGWATCH_MONITOR_USER};
36+ grant all privileges on table public .pgss_queryid_queries to ${PGWATCH_MONITOR_USER};
7337
38+ -- Ensure pgwatch can use sequences (if any are created)
39+ grant usage on schema public to ${PGWATCH_MONITOR_USER};
7440
75- CREATE OR REPLACE TRIGGER enforce_queryid_uniqueness_trigger
76- BEFORE INSERT
77- ON pgss_queryid_queries
78- FOR EACH ROW
79- EXECUTE FUNCTION enforce_queryid_uniqueness();
41+ -- Grant permissions on all future tables in public schema
42+ alter default privileges in schema public grant all on tables to ${PGWATCH_MONITOR_USER};
43+
44+ create or replace function enforce_queryid_uniqueness ()
45+ returns trigger as $$
46+ declare
47+ queryid_value text ;
48+ begin
49+ -- Extract queryid from the data jsonb
50+ queryid_value := new .data - >> ' queryid' ;
51+
52+ -- Allow null queryids through
53+ if queryid_value is null then
54+ return new;
55+ end if;
56+
57+ -- Silently skip if duplicate exists
58+ if exists (
59+ select 1
60+ from pgss_queryid_queries
61+ where
62+ dbname = new .dbname
63+ and data- >> ' queryid' = queryid_value
64+ limit 1
65+ ) then
66+ return null ; -- Cancels insert silently
67+ end if;
68+
69+ return new;
70+ end;
71+ $$ language plpgsql;
72+
73+ create or replace trigger enforce_queryid_uniqueness_trigger
74+ before insert on pgss_queryid_queries
75+ for each row
76+ execute function enforce_queryid_uniqueness();
0 commit comments