pg_upgrade check for invalid databases

Thomas Krennwallner <tk@postsubmeta.net>

From: Thomas Krennwallner <tk@postsubmeta.net>
To: pgsql-hackers@postgresql.org
Date: 2024-09-30T00:45:50Z
Lists: pgsql-hackers

Attachments

Hi,

if a cluster contains invalid databases that we cannot connect to 
anymore, pg_upgrade would currently fail when trying to connect to the 
first encountered invalid database with


Performing Consistency Checks
-----------------------------
Checking cluster versions                                     ok

connection failure: connection to server on socket "/tmp/.s.PGSQL.50432" 
failed: FATAL:  cannot connect to invalid database "foo"
HINT:  Use DROP DATABASE to drop invalid databases.

Failure, exiting


If there is more than one invalid database, we need to run pg_upgrade 
more than once (unless the user inspects pg_database).

I attached two small patches for PG 17 and PG 18 (can be easily 
backported to all previous versions upon request).  Instead of just 
failing to connect with an error, we collect all invalid databases in a 
report file invalid_databases.txt:


Performing Consistency Checks
-----------------------------
Checking cluster versions                                     ok
Checking for invalid databases                                fatal

Your installation contains invalid databases as a consequence of
interrupted DROP DATABASE.  They are now marked as corrupted databases
that cannot be connected to anymore.  Consider removing them using
     DROP DATABASE ...;
A list of invalid databases is in the file:

/usr/local/pgsql/data/18/pg_upgrade_output.d/20240929T200559.707/invalid_databases.txt
Failure, exiting


Any thoughts on the proposed patches?

Commits

  1. Find invalid databases during upgrade check stage