Primary veri tabanından diğerlerine replication işini hiç yapmadım.
Şartlar
Açıklaması şöyle
The basic requirement replication requirement,1. Replica DB required same PG version2. Replica DB is in always sync with Primary DB.
Replication Lag
Örnek
Gecikmenin sebebi için bir örnek burada. Burada replica üzerinde koşan sorgu, WAL sender'i engellediği için gecikme oluyor. Özet olarak silme işlemi eskice kayıtlar üzerinde yapılsa daha iyi
You see when you have postgres replication you have a dilemma.In one hand someone is querying standby with long running query, pinning a snapshot at t7.On the other hand the primary has deleted some rows and vacuum is about to purge deleted tuples on t7 as no query on the primary needs them. Vacuum purges those, new WAL records of the purge.WAL sender kicks in and send the vacuumed WAL entries to purge t7 on the standby but guess what? standby still need them. This blocks replication as the WAL receiver cannot apply the purged row if a query need them.This creates lag in replication and as a result entire replication halt until queries are done.
Replication Lag Ölçümü
Eğer Patroni kullanmıyorsak elle ölçüm yapılabilir
Örnek
Şöyle yaparız
# Check the current LSN on primary DB.
psql -h primary_host -c "SELECT pg_current_wal_lsn();"
# check replica lag
psql -h replica_host
-c "SELECT pg_is_in_recovery(),pg_is_wal_replay_paused(),pg_last_wal_receive_lsn(),
pg_last_wal_replay_lsn(), pg_last_xact_replay_timestamp();"
# Check the difference between primary DB LSN and replica replay LSN.
lsn_diff_size = psql -h replica_host
-c "SELECT pg_wal_lsn_diff('54/5A282990','54/5A282990');"
# Take the different and Check the size in MB or GB.
// LAG difference in MB
psql -h replica_host -c "SELECT round(lsn_diff_size/pow(1024,2.0),2 missing_lsn_mIB;"
// LAG difference in GB
psql -h replica_host -c "SELECT round(lsn_diff_size/pow(1024,3.0),2 missing_lsn_GIB;"Ayarlar
Not : Bir örnek burada
1. Master Ayarları
Master üzerinde bir kullanıcı yarat
CREATE USER replicationREPLICATION LOGIN CONNECTION LIMIT 1 ENCRYPTED PASSWORD 'replicationpa55word'; # change the maximum number of connections allowed to the replication user ALTER ROLE replication CONNECTION LIMIT -1;
/etc/postgresql/12/main/postgresql.conf dosyasına şunu ekle. 172.XXX adresini master bilgisayarın gerçek IP adresi ile değiştir.
listen_addresses = 'localhost,172.16.10.220' wal_level = replica max_wal_senders = 10 wal_keep_segments = 64
Slave bilgisayarın bağlanabilmesi için doğrulanması gerekir. etc/postgresql/12/main/pg_hba.conf dosyasına şunu ekle.
host replication replication 172.16.10.119/0 md5
Master bilgisayarı tekrar başlat
sudo /etc/init.d/postgresql restart veya sudo service postgresql restart
Örnek
Şöyle yaparız
# On primary — in postgresql.conf wal_level = replica max_wal_senders = 5 wal_keep_size = 1GB # On replica - recovery.conf or postgresql.conf primary_conninfo = 'host=primary-host port=5432 user=replicator' hot_standby = on
2. Slave Ayarları
/etc/postgresql/12/main/postgresql.conf dosyasına şunu ekle. 172.XXX adresini slave bilgisayarın gerçek IP adresi ile değiştir.
listen_addresses = 'localhost,172.16.10.119' wal_level = replica max_wal_senders = 10 wal_keep_segments = 64
sudo rm -rfv * cd /var/lib/postgresql/12/main/
Master bilgisayardaki tüm veriyi slave bilgisayara kopyala. Burada şifre isteyecektir. Şifre replicationpa55word. Bitince slave bilgisayar tekrar başlatılır
sudo su postgres \ pg_basebackup \ -h 172.16.10.220 \ -U replication \ -p 5432 \ -D /var/lib/postgresql/12/main/ \ -Fp \ -Xs \ -P \ -R
Synchronous Replication
Bir örnek şöyle
Ghost Commits in PostgreSQL: How to Lose a Committed Transaction in a Synchronous Cluster Without a Single ExceptionMany engineers assume that enabling synchronous replication (synchronous_commit = on) alongside Patroni guarantees that if a client gets no error on COMMIT, the data is permanently safe. In reality, failures expose a critical window of complete uncertainty.In my bare-bones lab (PostgreSQL 17 + Patroni + a 3-node etcd cluster on Testcontainers), I reproduced this exact failure mode end-to-end:1. The Commit Order: The Master flushes the transaction locally (writing to WAL and marking it committed in pg_xact) before waiting for the replica's acknowledgment.2. The JDBC/ORM Trap: If the replica hangs and the client cancels the request (Ctrl+C / query timeout), PostgreSQL does not throw an exception. Instead, it emits a non-fatal SQLWarning (SQLState 01000): "canceling wait for synchronous replication due to user request"—which standard ORMs (Hibernate, Spring) silently ignore.3. The pg_rewind Wipeout: If the Master then crashes, Patroni fails over to the replica. Once the old Master comes back online, pg_rewind aligns the timelines and permanently wipes the "ghost" commit from disk.How to protect your system in production:• Inspect SQLWarning: Explicitly check for SQLState 01000 inside custom JDBC wrappers or transaction managers after commit().• Enforce Idempotency: Attach a client-generated idempotency_key (UUID) backed by a strict UNIQUE index in the database.• Use a Reconciliation Queue: Upon catching a warning or network timeout during commit, return 202 Accepted to the caller and push the transaction context to an external queue (Kafka/Redis) for asynchronous verification against the current Leader.