-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg_sysinfo--1.0.sql
106 lines (91 loc) · 3.07 KB
/
pg_sysinfo--1.0.sql
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
CREATE EXTENSION IF NOT EXISTS plpgsql;
CREATE TYPE sysinfo_data AS (
name text,
value text
);
CREATE OR REPLACE FUNCTION sysinfo_os_name()
RETURNS text
AS 'MODULE_PATHNAME', 'sysinfo_os_name'
LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION sysinfo_os_release()
RETURNS text
AS 'MODULE_PATHNAME', 'sysinfo_os_release'
LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION sysinfo_os_version()
RETURNS text
AS 'MODULE_PATHNAME', 'sysinfo_os_version'
LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION sysinfo_cpu
(
OUT cpu TEXT,
OUT num_cpu INT,
OUT num_core INT
)
RETURNS SETOF record
AS 'MODULE_PATHNAME', 'sysinfo_cpu'
LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION sysinfo_cpu_usage
(
OUT user_time float8,
OUT sys_time float8,
OUT total_time float8
)
RETURNS SETOF record
AS 'MODULE_PATHNAME', 'sysinfo_cpu_usage'
LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION sysinfo_disk
(
OUT mount TEXT,
OUT total_size float8,
OUT used_size float8,
OUT free_size float8
)
RETURNS SETOF record
AS 'MODULE_PATHNAME', 'sysinfo_disk'
LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION sysinfo_ram
(
OUT total_memory float8,
OUT used_memory float8,
OUT free_memory float8
)
RETURNS SETOF record
AS 'MODULE_PATHNAME', 'sysinfo_ram'
LANGUAGE C STRICT;
CREATE VIEW pg_osinfo AS SELECT * FROM sysinfo_os_name(), sysinfo_os_release(), sysinfo_os_version();
CREATE VIEW pg_cpuinfo AS SELECT * FROM sysinfo_cpu();
CREATE VIEW pg_cpu_usage AS SELECT * FROM sysinfo_cpu_usage();
CREATE VIEW pg_raminfo AS SELECT * FROM sysinfo_ram();
CREATE VIEW pg_diskinfo AS SELECT * FROM sysinfo_disk();
CREATE TABLE pg_sys_usage AS SELECT now(), * FROM pg_cpu_usage, pg_raminfo;
CREATE OR REPLACE FUNCTION pg_update_sys_usage()
RETURNS VOID AS $$
BEGIN
INSERT INTO pg_sys_usage (now, user_time, sys_time, total_time, total_memory, used_memory, free_memory)
SELECT now(), user_time, sys_time, total_time, total_memory, used_memory, free_memory FROM pg_cpu_usage, pg_raminfo;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION pg_adjust_setting()
RETURNS VOID AS $$
BEGIN
set_shared_buffers(get_shared_buffers() + 1024*1024);
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION get_shared_buffers() RETURNS integer AS $$
DECLARE
shared_buffers_setting text;
BEGIN
EXECUTE 'SHOW shared_buffers' INTO shared_buffers_setting;
RETURN regexp_replace(shared_buffers_setting, '[^0-9]', '', 'g')::integer;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION set_shared_buffers(size varchar)
RETURNS void AS
$$
BEGIN
EXECUTE 'ALTER SYSTEM SET shared_buffers = ' || size;
END;
$$
LANGUAGE plpgsql;
CREATE VIEW pg_suggested_settings AS SELECT (total_memory * 40) / 100 AS shared_buffer, (total_memory * 2) / 100 work_mem,
(total_memory * 4) / 100 as maintance_workmem FROM sysinfo_ram();