-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.sql
127 lines (109 loc) · 4.37 KB
/
queries.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
-- Период запуска autovacuum worker для каждой "активной" базы данных из списка
show autovacuum_naptime;
-- Максимальное количество воркеров
show autovacuum_max_workers;
-- Размер фрагмента памяти для накопления идентификаторов версий при ручной очистке
show maintenance_work_mem;
-- При автоматической очистке
-- Если равен -1, используется значение maintenance_work_mem;
show autovacuum_work_mem;
-- Пороговое значение неактуальных (мёртвых) версий строк для запуска автоочистки
show autovacuum_vacuum_threshold;
show autovacuum_vacuum_scale_factor;
-- Пороговое значение изменённых с момента прошлого анализа строк для запуска анализа
show autovacuum_analyze_threshold;
show autovacuum_analyze_scale_factor;
-- Регулирование автоочистки
show vacuum_cost_limit;
show autovacuum_vacuum_cost_limit;
show autovacuum_vacuum_cost_delay;
-- 21.03.2024
-- Размер буферного кеша
SHOW shared_buffers;
-- Размер базы данных
SELECT pg_size_pretty(pg_database_size(current_database()));
-- Распределение буферов
-- DROP FUNCTION buffercache;
-- DROP EXTENSION pg_buffercache;
CREATE EXTENSION pg_buffercache;
CREATE FUNCTION buffercache(rel regclass)
RETURNS TABLE(
bufferid INTEGER, relfork TEXT, relblk BIGINT,
isdirty BOOLEAN, usagecount SMALLINT, pins INTEGER
) AS $$
SELECT bufferid,
CASE relforknumber
WHEN 0 THEN 'main'
WHEN 1 THEN 'fsm'
WHEN 2 THEN 'vm'
END,
relblocknumber,
isdirty,
usagecount,
pinning_backends
FROM pg_buffercache
WHERE relfilenode = pg_relation_filenode(rel)
ORDER BY relforknumber, relblocknumber;
$$ LANGUAGE sql;
SELECT usagecount, count(*) FROM pg_buffercache
GROUP BY usagecount
ORDER BY usagecount;
SELECT c.relname,
count(*) blocks,
round ( 100.0 * 8192 * count(*) /
pg_table_size(c.oid) ) AS "% of rel",
round( 100.0 * 8192 * count(*) FILTER (WHERE b.usagecount > 1) /
pg_table_size(c.oid) ) AS "% hot"
FROM pg_buffercache b
JOIN pg_class c ON pg_relation_filenode(c.oid) = b.relfilenode
WHERE b.reldatabase IN (
0, -- общие объекты кластера
(SELECT oid FROM pg_database WHERE datname = current_database())
)
AND b.usagecount IS NOT NULL
GROUP BY c.relname, c.oid
ORDER BY 2 DESC
LIMIT 250;
SELECT c.relname,
count(*) blocks,
round ( 100.0 * 8192 * count(*) /
pg_table_size(c.oid) ) AS "% of rel",
round( 100.0 * 8192 * count(*) FILTER (WHERE b.usagecount > 1) /
pg_table_size(c.oid) ) AS "% hot"
FROM pg_buffercache b
JOIN pg_class c ON pg_relation_filenode(c.oid) = b.relfilenode
WHERE b.reldatabase IN (
0, -- общие объекты кластера
(SELECT oid FROM pg_database WHERE datname = current_database())
)
AND b.usagecount IS NOT NULL
GROUP BY c.relname, c.oid
ORDER BY 2 ASC
LIMIT 250;
-- Сканирование индексов
SELECT
schemaname || '.' || relname AS table,
indexrelname AS index,
pg_size_pretty(pg_relation_size(i.indexrelid)) AS index_size,
idx_scan AS index_scans_count
FROM pg_stat_user_indexes ui
JOIN pg_index i ON ui.indexrelid = i.indexrelid
WHERE NOT indisunique AND pg_relation_size(relid) > 5 * 8192
ORDER BY pg_relation_size(i.indexrelid) / NULLIF(idx_scan, 0) DESC NULLS FIRST, pg_relation_size(i.indexrelid) DESC
LIMIT 250;
SELECT
schemaname || '.' || relname AS table,
indexrelname AS index,
pg_size_pretty(pg_relation_size(i.indexrelid)) AS index_size,
idx_scan AS index_scans_count
FROM pg_stat_user_indexes ui
JOIN pg_index i ON ui.indexrelid = i.indexrelid
WHERE NOT indisunique AND pg_relation_size(relid) > 5 * 8192
ORDER BY pg_relation_size(i.indexrelid) / NULLIF(idx_scan, 0) DESC NULLS FIRST, pg_relation_size(i.indexrelid) ASC
LIMIT 250;
-- Необходимо ли делать analyze или vacuum
-- Создать функции из файла analyze.sql
SELECT * FROM ck_need_vacuum ORDER BY max_dead_tup DESC LIMIT 250;
SELECT * FROM ck_need_vacuum ORDER BY last_autovacuum ASC LIMIT 250;
SELECT * FROM ck_need_analyze ORDER BY max_dead_tup DESC LIMIT 250;
SELECT * FROM ck_need_analyze ORDER BY last_autovacuum ASC LIMIT 250;