Based on some testing(dump was shared by Andrew, Thanks Andrew), I fixed some more bugs in the attached patch.
There are some open points for this patch. I will put those points in follow-up patches also.
Point 1: With pg_dumpall, we have option --exclude-database=PATTERN, and there we are validating this PATTERN by server because
we have connection but in pg_restore, we don't have some db connection in some case so how to handle these patterns? or should we use
only NAMES for --exclude-database ?
Point 2:
For each database, we are registering entry to on_exit_nicely array due AH entry but max size of array is MAX_ON_EXIT_NICELY=20,
so after 20 db restoring, we are getting fatal so either my code needs to reset this array or do we need to increase array size?
Here, I am attaching an updated patch for review and testing.