proposal - psql - possibility to redirect only tabular output

Started by Pavel Stehulealmost 6 years ago8 messages
#1Pavel Stehule
pavel.stehule@gmail.com

Hi

Now, the content of redirect output has two parts

1. tabular output
2. cmd tags

There is a problem with command tags, because it is specific kind of
information and can be nice if can be redirected to stdout every time like
\h output. There can be new psql variable like REDIRECTED_OUTPUT with
possibilities (all, tabular)

What do you think about this?

Pavel

#2Pavel Stehule
pavel.stehule@gmail.com
In reply to: Pavel Stehule (#1)
1 attachment(s)
Re: proposal - psql - possibility to redirect only tabular output

so 11. 4. 2020 v 8:54 odesílatel Pavel Stehule <pavel.stehule@gmail.com>
napsal:

Hi

Now, the content of redirect output has two parts

1. tabular output
2. cmd tags

There is a problem with command tags, because it is specific kind of
information and can be nice if can be redirected to stdout every time like
\h output. There can be new psql variable like REDIRECTED_OUTPUT with
possibilities (all, tabular)

What do you think about this?

or different method - set target of status row - with result (default) or
stdout (terminal)

patch assigned

When I pin status rows just to stdout, then redirected output contains only
query results

Regards

Pavel

Show quoted text

Pavel

Attachments:

psql-status-target.patchtext/x-patch; charset=US-ASCII; name=psql-status-target.patchDownload
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index 621a33f7e8..2b722eb2de 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1072,17 +1072,20 @@ static void
 PrintQueryStatus(PGresult *results)
 {
 	char		buf[16];
+	FILE	   *fp;
+
+	fp = pset.status_target == PSQL_STATUS_RESULT ? pset.queryFout : stdout;
 
 	if (!pset.quiet)
 	{
 		if (pset.popt.topt.format == PRINT_HTML)
 		{
-			fputs("<p>", pset.queryFout);
-			html_escaped_print(PQcmdStatus(results), pset.queryFout);
-			fputs("</p>\n", pset.queryFout);
+			fputs("<p>", fp);
+			html_escaped_print(PQcmdStatus(results), fp);
+			fputs("</p>\n", fp);
 		}
 		else
-			fprintf(pset.queryFout, "%s\n", PQcmdStatus(results));
+			fprintf(fp, "%s\n", PQcmdStatus(results));
 	}
 
 	if (pset.logfile)
diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h
index 97941aa10c..2299cb2b6d 100644
--- a/src/bin/psql/settings.h
+++ b/src/bin/psql/settings.h
@@ -70,6 +70,12 @@ typedef enum
 	hctl_ignoreboth = hctl_ignorespace | hctl_ignoredups
 } HistControl;
 
+typedef enum
+{
+	PSQL_STATUS_STDOUT,
+	PSQL_STATUS_RESULT
+} PSQL_STATUS_TARGET;
+
 enum trivalue
 {
 	TRI_DEFAULT,
@@ -135,6 +141,7 @@ typedef struct _psqlSettings
 	PSQL_ECHO_HIDDEN echo_hidden;
 	PSQL_ERROR_ROLLBACK on_error_rollback;
 	PSQL_COMP_CASE comp_case;
+	PSQL_STATUS_TARGET status_target;
 	HistControl histcontrol;
 	const char *prompt1;
 	const char *prompt2;
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 3302bd4dd3..e6b0ea91aa 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -1076,6 +1076,30 @@ histcontrol_hook(const char *newval)
 	return true;
 }
 
+static char *
+status_target_substitute_hook(char *newVal)
+{
+	if (newVal == NULL)
+		newVal = pg_strdup("result");
+	return newVal;
+}
+
+static bool
+status_target_hook(const char *newVal)
+{
+	Assert(newVal != NULL);
+	if (pg_strcasecmp(newVal, "stdout") == 0)
+		pset.status_target = PSQL_STATUS_STDOUT;
+	else if (pg_strcasecmp(newVal, "result") == 0)
+		pset.status_target = PSQL_STATUS_RESULT;
+	else
+	{
+		PsqlVarEnumError("STATUS_TARGET", newVal, "result, stdout");
+		return false;
+	}
+	return true;
+}
+
 static bool
 prompt1_hook(const char *newval)
 {
@@ -1228,4 +1252,7 @@ EstablishVariableSpace(void)
 	SetVariableHooks(pset.vars, "HIDE_TABLEAM",
 					 bool_substitute_hook,
 					 hide_tableam_hook);
+	SetVariableHooks(pset.vars, "STATUS_TARGET",
+					 status_target_substitute_hook,
+					 status_target_hook);
 }
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 0e7a373caf..882baf553e 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -3879,6 +3879,8 @@ psql_completion(const char *text, int start, int end)
 			COMPLETE_WITH_CS("on", "off", "interactive");
 		else if (TailMatchesCS("SHOW_CONTEXT"))
 			COMPLETE_WITH_CS("never", "errors", "always");
+		else if (TailMatchesCS("STATUS_TARGET"))
+			COMPLETE_WITH_CS("result", "stdout");
 		else if (TailMatchesCS("VERBOSITY"))
 			COMPLETE_WITH_CS("default", "verbose", "terse", "sqlstate");
 	}
