-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg_get_wal_stats.sql
24 lines (24 loc) · 1.13 KB
/
pg_get_wal_stats.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
-- Author: Vignesh
-- Compatibility: PostgreSQL
-- This will provide the wal file statistics to understand and analyze how long its been modified by system.
SELECT
CASE WHEN pg_is_in_recovery()::BOOL is True THEN 'Secondary' ELSE 'Primary' END as node_status,
inet_client_addr()::inet as node_ip, --- use inet_server_addr() if you are not using containerized postgres
name as walfilename,
pg_size_pretty(walsize) as walsize,
CASE
WHEN age_diff >= 86400 THEN ROUND(age_diff / 86400)::int || ' days'
WHEN age_diff >= 3600 THEN ROUND(age_diff / 3600)::int || ' hours'
WHEN age_diff >= 60 THEN ROUND(age_diff / 60)::int || ' minutes'
ELSE age_diff::int || ' seconds'
END AS wal_modified_since
FROM
(
SELECT name,
size as walsize,
EXTRACT(EPOCH FROM now() - modification) AS age_diff FROM pg_ls_waldir()
where name ~ '^.{24}$' OR name ~ '^.{24}\.partial$' --filter only wal files that are reliable.
ORDER BY modification DESC -- show the recent modified walfile at the first.
LIMIT 10 -- disable or comment this if you do not want to limit records that are returned.
)
AS age_data;