psql - factor out echo code

Started by Fabien COELHOover 4 years ago22 messages
#1Fabien COELHO
coelho@cri.ensmp.fr
1 attachment(s)

While working on something in "psql/common.c" I noticed some triplicated
code, including a long translatable string. This minor patch refactors
this in one function.

--
Fabien.

Attachments:

psql-echo-1.patchtext/x-diff; name=psql-echo-1.patchDownload
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index 7a95465111..4fd80ec6bb 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -523,6 +523,15 @@ PrintTiming(double elapsed_msec)
 		   elapsed_msec, days, (int) hours, (int) minutes, seconds);
 }
 
+static void
+echoQuery(FILE *out, const char *query)
+{
+	fprintf(out,
+			_("********* QUERY **********\n"
+			  "%s\n"
+			  "**************************\n\n"), query);
+	fflush(out);
+}
 
 /*
  * PSQLexec
@@ -549,18 +558,9 @@ PSQLexec(const char *query)
 
 	if (pset.echo_hidden != PSQL_ECHO_HIDDEN_OFF)
 	{
-		printf(_("********* QUERY **********\n"
-				 "%s\n"
-				 "**************************\n\n"), query);
-		fflush(stdout);
+		echoQuery(stdout, query);
 		if (pset.logfile)
-		{
-			fprintf(pset.logfile,
-					_("********* QUERY **********\n"
-					  "%s\n"
-					  "**************************\n\n"), query);
-			fflush(pset.logfile);
-		}
+			echoQuery(pset.logfile, query);
 
 		if (pset.echo_hidden == PSQL_ECHO_HIDDEN_NOEXEC)
 			return NULL;
@@ -1226,13 +1226,7 @@ SendQuery(const char *query)
 	}
 
 	if (pset.logfile)
-	{
-		fprintf(pset.logfile,
-				_("********* QUERY **********\n"
-				  "%s\n"
-				  "**************************\n\n"), query);
-		fflush(pset.logfile);
-	}
+		echoQuery(pset.logfile, query);
 
 	SetCancelConn(pset.db);
 
#2Noname
Shinya11.Kato@nttdata.com
In reply to: Fabien COELHO (#1)
RE: psql - factor out echo code

-----Original Message-----
From: Fabien COELHO <coelho@cri.ensmp.fr>
Sent: Sunday, May 30, 2021 6:10 PM
To: PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>
Subject: psql - factor out echo code

While working on something in "psql/common.c" I noticed some triplicated code,
including a long translatable string. This minor patch refactors this in one
function.

--
Fabien.

Wouldn't it be better to comment it like any other function?

Best regards,
Shinya Kato

#3Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Noname (#2)
1 attachment(s)
RE: psql - factor out echo code

Wouldn't it be better to comment it like any other function?

Sure. Attached.

--
Fabien.

Attachments:

psql-echo-2.patchtext/x-diff; name=psql-echo-2.patchDownload
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index 9a00499510..00e5bf290b 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -523,6 +523,18 @@ PrintTiming(double elapsed_msec)
 		   elapsed_msec, days, (int) hours, (int) minutes, seconds);
 }
 
+/*
+ * Echo user query
+ */
+static void
+echoQuery(FILE *out, const char *query)
+{
+	fprintf(out,
+			_("********* QUERY **********\n"
+			  "%s\n"
+			  "**************************\n\n"), query);
+	fflush(out);
+}
 
 /*
  * PSQLexec
@@ -549,18 +561,9 @@ PSQLexec(const char *query)
 
 	if (pset.echo_hidden != PSQL_ECHO_HIDDEN_OFF)
 	{
-		printf(_("********* QUERY **********\n"
-				 "%s\n"
-				 "**************************\n\n"), query);
-		fflush(stdout);
+		echoQuery(stdout, query);
 		if (pset.logfile)
-		{
-			fprintf(pset.logfile,
-					_("********* QUERY **********\n"
-					  "%s\n"
-					  "**************************\n\n"), query);
-			fflush(pset.logfile);
-		}
+			echoQuery(pset.logfile, query);
 
 		if (pset.echo_hidden == PSQL_ECHO_HIDDEN_NOEXEC)
 			return NULL;
@@ -1226,13 +1229,7 @@ SendQuery(const char *query)
 	}
 
 	if (pset.logfile)
-	{
-		fprintf(pset.logfile,
-				_("********* QUERY **********\n"
-				  "%s\n"
-				  "**************************\n\n"), query);
-		fflush(pset.logfile);
-	}
+		echoQuery(pset.logfile, query);
 
 	SetCancelConn(pset.db);
 
#4Noname
Shinya11.Kato@nttdata.com
In reply to: Fabien COELHO (#3)
RE: psql - factor out echo code

Wouldn't it be better to comment it like any other function?

Sure. Attached.

Thank you for your revision.
I think this patch is good, so I will move it to ready for committer.

Best regards,
Shinya Kato

#5Tom Lane
tgl@sss.pgh.pa.us
In reply to: Fabien COELHO (#3)
Re: psql - factor out echo code

Fabien COELHO <coelho@cri.ensmp.fr> writes:

[ psql-echo-2.patch ]

I went to commit this, figuring that it was a trivial bit of code
consolidation, but as I looked around in common.c I got rather
unhappy with the inconsistent behavior of things. Examining
the various places that implement "echo"-related logic, we have
the three places this patch proposes to unify, which log queries
using

fprintf(out,
_("********* QUERY **********\n"
"%s\n"
"**************************\n\n"), query);

and then we have two more that just do

puts(query);

plus this:

if (!OK && pset.echo == PSQL_ECHO_ERRORS)
pg_log_info("STATEMENT: %s", query);

So it's exactly fifty-fifty as to whether we add all that decoration
or none at all. I think if we're going to touch this logic, we
ought to try to unify the behavior. My vote would be to drop the
decoration everywhere, but perhaps there are votes not to?

A different angle is that the identical decoration is used for both
psql-generated queries that are logged because of ECHO_HIDDEN, and
user-entered queries. This seems at best rather unhelpful. If
we keep the decoration, should we make it different for those two
cases? (Maybe "INTERNAL QUERY" vs "QUERY", for example.) The
cases with no decoration likewise fall into multiple categories,
both user-entered and generated-by-gexec; if we were going with
a decorated approach I'd think it useful to make a distinction
between those, too.

Thoughts?

regards, tom lane

#6Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Tom Lane (#5)
Re: psql - factor out echo code

Hello Tom,

I went to commit this, figuring that it was a trivial bit of code
consolidation, but as I looked around in common.c I got rather
unhappy with the inconsistent behavior of things. Examining
the various places that implement "echo"-related logic, we have
the three places this patch proposes to unify, which log queries
using

fprintf(out,
_("********* QUERY **********\n"
"%s\n"
"**************************\n\n"), query);

and then we have two more that just do

puts(query);

plus this:

if (!OK && pset.echo == PSQL_ECHO_ERRORS)
pg_log_info("STATEMENT: %s", query);

So it's exactly fifty-fifty as to whether we add all that decoration
or none at all. I think if we're going to touch this logic, we
ought to try to unify the behavior.

+1.

I did not go this way because I wanted it to be a simple restructuring
patch so that it could go through without much ado, but I agree with
improving the current status. I'm not sure we want too much ascii-art.

My vote would be to drop the decoration everywhere, but perhaps there
are votes not to?

No, I'd be ok with removing the decoration, or at least simplify them, or
as you suggest below make the have a useful semantics.

A different angle is that the identical decoration is used for both
psql-generated queries that are logged because of ECHO_HIDDEN, and
user-entered queries. This seems at best rather unhelpful.

Indeed.

If we keep the decoration, should we make it different for those two
cases? (Maybe "INTERNAL QUERY" vs "QUERY", for example.) The cases
with no decoration likewise fall into multiple categories, both
user-entered and generated-by-gexec; if we were going with a decorated
approach I'd think it useful to make a distinction between those, too.

Thoughts?

Yes. Maybe decorations should be SQL comments, and the purpose/origin of
the query could be made clear as you suggest, eg something like markdown
in a comment:

"-- # <whatever> QUERY\n%s\n\n"

with <whatever> in USER DESCRIPTION COMPLETION GEXEC…

--
Fabien.

#7Tom Lane
tgl@sss.pgh.pa.us
In reply to: Fabien COELHO (#6)
Re: psql - factor out echo code

Fabien COELHO <coelho@cri.ensmp.fr> writes:

Yes. Maybe decorations should be SQL comments, and the purpose/origin of
the query could be made clear as you suggest, eg something like markdown
in a comment:
"-- # <whatever> QUERY\n%s\n\n"

If we keep the decoration, I'd agree with dropping all the asterisks.
I'd vote for something pretty minimalistic, like

-- INTERNAL QUERY:

regards, tom lane

#8Alvaro Herrera
alvherre@alvh.no-ip.org
In reply to: Tom Lane (#7)
Re: psql - factor out echo code

On 2021-Jul-02, Tom Lane wrote:

Fabien COELHO <coelho@cri.ensmp.fr> writes:

Yes. Maybe decorations should be SQL comments, and the purpose/origin of
the query could be made clear as you suggest, eg something like markdown
in a comment:
"-- # <whatever> QUERY\n%s\n\n"

If we keep the decoration, I'd agree with dropping all the asterisks.
I'd vote for something pretty minimalistic, like

-- INTERNAL QUERY:

I think the most interesting case for decoration is the "step by step"
mode, where you want the "title" that precedes each query be easily
visible. I think two uppercase words are not sufficient for that ...
and Markdown format which would force you to convert to HTML before you
can notice where it is, are not sufficient either. The line with a few
asterisks seems fine to me for that. Removing the asterisks in the
other case seems fine. I admit I don't use the step-by-step mode all
that much, though.

Also: one place that prints queries that wasn't mentioned before is
exec_command_print() in command.c.

--
Álvaro Herrera Valdivia, Chile — https://www.EnterpriseDB.com/
"Ed is the standard text editor."
http://groups.google.com/group/alt.religion.emacs/msg/8d94ddab6a9b0ad3

#9Tom Lane
tgl@sss.pgh.pa.us
In reply to: Alvaro Herrera (#8)
Re: psql - factor out echo code

Alvaro Herrera <alvherre@alvh.no-ip.org> writes:

I think the most interesting case for decoration is the "step by step"
mode, where you want the "title" that precedes each query be easily
visible.

I'm okay with leaving the step-by-step prompt as-is, personally.
It's the inconsistency of the other ones that bugs me.

Also: one place that prints queries that wasn't mentioned before is
exec_command_print() in command.c.

Ah, I was wondering if anyplace outside common.c did so. But that
one seems to me to be a different animal -- it's not logging
queries-about-to-be-executed.

regards, tom lane

#10Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Fabien COELHO (#6)
1 attachment(s)
Re: psql - factor out echo code

"-- # <whatever> QUERY\n%s\n\n"

Attached an attempt along those lines. I found another duplicate of the
ascii-art printing in another function.

Completion queries seems to be out of the echo/echo hidden feature.

Incredible, there is a (small) impact on regression tests for the \gexec
case. All other changes have no impact, because they are not tested:-(

--
Fabien.

Attachments:

psql-echo-3.patchtext/x-diff; name=psql-echo-3.patchDownload
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 543401c6d6..8ec00881db 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -2061,7 +2061,7 @@ exec_command_password(PsqlScanState scan_state, bool active_branch)
 				printfPQExpBuffer(&buf, "ALTER USER %s PASSWORD ",
 								  fmtId(user));
 				appendStringLiteralConn(&buf, encrypted_password, pset.db);
-				res = PSQLexec(buf.data);
+				res = PSQLexec("PASSWORD", buf.data);
 				termPQExpBuffer(&buf);
 				if (!res)
 					success = false;
@@ -4993,22 +4993,13 @@ do_watch(PQExpBuffer query_buf, double sleep)
  * returns true unless we have ECHO_HIDDEN_NOEXEC.
  */
 static bool