#3Erik Rijkers
er@xs4all.nl
In reply to: Pavel Stehule (#2)
Re: proposal - psql - possibility to redirect only tabular output

On 2020-04-11 10:21, Pavel Stehule wrote:

so 11. 4. 2020 v 8:54 odesílatel Pavel Stehule
<pavel.stehule@gmail.com>
napsal:

[psql-status-target.patch]

Hi Pavel,

This looks interesting, and I built an instance with the patch to try
it, but I can't figure out how to use it.

Can you perhaps give a few or even just one example?

thanks!

Erik Rijkers

#4Pavel Stehule
pavel.stehule@gmail.com
In reply to: Erik Rijkers (#3)
Re: proposal - psql - possibility to redirect only tabular output

so 11. 4. 2020 v 11:04 odesílatel Erik Rijkers <er@xs4all.nl> napsal:

On 2020-04-11 10:21, Pavel Stehule wrote:

so 11. 4. 2020 v 8:54 odesílatel Pavel Stehule
<pavel.stehule@gmail.com>
napsal:

[psql-status-target.patch]

Hi Pavel,

This looks interesting, and I built an instance with the patch to try
it, but I can't figure out how to use it.

Can you perhaps give a few or even just one example?

Main motivation for this patch is working with psql for writing and editing
queries, and browsing result in second terminal with pspg or any other
similar tool (tail, ...). The advantage of this setup is possibility to see
sql and query result together. I use terminal multiplicator (Tilix), but it
can be used without it.

So example with pspg (should be some fresh release)

1. create fifo used for communication - mkfifo ~/pipe

2. run in one terminal pspg - pspg -f ~/pipe --hold-stream=2

3. run psql in other terminal

psql
\o ~/pipe
CREATE TABLE foo(a int);
INSERT INTO foo VALUES(10);
-- in default case, the status row "CREATE", "INSERT" is redirected to
"browser terminal" and it doesn't look well (and it is not user friendly).

with patched version you can

\set STATUS_TARGET stdout
-- after this setting, the status row will be displayed in psql terminal

Regards

Pavel

Show quoted text

thanks!

Erik Rijkers

#5Artur Zakirov
zaartur@gmail.com
In reply to: Pavel Stehule (#4)
Re: proposal - psql - possibility to redirect only tabular output

Hello,

On Sat, Apr 11, 2020 at 6:20 PM Pavel Stehule <pavel.stehule@gmail.com> wrote:

Main motivation for this patch is working with psql for writing and editing queries, and browsing result in second terminal with pspg or any other similar tool (tail, ...). The advantage of this setup is possibility to see sql and query result together. I use terminal multiplicator (Tilix), but it can be used without it.

So example with pspg (should be some fresh release)

1. create fifo used for communication - mkfifo ~/pipe

2. run in one terminal pspg - pspg -f ~/pipe --hold-stream=2

3. run psql in other terminal

The patch looks interesting. As far as I understand the purpose of the
patch is to hide status messages from result output.
So maybe it would be enough just to hide status messages at all. There
is the QUIET variable for that. The main advantage of this variable is
that it hides a status of "\lo_" commands, for example, as well as a
status of utility commands. So the QUIET variable covers more use
cases already.

--
Artur

#6Pavel Stehule
pavel.stehule@gmail.com
In reply to: Artur Zakirov (#5)
Re: proposal - psql - possibility to redirect only tabular output

pá 3. 7. 2020 v 12:16 odesílatel Artur Zakirov <zaartur@gmail.com> napsal:

Hello,

On Sat, Apr 11, 2020 at 6:20 PM Pavel Stehule <pavel.stehule@gmail.com>
wrote:

Main motivation for this patch is working with psql for writing and

editing queries, and browsing result in second terminal with pspg or any
other similar tool (tail, ...). The advantage of this setup is possibility
to see sql and query result together. I use terminal multiplicator (Tilix),
but it can be used without it.

So example with pspg (should be some fresh release)

1. create fifo used for communication - mkfifo ~/pipe

2. run in one terminal pspg - pspg -f ~/pipe --hold-stream=2

3. run psql in other terminal

The patch looks interesting. As far as I understand the purpose of the
patch is to hide status messages from result output.
So maybe it would be enough just to hide status messages at all. There
is the QUIET variable for that. The main advantage of this variable is
that it hides a status of "\lo_" commands, for example, as well as a
status of utility commands. So the QUIET variable covers more use
cases already.

The quiet mode isn't exactly what I want (it can be used as a workaround -
and now, pspg https://github.com/okbob/pspg knows a format of status line
and can work it).

I would like to see a status row. For me it is a visual check so some
statements like INSERT or UPDATE was done successfully. But I would not
send it to the terminal with an active tabular pager.

Pavel

Show quoted text

--
Artur

#7David Steele
david@pgmasters.net
In reply to: Pavel Stehule (#6)
Re: proposal - psql - possibility to redirect only tabular output

On 7/3/20 11:27 PM, Pavel Stehule wrote:

pá 3. 7. 2020 v 12:16 odesílatel Artur Zakirov <zaartur@gmail.com
<mailto:zaartur@gmail.com>> napsal:

The patch looks interesting. As far as I understand the purpose of the
patch is to hide status messages from result output.
So maybe it would be enough just to hide status messages at all. There
is the QUIET variable for that. The main advantage of this variable is
that it hides a status of "\lo_" commands, for example, as well as a
status of utility commands. So the QUIET variable covers more use
cases already.

The quiet mode isn't exactly what I want (it can be used as a workaround
- and now, pspg https://github.com/okbob/pspg
<https://github.com/okbob/pspg&gt; knows a format of status line and can
work it).

I would like to see a status row. For me it is a visual check so some
statements like INSERT or UPDATE was done successfully. But I would not
send it to the terminal with an active tabular pager.

It's been quite a while since the patch has seen any review. It's not
clear that this is enough of an improvement over QUIET to be worthwhile.

I think it would be best to close this patch at the end of the CF if
there is no further reviewer/committer interest.

Regards,
--
-David
david@pgmasters.net

#8Pavel Stehule
pavel.stehule@gmail.com
In reply to: David Steele (#7)
Re: proposal - psql - possibility to redirect only tabular output

st 3. 3. 2021 v 17:58 odesílatel David Steele <david@pgmasters.net> napsal:

On 7/3/20 11:27 PM, Pavel Stehule wrote:

pá 3. 7. 2020 v 12:16 odesílatel Artur Zakirov <zaartur@gmail.com
<mailto:zaartur@gmail.com>> napsal:

The patch looks interesting. As far as I understand the purpose of

the

patch is to hide status messages from result output.
So maybe it would be enough just to hide status messages at all.

There

is the QUIET variable for that. The main advantage of this variable

is

that it hides a status of "\lo_" commands, for example, as well as a
status of utility commands. So the QUIET variable covers more use
cases already.

The quiet mode isn't exactly what I want (it can be used as a workaround
- and now, pspg https://github.com/okbob/pspg
<https://github.com/okbob/pspg&gt; knows a format of status line and can
work it).

I would like to see a status row. For me it is a visual check so some
statements like INSERT or UPDATE was done successfully. But I would not
send it to the terminal with an active tabular pager.

It's been quite a while since the patch has seen any review. It's not
clear that this is enough of an improvement over QUIET to be worthwhile.

I think it would be best to close this patch at the end of the CF if
there is no further reviewer/committer interest.

ok

Thank you

Pavel

Show quoted text

Regards,
--
-David
david@pgmasters.net