Debian Codegraph @

contrib/bloom/t/001_wal.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # Test generic xlog record work for bloom index replication.
5 use strict;
6 use warnings FATAL => 'all';
7 use PostgreSQL::Test::Cluster;
8 use PostgreSQL::Test::Utils;
9 use Test::More;
10
11 my $node_primary;
12 my $node_standby;
13
14 # Run few queries on both primary and standby and check their results match.
15 sub test_index_replay
16 {
17 my ($test_name) = @_;
18
19 local $Test::Builder::Level = $Test::Builder::Level + 1;
20
21 # Wait for standby to catch up
22 $node_primary->wait_for_catchup($node_standby);
23
24 my $queries = qq(SET enable_seqscan=off;
25 SET enable_bitmapscan=on;
26 SET enable_indexscan=on;
27 SELECT * FROM tst WHERE i = 0;
28 SELECT * FROM tst WHERE i = 3;
29 SELECT * FROM tst WHERE t = 'b';
30 SELECT * FROM tst WHERE t = 'f';
31 SELECT * FROM tst WHERE i = 3 AND t = 'c';
32 SELECT * FROM tst WHERE i = 7 AND t = 'e';
33 );
34
35 # Run test queries and compare their result
36 my $primary_result = $node_primary->safe_psql("postgres", $queries);
37 my $standby_result = $node_standby->safe_psql("postgres", $queries);
38
39 is($primary_result, $standby_result, "$test_name: query result matches");
40 return;
41 }
42
43 # Initialize primary node
44 $node_primary = PostgreSQL::Test::Cluster->new('primary');
45 $node_primary->init(allows_streaming => 1);
46 $node_primary->start;
47 my $backup_name = 'my_backup';
48
49 # Take backup
50 $node_primary->backup($backup_name);
51
52 # Create streaming standby linking to primary
53 $node_standby = PostgreSQL::Test::Cluster->new('standby');
54 $node_standby->init_from_backup($node_primary, $backup_name,
55 has_streaming => 1);
56 $node_standby->start;
57
58 # Create some bloom index on primary
59 $node_primary->safe_psql("postgres", "CREATE EXTENSION bloom;");
60 $node_primary->safe_psql("postgres", "CREATE TABLE tst (i int4, t text);");
61 $node_primary->safe_psql("postgres",
62 "INSERT INTO tst SELECT i%10, substr(encode(sha256(i::text::bytea), 'hex'), 1, 1) FROM generate_series(1,10000) i;"
63 );
64 $node_primary->safe_psql("postgres",
65 "CREATE INDEX bloomidx ON tst USING bloom (i, t) WITH (col1 = 3);");
66
67 # Test that queries give same result
68 test_index_replay('initial');
69
70 # Run 10 cycles of table modification. Run test queries after each modification.
71 for my $i (1 .. 10)
72 {
73 $node_primary->safe_psql("postgres", "DELETE FROM tst WHERE i = $i;");
74 test_index_replay("delete $i");
75 $node_primary->safe_psql("postgres", "VACUUM tst;");
76 test_index_replay("vacuum $i");
77 my ($start, $end) = (100001 + ($i - 1) * 10000, 100000 + $i * 10000);
78 $node_primary->safe_psql("postgres",
79 "INSERT INTO tst SELECT i%10, substr(encode(sha256(i::text::bytea), 'hex'), 1, 1) FROM generate_series($start,$end) i;"
80 );
81 test_index_replay("insert $i");
82 }
83
84 done_testing();