diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index bcd8eb3538..c5f7331ecb 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -1239,6 +1239,13 @@ exec_command_ef_ev(PsqlScanState scan_state, bool active_branch,
 				status = PSQL_CMD_NEWEDIT;
 		}
 
+		/*
+		 * On error while doing object lookup or while editing, reset the
+		 * query buffer.
+		 */
+		if (status == PSQL_CMD_ERROR)
+			resetPQExpBuffer(query_buf);
+
 		free(obj_desc);
 	}
 	else
