Last active
November 8, 2024 10:19
-
-
Save CHERTS/bc4d73f5248cade48aa76efa06f5337c to your computer and use it in GitHub Desktop.
Determine whether the running PostgreSQL cluster is the primary one or not.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT DISTINCT | |
CASE | |
WHEN b.sender=0 AND c.receiver=0 THEN | |
'standalone' | |
WHEN b.sender>0 AND c.receiver=0 THEN | |
'primary' | |
WHEN b.sender=0 AND c.receiver>0 THEN | |
'replica' | |
WHEN b.sender>0 AND c.receiver>0 THEN | |
'primary+replica' | |
END AS pg_cluster_role | |
FROM | |
pg_database a, | |
( | |
SELECT count(*) AS sender | |
FROM pg_stat_replication | |
) b, | |
( | |
SELECT count(*) AS receiver | |
FROM pg_stat_wal_receiver | |
) c | |
WHERE | |
NOT a.datistemplate; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment