pgsql: Fix handling of shared statistics with dropped databases - Mailing list pgsql-committers

From Michael Paquier
Subject pgsql: Fix handling of shared statistics with dropped databases
Date
Msg-id E1qcw9a-002J32-IK@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Fix handling of shared statistics with dropped databases

Dropping a database while a connection is attempted on it was able to
lead to the presence of valid database entries in shared statistics.
The issue is that MyDatabaseId was getting set too early than it should,
as, if the connection attempted on the dropped database fails when
renamed or dropped, the shutdown callback of the shared statistics would
finish by re-inserting a correct entry related to the database already
dropped.

As analyzed by the bug reporters, this issue could lead to phantom
entries in the database list maintained by the autovacuum launcher
(in rebuild_database_list()) if the database dropped was part of the
database list when it was still valid.  After the database was dropped,
it would remain the highest on the list of databases to considered by
the autovacuum worker as things to process.  This would prevent
autovacuum jobs to happen on all the other databases still present.

The commit fixes this issue by delaying setting MyDatabaseId until the
database existence has been re-checked with the second scan on
pg_database after getting a shared lock on it, and by switching
pgstat_update_dbstats() so as nothing happens if MyDatabaseId is not
valid.

Issue introduced by 5891c7a8ed8f, so backpatch down to 15.

Reported-by: Will Mortensen, Jacob Speidel
Analyzed-by: Will Mortensen, Jacob Speidel
Author: Andres Freund
Discussion: https://postgr.es/m/17973-bca1f7d5c14f601e@postgresql.org
Backpatch-through: 15

Branch
------
REL_15_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/5a9325fdde1d8ab8031fa09ebd0f63dc3f967a0a

Modified Files
--------------
src/backend/utils/activity/pgstat_database.c |  13 +++
src/backend/utils/init/postinit.c            | 118 ++++++++++++++-------------
2 files changed, 74 insertions(+), 57 deletions(-)


pgsql-committers by date:

Previous
From: Alvaro Herrera
Date:
Subject: pgsql: Fix not-null constraint test
Next
From: Amit Kapila
Date:
Subject: pgsql: Fix typo in decode.c.