-echo_hidden_command(const char *query)
+echo_hidden_command(const char *origin, const char *query)
 {
 	if (pset.echo_hidden != PSQL_ECHO_HIDDEN_OFF)
 	{
-		printf(_("********* QUERY **********\n"
-				 "%s\n"
-				 "**************************\n\n"), query);
-		fflush(stdout);
+		echoQuery(stdout, origin, query);
 		if (pset.logfile)
-		{
-			fprintf(pset.logfile,
-					_("********* QUERY **********\n"
-					  "%s\n"
-					  "**************************\n\n"), query);
-			fflush(pset.logfile);
-		}
+			echoQuery(pset.logfile, origin, query);
 
 		if (pset.echo_hidden == PSQL_ECHO_HIDDEN_NOEXEC)
 			return false;
@@ -5060,7 +5051,7 @@ lookup_object_oid(EditableObjectType obj_type, const char *desc,
 			break;
 	}
 
-	if (!echo_hidden_command(query->data))
+	if (!echo_hidden_command("INTERNAL", query->data))
 	{
 		destroyPQExpBuffer(query);
 		return false;
@@ -5155,7 +5146,7 @@ get_create_object_cmd(EditableObjectType obj_type, Oid oid,
 			break;
 	}
 
-	if (!echo_hidden_command(query->data))
+	if (!echo_hidden_command("INTERNAL", query->data))
 	{
 		destroyPQExpBuffer(query);
 		return false;
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index 9a00499510..69d4e33093 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -523,6 +523,17 @@ PrintTiming(double elapsed_msec)
 		   elapsed_msec, days, (int) hours, (int) minutes, seconds);
 }
 
+/*
+ * Echo user query
+ */
+void
+echoQuery(FILE *out, const char *origin, const char *query)
+{
+	fprintf(out,
+			/* FIXME should this really be translatable? */
+			_("-- # %s QUERY\n%s\n\n"), origin, query);
+	fflush(out);
+}
 
 /*
  * PSQLexec
@@ -537,7 +548,7 @@ PrintTiming(double elapsed_msec)
  * caller uses this path to issue "SET CLIENT_ENCODING".
  */
 PGresult *
-PSQLexec(const char *query)
+PSQLexec(const char *origin, const char *query)
 {
 	PGresult   *res;
 
@@ -549,18 +560,9 @@ PSQLexec(const char *query)
 
 	if (pset.echo_hidden != PSQL_ECHO_HIDDEN_OFF)
 	{
-		printf(_("********* QUERY **********\n"
-				 "%s\n"
-				 "**************************\n\n"), query);
-		fflush(stdout);
+		echoQuery(stdout, origin, query);
 		if (pset.logfile)
-		{
-			fprintf(pset.logfile,
-					_("********* QUERY **********\n"
-					  "%s\n"
-					  "**************************\n\n"), query);
-			fflush(pset.logfile);
-		}
+			echoQuery(pset.logfile, origin, query);
 
 		if (pset.echo_hidden == PSQL_ECHO_HIDDEN_NOEXEC)
 			return NULL;
@@ -856,10 +858,7 @@ ExecQueryTuples(const PGresult *result)
 				 * assumes that MainLoop did that, so we have to do it here.
 				 */
 				if (pset.echo == PSQL_ECHO_ALL && !pset.singlestep)
-				{
-					puts(query);
-					fflush(stdout);
-				}
+					echoQuery(stdout, "GEXEC", query);
 
 				if (!SendQuery(query))
 				{
@@ -1226,13 +1225,7 @@ SendQuery(const char *query)
 	}
 
 	if (pset.logfile)
-	{
-		fprintf(pset.logfile,
-				_("********* QUERY **********\n"
-				  "%s\n"
-				  "**************************\n\n"), query);
-		fflush(pset.logfile);
-	}
+		echoQuery(pset.logfile, "USER", query);
 
 	SetCancelConn(pset.db);
 
diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h
index 041b2ac068..cdad55414a 100644
--- a/src/bin/psql/common.h
+++ b/src/bin/psql/common.h
@@ -28,9 +28,10 @@ extern sigjmp_buf sigint_interrupt_jmp;
 
 extern void psql_setup_cancel_handler(void);
 
-extern PGresult *PSQLexec(const char *query);
+extern PGresult *PSQLexec(const char *origin, const char *query);
 extern int	PSQLexecWatch(const char *query, const printQueryOpt *opt);
 
+extern void echoQuery(FILE *out, const char *origin, const char *query);
 extern bool SendQuery(const char *query);
 
 extern bool is_superuser(void);
diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index 2abf255798..b7f701a68a 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -127,7 +127,7 @@ describeAggregates(const char *pattern, bool verbose, bool showSystem)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -195,7 +195,7 @@ describeAccessMethods(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -282,7 +282,7 @@ describeTablespaces(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -682,7 +682,7 @@ describeFunctions(const char *functypes, const char *func_pattern,
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -813,7 +813,7 @@ describeTypes(const char *pattern, bool verbose, bool showSystem)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -1001,7 +1001,7 @@ describeOperators(const char *oper_pattern,
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3, 4;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -1072,7 +1072,7 @@ listAllDbs(const char *pattern, bool verbose)
 							  NULL, "d.datname", NULL, NULL);
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -1225,7 +1225,7 @@ permissionsList(const char *pattern)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	if (!res)
 	{
 		termPQExpBuffer(&buf);
@@ -1304,7 +1304,7 @@ listDefaultACLs(const char *pattern)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	if (!res)
 	{
 		termPQExpBuffer(&buf);
@@ -1503,7 +1503,7 @@ objectDescription(const char *pattern, bool showSystem)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -1555,7 +1555,7 @@ describeTableDetails(const char *pattern, bool verbose, bool showSystem)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 2, 3;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -1816,7 +1816,7 @@ describeOneTableDetails(const char *schemaname,
 						  oid);
 	}
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	if (!res)
 		goto error_return;
 
@@ -1913,7 +1913,7 @@ describeOneTableDetails(const char *schemaname,
 			appendPQExpBuffer(&buf, ".%s;", fmtId(relationname));
 		}
 
-		res = PSQLexec(buf.data);
+		res = PSQLexec("DESCRIBE", buf.data);
 		if (!res)
 			goto error_return;
 
@@ -1936,7 +1936,7 @@ describeOneTableDetails(const char *schemaname,
 						  "\n AND d.deptype IN ('a', 'i')",
 						  oid);
 
-		result = PSQLexec(buf.data);
+		result = PSQLexec("DESCRIBE", buf.data);
 
 		/*
 		 * If we get no rows back, don't show anything (obviously). We should
@@ -2098,7 +2098,7 @@ describeOneTableDetails(const char *schemaname,
 	appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid);
 	appendPQExpBufferStr(&buf, "\nORDER BY a.attnum;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	if (!res)
 		goto error_return;
 	numrows = PQntuples(res);
@@ -2321,7 +2321,7 @@ describeOneTableDetails(const char *schemaname,
 						  " JOIN pg_catalog.pg_inherits i"
 						  " ON c.oid = inhrelid"
 						  "\nWHERE c.oid = '%s';", oid);
-		result = PSQLexec(buf.data);
+		result = PSQLexec("DESCRIBE", buf.data);
 		if (!result)
 			goto error_return;
 
@@ -2362,7 +2362,7 @@ describeOneTableDetails(const char *schemaname,
 		printfPQExpBuffer(&buf,
 						  "SELECT pg_catalog.pg_get_partkeydef('%s'::pg_catalog.oid);",
 						  oid);
-		result = PSQLexec(buf.data);
+		result = PSQLexec("DESCRIBE", buf.data);
 		if (!result)
 			goto error_return;
 
@@ -2387,7 +2387,7 @@ describeOneTableDetails(const char *schemaname,
 						  " JOIN pg_catalog.pg_namespace n"
 						  " ON n.oid = c.relnamespace\n"
 						  "WHERE reltoastrelid = '%s';", oid);
-		result = PSQLexec(buf.data);
+		result = PSQLexec("DESCRIBE", buf.data);
 		if (!result)
 			goto error_return;
 
@@ -2445,7 +2445,7 @@ describeOneTableDetails(const char *schemaname,
 						  "AND i.indrelid = c2.oid;",
 						  oid);
 
-		result = PSQLexec(buf.data);
+		result = PSQLexec("DESCRIBE", buf.data);
 		if (!result)
 			goto error_return;
 		else if (PQntuples(result) != 1)
@@ -2553,7 +2553,7 @@ describeOneTableDetails(const char *schemaname,
 							  "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
 							  "ORDER BY i.indisprimary DESC, c2.relname;",
 							  oid);
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 				goto error_return;
 			else
@@ -2637,7 +2637,7 @@ describeOneTableDetails(const char *schemaname,
 							  "WHERE r.conrelid = '%s' AND r.contype = 'c'\n"
 							  "ORDER BY 1;",
 							  oid);
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 				goto error_return;
 			else
@@ -2700,7 +2700,7 @@ describeOneTableDetails(const char *schemaname,
 				appendPQExpBufferStr(&buf, "ORDER BY conname");
 			}
 
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 				goto error_return;
 			else
@@ -2764,7 +2764,7 @@ describeOneTableDetails(const char *schemaname,
 								  oid);
 			}
 
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 				goto error_return;
 			else
@@ -2814,7 +2814,7 @@ describeOneTableDetails(const char *schemaname,
 							  "WHERE pol.polrelid = '%s' ORDER BY 1;",
 							  oid);
 
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 				goto error_return;
 			else
@@ -2891,7 +2891,7 @@ describeOneTableDetails(const char *schemaname,
 							  "ORDER BY 1;",
 							  oid);
 
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 				goto error_return;
 			else
@@ -2995,7 +2995,7 @@ describeOneTableDetails(const char *schemaname,
 							  "ORDER BY 1;",
 							  oid);
 
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 				goto error_return;
 			else
@@ -3070,7 +3070,7 @@ describeOneTableDetails(const char *schemaname,
 								  "WHERE r.ev_class = '%s' ORDER BY 1;",
 								  oid);
 			}
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 				goto error_return;
 			else
@@ -3159,7 +3159,7 @@ describeOneTableDetails(const char *schemaname,
 							  "ORDER BY 1;",
 							  oid, oid);
 
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 				goto error_return;
 			else
@@ -3189,7 +3189,7 @@ describeOneTableDetails(const char *schemaname,
 		printfPQExpBuffer(&buf,
 						  "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid, true);",
 						  oid);
-		result = PSQLexec(buf.data);
+		result = PSQLexec("DESCRIBE", buf.data);
 		if (!result)
 			goto error_return;
 
@@ -3215,7 +3215,7 @@ describeOneTableDetails(const char *schemaname,
 							  "FROM pg_catalog.pg_rewrite r\n"
 							  "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN' ORDER BY 1;",
 							  oid);
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 				goto error_return;
 
@@ -3284,7 +3284,7 @@ describeOneTableDetails(const char *schemaname,
 								 "   WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))");
 		appendPQExpBufferStr(&buf, "\nORDER BY 1;");
 
-		result = PSQLexec(buf.data);
+		result = PSQLexec("DESCRIBE", buf.data);
 		if (!result)
 			goto error_return;
 		else
@@ -3428,7 +3428,7 @@ describeOneTableDetails(const char *schemaname,
 							  "     pg_catalog.pg_foreign_server s\n"
 							  "WHERE f.ftrelid = '%s' AND s.oid = f.ftserver;",
 							  oid);
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 				goto error_return;
 			else if (PQntuples(result) != 1)
@@ -3462,7 +3462,7 @@ describeOneTableDetails(const char *schemaname,
 						  "\nORDER BY inhseqno;",
 						  oid);
 
-		result = PSQLexec(buf.data);
+		result = PSQLexec("DESCRIBE", buf.data);
 		if (!result)
 			goto error_return;
 		else
@@ -3527,7 +3527,7 @@ describeOneTableDetails(const char *schemaname,
 							  "ORDER BY c.relname;",
 							  oid);
 
-		result = PSQLexec(buf.data);
+		result = PSQLexec("DESCRIBE", buf.data);
 		if (!result)
 			goto error_return;
 		tuples = PQntuples(result);
@@ -3695,7 +3695,7 @@ add_tablespace_footer(printTableContent *const cont, char relkind,
 			printfPQExpBuffer(&buf,
 							  "SELECT spcname FROM pg_catalog.pg_tablespace\n"
 							  "WHERE oid = '%u';", tablespace);
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 			{
 				termPQExpBuffer(&buf);
@@ -3805,7 +3805,7 @@ describeRoles(const char *pattern, bool verbose, bool showSystem)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	if (!res)
 		return false;
 
@@ -3941,7 +3941,7 @@ listDbRoleSettings(const char *pattern, const char *pattern2)
 						  NULL, "d.datname", NULL, NULL);
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -4159,7 +4159,7 @@ listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSys
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1,2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -4376,7 +4376,7 @@ listPartitionedTables(const char *reltypes, const char *pattern, bool verbose)
 					  mixed_output ? "\"Type\" DESC, " : "",
 					  showNested || pattern ? "\"Parent name\" NULLS FIRST, " : "");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -4457,7 +4457,7 @@ listLanguages(const char *pattern, bool verbose, bool showSystem)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -4544,7 +4544,7 @@ listDomains(const char *pattern, bool verbose, bool showSystem)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -4618,7 +4618,7 @@ listConversions(const char *pattern, bool verbose, bool showSystem)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -4684,7 +4684,7 @@ listEventTriggers(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -4778,7 +4778,7 @@ listExtendedStats(const char *pattern)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -4894,7 +4894,7 @@ listCasts(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, ") )\nORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -4993,7 +4993,7 @@ listCollations(const char *pattern, bool verbose, bool showSystem)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5052,7 +5052,7 @@ listSchemas(const char *pattern, bool verbose, bool showSystem)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5112,7 +5112,7 @@ listTSParsers(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5153,7 +5153,7 @@ listTSParsersVerbose(const char *pattern)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5255,7 +5255,7 @@ describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
 					  gettext_noop("Get token types"),
 					  oid);
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5289,7 +5289,7 @@ describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
 					  gettext_noop("Description"),
 					  oid);
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5370,7 +5370,7 @@ listTSDictionaries(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5441,7 +5441,7 @@ listTSTemplates(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5501,7 +5501,7 @@ listTSConfigs(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5543,7 +5543,7 @@ listTSConfigsVerbose(const char *pattern)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 3, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5626,7 +5626,7 @@ describeOneTSConfig(const char *oid, const char *nspname, const char *cfgname,
 					  gettext_noop("Dictionaries"),
 					  oid);
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5730,7 +5730,7 @@ listForeignDataWrappers(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5812,7 +5812,7 @@ listForeignServers(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5873,7 +5873,7 @@ listUserMappings(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5951,7 +5951,7 @@ listForeignTables(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -6008,7 +6008,7 @@ listExtensions(const char *pattern)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -6057,7 +6057,7 @@ listExtensionContents(const char *pattern)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -6117,7 +6117,7 @@ listOneExtensionContents(const char *extname, const char *oid)
 					  gettext_noop("Object description"),
 					  oid);
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -6192,7 +6192,7 @@ listPublications(const char *pattern)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -6259,7 +6259,7 @@ describePublications(const char *pattern)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	if (!res)
 	{
 		termPQExpBuffer(&buf);
@@ -6338,7 +6338,7 @@ describePublications(const char *pattern)
 							  "  AND pr.prpubid = '%s'\n"
 							  "ORDER BY 1,2", pubid);
 
-			tabres = PSQLexec(buf.data);
+			tabres = PSQLexec("DESCRIBE", buf.data);
 			if (!tabres)
 			{
 				printTableCleanup(&cont);
@@ -6443,7 +6443,7 @@ describeSubscriptions(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -6557,7 +6557,7 @@ listOperatorClasses(const char *access_method_pattern,
 	}
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -6638,7 +6638,7 @@ listOperatorFamilies(const char *access_method_pattern,
 	}
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -6725,7 +6725,7 @@ listOpFamilyOperators(const char *access_method_pattern,
 						 "  pg_catalog.format_type(o.amoprighttype, NULL),\n"
 						 "  o.amopstrategy;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -6806,7 +6806,7 @@ listOpFamilyFunctions(const char *access_method_pattern,
 						 "  ap.amproclefttype = ap.amprocrighttype DESC,\n"
 						 "  3, 4, 5;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
diff --git a/src/bin/psql/large_obj.c b/src/bin/psql/large_obj.c
index c15fcc0885..4d7a24e4e1 100644
--- a/src/bin/psql/large_obj.c
+++ b/src/bin/psql/large_obj.c
@@ -72,7 +72,7 @@ start_lo_xact(const char *operation, bool *own_transaction)
 	{
 		case PQTRANS_IDLE:
 			/* need to start our own xact */
-			if (!(res = PSQLexec("BEGIN")))
+			if (!(res = PSQLexec("LO", "BEGIN")))
 				return false;
 			PQclear(res);
 			*own_transaction = true;
@@ -102,9 +102,9 @@ finish_lo_xact(const char *operation, bool own_transaction)
 	if (own_transaction && pset.autocommit)
 	{
 		/* close out our own xact */
-		if (!(res = PSQLexec("COMMIT")))
+		if (!(res = PSQLexec("LO", "COMMIT")))
 		{
-			res = PSQLexec("ROLLBACK");
+			res = PSQLexec("LO", "ROLLBACK");
 			PQclear(res);
 			return false;
 		}
@@ -125,7 +125,7 @@ fail_lo_xact(const char *operation, bool own_transaction)
 	if (own_transaction && pset.autocommit)
 	{
 		/* close out our own xact */
-		res = PSQLexec("ROLLBACK");
+		res = PSQLexec("LO", "ROLLBACK");
 		PQclear(res);
 	}
 
@@ -208,7 +208,7 @@ do_lo_import(const char *filename_arg, const char *comment_arg)
 		bufptr += PQescapeStringConn(pset.db, bufptr, comment_arg, slen, NULL);
 		strcpy(bufptr, "'");
 
-		if (!(res = PSQLexec(cmdbuf)))
+		if (!(res = PSQLexec("LO", cmdbuf)))
 		{
 			free(cmdbuf);
 			return fail_lo_xact("\\lo_import", own_transaction);
@@ -300,7 +300,7 @@ do_lo_list(void)
 				 gettext_noop("Description"));
 	}
 
-	res = PSQLexec(buf);
+	res = PSQLexec("LO", buf);
 	if (!res)
 		return false;
 
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 110906a4e9..773d35d010 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -345,7 +345,7 @@ main(int argc, char *argv[])
 
 		if (options.single_txn)
 		{
-			if ((res = PSQLexec("BEGIN")) == NULL)
+			if ((res = PSQLexec("STARTUP", "BEGIN")) == NULL)
 			{
 				if (pset.on_error_stop)
 				{
@@ -411,7 +411,7 @@ main(int argc, char *argv[])
 
 		if (options.single_txn)
 		{
-			if ((res = PSQLexec("COMMIT")) == NULL)
+			if ((res = PSQLexec("STARTUP", "COMMIT")) == NULL)
 			{
 				if (pset.on_error_stop)
 				{
diff --git a/src/test/regress/expected/psql.out b/src/test/regress/expected/psql.out
index 1b2f6bc418..0bc890ab72 100644
--- a/src/test/regress/expected/psql.out
+++ b/src/test/regress/expected/psql.out
@@ -244,10 +244,18 @@ from pg_attribute
 where attrelid = 'gexec_test'::regclass and attnum > 0
 order by attnum
 \gexec
+-- # GEXEC QUERY
 create index on gexec_test(a)
+
+-- # GEXEC QUERY
 create index on gexec_test(b)
+
+-- # GEXEC QUERY
 create index on gexec_test(c)
+
+-- # GEXEC QUERY
 create index on gexec_test(d)
+
 -- \gexec should work in FETCH_COUNT mode too
 -- (though the fetch limit applies to the executed queries not the meta query)
 \set FETCH_COUNT 1
@@ -257,13 +265,17 @@ select 'drop table gexec_test', NULL
 union all
 select 'drop table gexec_test', 'select ''2000-01-01''::date as party_over'
 \gexec
+-- # GEXEC QUERY
 select 1 as ones
+
  ones 
 ------
     1
 (1 row)
 
+-- # GEXEC QUERY
 select x.y, x.y*2 as double from generate_series(1,4) as x(y)
+
  y | double 
 ---+--------
  1 |      2
@@ -272,10 +284,16 @@ select x.y, x.y*2 as double from generate_series(1,4) as x(y)
  4 |      8
 (4 rows)
 
+-- # GEXEC QUERY
 drop table gexec_test
+
+-- # GEXEC QUERY
 drop table gexec_test
+
 ERROR:  table "gexec_test" does not exist
+-- # GEXEC QUERY
 select '2000-01-01'::date as party_over
+
  party_over 
 ------------
  01-01-2000
#11vignesh C
vignesh21@gmail.com
In reply to: Fabien COELHO (#10)
Re: psql - factor out echo code

On Sat, Jul 3, 2021 at 3:07 AM Fabien COELHO <coelho@cri.ensmp.fr> wrote:

"-- # <whatever> QUERY\n%s\n\n"

Attached an attempt along those lines. I found another duplicate of the
ascii-art printing in another function.

Completion queries seems to be out of the echo/echo hidden feature.

Incredible, there is a (small) impact on regression tests for the \gexec
case. All other changes have no impact, because they are not tested:-(

I am changing the status to "Needs review" as the review is not
completed for this patch and also there are some tests failing, that
need to be fixed:
test test_extdepend ... FAILED 50 ms

Regards,
Vignesh

#12Fabien COELHO
coelho@cri.ensmp.fr
In reply to: vignesh C (#11)
1 attachment(s)
Re: psql - factor out echo code

Hello Vignesh,

I am changing the status to "Needs review" as the review is not
completed for this patch and also there are some tests failing, that
need to be fixed:
test test_extdepend ... FAILED 50 ms

Indeed,

Attached v4 simplifies the format and fixes this one.
I ran check-world, this time.

--
Fabien.

Attachments:

psql-echo-4.patchtext/x-diff; name=psql-echo-4.patchDownload
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 543401c6d6..8ec00881db 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -2061,7 +2061,7 @@ exec_command_password(PsqlScanState scan_state, bool active_branch)
 				printfPQExpBuffer(&buf, "ALTER USER %s PASSWORD ",
 								  fmtId(user));
 				appendStringLiteralConn(&buf, encrypted_password, pset.db);
-				res = PSQLexec(buf.data);
+				res = PSQLexec("PASSWORD", buf.data);
 				termPQExpBuffer(&buf);
 				if (!res)
 					success = false;
@@ -4993,22 +4993,13 @@ do_watch(PQExpBuffer query_buf, double sleep)
  * returns true unless we have ECHO_HIDDEN_NOEXEC.
  */
 static bool
-echo_hidden_command(const char *query)
+echo_hidden_command(const char *origin, const char *query)
 {
 	if (pset.echo_hidden != PSQL_ECHO_HIDDEN_OFF)
 	{
-		printf(_("********* QUERY **********\n"
-				 "%s\n"
-				 "**************************\n\n"), query);
-		fflush(stdout);
+		echoQuery(stdout, origin, query);
 		if (pset.logfile)
-		{
-			fprintf(pset.logfile,
-					_("********* QUERY **********\n"
-					  "%s\n"
-					  "**************************\n\n"), query);
-			fflush(pset.logfile);
-		}
+			echoQuery(pset.logfile, origin, query);
 
 		if (pset.echo_hidden == PSQL_ECHO_HIDDEN_NOEXEC)
 			return false;
@@ -5060,7 +5051,7 @@ lookup_object_oid(EditableObjectType obj_type, const char *desc,
 			break;
 	}
 
-	if (!echo_hidden_command(query->data))
+	if (!echo_hidden_command("INTERNAL", query->data))
 	{
 		destroyPQExpBuffer(query);
 		return false;
@@ -5155,7 +5146,7 @@ get_create_object_cmd(EditableObjectType obj_type, Oid oid,
 			break;
 	}
 
-	if (!echo_hidden_command(query->data))
+	if (!echo_hidden_command("INTERNAL", query->data))
 	{
 		destroyPQExpBuffer(query);
 		return false;
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index 9a00499510..50e8023c90 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -523,6 +523,17 @@ PrintTiming(double elapsed_msec)
 		   elapsed_msec, days, (int) hours, (int) minutes, seconds);
 }
 
+/*
+ * Echo user query
+ */
+void
+echoQuery(FILE *out, const char *origin, const char *query)
+{
+	fprintf(out,
+			/* FIXME should this really be translatable? */
+			_("-- %s QUERY:\n%s\n\n"), origin, query);
+	fflush(out);
+}
 
 /*
  * PSQLexec
@@ -537,7 +548,7 @@ PrintTiming(double elapsed_msec)
  * caller uses this path to issue "SET CLIENT_ENCODING".
  */
 PGresult *
-PSQLexec(const char *query)
+PSQLexec(const char *origin, const char *query)
 {
 	PGresult   *res;
 
@@ -549,18 +560,9 @@ PSQLexec(const char *query)
 
 	if (pset.echo_hidden != PSQL_ECHO_HIDDEN_OFF)
 	{
-		printf(_("********* QUERY **********\n"
-				 "%s\n"
-				 "**************************\n\n"), query);
-		fflush(stdout);
+		echoQuery(stdout, origin, query);
 		if (pset.logfile)
-		{
-			fprintf(pset.logfile,
-					_("********* QUERY **********\n"
-					  "%s\n"
-					  "**************************\n\n"), query);
-			fflush(pset.logfile);
-		}
+			echoQuery(pset.logfile, origin, query);
 
 		if (pset.echo_hidden == PSQL_ECHO_HIDDEN_NOEXEC)
 			return NULL;
@@ -856,10 +858,7 @@ ExecQueryTuples(const PGresult *result)
 				 * assumes that MainLoop did that, so we have to do it here.
 				 */
 				if (pset.echo == PSQL_ECHO_ALL && !pset.singlestep)
-				{
-					puts(query);
-					fflush(stdout);
-				}
+					echoQuery(stdout, "GEXEC", query);
 
 				if (!SendQuery(query))
 				{
@@ -1226,13 +1225,7 @@ SendQuery(const char *query)
 	}
 
 	if (pset.logfile)
-	{
-		fprintf(pset.logfile,
-				_("********* QUERY **********\n"
-				  "%s\n"
-				  "**************************\n\n"), query);
-		fflush(pset.logfile);
-	}
+		echoQuery(pset.logfile, "USER", query);
 
 	SetCancelConn(pset.db);
 
diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h
index 041b2ac068..cdad55414a 100644
--- a/src/bin/psql/common.h
+++ b/src/bin/psql/common.h
@@ -28,9 +28,10 @@ extern sigjmp_buf sigint_interrupt_jmp;
 
 extern void psql_setup_cancel_handler(void);
 
-extern PGresult *PSQLexec(const char *query);
+extern PGresult *PSQLexec(const char *origin, const char *query);
 extern int	PSQLexecWatch(const char *query, const printQueryOpt *opt);
 
+extern void echoQuery(FILE *out, const char *origin, const char *query);
 extern bool SendQuery(const char *query);
 
 extern bool is_superuser(void);
diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index 2abf255798..b7f701a68a 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -127,7 +127,7 @@ describeAggregates(const char *pattern, bool verbose, bool showSystem)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -195,7 +195,7 @@ describeAccessMethods(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -282,7 +282,7 @@ describeTablespaces(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -682,7 +682,7 @@ describeFunctions(const char *functypes, const char *func_pattern,
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -813,7 +813,7 @@ describeTypes(const char *pattern, bool verbose, bool showSystem)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -1001,7 +1001,7 @@ describeOperators(const char *oper_pattern,
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3, 4;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -1072,7 +1072,7 @@ listAllDbs(const char *pattern, bool verbose)
 							  NULL, "d.datname", NULL, NULL);
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -1225,7 +1225,7 @@ permissionsList(const char *pattern)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	if (!res)
 	{
 		termPQExpBuffer(&buf);
@@ -1304,7 +1304,7 @@ listDefaultACLs(const char *pattern)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	if (!res)
 	{
 		termPQExpBuffer(&buf);
@@ -1503,7 +1503,7 @@ objectDescription(const char *pattern, bool showSystem)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -1555,7 +1555,7 @@ describeTableDetails(const char *pattern, bool verbose, bool showSystem)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 2, 3;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -1816,7 +1816,7 @@ describeOneTableDetails(const char *schemaname,
 						  oid);
 	}
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	if (!res)
 		goto error_return;
 
@@ -1913,7 +1913,7 @@ describeOneTableDetails(const char *schemaname,
 			appendPQExpBuffer(&buf, ".%s;", fmtId(relationname));
 		}
 
-		res = PSQLexec(buf.data);
+		res = PSQLexec("DESCRIBE", buf.data);
 		if (!res)
 			goto error_return;
 
@@ -1936,7 +1936,7 @@ describeOneTableDetails(const char *schemaname,
 						  "\n AND d.deptype IN ('a', 'i')",
 						  oid);
 
-		result = PSQLexec(buf.data);
+		result = PSQLexec("DESCRIBE", buf.data);
 
 		/*
 		 * If we get no rows back, don't show anything (obviously). We should
@@ -2098,7 +2098,7 @@ describeOneTableDetails(const char *schemaname,
 	appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid);
 	appendPQExpBufferStr(&buf, "\nORDER BY a.attnum;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	if (!res)
 		goto error_return;
 	numrows = PQntuples(res);
@@ -2321,7 +2321,7 @@ describeOneTableDetails(const char *schemaname,
 						  " JOIN pg_catalog.pg_inherits i"
 						  " ON c.oid = inhrelid"
 						  "\nWHERE c.oid = '%s';", oid);
-		result = PSQLexec(buf.data);
+		result = PSQLexec("DESCRIBE", buf.data);
 		if (!result)
 			goto error_return;
 
@@ -2362,7 +2362,7 @@ describeOneTableDetails(const char *schemaname,
 		printfPQExpBuffer(&buf,
 						  "SELECT pg_catalog.pg_get_partkeydef('%s'::pg_catalog.oid);",
 						  oid);
-		result = PSQLexec(buf.data);
+		result = PSQLexec("DESCRIBE", buf.data);
 		if (!result)
 			goto error_return;
 
@@ -2387,7 +2387,7 @@ describeOneTableDetails(const char *schemaname,
 						  " JOIN pg_catalog.pg_namespace n"
 						  " ON n.oid = c.relnamespace\n"
 						  "WHERE reltoastrelid = '%s';", oid);
-		result = PSQLexec(buf.data);
+		result = PSQLexec("DESCRIBE", buf.data);
 		if (!result)
 			goto error_return;
 
@@ -2445,7 +2445,7 @@ describeOneTableDetails(const char *schemaname,
 						  "AND i.indrelid = c2.oid;",
 						  oid);
 
-		result = PSQLexec(buf.data);
+		result = PSQLexec("DESCRIBE", buf.data);
 		if (!result)
 			goto error_return;
 		else if (PQntuples(result) != 1)
@@ -2553,7 +2553,7 @@ describeOneTableDetails(const char *schemaname,
 							  "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
 							  "ORDER BY i.indisprimary DESC, c2.relname;",
 							  oid);
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 				goto error_return;
 			else
@@ -2637,7 +2637,7 @@ describeOneTableDetails(const char *schemaname,
 							  "WHERE r.conrelid = '%s' AND r.contype = 'c'\n"
 							  "ORDER BY 1;",
 							  oid);
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 				goto error_return;
 			else
@@ -2700,7 +2700,7 @@ describeOneTableDetails(const char *schemaname,
 				appendPQExpBufferStr(&buf, "ORDER BY conname");
 			}
 
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 				goto error_return;
 			else
@@ -2764,7 +2764,7 @@ describeOneTableDetails(const char *schemaname,
 								  oid);
 			}
 
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 				goto error_return;
 			else
@@ -2814,7 +2814,7 @@ describeOneTableDetails(const char *schemaname,
 							  "WHERE pol.polrelid = '%s' ORDER BY 1;",
 							  oid);
 
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 				goto error_return;
 			else
@@ -2891,7 +2891,7 @@ describeOneTableDetails(const char *schemaname,
 							  "ORDER BY 1;",
 							  oid);
 
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 				goto error_return;
 			else
@@ -2995,7 +2995,7 @@ describeOneTableDetails(const char *schemaname,
 							  "ORDER BY 1;",
 							  oid);
 
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 				goto error_return;
 			else
@@ -3070,7 +3070,7 @@ describeOneTableDetails(const char *schemaname,
 								  "WHERE r.ev_class = '%s' ORDER BY 1;",
 								  oid);
 			}
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 				goto error_return;
 			else
@@ -3159,7 +3159,7 @@ describeOneTableDetails(const char *schemaname,
 							  "ORDER BY 1;",
 							  oid, oid);
 
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 				goto error_return;
 			else
@@ -3189,7 +3189,7 @@ describeOneTableDetails(const char *schemaname,
 		printfPQExpBuffer(&buf,
 						  "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid, true);",
 						  oid);
-		result = PSQLexec(buf.data);
+		result = PSQLexec("DESCRIBE", buf.data);
 		if (!result)
 			goto error_return;
 
@@ -3215,7 +3215,7 @@ describeOneTableDetails(const char *schemaname,
 							  "FROM pg_catalog.pg_rewrite r\n"
 							  "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN' ORDER BY 1;",
 							  oid);
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 				goto error_return;
 
@@ -3284,7 +3284,7 @@ describeOneTableDetails(const char *schemaname,
 								 "   WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))");
 		appendPQExpBufferStr(&buf, "\nORDER BY 1;");
 
-		result = PSQLexec(buf.data);
+		result = PSQLexec("DESCRIBE", buf.data);
 		if (!result)
 			goto error_return;
 		else
@@ -3428,7 +3428,7 @@ describeOneTableDetails(const char *schemaname,
 							  "     pg_catalog.pg_foreign_server s\n"
 							  "WHERE f.ftrelid = '%s' AND s.oid = f.ftserver;",
 							  oid);
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 				goto error_return;
 			else if (PQntuples(result) != 1)
@@ -3462,7 +3462,7 @@ describeOneTableDetails(const char *schemaname,
 						  "\nORDER BY inhseqno;",
 						  oid);
 
-		result = PSQLexec(buf.data);
+		result = PSQLexec("DESCRIBE", buf.data);
 		if (!result)
 			goto error_return;
 		else
@@ -3527,7 +3527,7 @@ describeOneTableDetails(const char *schemaname,
 							  "ORDER BY c.relname;",
 							  oid);
 
-		result = PSQLexec(buf.data);
+		result = PSQLexec("DESCRIBE", buf.data);
 		if (!result)
 			goto error_return;
 		tuples = PQntuples(result);
@@ -3695,7 +3695,7 @@ add_tablespace_footer(printTableContent *const cont, char relkind,
 			printfPQExpBuffer(&buf,
 							  "SELECT spcname FROM pg_catalog.pg_tablespace\n"
 							  "WHERE oid = '%u';", tablespace);
-			result = PSQLexec(buf.data);
+			result = PSQLexec("DESCRIBE", buf.data);
 			if (!result)
 			{
 				termPQExpBuffer(&buf);
@@ -3805,7 +3805,7 @@ describeRoles(const char *pattern, bool verbose, bool showSystem)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	if (!res)
 		return false;
 
@@ -3941,7 +3941,7 @@ listDbRoleSettings(const char *pattern, const char *pattern2)
 						  NULL, "d.datname", NULL, NULL);
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -4159,7 +4159,7 @@ listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSys
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1,2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -4376,7 +4376,7 @@ listPartitionedTables(const char *reltypes, const char *pattern, bool verbose)
 					  mixed_output ? "\"Type\" DESC, " : "",
 					  showNested || pattern ? "\"Parent name\" NULLS FIRST, " : "");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -4457,7 +4457,7 @@ listLanguages(const char *pattern, bool verbose, bool showSystem)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -4544,7 +4544,7 @@ listDomains(const char *pattern, bool verbose, bool showSystem)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -4618,7 +4618,7 @@ listConversions(const char *pattern, bool verbose, bool showSystem)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -4684,7 +4684,7 @@ listEventTriggers(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -4778,7 +4778,7 @@ listExtendedStats(const char *pattern)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -4894,7 +4894,7 @@ listCasts(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, ") )\nORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -4993,7 +4993,7 @@ listCollations(const char *pattern, bool verbose, bool showSystem)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5052,7 +5052,7 @@ listSchemas(const char *pattern, bool verbose, bool showSystem)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5112,7 +5112,7 @@ listTSParsers(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5153,7 +5153,7 @@ listTSParsersVerbose(const char *pattern)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5255,7 +5255,7 @@ describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
 					  gettext_noop("Get token types"),
 					  oid);
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5289,7 +5289,7 @@ describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
 					  gettext_noop("Description"),
 					  oid);
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5370,7 +5370,7 @@ listTSDictionaries(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5441,7 +5441,7 @@ listTSTemplates(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5501,7 +5501,7 @@ listTSConfigs(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5543,7 +5543,7 @@ listTSConfigsVerbose(const char *pattern)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 3, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5626,7 +5626,7 @@ describeOneTSConfig(const char *oid, const char *nspname, const char *cfgname,
 					  gettext_noop("Dictionaries"),
 					  oid);
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5730,7 +5730,7 @@ listForeignDataWrappers(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5812,7 +5812,7 @@ listForeignServers(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5873,7 +5873,7 @@ listUserMappings(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -5951,7 +5951,7 @@ listForeignTables(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -6008,7 +6008,7 @@ listExtensions(const char *pattern)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -6057,7 +6057,7 @@ listExtensionContents(const char *pattern)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -6117,7 +6117,7 @@ listOneExtensionContents(const char *extname, const char *oid)
 					  gettext_noop("Object description"),
 					  oid);
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -6192,7 +6192,7 @@ listPublications(const char *pattern)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -6259,7 +6259,7 @@ describePublications(const char *pattern)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 2;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	if (!res)
 	{
 		termPQExpBuffer(&buf);
@@ -6338,7 +6338,7 @@ describePublications(const char *pattern)
 							  "  AND pr.prpubid = '%s'\n"
 							  "ORDER BY 1,2", pubid);
 
-			tabres = PSQLexec(buf.data);
+			tabres = PSQLexec("DESCRIBE", buf.data);
 			if (!tabres)
 			{
 				printTableCleanup(&cont);
@@ -6443,7 +6443,7 @@ describeSubscriptions(const char *pattern, bool verbose)
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -6557,7 +6557,7 @@ listOperatorClasses(const char *access_method_pattern,
 	}
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -6638,7 +6638,7 @@ listOperatorFamilies(const char *access_method_pattern,
 	}
 
 	appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -6725,7 +6725,7 @@ listOpFamilyOperators(const char *access_method_pattern,
 						 "  pg_catalog.format_type(o.amoprighttype, NULL),\n"
 						 "  o.amopstrategy;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
@@ -6806,7 +6806,7 @@ listOpFamilyFunctions(const char *access_method_pattern,
 						 "  ap.amproclefttype = ap.amprocrighttype DESC,\n"
 						 "  3, 4, 5;");
 
-	res = PSQLexec(buf.data);
+	res = PSQLexec("DESCRIBE", buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
 		return false;
diff --git a/src/bin/psql/large_obj.c b/src/bin/psql/large_obj.c
index c15fcc0885..4d7a24e4e1 100644
--- a/src/bin/psql/large_obj.c
+++ b/src/bin/psql/large_obj.c
@@ -72,7 +72,7 @@ start_lo_xact(const char *operation, bool *own_transaction)
 	{
 		case PQTRANS_IDLE:
 			/* need to start our own xact */
-			if (!(res = PSQLexec("BEGIN")))
+			if (!(res = PSQLexec("LO", "BEGIN")))
 				return false;
 			PQclear(res);
 			*own_transaction = true;
@@ -102,9 +102,9 @@ finish_lo_xact(const char *operation, bool own_transaction)
 	if (own_transaction && pset.autocommit)
 	{
 		/* close out our own xact */
-		if (!(res = PSQLexec("COMMIT")))
+		if (!(res = PSQLexec("LO", "COMMIT")))
 		{
-			res = PSQLexec("ROLLBACK");
+			res = PSQLexec("LO", "ROLLBACK");
 			PQclear(res);
 			return false;
 		}
@@ -125,7 +125,7 @@ fail_lo_xact(const char *operation, bool own_transaction)
 	if (own_transaction && pset.autocommit)
 	{
 		/* close out our own xact */
-		res = PSQLexec("ROLLBACK");
+		res = PSQLexec("LO", "ROLLBACK");
 		PQclear(res);
 	}
 
@@ -208,7 +208,7 @@ do_lo_import(const char *filename_arg, const char *comment_arg)
 		bufptr += PQescapeStringConn(pset.db, bufptr, comment_arg, slen, NULL);
 		strcpy(bufptr, "'");
 
-		if (!(res = PSQLexec(cmdbuf)))
+		if (!(res = PSQLexec("LO", cmdbuf)))
 		{
 			free(cmdbuf);
 			return fail_lo_xact("\\lo_import", own_transaction);
@@ -300,7 +300,7 @@ do_lo_list(void)
 				 gettext_noop("Description"));
 	}
 
-	res = PSQLexec(buf);
+	res = PSQLexec("LO", buf);
 	if (!res)
 		return false;
 
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 110906a4e9..773d35d010 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -345,7 +345,7 @@ main(int argc, char *argv[])
 
 		if (options.single_txn)
 		{
-			if ((res = PSQLexec("BEGIN")) == NULL)
+			if ((res = PSQLexec("STARTUP", "BEGIN")) == NULL)
 			{
 				if (pset.on_error_stop)
 				{
@@ -411,7 +411,7 @@ main(int argc, char *argv[])
 
 		if (options.single_txn)
 		{
-			if ((res = PSQLexec("COMMIT")) == NULL)
+			if ((res = PSQLexec("STARTUP", "COMMIT")) == NULL)
 			{
 				if (pset.on_error_stop)
 				{
diff --git a/src/test/modules/test_extensions/expected/test_extdepend.out b/src/test/modules/test_extensions/expected/test_extdepend.out
index 0b62015d18..63eaa3d9b7 100644
--- a/src/test/modules/test_extensions/expected/test_extdepend.out
+++ b/src/test/modules/test_extensions/expected/test_extdepend.out
@@ -29,24 +29,58 @@ SELECT * FROM test_extdep_commands;
 
 -- First, test that dependent objects go away when the extension is dropped.
 SELECT * FROM test_extdep_commands \gexec
+-- GEXEC QUERY:
  CREATE SCHEMA test_ext
+
+-- GEXEC QUERY:
  CREATE EXTENSION test_ext5 SCHEMA test_ext
+
+-- GEXEC QUERY:
  SET search_path TO test_ext
+
+-- GEXEC QUERY:
  CREATE TABLE a (a1 int)
 
+-- GEXEC QUERY:
+
+
+-- GEXEC QUERY:
  CREATE FUNCTION b() RETURNS TRIGGER LANGUAGE plpgsql AS
    $$ BEGIN NEW.a1 := NEW.a1 + 42; RETURN NEW; END; $$
+
+-- GEXEC QUERY:
  ALTER FUNCTION b() DEPENDS ON EXTENSION test_ext5
 
+-- GEXEC QUERY:
+
+
+-- GEXEC QUERY:
  CREATE TRIGGER c BEFORE INSERT ON a FOR EACH ROW EXECUTE PROCEDURE b()
+
+-- GEXEC QUERY:
  ALTER TRIGGER c ON a DEPENDS ON EXTENSION test_ext5
 
+-- GEXEC QUERY:
+
+
+-- GEXEC QUERY:
  CREATE MATERIALIZED VIEW d AS SELECT * FROM a
+
+-- GEXEC QUERY:
  ALTER MATERIALIZED VIEW d DEPENDS ON EXTENSION test_ext5
 
+-- GEXEC QUERY:
+
+
+-- GEXEC QUERY:
  CREATE INDEX e ON a (a1)
+
+-- GEXEC QUERY:
  ALTER INDEX e DEPENDS ON EXTENSION test_ext5
+
+-- GEXEC QUERY:
  RESET search_path
+
 -- A dependent object made dependent again has no effect
 ALTER FUNCTION test_ext.b() DEPENDS ON EXTENSION test_ext5;
 -- make sure we have the right dependencies on the extension
@@ -78,24 +112,58 @@ NOTICE:  drop cascades to table test_ext.a
 -- Second test: If we drop the table, the objects are dropped too and no
 -- vestige remains in pg_depend.
 SELECT * FROM test_extdep_commands \gexec
+-- GEXEC QUERY:
  CREATE SCHEMA test_ext
+
+-- GEXEC QUERY:
  CREATE EXTENSION test_ext5 SCHEMA test_ext
+
+-- GEXEC QUERY:
  SET search_path TO test_ext
+
+-- GEXEC QUERY:
  CREATE TABLE a (a1 int)
 
+-- GEXEC QUERY:
+
+
+-- GEXEC QUERY:
  CREATE FUNCTION b() RETURNS TRIGGER LANGUAGE plpgsql AS
    $$ BEGIN NEW.a1 := NEW.a1 + 42; RETURN NEW; END; $$
+
+-- GEXEC QUERY:
  ALTER FUNCTION b() DEPENDS ON EXTENSION test_ext5
 
+-- GEXEC QUERY:
+
+
+-- GEXEC QUERY:
  CREATE TRIGGER c BEFORE INSERT ON a FOR EACH ROW EXECUTE PROCEDURE b()
+
+-- GEXEC QUERY:
  ALTER TRIGGER c ON a DEPENDS ON EXTENSION test_ext5
 
+-- GEXEC QUERY:
+
+
+-- GEXEC QUERY:
  CREATE MATERIALIZED VIEW d AS SELECT * FROM a
+
+-- GEXEC QUERY:
  ALTER MATERIALIZED VIEW d DEPENDS ON EXTENSION test_ext5
 
+-- GEXEC QUERY:
+
+
+-- GEXEC QUERY:
  CREATE INDEX e ON a (a1)
+
+-- GEXEC QUERY:
  ALTER INDEX e DEPENDS ON EXTENSION test_ext5
+
+-- GEXEC QUERY:
  RESET search_path
+
 DROP TABLE test_ext.a;		-- should fail, require cascade
 ERROR:  cannot drop table test_ext.a because other objects depend on it
 DETAIL:  materialized view test_ext.d depends on table test_ext.a
@@ -116,24 +184,58 @@ DROP EXTENSION test_ext5;
 DROP SCHEMA test_ext CASCADE;
 -- Third test: we can drop the objects individually
 SELECT * FROM test_extdep_commands \gexec
+-- GEXEC QUERY:
  CREATE SCHEMA test_ext
+
+-- GEXEC QUERY:
  CREATE EXTENSION test_ext5 SCHEMA test_ext
+
+-- GEXEC QUERY:
  SET search_path TO test_ext
+
+-- GEXEC QUERY:
  CREATE TABLE a (a1 int)
 
+-- GEXEC QUERY:
+
+
+-- GEXEC QUERY:
  CREATE FUNCTION b() RETURNS TRIGGER LANGUAGE plpgsql AS
    $$ BEGIN NEW.a1 := NEW.a1 + 42; RETURN NEW; END; $$
+
+-- GEXEC QUERY:
  ALTER FUNCTION b() DEPENDS ON EXTENSION test_ext5
 
+-- GEXEC QUERY:
+
+
+-- GEXEC QUERY:
  CREATE TRIGGER c BEFORE INSERT ON a FOR EACH ROW EXECUTE PROCEDURE b()
+
+-- GEXEC QUERY:
  ALTER TRIGGER c ON a DEPENDS ON EXTENSION test_ext5
 
+-- GEXEC QUERY:
+
+
+-- GEXEC QUERY:
  CREATE MATERIALIZED VIEW d AS SELECT * FROM a
+
+-- GEXEC QUERY:
  ALTER MATERIALIZED VIEW d DEPENDS ON EXTENSION test_ext5
 
+-- GEXEC QUERY:
+
+
+-- GEXEC QUERY:
  CREATE INDEX e ON a (a1)
+
+-- GEXEC QUERY:
  ALTER INDEX e DEPENDS ON EXTENSION test_ext5
+
+-- GEXEC QUERY:
  RESET search_path
+
 SET search_path TO test_ext;
 DROP TRIGGER c ON a;
 DROP FUNCTION b();
@@ -156,24 +258,58 @@ NOTICE:  drop cascades to extension test_ext5
 -- Fourth test: we can mark the objects as dependent, then unmark; then the
 -- drop of the extension does nothing
 SELECT * FROM test_extdep_commands \gexec
+-- GEXEC QUERY:
  CREATE SCHEMA test_ext
+
+-- GEXEC QUERY:
  CREATE EXTENSION test_ext5 SCHEMA test_ext
+
+-- GEXEC QUERY:
  SET search_path TO test_ext
+
+-- GEXEC QUERY:
  CREATE TABLE a (a1 int)
 
+-- GEXEC QUERY:
+
+
+-- GEXEC QUERY:
  CREATE FUNCTION b() RETURNS TRIGGER LANGUAGE plpgsql AS
    $$ BEGIN NEW.a1 := NEW.a1 + 42; RETURN NEW; END; $$
+
+-- GEXEC QUERY:
  ALTER FUNCTION b() DEPENDS ON EXTENSION test_ext5
 
+-- GEXEC QUERY:
+
+
+-- GEXEC QUERY:
  CREATE TRIGGER c BEFORE INSERT ON a FOR EACH ROW EXECUTE PROCEDURE b()
+
+-- GEXEC QUERY:
  ALTER TRIGGER c ON a DEPENDS ON EXTENSION test_ext5
 
+-- GEXEC QUERY:
+
+
+-- GEXEC QUERY:
  CREATE MATERIALIZED VIEW d AS SELECT * FROM a
+
+-- GEXEC QUERY:
  ALTER MATERIALIZED VIEW d DEPENDS ON EXTENSION test_ext5
 
+-- GEXEC QUERY:
+
+
+-- GEXEC QUERY:
  CREATE INDEX e ON a (a1)
+
+-- GEXEC QUERY:
  ALTER INDEX e DEPENDS ON EXTENSION test_ext5
+
+-- GEXEC QUERY:
  RESET search_path
+
 SET search_path TO test_ext;
 ALTER FUNCTION b() NO DEPENDS ON EXTENSION test_ext5;
 ALTER TRIGGER c ON a NO DEPENDS ON EXTENSION test_ext5;
diff --git a/src/test/regress/expected/psql.out b/src/test/regress/expected/psql.out
index 1b2f6bc418..02947cd421 100644
--- a/src/test/regress/expected/psql.out
+++ b/src/test/regress/expected/psql.out
@@ -244,10 +244,18 @@ from pg_attribute
 where attrelid = 'gexec_test'::regclass and attnum > 0
 order by attnum
 \gexec
+-- GEXEC QUERY:
 create index on gexec_test(a)
+
+-- GEXEC QUERY:
 create index on gexec_test(b)
+
+-- GEXEC QUERY:
 create index on gexec_test(c)
+
+-- GEXEC QUERY:
 create index on gexec_test(d)
+
 -- \gexec should work in FETCH_COUNT mode too
 -- (though the fetch limit applies to the executed queries not the meta query)
 \set FETCH_COUNT 1
@@ -257,13 +265,17 @@ select 'drop table gexec_test', NULL
 union all
 select 'drop table gexec_test', 'select ''2000-01-01''::date as party_over'
 \gexec
+-- GEXEC QUERY:
 select 1 as ones
+
  ones 
 ------
     1
 (1 row)
 
+-- GEXEC QUERY:
 select x.y, x.y*2 as double from generate_series(1,4) as x(y)
+
  y | double 
 ---+--------
  1 |      2
@@ -272,10 +284,16 @@ select x.y, x.y*2 as double from generate_series(1,4) as x(y)
  4 |      8
 (4 rows)
 
+-- GEXEC QUERY:
 drop table gexec_test
+
+-- GEXEC QUERY:
 drop table gexec_test
+
 ERROR:  table "gexec_test" does not exist
+-- GEXEC QUERY:
 select '2000-01-01'::date as party_over
+
  party_over 
 ------------
  01-01-2000
#13vignesh C
vignesh21@gmail.com
In reply to: Fabien COELHO (#12)
Re: psql - factor out echo code

On Sat, Jul 10, 2021 at 10:25 PM Fabien COELHO <coelho@cri.ensmp.fr> wrote:

Hello Vignesh,

I am changing the status to "Needs review" as the review is not
completed for this patch and also there are some tests failing, that
need to be fixed:
test test_extdepend ... FAILED 50 ms

Indeed,

Attached v4 simplifies the format and fixes this one.
I ran check-world, this time.

Thanks for posting an updated patch, the tests are passing now. I have
changed the status back to Ready For Committer.

Regards,
Vignesh

#14Tom Lane
tgl@sss.pgh.pa.us
In reply to: Fabien COELHO (#12)
Re: psql - factor out echo code

Fabien COELHO <coelho@cri.ensmp.fr> writes:

Attached v4 simplifies the format and fixes this one.

I think this goes way way overboard in terms of invasiveness.
There's no need to identify individual call sites of PSQLexec.
We didn't have anything like that level of detail before, and
there has been no field demand for it either. What I had
in mind was basically to identify the call sites of echoQuery,
ie distinguish user commands from psql-generated commands
with labels like "QUERY:" vs "INTERNAL QUERY:". We don't
need to change the APIs of existing functions, I don't think.

It also looks like a mess from the translatibility standpoint.
You can't expect "%s QUERY" to be a useful thing for translators.

regards, tom lane

#15Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Tom Lane (#14)
1 attachment(s)
Re: psql - factor out echo code

Attached v4 simplifies the format and fixes this one.

I think this goes way way overboard in terms of invasiveness. There's no
need to identify individual call sites of PSQLexec. [...]

ISTM that having the information was useful for the user who actually
asked for psql to show hidden queries, and pretty simple to get, although
somehow invasive.

It also looks like a mess from the translatibility standpoint.
You can't expect "%s QUERY" to be a useful thing for translators.

Sure. Maybe I should have used an enum have a explicit switch in
echoQuery, but I do not like writing this kind of code.

Attached a v5 without hinting at the origin of the query beyond internal
or not.

--
Fabien.

Attachments:

psql-echo-5.patchtext/x-diff; name=psql-echo-5.patchDownload
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index d704c4220c..7e91e588cc 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -5112,18 +5112,9 @@ echo_hidden_command(const char *query)
 {
 	if (pset.echo_hidden != PSQL_ECHO_HIDDEN_OFF)
 	{
-		printf(_("********* QUERY **********\n"
-				 "%s\n"
-				 "**************************\n\n"), query);
-		fflush(stdout);
+		echoQuery(stdout, true, query);
 		if (pset.logfile)
-		{
-			fprintf(pset.logfile,
-					_("********* QUERY **********\n"
-					  "%s\n"
-					  "**************************\n\n"), query);
-			fflush(pset.logfile);
-		}
+			echoQuery(pset.logfile, true, query);
 
 		if (pset.echo_hidden == PSQL_ECHO_HIDDEN_NOEXEC)
 			return false;
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index 5640786678..15dab3c543 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -523,6 +523,18 @@ PrintTiming(double elapsed_msec)
 		   elapsed_msec, days, (int) hours, (int) minutes, seconds);
 }
 
+/*
+ * Echo user query
+ */
+void
+echoQuery(FILE *out, bool is_internal, const char *query)
+{
+	if (is_internal)
+		fprintf(out, _("-- INTERNAL QUERY:\n%s\n\n"), query);
+	else
+		fprintf(out, _("-- QUERY:\n%s\n\n"), query);
+	fflush(out);
+}
 
 /*
  * PSQLexec
@@ -549,18 +561,9 @@ PSQLexec(const char *query)
 
 	if (pset.echo_hidden != PSQL_ECHO_HIDDEN_OFF)
 	{
-		printf(_("********* QUERY **********\n"
-				 "%s\n"
-				 "**************************\n\n"), query);
-		fflush(stdout);
+		echoQuery(stdout, true, query);
 		if (pset.logfile)
-		{
-			fprintf(pset.logfile,
-					_("********* QUERY **********\n"
-					  "%s\n"
-					  "**************************\n\n"), query);
-			fflush(pset.logfile);
-		}
+			echoQuery(pset.logfile, true, query);
 
 		if (pset.echo_hidden == PSQL_ECHO_HIDDEN_NOEXEC)
 			return NULL;
@@ -859,10 +862,7 @@ ExecQueryTuples(const PGresult *result)
 				 * assumes that MainLoop did that, so we have to do it here.
 				 */
 				if (pset.echo == PSQL_ECHO_ALL && !pset.singlestep)
-				{
-					puts(query);
-					fflush(stdout);
-				}
+					echoQuery(stdout, false, query);
 
 				if (!SendQuery(query))
 				{
@@ -1229,13 +1229,7 @@ SendQuery(const char *query)
 	}
 
 	if (pset.logfile)
-	{
-		fprintf(pset.logfile,
-				_("********* QUERY **********\n"
-				  "%s\n"
-				  "**************************\n\n"), query);
-		fflush(pset.logfile);
-	}
+		echoQuery(pset.logfile, false, query);
 
 	SetCancelConn(pset.db);
 
diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h
index d8538a4e06..04ece4e9b1 100644
--- a/src/bin/psql/common.h
+++ b/src/bin/psql/common.h
@@ -31,6 +31,7 @@ extern void psql_setup_cancel_handler(void);
 extern PGresult *PSQLexec(const char *query);
 extern int	PSQLexecWatch(const char *query, const printQueryOpt *opt, FILE *printQueryFout);
 
+extern void echoQuery(FILE *out, bool is_internal, const char *query);
 extern bool SendQuery(const char *query);
 
 extern bool is_superuser(void);
diff --git a/src/test/modules/test_extensions/expected/test_extdepend.out b/src/test/modules/test_extensions/expected/test_extdepend.out
index 0b62015d18..5fd826ac56 100644
--- a/src/test/modules/test_extensions/expected/test_extdepend.out
+++ b/src/test/modules/test_extensions/expected/test_extdepend.out
@@ -29,24 +29,58 @@ SELECT * FROM test_extdep_commands;
 
 -- First, test that dependent objects go away when the extension is dropped.
 SELECT * FROM test_extdep_commands \gexec
+-- QUERY:
  CREATE SCHEMA test_ext
+
+-- QUERY:
  CREATE EXTENSION test_ext5 SCHEMA test_ext
+
+-- QUERY:
  SET search_path TO test_ext
+
+-- QUERY:
  CREATE TABLE a (a1 int)
 
+-- QUERY:
+
+
+-- QUERY:
  CREATE FUNCTION b() RETURNS TRIGGER LANGUAGE plpgsql AS
    $$ BEGIN NEW.a1 := NEW.a1 + 42; RETURN NEW; END; $$
+
+-- QUERY:
  ALTER FUNCTION b() DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+
+
+-- QUERY:
  CREATE TRIGGER c BEFORE INSERT ON a FOR EACH ROW EXECUTE PROCEDURE b()
+
+-- QUERY:
  ALTER TRIGGER c ON a DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+
+
+-- QUERY:
  CREATE MATERIALIZED VIEW d AS SELECT * FROM a
+
+-- QUERY:
  ALTER MATERIALIZED VIEW d DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+
+
+-- QUERY:
  CREATE INDEX e ON a (a1)
+
+-- QUERY:
  ALTER INDEX e DEPENDS ON EXTENSION test_ext5
+
+-- QUERY:
  RESET search_path
+
 -- A dependent object made dependent again has no effect
 ALTER FUNCTION test_ext.b() DEPENDS ON EXTENSION test_ext5;
 -- make sure we have the right dependencies on the extension
@@ -78,24 +112,58 @@ NOTICE:  drop cascades to table test_ext.a
 -- Second test: If we drop the table, the objects are dropped too and no
 -- vestige remains in pg_depend.
 SELECT * FROM test_extdep_commands \gexec
+-- QUERY:
  CREATE SCHEMA test_ext
+
+-- QUERY:
  CREATE EXTENSION test_ext5 SCHEMA test_ext
+
+-- QUERY:
  SET search_path TO test_ext
+
+-- QUERY:
  CREATE TABLE a (a1 int)
 
+-- QUERY:
+
+
+-- QUERY:
  CREATE FUNCTION b() RETURNS TRIGGER LANGUAGE plpgsql AS
    $$ BEGIN NEW.a1 := NEW.a1 + 42; RETURN NEW; END; $$
+
+-- QUERY:
  ALTER FUNCTION b() DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+
+
+-- QUERY:
  CREATE TRIGGER c BEFORE INSERT ON a FOR EACH ROW EXECUTE PROCEDURE b()
+
+-- QUERY:
  ALTER TRIGGER c ON a DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+
+
+-- QUERY:
  CREATE MATERIALIZED VIEW d AS SELECT * FROM a
+
+-- QUERY:
  ALTER MATERIALIZED VIEW d DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+
+
+-- QUERY:
  CREATE INDEX e ON a (a1)
+
+-- QUERY:
  ALTER INDEX e DEPENDS ON EXTENSION test_ext5
+
+-- QUERY:
  RESET search_path
+
 DROP TABLE test_ext.a;		-- should fail, require cascade
 ERROR:  cannot drop table test_ext.a because other objects depend on it
 DETAIL:  materialized view test_ext.d depends on table test_ext.a
@@ -116,24 +184,58 @@ DROP EXTENSION test_ext5;
 DROP SCHEMA test_ext CASCADE;
 -- Third test: we can drop the objects individually
 SELECT * FROM test_extdep_commands \gexec
+-- QUERY:
  CREATE SCHEMA test_ext
+
+-- QUERY:
  CREATE EXTENSION test_ext5 SCHEMA test_ext
+
+-- QUERY:
  SET search_path TO test_ext
+
+-- QUERY:
  CREATE TABLE a (a1 int)
 
+-- QUERY:
+
+
+-- QUERY:
  CREATE FUNCTION b() RETURNS TRIGGER LANGUAGE plpgsql AS
    $$ BEGIN NEW.a1 := NEW.a1 + 42; RETURN NEW; END; $$
+
+-- QUERY:
  ALTER FUNCTION b() DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+
+
+-- QUERY:
  CREATE TRIGGER c BEFORE INSERT ON a FOR EACH ROW EXECUTE PROCEDURE b()
+
+-- QUERY:
  ALTER TRIGGER c ON a DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+
+
+-- QUERY:
  CREATE MATERIALIZED VIEW d AS SELECT * FROM a
+
+-- QUERY:
  ALTER MATERIALIZED VIEW d DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+
+
+-- QUERY:
  CREATE INDEX e ON a (a1)
+
+-- QUERY:
  ALTER INDEX e DEPENDS ON EXTENSION test_ext5
+
+-- QUERY:
  RESET search_path
+
 SET search_path TO test_ext;
 DROP TRIGGER c ON a;
 DROP FUNCTION b();
@@ -156,24 +258,58 @@ NOTICE:  drop cascades to extension test_ext5
 -- Fourth test: we can mark the objects as dependent, then unmark; then the
 -- drop of the extension does nothing
 SELECT * FROM test_extdep_commands \gexec
+-- QUERY:
  CREATE SCHEMA test_ext
+
+-- QUERY:
  CREATE EXTENSION test_ext5 SCHEMA test_ext
+
+-- QUERY:
  SET search_path TO test_ext
+
+-- QUERY:
  CREATE TABLE a (a1 int)
 
+-- QUERY:
+
+
+-- QUERY:
  CREATE FUNCTION b() RETURNS TRIGGER LANGUAGE plpgsql AS
    $$ BEGIN NEW.a1 := NEW.a1 + 42; RETURN NEW; END; $$
+
+-- QUERY:
  ALTER FUNCTION b() DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+
+
+-- QUERY:
  CREATE TRIGGER c BEFORE INSERT ON a FOR EACH ROW EXECUTE PROCEDURE b()
+
+-- QUERY:
  ALTER TRIGGER c ON a DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+
+
+-- QUERY:
  CREATE MATERIALIZED VIEW d AS SELECT * FROM a
+
+-- QUERY:
  ALTER MATERIALIZED VIEW d DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+
+
+-- QUERY:
  CREATE INDEX e ON a (a1)
+
+-- QUERY:
  ALTER INDEX e DEPENDS ON EXTENSION test_ext5
+
+-- QUERY:
  RESET search_path
+
 SET search_path TO test_ext;
 ALTER FUNCTION b() NO DEPENDS ON EXTENSION test_ext5;
 ALTER TRIGGER c ON a NO DEPENDS ON EXTENSION test_ext5;
diff --git a/src/test/regress/expected/psql.out b/src/test/regress/expected/psql.out
index 1b2f6bc418..7bdc4045f2 100644
--- a/src/test/regress/expected/psql.out
+++ b/src/test/regress/expected/psql.out
@@ -244,10 +244,18 @@ from pg_attribute
 where attrelid = 'gexec_test'::regclass and attnum > 0
 order by attnum
 \gexec
+-- QUERY:
 create index on gexec_test(a)
+
+-- QUERY:
 create index on gexec_test(b)
+
+-- QUERY:
 create index on gexec_test(c)
+
+-- QUERY:
 create index on gexec_test(d)
+
 -- \gexec should work in FETCH_COUNT mode too
 -- (though the fetch limit applies to the executed queries not the meta query)
 \set FETCH_COUNT 1
@@ -257,13 +265,17 @@ select 'drop table gexec_test', NULL
 union all
 select 'drop table gexec_test', 'select ''2000-01-01''::date as party_over'
 \gexec
+-- QUERY:
 select 1 as ones
+
  ones 
 ------
     1
 (1 row)
 
+-- QUERY:
 select x.y, x.y*2 as double from generate_series(1,4) as x(y)
+
  y | double 
 ---+--------
  1 |      2
@@ -272,10 +284,16 @@ select x.y, x.y*2 as double from generate_series(1,4) as x(y)
  4 |      8
 (4 rows)
 
+-- QUERY:
 drop table gexec_test
+
+-- QUERY:
 drop table gexec_test
+
 ERROR:  table "gexec_test" does not exist
+-- QUERY:
 select '2000-01-01'::date as party_over
+
  party_over 
 ------------
  01-01-2000
#16Pavel Stehule
pavel.stehule@gmail.com
In reply to: Fabien COELHO (#15)
Re: psql - factor out echo code

Hi

ne 24. 7. 2022 v 21:39 odesílatel Fabien COELHO <coelho@cri.ensmp.fr>
napsal:

Attached v4 simplifies the format and fixes this one.

I think this goes way way overboard in terms of invasiveness. There's no
need to identify individual call sites of PSQLexec. [...]

ISTM that having the information was useful for the user who actually
asked for psql to show hidden queries, and pretty simple to get, although
somehow invasive.

It also looks like a mess from the translatibility standpoint.
You can't expect "%s QUERY" to be a useful thing for translators.

Sure. Maybe I should have used an enum have a explicit switch in
echoQuery, but I do not like writing this kind of code.

Attached a v5 without hinting at the origin of the query beyond internal
or not.

I had just one question - with this patch, the format of output of modes
ECHO ALL and ECHO QUERIES will be different, and that can be a little bit
messy. On second hand, the prefix --QUERY can be disturbing in echo queries
mode. It is not a problem in echo all mode, because queries and results are
mixed together. So in the end, I think the current design can work.

All tests passed, this is trivial patch without impacts on users

I'll mark this patch as ready for committer

Regards

Pavel

#17Michael Paquier
michael@paquier.xyz
In reply to: Pavel Stehule (#16)
Re: psql - factor out echo code

On Sun, Jul 24, 2022 at 10:23:39PM +0200, Pavel Stehule wrote:

I had just one question - with this patch, the format of output of modes
ECHO ALL and ECHO QUERIES will be different, and that can be a little bit
messy. On second hand, the prefix --QUERY can be disturbing in echo queries
mode. It is not a problem in echo all mode, because queries and results are
mixed together. So in the end, I think the current design can work.

All tests passed, this is trivial patch without impacts on users

I'll mark this patch as ready for committer

Hmm. The refactoring is worth it as much as the differentiation
between QUERY and INTERNAL QUERY as the same pattern is repeated 5
times.

Now some of the output generated by test_extdepend gets a bit
confusing:
+-- QUERY:
+
+
+-- QUERY:

That's not entirely this patch's fault. Still that's not really
intuitive to see the output of a query that's just a blank spot..
--
Michael

#18Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Michael Paquier (#17)
Re: psql - factor out echo code

Hmm. The refactoring is worth it as much as the differentiation
between QUERY and INTERNAL QUERY as the same pattern is repeated 5
times.

Now some of the output generated by test_extdepend gets a bit
confusing:
+-- QUERY:
+
+
+-- QUERY:

That's not entirely this patch's fault. Still that's not really
intuitive to see the output of a query that's just a blank spot..

Hmmm.

What about adding an explicit \echo before these empty outputs to mitigate
the possible induced confusion?

--
Fabien.

#19Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Fabien COELHO (#18)
1 attachment(s)
Re: psql - factor out echo code
Now some of the output generated by test_extdepend gets a bit
confusing:
+-- QUERY:
+
+
+-- QUERY:

That's not entirely this patch's fault. Still that's not really
intuitive to see the output of a query that's just a blank spot..

Hmmm.

What about adding an explicit \echo before these empty outputs to mitigate
the possible induced confusion?

\echo is not possible.

Attached an attempt to improve the situation by replacing empty lines with
comments in this test.

--
Fabien.

Attachments:

psql-echo-6.patchtext/x-diff; name=psql-echo-6.patchDownload
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 7672ed9e9d..16873aff62 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -5284,18 +5284,9 @@ echo_hidden_command(const char *query)
 {
 	if (pset.echo_hidden != PSQL_ECHO_HIDDEN_OFF)
 	{
-		printf(_("********* QUERY **********\n"
-				 "%s\n"
-				 "**************************\n\n"), query);
-		fflush(stdout);
+		echoQuery(stdout, true, query);
 		if (pset.logfile)
-		{
-			fprintf(pset.logfile,
-					_("********* QUERY **********\n"
-					  "%s\n"
-					  "**************************\n\n"), query);
-			fflush(pset.logfile);
-		}
+			echoQuery(pset.logfile, true, query);
 
 		if (pset.echo_hidden == PSQL_ECHO_HIDDEN_NOEXEC)
 			return false;
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index b989d792aa..30fd5bcda1 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -543,6 +543,18 @@ PrintTiming(double elapsed_msec)
 		   elapsed_msec, days, (int) hours, (int) minutes, seconds);
 }
 
+/*
+ * Echo user query
+ */
+void
+echoQuery(FILE *out, bool is_internal, const char *query)
+{
+	if (is_internal)
+		fprintf(out, _("-- INTERNAL QUERY:\n%s\n\n"), query);
+	else
+		fprintf(out, _("-- QUERY:\n%s\n\n"), query);
+	fflush(out);
+}
 
 /*
  * PSQLexec
@@ -569,18 +581,9 @@ PSQLexec(const char *query)
 
 	if (pset.echo_hidden != PSQL_ECHO_HIDDEN_OFF)
 	{
-		printf(_("********* QUERY **********\n"
-				 "%s\n"
-				 "**************************\n\n"), query);
-		fflush(stdout);
+		echoQuery(stdout, true, query);
 		if (pset.logfile)
-		{
-			fprintf(pset.logfile,
-					_("********* QUERY **********\n"
-					  "%s\n"
-					  "**************************\n\n"), query);
-			fflush(pset.logfile);
-		}
+			echoQuery(pset.logfile, true, query);
 
 		if (pset.echo_hidden == PSQL_ECHO_HIDDEN_NOEXEC)
 			return NULL;
@@ -796,10 +799,7 @@ ExecQueryTuples(const PGresult *result)
 				 * assumes that MainLoop did that, so we have to do it here.
 				 */
 				if (pset.echo == PSQL_ECHO_ALL && !pset.singlestep)
-				{
-					puts(query);
-					fflush(stdout);
-				}
+					echoQuery(stdout, false, query);
 
 				if (!SendQuery(query))
 				{
@@ -1058,13 +1058,7 @@ SendQuery(const char *query)
 	}
 
 	if (pset.logfile)
-	{
-		fprintf(pset.logfile,
-				_("********* QUERY **********\n"
-				  "%s\n"
-				  "**************************\n\n"), query);
-		fflush(pset.logfile);
-	}
+		echoQuery(pset.logfile, false, query);
 
 	SetCancelConn(pset.db);
 
diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h
index f0820dd7d5..359c48143e 100644
--- a/src/bin/psql/common.h
+++ b/src/bin/psql/common.h
@@ -32,6 +32,7 @@ extern void psql_setup_cancel_handler(void);
 extern PGresult *PSQLexec(const char *query);
 extern int	PSQLexecWatch(const char *query, const printQueryOpt *opt, FILE *printQueryFout);
 
+extern void echoQuery(FILE *out, bool is_internal, const char *query);
 extern bool SendQuery(const char *query);
 
 extern bool is_superuser(void);
diff --git a/src/test/modules/test_extensions/expected/test_extdepend.out b/src/test/modules/test_extensions/expected/test_extdepend.out
index 0b62015d18..a08ef8d19d 100644
--- a/src/test/modules/test_extensions/expected/test_extdepend.out
+++ b/src/test/modules/test_extensions/expected/test_extdepend.out
@@ -11,17 +11,17 @@ SELECT * FROM test_extdep_commands;
   CREATE EXTENSION test_ext5 SCHEMA test_ext
   SET search_path TO test_ext
   CREATE TABLE a (a1 int)
- 
+  -- function b
   CREATE FUNCTION b() RETURNS TRIGGER LANGUAGE plpgsql AS               +
     $$ BEGIN NEW.a1 := NEW.a1 + 42; RETURN NEW; END; $$
   ALTER FUNCTION b() DEPENDS ON EXTENSION test_ext5
- 
+  -- trigger c
   CREATE TRIGGER c BEFORE INSERT ON a FOR EACH ROW EXECUTE PROCEDURE b()
   ALTER TRIGGER c ON a DEPENDS ON EXTENSION test_ext5
- 
+  -- materialized view d
   CREATE MATERIALIZED VIEW d AS SELECT * FROM a
   ALTER MATERIALIZED VIEW d DEPENDS ON EXTENSION test_ext5
- 
+  -- index e
   CREATE INDEX e ON a (a1)
   ALTER INDEX e DEPENDS ON EXTENSION test_ext5
   RESET search_path
@@ -29,24 +29,58 @@ SELECT * FROM test_extdep_commands;
 
 -- First, test that dependent objects go away when the extension is dropped.
 SELECT * FROM test_extdep_commands \gexec
+-- QUERY:
  CREATE SCHEMA test_ext
+
+-- QUERY:
  CREATE EXTENSION test_ext5 SCHEMA test_ext
+
+-- QUERY:
  SET search_path TO test_ext
+
+-- QUERY:
  CREATE TABLE a (a1 int)
 
+-- QUERY:
+ -- function b
+
+-- QUERY:
  CREATE FUNCTION b() RETURNS TRIGGER LANGUAGE plpgsql AS
    $$ BEGIN NEW.a1 := NEW.a1 + 42; RETURN NEW; END; $$
+
+-- QUERY:
  ALTER FUNCTION b() DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+ -- trigger c
+
+-- QUERY:
  CREATE TRIGGER c BEFORE INSERT ON a FOR EACH ROW EXECUTE PROCEDURE b()
+
+-- QUERY:
  ALTER TRIGGER c ON a DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+ -- materialized view d
+
+-- QUERY:
  CREATE MATERIALIZED VIEW d AS SELECT * FROM a
+
+-- QUERY:
  ALTER MATERIALIZED VIEW d DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+ -- index e
+
+-- QUERY:
  CREATE INDEX e ON a (a1)
+
+-- QUERY:
  ALTER INDEX e DEPENDS ON EXTENSION test_ext5
+
+-- QUERY:
  RESET search_path
+
 -- A dependent object made dependent again has no effect
 ALTER FUNCTION test_ext.b() DEPENDS ON EXTENSION test_ext5;
 -- make sure we have the right dependencies on the extension
@@ -78,24 +112,58 @@ NOTICE:  drop cascades to table test_ext.a
 -- Second test: If we drop the table, the objects are dropped too and no
 -- vestige remains in pg_depend.
 SELECT * FROM test_extdep_commands \gexec
+-- QUERY:
  CREATE SCHEMA test_ext
+
+-- QUERY:
  CREATE EXTENSION test_ext5 SCHEMA test_ext
+
+-- QUERY:
  SET search_path TO test_ext
+
+-- QUERY:
  CREATE TABLE a (a1 int)
 
+-- QUERY:
+ -- function b
+
+-- QUERY:
  CREATE FUNCTION b() RETURNS TRIGGER LANGUAGE plpgsql AS
    $$ BEGIN NEW.a1 := NEW.a1 + 42; RETURN NEW; END; $$
+
+-- QUERY:
  ALTER FUNCTION b() DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+ -- trigger c
+
+-- QUERY:
  CREATE TRIGGER c BEFORE INSERT ON a FOR EACH ROW EXECUTE PROCEDURE b()
+
+-- QUERY:
  ALTER TRIGGER c ON a DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+ -- materialized view d
+
+-- QUERY:
  CREATE MATERIALIZED VIEW d AS SELECT * FROM a
+
+-- QUERY:
  ALTER MATERIALIZED VIEW d DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+ -- index e
+
+-- QUERY:
  CREATE INDEX e ON a (a1)
+
+-- QUERY:
  ALTER INDEX e DEPENDS ON EXTENSION test_ext5
+
+-- QUERY:
  RESET search_path
+
 DROP TABLE test_ext.a;		-- should fail, require cascade
 ERROR:  cannot drop table test_ext.a because other objects depend on it
 DETAIL:  materialized view test_ext.d depends on table test_ext.a
@@ -116,24 +184,58 @@ DROP EXTENSION test_ext5;
 DROP SCHEMA test_ext CASCADE;
 -- Third test: we can drop the objects individually
 SELECT * FROM test_extdep_commands \gexec
+-- QUERY:
  CREATE SCHEMA test_ext
+
+-- QUERY:
  CREATE EXTENSION test_ext5 SCHEMA test_ext
+
+-- QUERY:
  SET search_path TO test_ext
+
+-- QUERY:
  CREATE TABLE a (a1 int)
 
+-- QUERY:
+ -- function b
+
+-- QUERY:
  CREATE FUNCTION b() RETURNS TRIGGER LANGUAGE plpgsql AS
    $$ BEGIN NEW.a1 := NEW.a1 + 42; RETURN NEW; END; $$
+
+-- QUERY:
  ALTER FUNCTION b() DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+ -- trigger c
+
+-- QUERY:
  CREATE TRIGGER c BEFORE INSERT ON a FOR EACH ROW EXECUTE PROCEDURE b()
+
+-- QUERY:
  ALTER TRIGGER c ON a DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+ -- materialized view d
+
+-- QUERY:
  CREATE MATERIALIZED VIEW d AS SELECT * FROM a
+
+-- QUERY:
  ALTER MATERIALIZED VIEW d DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+ -- index e
+
+-- QUERY:
  CREATE INDEX e ON a (a1)
+
+-- QUERY:
  ALTER INDEX e DEPENDS ON EXTENSION test_ext5
+
+-- QUERY:
  RESET search_path
+
 SET search_path TO test_ext;
 DROP TRIGGER c ON a;
 DROP FUNCTION b();
@@ -156,24 +258,58 @@ NOTICE:  drop cascades to extension test_ext5
 -- Fourth test: we can mark the objects as dependent, then unmark; then the
 -- drop of the extension does nothing
 SELECT * FROM test_extdep_commands \gexec
+-- QUERY:
  CREATE SCHEMA test_ext
+
+-- QUERY:
  CREATE EXTENSION test_ext5 SCHEMA test_ext
+
+-- QUERY:
  SET search_path TO test_ext
+
+-- QUERY:
  CREATE TABLE a (a1 int)
 
+-- QUERY:
+ -- function b
+
+-- QUERY:
  CREATE FUNCTION b() RETURNS TRIGGER LANGUAGE plpgsql AS
    $$ BEGIN NEW.a1 := NEW.a1 + 42; RETURN NEW; END; $$
+
+-- QUERY:
  ALTER FUNCTION b() DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+ -- trigger c
+
+-- QUERY:
  CREATE TRIGGER c BEFORE INSERT ON a FOR EACH ROW EXECUTE PROCEDURE b()
+
+-- QUERY:
  ALTER TRIGGER c ON a DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+ -- materialized view d
+
+-- QUERY:
  CREATE MATERIALIZED VIEW d AS SELECT * FROM a
+
+-- QUERY:
  ALTER MATERIALIZED VIEW d DEPENDS ON EXTENSION test_ext5
 
+-- QUERY:
+ -- index e
+
+-- QUERY:
  CREATE INDEX e ON a (a1)
+
+-- QUERY:
  ALTER INDEX e DEPENDS ON EXTENSION test_ext5
+
+-- QUERY:
  RESET search_path
+
 SET search_path TO test_ext;
 ALTER FUNCTION b() NO DEPENDS ON EXTENSION test_ext5;
 ALTER TRIGGER c ON a NO DEPENDS ON EXTENSION test_ext5;
diff --git a/src/test/modules/test_extensions/sql/test_extdepend.sql b/src/test/modules/test_extensions/sql/test_extdepend.sql
index 63240a1af5..7a23c18bf8 100644
--- a/src/test/modules/test_extensions/sql/test_extdepend.sql
+++ b/src/test/modules/test_extensions/sql/test_extdepend.sql
@@ -9,16 +9,16 @@ COPY test_extdep_commands FROM stdin;
  CREATE EXTENSION test_ext5 SCHEMA test_ext
  SET search_path TO test_ext
  CREATE TABLE a (a1 int)
-
+ -- function b
  CREATE FUNCTION b() RETURNS TRIGGER LANGUAGE plpgsql AS\n   $$ BEGIN NEW.a1 := NEW.a1 + 42; RETURN NEW; END; $$
  ALTER FUNCTION b() DEPENDS ON EXTENSION test_ext5
-
+ -- trigger c
  CREATE TRIGGER c BEFORE INSERT ON a FOR EACH ROW EXECUTE PROCEDURE b()
  ALTER TRIGGER c ON a DEPENDS ON EXTENSION test_ext5
-
+ -- materialized view d
  CREATE MATERIALIZED VIEW d AS SELECT * FROM a
  ALTER MATERIALIZED VIEW d DEPENDS ON EXTENSION test_ext5
-
+ -- index e
  CREATE INDEX e ON a (a1)
  ALTER INDEX e DEPENDS ON EXTENSION test_ext5
  RESET search_path
diff --git a/src/test/regress/expected/psql.out b/src/test/regress/expected/psql.out
index 1047399ef8..591149dee9 100644
--- a/src/test/regress/expected/psql.out
+++ b/src/test/regress/expected/psql.out
@@ -275,10 +275,18 @@ from pg_attribute
 where attrelid = 'gexec_test'::regclass and attnum > 0
 order by attnum
 \gexec
+-- QUERY:
 create index on gexec_test(a)
+
+-- QUERY:
 create index on gexec_test(b)
+
+-- QUERY:
 create index on gexec_test(c)
+
+-- QUERY:
 create index on gexec_test(d)
+
 -- \gexec should work in FETCH_COUNT mode too
 -- (though the fetch limit applies to the executed queries not the meta query)
 \set FETCH_COUNT 1
@@ -288,13 +296,17 @@ select 'drop table gexec_test', NULL
 union all
 select 'drop table gexec_test', 'select ''2000-01-01''::date as party_over'
 \gexec
+-- QUERY:
 select 1 as ones
+
  ones 
 ------
     1
 (1 row)
 
+-- QUERY:
 select x.y, x.y*2 as double from generate_series(1,4) as x(y)
+
  y | double 
 ---+--------
  1 |      2
@@ -303,10 +315,16 @@ select x.y, x.y*2 as double from generate_series(1,4) as x(y)
  4 |      8
 (4 rows)
 
+-- QUERY:
 drop table gexec_test
+
+-- QUERY:
 drop table gexec_test
+
 ERROR:  table "gexec_test" does not exist
+-- QUERY:
 select '2000-01-01'::date as party_over
+
  party_over 
 ------------
  01-01-2000
#20Pavel Stehule
pavel.stehule@gmail.com
In reply to: Fabien COELHO (#19)
Re: psql - factor out echo code

st 30. 11. 2022 v 10:43 odesílatel Fabien COELHO <coelho@cri.ensmp.fr>
napsal:

Now some of the output generated by test_extdepend gets a bit
confusing:
+-- QUERY:
+
+
+-- QUERY:

That's not entirely this patch's fault. Still that's not really
intuitive to see the output of a query that's just a blank spot..

Hmmm.

What about adding an explicit \echo before these empty outputs to

mitigate

the possible induced confusion?

\echo is not possible.

Attached an attempt to improve the situation by replacing empty lines with
comments in this test.

I can confirm so all regress tests passed

Regards

Pavel

Show quoted text

--
Fabien.

#21Peter Eisentraut
peter.eisentraut@enterprisedb.com
In reply to: Pavel Stehule (#20)
Re: psql - factor out echo code

On 01.12.22 08:27, Pavel Stehule wrote:

st 30. 11. 2022 v 10:43 odesílatel Fabien COELHO <coelho@cri.ensmp.fr
<mailto:coelho@cri.ensmp.fr>> napsal:

Now some of the output generated by test_extdepend gets a bit
confusing:
+-- QUERY:
+
+
+-- QUERY:

That's not entirely this patch's fault.  Still that's not really
intuitive to see the output of a query that's just a blank spot..

Hmmm.

What about adding an explicit \echo before these empty outputs to

mitigate

the possible induced confusion?

\echo is not possible.

Attached an attempt to improve the situation by replacing empty
lines with
comments in this test.

I can confirm so all regress tests passed

I think this patch requires an up-to-date summary and explanation. The
thread is over a year old and the patch has evolved quite a bit. There
are some test changes that are not explained. Please provide more
detail so that the patch can be considered.

#22Gregory Stark (as CFM)
stark.cfm@gmail.com
In reply to: Peter Eisentraut (#21)
Re: psql - factor out echo code

On Mon, 13 Feb 2023 at 05:41, Peter Eisentraut
<peter.eisentraut@enterprisedb.com> wrote:

I think this patch requires an up-to-date summary and explanation. The
thread is over a year old and the patch has evolved quite a bit. There
are some test changes that are not explained. Please provide more
detail so that the patch can be considered.

Given this feedback I'm going to mark this Returned with Feedback. I
think it'll be clearer to start with a new thread explaining the
intent of the patch as it is now.

--
Gregory Stark
As Commitfest Manager