From 209e4a0ab51f88a82e1fc0d4e4efd24d38a7d26c Mon Sep 17 00:00:00 2001
From: Laurenz Albe <laurenz.albe@cybertec.at>
Date: Tue, 1 Dec 2020 04:28:50 +0100
Subject: [PATCH] Discard query buffer if editor is quit in \e

Before, the current query buffer or the previous query was executed
when you quit the editor without saving.

This was frequently annoying, but downright confusing if \e was used
to edit a script file, because it would then execute the previous
command rather than the script file.
---
 doc/src/sgml/ref/psql-ref.sgml | 4 +++-
 src/bin/psql/command.c         | 2 ++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index 221a967bfe..002ed38fe7 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -1949,7 +1949,9 @@ testdb=&gt;
         </para>
 
         <para>
-        The new contents of the query buffer are then re-parsed according to
+        If the editor is quit without modifying the file, the query buffer
+        is cleared.
+        Otherwise, the new contents of the query buffer are re-parsed according to
         the normal rules of <application>psql</application>, treating the
         whole buffer as a single line.  Any complete queries are immediately
         executed; that is, if the query buffer contains or ends with a
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index c7a83d5dfc..ffc5d209bc 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -3838,6 +3838,8 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf,
 			fclose(stream);
 		}
 	}
+	else
+		resetPQExpBuffer(query_buf);
 
 	/* remove temp file */
 	if (!filename_arg)
-- 
2.26.2

