Skip to content

Commit ee646df

Browse files
committed
pg_upgrade: assume user is install user
The user specified to the upgrade was effectively the install user, but that was not clearly stated in the comments, documentation, or error messages.
1 parent b4bd6f6 commit ee646df

File tree

3 files changed

+25
-30
lines changed

3 files changed

+25
-30
lines changed

contrib/pg_upgrade/check.c

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "postgres_fe.h"
1111

12+
#include "catalog/pg_authid.h"
1213
#include "mb/pg_wchar.h"
1314
#include "pg_upgrade.h"
1415

@@ -19,7 +20,7 @@ static void check_locale_and_encoding(ControlData *oldctrl,
1920
ControlData *newctrl);
2021
static bool equivalent_locale(const char *loca, const char *locb);
2122
static bool equivalent_encoding(const char *chara, const char *charb);
22-
static void check_is_super_user(ClusterInfo *cluster);
23+
static void check_is_install_user(ClusterInfo *cluster);
2324
static void check_for_prepared_transactions(ClusterInfo *cluster);
2425
static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster);
2526
static void check_for_reg_data_type_usage(ClusterInfo *cluster);
@@ -94,7 +95,7 @@ check_and_dump_old_cluster(bool live_check, char **sequence_script_file_name)
9495
/*
9596
* Check for various failure cases
9697
*/
97-
check_is_super_user(&old_cluster);
98+
check_is_install_user(&old_cluster);
9899
check_for_prepared_transactions(&old_cluster);
99100
check_for_reg_data_type_usage(&old_cluster);
100101
check_for_isn_and_int8_passing_mismatch(&old_cluster);
@@ -158,22 +159,7 @@ check_new_cluster(void)
158159
if (user_opts.transfer_mode == TRANSFER_MODE_LINK)
159160
check_hard_link();
160161

161-
check_is_super_user(&new_cluster);
162-
163-
/*
164-
* We don't restore our own user, so both clusters must match have
165-
* matching install-user oids.
166-
*/
167-
if (old_cluster.install_role_oid != new_cluster.install_role_oid)
168-
pg_fatal("Old and new cluster install users have different values for pg_authid.oid.\n");
169-
170-
/*
171-
* We only allow the install user in the new cluster because other defined
172-
* users might match users defined in the old cluster and generate an
173-
* error during pg_dump restore.
174-
*/
175-
if (new_cluster.role_count != 1)
176-
pg_fatal("Only the install user can be defined in the new cluster.\n");
162+
check_is_install_user(&new_cluster);
177163

178164
check_for_prepared_transactions(&new_cluster);
179165
}
@@ -698,30 +684,35 @@ create_script_for_old_cluster_deletion(char **deletion_script_file_name)
698684

699685

700686
/*
701-
* check_is_super_user()
687+
* check_is_install_user()
702688
*
703-
* Check we are superuser, and output user id and user count
689+
* Check we are the install user, and that the new cluster
690+
* has no other users.
704691
*/
705692
static void
706-
check_is_super_user(ClusterInfo *cluster)
693+
check_is_install_user(ClusterInfo *cluster)
707694
{
708695
PGresult *res;
709696
PGconn *conn = connectToServer(cluster, "template1");
710697

711-
prep_status("Checking database user is a superuser");
698+
prep_status("Checking database user is the install user");
712699

713700
/* Can't use pg_authid because only superusers can view it. */
714701
res = executeQueryOrDie(conn,
715702
"SELECT rolsuper, oid "
716703
"FROM pg_catalog.pg_roles "
717704
"WHERE rolname = current_user");
718705

719-
if (PQntuples(res) != 1 || strcmp(PQgetvalue(res, 0, 0), "t") != 0)
720-
pg_fatal("database user \"%s\" is not a superuser\n",
706+
/*
707+
* We only allow the install user in the new cluster (see comment below)
708+
* and we preserve pg_authid.oid, so this must be the install user in
709+
* the old cluster too.
710+
*/
711+
if (PQntuples(res) != 1 ||
712+
atooid(PQgetvalue(res, 0, 1)) != BOOTSTRAP_SUPERUSERID)
713+
pg_fatal("database user \"%s\" is not the install user\n",
721714
os_info.user);
722715

723-
cluster->install_role_oid = atooid(PQgetvalue(res, 0, 1));
724-
725716
PQclear(res);
726717

727718
res = executeQueryOrDie(conn,
@@ -731,7 +722,13 @@ check_is_super_user(ClusterInfo *cluster)
731722
if (PQntuples(res) != 1)
732723
pg_fatal("could not determine the number of users\n");
733724

734-
cluster->role_count = atoi(PQgetvalue(res, 0, 0));
725+
/*
726+
* We only allow the install user in the new cluster because other defined
727+
* users might match users defined in the old cluster and generate an
728+
* error during pg_dump restore.
729+
*/
730+
if (cluster == &new_cluster && atooid(PQgetvalue(res, 0, 0)) != 1)
731+
pg_fatal("Only the install user can be defined in the new cluster.\n");
735732

736733
PQclear(res);
737734

contrib/pg_upgrade/pg_upgrade.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,6 @@ typedef struct
256256
char major_version_str[64]; /* string PG_VERSION of cluster */
257257
uint32 bin_version; /* version returned from pg_ctl */
258258
Oid pg_database_oid; /* OID of pg_database relation */
259-
Oid install_role_oid; /* OID of connected role */
260-
Oid role_count; /* number of roles defined in the cluster */
261259
const char *tablespace_suffix; /* directory specification */
262260
} ClusterInfo;
263261

doc/src/sgml/pgupgrade.sgml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
<varlistentry>
165165
<term><option>-U</option> <replaceable>username</></term>
166166
<term><option>--username=</option><replaceable>username</></term>
167-
<listitem><para>cluster's super user name; environment
167+
<listitem><para>cluster's install user name; environment
168168
variable <envar>PGUSER</></para></listitem>
169169
</varlistentry>
170170

0 commit comments

Comments
 (0)