Skip to content

Commit 959f6d6

Browse files
committed
pg_upgrade: Default new bindir to pg_upgrade location
Make the directory where the pg_upgrade binary resides the default for new bindir, as running the pg_upgrade binary from where the new cluster is installed is a very common scenario. Setting this as the defauly bindir for the new cluster will remove the need to provide it explicitly via -B in many cases. To support directories being missing from option parsing, extend the directory check with a missingOk mode where the path must be filled at a later point before being used. Also move the exec_path check to earlier in setup to make sure we know the new cluster bindir when we scan for required executables. This removes the exec_path from the OSInfo struct as it is not used anywhere. Author: Daniel Gustafsson <daniel@yesql.se> Reviewed-by: Peter Eisentraut <peter.eisentraut@2ndquadrant.com> Discussion: https://www.postgresql.org/message-id/flat/9328.1552952117@sss.pgh.pa.us
1 parent 0befb4f commit 959f6d6

File tree

6 files changed

+34
-22
lines changed

6 files changed

+34
-22
lines changed

doc/src/sgml/ref/pgupgrade.sgml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
<term><option>-B</option> <replaceable>bindir</replaceable></term>
9090
<term><option>--new-bindir=</option><replaceable>bindir</replaceable></term>
9191
<listitem><para>the new PostgreSQL executable directory;
92+
default is the directory where <application>pg_upgrade</application> resides;
9293
environment variable <envar>PGBINNEW</envar></para></listitem>
9394
</varlistentry>
9495

src/bin/pg_upgrade/option.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
static void usage(void);
2424
static void check_required_directory(char **dirpath,
2525
const char *envVarName, bool useCwd,
26-
const char *cmdLineOption, const char *description);
26+
const char *cmdLineOption, const char *description,
27+
bool missingOk);
2728
#define FIX_DEFAULT_READ_ONLY "-c default_transaction_read_only=false"
2829

2930

@@ -251,15 +252,15 @@ parseCommandLine(int argc, char *argv[])
251252

252253
/* Get values from env if not already set */
253254
check_required_directory(&old_cluster.bindir, "PGBINOLD", false,
254-
"-b", _("old cluster binaries reside"));
255+
"-b", _("old cluster binaries reside"), false);
255256
check_required_directory(&new_cluster.bindir, "PGBINNEW", false,
256-
"-B", _("new cluster binaries reside"));
257+
"-B", _("new cluster binaries reside"), true);
257258
check_required_directory(&old_cluster.pgdata, "PGDATAOLD", false,
258-
"-d", _("old cluster data resides"));
259+
"-d", _("old cluster data resides"), false);
259260
check_required_directory(&new_cluster.pgdata, "PGDATANEW", false,
260-
"-D", _("new cluster data resides"));
261+
"-D", _("new cluster data resides"), false);
261262
check_required_directory(&user_opts.socketdir, "PGSOCKETDIR", true,
262-
"-s", _("sockets will be created"));
263+
"-s", _("sockets will be created"), false);
263264

264265
#ifdef WIN32
265266

