SlideShare a Scribd company logo
Copyright © 2013 NTT DATA Corporation
27 April 2013
Fujii Masao (twitter: @fujii_masao)
PostgreSQL replication
Inaugural Indian PostgreSQL User Group meetup!
2Copyright © 2013NTT DATA Corporation
Who am I ?
 Database engineer in NTT DATA
• NTT DATA is global IT service and consulting
company
 PostgreSQL developer since 2008
 One of main authors of PostgreSQL replication
3Copyright © 2013NTT DATA Corporation
Index
 What's replication?
 History
 PostgreSQL replication
1. Features
2. Synchronous vs. Asynchronous
Copyright © 2013 NTT DATA Corporation 4
What's replication?
5Copyright © 2013 NTT DATA Corporation
What's replication?
ClientClient
ChangeChange
DB servers
Change
Replicate
DB servers
Change
Proxy server
Create a replica of the database on multiple servers
6Copyright © 2013 NTT DATA Corporation
Why is replication required?
Replication is an essential feature to get the system to work properly 24/7!
High
Availability
Load
Balancing
If one server crashes, another can keep providing the service
We can easily reduce the downtime of the system
The load of query execution can be distributed to multiple servers
We can improve the performance of whole system
ClientClient
SQL SQLSQL
High Availability Load Balancing
DB servers DB servers
Copyright © 2013 NTT DATA Corporation 7
History
8Copyright © 2013 NTT DATA Corporation
History of PostgreSQL replication
2007
2008
2009
2010
2012
2011
9.0(Sep 2010 release)
• Async reploication
9.1(Sep 2011 release)
• Synchronous replication
• Improve replication monitoring
• pg_basebackup
9.2(Sep 2012 release)
• Cascade replication
• Backup from standby
• New sync rep mode
Slony-I
Bucardo Londiste
Sequoia
PGCluster
PostgresForest
Postgres-R
Mammoth
PL/Proxy
pgpool-II
rubyrep
Postgres-XC
GridSQL
syncreplicator
Replication war !
Historically the community policy has been to avoid putting replication into core. But,
because of large user needs, replication has been introduced in 9.0.
Then, replication keeps evolving steady.
Copyright © 2013 NTT DATA Corporation 9
Features
10Copyright © 2013 NTT DATA Corporation
Single master / Multiple standbys
Single master
Multiple standbys Multiple standbys
Replicate Replicate
Write SQL
Read SQL
Read SQL
Read SQL
 Replication from single master to multiple standbys
 Cascade replication
 Only master accepts write query, both accepts read query
Read scalable, not write scalable
11Copyright © 2013 NTT DATA Corporation
Read-only query on standby
Online backup
– (Logical) pg_dump
– (Physical) pg_basebackup
Maintenance command
– VACUUM, ANALYZE
※No need to do VACUUM and
ANALYZE on standby because
the result of mantenance
execution on master is
automatically replicated to
standby
Allow
Query access
– SELECT
– PREPARE, EXECUTE
– CURSOR operation
Disallow
DML
– INSERT, UPDATE, DELETE
– SELECT FOR UPDATE
DDL
– CREATE, DROP, ALTER
Temporary table
12Copyright © 2013 NTT DATA Corporation
Log-shipping
 Transaction log (WAL) is shipped from master to standby
 Standby is in recovery mode
 Standby keeps the database current by replaying shipped WAL
RecoveryMaster Standby
ClientWrite SQL
WAL writing
WAL shipping
WAL writing
13Copyright © 2013 NTT DATA Corporation
Limitation by log-shipping
The followings must be the same between master and standby
 H/W and OS architecture
 PostgreSQL major version
Slony-I can be used for replication between different
architectures and major versions
Master
Standby
64bit OS
PostgreSQL9.2.1
PostgreSQL9.1.0
32bit OS
64bit OS
PostgreSQL9.2.0
NG
NG
OK
2
1
14Copyright © 2013 NTT DATA Corporation
All database objects are replicated
 Per-table granularity is not allowed
Slony-I supports per-table granularity replication
Per database cluster granularity
Per database cluster Per table
Master Standby Master Standby
15Copyright © 2013 NTT DATA Corporation
SQL distribution
PostgreSQL doesn't provide SQL distribution feature
 Implement SQL distribution logic into client application
 Use SQL distributor like pgpool-II
