Commit abe075df authored by Tom Lane's avatar Tom Lane

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
parent b203c57b
...@@ -559,6 +559,7 @@ create_tablespace_directories(const char *location, const Oid tablespaceoid) ...@@ -559,6 +559,7 @@ create_tablespace_directories(const char *location, const Oid tablespaceoid)
{ {
char *linkloc; char *linkloc;
char *location_with_version_dir; char *location_with_version_dir;
struct stat st;
linkloc = psprintf("pg_tblspc/%u", tablespaceoid); linkloc = psprintf("pg_tblspc/%u", tablespaceoid);
location_with_version_dir = psprintf("%s/%s", location, location_with_version_dir = psprintf("%s/%s", location,
...@@ -585,8 +586,6 @@ create_tablespace_directories(const char *location, const Oid tablespaceoid) ...@@ -585,8 +586,6 @@ create_tablespace_directories(const char *location, const Oid tablespaceoid)
if (InRecovery) if (InRecovery)
{ {
struct stat st;
/* /*
* Our theory for replaying a CREATE is to forcibly drop the target * Our theory for replaying a CREATE is to forcibly drop the target
* subdirectory if present, and then recreate it. This may be more * subdirectory if present, and then recreate it. This may be more
...@@ -620,14 +619,32 @@ create_tablespace_directories(const char *location, const Oid tablespaceoid) ...@@ -620,14 +619,32 @@ create_tablespace_directories(const char *location, const Oid tablespaceoid)
location_with_version_dir))); location_with_version_dir)));
} }
/* Remove old symlink in recovery, in case it points to the wrong place */ /*
* In recovery, remove old symlink, in case it points to the wrong place.
*
* On Windows, junction points act like directories so we must be able to
* apply rmdir; in general it seems best to make this code work like the
* symlink removal code in destroy_tablespace_directories, except that
* failure to remove is always an ERROR.
*/
if (InRecovery) if (InRecovery)
{ {
if (unlink(linkloc) < 0 && errno != ENOENT) if (lstat(linkloc, &st) == 0 && S_ISDIR(st.st_mode))
ereport(ERROR, {
(errcode_for_file_access(), if (rmdir(linkloc) < 0)
errmsg("could not remove symbolic link \"%s\": %m", ereport(ERROR,
linkloc))); (errcode_for_file_access(),
errmsg("could not remove directory \"%s\": %m",
linkloc)));
}
else
{
if (unlink(linkloc) < 0 && errno != ENOENT)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not remove symbolic link \"%s\": %m",
linkloc)));
}
} }
/* /*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment