Repository navigation
Expand file tree
/
Copy pathpghero.sql
More file actions
255 lines (246 loc) · 7.17 KB
/
Copy pathpghero.sql
File metadata and controls
255 lines (246 loc) · 7.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
-----------------------------------------------------------------
-- pghero.sql
-- Copyright 2024 perrygeo
--
-- SQL queries ported to HugSQL syntax from pghero:
-- https://github.com/ankane/pghero
--
-- Portions of the query logic are Copyright (c) Andrew Kane and are
-- licensed under the MIT License, reproduced below.
--
-- MIT License
--
-- Copyright (c) Andrew Kane
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.
--
-- CHANGES:
-- * ported the queries to HugSQL syntax
-- * formatted the SQL code
-----------------------------------------------------------------
--
-- :name invalid-indexes
-- :command :query
-- :result :many
-- :doc Indexes that are invalid and need REINDEX
/* HUG_PSQL_EXTRAS: Indexes that are invalid and need REINDEX */
SELECT
n.nspname AS schema,
c.relname AS index,
pg_relation_size(c.oid) AS index_size_bytes
FROM
pg_index i
INNER JOIN pg_class c ON c.oid = i.indexrelid
INNER JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE
NOT i.indisvalid
AND n.nspname NOT IN ('pg_catalog', 'information_schema')
AND n.nspname !~ '^pg_toast'
AND n.nspname !~ '^(_timescaledb_|timescaledb_(information|experimental)$)'
ORDER BY
index_size_bytes DESC;
-- :name invalid-constraints
-- :command :query
-- :result :many
-- :doc Constraints that are NOT VALID
/* HUG_PSQL_EXTRAS: Constraints that are NOT VALID */
SELECT
nsp.nspname AS schema,
rel.relname AS table,
con.conname AS constraint,
con.contype,
fnsp.nspname AS referenced_schema,
frel.relname AS referenced_table
FROM
pg_catalog.pg_constraint con
INNER JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid
LEFT JOIN pg_catalog.pg_class frel ON frel.oid = con.confrelid
LEFT JOIN pg_catalog.pg_namespace nsp ON nsp.oid = con.connamespace
LEFT JOIN pg_catalog.pg_namespace fnsp ON fnsp.oid = frel.relnamespace
WHERE
con.convalidated = FALSE
AND nsp.nspname NOT IN ('pg_catalog', 'information_schema')
AND nsp.nspname !~ '^pg_toast'
AND nsp.nspname !~ '^(_timescaledb_|timescaledb_(information|experimental)$)'
ORDER BY
nsp.nspname,
rel.relname,
con.conname;
-- :name sequences
-- :command :query
-- :result :many
-- :doc Sequences near exhaustion or on small integer types
/* HUG_PSQL_EXTRAS: Sequences near exhaustion or on small integer types */
SELECT
schemaname AS schema,
sequencename AS sequence,
data_type::text AS data_type,
start_value,
increment_by,
CYCLE,
last_value,
max_value,
CASE WHEN max_value IS NULL THEN
NULL
ELSE
COALESCE(last_value, start_value)::numeric / max_value
END AS used_ratio
FROM
pg_sequences
WHERE
schemaname NOT IN ('pg_catalog', 'information_schema')
AND schemaname !~ '^pg_toast'
AND schemaname !~ '^(_timescaledb_|timescaledb_(information|experimental)$)'
ORDER BY
used_ratio DESC NULLS LAST,
schema,
SEQUENCE;
-- :name replication-lag
-- :command :query
-- :result :one
-- :doc Standby replication lag in seconds
/* HUG_PSQL_EXTRAS: Standby replication lag in seconds */
SELECT
CASE WHEN NOT pg_is_in_recovery()
OR pg_last_wal_receive_lsn () = pg_last_wal_replay_lsn () THEN
0
ELSE
EXTRACT(EPOCH FROM now() - pg_last_xact_replay_timestamp())
END AS lag_seconds;
-- :name replication-slots
-- :command :query
-- :result :many
-- :doc Replication slots with lag and retention
/* HUG_PSQL_EXTRAS: Replication slots with lag and retention */
SELECT
slot_name,
slot_type,
DATABASE,
active,
wal_status,
restart_lsn::text AS restart_lsn,
confirmed_flush_lsn::text AS confirmed_flush_lsn,
pg_wal_lsn_diff (pg_current_wal_lsn (), restart_lsn) AS restart_lag_bytes,
CASE WHEN slot_type = 'logical' THEN
pg_wal_lsn_diff (pg_current_wal_lsn (), confirmed_flush_lsn)
ELSE
NULL
END AS lag_bytes
FROM
pg_replication_slots
ORDER BY
slot_name;
-- :name transaction-id
-- :command :query
-- :result :many
-- :doc Transaction ID wraparound risk per database
/* HUG_PSQL_EXTRAS: Transaction ID wraparound risk per database */
SELECT
datname,
age(datfrozenxid) AS xid_age,
age(datfrozenxid)::numeric / 2000000000 AS wrap_risk_ratio
FROM
pg_database
ORDER BY
xid_age DESC;
-- :name maintenance
-- :command :query
-- :result :many
-- :doc Last vacuum/analyze and their counts per table
/* HUG_PSQL_EXTRAS: Last vacuum/analyze and their counts per table */
SELECT
schemaname AS schema,
relname AS table,
last_vacuum,
last_autovacuum,
last_analyze,
last_autoanalyze,
vacuum_count,
autovacuum_count,
analyze_count,
autoanalyze_count
FROM
pg_stat_user_tables
WHERE
schemaname !~ '^(_timescaledb_|timescaledb_(information|experimental)$)'
ORDER BY
1,
2;
-- :name unused-tables
-- :command :query
-- :result :many
-- :doc Tables never accessed via an index
/* HUG_PSQL_EXTRAS: Tables never accessed via an index */
SELECT
schemaname AS schema,
relname AS table,
seq_scan,
idx_scan,
n_live_tup AS estimated_rows,
pg_total_relation_size(relid) AS size_bytes
FROM
pg_stat_user_tables
WHERE
schemaname !~ '^(_timescaledb_|timescaledb_(information|experimental)$)'
AND idx_scan = 0
ORDER BY
size_bytes DESC,
1,
2;
-- :name missing-indexes
-- :command :query
-- :result :many
-- :doc Tables with low index usage and many rows
/* HUG_PSQL_EXTRAS: Tables with low index usage and many rows */
SELECT
schemaname AS schema,
relname AS table,
idx_scan,
seq_scan,
n_live_tup AS estimated_rows,
CASE WHEN (seq_scan + idx_scan) = 0 THEN
0
ELSE
idx_scan::float / (seq_scan + idx_scan)
END AS index_usage_ratio
FROM
pg_stat_user_tables
WHERE
schemaname !~ '^(_timescaledb_|timescaledb_(information|experimental)$)'
AND idx_scan > 0
AND idx_scan::float / (seq_scan + idx_scan) < 0.95
AND n_live_tup >= 10000
ORDER BY
n_live_tup DESC,
relname;
-- :name connection-states
-- :command :query
-- :result :many
-- :doc Connections grouped by state
/* HUG_PSQL_EXTRAS: Connections grouped by state */
SELECT
state,
count(*) AS connections
FROM
pg_stat_activity
GROUP BY
state
ORDER BY
connections DESC,
state;