From f08f8d860010379dcd3091220960d83adc27cc91 Mon Sep 17 00:00:00 2001
From: Jelte Fennema <jelte.fennema@microsoft.com>
Date: Thu, 30 Sep 2021 11:46:21 +0200
Subject: [PATCH] Consider ETIMEDOUT a connection failure

Previously successfully opened TCP connections can still fail on reads
with ETIMEDOUT. This should be considered a connection failure, so that
the connection in libpq is marked as CONNECTION_BAD. The reason I got an
ETIMEDOUT was, because I had set a low tcp_user_timeout in the
connection string. However, it can probably also happen due to
keepalive limits being reached.
---
 src/include/port.h            | 3 ++-
 src/include/port/win32_port.h | 2 ++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/src/include/port.h b/src/include/port.h
index 82f63de325..2ff529fa59 100644
--- a/src/include/port.h
+++ b/src/include/port.h
@@ -119,7 +119,8 @@ extern void pgfnames_cleanup(char **filenames);
 	case EHOSTUNREACH: \
 	case ENETDOWN: \
 	case ENETRESET: \
-	case ENETUNREACH
+	case ENETUNREACH: \
+	case ETIMEDOUT
 
 /* Portable locale initialization (in exec.c) */
 extern void set_pglocale_pgservice(const char *argv0, const char *app);
diff --git a/src/include/port/win32_port.h b/src/include/port/win32_port.h
index 05c5a53442..093a009aee 100644
--- a/src/include/port/win32_port.h
+++ b/src/include/port/win32_port.h
@@ -381,6 +381,8 @@ extern int	_pgstat64(const char *name, struct stat *buf);
 #define ENETUNREACH WSAENETUNREACH
 #undef ENOTCONN
 #define ENOTCONN WSAENOTCONN
+#undef ETIMEDOUT
+#define ETIMEDOUT WSAETIMEDOUT
 
 /*
  * Locale stuff.
-- 
2.17.1

