🌐 AI搜索 & 代理 主页
Skip to content

Commit 889fdc3

Browse files
NikolaySclaude
andcommitted
style: Apply SQL style guide to template files
Changed all SQL keywords to lowercase per project SQL style guide: - create/grant/alter instead of CREATE/GRANT/ALTER - select/from/where/join instead of SELECT/FROM/WHERE/JOIN - Data types: timestamptz/text/jsonb instead of TIMESTAMPTZ/TEXT/JSONB - PL/pgSQL keywords: declare/begin/end/if/return (lowercase) Added explicit 'as' for aliases in queries. Improved formatting for multi-line statements with proper indentation. Follows .cursor/rules/development__db-sql-style-guide.mdc Related to MR !56 Co-Authored-By: Claude <noreply@anthropic.com>
1 parent ba5a25e commit 889fdc3

File tree

2 files changed

+93
-92
lines changed

2 files changed

+93
-92
lines changed
Lines changed: 57 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,76 @@
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();

config/target-db/init-template.sql

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,53 @@
11
-- Initialize target database for monitoring
22
-- Enable pg_stat_statements extension for query monitoring
3-
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
3+
create extension if not exists pg_stat_statements;
44

55
-- Create a sample table for demonstration
6-
CREATE TABLE IF NOT EXISTS sample_data (
7-
id SERIAL PRIMARY KEY,
8-
name VARCHAR(100),
9-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
6+
create table if not exists sample_data (
7+
id serial primary key,
8+
name varchar(100),
9+
created_at timestamp default current_timestamp
1010
);
1111

1212
-- Insert some sample data
13-
INSERT INTO sample_data (name) VALUES
14-
('Sample Record 1'),
15-
('Sample Record 2'),
16-
('Sample Record 3');
13+
insert into sample_data (name)
14+
values
15+
('Sample Record 1'),
16+
('Sample Record 2'),
17+
('Sample Record 3');
1718

18-
-- Create a user for PGWatch monitoring
19-
CREATE USER ${TARGET_MONITOR_USER} WITH PASSWORD '${TARGET_MONITOR_PASSWORD}';
20-
GRANT CONNECT ON DATABASE target_database TO ${TARGET_MONITOR_USER};
21-
GRANT USAGE ON SCHEMA public TO ${TARGET_MONITOR_USER};
19+
-- Create a user for pgwatch monitoring
20+
create user ${TARGET_MONITOR_USER} with password '${TARGET_MONITOR_PASSWORD}';
21+
grant connect on database target_database to ${TARGET_MONITOR_USER};
22+
grant usage on schema public to ${TARGET_MONITOR_USER};
2223

2324
-- Create a public view for pg_statistic access
24-
CREATE OR REPLACE VIEW public.pg_statistic AS
25-
SELECT
26-
n.nspname as schemaname,
27-
c.relname as tablename,
28-
a.attname,
29-
s.stanullfrac as null_frac,
30-
s.stawidth as avg_width,
31-
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
36-
WHERE a.attnum > 0 AND NOT a.attisdropped;
25+
create or replace view public.pg_statistic as
26+
select
27+
n.nspname as schemaname,
28+
c.relname as tablename,
29+
a.attname,
30+
s.stanullfrac as null_frac,
31+
s.stawidth as avg_width,
32+
false as inherited
33+
from pg_statistic as s
34+
join pg_class as c on c.oid = s.starelid
35+
join pg_namespace as n on n.oid = c.relnamespace
36+
join pg_attribute as a on a.attrelid = s.starelid and a.attnum = s.staattnum
37+
where
38+
a.attnum > 0
39+
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 public.pg_statistic to pg_monitor;
4043

4144
-- Grant access to monitoring views
42-
GRANT SELECT ON pg_stat_statements TO ${TARGET_MONITOR_USER};
43-
GRANT SELECT ON pg_stat_database TO ${TARGET_MONITOR_USER};
44-
GRANT SELECT ON pg_stat_user_tables TO ${TARGET_MONITOR_USER};
45+
grant select on pg_stat_statements to ${TARGET_MONITOR_USER};
46+
grant select on pg_stat_database to ${TARGET_MONITOR_USER};
47+
grant select on pg_stat_user_tables to ${TARGET_MONITOR_USER};
48+
4549
-- Grant pg_monitor role to monitor user for enhanced monitoring capabilities
46-
GRANT pg_monitor TO ${TARGET_MONITOR_USER};
50+
grant pg_monitor to ${TARGET_MONITOR_USER};
4751

4852
-- Set search path for the monitor user
49-
ALTER USER ${TARGET_MONITOR_USER} SET search_path = "$user", public, pg_catalog;
53+
alter user ${TARGET_MONITOR_USER} set search_path = "$user", public, pg_catalog;

0 commit comments

Comments
 (0)