1
1
Exercises for PostgreSQL Replication Tutorial
2
2
=============================================
3
3
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
+
4
15
Start Vagrant and Tmux
5
16
----------------------
6
17
@@ -24,15 +35,16 @@ basic 2-node replication
24
35
------------------------
25
36
26
37
cd 9.3
38
+ ls
27
39
pg_basebackup --help | less
28
40
pg_basebackup -x -P -D replica1
29
41
cp /setup/postgres/replica1/* replica1/
30
42
pg_ctl -D replica1 start
31
43
ps -aux | grep postgres
32
- psql -p 5432 libdata
44
+ psql -p 5432 earth
33
45
\dt
34
46
\q
35
- psql -p 5433 libdata
47
+ psql -p 5433 earth
36
48
\dt
37
49
\q
38
50
createdb -p 5432 -U bench bench
@@ -52,6 +64,8 @@ $EDITOR master/postgresql.conf
52
64
$EDITOR replica1/postgresql.conf
53
65
$EDITOR replica1/recovery.conf
54
66
67
+ (here we are just looking at the contents of these files, not modifying them)
68
+
55
69
Administering replication
56
70
-------------------------
57
71
@@ -60,13 +74,17 @@ Administering replication
60
74
cd /setup/pgbench
61
75
./runbench.sh
62
76
^b n
63
- psql -p 5432
77
+ psql -p 5432 bench
78
+ select count(*) from pgbench_history;
79
+ (repeat above several times)
64
80
\x
65
81
select * from pg_stat_activity;
66
82
select * from pg_stat_replication;
67
83
select pg_xlog_location_diff(write_location,replay_location) from pg_stat_replication;
84
+ (repeat above 2-4 times)
68
85
\q
69
- psql -p 5433
86
+ psql -p 5433 bench
87
+ select count(*) from pgbench_history;
70
88
select pg_is_in_recovery();
71
89
select pg_last_xlog_receive_location();
72
90
select pg_last_xlog_receive_location(), pg_last_xlog_replay_location();
@@ -75,11 +93,9 @@ select pg_last_xact_replay_timestamp();
75
93
select now() - pg_last_xact_replay_timestamp();
76
94
select pg_xlog_replay_pause();
77
95
select pg_xlog_location_diff(pg_last_xlog_receive_location(), pg_last_xlog_replay_location());
96
+ (repeat above twice)
78
97
select now() - pg_last_xact_replay_timestamp();
79
98
select pg_xlog_replay_resume();
80
- \c bench
81
- select count(*) from pgbench_history;
82
- (repeat above several times)
83
99
\q
84
100
85
101
Replication and Security
@@ -90,10 +106,13 @@ create role replicator password 'replicate' login replication;
90
106
\du
91
107
\q
92
108
$EDITOR master/pg_hba.conf
109
+ (comment out postgres - replication lines)
93
110
$EDITOR replica1/recovery.conf
111
+ (change primary_conninfo to use replicator user)
94
112
cp /setup/postgres/.pgpass ~/
95
113
chmod 700 ~/.pgpass
96
114
$EDITOR ~/.pgpass
115
+ (check password info for replicator user)
97
116
pg_ctl -D master reload
98
117
pg_ctl -D replica1 restart
99
118
ps -aux | grep replicator
@@ -111,7 +130,9 @@ Cloning and Archiving
111
130
pg_ctl -D replica1 stop
112
131
rm -rf replica1/*
113
132
$EDITOR master/postgresql.conf
133
+ (edit to enable archiving by uncommenting the "archive" lines)
114
134
pg_ctl -D master restart
135
+ ps aux | grep archiver
115
136
^b n
116
137
runbench.sh
117
138
^b n
@@ -128,36 +149,40 @@ cp /setup/postgres/replica1/* replica1/
128
149
cp replica1/recovery.conf.replica1.archiving replica1/recovery.conf
129
150
$EDITOR replica1/recovery.conf
130
151
pg_ctl -D replica1 start
152
+ ps aux | grep startup
131
153
psql -p 5432
132
154
create table test(test text);
133
155
select * from pg_stat_replication;
134
156
\q
135
157
tail -f /var/log/postgresql/postgresql-replica1
136
158
psql -p 5433
137
159
\dt
160
+ (repeat above until "test" table appears)
138
161
\c bench
139
162
select count(*) from pgbench_history;
140
- (repeat above several times)
163
+ (repeat above several times until result changes )
141
164
\q
142
165
143
166
Dual Replication
144
167
----------------
145
168
146
- $EDITOR master/postgresql.conf
147
169
cp replica1/recovery.conf.replica1.dual replica1/recovery.conf
170
+ $EDITOR replica1/recovery.conf
148
171
^b n
149
- ^c (kill pgbench)
150
- ^b n
151
- pg_ctl -D master restart
152
172
pg_ctl -D replica1 restart
153
173
tail -f /var/log/postgresql/postgresql-replica1
154
174
^c
175
+ psql -p 5432
176
+ select * from pg_stat_replication;
177
+ \q
178
+
155
179
156
180
Failover and Failback
157
181
---------------------
158
182
159
183
^b n
160
- runbench.sh
184
+ (check if runbench is still running, if not run the below)
185
+ ./runbench.sh
161
186
^b n
162
187
pg_ctl -D master -m immediate stop
163
188
^b n
@@ -169,27 +194,27 @@ select pg_is_in_recovery();
169
194
create table test2(test text);
170
195
\q
171
196
^b n
172
- runbench_replica1.sh
197
+ ./ runbench_replica1.sh
173
198
^b n
174
199
rm -rf master/*
175
200
pg_basebackup -x -P -p 5433 -U replicator -D master
176
201
cp /setup/postgres/master/* master/
177
202
cp replica1/recovery.done master/recovery.conf
178
203
$EDITOR master/recovery.conf
204
+ (edit recovery.conf to connect to replica1)
179
205
pg_ctl -D master start
180
- psql -p 5433
181
206
tail -f /var/log/postgresql/postgresql-master
207
+ ^c
208
+ psql -p 5433
182
209
select * from pg_stat_replication;
210
+ \q
211
+ pg_ctl -D replica1 -m fast stop
183
212
touch master/PROMOTE
184
213
(wait)
185
214
psql -p 5432
186
215
select pg_is_in_recovery();
187
216
\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
193
218
pg_ctl -D replica1 start
194
219
psql -p 5432
195
220
select * from pg_stat_replication;
@@ -199,27 +224,32 @@ Query Lag
199
224
---------
200
225
201
226
$EDITOR replica1/postgresql.conf
227
+ (look at streaming_delay settings)
202
228
psql -p 5433 libdata
203
229
begin;
204
230
set transaction_isolation = 'repeatable read';
205
231
select * from copies;
232
+ q
206
233
^b n
207
- psql -p 5432 libdata
234
+ psql -U postgres - p 5432 libdata
208
235
vacuum full copies;
209
236
select * from pg_stat_replication;
210
237
select pg_xlog_location_diff(write_location, replay_location) from pg_stat_replication;
211
238
^b n
212
239
select * from copies;
213
- wait 10 sec.
240
+ ( wait 10-20 sec. )
214
241
select * from copies;
242
+ ( repeat above until error )
215
243
\q
216
244
^b n
217
245
\q
246
+ ^b n (postgres shell)
218
247
219
248
Synchronous Replication
220
249
-----------------------
221
250
222
251
$EDITOR master/postgresql.conf
252
+ (add "replica1" to list of synchronous_standby_names, and turn on hot_standby_feedback)
223
253
pg_ctl -D master reload
224
254
^b n
225
255
runbench.sh
@@ -247,6 +277,7 @@ insert into test values ('test4');
247
277
commit;
248
278
\q
249
279
$EDITOR master/postgresql.conf
280
+ (remove synchronous replica)
250
281
pg_ctl -D master reload
251
282
pg_ctl -D replica1 start
252
283
@@ -263,12 +294,16 @@ psql -p 5433
263
294
select * from pg_stat_replication;
264
295
\q
265
296
psql -p 5434
266
- select * from pg_stat_replication ;
297
+ select pg_is_in_recovery() ;
267
298
\q
268
299
pg_ctl -D replica1 promote
300
+ psql -p 5432
301
+ select * from pg_stat_replication;
302
+ \q
269
303
psql -p 5433
270
304
select * from pg_stat_replication;
271
305
\q
306
+ pg_ctl -D master stop
272
307
273
308
pgbouncer LB/Failover
274
309
---------------------
@@ -284,18 +319,28 @@ psql -p 6432 bench_ro
284
319
show port;
285
320
drop table branches2;
286
321
\q
287
- pg_ctl -D replica1 -m immediate stop
322
+ pg_ctl -D replica1 -m fast stop
288
323
pg_ctl -D replica2 promote
289
324
psql -p 6432 bench
290
325
^b n
291
326
$EDITOR /etc/pgbouncer/pgbouncer.ini
327
+ (edit the rw connections so that the point to replica2)
292
328
service pgbouncer restart
293
329
^b n
294
330
psql -p 6432 bench
295
331
show port;
296
332
drop table branches2;
297
333
\q
298
334
335
+ Exit
336
+ ----
337
+
338
+ ^b d
339
+ exit
340
+ exit
341
+ vagrant halt (or destroy)
342
+
343
+
299
344
300
345
301
346
0 commit comments