Re: pg_upgrade - add config directory setting - Mailing list pgsql-hackers

From Bruce Momjian
Subject Re: pg_upgrade - add config directory setting
Date
Msg-id 201110070145.p971jD701976@momjian.us
Whole thread Raw
In response to Re: pg_upgrade - add config directory setting  (Bruce Momjian <bruce@momjian.us>)
Responses Re: pg_upgrade - add config directory setting
List pgsql-hackers
Bruce Momjian wrote:
> I will now work on pg_upgrade to also use the new flag to find the data
> directory from a config-only install.  However, this is only available
> in PG 9.2, and it will only be in PG 9.3 that you can hope to use this
> feature (if old is PG 9.2 or later).  I am afraid the symlink hack will
> have to be used for several more years, and if you are supporting
> upgrades from pre-9.2, perhaps forever.

The attached patch uses "postmaster -C data_directory" to allow
config-only upgrades.  It will allow a normal 9.1 cluster to be upgraded
to a 9.2 config-only cluster.

--
  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/option.c b/contrib/pg_upgrade/option.c
new file mode 100644
index bdb7ddb..3ab1b5c
*** a/contrib/pg_upgrade/option.c
--- b/contrib/pg_upgrade/option.c
*************** parseCommandLine(int argc, char *argv[])
*** 112,121 ****
--- 112,123 ----

              case 'd':
                  old_cluster.pgdata = pg_strdup(optarg);
+                 old_cluster.pgconfig = pg_strdup(optarg);
                  break;

              case 'D':
                  new_cluster.pgdata = pg_strdup(optarg);
+                 new_cluster.pgconfig = pg_strdup(optarg);
                  break;

              case 'g':
*************** check_required_directory(char **dirpath,
*** 319,321 ****
--- 321,381 ----
  #endif
          (*dirpath)[strlen(*dirpath) - 1] = 0;
  }
+
+ /*
+  * adjust_data_dir
+  *
+  * If a configuration-only directory was specified, find the real data dir
+  * by quering the running server.  This has limited checking because we
+  * can't check for a running server because we can't find postmaster.pid.
+  */
+ void
+ adjust_data_dir(ClusterInfo *cluster)
+ {
+     char        filename[MAXPGPATH];
+     char        cmd[MAXPGPATH], cmd_output[MAX_STRING];
+     FILE       *fd, *output;
+
+     /* If there is no postgresql.conf, it can't be a config-only dir */
+     snprintf(filename, sizeof(filename), "%s/postgresql.conf", cluster->pgconfig);
+     if ((fd = fopen(filename, "r")) == NULL)
+         return;
+     fclose(fd);
+
+     /* If PG_VERSION exists, it can't be a config-only dir */
+     snprintf(filename, sizeof(filename), "%s/PG_VERSION", cluster->pgconfig);
+     if ((fd = fopen(filename, "r")) != NULL)
+     {
+         fclose(fd);
+         return;
+     }
+
+     /* Must be a configuration directory, so find the real data directory. */
+
+     prep_status("Finding the real data directory for the %s cluster",
+                 CLUSTER_NAME(cluster));
+
+     /*
+      * We don't have a data directory yet, so we can't check the PG
+      * version, so this might fail --- only works for PG 9.2+.   If this
+      * fails, pg_upgrade will fail anyway because the data files will not
+      * be found.
+      */
+     snprintf(cmd, sizeof(cmd), "\"%s/postmaster\" -D \"%s\" -C data_directory",
+              cluster->bindir, cluster->pgconfig);
+
+     if ((output = popen(cmd, "r")) == NULL ||
+         fgets(cmd_output, sizeof(cmd_output), output) == NULL)
+         pg_log(PG_FATAL, "Could not get data directory using %s: %s\n",
+         cmd, getErrorText(errno));
+
+     pclose(output);
+
+     /* Remove trailing newline */
+     if (strchr(cmd_output, '\n') != NULL)
+         *strchr(cmd_output, '\n') = '\0';
+
+     cluster->pgdata = pg_strdup(cmd_output);
+
+     check_ok();
+ }
diff --git a/contrib/pg_upgrade/pg_upgrade.c b/contrib/pg_upgrade/pg_upgrade.c
new file mode 100644
index 0568aca..273561e
*** a/contrib/pg_upgrade/pg_upgrade.c
--- b/contrib/pg_upgrade/pg_upgrade.c
*************** main(int argc, char **argv)
*** 68,73 ****
--- 68,76 ----

      parseCommandLine(argc, argv);

+     adjust_data_dir(&old_cluster);
+     adjust_data_dir(&new_cluster);
+
      output_check_banner(&live_check);

      setup(argv[0], live_check);
