-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgres-init.sql
More file actions
37 lines (32 loc) · 1.17 KB
/
postgres-init.sql
File metadata and controls
37 lines (32 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
-- Create Datadog user with minimal required permissions
CREATE
USER datadog WITH PASSWORD 'datadog';
GRANT pg_monitor TO datadog;
GRANT SELECT ON pg_stat_database TO datadog;
-- Ensure the pg_stat_statements extension is available for query metrics
CREATE
EXTENSION IF NOT EXISTS pg_stat_statements;
-- Create the datadog schema used by helper functions
CREATE SCHEMA IF NOT EXISTS datadog AUTHORIZATION postgres;
REVOKE ALL ON SCHEMA datadog FROM PUBLIC;
GRANT
USAGE
ON
SCHEMA
datadog TO datadog;
-- Helper function used by Datadog DBM to collect EXPLAIN plans
-- SECURITY DEFINER allows the datadog user to execute EXPLAIN safely without broad privileges
CREATE
OR REPLACE FUNCTION datadog.explain_statement(l_query TEXT)
RETURNS SETOF JSON AS $$
BEGIN
RETURN QUERY EXECUTE FORMAT('EXPLAIN (FORMAT JSON) %s', l_query);
END;
$$
LANGUAGE plpgsql SECURITY DEFINER;
-- Restrict the function search_path to prevent function hijacking
ALTER FUNCTION datadog.explain_statement(TEXT) SET search_path = pg_catalog;
COMMENT
ON SCHEMA datadog IS 'Schema for Datadog helper functions';
COMMENT
ON FUNCTION datadog.explain_statement(TEXT) IS 'Used by Datadog to collect EXPLAIN plans (FORMAT JSON)';