ClientClient
Read SQLWrite SQL
マスタ
Write SQL
Write/Read SQL
Distributor
スタンバイ
Read SQL
Master Standby
Implement logic Use SQL distributor
16Copyright © 2013 NTT DATA Corporation
Failover
PostgreSQL doesn't provide automatic failover feature
 Standby can be promoted to master anytime(pg_ctl promote)
 Automatic error detection and faliover requires clusterware
ClientClient
Master
pgpool-II
StandbyMaster スタンバイ
Pacemaker pgpool-II
VIP
Pacemaker supports the
resource agent for HA cluster
using PostgreSQL replication!
17Copyright © 2013 NTT DATA Corporation
Monitoring
=# SELECT * FROM pg_stat_replication;
-[ RECORD 1 ]----+------------------------------
procpid | 26531
usesysid | 10
usename | postgres
application_name | tokyo
client_addr | 192.168.1.2
client_hostname |
client_port | 39654
backend_start | 2012-02-01 18:54:49.429459+09
state | streaming
sent_location | 0/406E7CC
write_location | 0/406E7CC
flush_location | 0/406E7CC
replay_location | 0/406E1B0
sync_priority | 1
sync_state | sync
Replication progress
How far has master sent WAL?
How far has standby written, flushed or
replayed WAL?
Replication connection information
Standby IP address, Port number,
ROLE for replication、
Replication start time, etc
Replication status
Current synchronous mode,
Standby has already caught up with master?
Copyright © 2013 NTT DATA Corporation 18
Synchronous vs. Asynchronous
19Copyright © 2013 NTT DATA Corporation
Replication mode
Replication mode is configurable
 Asynchronous
 Synchronous
20Copyright © 2013 NTT DATA Corporation
Asynchronous replication
COMMIT doesn't wait for the completion of replication
 No guarantee that WAL has already arrived at standby at end of
COMMIT
Data loss window on failover
Query on standby may see outdated data
Low performance impact on master
RecoveryMaster Standby
ClientCOMMIT
WAL writing
WAL shipping
WAL writing
OK
1
2
3
4
5
6
Committed data gets
lost if failover happens
between and
operations
3 4



21Copyright © 2013 NTT DATA Corporation
Synchronous replication
COMMIT waits for completion of replication
 WAL is guaranteed to be flushed in master and standby at end of COMMIT
No data loss on failover!
Relatively high performance impact on master
Query on standby may see outdated data
 「Synchronous ≠ Committed data is available on standby immediately」
Recovery
Master Standby
ClientCOMMIT
WAL writing
WAL shipping
WAL writing
OK
1
2
6
3
4
7
Reply5


WAL shipping is synchronous,
but recovery is asynchronous

Copyright © 2011 NTT DATA Corporation
Copyright © 2013 NTT DATA Corporation
(Please omit notations when unnecessary)
This document contains confidential Company information. Do not disclose it to third parties without permission from the Company.

More Related Content

PDF
Built-in Replication in PostgreSQL
PPT
PostgreSQL9.3 Switchover/Switchback
PDF
PostgreSQL Scaling And Failover
PPT
Building tungsten-clusters-with-postgre sql-hot-standby-and-streaming-replica...
ODP
PostgreSQL Replication in 10 Minutes - SCALE
PDF
Replication Solutions for PostgreSQL
PDF
PostgreSQL HA
PDF
Streaming Replication (Keynote @ PostgreSQL Conference 2009 Japan)
Built-in Replication in PostgreSQL
PostgreSQL9.3 Switchover/Switchback
PostgreSQL Scaling And Failover
Building tungsten-clusters-with-postgre sql-hot-standby-and-streaming-replica...
PostgreSQL Replication in 10 Minutes - SCALE
Replication Solutions for PostgreSQL
PostgreSQL HA
Streaming Replication (Keynote @ PostgreSQL Conference 2009 Japan)

What's hot (20)

PDF
Postgres on OpenStack
 
PDF
Postgres & Red Hat Cluster Suite
 
PDF
PostreSQL HA and DR Setup & Use Cases
PDF
Best Practices of HA and Replication of PostgreSQL in Virtualized Environments
PDF
Architecture for building scalable and highly available Postgres Cluster
PDF
On The Building Of A PostgreSQL Cluster
PDF
What's New in Postgres Plus Advanced Server 9.3
 
