@@ -1259,9 +1259,13 @@ SaveSlotToPath(ReplicationSlot *slot, const char *dir, int elevel)
12591259/*
12601260 * If not an ERROR, then release the lock before returning. In case
12611261 * of an ERROR, the error recovery path automatically releases the
1262- * lock, but no harm in explicitly releasing even in that case.
1262+ * lock, but no harm in explicitly releasing even in that case. Note
1263+ * that LWLockRelease() could affect errno.
12631264 */
1265+ int save_errno = errno ;
1266+
12641267LWLockRelease (& slot -> io_in_progress_lock );
1268+ errno = save_errno ;
12651269ereport (elevel ,
12661270(errcode_for_file_access (),
12671271errmsg ("could not create file \"%s\": %m" ,
@@ -1325,7 +1329,10 @@ SaveSlotToPath(ReplicationSlot *slot, const char *dir, int elevel)
13251329
13261330if (CloseTransientFile (fd ))
13271331{
1332+ int save_errno = errno ;
1333+
13281334LWLockRelease (& slot -> io_in_progress_lock );
1335+ errno = save_errno ;
13291336ereport (elevel ,
13301337(errcode_for_file_access (),
13311338errmsg ("could not close file \"%s\": %m" ,
@@ -1336,7 +1343,10 @@ SaveSlotToPath(ReplicationSlot *slot, const char *dir, int elevel)
13361343/* rename to permanent file, fsync file and directory */
13371344if (rename (tmppath ,path )!= 0 )
13381345{
1346+ int save_errno = errno ;
1347+
13391348LWLockRelease (& slot -> io_in_progress_lock );
1349+ errno = save_errno ;
13401350ereport (elevel ,
13411351(errcode_for_file_access (),
13421352errmsg ("could not rename file \"%s\" to \"%s\": %m" ,