Skip to content

Commit caa1054

Browse files
committed
In pg_upgrade, use the new postmaster -C option to get the real data
directory, for config-only directory installs. Only works for PG 9.2+ servers.
1 parent a399675 commit caa1054

File tree

4 files changed

+70
-5
lines changed

4 files changed

+70
-5
lines changed

contrib/pg_upgrade/option.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,12 @@ parseCommandLine(int argc, char *argv[])
112112

113113
case 'd':
114114
old_cluster.pgdata = pg_strdup(optarg);
115+
old_cluster.pgconfig = pg_strdup(optarg);
115116
break;
116117

117118
case 'D':
118119
new_cluster.pgdata = pg_strdup(optarg);
120+
new_cluster.pgconfig = pg_strdup(optarg);
119121
break;
120122

121123
case 'g':
@@ -319,3 +321,61 @@ check_required_directory(char **dirpath, char *envVarName,
319321
#endif
320322
(*dirpath)[strlen(*dirpath) - 1] = 0;
321323
}
324+
325+
/*
326+
* adjust_data_dir
327+
*
328+
* If a configuration-only directory was specified, find the real data dir
329+
* by quering the running server. This has limited checking because we
330+
* can't check for a running server because we can't find postmaster.pid.
331+
*/
332+
void
333+
adjust_data_dir(ClusterInfo *cluster)
334+
{
335+
char filename[MAXPGPATH];
336+
char cmd[MAXPGPATH], cmd_output[MAX_STRING];
337+
FILE *fd, *output;
338+
339+
/* If there is no postgresql.conf, it can't be a config-only dir */
340+
snprintf(filename, sizeof(filename), "%s/postgresql.conf", cluster->pgconfig);
341+
if ((fd = fopen(filename, "r")) == NULL)
342+
return;
343+
fclose(fd);
344+
345+
/* If PG_VERSION exists, it can't be a config-only dir */
346+
snprintf(filename, sizeof(filename), "%s/PG_VERSION", cluster->pgconfig);
347+
if ((fd = fopen(filename, "r")) != NULL)
348+
{
349+
fclose(fd);
350+
return;
351+
}
352+
353+
/* Must be a configuration directory, so find the real data directory. */
354+
355+
prep_status("Finding the real data directory for the %s cluster",
356+
CLUSTER_NAME(cluster));
357+
358+
/*
359+
* We don't have a data directory yet, so we can't check the PG
360+
* version, so this might fail --- only works for PG 9.2+. If this
361+
* fails, pg_upgrade will fail anyway because the data files will not
362+
* be found.
363+
*/
364+
snprintf(cmd, sizeof(cmd), "\"%s/postmaster\" -D \"%s\" -C data_directory",
365+
cluster->bindir, cluster->pgconfig);
366+
367+
if ((output = popen(cmd, "r")) == NULL ||
368+
fgets(cmd_output, sizeof(cmd_output), output) == NULL)
369+
pg_log(PG_FATAL, "Could not get data directory using %s: %s\n",
370+
cmd, getErrorText(errno));
371+
372+
pclose(output);
373+
374+
/* Remove trailing newline */
375+
if (strchr(cmd_output, '\n') != NULL)
376+
*strchr(cmd_output, '\n') = '\0';
377+
378+
cluster->pgdata = pg_strdup(cmd_output);
379+
380+
check_ok();
381+
}

contrib/pg_upgrade/pg_upgrade.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ main(int argc, char **argv)
6868

6969
parseCommandLine(argc, argv);
7070

71+
adjust_data_dir(&old_cluster);
72+
adjust_data_dir(&new_cluster);
73+
7174
output_check_banner(&live_check);
7275

7376
setup(argv[0], live_check);

contrib/pg_upgrade/pg_upgrade.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ typedef struct
187187
ControlData controldata; /* pg_control information */
188188
DbInfoArr dbarr; /* dbinfos array */
189189
char *pgdata; /* pathname for cluster's $PGDATA directory */
190+
char *pgconfig; /* pathname for cluster's config file directory */
190191
char *bindir; /* pathname for cluster's executable directory */
191192
unsigned short port; /* port number where postmaster is waiting */
192193
uint32 major_version; /* PG_VERSION of cluster */
@@ -361,6 +362,7 @@ void print_maps(FileNameMap *maps, int n,
361362
/* option.c */
362363

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

365367
/* relfilenode.c */
366368

contrib/pg_upgrade/server.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ start_postmaster(ClusterInfo *cluster)
169169
snprintf(cmd, sizeof(cmd),
170170
SYSTEMQUOTE "\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" "
171171
"-o \"-p %d %s\" start >> \"%s\" 2>&1" SYSTEMQUOTE,
172-
cluster->bindir, log_opts.filename2, cluster->pgdata, cluster->port,
172+
cluster->bindir, log_opts.filename2, cluster->pgconfig, cluster->port,
173173
(cluster->controldata.cat_ver >=
174174
BINARY_UPGRADE_SERVER_FLAG_CAT_VER) ? "-b" :
175175
"-c autovacuum=off -c autovacuum_freeze_max_age=2000000000",
@@ -208,25 +208,25 @@ stop_postmaster(bool fast)
208208
{
209209
char cmd[MAXPGPATH];
210210
const char *bindir;
211-
const char *datadir;
211+
const char *configdir;
212212

213213
if (os_info.running_cluster == &old_cluster)
214214
{
215215
bindir = old_cluster.bindir;
216-
datadir = old_cluster.pgdata;
216+
configdir = old_cluster.pgconfig;
217217
}
218218
else if (os_info.running_cluster == &new_cluster)
219219
{
220220
bindir = new_cluster.bindir;
221-
datadir = new_cluster.pgdata;
221+
configdir = new_cluster.pgconfig;
222222
}
223223
else
224224
return; /* no cluster running */
225225

226226
snprintf(cmd, sizeof(cmd),
227227
SYSTEMQUOTE "\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" %s stop >> "
228228
"\"%s\" 2>&1" SYSTEMQUOTE,
229-
bindir, log_opts.filename2, datadir, fast ? "-m fast" : "",
229+
bindir, log_opts.filename2, configdir, fast ? "-m fast" : "",
230230
log_opts.filename2);
231231

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

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