diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
new file mode 100644
index a7789df..34a4507
*** a/src/bin/psql/common.c
--- b/src/bin/psql/common.c
*************** CheckConnection(void)
*** 386,391 ****
--- 386,393 ----
  		}
  		else
  			psql_error("Succeeded.\n");
+ 
+ 		pset.conn_was_reset = true;
  	}
  
  	return OK;
diff --git a/src/bin/psql/mainloop.c b/src/bin/psql/mainloop.c
new file mode 100644
index 37dfa4d..c049a39
*** a/src/bin/psql/mainloop.c
--- b/src/bin/psql/mainloop.c
*************** MainLoop(FILE *source)
*** 390,401 ****
  					break;
  			}
  
! 			/* fall out of loop if lexer reached EOL */
  			if (scan_result == PSCAN_INCOMPLETE ||
! 				scan_result == PSCAN_EOL)
  				break;
  		}
  
  		/* Add line to pending history if we didn't execute anything yet */
  		if (pset.cur_cmd_interactive && !line_saved_in_history)
  			pg_append_history(line, history_buf);
--- 390,404 ----
  					break;
  			}
  
! 			/* fall out of loop if lexer reached EOL or connection was reset */
  			if (scan_result == PSCAN_INCOMPLETE ||
! 				scan_result == PSCAN_EOL ||
! 				pset.conn_was_reset)
  				break;
  		}
  
+ 		pset.conn_was_reset = false;
+ 
  		/* Add line to pending history if we didn't execute anything yet */
  		if (pset.cur_cmd_interactive && !line_saved_in_history)
  			pg_append_history(line, history_buf);
diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h
new file mode 100644
index 8cfe9d2..39a4be0
*** a/src/bin/psql/settings.h
--- b/src/bin/psql/settings.h
*************** typedef struct _psqlSettings
*** 102,107 ****
--- 102,110 ----
  	FILE	   *cur_cmd_source; /* describe the status of the current main
  								 * loop */
  	bool		cur_cmd_interactive;
+ 	bool		conn_was_reset;	/* indicates that the connection was reset
+ 								 * during the last attempt to execute an
+ 								 * interactive command */
  	int			sversion;		/* backend server version */
  	const char *progname;		/* in case you renamed psql */
  	char	   *inputfile;		/* file being currently processed, if any */
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
new file mode 100644
index 7ce05fb..e238de9
*** a/src/bin/psql/startup.c
--- b/src/bin/psql/startup.c
*************** main(int argc, char *argv[])
*** 139,144 ****
--- 139,145 ----
  	pset.last_error_result = NULL;
  	pset.cur_cmd_source = stdin;
  	pset.cur_cmd_interactive = false;
+ 	pset.conn_was_reset = false;
  
  	/* We rely on unmentioned fields of pset.popt to start out 0/false/NULL */
  	pset.popt.topt.format = PRINT_ALIGNED;
