Skip to content

Commit 5c7e91e

Browse files
committed
Back-patch recent pg_upgrade fixes into 9.2.
This syncs contrib/pg_upgrade in the 9.2 branch with HEAD, except for the HEAD changes related to converting XLogRecPtr to 64-bit int. It includes back-patching these commits: 666d494 pg_upgrade: abstract out copying of files from old cluster to new 7afa8be pg_upgrade: Run the created scripts in the test suite ab577e6 Remove analyze_new_cluster.sh on make clean, too 34c0204 Fix thinko in comment 088c065 pg_upgrade: Fix exec_prog API to be less flaky f763b77 Fix pg_upgrade to cope with non-default unix_socket_directory scenarios.
1 parent b681a87 commit 5c7e91e

File tree

11 files changed

+268
-140
lines changed

11 files changed

+268
-140
lines changed

contrib/pg_upgrade/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ OBJS = check.o controldata.o dump.o exec.o file.o function.o info.o \
1111
PG_CPPFLAGS = -DFRONTEND -DDLSUFFIX=\"$(DLSUFFIX)\" -I$(srcdir) -I$(libpq_srcdir)
1212
PG_LIBS = $(libpq_pgport)
1313

14-
EXTRA_CLEAN = delete_old_cluster.sh log/ tmp_check/
14+
EXTRA_CLEAN = analyze_new_cluster.sh delete_old_cluster.sh log/ tmp_check/
1515

1616
ifdef USE_PGXS
1717
PG_CONFIG = pg_config

contrib/pg_upgrade/check.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,10 @@ issue_warnings(char *sequence_script_file_name)
183183
if (sequence_script_file_name)
184184
{
185185
prep_status("Adjusting sequences");
186-
exec_prog(true, true, UTILITY_LOG_FILE, NULL,
187-
SYSTEMQUOTE "\"%s/psql\" --echo-queries "
188-
"--set ON_ERROR_STOP=on "
189-
"--no-psqlrc --port %d --username \"%s\" "
190-
"-f \"%s\" --dbname template1 >> \"%s\" 2>&1" SYSTEMQUOTE,
191-
new_cluster.bindir, new_cluster.port, os_info.user,
192-
sequence_script_file_name, UTILITY_LOG_FILE);
186+
exec_prog(UTILITY_LOG_FILE, NULL, true,
187+
"\"%s/psql\" " EXEC_PSQL_ARGS " %s -f \"%s\"",
188+
new_cluster.bindir, cluster_conn_opts(&new_cluster),
189+
sequence_script_file_name);
193190
unlink(sequence_script_file_name);
194191
check_ok();
195192
}

contrib/pg_upgrade/dump.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ generate_old_dump(void)
2323
* --binary-upgrade records the width of dropped columns in pg_class, and
2424
* restores the frozenid's for databases and relations.
2525
*/
26-
exec_prog(true, true, UTILITY_LOG_FILE, NULL,
27-
SYSTEMQUOTE "\"%s/pg_dumpall\" --port %d --username \"%s\" "
28-
"--schema-only --binary-upgrade %s > \"%s\" 2>> \"%s\""
29-
SYSTEMQUOTE, new_cluster.bindir, old_cluster.port, os_info.user,
26+
exec_prog(UTILITY_LOG_FILE, NULL, true,
27+
"\"%s/pg_dumpall\" %s --schema-only --binary-upgrade %s -f %s",
28+
new_cluster.bindir, cluster_conn_opts(&old_cluster),
3029
log_opts.verbose ? "--verbose" : "",
31-
ALL_DUMP_FILE, UTILITY_LOG_FILE);
30+
ALL_DUMP_FILE);
3231
check_ok();
3332
}
3433

contrib/pg_upgrade/exec.c

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,77 +26,81 @@ static int win32_check_directory_write_permissions(void);
2626

