|
1 | 1 | use strict;
|
2 | 2 | use warnings;
|
3 | 3 | use TestLib;
|
4 |
| -use Test::More tests => 4; |
| 4 | +use Test::More tests => 8; |
5 | 5 |
|
6 | 6 | use RewindTest;
|
7 | 7 |
|
8 |
| -my $testmode = shift; |
| 8 | +sub run_test |
| 9 | +{ |
| 10 | + my $test_mode = shift; |
9 | 11 |
|
10 |
| -RewindTest::init_rewind_test('basic', $testmode); |
11 |
| -RewindTest::setup_cluster(); |
| 12 | + RewindTest::init_rewind_test('basic', $test_mode); |
| 13 | + RewindTest::setup_cluster(); |
12 | 14 |
|
13 |
| -# Create a test table and insert a row in master. |
14 |
| -master_psql("CREATE TABLE tbl1 (d text)"); |
15 |
| -master_psql("INSERT INTO tbl1 VALUES ('in master')"); |
| 15 | + # Create a test table and insert a row in master. |
| 16 | + master_psql("CREATE TABLE tbl1 (d text)"); |
| 17 | + master_psql("INSERT INTO tbl1 VALUES ('in master')"); |
16 | 18 |
|
17 |
| -# This test table will be used to test truncation, i.e. the table |
18 |
| -# is extended in the old master after promotion |
19 |
| -master_psql("CREATE TABLE trunc_tbl (d text)"); |
20 |
| -master_psql("INSERT INTO trunc_tbl VALUES ('in master')"); |
| 19 | + # This test table will be used to test truncation, i.e. the table |
| 20 | + # is extended in the old master after promotion |
| 21 | + master_psql("CREATE TABLE trunc_tbl (d text)"); |
| 22 | + master_psql("INSERT INTO trunc_tbl VALUES ('in master')"); |
21 | 23 |
|
22 |
| -# This test table will be used to test the "copy-tail" case, i.e. the |
23 |
| -# table is truncated in the old master after promotion |
24 |
| -master_psql("CREATE TABLE tail_tbl (id integer, d text)"); |
25 |
| -master_psql("INSERT INTO tail_tbl VALUES (0, 'in master')"); |
| 24 | + # This test table will be used to test the "copy-tail" case, i.e. the |
| 25 | + # table is truncated in the old master after promotion |
| 26 | + master_psql("CREATE TABLE tail_tbl (id integer, d text)"); |
| 27 | + master_psql("INSERT INTO tail_tbl VALUES (0, 'in master')"); |
26 | 28 |
|
| 29 | + master_psql("CHECKPOINT"); |
27 | 30 |
|
28 |
| -master_psql("CHECKPOINT"); |
| 31 | + RewindTest::create_standby(); |
29 | 32 |
|
30 |
| -RewindTest::create_standby(); |
| 33 | + # Insert additional data on master that will be replicated to standby |
| 34 | + master_psql("INSERT INTO tbl1 values ('in master, before promotion')"); |
| 35 | + master_psql("INSERT INTO trunc_tbl values ('in master, before promotion')"); |
| 36 | + master_psql("INSERT INTO tail_tbl SELECT g, 'in master, before promotion: ' || g FROM generate_series(1, 10000) g"); |
31 | 37 |
|
32 |
| -# Insert additional data on master that will be replicated to standby |
33 |
| -master_psql("INSERT INTO tbl1 values ('in master, before promotion')"); |
34 |
| -master_psql("INSERT INTO trunc_tbl values ('in master, before promotion')"); |
35 |
| -master_psql("INSERT INTO tail_tbl SELECT g, 'in master, before promotion: ' || g FROM generate_series(1, 10000) g"); |
| 38 | + master_psql('CHECKPOINT'); |
36 | 39 |
|
37 |
| -master_psql('CHECKPOINT'); |
| 40 | + RewindTest::promote_standby(); |
38 | 41 |
|
39 |
| -RewindTest::promote_standby(); |
| 42 | + # Insert a row in the old master. This causes the master and standby |
| 43 | + # to have "diverged", it's no longer possible to just apply the |
| 44 | + # standy's logs over master directory - you need to rewind. |
| 45 | + master_psql("INSERT INTO tbl1 VALUES ('in master, after promotion')"); |
40 | 46 |
|
41 |
| -# Insert a row in the old master. This causes the master and standby |
42 |
| -# to have "diverged", it's no longer possible to just apply the |
43 |
| -# standy's logs over master directory - you need to rewind. |
44 |
| -master_psql("INSERT INTO tbl1 VALUES ('in master, after promotion')"); |
| 47 | + # Also insert a new row in the standby, which won't be present in the |
| 48 | + # old master. |
| 49 | + standby_psql("INSERT INTO tbl1 VALUES ('in standby, after promotion')"); |
45 | 50 |
|
46 |
| -# Also insert a new row in the standby, which won't be present in the |
47 |
| -# old master. |
48 |
| -standby_psql("INSERT INTO tbl1 VALUES ('in standby, after promotion')"); |
| 51 | + # Insert enough rows to trunc_tbl to extend the file. pg_rewind should |
| 52 | + # truncate it back to the old size. |
| 53 | + master_psql("INSERT INTO trunc_tbl SELECT 'in master, after promotion: ' || g FROM generate_series(1, 10000) g"); |
49 | 54 |
|
50 |
| -# Insert enough rows to trunc_tbl to extend the file. pg_rewind should |
51 |
| -# truncate it back to the old size. |
52 |
| -master_psql("INSERT INTO trunc_tbl SELECT 'in master, after promotion: ' || g FROM generate_series(1, 10000) g"); |
| 55 | + # Truncate tail_tbl. pg_rewind should copy back the truncated part |
| 56 | + # (We cannot use an actual TRUNCATE command here, as that creates a |
| 57 | + # whole new relfilenode) |
| 58 | + master_psql("DELETE FROM tail_tbl WHERE id > 10"); |
| 59 | + master_psql("VACUUM tail_tbl"); |
53 | 60 |
|
54 |
| -# Truncate tail_tbl. pg_rewind should copy back the truncated part |
55 |
| -# (We cannot use an actual TRUNCATE command here, as that creates a |
56 |
| -# whole new relfilenode) |
57 |
| -master_psql("DELETE FROM tail_tbl WHERE id > 10"); |
58 |
| -master_psql("VACUUM tail_tbl"); |
| 61 | + RewindTest::run_pg_rewind($test_mode); |
59 | 62 |
|
60 |
| -RewindTest::run_pg_rewind(); |
61 |
| - |
62 |
| -check_query('SELECT * FROM tbl1', |
63 |
| - qq(in master |
| 63 | + check_query('SELECT * FROM tbl1', |
| 64 | + qq(in master |
64 | 65 | in master, before promotion
|
65 | 66 | in standby, after promotion
|
66 | 67 | ),
|
67 |
| - 'table content'); |
| 68 | + 'table content'); |
68 | 69 |
|
69 |
| -check_query('SELECT * FROM trunc_tbl', |
70 |
| - qq(in master |
| 70 | + check_query('SELECT * FROM trunc_tbl', |
| 71 | + qq(in master |
71 | 72 | in master, before promotion
|
72 | 73 | ),
|
73 |
| - 'truncation'); |
| 74 | + 'truncation'); |
74 | 75 |
|
75 |
| -check_query('SELECT count(*) FROM tail_tbl', |
76 |
| - qq(10001 |
| 76 | + check_query('SELECT count(*) FROM tail_tbl', |
| 77 | + qq(10001 |
77 | 78 | ),
|
78 |
| - 'tail-copy'); |
| 79 | + 'tail-copy'); |
| 80 | + |
| 81 | +} |
| 82 | + |
| 83 | +# Run the test in both modes |
| 84 | +run_test('local'); |
| 85 | +run_test('remote'); |
79 | 86 |
|
80 | 87 | exit(0);
|
0 commit comments