streaming replication hot standby - postgresql · • implementing streaming replication. history....
TRANSCRIPT
![Page 1: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/1.jpg)
Streaming Replication
&
Hot Standby
![Page 2: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/2.jpg)
query
Hot StandbyClient
Streaming Replication
query
SlaveMaster
changes
v8.5~
![Page 3: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/3.jpg)
High Availability Load Balancing
Master Slave
Client
Master Slave
Client
Why Streaming Replication & Hot Standby?
query queryquery
![Page 4: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/4.jpg)
Schedule
1. Talk: Streaming Replication
2. Talk: Hot Standby
3. Demo
![Page 5: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/5.jpg)
Streaming Replication
Masao FujiiNTT OSS Center
Copyright(c)2009 NTT, Inc. All Rights Reserved.
![Page 6: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/6.jpg)
Fujii Masao
• Database engineer at NTT OSS Center
• Support and consulting
• Implementing Streaming Replication
![Page 7: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/7.jpg)
History
![Page 8: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/8.jpg)
Historical policy
• Avoid putting replication into core Postgres
• No "one size fits all" replication solution
![Page 9: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/9.jpg)
Replication War!?
pgpool
CyberclusterSlony-I
Bucardo
warm-standby
LondisteSequoia
PGClusterPostgresForest
Postgres-RMammoth
syncreplicator
PyReplica
PGCluster-II
PL/Proxy
pgpool-II RepDB
DBmirror
twin
rubyrep
Postgres-2
GridSQL
![Page 10: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/10.jpg)
No default choice
• Too complex to use for simple cases
• vs. other dbms
![Page 11: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/11.jpg)
Proposal of built-in replication
• by NTT OSSC @ PGCon 2008 Ottawa
![Page 12: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/12.jpg)
Core team statement
• It is time to include a simple, reliable
basic replication feature in the coresystem
• NOT replace the existing projects
![Page 13: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/13.jpg)
Features
![Page 14: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/14.jpg)
Slaves
Master
write query
Client
changes
Master - Slaves
![Page 15: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/15.jpg)
WALRecovery
DatabaseWALWAL
write queryMaster
Client
Slave
Log shipping
![Page 16: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/16.jpg)
Record-based log shipping
Record-based
File-based
Master
Slave
Slave
![Page 17: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/17.jpg)
No migration required
Master
Client
Slave
Client
Stand-alone
![Page 18: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/18.jpg)
Per database cluster Per table
Per database cluster granularity
Master Slave Master Slave
![Page 19: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/19.jpg)
Shared nothing Shared disk
Master Slave Master Slave
Shared nothing
![Page 20: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/20.jpg)
Synchronization modes
• async
• recv
• fsync
• apply
![Page 21: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/21.jpg)
Master Slave
Client
COMMIT
“Success”
WAL
fsync
async mode
![Page 22: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/22.jpg)
Master Slave
Client
COMMIT
“Success”
WAL
fsync
WAL
recv
recv mode
![Page 23: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/23.jpg)
Master Slave
Client
COMMIT
“Success”
WAL
fsync
WAL
recv
WAL
fsync
fsync mode
![Page 24: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/24.jpg)
Master Slave
Client
COMMIT
“Success”
WAL
apply (Recovery)
WAL
recvDatabase
fsync
apply mode
![Page 25: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/25.jpg)
Master Slave
fsync recv
✔
✔
✔
fsync
async ✔
apply
✔
recv ✔
fsync ✔
apply ✔ ✔
modesfast
durable
Synchronization mode
![Page 26: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/26.jpg)
Master Slave
Client
COMMIT
“Success”
WAL
recvfsync
My favorite mode
WAL
![Page 27: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/27.jpg)
Fail Over Split
Master Slave
Client
Master Slave
Client
![Page 28: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/28.jpg)
Online Re-sync
Master
Slave
Client
![Page 29: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/29.jpg)
Built-in
• Easy to install and use
• Highly active community
![Page 31: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/31.jpg)
Hot Standby
PRIMARY
postgres
startupDB
STANDBY
Run querieswhile still in recovery
User
Transaction Log Shipping
![Page 32: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/32.jpg)
Hot Standby Overview
• Allows users to connect in read-only mode– Allowed: SELECT, SET, LOAD, COMMIT/ROLLBACK– Disallowed: INSERT, UPDATE, DELETE, CREATE, 2PC,– SELECT … FOR SHARE/UPDATE, nextval(), LOCK– No admin commands:
ANALYZE, VACUUM, REINDEX, GRANT
• Simple configuration– recovery_connections = on # default on
• Performance Overhead– Master: <0.1% overhead from additional WAL– Standby: 2% CPU overhead
• Queries continue running when exit recovery
![Page 33: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/33.jpg)
Hot Standby Query Conflicts
• Master: Connections can interfere and deadlock• Standby: Queries can conflict with recovery
– Recovery always wins
• Causes of conflicts– Cleanup records (HOT, VACUUM)– Btree cleanup records are a problem!– DROP DATABASE, DROP TABLESPACE
• Conflict resolution– Wait, then Cancel – set with max_standby_delay
![Page 34: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/34.jpg)
How does it work?
• Read-only transactions forced• Snapshot data emulated on standby
– Minimal information inferred from WAL
• Locks held only for AccessExclusiveLocks• Cache invalidations• Careful analysis of conflicts
![Page 35: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/35.jpg)
Project Deliverables
• Virtual Transactions (8.3) (Florian/Tom)
• Atomic Subtransactions (8.4) (Simon)
• Database consistent state (8.4) (Simon/Heikki)
• Bgwriter active during recovery (8.4) (Simon/Heikki)
• Removal of DB/Auth Flat File (8.5) (Tom)
• Main Hot Standby patch (8.5) (Simon/Heikki)
• Removal of Non-Transactional Cache Inval(Tom!)
• Advanced PITR functions (8.5) (Simon)
![Page 36: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/36.jpg)
Project Overview
Touches ~80 files, >10,000 linesEffort− Analysis & Dev ~7 man months from Simon− Testing by 5 staff in 2ndQuadrant, led by
Gianni Ciolli− Lengthy review by Heikki Linnakangas
Changes− Around 50% of bugs found by code inspection− > 50 changes and enhancements as a result of
refactoring, review and discussion
![Page 37: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/37.jpg)
Demo
![Page 38: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/38.jpg)
Scenario
• Configuration
• Checking of basic features
• Failover
![Page 39: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/39.jpg)
Configuration
Masterport = 5432
Slaveport = 9999
$HOME
master -- $PGDATA
archive_master -- archival area
slave -- $PGDATA
archive_slave -- archival area
host = 192.168.0.99
![Page 40: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/40.jpg)
1. Create the initial database cluster in the master as usual
$ initdb –D master -–locale=C --encoding=UTF8
2. Enable XLOG archiving
$ mkdir archive_master$ emacs master/postgresql.confarchive_mode = onarchive_command = ‘cp %p ../archive_master/%f’
Configuration
![Page 41: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/41.jpg)
3. Set the maximum number of concurrent connections from the slaves
$ emacs master/postgresql.confmax_wal_senders = 5
4. Set up connections and authentication
$ emacs master/postgresql.conflisten_addresses = ‘192.168.0.99’
$ emacs master/pg_hba.confhost replication postgres 192.168.0.99/32 trust
Configuration
![Page 42: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/42.jpg)
5. Start postgres on the master
$ pg_ctl –D master start
6. Make a base backup, load it onto the slave
$ psql –p5432 –c”SELECT pg_start_backup(‘demo’, true)”$ cp –r master slave$ psql –p5432 –c”SELECT pg_stop_backup()”
Configuration
![Page 43: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/43.jpg)
7. Change the slave’s configuration
$ rm slave/postmaster.pid$ mkdir archive_slave$ emacs slave/postgresql.confport = 9999archive_command = ‘cp %p ../archive_slave/%f’
Configuration
![Page 44: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/44.jpg)
8. Create a recovery.conf in the slave
$ emacs slave/recovery.confstandby_mode = ‘on’primary_conninfo = ‘host=192.168.0.99 port=5432
user=postgres’trigger_file = ‘../trigger’
9. Start postgres on the slave
$ pg_ctl –D slave start
Configuration
![Page 45: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/45.jpg)
• Session1 on master$ psql –p5432=# CREATE TABLE demo (i int);=# INSERT INTO demo VALUES (generate_series(1,100));
//write queries can be executed on master=# SELECT count(*) FROM demo;
//read queries also can be executed on master
• Session1 on slave$ psql –p9999=# SELECT count(*) FROM demo;
//read queries can be executed on slave=# INSERT INTO demo VALUES (9999);
//error occurs: write queries cannot be executed on slave
Checking of basic features
![Page 46: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/46.jpg)
• Session1 on slave=# BEGIN;=# SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
//cannot see the transaction which starts after this=# SELECT count(*) FROM demo;
• Session1 on master=# INSERT INTO demo VALUES(generate_series(1, 100));
• Session1 on slave=# SELECT count(*) FROM demo;
//result=100, cannot see the recent insertion on master because of serializable isolation level
• Session 2 on slave=# SELECT count(*) FROM demo;
//result=200, the recent insertion is visible
Correct handling of snapshots
![Page 47: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/47.jpg)
• Session1 on master=# BEGIN;=# LOCK TABLE demo;=# SELECT pg_switch_xlog();
//required to ship the WAL of “LOCK TABLE” to slave
• Session1 on slave=# SELECT count(*) FROM demo;
//sleep until “LOCK TABLE” is committed on master
• Sessionn2 on slave=# SELECT current_query, waiting FROM pg_stat_activity;
//shows query waiting
• Session1 on master#= COMMIT;
//the waiting query gets up
Lock propagation
![Page 48: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/48.jpg)
• Let’s see the query is still running when failover completes
• Session1 on slave=# SELECT pg_sleep(20);
• Kill the master’s postmaster$ pg_ctl –D master –mi stop
• Bring the slave up$ touch trigger$ psql –p9999#= SELECT current_query FROM pg_stat_activity;
//can see pg_sleep is still running#= INSERT INTO demo VALUES(9999);
//write queries can be executed because slave becomes master
Failover
![Page 49: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/49.jpg)
Ending
![Page 50: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/50.jpg)
Road to v8.5
• Needs your help
![Page 51: Streaming Replication Hot Standby - PostgreSQL · • Implementing Streaming Replication. History. Historical policy • Avoid putting replication into core Postgres • No "one size](https://reader033.vdocuments.net/reader033/viewer/2022053100/6057d865d8659f43c4132677/html5/thumbnails/51.jpg)
Postgres