PDF
Geographically Distributed PostgreSQL
PDF
PostgreSQL High Availability in a Containerized World
PDF
Basics of Logical Replication,Streaming replication vs Logical Replication ,U...
PPTX
PostgreSQL Hangout Parameter Tuning
PDF
Linux tuning to improve PostgreSQL performance
PPTX
Streaming Replication Made Easy in v9.3
PDF
Deep Dive into RDS PostgreSQL Universe
PDF
Problems with PostgreSQL on Multi-core Systems with MultiTerabyte Data
PDF
MySQL Server Backup, Restoration, and Disaster Recovery Planning
PDF
Overview of Postgres Utility Processes
 
PDF
Best Practices with PostgreSQL on Solaris
PDF
Postgres-XC: Symmetric PostgreSQL Cluster
PDF
Tuning DB2 in a Solaris Environment
Postgres on OpenStack
 
Postgres & Red Hat Cluster Suite
 
PostreSQL HA and DR Setup & Use Cases
Best Practices of HA and Replication of PostgreSQL in Virtualized Environments
Architecture for building scalable and highly available Postgres Cluster
On The Building Of A PostgreSQL Cluster
What's New in Postgres Plus Advanced Server 9.3
 
Geographically Distributed PostgreSQL
PostgreSQL High Availability in a Containerized World
Basics of Logical Replication,Streaming replication vs Logical Replication ,U...
PostgreSQL Hangout Parameter Tuning
Linux tuning to improve PostgreSQL performance
Streaming Replication Made Easy in v9.3
Deep Dive into RDS PostgreSQL Universe
Problems with PostgreSQL on Multi-core Systems with MultiTerabyte Data
MySQL Server Backup, Restoration, and Disaster Recovery Planning
Overview of Postgres Utility Processes
 
Best Practices with PostgreSQL on Solaris
Postgres-XC: Symmetric PostgreSQL Cluster
Tuning DB2 in a Solaris Environment
Ad

Similar to PostgreSQL replication (20)

PDF
PostgreSQL replication
PDF
MySql's NoSQL -- best of both worlds on the same disks
PDF
MySQL 5.7 -- SCaLE Feb 2014
PDF
MySQL 5.7 New Features to Exploit -- PHPTek/Chicago MySQL User Group May 2014
PDF
Greenplum Architecture
PPTX
Boston_sql_kegorman_highIO.pptx
PDF
DB2 pureScale Overview Sept 2010
PDF
The Good, The Bad and the Ugly
PDF
Real-Time Query for Data Guard
PDF
MySQL Cluster Asynchronous replication (2014)
PDF
What's New in PostgreSQL 9.3
 
PDF
Disaster Recovery pomocí Oracle Cloudu
PDF
MySQL 5.7 NEW FEATURES, BETTER PERFORMANCE, AND THINGS THAT WILL BREAK -- Mid...
PDF
Oracle Solaris 11.1 New Features
PPTX
Integrating Hybrid Cloud Database-as-a-Service with Cloud Foundry’s Service​ ...
PPTX
Why oracle data guard new features in oracle 18c, 19c
PDF
Replication features, technologies and 3rd party Extinction
PDF
Solaris 11 Consolidation Tools
PDF
Database failover from client perspective
PPTX
BigData Clusters Redefined
PostgreSQL replication
MySql's NoSQL -- best of both worlds on the same disks
MySQL 5.7 -- SCaLE Feb 2014
MySQL 5.7 New Features to Exploit -- PHPTek/Chicago MySQL User Group May 2014
Greenplum Architecture
Boston_sql_kegorman_highIO.pptx
DB2 pureScale Overview Sept 2010
The Good, The Bad and the Ugly
Real-Time Query for Data Guard
MySQL Cluster Asynchronous replication (2014)
What's New in PostgreSQL 9.3
 
Disaster Recovery pomocí Oracle Cloudu
MySQL 5.7 NEW FEATURES, BETTER PERFORMANCE, AND THINGS THAT WILL BREAK -- Mid...
Oracle Solaris 11.1 New Features
Integrating Hybrid Cloud Database-as-a-Service with Cloud Foundry’s Service​ ...
Why oracle data guard new features in oracle 18c, 19c
Replication features, technologies and 3rd party Extinction
Solaris 11 Consolidation Tools
Database failover from client perspective
BigData Clusters Redefined
Ad

More from Masao Fujii (10)

