Skip to content

Commit d9ae229

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 43ca5e0 commit d9ae229

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
@@ -24,6 +24,7 @@ static void check_for_prepared_transactions(ClusterInfo *cluster);
2424
static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster);
2525
static void check_for_reg_data_type_usage(ClusterInfo *cluster);
2626
static void check_for_jsonb_9_4_usage(ClusterInfo *cluster);
27+
static void check_for_new_tablespace_dir(ClusterInfo *new_cluster);
2728
static void get_bin_version(ClusterInfo *cluster);
2829
static char *get_canonical_locale_name(int category, const char *locale);
2930

@@ -138,6 +139,8 @@ check_new_cluster(void)
138139
check_is_install_user(&new_cluster);
139140

140141
check_for_prepared_transactions(&new_cluster);
142+
143+
check_for_new_tablespace_dir(&new_cluster);
141144
}
142145

143146

@@ -476,6 +479,43 @@ create_script_for_cluster_analyze(char **analyze_script_file_name)
476479
}
477480

478481

482+
/*
483+
* A previous run of pg_upgrade might have failed and the new cluster
484+
* directory recreated, but they might have forgotten to remove
485+
* the new cluster's tablespace directories. Therefore, check that
486+
* new cluster tablespace directories do not already exist. If
487+
* they do, it would cause an error while restoring global objects.
488+
* This allows the failure to be detected at check time, rather than
489+
* during schema restore.
490+
*
491+
* Note, v8.4 has no tablespace_suffix, which is fine so long as the
492+
* version being upgraded *to* has a suffix, since it's not allowed
493+
* to pg_upgrade from a version to the same version if tablespaces are
494+
* in use.
495+
*/
496+
static void
497+
check_for_new_tablespace_dir(ClusterInfo *new_cluster)
498+
{
499+
char new_tablespace_dir[MAXPGPATH];
500+
501+
prep_status("Checking for new cluster tablespace directories");
502+
503+
for (int tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
504+
{
505+
struct stat statbuf;
506+
507+
snprintf(new_tablespace_dir, MAXPGPATH, "%s%s",
508+
os_info.old_tablespaces[tblnum],
509+
new_cluster->tablespace_suffix);
510+
511+
if (stat(new_tablespace_dir, &statbuf) == 0 || errno != ENOENT)
512+
pg_fatal("new cluster tablespace directory already exists: \"%s\"\n",
513+
new_tablespace_dir);
514+
}
515+
516+
check_ok();
517+
}
518+
479519
/*
480520
* create_script_for_old_cluster_deletion()
481521
*

0 commit comments

Comments
 (0)