Author: Noah Misch Commit: Noah Misch diff --git a/contrib/amcheck/t/089_vac.pl b/contrib/amcheck/t/089_vac.pl new file mode 100644 index 0000000..a28ebd9 --- /dev/null +++ b/contrib/amcheck/t/089_vac.pl @@ -0,0 +1,50 @@ + +# Copyright (c) 2021-2024, PostgreSQL Global Development Group + +# Test VACUUM vs. slow autovac +use strict; +use warnings FATAL => 'all'; + +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; + +use Test::More; + +my ($node, $result); + +# +# Test set-up +# +$node = PostgreSQL::Test::Cluster->new('main'); +$node->init; +$node->append_conf('postgresql.conf', 'autovacuum_naptime = 1s'); +$node->append_conf('postgresql.conf', 'log_lock_waits = on'); +$node->append_conf('postgresql.conf', 'deadlock_timeout = 10ms'); +$node->append_conf('postgresql.conf', q{backtrace_functions = 'ProcessInterrupts'}); +$node->start; + +my $grant_h = $node->background_psql('postgres'); + +# This running would block all vac_update_datfrozenxid => lock_timeout always +# $grant_h->query_safe( +# q( +# BEGIN; +# GRANT TEMP ON DATABASE postgres TO pg_monitor; +# )); + +{ + local $ENV{PGOPTIONS} = '-clock_timeout=5s'; +$node->pgbench( + '--no-vacuum --client=1 --time=600', + 0, + [qr{actually processed}], + [qr{^$}], + 'vac', + { + 'vac' => 'VACUUM (ONLY_DATABASE_STATS);', + }); +} + +$grant_h->quit; +$node->stop; +done_testing(); diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index ac8f5d9..20fe426 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -1582,6 +1582,7 @@ vac_update_relstats(Relation relation, * commits. As with vac_update_relstats, this avoids leaving dead tuples * behind after a VACUUM. */ +#include "utils/fmgrprotos.h" void vac_update_datfrozenxid(void) { @@ -1607,6 +1608,9 @@ vac_update_datfrozenxid(void) */ LockDatabaseFrozenIds(ExclusiveLock); + if (AmAutoVacuumWorkerProcess()) + DirectFunctionCall1(pg_sleep, Float8GetDatum(10.0)); + /* * Initialize the "min" calculation with * GetOldestNonRemovableTransactionId(), which is a reasonable