Skip to content

Commit 3c0471b

Browse files
committed
pg_upgrade: generate check error for left-over new tablespace
Previously, if pg_upgrade failed, and the user recreated the cluster but did not remove the new cluster tablespace directory, a later pg_upgrade would fail since the new tablespace directory would already exists. This adds error reporting for this during check. Reported-by: Justin Pryzby Discussion: https://postgr.es/m/20200925005531.GJ23631@telsasoft.com Backpatch-through: 9.5
1 parent 7255943 commit 3c0471b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/bin/pg_upgrade/check.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ static void check_for_tables_with_oids(ClusterInfo *cluster);
2727
static void check_for_reg_data_type_usage(ClusterInfo *cluster);
2828
static void check_for_jsonb_9_4_usage(ClusterInfo *cluster);
2929
static void check_for_pg_role_prefix(ClusterInfo *cluster);
30+
static void check_for_new_tablespace_dir(ClusterInfo *new_cluster);
3031
static char *get_canonical_locale_name(int category, const char *locale);
3132

3233

@@ -187,6 +188,8 @@ check_new_cluster(void)
187188
check_is_install_user(&new_cluster);
188189

189190
check_for_prepared_transactions(&new_cluster);
191+
192+
check_for_new_tablespace_dir(&new_cluster);
190193
}
191194

192195

@@ -527,6 +530,43 @@ create_script_for_cluster_analyze(char **analyze_script_file_name)
527530
}
528531

529532

533+
/*
534+
* A previous run of pg_upgrade might have failed and the new cluster
535+
* directory recreated, but they might have forgotten to remove
536+
* the new cluster's tablespace directories. Therefore, check that
537+
* new cluster tablespace directories do not already exist. If
538+
* they do, it would cause an error while restoring global objects.
539+
* This allows the failure to be detected at check time, rather than
540+
* during schema restore.
541+
*
542+
* Note, v8.4 has no tablespace_suffix, which is fine so long as the
543+
* version being upgraded *to* has a suffix, since it's not allowed
544+
* to pg_upgrade from a version to the same version if tablespaces are
545+
* in use.
546+
*/
547+
static void
548+
check_for_new_tablespace_dir(ClusterInfo *new_cluster)
549+
{
550+
char new_tablespace_dir[MAXPGPATH];
551+
552+
prep_status("Checking for new cluster tablespace directories");
553+
554+
for (int tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
555+
{
556+
struct stat statbuf;
557+
558+
snprintf(new_tablespace_dir, MAXPGPATH, "%s%s",
559+
os_info.old_tablespaces[tblnum],
560+
new_cluster->tablespace_suffix);
561+
562+
if (stat(new_tablespace_dir, &statbuf) == 0 || errno != ENOENT)
563+
pg_fatal("new cluster tablespace directory already exists: \"%s\"\n",
564+
new_tablespace_dir);
565+
}
566+
567+
check_ok();
568+
}
569+
530570
/*
531571
* create_script_for_old_cluster_deletion()
532572
*

0 commit comments

Comments
 (0)