From: Nasby, Jim <nasbyj@amazon.com>
To: pgsql-hackers@postgresql.org <pgsql-hackers@postgresql.org>
Subject: autovac issue with large number of tables
Date: Sun, 26 Jul 2020 21:43:29 +0000
Message-ID: <3FC6C2F2-8A47-44C0-B997-28830B5716D0@amazon.com> (raw)
A database with a very large number of tables eligible for autovacuum can result in autovacuum workers “stuck” in a tight loop of table_recheck_autovac() constantly reporting nothing to do on the table. This is because a database with a very large number of tables means it takes a while to search the statistics hash to verify that the table still needs to be processed[1]. If a worker spends some time processing a table, when it’s done it can spend a significant amount of time rechecking each table that it identified at launch (I’ve seen a worker in this state for over an hour). A simple work-around in this scenario is to kill the worker; the launcher will quickly fire up a new worker on the same database, and that worker will build a new list of tables.
That’s not a complete solution though… if the database contains a large number of very small tables you can end up in a state where 1 or 2 workers is busy chugging through those small tables so quickly than any additional workers spend all their time in table_recheck_autovac(), because that takes long enough that the additional workers are never able to “leapfrog” the workers that are doing useful work.
PoC patch attached.
1: top hits from `perf top -p xxx` on an affected worker
Samples: 72K of event 'cycles', Event count (approx.): 17131910436
Overhead Shared Object Symbol
42.62% postgres [.] hash_search_with_hash_value
10.34% libc-2.17.so [.] __memcpy_sse2
6.99% [kernel] [k] copy_user_enhanced_fast_string
4.73% libc-2.17.so [.] _IO_fread
3.91% postgres [.] 0x00000000002d6478
2.95% libc-2.17.so [.] _IO_getc
2.44% libc-2.17.so [.] _IO_file_xsgetn
1.73% postgres [.] hash_search
1.65% [kernel] [k] find_get_entry
1.10% postgres [.] hash_uint32
0.99% libc-2.17.so [.] __memcpy_ssse3_back
Attachments:autovac.patch (application/octet-stream, 3.1K) downloaddiff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.cindex a8d4dfdd7c..3c2027e77c 100644--- a/src/backend/postmaster/autovacuum.c+++ b/src/backend/postmaster/autovacuum.c@@ -1947,6 +1947,8 @@ do_autovacuum(void)
bool did_vacuum = false;
bool found_concurrent_worker = false;
int i;
+ int skipped, modulo, to_skip;+ skipped = modulo = to_skip = 0;
/*
* StartTransactionCommand and CommitTransactionCommand will automatically
@@ -2325,6 +2327,66 @@ do_autovacuum(void)
*/
}
+ /*+ * If we've had to skip a large number of tables consecutively, that's+ * an indication that either our initial list is now very out-of-date+ * with reality, or we are competing with other workers to process a+ * lot of really small tables. Both cases are problematic because on a+ * database with enough tables to trigger this the hash search to find+ * updated stats for the table we're looking at (in+ * table_recheck_autovac) will be non-trivially expensive. When this+ * happens, we need to take some defensive measures to avoid hopelessly+ * spinning our wheels.+ */+ if (skipped > 1000)+ {+ if (modulo == 0)+ {+ /*+ * The first time we get here, we just assume it's because we're+ * competing with other workers over the same set of tables, not+ * because our list is very out of date. If it turns out our list+ * is way out of date we'll quickly max out skipped again and exit+ * the loop.+ *+ * Figure out how many other workers are handling this database and+ * start skipping enough records to "stay ahead of" them. This+ * doesn't need to be perfect; the goal is simply to try and get+ * real work done. If it turns out there's no competing workers+ * we'll break out soon enough anyway.+ */+ LWLockAcquire(AutovacuumLock, LW_SHARED);+ dlist_foreach(iter, &AutoVacuumShmem->av_runningWorkers)+ {+ WorkerInfo worker = dlist_container(WorkerInfoData, wi_links, iter.cur);++ /* we intentionally count ourselves to ensure modulo > 0 */++ /* ignore workers in other databases */+ if (worker->wi_dboid != MyDatabaseId)+ continue;++ modulo++;+ }+ LWLockRelease(AutovacuumLock);+ to_skip = modulo;+ }+ /*+ * Handle the case of our list being hopelessly out of date. In+ * this scenario we built a very large initial list, then spent+ * enough time processing a table (while other workers carried on)+ * that our list is hopelessly out of date, so just exit and let+ * the launcher fire up a new worker.+ */+ else+ break;+ }++ if (--to_skip > 0)+ continue;+ else+ to_skip = modulo;+
/*
* Find out whether the table is shared or not. (It's slightly
* annoying to fetch the syscache entry just for this, but in typical
@@ -2408,8 +2470,11 @@ do_autovacuum(void)
MyWorkerInfo->wi_tableoid = InvalidOid;
MyWorkerInfo->wi_sharedrel = false;
LWLockRelease(AutovacuumScheduleLock);
+ skipped++;
continue;
}
+ else+ skipped = 0;
/*
* Remember the prevailing values of the vacuum cost GUCs. We have to
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: nasbyj@amazon.com
Subject: Re: autovac issue with large number of tables
In-Reply-To: <3FC6C2F2-8A47-44C0-B997-28830B5716D0@amazon.com>
References: <3FC6C2F2-8A47-44C0-B997-28830B5716D0@amazon.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox