Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8a22633

Browse files
committed
Change the way pg_basebackup's tablespace mapping is implemented.
Previously, we would first create the symlinks the way they are in the original system, and at the end replace them with the mapped symlinks. That never really made much sense, so now we create the symlink pointing to the correct location to begin with, so that there's no need to fix them at the end. The old coding didn't work correctly on Windows, because Windows junction points look more like directories than files, and ought to be removed with rmdir rather than unlink. Also, it incorrectly used "%d" rather than "%u" to print an Oid, but that's gone now. Report and patch by Amit Kapila, with minor changes by me. Reviewed by MauMau. Backpatch to 9.4, where the --tablespace feature was added.
1 parent d9b2bc4 commit 8a22633

File tree

1 file changed

+20
-44
lines changed

1 file changed

+20
-44
lines changed

src/bin/pg_basebackup/pg_basebackup.c

+20-44
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ static bool reached_end_position(XLogRecPtr segendpos, uint32 timeline,
109109
bool segment_finished);
110110

111111
static const char *get_tablespace_mapping(const char *dir);
112-
static void update_tablespace_symlink(Oid oid, const char *old_dir);
113112
static void tablespace_list_append(const char *arg);
114113

115114

@@ -1109,34 +1108,6 @@ get_tablespace_mapping(const char *dir)
11091108
}
11101109

11111110

1112-
/*
1113-
* Update symlinks to reflect relocated tablespace.
1114-
*/
1115-
static void
1116-
update_tablespace_symlink(Oid oid, const char *old_dir)
1117-
{
1118-
const char *new_dir = get_tablespace_mapping(old_dir);
1119-
1120-
if (strcmp(old_dir, new_dir) != 0)
1121-
{
1122-
char *linkloc = psprintf("%s/pg_tblspc/%d", basedir, oid);
1123-
1124-
if (unlink(linkloc) != 0 && errno != ENOENT)
1125-
{
1126-
fprintf(stderr, _("%s: could not remove symbolic link \"%s\": %s\n"),
1127-
progname, linkloc, strerror(errno));
1128-
disconnect_and_exit(1);
1129-
}
1130-
if (symlink(new_dir, linkloc) != 0)
1131-
{
1132-
fprintf(stderr, _("%s: could not create symbolic link \"%s\": %s\n"),
1133-
progname, linkloc, strerror(errno));
1134-
disconnect_and_exit(1);
1135-
}
1136-
}
1137-
}
1138-
1139-
11401111
/*
11411112
* Receive a tar format stream from the connection to the server, and unpack
11421113
* the contents of it into a directory. Only files, directories and
@@ -1151,16 +1122,20 @@ ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum)
11511122
{
11521123
char current_path[MAXPGPATH];
11531124
char filename[MAXPGPATH];
1125+
const char *mapped_tblspc_path;
11541126
int current_len_left;
11551127
int current_padding = 0;
1156-
bool basetablespace = PQgetisnull(res, rownum, 0);
1128+
bool basetablespace;
11571129
char *copybuf = NULL;
11581130
FILE *file = NULL;
11591131

1132+
basetablespace = PQgetisnull(res, rownum, 0);
11601133
if (basetablespace)
11611134
strlcpy(current_path, basedir, sizeof(current_path));
11621135
else
1163-
strlcpy(current_path, get_tablespace_mapping(PQgetvalue(res, rownum, 1)), sizeof(current_path));
1136+
strlcpy(current_path,
1137+
get_tablespace_mapping(PQgetvalue(res, rownum, 1)),
1138+
sizeof(current_path));
11641139

11651140
/*
11661141
* Get the COPY data
@@ -1284,13 +1259,25 @@ ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum)
12841259
{
12851260
/*
12861261
* Symbolic link
1262+
*
1263+
* It's most likely a link in pg_tblspc directory, to
1264+
* the location of a tablespace. Apply any tablespace
1265+
* mapping given on the command line (--tablespace).
1266+
* (We blindly apply the mapping without checking that
1267+
* the link really is inside pg_tblspc. We don't expect
1268+
* there to be other symlinks in a data directory, but
1269+
* if there are, you can call it an undocumented feature
1270+
* that you can map them too.)
12871271
*/
12881272
filename[strlen(filename) - 1] = '\0'; /* Remove trailing slash */
1289-
if (symlink(&copybuf[157], filename) != 0)
1273+
1274+
mapped_tblspc_path = get_tablespace_mapping(&copybuf[157]);
1275+
if (symlink(mapped_tblspc_path, filename) != 0)
12901276
{
12911277
fprintf(stderr,
12921278
_("%s: could not create symbolic link from \"%s\" to \"%s\": %s\n"),
1293-
progname, filename, &copybuf[157], strerror(errno));
1279+
progname, filename, mapped_tblspc_path,
1280+
strerror(errno));
12941281
disconnect_and_exit(1);
12951282
}
12961283
}
@@ -1793,17 +1780,6 @@ BaseBackup(void)
17931780
fprintf(stderr, "\n"); /* Need to move to next line */
17941781
}
17951782

1796-
if (format == 'p' && tablespace_dirs.head != NULL)
1797-
{
1798-
for (i = 0; i < PQntuples(res); i++)
1799-
{
1800-
Oid tblspc_oid = atooid(PQgetvalue(res, i, 0));
1801-
1802-
if (tblspc_oid)
1803-
update_tablespace_symlink(tblspc_oid, PQgetvalue(res, i, 1));
1804-
}
1805-
}
1806-
18071783
PQclear(res);
18081784

18091785
/*

0 commit comments

Comments
 (0)