2727
/*
2828
* exec_prog()
29+
* Execute an external program with stdout/stderr redirected, and report
30+
* errors
2931
*
30-
* Formats a command from the given argument list and executes that
31-
* command. If the command executes, exec_prog() returns 1 otherwise
32-
* exec_prog() logs an error message and returns 0. Either way, the command
33-
* line to be executed is saved to the specified log file.
32+
* Formats a command from the given argument list, logs it to the log file,
33+
* and attempts to execute that command. If the command executes
34+
* successfully, exec_prog() returns true.
3435
*
35-
* If throw_error is TRUE, this function will throw a PG_FATAL error
36-
* instead of returning should an error occur. The command it appended
37-
* to log_file; opt_log_file is used in error messages.
36+
* If the command fails, an error message is saved to the specified log_file.
37+
* If throw_error is true, this raises a PG_FATAL error and pg_upgrade
38+
* terminates; otherwise it is just reported as PG_REPORT and exec_prog()
39+
* returns false.
3840
*/
39-
int
40-
exec_prog(bool throw_error, bool is_priv, const char *log_file,
41-
const char *opt_log_file, const char *fmt,...)
41+
bool
42+
exec_prog(const char *log_file, const char *opt_log_file,
43+
bool throw_error, const char *fmt,...)
4244
{
43-
va_list args;
4445
int result;
45-
int retval;
46-
char cmd[MAXPGPATH];
46+
int written;
47+
#define MAXCMDLEN (2 * MAXPGPATH)
48+
char cmd[MAXCMDLEN];
4749
mode_t old_umask = 0;
4850
FILE *log;
51+
va_list ap;
4952

50-
if (is_priv)
51-
old_umask = umask(S_IRWXG | S_IRWXO);
53+
old_umask = umask(S_IRWXG | S_IRWXO);
5254

53-
va_start(args, fmt);
54-
vsnprintf(cmd, MAXPGPATH, fmt, args);
55-
va_end(args);
55+
written = strlcpy(cmd, SYSTEMQUOTE, strlen(SYSTEMQUOTE));
56+
va_start(ap, fmt);
57+
written += vsnprintf(cmd + written, MAXCMDLEN - written, fmt, ap);
58+
va_end(ap);
59+
if (written >= MAXCMDLEN)
60+
pg_log(PG_FATAL, "command too long\n");
61+
written += snprintf(cmd + written, MAXCMDLEN - written,
62+
" >> \"%s\" 2>&1" SYSTEMQUOTE, log_file);
63+
if (written >= MAXCMDLEN)
64+
pg_log(PG_FATAL, "command too long\n");
5665

5766
if ((log = fopen_priv(log_file, "a+")) == NULL)
5867
pg_log(PG_FATAL, "cannot write to log file %s\n", log_file);
5968
pg_log(PG_VERBOSE, "%s\n", cmd);
6069
fprintf(log, "command: %s\n", cmd);
70+
6171
/*
62-
* In Windows, we must close then reopen the log file so the file is
63-
* not open while the command is running, or we get a share violation.
72+
* In Windows, we must close the log file at this point so the file is not
73+
* open while the command is running, or we get a share violation.
6474
*/
6575
fclose(log);
6676

6777
result = system(cmd);
6878

69-
if (is_priv)
70-
umask(old_umask);
79+
umask(old_umask);
7180

7281
if (result != 0)
7382
{
74-
char opt_string[MAXPGPATH];
75-
76-
/* Create string for optional second log file */
77-
if (opt_log_file)
78-
snprintf(opt_string, sizeof(opt_string), " or \"%s\"", opt_log_file);
79-
else
80-
opt_string[0] = '\0';
81-
8283
report_status(PG_REPORT, "*failure*");
8384
fflush(stdout);
8485
pg_log(PG_VERBOSE, "There were problems executing \"%s\"\n", cmd);
85-
pg_log(throw_error ? PG_FATAL : PG_REPORT,
86-
"Consult the last few lines of \"%s\"%s for\n"
87-
"the probable cause of the failure.\n",
88-
log_file, opt_string);
89-
retval = 1;
86+
if (opt_log_file)
87+
pg_log(throw_error ? PG_FATAL : PG_REPORT,
88+
"Consult the last few lines of \"%s\" or \"%s\" for\n"
89+
"the probable cause of the failure.\n",
90+
log_file, opt_log_file);
91+
else
92+
pg_log(throw_error ? PG_FATAL : PG_REPORT,
93+
"Consult the last few lines of \"%s\" for\n"
94+
"the probable cause of the failure.\n",
95+
log_file);
9096
}
91-
else
92-
retval = 0;
9397

9498
if ((log = fopen_priv(log_file, "a+")) == NULL)
9599
pg_log(PG_FATAL, "cannot write to log file %s\n", log_file);
96100
fprintf(log, "\n\n");
97101
fclose(log);
98102

99-
return retval;
103+
return result == 0;
100104
}
101105

