If you quit the editor without saving, the current query buffer
or the last executed SQL statement get run.

This can be annoying and disruptive, and it requires you to
empty the file and save it if you don't want to execute anything.

But when editing a script, it is a clear POLA violation:

test=> \! cat q.sql
SELECT 99;

test=> SELECT 42;
 ?column? 
----------
       42
(1 row)

test=> \e q.sql
[quit the editor without saving]
 ?column? 
----------
       42
(1 row)

This is pretty bad: you either have to re-run the previous statement
or you have to empty your script file.  Both are unappealing.

I have been annoyed about this myself, and I have heard other prople
complain about it, so I propose to clear the query buffer if the
editor exits without modifying the file.

This behavior is much more intuitive for me.

Yours,
Laurenz Albe
From 209e4a0ab51f88a82e1fc0d4e4efd24d38a7d26c Mon Sep 17 00:00:00 2001
From: Laurenz Albe <laurenz.a...@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

Reply via email to