Pgconfig Slony
Pgconfig Slony
Pgconfig Slony
French translator of the PostgreSQL manual Member of pgAdmin's team Vice-treasurer of PostgreSQL Europe CTO of Dalibo Mail: [email protected] Twitter: g_lelarge Blog: http://blog.guillaume.lelarge.info
Agenda
What's Slony?
Replication system
One master Multiple slaves Asynchronous By trigger Nodes cascade Read-only queries on replicated tables Read/write for everything else
Slaves available
Developers
Jan Wieck Christopher Browne Steve Singer Devrim Gunduz (RPM packages)
More detail
Use cases
High-availability
Pooling may be done with pgpool or pgbouncer Load balancing may be done with pgpool
Concepts
Concepts - Triggers
Catches data changes on the master Prevents data changes on the slaves Allows finer grain in the replication
Concepts - Cluster
Concepts - Node
Each PostgreSQL instance is a node Each node is declared in the cluster A node has a specific schema containing Slony catalog tables
A replication set is a set of tables that will be replicated A set has one master node, and one or more slaves node You can have several sets in the same cluster They can have different master node, and different slaves Tables of the set will atuomatically acquire triggers
Concepts Master/Slave
Master node
Slave node
adding and removing nodes adding and removing sets merging sets adding and removing subscriptions switchover / failover etc.
Limitations
Logical structure (schema, table, ...) Physical structure (tablespace) Users and roles
Two branches
1.2
Works with PostgreSQL 7.3 to 9.1 Works with PostgreSQL 8.3 to 9.1
2.0
Installation
Prerequisites Sources
Binary
Sources - Prerequisites
Sources - Download
http://slony.info/downloads Choose the branch Choose source Grab one of the tar files For example:
wget http://slony.info/downloads/2.0/source/slony1-2.0.7.tar.bz2
Sources - Extract
Quite simple:
tar xvfj slony1-2.0.7.tar.bz2
Sources - Configure
./configure options
--prefix to specify the installation directory --with-pgconfigdir to specify the location of the pg_config tool --with-perltools to add perl tools And a few more less interesting
./configure \ --with-perltools \ --with-pgconfigdir=/opt/postgresql-9.1/bin \ --prefix=/opt/slony-2.0
Example
Sources Compile
Quite simple
make
Sources - Install
Binary - RPM
Binary - DEB
Example:
# aptitude install slony1-2-bin postgresql-8.4-slony1
Binary - Windows
With StackBuilder
Prerequisites
Some examples
Three examples
Slon connects to the database It's better to have a specific PostgreSQL user Must be a superuser Should have a password Simple command:
CREATE USER slony PASSWORD 'supersecret';
Contents:
10.0.0.1:5432:b1:slony:supersecret 10.0.0.2:5432:b1:slony:supersecret
We use pgbench
Only way to select safely a row Let's see the tables with no PKEY:
SELECT relname FROM pg_class WHERE relkind='r' AND NOT relhaspkey; relname ----------------pgbench_history
Install PL/pgsql
We need to write a little script It helps Slony know what to do Compose of a few steps
Declare cluster, and nodes Initialize cluster Create set and add tables to set Add node, and paths Subscribe slave node to set
Declare cluster
cluster name = 'repli1';
Declare nodes
node 1 admin conninfo='dbname=b1 host=10.0.0.1 user=slony'; node 2 admin conninfo='dbname=b1 host=10.0.0.2 user=slony';
Create a set
create set (id=1, origin=1, comment='All tables and sequences from b1');
Add a slave
store node (id=2, comment='Amsterdam', event node=1);
What happened?
New schema _repli1 Lots of tables in that schema Triggers on replicated tables
_repli1 for this example sl_node, sl_path, sl_tables, sl_sequences, ... sl_log_1, sl_log_2, sl_event, ...
Dictionnary tables
Work tables
cluster_name conn_info
And it works!
Each (data) change on the master will be recorded by the trigger in the log tables And the slon daemon will push them to the slave Each (data) change on the slave will be denied by the trigger
How to add a table How to remove a table How to add another PostgreSQL object How to add a new slave How to do a switchover How to do a failover How to monitor
Add a table
Add the new table to every slave Create a new set for the new table Add the new table to the new set Subscribe the slave to the new set Wait for every events to be confirmed Merge the new set with the old one
On the master:
CREATE TABLE t1 ( c1 serial PRIMARY KEY, c2 text );
Now, create a new set for the new table and sequence
create set (id=2, origin=1, comment='Temporary set to add the new table from b1');
Be careful
Set's id must be max of all sets' id The same for table's id and sequence's id Quick way to find out
Now, we have to force a pause until all replication events are confirmed by the slave
wait for event (origin = 1, confirmed = all, wait on = 1);
Drop a table
Drop the table from the replication Then drop the table
We now need to drop it from the database We can do it manually on each node Or via the EXECUTE SCRIPT slonik command
cluster name = 'repli1'; node 1 admin conninfo='dbname=b1 host=10.0.0.1 user=slony'; node 2 admin conninfo='dbname=b1 host=10.0.0.2 user=slony'; execute script (set id = 1, filename = 'script.sql', event node = 1);
So you can't do it manually You must use slonik Also works for renaming a column, or dropping it Don't drop the primary key!
Create a slonik script file that executes the script on all the nodes
cluster name = 'repli1'; node 1 admin conninfo='dbname=b1 host=10.0.0.1 user=slony'; node 2 admin conninfo='dbname=b1 host=10.0.0.2 user=slony'; execute script (set id = 1, filename = 'drop_column_script.sql', event node = 1);
Add them manually Or, much better, with the EXECUTE SCRIPT And that's all
Restore schema in new slave Create a slonik script to add the new node, and subscribe to the set And we're done!
Add the slony PostgreSQL user Restore the schema (from master to the new node)
pg_dump -s b1 | psql -h 10.0.0.3 b1
Switchover
We want the slave to become the master node But the master node is still alive, and we want it to become a slave node This is switchover More exactly, we want to move the origin of a set
failover
Master node died We want the slave to become the master node This is failover More exactly, we want to move the origin of a set when the current origin is dead
Monitoring
Checking lag
./check_postgres.pl --action slony_status --host=192.168.56.101,192.168.56.102 --dbname=b1 --dbuser=slony --warning 60 --critical 120 POSTGRES_SLONY_STATUS OK: DB "b1" schema:_repli1 (host:192.168.56.101) Slony lag time: 10 (10 seconds) DB "b1" schema:_repli1 (host:192.168.56.102) Slony lag time: 11 (11 seconds) | time=0.04s 'b1._repli1 Node 1(Paris) -> Node 2(Amsterdam)'=10;60;300 time=0.07s 'b1._repli1 Node 2(Amsterdam) -> Node 1(Paris)'=11;60;300
Tools
altperl slony1-ctl
Tools - altperl
Tools - slony1-ctl
http://pgfoundry.org/projects/slony1-ctl
slony_include.h
Paths for directories, and files List of nodes with cluster name, database, host, port List of sets, masters, and slaves Config file for the slon daemon
bases.h
relations.h
slon.cfg
Switchover
Failover
What's next?
Add TRUNCATE trigger support Add column replication support Add application name support Add monitoring thread New features for altperl tools (bulk adding of tables and sequences) Etc...
Conclusion
Fine-grain replication Slaves can have other objects No limitation on PostgreSQL version, or architecture No DDL replication No large object replication