@@ -293,7 +294,8 @@ usage(void)
293294
printf(_(" pg_upgrade [OPTION]...\n\n"));
294295
printf(_("Options:\n"));
295296
printf(_(" -b, --old-bindir=BINDIR old cluster executable directory\n"));
296-
printf(_(" -B, --new-bindir=BINDIR new cluster executable directory\n"));
297+
printf(_(" -B, --new-bindir=BINDIR new cluster executable directory (default\n"
298+
" same directory as pg_upgrade)"));
297299
printf(_(" -c, --check check clusters only, don't change any data\n"));
298300
printf(_(" -d, --old-datadir=DATADIR old cluster data directory\n"));
299301
printf(_(" -D, --new-datadir=DATADIR new cluster data directory\n"));
@@ -351,13 +353,15 @@ usage(void)
351353
* useCwd - true if OK to default to CWD
352354
* cmdLineOption - the command line option for this directory
353355
* description - a description of this directory option
356+
* missingOk - true if OK that both dirpath and envVarName are not existing
354357
*
355358
* We use the last two arguments to construct a meaningful error message if the
356359
* user hasn't provided the required directory name.
357360
*/
358361
static void
359362
check_required_directory(char **dirpath, const char *envVarName, bool useCwd,
360-
const char *cmdLineOption, const char *description)
363+
const char *cmdLineOption, const char *description,
364+
bool missingOk)
361365
{
362366
if (*dirpath == NULL || strlen(*dirpath) == 0)
363367
{
@@ -373,6 +377,8 @@ check_required_directory(char **dirpath, const char *envVarName, bool useCwd,
373377
pg_fatal("could not determine current directory\n");
374378
*dirpath = pg_strdup(cwd);
375379
}
380+
else if (missingOk)
381+
return;
376382
else
377383
pg_fatal("You must identify the directory where the %s.\n"
378384
"Please use the %s command-line option or the %s environment variable.\n",

src/bin/pg_upgrade/pg_upgrade.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,29 @@ main(int argc, char **argv)
204204
static void
205205
setup(char *argv0, bool *live_check)
206206
{
207-
char exec_path[MAXPGPATH]; /* full path to my executable */
208-
209207
/*
210208
* make sure the user has a clean environment, otherwise, we may confuse
211209
* libpq when we connect to one (or both) of the servers.
212210
*/
213211
check_pghost_envvar();
214212

213+
/*
214+
* In case the user hasn't specified the directory for the new binaries
215+
* with -B, default to using the path of the currently executed pg_upgrade
216+
* binary.
217+
*/
218+
if (!new_cluster.bindir)
219+
{
220+
char exec_path[MAXPGPATH];
221+
222+
if (find_my_exec(argv0, exec_path) < 0)
223+
pg_fatal("%s: could not find own program executable\n", argv0);
224+
/* Trim off program name and keep just path */
225+
*last_dir_separator(exec_path) = '\0';
226+
canonicalize_path(exec_path);
227+
new_cluster.bindir = pg_strdup(exec_path);
228+
}
229+
215230
verify_directories();
216231

217232
/* no postmasters should be running, except for a live check */
@@ -247,15 +262,6 @@ setup(char *argv0, bool *live_check)
247262
pg_fatal("There seems to be a postmaster servicing the new cluster.\n"
248263
"Please shutdown that postmaster and try again.\n");
249264
}
250-
251-
/* get path to pg_upgrade executable */
252-
if (find_my_exec(argv0, exec_path) < 0)
253-
pg_fatal("%s: could not find own program executable\n", argv0);
254-
255-
/* Trim off program name and keep just path */
256-
*last_dir_separator(exec_path) = '\0';
257-
canonicalize_path(exec_path);
258-
os_info.exec_path = pg_strdup(exec_path);
259265
}
260266

261267

src/bin/pg_upgrade/pg_upgrade.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,6 @@ typedef struct
314314
typedef struct
315315
{
316316
const char *progname; /* complete pathname for this program */
317-
char *exec_path; /* full path to my executable */
318317
char *user; /* username for clusters */
319318
bool user_specified; /* user specified on command-line */
320319
char **old_tablespaces; /* tablespaces */

src/bin/pg_upgrade/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ PGDATA="$BASE_PGDATA"
220220

221221
standard_initdb 'initdb'
222222

223-
pg_upgrade $PG_UPGRADE_OPTS -d "${PGDATA}.old" -D "$PGDATA" -b "$oldbindir" -B "$bindir" -p "$PGPORT" -P "$PGPORT"
223+
pg_upgrade $PG_UPGRADE_OPTS -d "${PGDATA}.old" -D "$PGDATA" -b "$oldbindir" -p "$PGPORT" -P "$PGPORT"
224224

225225
# make sure all directories and files have group permissions, on Unix hosts
226226
# Windows hosts don't support Unix-y permissions.

src/tools/msvc/vcregress.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ sub upgradecheck
604604
print "\nRunning pg_upgrade\n\n";
605605
@args = (
606606
'pg_upgrade', '-d', "$data.old", '-D', $data, '-b',
607-
$bindir, '-B', $bindir);
607+
$bindir);
608608
system(@args) == 0 or exit 1;
609609
print "\nStarting new cluster\n\n";
610610
@args = ('pg_ctl', '-l', "$logdir/postmaster2.log", 'start');

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy