-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg_profile--0.0.5--0.0.5a.sql
197 lines (190 loc) · 6.51 KB
/
pg_profile--0.0.5--0.0.5a.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
\echo Use "ALTER EXTENSION pg_profile UPDATE" to load this file. \quit
CREATE OR REPLACE FUNCTION collect_obj_stats(IN s_id integer) RETURNS integer SET search_path=@extschema@,public AS $$
DECLARE
--Cursor for db stats
c_dblist CURSOR FOR
select dbs.datid,dbs.datname,s1.setting as port from pg_catalog.pg_stat_database dbs, pg_catalog.pg_settings s1
where dbs.datname not like 'template_' and s1.name='port';
r_result RECORD;
BEGIN
-- Creating temporary tables, holding data for objects of all cluster databases
CREATE TEMPORARY TABLE IF NOT EXISTS temp_stat_user_tables (
snap_id integer,
dbid oid,
relid oid,
schemaname name,
relname name,
seq_scan bigint,
seq_tup_read bigint,
idx_scan bigint,
idx_tup_fetch bigint,
n_tup_ins bigint,
n_tup_upd bigint,
n_tup_del bigint,
n_tup_hot_upd bigint,
n_live_tup bigint,
n_dead_tup bigint,
n_mod_since_analyze bigint,
last_vacuum timestamp with time zone,
last_autovacuum timestamp with time zone,
last_analyze timestamp with time zone,
last_autoanalyze timestamp with time zone,
vacuum_count bigint,
autovacuum_count bigint,
analyze_count bigint,
autoanalyze_count bigint,
relsize bigint,
relsize_diff bigint
) ON COMMIT DROP;
CREATE TEMPORARY TABLE IF NOT EXISTS temp_stat_user_functions (
snap_id integer,
dbid oid,
funcid oid,
schemaname name,
funcname name,
calls bigint,
total_time double precision,
self_time double precision
) ON COMMIT DROP;
CREATE TEMPORARY TABLE IF NOT EXISTS temp_stat_user_indexes (
snap_id integer,
dbid oid,
relid oid,
indexrelid oid,
schemaname name,
relname name,
indexrelname name,
idx_scan bigint,
idx_tup_read bigint,
idx_tup_fetch bigint,
relsize bigint,
relsize_diff bigint,
indisunique bool
) ON COMMIT DROP;
CREATE TEMPORARY TABLE IF NOT EXISTS temp_statio_user_tables (
snap_id integer,
dbid oid,
relid oid,
schemaname name,
relname name,
heap_blks_read bigint,
heap_blks_hit bigint,
idx_blks_read bigint,
idx_blks_hit bigint,
toast_blks_read bigint,
toast_blks_hit bigint,
tidx_blks_read bigint,
tidx_blks_hit bigint,
relsize bigint,
relsize_diff bigint
) ON COMMIT DROP;
CREATE TEMPORARY TABLE IF NOT EXISTS temp_statio_user_indexes (
snap_id integer,
dbid oid,
relid oid,
indexrelid oid,
schemaname name,
relname name,
indexrelname name,
idx_blks_read bigint,
idx_blks_hit bigint,
relsize bigint,
relsize_diff bigint
) ON COMMIT DROP;
-- Load new data from statistic views of all cluster databases
FOR r_result IN c_dblist LOOP
INSERT INTO temp_stat_user_tables
SELECT s_id,r_result.datid,t.*
FROM dblink('dbname='||r_result.datname||' port='||r_result.port, 'select *,pg_relation_size(relid) relsize,0 relsize_diff from pg_catalog.pg_stat_user_tables')
AS t (
relid oid,
schemaname name,
relname name,
seq_scan bigint,
seq_tup_read bigint,
idx_scan bigint,
idx_tup_fetch bigint,
n_tup_ins bigint,
n_tup_upd bigint,
n_tup_del bigint,
n_tup_hot_upd bigint,
n_live_tup bigint,
n_dead_tup bigint,
n_mod_since_analyze bigint,
last_vacuum timestamp with time zone,
last_autovacuum timestamp with time zone,
last_analyze timestamp with time zone,
last_autoanalyze timestamp with time zone,
vacuum_count bigint,
autovacuum_count bigint,
analyze_count bigint,
autoanalyze_count bigint,
relsize bigint,
relsize_diff bigint
);
INSERT INTO temp_stat_user_indexes
SELECT s_id,r_result.datid,t.*
FROM dblink('dbname='||r_result.datname||' port='||r_result.port, 'select st.*,pg_relation_size(st.indexrelid),0,(ix.indisunique or con.conindid IS NOT NULL) as indisunique
from pg_catalog.pg_stat_user_indexes st
join pg_catalog.pg_index ix on (ix.indexrelid = st.indexrelid)
left join pg_catalog.pg_constraint con on(con.conindid = ix.indexrelid and con.contype in (''p'',''u''))')
AS t (
relid oid,
indexrelid oid,
schemaname name,
relname name,
indexrelname name,
idx_scan bigint,
idx_tup_read bigint,
idx_tup_fetch bigint,
relsize bigint,
relsize_diff bigint,
indisunique bool
);
INSERT INTO temp_stat_user_functions
SELECT s_id,r_result.datid,t.*
FROM dblink('dbname='||r_result.datname||' port='||r_result.port, 'select * from pg_catalog.pg_stat_user_functions')
AS t (
funcid oid,
schemaname name,
funcname name,
calls bigint,
total_time double precision,
self_time double precision
);
INSERT INTO temp_statio_user_tables
SELECT s_id,r_result.datid,t.*
FROM dblink('dbname='||r_result.datname||' port='||r_result.port, 'select *,pg_relation_size(relid),0 from pg_catalog.pg_statio_user_tables')
AS t (
relid oid,
schemaname name,
relname name,
heap_blks_read bigint,
heap_blks_hit bigint,
idx_blks_read bigint,
idx_blks_hit bigint,
toast_blks_read bigint,
toast_blks_hit bigint,
tidx_blks_read bigint,
tidx_blks_hit bigint,
relsize bigint,
relsize_diff bigint
);
INSERT INTO temp_statio_user_indexes
SELECT s_id,r_result.datid,t.*
FROM dblink('dbname='||r_result.datname||' port='||r_result.port, 'select *,pg_relation_size(indexrelid),0 from pg_catalog.pg_statio_user_indexes')
AS t (
relid oid,
indexrelid oid,
schemaname name,
relname name,
indexrelname name,
idx_blks_read bigint,
idx_blks_hit bigint,
relsize bigint,
relsize_diff bigint
);
END LOOP;
RETURN 0;
END;
$$ LANGUAGE plpgsql;