Skip to content

Commit b2355a2

Browse files
committed
Ensure that backends see up-to-date statistics for shared catalogs.
Ever since we split the statistics collector's reports into per-database files (commit 187492b), backends have been seeing stale statistics for shared catalogs. This is because the inquiry message only prompts the collector to write the per-database file for the requesting backend's own database. Stats for shared catalogs are in a separate file for "DB 0", which didn't get updated. In normal operation this was partially masked by the fact that the autovacuum launcher would send an inquiry message at least once per autovacuum_naptime that asked for "DB 0"; so the shared-catalog stats would never be more than a minute out of date. However the problem becomes very obvious with autovacuum disabled, as reported by Peter Eisentraut. To fix, redefine the semantics of inquiry messages so that both the specified DB and DB 0 will be dumped. (This might seem a bit inefficient, but we have no good way to know whether a backend's transaction will look at shared-catalog stats, so we have to read both groups of stats whenever we request stats. Sending two inquiry messages would definitely not be better.) Back-patch to 9.3 where the bug was introduced. Report: <56AD41AC.1030509@gmx.net>
1 parent af6555b commit b2355a2

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/backend/postmaster/pgstat.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5351,7 +5351,16 @@ pgstat_db_requested(Oid databaseid)
53515351
{
53525352
slist_iter iter;
53535353

5354-
/* Check the databases if they need to refresh the stats. */
5354+
/*
5355+
* If any requests are outstanding at all, we should write the stats for
5356+
* shared catalogs (the "database" with OID 0). This ensures that
5357+
* backends will see up-to-date stats for shared catalogs, even though
5358+
* they send inquiry messages mentioning only their own DB.
5359+
*/
5360+
if (databaseid == InvalidOid && !slist_is_empty(&last_statrequests))
5361+
return true;
5362+
5363+
/* Search to see if there's an open request to write this database. */
53555364
slist_foreach(iter, &last_statrequests)
53565365
{
53575366
DBWriteRequest *req = slist_container(DBWriteRequest, next, iter.cur);

0 commit comments

Comments
 (0)