|
| 1 | +# Copyright (c) 2023, PostgreSQL Global Development Group |
| 2 | + |
| 3 | +# Test wraparound emergency autovacuum. |
| 4 | +use strict; |
| 5 | +use warnings; |
| 6 | +use PostgreSQL::Test::Cluster; |
| 7 | +use PostgreSQL::Test::Utils; |
| 8 | +use Test::More; |
| 9 | + |
| 10 | +if ($ENV{PG_TEST_EXTRA} !~ /\bxid_wraparound\b/) |
| 11 | +{ |
| 12 | + plan skip_all => "test xid_wraparound not enabled in PG_TEST_EXTRA"; |
| 13 | +} |
| 14 | + |
| 15 | +# Initialize node |
| 16 | +my $node = PostgreSQL::Test::Cluster->new('main'); |
| 17 | + |
| 18 | +$node->init; |
| 19 | +$node->append_conf( |
| 20 | + 'postgresql.conf', qq[ |
| 21 | +autovacuum = off # run autovacuum only when to anti wraparound |
| 22 | +autovacuum_naptime = 1s |
| 23 | +# so it's easier to verify the order of operations |
| 24 | +autovacuum_max_workers = 1 |
| 25 | +log_autovacuum_min_duration = 0 |
| 26 | +]); |
| 27 | +$node->start; |
| 28 | +$node->safe_psql('postgres', 'CREATE EXTENSION xid_wraparound'); |
| 29 | + |
| 30 | +# Create tables for a few different test scenarios |
| 31 | +$node->safe_psql( |
| 32 | + 'postgres', qq[ |
| 33 | +CREATE TABLE large(id serial primary key, data text, filler text default repeat(random()::text, 10)); |
| 34 | +INSERT INTO large(data) SELECT generate_series(1,30000); |
| 35 | +
|
| 36 | +CREATE TABLE large_trunc(id serial primary key, data text, filler text default repeat(random()::text, 10)); |
| 37 | +INSERT INTO large_trunc(data) SELECT generate_series(1,30000); |
| 38 | +
|
| 39 | +CREATE TABLE small(id serial primary key, data text, filler text default repeat(random()::text, 10)); |
| 40 | +INSERT INTO small(data) SELECT generate_series(1,15000); |
| 41 | +
|
| 42 | +CREATE TABLE small_trunc(id serial primary key, data text, filler text default repeat(random()::text, 10)); |
| 43 | +INSERT INTO small_trunc(data) SELECT generate_series(1,15000); |
| 44 | +
|
| 45 | +CREATE TABLE autovacuum_disabled(id serial primary key, data text) WITH (autovacuum_enabled=false); |
| 46 | +INSERT INTO autovacuum_disabled(data) SELECT generate_series(1,1000); |
| 47 | +]); |
| 48 | + |
| 49 | +# Bump the query timeout to avoid false negatives on slow test systems. |
| 50 | +my $psql_timeout_secs = 4 * $PostgreSQL::Test::Utils::timeout_default; |
| 51 | + |
| 52 | +# Start a background session, which holds a transaction open, preventing |
| 53 | +# autovacuum from advancing relfrozenxid and datfrozenxid. |
| 54 | +my $background_psql = $node->background_psql( |
| 55 | + 'postgres', |
| 56 | + on_error_stop => 0, |
| 57 | + timeout => $psql_timeout_secs); |
| 58 | +$background_psql->set_query_timer_restart(); |
| 59 | +$background_psql->query_safe( |
| 60 | + qq[ |
| 61 | + BEGIN; |
| 62 | + DELETE FROM large WHERE id % 2 = 0; |
| 63 | + DELETE FROM large_trunc WHERE id > 10000; |
| 64 | + DELETE FROM small WHERE id % 2 = 0; |
| 65 | + DELETE FROM small_trunc WHERE id > 1000; |
| 66 | + DELETE FROM autovacuum_disabled WHERE id % 2 = 0; |
| 67 | +]); |
| 68 | + |
| 69 | +# Consume 2 billion XIDs, to get us very close to wraparound |
| 70 | +$node->safe_psql('postgres', |
| 71 | + qq[SELECT consume_xids_until('2000000000'::xid8)]); |
| 72 | + |
| 73 | +# Make sure the latest completed XID is advanced |
| 74 | +$node->safe_psql('postgres', qq[INSERT INTO small(data) SELECT 1]); |
| 75 | + |
| 76 | +# Check that all databases became old enough to trigger failsafe. |
| 77 | +my $ret = $node->safe_psql( |
| 78 | + 'postgres', |
| 79 | + qq[ |
| 80 | +SELECT datname, |
| 81 | + age(datfrozenxid) > current_setting('vacuum_failsafe_age')::int as old |
| 82 | +FROM pg_database ORDER BY 1 |
| 83 | +]); |
| 84 | +is( $ret, "postgres|t |
| 85 | +template0|t |
| 86 | +template1|t", "all tables became old"); |
| 87 | + |
| 88 | +my $log_offset = -s $node->logfile; |
| 89 | + |
| 90 | +# Finish the old transaction, to allow vacuum freezing to advance |
| 91 | +# relfrozenxid and datfrozenxid again. |
| 92 | +$background_psql->query_safe(qq[COMMIT]); |
| 93 | +$background_psql->quit; |
| 94 | + |
| 95 | +# Wait until autovacuum processed all tables and advanced the |
| 96 | +# system-wide oldest-XID. |
| 97 | +$node->poll_query_until( |
| 98 | + 'postgres', qq[ |
| 99 | +SELECT NOT EXISTS ( |
| 100 | + SELECT * |
| 101 | + FROM pg_database |
| 102 | + WHERE age(datfrozenxid) > current_setting('autovacuum_freeze_max_age')::int) |
| 103 | +]) or die "timeout waiting for all databases to be vacuumed"; |
| 104 | + |
| 105 | +# Check if these tables are vacuumed. |
| 106 | +$ret = $node->safe_psql( |
| 107 | + 'postgres', qq[ |
| 108 | +SELECT relname, age(relfrozenxid) > current_setting('autovacuum_freeze_max_age')::int |
| 109 | +FROM pg_class |
| 110 | +WHERE relname IN ('large', 'large_trunc', 'small', 'small_trunc', 'autovacuum_disabled') |
| 111 | +ORDER BY 1 |
| 112 | +]); |
| 113 | + |
| 114 | +is( $ret, "autovacuum_disabled|f |
| 115 | +large|f |
| 116 | +large_trunc|f |
| 117 | +small|f |
| 118 | +small_trunc|f", "all tables are vacuumed"); |
| 119 | + |
| 120 | +# Check if vacuum failsafe was triggered for each table. |
| 121 | +my $log_contents = slurp_file($node->logfile, $log_offset); |
| 122 | +foreach my $tablename ('large', 'large_trunc', 'small', 'small_trunc', |
| 123 | + 'autovacuum_disabled') |
| 124 | +{ |
| 125 | + like( |
| 126 | + $log_contents, |
| 127 | + qr/bypassing nonessential maintenance of table "postgres.public.$tablename" as a failsafe after \d+ index scans/, |
| 128 | + "failsafe vacuum triggered for $tablename"); |
| 129 | +} |
| 130 | + |
| 131 | +$node->stop; |
| 132 | +done_testing(); |
0 commit comments