Skip to content

Commit c2126c3

Browse files
author
Josh Berkus
committed
Version 0.3, set up for FOSS4G, which means it has PostGIS. Interactives removed from the tutorial because in practice they took up way too much time. Various corrections and improvements to the exercises. New information about 9.4 added.
1 parent 9373e95 commit c2126c3

File tree

10 files changed

+123
-240
lines changed

10 files changed

+123
-240
lines changed

README.md

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ pgReplicationTutorial
44
This repository contains files for the PostgreSQL Binary
55
Replication tutorial.
66

7-
**NOT YET UPDATED FOR FOSS4G. Attendees will be notified when
8-
the new version of the tutorial is available.**
7+
This tutorial has been updated with PostGIS for FOSS4G 2014
98

109
These files are required to perform the hands-on exercises.
1110
Importantly, there is significant setup required in order to
@@ -32,24 +31,43 @@ Software and Wetware:
3231
* familiarity with one or more command-line text editors
3332
* vagrant and virtualbox (see below)
3433

35-
Installing Vagrant and VirtualBox
36-
=================================
34+
== Installing the Base Software ==
3735

38-
Go to http://www.vagrantup.com/
36+
First, you will need to install Vagrant and VirtualBox if you do not
37+
already have them. If you do already have them, please make sure that
38+
you have at least these minimum versions:
3939

40-
The Vagrant website has download packages, instructions
41-
on how to install and configure vagrant on various OSes, and
42-
a "getting started" guide. Please install and configure
43-
vagrant _right away_; this will take around an hour.
40+
* Vagrant 1.4.1 or later
41+
* VirtualBox 4.2 or later
4442

