]> granicus.if.org Git - postgresql/blob - contrib/bloom/t/001_wal.pl
dbb6a905b6097d9b3c7f92bcdc978cd0c3588d2b
[postgresql] / contrib / bloom / t / 001_wal.pl
1 # Test generic xlog record work for bloom index replication.
2 use strict;
3 use warnings;
4 use PostgresNode;
5 use TestLib;
6 use Test::More tests => 31;
7
8 my $node_master;
9 my $node_standby;
10
11 # Run few queries on both master and standby and check their results match.
12 sub test_index_replay
13 {
14         my ($test_name) = @_;
15
16         # Wait for standby to catch up
17         my $applname = $node_standby->name;
18         my $caughtup_query =
19                 "SELECT pg_current_xlog_location() <= write_location FROM pg_stat_replication WHERE application_name = '$applname';";
20         $node_master->poll_query_until('postgres', $caughtup_query)
21           or die "Timed out while waiting for standby 1 to catch up";
22
23         my $queries = qq(SET enable_seqscan=off;
24 SET enable_bitmapscan=on;
25 SET enable_indexscan=on;
26 SELECT * FROM tst WHERE i = 0;
27 SELECT * FROM tst WHERE i = 3;
28 SELECT * FROM tst WHERE t = 'b';
29 SELECT * FROM tst WHERE t = 'f';
30 SELECT * FROM tst WHERE i = 3 AND t = 'c';
31 SELECT * FROM tst WHERE i = 7 AND t = 'e';
32 );
33
34         # Run test queries and compare their result
35         my $master_result = $node_master->psql("postgres", $queries);
36         my $standby_result = $node_standby->psql("postgres", $queries);
37
38         is($master_result, $standby_result, "$test_name: query result matches");
39 }
40
41 # Initialize master node
42 $node_master = get_new_node('master');
43 $node_master->init(allows_streaming => 1);
44 $node_master->start;
45 my $backup_name = 'my_backup';
46
47 # Take backup
48 $node_master->backup($backup_name);
49
50 # Create streaming standby linking to master
51 $node_standby = get_new_node('standby');
52 $node_standby->init_from_backup($node_master, $backup_name,
53         has_streaming => 1);
54 $node_standby->start;
55
56 # Create some bloom index on master
57 $node_master->psql("postgres", "CREATE EXTENSION bloom;");
58 $node_master->psql("postgres", "CREATE TABLE tst (i int4, t text);");
59 $node_master->psql("postgres", "INSERT INTO tst SELECT i%10, substr(md5(i::text), 1, 1) FROM generate_series(1,100000) i;");
60 $node_master->psql("postgres", "CREATE INDEX bloomidx ON tst USING bloom (i, t) WITH (col1 = 3);");
61
62 # Test that queries give same result
63 test_index_replay('initial');
64
65 # Run 10 cycles of table modification. Run test queries after each modification.
66 for my $i (1..10)
67 {
68         $node_master->psql("postgres", "DELETE FROM tst WHERE i = $i;");
69         test_index_replay("delete $i");
70         $node_master->psql("postgres", "VACUUM tst;");
71         test_index_replay("vacuum $i");
72         my ($start, $end) = (100001 + ($i - 1) * 10000, 100000 + $i * 10000);
73         $node_master->psql("postgres", "INSERT INTO tst SELECT i%10, substr(md5(i::text), 1, 1) FROM generate_series($start,$end) i;");
74         test_index_replay("insert $i");
75 }