Stabilize xid_wraparound tests
authorAndrew Dunstan <[email protected]>
Tue, 30 Jul 2024 10:17:48 +0000 (06:17 -0400)
committerAndrew Dunstan <[email protected]>
Tue, 30 Jul 2024 10:26:36 +0000 (06:26 -0400)
The tests had a race condition if autovacuum was set to off. Instead we
create all the tables we are interested in with autovacuum disabled, so
they are only ever touched when in danger of wraparound.

Discussion: https://postgr.es/m/3e2cbd24-f45e-4b2b-ba83-8149214f0a4d@dunslane.net

Masahiko Sawada (slightly tweaked by me)

Backpatch to release 17 where these tests were introduced.

src/test/modules/xid_wraparound/t/001_emergency_vacuum.pl
src/test/modules/xid_wraparound/t/002_limits.pl
src/test/modules/xid_wraparound/t/003_wraparounds.pl

index 37550b67a4d67806c5e5ffd1d6de5fc362336a7e..2692b35f346f01d116e77235bd02c723ce052e78 100644 (file)
@@ -18,7 +18,6 @@ my $node = PostgreSQL::Test::Cluster->new('main');
 $node->init;
 $node->append_conf(
    'postgresql.conf', qq[
-autovacuum = off # run autovacuum only when to anti wraparound
 autovacuum_naptime = 1s
 # so it's easier to verify the order of operations
 autovacuum_max_workers = 1
@@ -27,23 +26,25 @@ log_autovacuum_min_duration = 0
 $node->start;
 $node->safe_psql('postgres', 'CREATE EXTENSION xid_wraparound');
 
-# Create tables for a few different test scenarios
+# Create tables for a few different test scenarios. We disable autovacuum
+# on these tables to run it only to prevent wraparound.
 $node->safe_psql(
    'postgres', qq[
-CREATE TABLE large(id serial primary key, data text, filler text default repeat(random()::text, 10));
+CREATE TABLE large(id serial primary key, data text, filler text default repeat(random()::text, 10))
+   WITH (autovacuum_enabled = off);
 INSERT INTO large(data) SELECT generate_series(1,30000);
 
-CREATE TABLE large_trunc(id serial primary key, data text, filler text default repeat(random()::text, 10));
+CREATE TABLE large_trunc(id serial primary key, data text, filler text default repeat(random()::text, 10))
+   WITH (autovacuum_enabled = off);
 INSERT INTO large_trunc(data) SELECT generate_series(1,30000);
 
-CREATE TABLE small(id serial primary key, data text, filler text default repeat(random()::text, 10));
+CREATE TABLE small(id serial primary key, data text, filler text default repeat(random()::text, 10))
+   WITH (autovacuum_enabled = off);
 INSERT INTO small(data) SELECT generate_series(1,15000);
 
-CREATE TABLE small_trunc(id serial primary key, data text, filler text default repeat(random()::text, 10));
+CREATE TABLE small_trunc(id serial primary key, data text, filler text default repeat(random()::text, 10))
+   WITH (autovacuum_enabled = off);
 INSERT INTO small_trunc(data) SELECT generate_series(1,15000);
-
-CREATE TABLE autovacuum_disabled(id serial primary key, data text) WITH (autovacuum_enabled=false);
-INSERT INTO autovacuum_disabled(data) SELECT generate_series(1,1000);
 ]);
 
 # Bump the query timeout to avoid false negatives on slow test systems.
@@ -63,7 +64,6 @@ $background_psql->query_safe(
    DELETE FROM large_trunc WHERE id > 10000;
    DELETE FROM small WHERE id % 2 = 0;
    DELETE FROM small_trunc WHERE id > 1000;
-   DELETE FROM autovacuum_disabled WHERE id % 2 = 0;
 ]);
 
 # Consume 2 billion XIDs, to get us very close to wraparound
@@ -107,20 +107,18 @@ $ret = $node->safe_psql(
    'postgres', qq[
 SELECT relname, age(relfrozenxid) > current_setting('autovacuum_freeze_max_age')::int
 FROM pg_class
-WHERE relname IN ('large', 'large_trunc', 'small', 'small_trunc', 'autovacuum_disabled')
+WHERE relname IN ('large', 'large_trunc', 'small', 'small_trunc')
 ORDER BY 1
 ]);
 
-is( $ret, "autovacuum_disabled|f
-large|f
+is( $ret, "large|f
 large_trunc|f
 small|f
 small_trunc|f", "all tables are vacuumed");
 
 # Check if vacuum failsafe was triggered for each table.
 my $log_contents = slurp_file($node->logfile, $log_offset);
-foreach my $tablename ('large', 'large_trunc', 'small', 'small_trunc',
-   'autovacuum_disabled')
+foreach my $tablename ('large', 'large_trunc', 'small', 'small_trunc')
 {
    like(
        $log_contents,
index c02c287167856b0cda8829c50c1b85630bdd1c6b..aca3fa15149bbc6a5f28bf7812e8756b4222c4b2 100644 (file)
@@ -27,17 +27,17 @@ my $node = PostgreSQL::Test::Cluster->new('wraparound');
 $node->init;
 $node->append_conf(
    'postgresql.conf', qq[
-autovacuum = off # run autovacuum only to prevent wraparound
 autovacuum_naptime = 1s
 log_autovacuum_min_duration = 0
 ]);
 $node->start;
 $node->safe_psql('postgres', 'CREATE EXTENSION xid_wraparound');
 
-# Create a test table
+# Create a test table. We disable autovacuum on the table to run it only
+# to prevent wraparound.
 $node->safe_psql(
    'postgres', qq[
-CREATE TABLE wraparoundtest(t text);
+CREATE TABLE wraparoundtest(t text) WITH (autovacuum_enabled = off);
 INSERT INTO wraparoundtest VALUES ('start');
 ]);
 
index 88063b4b52d61e42e8c477adbed1b82915250900..3eaa46a94d0778bffc73252f6527e0e302f26c92 100644 (file)
@@ -21,7 +21,6 @@ my $node = PostgreSQL::Test::Cluster->new('wraparound');
 $node->init;
 $node->append_conf(
    'postgresql.conf', qq[
-autovacuum = off # run autovacuum only when to anti wraparound
 autovacuum_naptime = 1s
 # so it's easier to verify the order of operations
 autovacuum_max_workers = 1
@@ -30,10 +29,11 @@ log_autovacuum_min_duration = 0
 $node->start;
 $node->safe_psql('postgres', 'CREATE EXTENSION xid_wraparound');
 
-# Create a test table
+# Create a test table. We disable autovacuum on the table to run
+# it only to prevent wraparound.
 $node->safe_psql(
    'postgres', qq[
-CREATE TABLE wraparoundtest(t text);
+CREATE TABLE wraparoundtest(t text) WITH (autovacuum_enabled = off);
 INSERT INTO wraparoundtest VALUES ('beginning');
 ]);