From a7d8052910d6254688ef66840554f1bba80a2238 Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Tue, 14 Oct 2025 16:27:13 +0900 Subject: [PATCH] pg_upgrade: Use new routine to retrieve data of PG_VERSION Unsurprisingly, this shaves code. get_major_server_version() can be replaced by the new routine added by cd0be131ba6f, with the contents of PG_VERSION stored in an allocated buffer instead of a fixed-sized one. Author: Michael Paquier Reviewed-by: Masahiko Sawada Discussion: https://postgr.es/m/aOiirvWJzwdVCXph@paquier.xyz --- src/bin/pg_upgrade/exec.c | 5 +++-- src/bin/pg_upgrade/pg_upgrade.h | 3 +-- src/bin/pg_upgrade/server.c | 39 --------------------------------- 3 files changed, 4 insertions(+), 43 deletions(-) diff --git a/src/bin/pg_upgrade/exec.c b/src/bin/pg_upgrade/exec.c index 63f2815a7cd..c045633d0c2 100644 --- a/src/bin/pg_upgrade/exec.c +++ b/src/bin/pg_upgrade/exec.c @@ -12,6 +12,7 @@ #include #include "common/string.h" +#include "fe_utils/version.h" #include "pg_upgrade.h" static void check_data_dir(ClusterInfo *cluster); @@ -343,8 +344,8 @@ check_data_dir(ClusterInfo *cluster) const char *pg_data = cluster->pgdata; /* get the cluster version */ - cluster->major_version = get_major_server_version(cluster); - + cluster->major_version = get_pg_version(cluster->pgdata, + &cluster->major_version_str); check_single_dir(pg_data, ""); check_single_dir(pg_data, "base"); check_single_dir(pg_data, "global"); diff --git a/src/bin/pg_upgrade/pg_upgrade.h b/src/bin/pg_upgrade/pg_upgrade.h index 0ef47be0dc1..e86336f4be9 100644 --- a/src/bin/pg_upgrade/pg_upgrade.h +++ b/src/bin/pg_upgrade/pg_upgrade.h @@ -298,7 +298,7 @@ typedef struct char *sockdir; /* directory for Unix Domain socket, if any */ unsigned short port; /* port number where postmaster is waiting */ uint32 major_version; /* PG_VERSION of cluster */ - char major_version_str[64]; /* string PG_VERSION of cluster */ + char *major_version_str; /* string PG_VERSION of cluster */ uint32 bin_version; /* version returned from pg_ctl */ char **tablespaces; /* tablespace directories */ int num_tablespaces; @@ -473,7 +473,6 @@ char *cluster_conn_opts(ClusterInfo *cluster); bool start_postmaster(ClusterInfo *cluster, bool report_and_exit_on_error); void stop_postmaster(bool in_atexit); -uint32 get_major_server_version(ClusterInfo *cluster); void check_pghost_envvar(void); diff --git a/src/bin/pg_upgrade/server.c b/src/bin/pg_upgrade/server.c index 7eb15bc7d5a..d51bee885b8 100644 --- a/src/bin/pg_upgrade/server.c +++ b/src/bin/pg_upgrade/server.c @@ -148,45 +148,6 @@ executeQueryOrDie(PGconn *conn, const char *fmt,...) } -/* - * get_major_server_version() - * - * gets the version (in unsigned int form) for the given datadir. Assumes - * that datadir is an absolute path to a valid pgdata directory. The version - * is retrieved by reading the PG_VERSION file. - */ -uint32 -get_major_server_version(ClusterInfo *cluster) -{ - FILE *version_fd; - char ver_filename[MAXPGPATH]; - int v1 = 0, - v2 = 0; - - snprintf(ver_filename, sizeof(ver_filename), "%s/PG_VERSION", - cluster->pgdata); - if ((version_fd = fopen(ver_filename, "r")) == NULL) - pg_fatal("could not open version file \"%s\": %m", ver_filename); - - if (fscanf(version_fd, "%63s", cluster->major_version_str) == 0 || - sscanf(cluster->major_version_str, "%d.%d", &v1, &v2) < 1) - pg_fatal("could not parse version file \"%s\"", ver_filename); - - fclose(version_fd); - - if (v1 < 10) - { - /* old style, e.g. 9.6.1 */ - return v1 * 10000 + v2 * 100; - } - else - { - /* new style, e.g. 10.1 */ - return v1 * 10000; - } -} - - static void stop_postmaster_atexit(void) {