Fix for pg_upgrade user flag - Mailing list pgsql-hackers

From Bruce Momjian
Subject Fix for pg_upgrade user flag
Date
Msg-id 201105071256.p47CuAb01225@momjian.us
Whole thread Raw
Responses Re: Fix for pg_upgrade user flag  (Robert Haas <robertmhaas@gmail.com>)
List pgsql-hackers
The attached, applied patch checks that the pg_upgrade user specified is
a super-user.  It also reports the error message when the post-pg_ctl
connection fails.

This was prompted by a private bug report from EnterpriseDB.

--
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + It's impossible for everything to be true. +
diff --git a/contrib/pg_upgrade/check.c b/contrib/pg_upgrade/check.c
new file mode 100644
index 35b178e..26dec39
*** a/contrib/pg_upgrade/check.c
--- b/contrib/pg_upgrade/check.c
*************** static void check_new_cluster_is_empty(v
*** 15,20 ****
--- 15,21 ----
  static void check_old_cluster_has_new_cluster_dbs(void);
  static void check_locale_and_encoding(ControlData *oldctrl,
                            ControlData *newctrl);
+ static void check_is_super_user(ClusterInfo *cluster);
  static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster);
  static void check_for_reg_data_type_usage(ClusterInfo *cluster);

*************** check_old_cluster(bool live_check,
*** 63,69 ****
      /*
       * Check for various failure cases
       */
!
      check_for_reg_data_type_usage(&old_cluster);
      check_for_isn_and_int8_passing_mismatch(&old_cluster);

--- 64,70 ----
      /*
       * Check for various failure cases
       */
!     check_is_super_user(&old_cluster);
      check_for_reg_data_type_usage(&old_cluster);
      check_for_isn_and_int8_passing_mismatch(&old_cluster);

*************** create_script_for_old_cluster_deletion(
*** 473,478 ****
--- 474,505 ----


  /*
+  *    check_is_super_user()
+  *
+  *    Make sure we are the super-user.
+  */
+ static void
+ check_is_super_user(ClusterInfo *cluster)
+ {
+     PGresult   *res;
+     PGconn       *conn = connectToServer(cluster, "template1");
+
+     /* Can't use pg_authid because only superusers can view it. */
+     res = executeQueryOrDie(conn,
+                             "SELECT rolsuper "
+                             "FROM pg_catalog.pg_roles "
+                             "WHERE rolname = current_user");
+
+     if (PQntuples(res) != 1 || strcmp(PQgetvalue(res, 0, 0), "t") != 0)
+         pg_log(PG_FATAL, "the database user is not a superuser\n");
+
+     PQclear(res);
+
+     PQfinish(conn);
+ }
+
+
+ /*
   *    check_for_isn_and_int8_passing_mismatch()
   *
   *    /contrib/isn relies on data type int8, and in 8.4 int8 can now be passed
diff --git a/contrib/pg_upgrade/server.c b/contrib/pg_upgrade/server.c
new file mode 100644
index 9a55075..d6efe9a
*** a/contrib/pg_upgrade/server.c
--- b/contrib/pg_upgrade/server.c
*************** connectToServer(ClusterInfo *cluster, co
*** 27,33 ****

      if (conn == NULL || PQstatus(conn) != CONNECTION_OK)
      {
!         pg_log(PG_REPORT, "Connection to database failed: %s\n",
                 PQerrorMessage(conn));

          if (conn)
--- 27,33 ----

      if (conn == NULL || PQstatus(conn) != CONNECTION_OK)
      {
!         pg_log(PG_REPORT, "connection to database failed: %s\n",
                 PQerrorMessage(conn));

          if (conn)
*************** start_postmaster(ClusterInfo *cluster)
*** 189,195 ****
      if ((conn = get_db_conn(cluster, "template1")) == NULL ||
          PQstatus(conn) != CONNECTION_OK)
      {
!         if (conn)
              PQfinish(conn);
          pg_log(PG_FATAL, "unable to connect to %s postmaster started with the command: %s\n"
                 "Perhaps pg_hba.conf was not set to \"trust\".\n",
--- 189,197 ----
      if ((conn = get_db_conn(cluster, "template1")) == NULL ||
          PQstatus(conn) != CONNECTION_OK)
      {
!         pg_log(PG_REPORT, "\nconnection to database failed: %s\n",
!                PQerrorMessage(conn));
!          if (conn)
              PQfinish(conn);
          pg_log(PG_FATAL, "unable to connect to %s postmaster started with the command: %s\n"
                 "Perhaps pg_hba.conf was not set to \"trust\".\n",

pgsql-hackers by date:

Previous
From: Dimitri Fontaine
Date:
Subject: Re: Why not install pgstattuple by default?
Next
From: Robert Haas
Date:
Subject: Re: time-delayed standbys