diff --git a/src/backend/libpq/be-secure-openssl.c b/src/backend/libpq/be-secure-openssl.c
index 44c84a7869..5aace0eb78 100644
--- a/src/backend/libpq/be-secure-openssl.c
+++ b/src/backend/libpq/be-secure-openssl.c
@@ -688,11 +688,13 @@ be_tls_read(Port *port, void *ptr, size_t len, int *waitfor)
 			ereport(COMMERROR,
 					(errcode(ERRCODE_PROTOCOL_VIOLATION),
 					 errmsg("SSL error: %s", SSLerrmessage(ecode))));
-			/* fall through */
-		case SSL_ERROR_ZERO_RETURN:
 			errno = ECONNRESET;
 			n = -1;
 			break;
+		case SSL_ERROR_ZERO_RETURN:
+			/* connection was cleanly shut down by peer */
+			n = 0;
+			break;
 		default:
 			ereport(COMMERROR,
 					(errcode(ERRCODE_PROTOCOL_VIOLATION),
@@ -748,11 +750,13 @@ be_tls_write(Port *port, void *ptr, size_t len, int *waitfor)
 			ereport(COMMERROR,
 					(errcode(ERRCODE_PROTOCOL_VIOLATION),
 					 errmsg("SSL error: %s", SSLerrmessage(ecode))));
-			/* fall through */
-		case SSL_ERROR_ZERO_RETURN:
 			errno = ECONNRESET;
 			n = -1;
 			break;
+		case SSL_ERROR_ZERO_RETURN:
+			/* connection was cleanly shut down by peer */
+			n = 0;
+			break;
 		default:
 			ereport(COMMERROR,
 					(errcode(ERRCODE_PROTOCOL_VIOLATION),
diff --git a/src/backend/libpq/be-secure.c b/src/backend/libpq/be-secure.c
index 785dadb6c2..655810e436 100644
--- a/src/backend/libpq/be-secure.c
+++ b/src/backend/libpq/be-secure.c
@@ -235,6 +235,10 @@ secure_raw_read(Port *port, void *ptr, size_t len)
 
 /*
  *	Write data to a secure connection.
+ *
+ * be_tls_write() can return 0 to caller if connection has been reset cleanly
+ * by peer when using a SSL connection, unlike its counterpart in
+ * secure_raw_write().
  */
 ssize_t
 secure_write(Port *port, void *ptr, size_t len)
diff --git a/src/interfaces/libpq/fe-secure-openssl.c b/src/interfaces/libpq/fe-secure-openssl.c
index a7c3d7af64..08f122cbe6 100644
--- a/src/interfaces/libpq/fe-secure-openssl.c
+++ b/src/interfaces/libpq/fe-secure-openssl.c
@@ -261,12 +261,10 @@ rloop:
 			/*
 			 * Per OpenSSL documentation, this error code is only returned for
 			 * a clean connection closure, so we should not report it as a
-			 * server crash.
+			 * server crash. Similarly to the non-SSL code path, this is
+			 * equivalent to nothing received to adapt accordindly.
 			 */
-			printfPQExpBuffer(&conn->errorMessage,
-			 libpq_gettext("SSL connection has been closed unexpectedly\n"));
-			result_errno = ECONNRESET;
-			n = -1;
+			n = 0;
 			break;
 		default:
 			printfPQExpBuffer(&conn->errorMessage,
@@ -370,12 +368,10 @@ pgtls_write(PGconn *conn, const void *ptr, size_t len)
 			/*
 			 * Per OpenSSL documentation, this error code is only returned for
 			 * a clean connection closure, so we should not report it as a
-			 * server crash.
+			 * server crash. Similarly to the non-SSL code path, this is
+			 * equivalent to nothing sent to adapt accordindly.
 			 */
-			printfPQExpBuffer(&conn->errorMessage,
-			 libpq_gettext("SSL connection has been closed unexpectedly\n"));
-			result_errno = ECONNRESET;
-			n = -1;
+			n = 0;
 			break;
 		default:
 			printfPQExpBuffer(&conn->errorMessage,
