pg_upgrade: Use new routine to retrieve data of PG_VERSION
authorMichael Paquier <michael@paquier.xyz>
Tue, 14 Oct 2025 07:27:13 +0000 (16:27 +0900)
committerMichael Paquier <michael@paquier.xyz>
Tue, 14 Oct 2025 07:27:13 +0000 (16:27 +0900)
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 <michael@paquier.xyz>
Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com>
Discussion: https://postgr.es/m/aOiirvWJzwdVCXph@paquier.xyz

src/bin/pg_upgrade/exec.c
src/bin/pg_upgrade/pg_upgrade.h
src/bin/pg_upgrade/server.c

index 63f2815a7cd10cfd32e8716042f2763fe375228b..c045633d0c2acb07ea06c5ebb998cb1564543d2c 100644 (file)
@@ -12,6 +12,7 @@
 #include <fcntl.h>
 
 #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");
index 0ef47be0dc1997831d57b54351b3332ae42c6219..e86336f4be953b1acd3850344bb82f59a4887a09 100644 (file)
@@ -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);
 
 
index 7eb15bc7d5acc6f97f6e3b9c929503d19ed3b38c..d51bee885b82245109977a6ea7deef750a50688a 100644 (file)
@@ -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)
 {