|
| 1 | +use warnings FATAL => 'all'; |
| 2 | +use PostgreSQL::Test::Cluster; |
| 3 | +use PostgreSQL::Test::Utils; |
| 4 | +use Test::More; |
| 5 | + |
| 6 | +my $psql_out; |
| 7 | + |
| 8 | +my $node = PostgreSQL::Test::Cluster->new('node1'); |
| 9 | +$node->init; |
| 10 | + |
| 11 | +# Configure postgres, so it can launch parallel autovacuum workers, log all |
| 12 | +# information we are interested in and autovacuum works frequently |
| 13 | +$node->append_conf('postgresql.conf', qq{ |
| 14 | + max_worker_processes = 20 |
| 15 | + max_parallel_workers = 20 |
| 16 | + max_parallel_maintenance_workers = 20 |
| 17 | + autovacuum_max_parallel_workers = 10 |
| 18 | + log_min_messages = debug2 |
| 19 | + log_autovacuum_min_duration = 0 |
| 20 | + autovacuum_naptime = '1s' |
| 21 | + min_parallel_index_scan_size = 0 |
| 22 | + shared_preload_libraries=test_autovacuum |
| 23 | +}); |
| 24 | +$node->start; |
| 25 | + |
| 26 | +my $indexes_num = 4; |
| 27 | +my $initial_rows_num = 10_000; |
| 28 | +my $autovacuum_parallel_workers = 2; |
| 29 | + |
| 30 | +# Create table with specified number of b-tree indexes on it |
| 31 | +$node->safe_psql('postgres', qq{ |
| 32 | + CREATE TABLE test_autovac ( |
| 33 | + id SERIAL PRIMARY KEY, |
| 34 | + col_1 INTEGER, col_2 INTEGER, col_3 INTEGER, col_4 INTEGER |
| 35 | + ) WITH (autovacuum_parallel_workers = $autovacuum_parallel_workers, |
| 36 | + autovacuum_enabled = false); |
| 37 | +
|
| 38 | + DO \$\$ |
| 39 | + DECLARE |
| 40 | + i INTEGER; |
| 41 | + BEGIN |
| 42 | + FOR i IN 1..$indexes_num LOOP |
| 43 | + EXECUTE format('CREATE INDEX idx_col_\%s ON test_autovac (col_\%s);', i, i); |
| 44 | + END LOOP; |
| 45 | + END \$\$; |
| 46 | +}); |
| 47 | + |
| 48 | +# Insert specified tuples num into the table |
| 49 | +$node->safe_psql('postgres', qq{ |
| 50 | + DO \$\$ |
| 51 | + DECLARE |
| 52 | + i INTEGER; |
| 53 | + BEGIN |
| 54 | + FOR i IN 1..$initial_rows_num LOOP |
| 55 | + INSERT INTO test_autovac VALUES (i, i + 1, i + 2, i + 3); |
| 56 | + END LOOP; |
| 57 | + END \$\$; |
| 58 | +}); |
| 59 | + |
| 60 | +# Now, create some dead tuples and refresh table statistics |
| 61 | +$node->safe_psql('postgres', qq{ |
| 62 | + UPDATE test_autovac SET col_1 = 0 WHERE (col_1 % 3) = 0; |
| 63 | + ANALYZE test_autovac; |
| 64 | +}); |
| 65 | + |
| 66 | +# Create all functions needed for testing |
| 67 | +$node->safe_psql('postgres', qq{ |
| 68 | + CREATE EXTENSION test_autovacuum; |
| 69 | + SELECT inj_set_free_workers_attach(); |
| 70 | + SELECT inj_leader_failure_attach(); |
| 71 | +}); |
| 72 | + |
| 73 | +# Test 1 : |
| 74 | +# Our table has enough indexes and appropriate reloptions, so autovacuum must |
| 75 | +# be able to process it in parallel mode. Just check if it can. |
| 76 | +# Also check whether all requested workers: |
| 77 | +# 1) launched |
| 78 | +# 2) correctly released |
| 79 | + |
| 80 | +$node->safe_psql('postgres', qq{ |
| 81 | + ALTER TABLE test_autovac SET (autovacuum_enabled = true); |
| 82 | +}); |
| 83 | + |
| 84 | +# Wait until the parallel autovacuum on table is completed. At the same time, |
| 85 | +# we check that the required number of parallel workers has been started. |
| 86 | +$node->wait_for_log(qr/workers usage statistics for all of index scans : / . |
| 87 | + qr/launched in total = 2, planned in total = 2/); |
| 88 | + |
| 89 | +$node->psql('postgres', |
| 90 | + "SELECT get_parallel_autovacuum_free_workers();", |
| 91 | + stdout => \$psql_out, |
| 92 | +); |
| 93 | +is($psql_out, 10, 'All parallel workers has been released by the leader'); |
| 94 | + |
| 95 | +# Disable autovacuum on table during preparation for the next test |
| 96 | +$node->append_conf('postgresql.conf', qq{ |
| 97 | + ALTER TABLE test_autovac SET (autovacuum_enabled = false); |
| 98 | +}); |
| 99 | + |
| 100 | +# Create more dead tuples |
| 101 | +$node->safe_psql('postgres', qq{ |
| 102 | + UPDATE test_autovac SET col_2 = 0 WHERE (col_2 % 3) = 0; |
| 103 | + ANALYZE test_autovac; |
| 104 | +}); |
| 105 | + |
| 106 | +# Test 2: |
| 107 | +# We want parallel autovacuum workers to be released even if leader gets an |
| 108 | +# error. At first, simulate situation, when leader exites due to an ERROR. |
| 109 | + |
| 110 | +$node->safe_psql('postgres', qq( |
| 111 | + SELECT trigger_leader_failure('ERROR'); |
| 112 | +)); |
| 113 | + |
| 114 | +$node->safe_psql('postgres', qq{ |
| 115 | + ALTER TABLE test_autovac SET (autovacuum_enabled = true); |
| 116 | +}); |
| 117 | + |
| 118 | +$node->wait_for_log(qr/error, triggered by injection point/); |
| 119 | + |
| 120 | +$node->psql('postgres', |
| 121 | + "SELECT get_parallel_autovacuum_free_workers();", |
| 122 | + stdout => \$psql_out, |
| 123 | +); |
| 124 | +is($psql_out, 10, |
| 125 | + 'All parallel workers has been released by the leader after ERROR'); |
| 126 | + |
| 127 | +# Disable autovacuum on table during preparation for the next test |
| 128 | +$node->append_conf('postgresql.conf', qq{ |
| 129 | + ALTER TABLE test_autovac SET (autovacuum_enabled = false); |
| 130 | +}); |
| 131 | + |
| 132 | +# Create more dead tuples |
| 133 | +$node->safe_psql('postgres', qq{ |
| 134 | + UPDATE test_autovac SET col_3 = 0 WHERE (col_3 % 3) = 0; |
| 135 | + ANALYZE test_autovac; |
| 136 | +}); |
| 137 | + |
| 138 | +# Test 3: |
| 139 | +# Same as Test 2, but simulate situation, when leader exites due to FATAL. |
| 140 | + |
| 141 | +$node->safe_psql('postgres', qq( |
| 142 | + SELECT trigger_leader_failure('FATAL'); |
| 143 | +)); |
| 144 | + |
| 145 | +$node->safe_psql('postgres', qq{ |
| 146 | + ALTER TABLE test_autovac SET (autovacuum_enabled = true); |
| 147 | +}); |
| 148 | + |
| 149 | +$node->wait_for_log(qr/fatal, triggered by injection point/); |
| 150 | + |
| 151 | +$node->psql('postgres', |
| 152 | + "SELECT get_parallel_autovacuum_free_workers();", |
| 153 | + stdout => \$psql_out, |
| 154 | +); |
| 155 | +is($psql_out, 10, |
| 156 | + 'All parallel workers has been released by the leader after FATAL'); |
| 157 | + |
| 158 | +# Cleanup |
| 159 | +$node->safe_psql('postgres', qq{ |
| 160 | + SELECT inj_set_free_workers_detach(); |
| 161 | + SELECT inj_leader_failure_detach(); |
| 162 | +}); |
| 163 | + |
| 164 | +$node->stop; |
| 165 | +done_testing(); |
0 commit comments