Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add function to monitor sequences #940

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions postgres-appliance/scripts/metric_helpers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,36 @@ $$ LANGUAGE sql IMMUTABLE SECURITY DEFINER STRICT;

CREATE OR REPLACE VIEW pg_stat_statements AS SELECT * FROM pg_stat_statements(true);

CREATE OR REPLACE FUNCTION get_nearly_exhausted_sequences(
IN threshold float,
OUT schemaname name,
OUT sequencename name,
OUT seq_percent_used numeric
) RETURNS SETOF record AS
$_$
SELECT *
FROM (
SELECT
schemaname,
sequencename,
round(abs(
ceil((abs(last_value::numeric - start_value) + 1) / increment_by) /
floor((CASE WHEN increment_by > 0
THEN (max_value::numeric - start_value)
ELSE (start_value::numeric - min_value)
END + 1) / increment_by
) * 100
),
2) AS seq_percent_used
FROM pg_sequences
WHERE NOT CYCLE AND last_value IS NOT NULL
) AS s
WHERE seq_percent_used >= threshold;
$_$
LANGUAGE sql SECURITY DEFINER STRICT SET search_path to 'pg_catalog';

CREATE OR REPLACE VIEW nearly_exhausted_sequences AS SELECT * FROM get_nearly_exhausted_sequences(0.8);

REVOKE ALL ON ALL TABLES IN SCHEMA metric_helpers FROM public;
GRANT SELECT ON ALL TABLES IN SCHEMA metric_helpers TO admin, robot_zmon;

Expand Down