SlideShare a Scribd company logo
1 of 22
Download to read offline
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

What's hot

Best Practices of HA and Replication of PostgreSQL in Virtualized Environments
Best Practices of HA and Replication of PostgreSQL in Virtualized EnvironmentsBest Practices of HA and Replication of PostgreSQL in Virtualized Environments
Best Practices of HA and Replication of PostgreSQL in Virtualized Environments
Jignesh Shah
 

What's hot (20)

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

Similar to PostgreSQL replication

MySQL 5.7 -- SCaLE Feb 2014
MySQL 5.7 -- SCaLE Feb 2014MySQL 5.7 -- SCaLE Feb 2014
MySQL 5.7 -- SCaLE Feb 2014
Dave Stokes
 
Oracle Solaris 11.1 New Features
Oracle Solaris 11.1 New FeaturesOracle Solaris 11.1 New Features
Oracle Solaris 11.1 New Features
Orgad Kimchi
 
Replication features, technologies and 3rd party Extinction
Replication features, technologies and 3rd party ExtinctionReplication features, technologies and 3rd party Extinction
Replication features, technologies and 3rd party Extinction
Ben Mildren
 
Solaris 11 Consolidation Tools
Solaris 11 Consolidation ToolsSolaris 11 Consolidation Tools
Solaris 11 Consolidation Tools
Roman Ivanov
 
BigData Clusters Redefined
BigData Clusters RedefinedBigData Clusters Redefined
BigData Clusters Redefined
DataWorks Summit
 

Similar to PostgreSQL replication (20)

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

More from Masao Fujii

More from Masao Fujii (10)

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

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Recently uploaded (20)

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

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.