Fix Invalid os.symlink Calls when Moving Data Dir (#1781)

Fixes #1780

Specifically fixes the calls to `os.symlink` within the `move_data_directory` function, as used to update the symlink(s) for any WAL or tablespace directories following an init failure.  The new name for a WAL and/or tablespace directory is now passed in as in as the first argument when calling `os.symlink`, while the second argument is now the name of the symlink that is being recreated.

This aligns with the Python documentation for `os.symlink`, which states the following regarding the first two arguments for the `os.symlink` function (arguments `src` and `dst`): 

> Create a symbolic link pointing to src named dst (https://docs.python.org/3/library/os.html#os.symlink)

With this change, all WAL and/or tablespaces directories will be properly renamed following a init failure, and the `FileExistsError` that would otherwise occur when attempting to recreate the associated symlinks is no longer thrown, e.g.:

```bash
2020-12-05 20:35:42.219 GMT [262] LOG:  unrecognized configuration parameter "invalid" in file "/pgdata/mycluster1/postgresql.auto.conf" line 5
2020-12-05 20:35:42.219 GMT [262] FATAL:  configuration file "/pgdata/mycluster1/postgresql.auto.conf" contains errors
2020-12-05 20:35:43,220 ERROR: postmaster is not running
2020-12-05 20:35:43,223 INFO: removing initialize key after failed attempt to bootstrap the cluster
2020-12-05 20:35:43,292 INFO: renaming user defined tablespace directory and updating symlink: /tablespaces/ts1/ts1
2020-12-05 20:35:43,314 INFO: renaming data directory to /pgdata/mycluster1_2020-12-05-20-35-43
Traceback (most recent call last):
```

Additionally, any/all WAL and/or tablespace symlinks are properly recreated, e.g.:

```bash
$ ls -l pg_tblspc
total 0
lrwxrwxrwx. 1 postgres postgres 40 Dec  5 20:35 16414 -> /tablespaces/ts1/ts1_2020-12-05-20-35-43
```
This commit is contained in:
andrewlecuyer
2020-12-14 15:50:14 +01:00
committed by GitHub
parent a43baece68
commit 37bff48915
+2 -2
View File
@@ -915,7 +915,7 @@ class Postgresql(object):
new_name = '{0}_{1}'.format(pg_wal_realpath, postfix)
os.rename(pg_wal_realpath, new_name)
os.unlink(source)
os.symlink(source, new_name)
os.symlink(new_name, source)
# Move user defined tablespace directory
for (source, pg_tsp_rpath) in self.pg_tblspc_realpaths().items():
@@ -923,7 +923,7 @@ class Postgresql(object):
new_name = '{0}_{1}'.format(pg_tsp_rpath, postfix)
os.rename(pg_tsp_rpath, new_name)
os.unlink(source)
os.symlink(source, new_name)
os.symlink(new_name, source)
new_name = '{0}_{1}'.format(self._data_dir, postfix)
logger.info('renaming data directory to %s', new_name)