diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index de9cd976ea960330f63bfef0225ee5ebfac44f72..63639e6d9a801e2ef282e55c35887bd5293a2c52 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2006, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.164 2006/03/05 15:58:51 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.165 2006/03/21 13:38:11 momjian Exp $
  */
 #include "postgres_fe.h"
 #include "command.h"
@@ -753,7 +753,7 @@ exec_command(const char *cmd,
 
 		expand_tilde(&fname);
 		/* This scrolls off the screen when using /dev/tty */
-		success = saveHistory(fname ? fname : DEVTTY);
+		success = saveHistory(fname ? fname : DEVTTY, false);
 		if (success && !quiet && fname)
 			printf(gettext("Wrote history to file \"%s/%s\".\n"),
 				   pset.dirname ? pset.dirname : ".", fname);
diff --git a/src/bin/psql/input.c b/src/bin/psql/input.c
index fdf0bbecdd6541214f97945a8c0825c9419b966c..1dc36d9dbc514d1d262e8a396c0d1b1f8665263e 100644
--- a/src/bin/psql/input.c
+++ b/src/bin/psql/input.c
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2006, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/input.c,v 1.52 2006/03/06 04:45:21 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/input.c,v 1.53 2006/03/21 13:38:12 momjian Exp $
  */
 #include "postgres_fe.h"
 
@@ -148,6 +148,10 @@ pg_write_history(char *s)
 	{
 		enum histcontrol HC;
 		
+		/* Flushing of empty buffer should do nothing */
+		if  (*s == 0)
+			return;
+		
 		prev_hist = NULL;
 			
 		HC = GetHistControlConfig();
@@ -295,13 +299,20 @@ initializeInput(int flags)
 }
 
 
+/* This function is designed for saving the readline history when user 
+ * run \s command or when psql finishes. 
+ * We have an argument named encodeFlag to handle those cases differently
+ * In that case of call via \s we don't really need to encode \n as \x01,
+ * but when we save history for Readline we must do that conversion
+ */
 bool
-saveHistory(char *fname)
+saveHistory(char *fname, bool encodeFlag)
 {
 #ifdef USE_READLINE
 	if (useHistory && fname)
 	{
-		encode_history();		
+		if (encodeFlag)
+			encode_history();
 		if (write_history(fname) == 0)
 			return true;
 
@@ -331,7 +342,7 @@ finishInput(int exitstatus, void *arg)
 		if (hist_size >= 0)
 			stifle_history(hist_size);
 
-		saveHistory(psql_history);
+		saveHistory(psql_history, true);
 		free(psql_history);
 		psql_history = NULL;
 	}
diff --git a/src/bin/psql/input.h b/src/bin/psql/input.h
index 3b85d416705a2c5e06e6040f3fc63ad18583a339..4fbc86079b4faf2526f79c5ff97d955c1e8662f6 100644
--- a/src/bin/psql/input.h
+++ b/src/bin/psql/input.h
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2006, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/input.h,v 1.26 2006/03/06 04:45:21 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/input.h,v 1.27 2006/03/21 13:38:12 momjian Exp $
  */
 #ifndef INPUT_H
 #define INPUT_H
@@ -37,7 +37,7 @@ char	   *gets_interactive(const char *prompt);
 char	   *gets_fromFile(FILE *source);
 
 void		initializeInput(int flags);
-bool		saveHistory(char *fname);
+bool		saveHistory(char *fname, bool encodeFlag);
 
 void pg_append_history(char *s, PQExpBuffer history_buf);
 void pg_clear_history(PQExpBuffer history_buf);
diff --git a/src/bin/psql/mainloop.c b/src/bin/psql/mainloop.c
index 2992fdc3f15f1d1989088f482ee9a421b7e1917c..edaa372c60d09291321326cae41aadb5426ac975 100644
--- a/src/bin/psql/mainloop.c
+++ b/src/bin/psql/mainloop.c
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2006, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/mainloop.c,v 1.73 2006/03/06 15:09:04 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/mainloop.c,v 1.74 2006/03/21 13:38:12 momjian Exp $
  */
 #include "postgres_fe.h"
 #include "mainloop.h"
@@ -112,7 +112,7 @@ MainLoop(FILE *source)
 			slashCmdStatus = PSQL_CMD_UNKNOWN;
 			prompt_status = PROMPT_READY;
 			if (pset.cur_cmd_interactive)
-				pg_clear_history(history_buf);			
+				pg_write_history(history_buf->data);
 
 			if (pset.cur_cmd_interactive)
 				putc('\n', stdout);
@@ -321,7 +321,8 @@ MainLoop(FILE *source)
 				break;
 		}
 
-		if (pset.cur_cmd_interactive && prompt_status != PROMPT_CONTINUE)
+		if ((pset.cur_cmd_interactive && prompt_status == PROMPT_READY) ||
+			(GetVariableBool(pset.vars, "SINGLELINE") && prompt_status == PROMPT_CONTINUE))
 		{
 			/*
 			 *	Pass all the contents of history_buf to readline