]> git.8kb.co.uk Git - postgresql/pg_settings_history/blob - pg_settings_history.plpgsql
Stop doing useless work
[postgresql/pg_settings_history] / pg_settings_history.plpgsql
1 -- 
2 -- Glyn Astill 02/01/2011
3 -- Function to schedule in order to keep a history of server run-time parameters
4 --
5
6 CREATE OR REPLACE FUNCTION public.log_pg_settings_history()
7 RETURNS boolean AS
8 $BODY$
9
10 DECLARE
11     v_changes integer;
12 BEGIN
13     -- Setup 2 tables to store the last snapshot of the settings and a history of changes
14     IF NOT EXISTS (SELECT 1 FROM pg_catalog.pg_tables WHERE schemaname = 'public' AND tablename = 'pg_settings_last') THEN
15         RAISE NOTICE 'A snapshot of the last contents of pg_settings will be stored in public.pg_settings_last';
16         CREATE TABLE public.pg_settings_last AS SELECT * FROM pg_settings;
17         ALTER TABLE pg_settings_last ADD COLUMN snapshot timestamp DEFAULT current_timestamp;
18
19         IF EXISTS (SELECT 1 FROM pg_catalog.pg_tables WHERE schemaname = 'public' AND tablename = 'pg_settings_history') THEN
20             RAISE EXCEPTION 'Please drop or rename table public.pg_settings_history and try again';
21         ELSE
22             RAISE NOTICE 'History of changes to pg_settings will be stored in public.pg_settings_history';
23             CREATE TABLE public.pg_settings_history (LIKE public.pg_settings_last);
24             ALTER TABLE pg_settings_history ADD COLUMN version varchar(3);
25             
26             CREATE OR REPLACE FUNCTION public.pg_settings_history_version() RETURNS trigger AS $TRIG$
27                 BEGIN
28                     IF NEW.snapshot = current_timestamp THEN 
29                         NEW.version = 'new';
30                     ELSE
31                         NEW.version = 'old';
32                 END IF;
33                 RETURN NEW;
34             END;
35             $TRIG$ LANGUAGE plpgsql;
36
37             CREATE TRIGGER g_settings_history_version_trigger
38             BEFORE INSERT ON public.pg_settings_history
39             FOR EACH ROW
40             EXECUTE PROCEDURE public.pg_settings_history_version();
41
42         END IF;
43
44         RETURN false;
45     ELSE 
46         -- Select out any changes since the last execution into our history table
47         INSERT INTO public.pg_settings_history 
48         WITH changed AS (
49             SELECT * FROM pg_settings
50             EXCEPT
51             SELECT name, setting, unit, category, short_desc, extra_desc, context, vartype, source, min_val, 
52                     max_val, enumvals, boot_val, reset_val, sourcefile, sourceline
53             FROM pg_settings_last
54         )
55         SELECT *, current_timestamp AS snapshot FROM pg_settings WHERE name IN (SELECT name FROM changed c)
56         UNION 
57         SELECT * FROM pg_settings_last  WHERE name IN (SELECT name FROM changed c)
58         ORDER BY name, snapshot;
59         
60         GET DIAGNOSTICS v_changes = ROW_COUNT;
61
62         IF (v_changes > 0) THEN
63             -- Update the snapshot ready for the next run.
64             TRUNCATE public.pg_settings_last;
65             INSERT INTO pg_settings_last (name, setting, unit, category, short_desc, extra_desc, context, vartype, source, min_val, 
66                 max_val, enumvals, boot_val, reset_val, sourcefile, sourceline)
67             SELECT * FROM pg_settings;
68
69             RETURN true;
70         ELSE 
71             RETURN false;
72         END IF; 
73     END IF;    
74 END;
75
76 $BODY$
77 LANGUAGE plpgsql VOLATILE;