Understanding Streaming Replication in PostgreSQL


Streaming replication in PostgreSQL works on log shipping methodology, But its not shipping of log file similar to SQL Server. It's the shipping of logs (records from one server to another. Every transaction in Postgres is written to a transaction log called WAL file (write-ahead log) to achieve durability from ACID properties. A slave uses these WAL segments to continuously replicate changes from its master.

In Postgres master is a primary server and save is a secondary server.

In streaming replication, there exist 3 mandatory processes that play a major role in achieving streaming replication in Postgres.


  1. Wal sender 
  2. Wal receiver 
  3. Startup process 

Wal sender:

  • This process runs on a master.
  • This process on master sends the WAL data until the latest LSN starting from the LSN sent by the Wal receiver, to the slave.

Wal receiver: 

  • This process runs on the slave. 
  • It sends the LSN (Log Sequence Number) up until when the WAL data has been replayed on a slave, to the master
  • This process writes the WAL data sent by Wal sender to WAL segments.


Startup:

  • This process runs on the slave. 
  • This process on the slave that replays the data written to the WAL segment.


How it works:


  1. On starting of  the replication, 
  2. A Wal receiver process sends the LSN (Log Sequence Number) up until when the WAL data has been replayed on a slave, to the master. 
  3. Then the Wal sender process on master sends the WAL data until the latest LSN starting from the LSN sent by the Wal receiver, to the slave. 
  4. Then Wal receiver writes the WAL data sent by Wal sender to WAL segments. 
  5. Now the startup process on the slave replays the data written to the Wal segment. 




1 comment:

  1. I feel SSIS and other components that really require an extensive and extra research on how to use them ideally are actually well suitable for most parts.

    SSIS Postgresql Read

    ReplyDelete