Skip to content

Commit af97980

Browse files
committed
In pg_upgrade, verify that the install user has the same oid on both
clusters, and make sure the new cluster has no additional users. Backpatch to 9.1.
1 parent 580b941 commit af97980

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

contrib/pg_upgrade/check.c

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,38 @@ check_new_cluster(void)
118118
{
119119
set_locale_and_encoding(&new_cluster);
120120

121+
check_locale_and_encoding(&old_cluster.controldata, &new_cluster.controldata);
122+
121123
get_db_and_rel_infos(&new_cluster);
122124

123125
check_new_cluster_is_empty();
124-
check_for_prepared_transactions(&new_cluster);
126+
125127
check_old_cluster_has_new_cluster_dbs();
126128

127129
check_loadable_libraries();
128130

129-
check_locale_and_encoding(&old_cluster.controldata, &new_cluster.controldata);
130-
131131
if (user_opts.transfer_mode == TRANSFER_MODE_LINK)
132132
check_hard_link();
133+
134+
check_is_super_user(&new_cluster);
135+
136+
/*
137+
* We don't restore our own user, so both clusters must match have
138+
* matching install-user oids.
139+
*/
140+
if (old_cluster.install_role_oid != new_cluster.install_role_oid)
141+
pg_log(PG_FATAL,
142+
"Old and new cluster install users have different values for pg_authid.oid.\n");
143+
144+
/*
145+
* We only allow the install user in the new cluster because other
146+
* defined users might match users defined in the old cluster and
147+
* generate an error during pg_dump restore.
148+
*/
149+
if (new_cluster.role_count != 1)
150+
pg_log(PG_FATAL, "Only the install user can be defined in the new cluster.\n");
151+
152+
check_for_prepared_transactions(&new_cluster);
133153
}
134154

135155

@@ -485,7 +505,7 @@ create_script_for_old_cluster_deletion(
485505
/*
486506
* check_is_super_user()
487507
*
488-
* Make sure we are the super-user.
508+
* Check we are superuser, and out user id and user count
489509
*/
490510
static void
491511
check_is_super_user(ClusterInfo *cluster)
@@ -497,14 +517,27 @@ check_is_super_user(ClusterInfo *cluster)
497517

498518
/* Can't use pg_authid because only superusers can view it. */
499519
res = executeQueryOrDie(conn,
500-
"SELECT rolsuper "
520+
"SELECT rolsuper, oid "
501521
"FROM pg_catalog.pg_roles "
502522
"WHERE rolname = current_user");
503523

504524
if (PQntuples(res) != 1 || strcmp(PQgetvalue(res, 0, 0), "t") != 0)
505525
pg_log(PG_FATAL, "database user \"%s\" is not a superuser\n",
506526
os_info.user);
507527

528+
cluster->install_role_oid = atooid(PQgetvalue(res, 0, 1));
529+
530+
PQclear(res);
531+
532+
res = executeQueryOrDie(conn,
533+
"SELECT COUNT(*) "
534+
"FROM pg_catalog.pg_roles ");
535+
536+
if (PQntuples(res) != 1)
537+
pg_log(PG_FATAL, "could not determine the number of users\n");
538+
539+
cluster->role_count = atoi(PQgetvalue(res, 0, 0));
540+
508541
PQclear(res);
509542

510543
PQfinish(conn);

contrib/pg_upgrade/pg_upgrade.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* We control all assignments of pg_enum.oid because these oids are stored
3030
* in user tables as enum values.
3131
*
32-
* We control all assignments of pg_auth.oid because these oids are stored
32+
* We control all assignments of pg_authid.oid because these oids are stored
3333
* in pg_largeobject_metadata.
3434
*/
3535

contrib/pg_upgrade/pg_upgrade.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ typedef struct
187187
char major_version_str[64]; /* string PG_VERSION of cluster */
188188
uint32 bin_version; /* version returned from pg_ctl */
189189
Oid pg_database_oid; /* OID of pg_database relation */
190+
Oid install_role_oid; /* OID of connected role */
191+
Oid role_count; /* number of roles defined in the cluster */
190192
char *tablespace_suffix; /* directory specification */
191193
} ClusterInfo;
192194

0 commit comments

Comments
 (0)