diff --git a/src/backend/libpq/auth.c b/src/backend/libpq/auth.c
index 3a041d9..304df03 100644
--- a/src/backend/libpq/auth.c
+++ b/src/backend/libpq/auth.c
@@ -1816,6 +1816,7 @@ auth_peer(hbaPort *port)
  * PAM conversation function
  */
 
+static int pam_auth_eof = false;
 static int
 pam_passwd_conv_proc(int num_msg, const struct pam_message ** msg,
 					 struct pam_response ** resp, void *appdata_ptr)
@@ -1824,6 +1825,8 @@ pam_passwd_conv_proc(int num_msg, const struct pam_message ** msg,
 	struct pam_response *reply;
 	int			i;
 
+	pam_auth_eof = false;
+
 	if (appdata_ptr)
 		passwd = (char *) appdata_ptr;
 	else
@@ -1872,6 +1875,7 @@ pam_passwd_conv_proc(int num_msg, const struct pam_message ** msg,
 						 * Client didn't want to send password.  We
 						 * intentionally do not log anything about this.
 						 */
+						pam_auth_eof = true;
 						goto fail;
 					}
 					if (strlen(passwd) == 0)
@@ -1986,6 +1990,16 @@ CheckPAMAuth(Port *port, char *user, char *password)
 
 	retval = pam_authenticate(pamh, 0);
 
+	/*
+	 * pam_auth_eof is true when the client disconnected for password request
+	 * in pam_passw_conv().
+	 */
+	if (pam_auth_eof)
+	{
+		pam_passwd = NULL;		/* Unset pam_passwd */
+		return STATUS_EOF;
+	}
+
 	if (retval != PAM_SUCCESS)
 	{
 		ereport(LOG,