45-
You will also need VirtualBox. The [Vagrant website has
46-
instructions on installing VirtualBox as
47-
well](http://docs.vagrantup.com/v2/virtualbox/index.html)
43+
Otherwise, you're going to need to install them. Fortunately, both have
44+
downloads for most OSes and extensive documentation. And when you get
45+
used to them, you'll find Vagrant an invaluable tool for testing.
4846

49-
Note that you need to have vagrant 1.2.5 or later, so you may
50-
need to upgrade even if you already had Vagrant installed.
51-
If you use an earlier version of vagrant, "vagrant up" will
52-
hang after it finishes provisioning.
47+
Vagrant Installation:
48+
49+
* https://docs.vagrantup.com/v2/installation/
50+
* https://docs.vagrantup.com/v2/getting-started/
51+
52+
VirtualBox Installation:
53+
54+
* https://www.virtualbox.org/wiki/Downloads
55+
* https://www.virtualbox.org/wiki/End-user_documentation
56+
57+
== Installing the Virtual Machine Image ==
58+
59+
Once you've installed Vagrant and VirtualBox, you'll need to get an
60+
operating system image, or "box". Please download one of the following
61+
large files:
62+
63+
If you have a 64-bit laptop: http://files.vagrantup.com/precise64.box
64+
65+
If you have a 32-bit laptop: http://files.vagrantup.com/precise32.box
66+
67+
Then run the following command from the folder where you downloaded it:
68+
69+
* 64-bit: vagrant box add precise precise64.box
70+
* 32-bit: vagrant box add precise precise32.box
5371

5472
Installing Tutorial Exercises
5573
=============================
@@ -100,19 +118,6 @@ for the tutorial:
100118

101119
vagrant suspend
102120

103-
32-Bit Machines
104-
===============
105-
106-
The exercises have not been tested on a 32-bit VM. However, it's quite possible
107-
that they will work that way. The way to switch to 32-bit is:
108-
109-
1. open the file "VagrantFile" in a text editor
110-
2. change every instance of "precise64" to "precise32"
111-
3. save
112-
4. run "vagrant up"
113-
114-
If it does not work on a 32-bit machine, please contact josh@pgexperts.com.
115-
116121
Other Files In This Package
117122
===========================
118123

@@ -132,21 +137,17 @@ pgReplicationTutorial.odt/pdf
132137

133138
These are copies of the slides for the tutorial.
134139

135-
interactives.txt
136-
----------------
140+
ChangeLog
141+
=========
137142

138-
These are script "notes" for interactive exercises to be done with
139-
participants on stage to demonstrate various replication concepts as
140-
metaphor. Right now, these notes are not that understandable on their
141-
own.
143+
**0.3** Version produced for FOSS4G 2014. Includes information about replicating PostGIS. Interactive "play" demonstrations have have been removed due to their time-consuming nature.
142144

143-
The interactions require a deck of playing cards, three ropes,
144-
and two hats.
145+
**0.2** Initial version produced for DjangoCon 2013.
145146

146147
License
147148
=======
148149

149-
The pgReplicationTutorial is Copyright 2013 Josh Berkus
150+
The pgReplicationTutorial is Copyright 2013-2014 Josh Berkus
150151
and PostgreSQL Experts Inc.
151152

152153
All slides, text, instructions and similar content in this tutorial are

tutorial/exercises.txt

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
Exercises for PostgreSQL Replication Tutorial
22
=============================================
33

4+
$EDITOR below is the editor of your choice. Options include:
5+
6+
* vi
7+
* nano (recommended if you're unfamiliar with Linux editors)
8+
* jmacs (emacs clone)
9+
* joe
10+
11+
Lines in (parentheses) are comments and shouldn't be pasted/typed in.
12+
13+
The ^ carat symbol is short for the [Ctrl] key. Thus ^c is "hold down [Ctrl], press [c]".
14+
415
Start Vagrant and Tmux
516
----------------------
617

@@ -24,15 +35,16 @@ basic 2-node replication
2435
------------------------
2536

2637
cd 9.3
38+
ls
2739
pg_basebackup --help | less
2840
pg_basebackup -x -P -D replica1
2941
cp /setup/postgres/replica1/* replica1/
3042
pg_ctl -D replica1 start
3143
ps -aux | grep postgres
32-
psql -p 5432 libdata
44+
psql -p 5432 earth
3345
\dt
3446
\q
35-
psql -p 5433 libdata
47+
psql -p 5433 earth
3648
\dt
3749
\q
3850
createdb -p 5432 -U bench bench
@@ -52,6 +64,8 @@ $EDITOR master/postgresql.conf
5264
$EDITOR replica1/postgresql.conf
5365
$EDITOR replica1/recovery.conf
5466

67+
(here we are just looking at the contents of these files, not modifying them)
68+
5569
Administering replication
5670
-------------------------
5771

@@ -60,13 +74,17 @@ Administering replication
6074
cd /setup/pgbench
6175
./runbench.sh
6276
^b n
63-
psql -p 5432
77+
psql -p 5432 bench
78+
select count(*) from pgbench_history;
79+
(repeat above several times)
6480
\x
6581
select * from pg_stat_activity;
6682
select * from pg_stat_replication;
6783
select pg_xlog_location_diff(write_location,replay_location) from pg_stat_replication;
84+
(repeat above 2-4 times)
6885
\q
69-
psql -p 5433
86+
psql -p 5433 bench
87+
select count(*) from pgbench_history;
7088
select pg_is_in_recovery();
7189
select pg_last_xlog_receive_location();
7290
select pg_last_xlog_receive_location(), pg_last_xlog_replay_location();
@@ -75,11 +93,9 @@ select pg_last_xact_replay_timestamp();
7593
select now() - pg_last_xact_replay_timestamp();
7694
select pg_xlog_replay_pause();
7795
select pg_xlog_location_diff(pg_last_xlog_receive_location(), pg_last_xlog_replay_location());
96+
(repeat above twice)
7897
select now() - pg_last_xact_replay_timestamp();
7998
select pg_xlog_replay_resume();
80-
\c bench
81-
select count(*) from pgbench_history;
82-
(repeat above several times)
8399
\q
84100

85101
Replication and Security
@@ -90,10 +106,13 @@ create role replicator password 'replicate' login replication;
90106
\du
91107
\q
92108
$EDITOR master/pg_hba.conf
109+
(comment out postgres - replication lines)
93110
$EDITOR replica1/recovery.conf
111+
(change primary_conninfo to use replicator user)
94112
cp /setup/postgres/.pgpass ~/
95113
chmod 700 ~/.pgpass
96114
$EDITOR ~/.pgpass
115+
(check password info for replicator user)
97116
pg_ctl -D master reload
98117
pg_ctl -D replica1 restart
99118
ps -aux | grep replicator
@@ -111,7 +130,9 @@ Cloning and Archiving
111130
pg_ctl -D replica1 stop
112131
rm -rf replica1/*
113132
$EDITOR master/postgresql.conf
133+
(edit to enable archiving by uncommenting the "archive" lines)
114134
pg_ctl -D master restart
135+
ps aux | grep archiver
115136
^b n
116137
runbench.sh
117138
^b n
@@ -128,36 +149,40 @@ cp /setup/postgres/replica1/* replica1/
128149
cp replica1/recovery.conf.replica1.archiving replica1/recovery.conf
129150
$EDITOR replica1/recovery.conf
130151
pg_ctl -D replica1 start
152+
ps aux | grep startup
131153
psql -p 5432
132154
create table test(test text);
133155
select * from pg_stat_replication;
134156
\q
135157
tail -f /var/log/postgresql/postgresql-replica1
136158
psql -p 5433
137159
\dt
160+
(repeat above until "test" table appears)
138161
\c bench
139162
select count(*) from pgbench_history;
140-
(repeat above several times)
163+
(repeat above several times until result changes)
141164
\q
142165

143166
Dual Replication
144167
----------------
145168

146-
$EDITOR master/postgresql.conf
147169
cp replica1/recovery.conf.replica1.dual replica1/recovery.conf
170+
$EDITOR replica1/recovery.conf
148171
^b n
149-
^c (kill pgbench)
150-
^b n
151-
pg_ctl -D master restart
152172
pg_ctl -D replica1 restart
153173
tail -f /var/log/postgresql/postgresql-replica1
154174
^c
175+
psql -p 5432
176+
select * from pg_stat_replication;
177+
\q
178+
155179

156180
Failover and Failback
157181
---------------------
158182

159183
^b n
160-
runbench.sh
184+
(check if runbench is still running, if not run the below)
185+
./runbench.sh
161186
^b n
162187
pg_ctl -D master -m immediate stop
163188
^b n
@@ -169,27 +194,27 @@ select pg_is_in_recovery();
169194
create table test2(test text);
170195
\q
171196
^b n
172-
runbench_replica1.sh
197+
./runbench_replica1.sh
173198
^b n
174199
rm -rf master/*
175200
pg_basebackup -x -P -p 5433 -U replicator -D master
176201
cp /setup/postgres/master/* master/
177202
cp replica1/recovery.done master/recovery.conf
178203
$EDITOR master/recovery.conf
204+
(edit recovery.conf to connect to replica1)
179205
pg_ctl -D master start
180-
psql -p 5433
181206
tail -f /var/log/postgresql/postgresql-master
207+
^c
208+
psql -p 5433
182209
select * from pg_stat_replication;
210+
\q
211+
pg_ctl -D replica1 -m fast stop
183212
touch master/PROMOTE
184213
(wait)
185214
psql -p 5432
186215
select pg_is_in_recovery();
187216
\q
188-
pg_ctl -D replica1 -m immedate stop
189-
rm -rf replica1/*
190-
pg_basebackup -x -P -p 5432 -U replicator -D replica1
191-
cp /setup/postgres/replica1/* replica1/
192-
cp replica1/recovery.conf.replica1.dual replica1/recovery.conf
217+
mv replica1/recovery.done replica1/recovery.conf
193218
pg_ctl -D replica1 start
194219
psql -p 5432
195220
select * from pg_stat_replication;
@@ -199,27 +224,32 @@ Query Lag
199224
---------
200225

201226
$EDITOR replica1/postgresql.conf
227+
(look at streaming_delay settings)
202228
psql -p 5433 libdata
203229
begin;
204230
set transaction_isolation = 'repeatable read';
205231
select * from copies;
232+
q
206233
^b n
207-
psql -p 5432 libdata
234+
psql -U postgres -p 5432 libdata
208235
vacuum full copies;
209236
select * from pg_stat_replication;
210237
select pg_xlog_location_diff(write_location, replay_location) from pg_stat_replication;
211238
^b n
212239
select * from copies;
213-
wait 10 sec.
240+
( wait 10-20 sec. )
214241
select * from copies;
242+
( repeat above until error )
215243
\q
216244
^b n
217245
\q
246+
^b n (postgres shell)
218247

219248
Synchronous Replication
220249
-----------------------
221250

222251
$EDITOR master/postgresql.conf
252+
(add "replica1" to list of synchronous_standby_names, and turn on hot_standby_feedback)
223253
pg_ctl -D master reload
224254
^b n
225255
runbench.sh
@@ -247,6 +277,7 @@ insert into test values ('test4');
247277
commit;
248278
\q
249279
$EDITOR master/postgresql.conf
280+
(remove synchronous replica)
250281
pg_ctl -D master reload
251282
pg_ctl -D replica1 start
252283

@@ -263,12 +294,16 @@ psql -p 5433
263294
select * from pg_stat_replication;
264295
\q
265296
psql -p 5434
266-
select * from pg_stat_replication;
297+
select pg_is_in_recovery();
267298
\q
268299
pg_ctl -D replica1 promote
300+
psql -p 5432
301+
select * from pg_stat_replication;
302+
\q
269303
psql -p 5433
270304
select * from pg_stat_replication;
271305
\q
306+
pg_ctl -D master stop
272307

273308
pgbouncer LB/Failover
274309
---------------------
@@ -284,18 +319,28 @@ psql -p 6432 bench_ro
284319
show port;
285320
drop table branches2;
286321
\q
287-
pg_ctl -D replica1 -m immediate stop
322+
pg_ctl -D replica1 -m fast stop
288323
pg_ctl -D replica2 promote
289324
psql -p 6432 bench
290325
^b n
291326
$EDITOR /etc/pgbouncer/pgbouncer.ini
327+
(edit the rw connections so that the point to replica2)
292328
service pgbouncer restart
293329
^b n
294330
psql -p 6432 bench
295331
show port;
296332
drop table branches2;
297333
\q
298334

335+
Exit
336+
----
337+
338+
^b d
339+
exit
340+
exit
341+
vagrant halt (or destroy)
342+
343+
299344

300345

301346

0 commit comments

Comments
 (0)