PDF
カスタムプランと汎用プラン
PDF
Introduction to pg_cheat_funcs
PDF
PostgreSQL Quiz
PDF
誰か私のTODOを解決してください
PDF
WAL圧縮
PDF
使ってみませんか?pg hint_plan
PDF
PostgreSQLレプリケーション徹底紹介
PDF
PostgreSQL V9 レプリケーション解説
PDF
PostgreSQL9.0アップデート レプリケーションがやってきた!
PDF
PostgreSQL9.1同期レプリケーションとPacemakerによる高可用クラスタ化の紹介
カスタムプランと汎用プラン
Introduction to pg_cheat_funcs
PostgreSQL Quiz
誰か私のTODOを解決してください
WAL圧縮
使ってみませんか?pg hint_plan
PostgreSQLレプリケーション徹底紹介
PostgreSQL V9 レプリケーション解説
PostgreSQL9.0アップデート レプリケーションがやってきた!
PostgreSQL9.1同期レプリケーションとPacemakerによる高可用クラスタ化の紹介

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Empathic Computing: Creating Shared Understanding
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
A Presentation on Artificial Intelligence
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
KodekX | Application Modernization Development
PDF
Modernizing your data center with Dell and AMD
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Machine learning based COVID-19 study performance prediction
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Unlocking AI with Model Context Protocol (MCP)
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
NewMind AI Weekly Chronicles - August'25 Week I
Empathic Computing: Creating Shared Understanding
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Reach Out and Touch Someone: Haptics and Empathic Computing
The Rise and Fall of 3GPP – Time for a Sabbatical?
A Presentation on Artificial Intelligence
Advanced methodologies resolving dimensionality complications for autism neur...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
KodekX | Application Modernization Development
Modernizing your data center with Dell and AMD
NewMind AI Monthly Chronicles - July 2025
Network Security Unit 5.pdf for BCA BBA.
Machine learning based COVID-19 study performance prediction
Per capita expenditure prediction using model stacking based on satellite ima...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf

