diff --git a/src/port/path.c b/src/port/path.c
index 438b52960423bfec077e78e7d80246fffa4899ac..3484f2cb6155fc7865adc4c3e4d807a661793ddc 100644
--- a/src/port/path.c
+++ b/src/port/path.c
@@ -608,7 +608,10 @@ make_absolute_path(const char *path)
 			}
 			else
 			{
+				int			save_errno = errno;
+
 				free(buf);
+				errno = save_errno;
 #ifndef FRONTEND
 				elog(ERROR, "could not get current working directory: %m");
 #else