postgresql replication sc2011 aug 2011 · streaming replication 2. edit pg_hba.conf on master host...
TRANSCRIPT
Who is Steve
PostgreSQL User since 2000
Author of contrib/dbmirror replication engine (7.x)
A Maintainer of Slony-I replication system
Blog http://scanningpages.wordpress.com
PostgreSQL
BSD Licensed SQL Database
Standards Compliant, Transactions
Large Open Community
Why Replicate
?
Failover
Load Balancing
Thinkarchitecture
database Application
Db1(master) Application
Db3(slave)
Db2(slave)
Read only
Db1
Application
Db2
sales
Application
customers
sales
Db1 Application
Db2Reports
What is important to you
Slony-I
http://www.slony.info
Slony-I
Key Features
Multiple Origin nodes (for different tables)
customers
sales
customers
sales
Slony-I
Key Features
Cascaded Replication
Slony-I
Key Features
sales sales
history
trigger
6 Steps to replicate with Slony
h1 h2
Node 1 Node 2
slon slon
Step 1
Write a preamble file
/tmp/preamble
cluster name=mycluster;node 1 admin conninfo='host=h1 dbname=mydb user=postgres';node 2 admin conninfo='host=h2 dbname=mydb user=postgres';
Step 2
Create The Slave
pg_dump -h h1 -s mydb | psql -h h2 mydb
Step 3
Add Slony to the Database
slonik <<_EOF_include</tmp/preamble>init cluster(id=1);store node(id=2,event node=1);store path(server=1,client=2,
conninfo='host=h1 dbname=mydb user=postgres');store path(server=2,client=1,
conninfo='host=h2 dbname=mydb user=postgres');_EOF_
Step 4
Start the slon daemons
$ slon mycluster 'host=h1 dbname=mydb'
$ slon mycluster 'host=h2 dbname=mydb'
Step 5
Create a replication set
slonik<<_EOF_include</tmp/preamble>create set(id=1,origin=1);set add table(set id=1, origin=1, id=100,
fully qualified name='public.sales');set add table(set id=1, origin=1, id=101,
fully qualified name='public.orders');
Step 6
Subscribe the set
slonik << _EOF_include<'/tmp/preamble'>subscribe set(id=1,provider=1,receiver=2);sync(id=1);wait for event(id=1,confirmed=all,wait on=1);_EOF_
Limitations
DDL Changes are not automatically replicated
10-30% performance impact
Can be complicated
Easy to shoot yourself in the foot
Streaming Replication
PostgreSQL Write Ahead Log (WAL)
base pg_clog global pg_xlog
WAL Log
data directory
data
WAL
Archive
Master
WAL
WAL
Postgres
WAL WAL WAL WAL
data
WAL
Slave
WAL
WAL
Postgres
Streaming ReplicationKey Features
Easy To Setup
Streaming ReplicationKey Features
All SQL is replicated (DDL,BLOGS,...)
Streaming ReplicationKey Features
Limited Performance Impact
5 Steps to Setting up Streaming Replication
1. Edit postgresql.conf (on master)
wal_level=hot_standbyarchive_mode=onarchive_command='rsync %p h2:/home/arch_dir/%f</dev/null'
5 Steps to Setting up Streaming Replication
2. Edit pg_hba.conf on master
host replication myuser 192.168.1.100/32 md5
5 Steps to Setting up Streaming Replication
3. Create a base pg backup
select pg_start_backup('myb');
rsync -r pgsql_data h2:/home/mydata
select pg_stop_backup();
5 Steps to Setting up Streaming Replication
4. Configure slaverecovery.conf
standby_mode='on'primary_conninfo='host=h1 port=5432 user=myuser password=foo'restore_command='cp /home/arch_dir/%f %p'trigger_file=/home/promote_standbyarchive_cleanup_command='pg_archivecleanup /home/arch_dir %r'
postgresql.conf
hot_standby=on
5 Steps to Setting up Streaming Replication
rm /home/mydata/postmaster.pid
5 Start slave
pg_ctl -D /home/mydata start
Avoid Long Running Transactions
Decide what you need to do
Questions ?
Steve Singerhttp://[email protected]: stevenSn
Photo Credits
http://www.flickr.com/photos/toolstop/4325151224
http://www.flickr.com/photos/danielaineurope/990977254
http://www.flickr.com/photos/frazzledjen/148709599
CC-BY-SA