diff --git a/contrib/pg_upgrade/pg_upgrade.h b/contrib/pg_upgrade/pg_upgrade.h
new file mode 100644
index 46aed74..0fb16ed
*** a/contrib/pg_upgrade/pg_upgrade.h
--- b/contrib/pg_upgrade/pg_upgrade.h
*************** typedef struct
*** 187,192 ****
--- 187,193 ----
      ControlData controldata;    /* pg_control information */
      DbInfoArr    dbarr;            /* dbinfos array */
      char       *pgdata;            /* pathname for cluster's $PGDATA directory */
+     char       *pgconfig;        /* pathname for cluster's config file directory */
      char       *bindir;            /* pathname for cluster's executable directory */
      unsigned short port;        /* port number where postmaster is waiting */
      uint32        major_version;    /* PG_VERSION of cluster */
*************** void print_maps(FileNameMap *maps, int n
*** 361,366 ****
--- 362,368 ----
  /* option.c */

  void        parseCommandLine(int argc, char *argv[]);
+ void        adjust_data_dir(ClusterInfo *cluster);

  /* relfilenode.c */

diff --git a/contrib/pg_upgrade/server.c b/contrib/pg_upgrade/server.c
new file mode 100644
index 8c4aec9..d512ef3
*** a/contrib/pg_upgrade/server.c
--- b/contrib/pg_upgrade/server.c
*************** start_postmaster(ClusterInfo *cluster)
*** 169,175 ****
      snprintf(cmd, sizeof(cmd),
               SYSTEMQUOTE "\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" "
               "-o \"-p %d %s\" start >> \"%s\" 2>&1" SYSTEMQUOTE,
!              cluster->bindir, log_opts.filename2, cluster->pgdata, cluster->port,
               (cluster->controldata.cat_ver >=
                BINARY_UPGRADE_SERVER_FLAG_CAT_VER) ? "-b" :
               "-c autovacuum=off -c autovacuum_freeze_max_age=2000000000",
--- 169,175 ----
      snprintf(cmd, sizeof(cmd),
               SYSTEMQUOTE "\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" "
               "-o \"-p %d %s\" start >> \"%s\" 2>&1" SYSTEMQUOTE,
!              cluster->bindir, log_opts.filename2, cluster->pgconfig, cluster->port,
               (cluster->controldata.cat_ver >=
                BINARY_UPGRADE_SERVER_FLAG_CAT_VER) ? "-b" :
               "-c autovacuum=off -c autovacuum_freeze_max_age=2000000000",
*************** stop_postmaster(bool fast)
*** 208,224 ****
  {
      char        cmd[MAXPGPATH];
      const char *bindir;
!     const char *datadir;

      if (os_info.running_cluster == &old_cluster)
      {
          bindir = old_cluster.bindir;
!         datadir = old_cluster.pgdata;
      }
      else if (os_info.running_cluster == &new_cluster)
      {
          bindir = new_cluster.bindir;
!         datadir = new_cluster.pgdata;
      }
      else
          return;                    /* no cluster running */
--- 208,224 ----
  {
      char        cmd[MAXPGPATH];
      const char *bindir;
!     const char *configdir;

      if (os_info.running_cluster == &old_cluster)
      {
          bindir = old_cluster.bindir;
!         configdir = old_cluster.pgconfig;
      }
      else if (os_info.running_cluster == &new_cluster)
      {
          bindir = new_cluster.bindir;
!         configdir = new_cluster.pgconfig;
      }
      else
          return;                    /* no cluster running */
*************** stop_postmaster(bool fast)
*** 226,232 ****
      snprintf(cmd, sizeof(cmd),
               SYSTEMQUOTE "\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" %s stop >> "
               "\"%s\" 2>&1" SYSTEMQUOTE,
!              bindir, log_opts.filename2, datadir, fast ? "-m fast" : "",
               log_opts.filename2);

      exec_prog(fast ? false : true, "%s", cmd);
--- 226,232 ----
      snprintf(cmd, sizeof(cmd),
               SYSTEMQUOTE "\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" %s stop >> "
               "\"%s\" 2>&1" SYSTEMQUOTE,
!              bindir, log_opts.filename2, configdir, fast ? "-m fast" : "",
               log_opts.filename2);

      exec_prog(fast ? false : true, "%s", cmd);

pgsql-hackers by date:

Previous
From: Alex Goncharov
Date:
Subject: Re: libpq, PQdescribePrepared -> PQftype, PQfmod, no PQnullable
Next
From: gabrielle
Date:
Subject: Re: [PATCH] Log crashed backend's query v3