102106

contrib/pg_upgrade/file.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ copyAndUpdateFile(pageCnvCtx *pageConverter,
103103
/*
104104
* linkAndUpdateFile()
105105
*
106-
* Creates a symbolic link between the given relation files. We use
106+
* Creates a hard link between the given relation files. We use
107107
* this function to perform a true in-place update. If the on-disk
108108
* format of the new cluster is bit-for-bit compatible with the on-disk
109-
* format of the old cluster, we can simply symlink each relation
109+
* format of the old cluster, we can simply link each relation
110110
* instead of copying the data from the old cluster to the new cluster.
111111
*/
112112
const char *

contrib/pg_upgrade/option.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include "postgres.h"
1111

12+
#include "miscadmin.h"
13+
1214
#include "pg_upgrade.h"
1315

1416
#include <getopt_long.h>
@@ -376,3 +378,64 @@ adjust_data_dir(ClusterInfo *cluster)
376378

377379
check_ok();
378380
}
381+
382+
383+
/*
384+
* get_sock_dir
385+
*
386+
* Identify the socket directory to use for this cluster. If we're doing
387+
* a live check (old cluster only), we need to find out where the postmaster
388+
* is listening. Otherwise, we're going to put the socket into the current
389+
* directory.
390+
*/
391+
void
392+
get_sock_dir(ClusterInfo *cluster, bool live_check)
393+
{
394+
#ifdef HAVE_UNIX_SOCKETS
395+
if (!live_check)
396+
{
397+
/* Use the current directory for the socket */
398+
cluster->sockdir = pg_malloc(MAXPGPATH);
399+
if (!getcwd(cluster->sockdir, MAXPGPATH))
400+
pg_log(PG_FATAL, "cannot find current directory\n");
401+
}
402+
else
403+
{
404+
/*
405+
* If we are doing a live check, we will use the old cluster's Unix
406+
* domain socket directory so we can connect to the live server.
407+
*/
408+
409+
/* sockdir was added to postmaster.pid in PG 9.1 */
410+
if (GET_MAJOR_VERSION(cluster->major_version) >= 901)
411+
{
412+
char filename[MAXPGPATH];
413+
FILE *fp;
414+
int i;
415+
416+
snprintf(filename, sizeof(filename), "%s/postmaster.pid",
417+
cluster->pgdata);
418+
if ((fp = fopen(filename, "r")) == NULL)
419+
pg_log(PG_FATAL, "Could not get socket directory of the old server\n");
420+
421+
cluster->sockdir = pg_malloc(MAXPGPATH);
422+
for (i = 0; i < LOCK_FILE_LINE_SOCKET_DIR; i++)
423+
if (fgets(cluster->sockdir, MAXPGPATH, fp) == NULL)
424+
pg_log(PG_FATAL, "Could not get socket directory of the old server\n");
425+
426+
fclose(fp);
427+
428+
/* Remove trailing newline */
429+
if (strchr(cluster->sockdir, '\n') != NULL)
430+
*strchr(cluster->sockdir, '\n') = '\0';
431+
}
432+
else
433+
{
434+
/* Can't get live sockdir, so assume the default is OK. */
435+
cluster->sockdir = NULL;
436+
}
437+
}
438+
#else /* !HAVE_UNIX_SOCKETS */
439+
cluster->sockdir = NULL;
440+
#endif
441+
}

0 commit comments

Comments
 (0)