Skip to content

Commit 1a496a1

Browse files
committed
Fix tablespace creation WAL replay to work on Windows.
The code segment that removes the old symlink (if present) wasn't clued into the fact that on Windows, symlinks are junction points which have to be removed with rmdir(). Backpatch to 9.0, where the failing code was introduced. MauMau, reviewed by Muhammad Asif Naeem and Amit Kapila
1 parent 6d25eb3 commit 1a496a1

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

src/backend/commands/tablespace.c

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ create_tablespace_directories(const char *location, const Oid tablespaceoid)
548548
char *linkloc = palloc(OIDCHARS + OIDCHARS + 1);
549549
char *location_with_version_dir = palloc(strlen(location) + 1 +
550550
strlen(TABLESPACE_VERSION_DIRECTORY) + 1);
551+
struct stat st;
551552

552553
sprintf(linkloc, "pg_tblspc/%u", tablespaceoid);
553554
sprintf(location_with_version_dir, "%s/%s", location,
@@ -574,8 +575,6 @@ create_tablespace_directories(const char *location, const Oid tablespaceoid)
574575

575576
if (InRecovery)
576577
{
577-
struct stat st;
578-
579578
/*
580579
* Our theory for replaying a CREATE is to forcibly drop the target
581580
* subdirectory if present, and then recreate it. This may be more
@@ -609,14 +608,32 @@ create_tablespace_directories(const char *location, const Oid tablespaceoid)
609608
location_with_version_dir)));
610609
}
611610

612-
/* Remove old symlink in recovery, in case it points to the wrong place */
611+
/*
612+
* In recovery, remove old symlink, in case it points to the wrong place.
613+
*
614+
* On Windows, junction points act like directories so we must be able to
615+
* apply rmdir; in general it seems best to make this code work like the
616+
* symlink removal code in destroy_tablespace_directories, except that
617+
* failure to remove is always an ERROR.
618+
*/
613619
if (InRecovery)
614620
{
615-
if (unlink(linkloc) < 0 && errno != ENOENT)
616-
ereport(ERROR,
617-
(errcode_for_file_access(),
618-
errmsg("could not remove symbolic link \"%s\": %m",
619-
linkloc)));
621+
if (lstat(linkloc, &st) == 0 && S_ISDIR(st.st_mode))
622+
{
623+
if (rmdir(linkloc) < 0)
624+
ereport(ERROR,
625+
(errcode_for_file_access(),
626+
errmsg("could not remove directory \"%s\": %m",
627+
linkloc)));
628+
}
629+
else
630+
{
631+
if (unlink(linkloc) < 0 && errno != ENOENT)
632+
ereport(ERROR,
633+
(errcode_for_file_access(),
634+
errmsg("could not remove symbolic link \"%s\": %m",
635+
linkloc)));
636+
}
620637
}
621638

622639
/*

0 commit comments

Comments
 (0)