Skip to content

Instantly share code, notes, and snippets.

@CHERTS
Last active November 8, 2024 10:19
Show Gist options
  • Save CHERTS/bc4d73f5248cade48aa76efa06f5337c to your computer and use it in GitHub Desktop.
Save CHERTS/bc4d73f5248cade48aa76efa06f5337c to your computer and use it in GitHub Desktop.
Determine whether the running PostgreSQL cluster is the primary one or not.
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