Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
A direct command skips the review prompt. Inspect the source before running it.
Comprehensive database replication management with streaming replication,...
shortcut
repl
Database Replication Manager
Implement production-grade database replication for PostgreSQL and MySQL with streaming replication (physical), logical replication (selective tables), synchronous and asynchronous modes, automatic failover, lag monitoring, conflict resolution, and read scaling across multiple replicas. Achieve 99.99% availability with RPO <5 seconds and RTO <30 seconds for automated failover.
When to Use This Command
Use /replication when you need to:
Implement high availability with automatic failover (99.99%+ uptime)
Scale read workloads across multiple replicas (10x read capacity)
Create disaster recovery instances in different regions
Enable zero-downtime database migrations and upgrades
Implement read-heavy application architectures
Meet compliance requirements for data redundancy
DON'T use this when:
Single server handles all load comfortably (<50% CPU)
Database size is small (<10GB) and backup/restore is fast
Application doesn't support read replica routing
Network latency between regions is high (>100ms for sync replication)
You lack monitoring infrastructure for replication lag
Write workload is too heavy for replication to keep up
Design Decisions
This command implements automated replication with failover because:
Streaming replication provides real-time data synchronization
Automatic failover reduces RTO from hours to seconds
EOF
-- Create replication user with strong password
CREATE ROLE $REPLICATION_USER WITH REPLICATION LOGIN PASSWORD '$REPLICATION_PASSWORD';
-- Grant necessary permissions
GRANT CONNECT ON DATABASE postgres TO $REPLICATION_USER;
-- Create replication slot (recommended for reliability)
SELECT * FROM pg_create_physical_replication_slot('replica1_slot');
-- Verify replication user
\du $REPLICATION_USER
EOF
# 5. Restart PostgreSQL to apply changes
echo
""
echo
"✅ Primary server configured successfully"
echo
""
echo
"Replication Status:"
sudo
"SELECT * FROM pg_replication_slots;"
sudo
"SELECT usename, application_name, client_addr, state, sync_state FROM pg_stat_replication;"
EOF
SELECT
client_addr,
application_name,
state,
sync_state,
sent_lsn,
write_lsn,
flush_lsn,
replay_lsn,
sync_priority,
EXTRACT(EPOCH FROM (NOW() - pg_last_xact_replay_timestamp())) AS lag_seconds
FROM pg_stat_replication;
EOF
echo
""
echo
"=== REPLICA SERVER STATUS ==="
sudo
$REPLICA_HOST
EOF
SELECT
pg_is_in_recovery() AS is_replica,
pg_last_wal_receive_lsn() AS receive_lsn,
pg_last_wal_replay_lsn() AS replay_lsn,
pg_last_xact_replay_timestamp() AS last_replay_timestamp,
EXTRACT(EPOCH FROM (NOW() - pg_last_xact_replay_timestamp())) AS lag_seconds;
EOF
echo
""
echo
"=== REPLICATION LAG ==="
# Acceptable lag: <1 second for local replicas, <5 seconds for remote
sudo
$PRIMARY_HOST
"SELECT EXTRACT(EPOCH FROM (NOW() - pg_last_xact_replay_timestamp())) FROM pg_stat_replication LIMIT 1;"
if
$LAG
5
then
echo
"✅ Replication lag: ${LAG}s (healthy)"
else
echo
"⚠️ Replication lag: ${LAG}s (high)"
fi
# ===== MAIN =====
case
"${1:-}"
in
echo
"Usage: $0 {primary|replica|verify}"
echo
""
echo
" primary - Configure primary server for replication"
echo
" replica - Set up replica from primary"
echo
" verify - Verify replication status"
exit
esac
str
str
"postgres"
str
""
int
30
Optional
str
None
"""
Initialize failover manager.
Args:
primary_host: Primary server hostname
replica_host: Replica server hostname
postgres_user: PostgreSQL superuser
postgres_password: PostgreSQL password
failover_threshold_seconds: Trigger failover after this many seconds down
alert_webhook: Slack/PagerDuty webhook for alerts
"""
self
self
self
self
self
self
self
Optional
float
None
def
check_server_health
self, host: str
bool
"""
Check if PostgreSQL server is healthy.
Args:
host: Server hostname
Returns:
True if server is healthy, False otherwise
"""
try
self
self
"postgres"
5
return
True
except
as
f"Health check failed for {host}: {e}"
return
False
def
get_replication_status
self, host: str
Optional
"""
Get replication status from a server.
Args:
host: Server hostname
Returns:
ReplicationStatus or None if unreachable
"""
try
self
self
"postgres"
5
with
as
# Check if primary or replica
"SELECT pg_is_in_recovery()"
0
not
# Get replication lag (for replicas)
None
None
None
if
"""
SELECT
EXTRACT(EPOCH FROM (NOW() - pg_last_xact_replay_timestamp())) AS lag_seconds,
pg_last_wal_receive_lsn()::text AS receive_lsn,
pg_last_wal_replay_lsn()::text AS replay_lsn
"""
0
1
2
# Count connected replicas (for primary)
0
if
"SELECT COUNT(*) FROM pg_stat_replication"
0
return
except
as
f"Failed to get replication status from {host}: {e}"
return
None
def
promote_replica_to_primary
self, replica_host: str
bool
"""
Promote replica to primary.
Args:
replica_host: Replica server to promote
Returns:
True if promotion successful
"""
f"Promoting replica {replica_host} to primary..."
try
# Execute pg_promote() via SSH or local command
# (Assuming replica is on same machine for this example)
self
self
"postgres"
with
as
# Promote replica to primary
"SELECT pg_promote()"
# Wait for promotion to complete
5
# Verify promotion
self
if
and
f"✅ Successfully promoted {replica_host} to primary"