Skip to content

Commit 26a4e0e

Browse files
committed
Fix initdb --sync-only to also sync tablespaces.
630cd14 added initdb --sync-only, for use by pg_upgrade, by just exposing the existing fsync code. That's wrong, because initdb so far had absolutely no reason to deal with tablespaces. Fix --sync-only by additionally explicitly syncing each of the tablespaces. Backpatch to 9.3 where --sync-only was introduced. Abhijit Menon-Sen and Andres Freund
1 parent 2c3ebfd commit 26a4e0e

File tree

1 file changed

+61
-2
lines changed

1 file changed

+61
-2
lines changed

src/bin/initdb/initdb.c

+61-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include <signal.h>
5757
#include <time.h>
5858

59+
#include "common/relpath.h"
5960
#include "mb/pg_wchar.h"
6061
#include "getaddrinfo.h"
6162
#include "getopt_long.h"
@@ -210,6 +211,7 @@ static char **filter_lines_with_token(char **lines, const char *token);
210211
static char **readfile(const char *path);
211212
static void writefile(char *path, char **lines);
212213
static void walkdir(char *path, void (*action) (char *fname, bool isdir));
214+
static void walktblspc_links(char *path, void (*action) (char *fname, bool isdir));
213215
static void pre_sync_fname(char *fname, bool isdir);
214216
static void fsync_fname(char *fname, bool isdir);
215217
static FILE *popen_check(const char *command, const char *mode);
@@ -585,6 +587,55 @@ walkdir(char *path, void (*action) (char *fname, bool isdir))
585587
(*action) (path, true);
586588
}
587589

590+
/*
591+
* walktblspc_links: call walkdir on each entry under the given
592+
* pg_tblspc directory, or do nothing if pg_tblspc doesn't exist.
593+
*/
594+
static void
595+
walktblspc_links(char *path, void (*action) (char *fname, bool isdir))
596+
{
597+
DIR *dir;
598+
struct dirent *direntry;
599+
char subpath[MAXPGPATH];
600+
601+
dir = opendir(path);
602+
if (dir == NULL)
603+
{
604+
if (errno == ENOENT)
605+
return;
606+
fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
607+
progname, path, strerror(errno));
608+
exit_nicely();
609+
}
610+
611+
while (errno = 0, (direntry = readdir(dir)) != NULL)
612+
{
613+
if (strcmp(direntry->d_name, ".") == 0 ||
614+
strcmp(direntry->d_name, "..") == 0)
615+
continue;
616+
617+
/* fsync the version specific tablespace subdirectory */
618+
snprintf(subpath, sizeof(subpath), "%s/%s/%s",
619+
path, direntry->d_name, TABLESPACE_VERSION_DIRECTORY);
620+
621+
walkdir(subpath, action);
622+
}
623+
624+
if (errno)
625+
{
626+
fprintf(stderr, _("%s: could not read directory \"%s\": %s\n"),
627+
progname, path, strerror(errno));
628+
exit_nicely();
629+
}
630+
631+
if (closedir(dir))
632+
{
633+
fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"),
634+
progname, path, strerror(errno));
635+
exit_nicely();
636+
}
637+
}
638+
588639
/*
589640
* Hint to the OS that it should get ready to fsync() this file.
590641
*/
@@ -2311,6 +2362,7 @@ static void
23112362
perform_fsync(void)
23122363
{
23132364
char pdir[MAXPGPATH];
2365+
char pg_tblspc[MAXPGPATH];
23142366

23152367
fputs(_("syncing data to disk ... "), stdout);
23162368
fflush(stdout);
@@ -2329,19 +2381,26 @@ perform_fsync(void)
23292381
/* first the parent of the PGDATA directory */
23302382
pre_sync_fname(pdir, true);
23312383

2332-
/* then recursively through the directory */
2384+
/* then recursively through the data directory */
23332385
walkdir(pg_data, pre_sync_fname);
23342386

2387+
/* now do the same thing for everything under pg_tblspc */
2388+
snprintf(pg_tblspc, MAXPGPATH, "%s/pg_tblspc", pg_data);
2389+
walktblspc_links(pg_tblspc, pre_sync_fname);
2390+
23352391
/*
23362392
* Now, do the fsync()s in the same order.
23372393
*/
23382394

23392395
/* first the parent of the PGDATA directory */
23402396
fsync_fname(pdir, true);
23412397

2342-
/* then recursively through the directory */
2398+
/* then recursively through the data directory */
23432399
walkdir(pg_data, fsync_fname);
23442400

2401+
/* and now the same for all tablespaces */
2402+
walktblspc_links(pg_tblspc, fsync_fname);
2403+
23452404
check_ok();
23462405
}
23472406

0 commit comments

Comments
 (0)