Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a50d860

Browse files
committed
Allow pg_upgrade to upgrade an old cluster that doesn't have a
'postgres' database.
1 parent 806a2ae commit a50d860

File tree

3 files changed

+17
-45
lines changed

3 files changed

+17
-45
lines changed

contrib/pg_upgrade/check.c

+1-36
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
static void set_locale_and_encoding(ClusterInfo *cluster);
1616
static void check_new_cluster_is_empty(void);
17-
static void check_old_cluster_has_new_cluster_dbs(void);
1817
static void check_locale_and_encoding(ControlData *oldctrl,
1918
ControlData *newctrl);
2019
static void check_is_super_user(ClusterInfo *cluster);
@@ -127,7 +126,6 @@ check_new_cluster(void)
127126

128127
check_new_cluster_is_empty();
129128
check_for_prepared_transactions(&new_cluster);
130-
check_old_cluster_has_new_cluster_dbs();
131129

132130
check_loadable_libraries();
133131

@@ -381,39 +379,6 @@ check_new_cluster_is_empty(void)
381379
}
382380

383381

384-
/*
385-
* If someone removes the 'postgres' database from the old cluster and
386-
* the new cluster has a 'postgres' database, the number of databases
387-
* will not match. We actually could upgrade such a setup, but it would
388-
* violate the 1-to-1 mapping of database counts, so we throw an error
389-
* instead. We would detect this as a database count mismatch during
390-
* upgrade, but we want to detect it during the check phase and report
391-
* the database name.
392-
*/
393-
static void
394-
check_old_cluster_has_new_cluster_dbs(void)
395-
{
396-
int old_dbnum,
397-
new_dbnum;
398-
399-
for (new_dbnum = 0; new_dbnum < new_cluster.dbarr.ndbs; new_dbnum++)
400-
{
401-
for (old_dbnum = 0; old_dbnum < old_cluster.dbarr.ndbs; old_dbnum++)
402-
if (strcmp(old_cluster.dbarr.dbs[old_dbnum].db_name,
403-
new_cluster.dbarr.dbs[new_dbnum].db_name) == 0)
404-
break;
405-
if (old_dbnum == old_cluster.dbarr.ndbs)
406-
{
407-
if (strcmp(new_cluster.dbarr.dbs[new_dbnum].db_name, "postgres") == 0)
408-
pg_log(PG_FATAL, "The \"postgres\" database must exist in the old cluster\n");
409-
else
410-
pg_log(PG_FATAL, "New cluster database \"%s\" does not exist in the old cluster\n",
411-
new_cluster.dbarr.dbs[new_dbnum].db_name);
412-
}
413-
}
414-
}
415-
416-
417382
/*
418383
* create_script_for_old_cluster_deletion()
419384
*
@@ -462,7 +427,7 @@ create_script_for_old_cluster_deletion(char **deletion_script_file_name)
462427
fprintf(script, RM_CMD " %s%s/PG_VERSION\n",
463428
os_info.tablespaces[tblnum], old_cluster.tablespace_suffix);
464429

465-
for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++)
430+
for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
466431
{
467432
fprintf(script, RMDIR_CMD " %s%s/%d\n",
468433
os_info.tablespaces[tblnum], old_cluster.tablespace_suffix,

contrib/pg_upgrade/function.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ get_loadable_libraries(void)
132132
int totaltups;
133133
int dbnum;
134134

135-
ress = (PGresult **)
136-
pg_malloc(old_cluster.dbarr.ndbs * sizeof(PGresult *));
135+
ress = (PGresult **) pg_malloc(old_cluster.dbarr.ndbs * sizeof(PGresult *));
137136
totaltups = 0;
138137

139138
/* Fetch all library names, removing duplicates within each DB */

contrib/pg_upgrade/relfilenode.c

+15-7
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,30 @@ const char *
3434
transfer_all_new_dbs(DbInfoArr *old_db_arr,
3535
DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata)
3636
{
37-
int dbnum;
37+
int old_dbnum, new_dbnum;
3838
const char *msg = NULL;
3939

4040
prep_status("Restoring user relation files\n");
4141

42-
if (old_db_arr->ndbs != new_db_arr->ndbs)
43-
pg_log(PG_FATAL, "old and new clusters have a different number of databases\n");
44-
45-
for (dbnum = 0; dbnum < old_db_arr->ndbs; dbnum++)
42+
/* Scan the old cluster databases and transfer their files */
43+
for (old_dbnum = new_dbnum = 0;
44+
old_dbnum < old_db_arr->ndbs && new_dbnum < new_db_arr->ndbs;
45+
old_dbnum++, new_dbnum++)
4646
{
47-
DbInfo *old_db = &old_db_arr->dbs[dbnum];
48-
DbInfo *new_db = &new_db_arr->dbs[dbnum];
47+
DbInfo *old_db = &old_db_arr->dbs[old_dbnum];
48+
DbInfo *new_db = &new_db_arr->dbs[new_dbnum];
4949
FileNameMap *mappings;
5050
int n_maps;
5151
pageCnvCtx *pageConverter = NULL;
5252

53+
/*
54+
* Advance past any databases that exist in the new cluster
55+
* but not in the old, e.g. "postgres".
56+
*/
57+
while (strcmp(old_db->db_name, new_db->db_name) != 0 &&
58+
new_dbnum < new_db_arr->ndbs)
59+
new_db = &new_db_arr->dbs[++new_dbnum];
60+
5361
if (strcmp(old_db->db_name, new_db->db_name) != 0)
5462
pg_log(PG_FATAL, "old and new databases have different names: old \"%s\", new \"%s\"\n",
5563
old_db->db_name, new_db->db_name);

0 commit comments

Comments
 (0)