PostgreSQL replication

  • 1. Copyright © 2013 NTT DATA Corporation 27 April 2013 Fujii Masao (twitter: @fujii_masao) PostgreSQL replication Inaugural Indian PostgreSQL User Group meetup!
  • 2. 2Copyright © 2013NTT DATA Corporation Who am I ?  Database engineer in NTT DATA • NTT DATA is global IT service and consulting company  PostgreSQL developer since 2008  One of main authors of PostgreSQL replication
  • 3. 3Copyright © 2013NTT DATA Corporation Index  What's replication?  History  PostgreSQL replication 1. Features 2. Synchronous vs. Asynchronous
  • 4. Copyright © 2013 NTT DATA Corporation 4 What's replication?
  • 5. 5Copyright © 2013 NTT DATA Corporation What's replication? ClientClient ChangeChange DB servers Change Replicate DB servers Change Proxy server Create a replica of the database on multiple servers
  • 6. 6Copyright © 2013 NTT DATA Corporation Why is replication required? Replication is an essential feature to get the system to work properly 24/7! High Availability Load Balancing If one server crashes, another can keep providing the service We can easily reduce the downtime of the system The load of query execution can be distributed to multiple servers We can improve the performance of whole system ClientClient SQL SQLSQL High Availability Load Balancing DB servers DB servers
  • 7. Copyright © 2013 NTT DATA Corporation 7 History
  • 8. 8Copyright © 2013 NTT DATA Corporation History of PostgreSQL replication 2007 2008 2009 2010 2012 2011 9.0(Sep 2010 release) • Async reploication 9.1(Sep 2011 release) • Synchronous replication • Improve replication monitoring • pg_basebackup 9.2(Sep 2012 release) • Cascade replication • Backup from standby • New sync rep mode Slony-I Bucardo Londiste Sequoia PGCluster PostgresForest Postgres-R Mammoth PL/Proxy pgpool-II rubyrep Postgres-XC GridSQL syncreplicator Replication war ! Historically the community policy has been to avoid putting replication into core. But, because of large user needs, replication has been introduced in 9.0. Then, replication keeps evolving steady.
  • 9. Copyright © 2013 NTT DATA Corporation 9 Features
  • 10. 10Copyright © 2013 NTT DATA Corporation Single master / Multiple standbys Single master Multiple standbys Multiple standbys Replicate Replicate Write SQL Read SQL Read SQL Read SQL  Replication from single master to multiple standbys  Cascade replication  Only master accepts write query, both accepts read query Read scalable, not write scalable
  • 11. 11Copyright © 2013 NTT DATA Corporation Read-only query on standby Online backup – (Logical) pg_dump – (Physical) pg_basebackup Maintenance command – VACUUM, ANALYZE ※No need to do VACUUM and ANALYZE on standby because the result of mantenance execution on master is automatically replicated to standby Allow Query access – SELECT – PREPARE, EXECUTE – CURSOR operation Disallow DML – INSERT, UPDATE, DELETE – SELECT FOR UPDATE DDL – CREATE, DROP, ALTER Temporary table
  • 12. 12Copyright © 2013 NTT DATA Corporation Log-shipping  Transaction log (WAL) is shipped from master to standby  Standby is in recovery mode  Standby keeps the database current by replaying shipped WAL RecoveryMaster Standby ClientWrite SQL WAL writing WAL shipping WAL writing
  • 13. 13Copyright © 2013 NTT DATA Corporation Limitation by log-shipping The followings must be the same between master and standby  H/W and OS architecture  PostgreSQL major version Slony-I can be used for replication between different architectures and major versions Master Standby 64bit OS PostgreSQL9.2.1 PostgreSQL9.1.0 32bit OS 64bit OS PostgreSQL9.2.0 NG NG OK 2 1
  • 14. 14Copyright © 2013 NTT DATA Corporation All database objects are replicated  Per-table granularity is not allowed Slony-I supports per-table granularity replication Per database cluster granularity Per database cluster Per table Master Standby Master Standby
  • 15. 15Copyright © 2013 NTT DATA Corporation SQL distribution PostgreSQL doesn't provide SQL distribution feature  Implement SQL distribution logic into client application  Use SQL distributor like pgpool-II ClientClient Read SQLWrite SQL マスタ Write SQL Write/Read SQL Distributor スタンバイ Read SQL Master Standby Implement logic Use SQL distributor
  • 16. 16Copyright © 2013 NTT DATA Corporation Failover PostgreSQL doesn't provide automatic failover feature  Standby can be promoted to master anytime(pg_ctl promote)  Automatic error detection and faliover requires clusterware ClientClient Master pgpool-II StandbyMaster スタンバイ Pacemaker pgpool-II VIP Pacemaker supports the resource agent for HA cluster using PostgreSQL replication!
  • 17. 17Copyright © 2013 NTT DATA Corporation Monitoring =# SELECT * FROM pg_stat_replication; -[ RECORD 1 ]----+------------------------------ procpid | 26531 usesysid | 10 usename | postgres application_name | tokyo client_addr | 192.168.1.2 client_hostname | client_port | 39654 backend_start | 2012-02-01 18:54:49.429459+09 state | streaming sent_location | 0/406E7CC write_location | 0/406E7CC flush_location | 0/406E7CC replay_location | 0/406E1B0 sync_priority | 1 sync_state | sync Replication progress How far has master sent WAL? How far has standby written, flushed or replayed WAL? Replication connection information Standby IP address, Port number, ROLE for replication、 Replication start time, etc Replication status Current synchronous mode, Standby has already caught up with master?
  • 18. Copyright © 2013 NTT DATA Corporation 18 Synchronous vs. Asynchronous
  • 19. 19Copyright © 2013 NTT DATA Corporation Replication mode Replication mode is configurable  Asynchronous  Synchronous
  • 20. 20Copyright © 2013 NTT DATA Corporation Asynchronous replication COMMIT doesn't wait for the completion of replication  No guarantee that WAL has already arrived at standby at end of COMMIT Data loss window on failover Query on standby may see outdated data Low performance impact on master RecoveryMaster Standby ClientCOMMIT WAL writing WAL shipping WAL writing OK 1 2 3 4 5 6 Committed data gets lost if failover happens between and operations 3 4   
  • 21. 21Copyright © 2013 NTT DATA Corporation Synchronous replication COMMIT waits for completion of replication  WAL is guaranteed to be flushed in master and standby at end of COMMIT No data loss on failover! Relatively high performance impact on master Query on standby may see outdated data  「Synchronous ≠ Committed data is available on standby immediately」 Recovery Master Standby ClientCOMMIT WAL writing WAL shipping WAL writing OK 1 2 6 3 4 7 Reply5   WAL shipping is synchronous, but recovery is asynchronous 
  • 22. Copyright © 2011 NTT DATA Corporation Copyright © 2013 NTT DATA Corporation (Please omit notations when unnecessary) This document contains confidential Company information. Do not disclose it to third parties without permission from the Company.