psql: show only failed queries

Started by Pavel Stehulealmost 12 years ago38 messages
#1Pavel Stehule
pavel.stehule@gmail.com
1 attachment(s)

Hello

I was asked, how can be showed only failed queries in psql.

I am thinking, so it is not possible now. But implementation is very simple

What do you think about it?

bash-4.1$ psql postgres -v ECHO=error -f data.sql
INSERT 0 1
Time: 27.735 ms
INSERT 0 1
Time: 8.303 ms
psql:data.sql:3: ERROR: value too long for type character varying(2)
insert into foo values('bbb');
Time: 0.178 ms
INSERT 0 1
Time: 8.285 ms
psql:data.sql:5: ERROR: value too long for type character varying(2)
insert into foo values('ssssss');
Time: 0.422 ms

Regards

Pavel

Attachments:

echo-error.patchtext/x-patch; charset=US-ASCII; name=echo-error.patchDownload
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index 3a820fa..8354f60 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -958,6 +958,12 @@ SendQuery(const char *query)
 		results = NULL;			/* PQclear(NULL) does nothing */
 	}
 
+	if (!OK && pset.echo == PSQL_ECHO_ERROR_QUERIES)
+	{
+		puts(query);
+		fflush(stdout);
+	}
+
 	/* If we made a temporary savepoint, possibly release/rollback */
 	if (on_error_rollback_savepoint)
 	{
diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h
index 3e8328d..ed80179 100644
--- a/src/bin/psql/settings.h
+++ b/src/bin/psql/settings.h
@@ -30,6 +30,7 @@
 typedef enum
 {
 	PSQL_ECHO_NONE,
+	PSQL_ECHO_ERROR_QUERIES,
 	PSQL_ECHO_QUERIES,
 	PSQL_ECHO_ALL
 } PSQL_ECHO;
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 1061992..666261f 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -712,6 +712,8 @@ echo_hook(const char *newval)
 {
 	if (newval == NULL)
 		pset.echo = PSQL_ECHO_NONE;
+	else if (strcmp(newval, "error") == 0)
+		pset.echo = PSQL_ECHO_ERROR_QUERIES;
 	else if (strcmp(newval, "queries") == 0)
 		pset.echo = PSQL_ECHO_QUERIES;
 	else if (strcmp(newval, "all") == 0)
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 1d69b95..641cff3 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -3397,6 +3397,23 @@ psql_completion(char *text, int start, int end)
 	{
 		matches = complete_from_variables(text, "", "");
 	}
+	else if (strcmp(prev2_wd, "\\set") == 0)
+	{
+		if (strcmp(prev_wd, "ECHO") == 0)
+		{
+			static const char *const my_list[] =
+			{"none", "error", "queries", "all", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "ECHO_HIDDEN") == 0)
+		{
+			static const char *const my_list[] =
+			{"noexec", "off", "on", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+	}
 	else if (strcmp(prev_wd, "\\sf") == 0 || strcmp(prev_wd, "\\sf+") == 0)
 		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
 	else if (strcmp(prev_wd, "\\cd") == 0 ||
#2Fabrízio de Royes Mello
fabriziomello@gmail.com
In reply to: Pavel Stehule (#1)
Re: psql: show only failed queries

On Sat, Mar 1, 2014 at 8:01 AM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

Hello

I was asked, how can be showed only failed queries in psql.

I am thinking, so it is not possible now. But implementation is very

simple

What do you think about it?

bash-4.1$ psql postgres -v ECHO=error -f data.sql
INSERT 0 1
Time: 27.735 ms
INSERT 0 1
Time: 8.303 ms
psql:data.sql:3: ERROR: value too long for type character varying(2)
insert into foo values('bbb');
Time: 0.178 ms
INSERT 0 1
Time: 8.285 ms
psql:data.sql:5: ERROR: value too long for type character varying(2)
insert into foo values('ssssss');
Time: 0.422 ms

The patch works fine, but I think we must add some prefix to printed query.
Like that:

fabrizio=# \set ECHO error
fabrizio=# insert into foo values ('XXX');
ERROR: value too long for type character varying(2)
DETAIL: insert into foo values ('XXX');

or

fabrizio=# \set ECHO error
fabrizio=# insert into foo values ('XXX');
ERROR: value too long for type character varying(2)
QUERY: insert into foo values ('XXX');

This may help to filter the output with some tool like 'grep'.

Regards,

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL

Show quoted text

Timbira: http://www.timbira.com.br
Blog sobre TI: http://fabriziomello.blogspot.com
Perfil Linkedin: http://br.linkedin.com/in/fabriziomello
Twitter: http://twitter.com/fabriziomello

#3Pavel Stehule
pavel.stehule@gmail.com
In reply to: Fabrízio de Royes Mello (#2)
Re: psql: show only failed queries

2014-03-04 6:35 GMT+01:00 Fabrízio de Royes Mello <fabriziomello@gmail.com>:

On Sat, Mar 1, 2014 at 8:01 AM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

Hello

I was asked, how can be showed only failed queries in psql.

I am thinking, so it is not possible now. But implementation is very

simple

What do you think about it?

bash-4.1$ psql postgres -v ECHO=error -f data.sql
INSERT 0 1
Time: 27.735 ms
INSERT 0 1
Time: 8.303 ms
psql:data.sql:3: ERROR: value too long for type character varying(2)
insert into foo values('bbb');
Time: 0.178 ms
INSERT 0 1
Time: 8.285 ms
psql:data.sql:5: ERROR: value too long for type character varying(2)
insert into foo values('ssssss');
Time: 0.422 ms

The patch works fine, but I think we must add some prefix to printed
query. Like that:

fabrizio=# \set ECHO error
fabrizio=# insert into foo values ('XXX');

ERROR: value too long for type character varying(2)
DETAIL: insert into foo values ('XXX');

or

fabrizio=# \set ECHO error
fabrizio=# insert into foo values ('XXX');

ERROR: value too long for type character varying(2)
QUERY: insert into foo values ('XXX');

This may help to filter the output with some tool like 'grep'.

sure, good idea.

I add link to your notice to commitfest app

Regards

Pavel

Show quoted text

Regards,

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL

Timbira: http://www.timbira.com.br
Blog sobre TI: http://fabriziomello.blogspot.com
Perfil Linkedin: http://br.linkedin.com/in/fabriziomello
Twitter: http://twitter.com/fabriziomello

#4Pavel Stehule
pavel.stehule@gmail.com
In reply to: Pavel Stehule (#3)
1 attachment(s)
Re: psql: show only failed queries

Hello

updated patch - only one change: query is prefixed by "QUERY: "

current state:

[pavel@localhost ~]$ src/postgresql/src/bin/psql/psql postgres -q -f
data.sql
psql:data.sql:6: ERROR: value too long for type character varying(3)

Show only errors mode:

[pavel@localhost ~]$ src/postgresql/src/bin/psql/psql postgres -q -v
ECHO=error -f data.sql
psql:data.sql:6: ERROR: value too long for type character varying(3)
QUERY: INSERT INTO bubu VALUES('Ahoj');

Now, when I am thinking about these results, I am thinking, so second
variant is more practical and can be default.

Opinions, notes?

Regards

Pavel

2014-03-04 8:52 GMT+01:00 Pavel Stehule <pavel.stehule@gmail.com>:

Show quoted text

2014-03-04 6:35 GMT+01:00 Fabrízio de Royes Mello <fabriziomello@gmail.com

:

On Sat, Mar 1, 2014 at 8:01 AM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

Hello

I was asked, how can be showed only failed queries in psql.

I am thinking, so it is not possible now. But implementation is very

simple

What do you think about it?

bash-4.1$ psql postgres -v ECHO=error -f data.sql
INSERT 0 1
Time: 27.735 ms
INSERT 0 1
Time: 8.303 ms
psql:data.sql:3: ERROR: value too long for type character varying(2)
insert into foo values('bbb');
Time: 0.178 ms
INSERT 0 1
Time: 8.285 ms
psql:data.sql:5: ERROR: value too long for type character varying(2)
insert into foo values('ssssss');
Time: 0.422 ms

The patch works fine, but I think we must add some prefix to printed
query. Like that:

fabrizio=# \set ECHO error
fabrizio=# insert into foo values ('XXX');

ERROR: value too long for type character varying(2)
DETAIL: insert into foo values ('XXX');

or

fabrizio=# \set ECHO error
fabrizio=# insert into foo values ('XXX');

ERROR: value too long for type character varying(2)
QUERY: insert into foo values ('XXX');

This may help to filter the output with some tool like 'grep'.

sure, good idea.

I add link to your notice to commitfest app

Regards

Pavel

Regards,

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL

Timbira: http://www.timbira.com.br
Blog sobre TI: http://fabriziomello.blogspot.com
Perfil Linkedin: http://br.linkedin.com/in/fabriziomello
Twitter: http://twitter.com/fabriziomello

Attachments:

echo_error.patchtext/x-patch; charset=US-ASCII; name=echo_error.patchDownload
commit 47b8944a5d3afb6763096bdd993e256ecae6691d
Author: Pavel Stehule <pavel.stehule@gooddata.com>
Date:   Wed Jun 4 17:44:54 2014 +0200

    initial

diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index ee6ec3a..cf0e78b 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -2812,7 +2812,8 @@ bar
         <literal>queries</literal>,
         <application>psql</application> merely prints all queries as
         they are sent to the server. The switch for this is
-        <option>-e</option>.
+        <option>-e</option>. If set to <literal>error</literal> then only
+        failed queries are displayed.
         </para>
         </listitem>
       </varlistentry>
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index 60169a2..5c94c03 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -995,6 +995,12 @@ SendQuery(const char *query)
 		results = NULL;			/* PQclear(NULL) does nothing */
 	}
 
+	if (!OK && pset.echo == PSQL_ECHO_ERROR)
+	{
+		printf("QUERY:  %s\n", query);
+		fflush(stdout);
+	}
+
 	/* If we made a temporary savepoint, possibly release/rollback */
 	if (on_error_rollback_savepoint)
 	{
diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h
index 0a60e68..e4a18f0 100644
--- a/src/bin/psql/settings.h
+++ b/src/bin/psql/settings.h
@@ -31,6 +31,7 @@ typedef enum
 {
 	PSQL_ECHO_NONE,
 	PSQL_ECHO_QUERIES,
+	PSQL_ECHO_ERROR,
 	PSQL_ECHO_ALL
 } PSQL_ECHO;
 
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 45653a1..b59bd59 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -720,6 +720,8 @@ echo_hook(const char *newval)
 		pset.echo = PSQL_ECHO_NONE;
 	else if (strcmp(newval, "queries") == 0)
 		pset.echo = PSQL_ECHO_QUERIES;
+	else if (strcmp(newval, "error") == 0)
+		pset.echo = PSQL_ECHO_ERROR;
 	else if (strcmp(newval, "all") == 0)
 		pset.echo = PSQL_ECHO_ALL;
 	else
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 3bb727f..e5e1558 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -3479,6 +3479,23 @@ psql_completion(const char *text, int start, int end)
 	{
 		matches = complete_from_variables(text, "", "");
 	}
+	else if (strcmp(prev2_wd, "\\set") == 0)
+	{
+		if (strcmp(prev_wd, "ECHO") == 0)
+		{
+			static const char *const my_list[] =
+			{"none", "error", "queries", "all", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "ECHO_HIDDEN") == 0)
+		{
+			static const char *const my_list[] =
+			{"noexec", "off", "on", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+	}
 	else if (strcmp(prev_wd, "\\sf") == 0 || strcmp(prev_wd, "\\sf+") == 0)
 		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
 	else if (strcmp(prev_wd, "\\cd") == 0 ||
#5Peter Eisentraut
peter_e@gmx.net
In reply to: Pavel Stehule (#4)
Re: psql: show only failed queries

On 6/4/14, 11:54 AM, Pavel Stehule wrote:

updated patch - only one change: query is prefixed by "QUERY: "

In the backend server log, this is called "STATEMENT: ".

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#6Pavel Stehule
pavel.stehule@gmail.com
In reply to: Peter Eisentraut (#5)
1 attachment(s)
Re: psql: show only failed queries

2014-06-04 18:16 GMT+02:00 Peter Eisentraut <peter_e@gmx.net>:

On 6/4/14, 11:54 AM, Pavel Stehule wrote:

updated patch - only one change: query is prefixed by "QUERY: "

In the backend server log, this is called "STATEMENT: ".

good idea

updated patch

Pavel

Attachments:

echo_error.patchtext/x-patch; charset=US-ASCII; name=echo_error.patchDownload
commit f752566830032438739b7e5ab1f55149c737e6d8
Author: Pavel Stehule <pavel.stehule@gooddata.com>
Date:   Wed Jun 4 17:44:54 2014 +0200

    initial

diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index ee6ec3a..cf0e78b 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -2812,7 +2812,8 @@ bar
         <literal>queries</literal>,
         <application>psql</application> merely prints all queries as
         they are sent to the server. The switch for this is
-        <option>-e</option>.
+        <option>-e</option>. If set to <literal>error</literal> then only
+        failed queries are displayed.
         </para>
         </listitem>
       </varlistentry>
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index 60169a2..fed3ee5 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -995,6 +995,12 @@ SendQuery(const char *query)
 		results = NULL;			/* PQclear(NULL) does nothing */
 	}
 
+	if (!OK && pset.echo == PSQL_ECHO_ERROR)
+	{
+		printf("STATEMENT:  %s\n", query);
+		fflush(stdout);
+	}
+
 	/* If we made a temporary savepoint, possibly release/rollback */
 	if (on_error_rollback_savepoint)
 	{
diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h
index 0a60e68..e4a18f0 100644
--- a/src/bin/psql/settings.h
+++ b/src/bin/psql/settings.h
@@ -31,6 +31,7 @@ typedef enum
 {
 	PSQL_ECHO_NONE,
 	PSQL_ECHO_QUERIES,
+	PSQL_ECHO_ERROR,
 	PSQL_ECHO_ALL
 } PSQL_ECHO;
 
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 45653a1..b59bd59 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -720,6 +720,8 @@ echo_hook(const char *newval)
 		pset.echo = PSQL_ECHO_NONE;
 	else if (strcmp(newval, "queries") == 0)
 		pset.echo = PSQL_ECHO_QUERIES;
+	else if (strcmp(newval, "error") == 0)
+		pset.echo = PSQL_ECHO_ERROR;
 	else if (strcmp(newval, "all") == 0)
 		pset.echo = PSQL_ECHO_ALL;
 	else
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 3bb727f..e5e1558 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -3479,6 +3479,23 @@ psql_completion(const char *text, int start, int end)
 	{
 		matches = complete_from_variables(text, "", "");
 	}
+	else if (strcmp(prev2_wd, "\\set") == 0)
+	{
+		if (strcmp(prev_wd, "ECHO") == 0)
+		{
+			static const char *const my_list[] =
+			{"none", "error", "queries", "all", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "ECHO_HIDDEN") == 0)
+		{
+			static const char *const my_list[] =
+			{"noexec", "off", "on", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+	}
 	else if (strcmp(prev_wd, "\\sf") == 0 || strcmp(prev_wd, "\\sf+") == 0)
 		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
 	else if (strcmp(prev_wd, "\\cd") == 0 ||
#7Samrat Revagade
revagade.samrat@gmail.com
In reply to: Pavel Stehule (#6)
Re: psql: show only failed queries

Hi Pavel,

After applying patch, on error condition it displays error message two
times as follows:

ERROR: column "abc" does not exist at character 23
STATEMENT: insert into ax
values(abc);
psql:a.sql:7: ERROR: column "abc" does not exist
LINE 2: values(abc);

user may confuse because of repeated error messages. so I think its better
to display only one message, one of the possible ways is as follows:

ERROR: column "abc" does not exist at character 23
STATEMENT: insert into ax
values(abc);

Am I missing something ?

On Wed, Jun 4, 2014 at 9:52 PM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

2014-06-04 18:16 GMT+02:00 Peter Eisentraut <peter_e@gmx.net>:

On 6/4/14, 11:54 AM, Pavel Stehule wrote:

updated patch - only one change: query is prefixed by "QUERY: "

In the backend server log, this is called "STATEMENT: ".

good idea

updated patch

Pavel

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

--
Regards,

Samrat Revgade

#8Pavel Stehule
pavel.stehule@gmail.com
In reply to: Samrat Revagade (#7)
1 attachment(s)
Re: psql: show only failed queries

2014-06-25 12:32 GMT+02:00 Samrat Revagade <revagade.samrat@gmail.com>:

Hi Pavel,

After applying patch, on error condition it displays error message two
times as follows:

ERROR: column "abc" does not exist at character 23
STATEMENT: insert into ax
values(abc);
psql:a.sql:7: ERROR: column "abc" does not exist
LINE 2: values(abc);

user may confuse because of repeated error messages. so I think its better
to display only one message, one of the possible ways is as follows:

ERROR: column "abc" does not exist at character 23
STATEMENT: insert into ax
values(abc);

Am I missing something ?

LINE info is a part of error message and should be eliminated by terse mode

[pavel@localhost ~]$ psql -v ECHO=error -f test.sql postgres > /dev/null
psql:test.sql:4: ERROR: syntax error at or near ";"
LINE 2: 10 + ;
^
psql:test.sql:4: STATEMENT: select
10 + ;
psql:test.sql:8: ERROR: syntax error at end of input
LINE 2: 30 +
^
psql:test.sql:8: STATEMENT: select
30 +

but you can switch to terse mode:

[pavel@localhost ~]$ psql -v ECHO=error -v VERBOSITY=terse -f test.sql
postgres > /dev/null
psql:test.sql:4: ERROR: syntax error at or near ";" at character 13
psql:test.sql:4: STATEMENT: select
10 + ;
psql:test.sql:8: ERROR: syntax error at end of input at character 13
psql:test.sql:8: STATEMENT: select
30 +

What is what you would

I am sending updated patch - buggy statement is printed via more logical
psql_error function instead printf

Regards

Pavel

Show quoted text

On Wed, Jun 4, 2014 at 9:52 PM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

2014-06-04 18:16 GMT+02:00 Peter Eisentraut <peter_e@gmx.net>:

On 6/4/14, 11:54 AM, Pavel Stehule wrote:

updated patch - only one change: query is prefixed by "QUERY: "

In the backend server log, this is called "STATEMENT: ".

good idea

updated patch

Pavel

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

--
Regards,

Samrat Revgade

Attachments:

echo-error-01.patchtext/x-patch; charset=US-ASCII; name=echo-error-01.patchDownload
commit b269613e0b261a7a5cfea35594299a0dd093682d
Author: Pavel Stehule <pavel.stehule@gooddata.com>
Date:   Wed Jun 25 20:45:30 2014 +0200

    initial

diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index ee6ec3a..cf0e78b 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -2812,7 +2812,8 @@ bar
         <literal>queries</literal>,
         <application>psql</application> merely prints all queries as
         they are sent to the server. The switch for this is
-        <option>-e</option>.
+        <option>-e</option>. If set to <literal>error</literal> then only
+        failed queries are displayed.
         </para>
         </listitem>
       </varlistentry>
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index 60169a2..69860d7 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -995,6 +995,9 @@ SendQuery(const char *query)
 		results = NULL;			/* PQclear(NULL) does nothing */
 	}
 
+	if (!OK && pset.echo == PSQL_ECHO_ERROR)
+		psql_error("STATEMENT:  %s\n", query);
+
 	/* If we made a temporary savepoint, possibly release/rollback */
 	if (on_error_rollback_savepoint)
 	{
diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h
index 0a60e68..e4a18f0 100644
--- a/src/bin/psql/settings.h
+++ b/src/bin/psql/settings.h
@@ -31,6 +31,7 @@ typedef enum
 {
 	PSQL_ECHO_NONE,
 	PSQL_ECHO_QUERIES,
+	PSQL_ECHO_ERROR,
 	PSQL_ECHO_ALL
 } PSQL_ECHO;
 
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 45653a1..b59bd59 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -720,6 +720,8 @@ echo_hook(const char *newval)
 		pset.echo = PSQL_ECHO_NONE;
 	else if (strcmp(newval, "queries") == 0)
 		pset.echo = PSQL_ECHO_QUERIES;
+	else if (strcmp(newval, "error") == 0)
+		pset.echo = PSQL_ECHO_ERROR;
 	else if (strcmp(newval, "all") == 0)
 		pset.echo = PSQL_ECHO_ALL;
 	else
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index be5c3c5..8611dd2 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -3591,6 +3591,23 @@ psql_completion(const char *text, int start, int end)
 	{
 		matches = complete_from_variables(text, "", "");
 	}
+	else if (strcmp(prev2_wd, "\\set") == 0)
+	{
+		if (strcmp(prev_wd, "ECHO") == 0)
+		{
+			static const char *const my_list[] =
+			{"none", "error", "queries", "all", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "ECHO_HIDDEN") == 0)
+		{
+			static const char *const my_list[] =
+			{"noexec", "off", "on", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+	}
 	else if (strcmp(prev_wd, "\\sf") == 0 || strcmp(prev_wd, "\\sf+") == 0)
 		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
 	else if (strcmp(prev_wd, "\\cd") == 0 ||
#9Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Pavel Stehule (#8)
Re: psql: show only failed queries

ECHO_HIDDEN?

--
�lvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#10Samrat Revagade
revagade.samrat@gmail.com
In reply to: Pavel Stehule (#8)
Re: psql: show only failed queries

I am sending updated patch - buggy statement is printed via more logical

psql_error function instead printf

Thank you for updating patch, I really appreciate your efforts.

Now, everything is good from my side.
* it apply cleanly to the current git master
* includes necessary docs
* I think It is very good and necessary feature.

If Kumar Rajeev Rastogi do not have any extra comments, then I think patch
is ready for committer.

#11Pavel Stehule
pavel.stehule@gmail.com
In reply to: Samrat Revagade (#10)
Re: psql: show only failed queries

2014-06-26 8:22 GMT+02:00 Samrat Revagade <revagade.samrat@gmail.com>:

I am sending updated patch - buggy statement is printed via more logical

psql_error function instead printf

Thank you for updating patch, I really appreciate your efforts.

Now, everything is good from my side.
* it apply cleanly to the current git master
* includes necessary docs
* I think It is very good and necessary feature.

If Kumar Rajeev Rastogi do not have any extra comments, then I think patch
is ready for committer.

Thank you very much

Regards

Pavel

#12Rajeev rastogi
rajeev.rastogi@huawei.com
In reply to: Samrat Revagade (#10)
Re: psql: show only failed queries

On 26 June 2014 11:53, Samrat Revagade Wrote:

I am sending updated patch - buggy statement is printed via more logical psql_error function instead printf

Thank you for updating patch, I really appreciate your efforts.

Now, everything is good from my side.
* it apply cleanly to the current git master
* includes necessary docs
* I think It is very good and necessary feature.

If Kumar Rajeev Rastogi do not have any extra comments, then I think patch is ready for committer.

I have reviewed this patch. Please find my review comments below:

1. Command start-up option (e.g. -a/--echo-all for --ECHO=all), for new functionality is not provided.

2. New Command start-up option should be added in "psql --help" as well as in documentation.

Also as I understand, this new option is kind of sub-set of existing option (ECHO=query), so should not we display
query string in the same format as it was getting printed earlier.
Though I also feel that prefixing query with STATEMENT word will be helpful to grep but at the same time I am worried
about inconsistency with existing option.

Thanks and Regards,
Kumar Rajeev Rastogi

#13Pavel Stehule
pavel.stehule@gmail.com
In reply to: Rajeev rastogi (#12)
Re: psql: show only failed queries

2014-06-30 8:17 GMT+02:00 Rajeev rastogi <rajeev.rastogi@huawei.com>:

On 26 June 2014 11:53, Samrat Revagade Wrote:

I am sending updated patch - buggy statement is printed via more

logical psql_error function instead printf

Thank you for updating patch, I really appreciate your efforts.

Now, everything is good from my side.

* it apply cleanly to the current git master

* includes necessary docs

* I think It is very good and necessary feature.

If Kumar Rajeev Rastogi do not have any extra comments, then I

think patch is ready for committer.

I have reviewed this patch. Please find my review comments below:

1. Command start-up option (e.g. -a/--echo-all for --ECHO=all), for
new functionality is not provided.

all not options entered via psql variables has psql option and psql
comment. I'll plan add new decription to --help-variables list.

If it is necessary I can add long option --echo-errors, I didn't a good
char for short option. Any idea?

2. New Command start-up option should be added in "psql --help" as
well as in documentation.

depends on previous,

Also as I understand, this new option is kind of sub-set of existing
option (ECHO=query), so should not we display

query string in the same format as it was getting printed earlier.

Though I also feel that prefixing query with STATEMENT word will be
helpful to grep but at the same time I am worried

about inconsistency with existing option.

This is question. And I am not strong in feeling what should be preferred.
But still I am inclined to prefer a variant with STATEMENT prefix. Mode
with -a is used with different purpose than mode "show errors only" - and
output with prefix is much more consistent with log entry - and displaying
error. So I agree, so there is potential inconsistency (but nowhere is
output defined), but this output is more practical, when you are
concentrated to error's processing.

Regards

Pavel

Show quoted text

*Thanks and Regards,*

*Kumar Rajeev Rastogi *

#14Rajeev rastogi
rajeev.rastogi@huawei.com
In reply to: Pavel Stehule (#13)
Re: psql: show only failed queries

On 30 June 2014 12:24, Pavel Stehule Wrote:

I have reviewed this patch. Please find my review comments below:

1. Command start-up option (e.g. -a/--echo-all for --ECHO=all), for new functionality is not provided.

all not options entered via psql variables has psql option and psql comment. I'll plan add new decription to --help-variables list.
If it is necessary I can add long option --echo-errors, I didn't a good char for short option. Any idea?

But the new option we are adding are on a track of existing option, so better we add start-up option for this also.
Yeah long option –echo-errors seems to be fine to me also. For short option, I think we can use “-b” stands for blunder. This is the closest one I could think of.

2. New Command start-up option should be added in "psql --help" as well as in documentation.

depends on previous,

Right.

Also as I understand, this new option is kind of sub-set of existing option (ECHO=query), so should not we display
query string in the same format as it was getting printed earlier.
Though I also feel that prefixing query with STATEMENT word will be helpful to grep but at the same time I am worried
about inconsistency with existing option.

This is question. And I am not strong in feeling what should be preferred. But still I am inclined to prefer a variant with STATEMENT prefix. Mode with -a is used with different purpose than mode "show errors only" - and output with prefix is much
more consistent with log entry - and displaying error. So I agree, so there is potential inconsistency (but nowhere is output defined), but this output is more practical, when you are concentrated to error's processing.

Yeah right, I just wanted to raise point to provoke other thought to see if anyone having different opinion. If no objection from others, we can go ahead with the current prefixing approach.
Thanks and Regards,
Kumar Rajeev Rastogi

#15Pavel Stehule
pavel.stehule@gmail.com
In reply to: Rajeev rastogi (#14)
1 attachment(s)
Re: psql: show only failed queries

2014-06-30 11:20 GMT+02:00 Rajeev rastogi <rajeev.rastogi@huawei.com>:

On 30 June 2014 12:24, Pavel Stehule Wrote:

I have reviewed this patch. Please find my review comments below:

1. Command start-up option (e.g. -a/--echo-all for --ECHO=all), for

new functionality is not provided.

all not options entered via psql variables has psql option and psql

comment. I'll plan add new decription to --help-variables list.

If it is necessary I can add long option --echo-errors, I didn't a good

char for short option. Any idea?

But the new option we are adding are on a track of existing option, so
better we add start-up option for this also.

Yeah long option –echo-errors seems to be fine to me also. For short
option, I think we can use “-b” stands for blunder. This is the closest one
I could think of.

fixed

see a attachment pls

2. New Command start-up option should be added in "psql --help" as

well as in documentation.

depends on previous,

Right.

Also as I understand, this new option is kind of sub-set of existing

option (ECHO=query), so should not we display

query string in the same format as it was getting printed earlier.

Though I also feel that prefixing query with STATEMENT word will be

helpful to grep but at the same time I am worried

about inconsistency with existing option.

This is question. And I am not strong in feeling what should be

preferred. But still I am inclined to prefer a variant with STATEMENT
prefix. Mode with -a is used with different purpose than mode "show errors
only" - and output with prefix is much

more consistent with log entry - and displaying error. So I agree, so

there is potential inconsistency (but nowhere is output defined), but this
output is more practical, when you are concentrated to error's processing.

Yeah right, I just wanted to raise point to provoke other thought to see
if anyone having different opinion. If no objection from others, we can go
ahead with the current prefixing approach.

ok, we can wait two days

Regards

Pavel

Show quoted text

*Thanks and Regards,*

*Kumar Rajeev Rastogi*

Attachments:

echo-error-02.patchtext/x-patch; charset=US-ASCII; name=echo-error-02.patchDownload
commit e05f23c69db53cefa0c3438c5eaeec11e5fa05b0
Author: Pavel Stehule <pavel.stehule@gooddata.com>
Date:   Mon Jun 30 12:39:42 2014 +0200

    new -b, --echo-errors option

diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index ee6ec3a..ccaf1c3 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -73,6 +73,18 @@ PostgreSQL documentation
     </varlistentry>
 
     <varlistentry>
+      <term><option>-b</></term>
+      <term><option>--echo-errors</></term>
+      <listitem>
+      <para>
+      Print a failed SQL commands to standard error output. This is
+      equivalent to setting the variable <varname>ECHO</varname> to
+      <literal>errors</literal>.
+      </para>
+      </listitem>
+    </varlistentry>
+
+    <varlistentry>
       <term><option>-c <replaceable class="parameter">command</replaceable></></term>
       <term><option>--command=<replaceable class="parameter">command</replaceable></></term>
       <listitem>
@@ -2812,7 +2824,8 @@ bar
         <literal>queries</literal>,
         <application>psql</application> merely prints all queries as
         they are sent to the server. The switch for this is
-        <option>-e</option>.
+        <option>-e</option>. If set to <literal>error</literal> then only
+        failed queries are displayed.
         </para>
         </listitem>
       </varlistentry>
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index 60169a2..6551b15 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -995,6 +995,9 @@ SendQuery(const char *query)
 		results = NULL;			/* PQclear(NULL) does nothing */
 	}
 
+	if (!OK && pset.echo == PSQL_ECHO_ERRORS)
+		psql_error("STATEMENT:  %s\n", query);
+
 	/* If we made a temporary savepoint, possibly release/rollback */
 	if (on_error_rollback_savepoint)
 	{
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index 3aa3c16..0128060 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -87,6 +87,7 @@ usage(void)
 
 	printf(_("\nInput and output options:\n"));
 	printf(_("  -a, --echo-all           echo all input from script\n"));
+	printf(_("  -b  --echo-errors        echo failed commands sent to server\n"));
 	printf(_("  -e, --echo-queries       echo commands sent to server\n"));
 	printf(_("  -E, --echo-hidden        display queries that internal commands generate\n"));
 	printf(_("  -L, --log-file=FILENAME  send session log to file\n"));
diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h
index 0a60e68..453d6c8 100644
--- a/src/bin/psql/settings.h
+++ b/src/bin/psql/settings.h
@@ -31,6 +31,7 @@ typedef enum
 {
 	PSQL_ECHO_NONE,
 	PSQL_ECHO_QUERIES,
+	PSQL_ECHO_ERRORS,
 	PSQL_ECHO_ALL
 } PSQL_ECHO;
 
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 45653a1..3ca3f1e 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -354,6 +354,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
 		{"command", required_argument, NULL, 'c'},
 		{"dbname", required_argument, NULL, 'd'},
 		{"echo-queries", no_argument, NULL, 'e'},
+		{"echo-errors", no_argument, NULL, 'b'},
 		{"echo-hidden", no_argument, NULL, 'E'},
 		{"file", required_argument, NULL, 'f'},
 		{"field-separator", required_argument, NULL, 'F'},
@@ -402,6 +403,9 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
 			case 'A':
 				pset.popt.topt.format = PRINT_UNALIGNED;
 				break;
+			case 'b':
+				SetVariable(pset.vars, "ECHO", "errors");
+				break;
 			case 'c':
 				options->action_string = pg_strdup(optarg);
 				if (optarg[0] == '\\')
@@ -720,6 +724,8 @@ echo_hook(const char *newval)
 		pset.echo = PSQL_ECHO_NONE;
 	else if (strcmp(newval, "queries") == 0)
 		pset.echo = PSQL_ECHO_QUERIES;
+	else if (strcmp(newval, "errors") == 0)
+		pset.echo = PSQL_ECHO_ERRORS;
 	else if (strcmp(newval, "all") == 0)
 		pset.echo = PSQL_ECHO_ALL;
 	else
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index be5c3c5..8611dd2 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -3591,6 +3591,23 @@ psql_completion(const char *text, int start, int end)
 	{
 		matches = complete_from_variables(text, "", "");
 	}
+	else if (strcmp(prev2_wd, "\\set") == 0)
+	{
+		if (strcmp(prev_wd, "ECHO") == 0)
+		{
+			static const char *const my_list[] =
+			{"none", "errors", "queries", "all", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "ECHO_HIDDEN") == 0)
+		{
+			static const char *const my_list[] =
+			{"noexec", "off", "on", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+	}
 	else if (strcmp(prev_wd, "\\sf") == 0 || strcmp(prev_wd, "\\sf+") == 0)
 		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
 	else if (strcmp(prev_wd, "\\cd") == 0 ||
#16Abhijit Menon-Sen
ams@2ndQuadrant.com
In reply to: Pavel Stehule (#15)
Re: psql: show only failed queries

At 2014-06-30 12:48:30 +0200, pavel.stehule@gmail.com wrote:

+      <para>
+      Print a failed SQL commands to standard error output. This is
+      equivalent to setting the variable <varname>ECHO</varname> to
+      <literal>errors</literal>.

No "a", just "Print failed SQL commands …".

-        <option>-e</option>.
+        <option>-e</option>. If set to <literal>error</literal> then only
+        failed queries are displayed.

Should be "errors" here, not "error".

printf(_(" -a, --echo-all echo all input from script\n"));
+ printf(_(" -b --echo-errors echo failed commands sent to server\n"));
printf(_(" -e, --echo-queries echo commands sent to server\n"));

Should have a comma after -b to match other options. Also I would remove
"sent to server" from the description: "echo failed commands" is fine.

Otherwise looks fine. I see no reason to wait for further feedback, so
I'll mark this ready for committer if you make the above corrections.

At some point, you should probably also update your --help-variables
patch to add this new value to the description of ECHO.

-- Abhijit

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#17Pavel Stehule
pavel.stehule@gmail.com
In reply to: Abhijit Menon-Sen (#16)
1 attachment(s)
Re: psql: show only failed queries

2014-06-30 13:01 GMT+02:00 Abhijit Menon-Sen <ams@2ndquadrant.com>:

At 2014-06-30 12:48:30 +0200, pavel.stehule@gmail.com wrote:

+      <para>
+      Print a failed SQL commands to standard error output. This is
+      equivalent to setting the variable <varname>ECHO</varname> to
+      <literal>errors</literal>.

No "a", just "Print failed SQL commands …".

-        <option>-e</option>.
+        <option>-e</option>. If set to <literal>error</literal> then

only

+ failed queries are displayed.

Should be "errors" here, not "error".

printf(_(" -a, --echo-all echo all input from

script\n"));

+ printf(_(" -b --echo-errors echo failed commands sent to

server\n"));

printf(_(" -e, --echo-queries echo commands sent to

server\n"));

Should have a comma after -b to match other options. Also I would remove
"sent to server" from the description: "echo failed commands" is fine.

fixed

Otherwise looks fine. I see no reason to wait for further feedback, so
I'll mark this ready for committer if you make the above corrections.

At some point, you should probably also update your --help-variables
patch to add this new value to the description of ECHO.

I have it in TODO. But I don't would to introduce a dependency between
these patches - so when first patch will be committed, than I update second
patch

Regards

Pavel

Show quoted text

-- Abhijit

Attachments:

echo-error-03.patchtext/x-patch; charset=US-ASCII; name=echo-error-03.patchDownload
commit 998203a2e35643430059c4d3490a176a830cfee2
Author: Pavel Stehule <pavel.stehule@gooddata.com>
Date:   Mon Jun 30 12:39:42 2014 +0200

    new -b, --echo-errors option

diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index ee6ec3a..2c4cc96 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -73,6 +73,18 @@ PostgreSQL documentation
     </varlistentry>
 
     <varlistentry>
+      <term><option>-b</></term>
+      <term><option>--echo-errors</></term>
+      <listitem>
+      <para>
+      Print failed SQL commands to standard error output. This is
+      equivalent to setting the variable <varname>ECHO</varname> to
+      <literal>errors</literal>.
+      </para>
+      </listitem>
+    </varlistentry>
+
+    <varlistentry>
       <term><option>-c <replaceable class="parameter">command</replaceable></></term>
       <term><option>--command=<replaceable class="parameter">command</replaceable></></term>
       <listitem>
@@ -2812,7 +2824,8 @@ bar
         <literal>queries</literal>,
         <application>psql</application> merely prints all queries as
         they are sent to the server. The switch for this is
-        <option>-e</option>.
+        <option>-e</option>. If set to <literal>errors</literal> then only
+        failed queries are displayed.
         </para>
         </listitem>
       </varlistentry>
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index 60169a2..6551b15 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -995,6 +995,9 @@ SendQuery(const char *query)
 		results = NULL;			/* PQclear(NULL) does nothing */
 	}
 
+	if (!OK && pset.echo == PSQL_ECHO_ERRORS)
+		psql_error("STATEMENT:  %s\n", query);
+
 	/* If we made a temporary savepoint, possibly release/rollback */
 	if (on_error_rollback_savepoint)
 	{
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index 3aa3c16..f8f000f 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -87,6 +87,7 @@ usage(void)
 
 	printf(_("\nInput and output options:\n"));
 	printf(_("  -a, --echo-all           echo all input from script\n"));
+	printf(_("  -b, --echo-errors        echo failed commands\n"));
 	printf(_("  -e, --echo-queries       echo commands sent to server\n"));
 	printf(_("  -E, --echo-hidden        display queries that internal commands generate\n"));
 	printf(_("  -L, --log-file=FILENAME  send session log to file\n"));
diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h
index 0a60e68..453d6c8 100644
--- a/src/bin/psql/settings.h
+++ b/src/bin/psql/settings.h
@@ -31,6 +31,7 @@ typedef enum
 {
 	PSQL_ECHO_NONE,
 	PSQL_ECHO_QUERIES,
+	PSQL_ECHO_ERRORS,
 	PSQL_ECHO_ALL
 } PSQL_ECHO;
 
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 45653a1..3ca3f1e 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -354,6 +354,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
 		{"command", required_argument, NULL, 'c'},
 		{"dbname", required_argument, NULL, 'd'},
 		{"echo-queries", no_argument, NULL, 'e'},
+		{"echo-errors", no_argument, NULL, 'b'},
 		{"echo-hidden", no_argument, NULL, 'E'},
 		{"file", required_argument, NULL, 'f'},
 		{"field-separator", required_argument, NULL, 'F'},
@@ -402,6 +403,9 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
 			case 'A':
 				pset.popt.topt.format = PRINT_UNALIGNED;
 				break;
+			case 'b':
+				SetVariable(pset.vars, "ECHO", "errors");
+				break;
 			case 'c':
 				options->action_string = pg_strdup(optarg);
 				if (optarg[0] == '\\')
@@ -720,6 +724,8 @@ echo_hook(const char *newval)
 		pset.echo = PSQL_ECHO_NONE;
 	else if (strcmp(newval, "queries") == 0)
 		pset.echo = PSQL_ECHO_QUERIES;
+	else if (strcmp(newval, "errors") == 0)
+		pset.echo = PSQL_ECHO_ERRORS;
 	else if (strcmp(newval, "all") == 0)
 		pset.echo = PSQL_ECHO_ALL;
 	else
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index be5c3c5..8611dd2 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -3591,6 +3591,23 @@ psql_completion(const char *text, int start, int end)
 	{
 		matches = complete_from_variables(text, "", "");
 	}
+	else if (strcmp(prev2_wd, "\\set") == 0)
+	{
+		if (strcmp(prev_wd, "ECHO") == 0)
+		{
+			static const char *const my_list[] =
+			{"none", "errors", "queries", "all", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "ECHO_HIDDEN") == 0)
+		{
+			static const char *const my_list[] =
+			{"noexec", "off", "on", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+	}
 	else if (strcmp(prev_wd, "\\sf") == 0 || strcmp(prev_wd, "\\sf+") == 0)
 		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
 	else if (strcmp(prev_wd, "\\cd") == 0 ||
#18Fujii Masao
masao.fujii@gmail.com
In reply to: Pavel Stehule (#17)
Re: psql: show only failed queries

On Mon, Jun 30, 2014 at 8:33 PM, Pavel Stehule <pavel.stehule@gmail.com> wrote:

2014-06-30 13:01 GMT+02:00 Abhijit Menon-Sen <ams@2ndquadrant.com>:

At 2014-06-30 12:48:30 +0200, pavel.stehule@gmail.com wrote:

+      <para>
+      Print a failed SQL commands to standard error output. This is
+      equivalent to setting the variable <varname>ECHO</varname> to
+      <literal>errors</literal>.

No "a", just "Print failed SQL commands …".

-        <option>-e</option>.
+        <option>-e</option>. If set to <literal>error</literal> then
only
+        failed queries are displayed.

Should be "errors" here, not "error".

printf(_(" -a, --echo-all echo all input from
script\n"));
+ printf(_(" -b --echo-errors echo failed commands sent to
server\n"));
printf(_(" -e, --echo-queries echo commands sent to
server\n"));

Should have a comma after -b to match other options. Also I would remove
"sent to server" from the description: "echo failed commands" is fine.

fixed

$ psql -b
bin/psql: invalid option -- 'b'
Try "psql --help" for more information.

I got this error. ISTM you forgot to add 'b' into the third argument of
getopt_long in startup.c.

         <application>psql</application> merely prints all queries as
         they are sent to the server. The switch for this is
-        <option>-e</option>.
+        <option>-e</option>. If set to <literal>errors</literal> then only
+        failed queries are displayed.

I think that where failed queries are output should be documented here.
Otherwise users might misunderstand they are output to standard output
like ECHO=all and queries do.

It's better to add "The switch for this is <option>-b</option>." into the doc.

+    else if (strcmp(prev2_wd, "\\set") == 0)
+    {
+        if (strcmp(prev_wd, "ECHO") == 0)
+        {
+            static const char *const my_list[] =
+            {"none", "errors", "queries", "all", NULL};
+
+            COMPLETE_WITH_LIST_CS(my_list);
+        }
+        else if (strcmp(prev_wd, "ECHO_HIDDEN") == 0)
+        {
+            static const char *const my_list[] =
+            {"noexec", "off", "on", NULL};
+
+            COMPLETE_WITH_LIST_CS(my_list);
+        }
+    }

I think that adding tab-completions of psql variables is good, but
adding those of only ECHO and ECHO_HIDDEN seems half-baked.
Probably this part should be split into separate patch.

Regards,

--
Fujii Masao

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#19Pavel Stehule
pavel.stehule@gmail.com
In reply to: Fujii Masao (#18)
1 attachment(s)
Re: psql: show only failed queries

Hi

2014-07-09 7:07 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Mon, Jun 30, 2014 at 8:33 PM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

2014-06-30 13:01 GMT+02:00 Abhijit Menon-Sen <ams@2ndquadrant.com>:

At 2014-06-30 12:48:30 +0200, pavel.stehule@gmail.com wrote:

+      <para>
+      Print a failed SQL commands to standard error output. This is
+      equivalent to setting the variable <varname>ECHO</varname> to
+      <literal>errors</literal>.

No "a", just "Print failed SQL commands …".

-        <option>-e</option>.
+        <option>-e</option>. If set to <literal>error</literal> then
only
+        failed queries are displayed.

Should be "errors" here, not "error".

printf(_(" -a, --echo-all echo all input from
script\n"));
+ printf(_(" -b --echo-errors echo failed commands sent

to

server\n"));
printf(_(" -e, --echo-queries echo commands sent to
server\n"));

Should have a comma after -b to match other options. Also I would remove
"sent to server" from the description: "echo failed commands" is fine.

fixed

$ psql -b
bin/psql: invalid option -- 'b'
Try "psql --help" for more information.

I got this error. ISTM you forgot to add 'b' into the third argument of
getopt_long in startup.c.

fixed

<application>psql</application> merely prints all queries as
they are sent to the server. The switch for this is
-        <option>-e</option>.
+        <option>-e</option>. If set to <literal>errors</literal> then only
+        failed queries are displayed.

I think that where failed queries are output should be documented here.
Otherwise users might misunderstand they are output to standard output
like ECHO=all and queries do.

It's better to add "The switch for this is <option>-b</option>." into the
doc.

fixed

+    else if (strcmp(prev2_wd, "\\set") == 0)
+    {
+        if (strcmp(prev_wd, "ECHO") == 0)
+        {
+            static const char *const my_list[] =
+            {"none", "errors", "queries", "all", NULL};
+
+            COMPLETE_WITH_LIST_CS(my_list);
+        }
+        else if (strcmp(prev_wd, "ECHO_HIDDEN") == 0)
+        {
+            static const char *const my_list[] =
+            {"noexec", "off", "on", NULL};
+
+            COMPLETE_WITH_LIST_CS(my_list);
+        }
+    }

I think that adding tab-completions of psql variables is good, but
adding those of only ECHO and ECHO_HIDDEN seems half-baked.
Probably this part should be split into separate patch.

fixed

please, see updated patch in attachment

Thank you

Regards

Pavel

Show quoted text

Regards,

--
Fujii Masao

Attachments:

echo-error-04.patchtext/x-patch; charset=US-ASCII; name=echo-error-04.patchDownload
commit a05543802d000b3e66276b5ff370787d6f5ee7e3
Author: Pavel Stehule <pavel.stehule@gooddata.com>
Date:   Wed Jul 9 14:03:42 2014 +0200

    version 04

diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index 255e8ca..bbe5935 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -73,6 +73,18 @@ PostgreSQL documentation
     </varlistentry>
 
     <varlistentry>
+      <term><option>-b</></term>
+      <term><option>--echo-errors</></term>
+      <listitem>
+      <para>
+      Print failed SQL commands to standard error output. This is
+      equivalent to setting the variable <varname>ECHO</varname> to
+      <literal>errors</literal>.
+      </para>
+      </listitem>
+    </varlistentry>
+
+    <varlistentry>
       <term><option>-c <replaceable class="parameter">command</replaceable></></term>
       <term><option>--command=<replaceable class="parameter">command</replaceable></></term>
       <listitem>
@@ -2812,7 +2824,9 @@ bar
         <literal>queries</literal>,
         <application>psql</application> merely prints all queries as
         they are sent to the server. The switch for this is
-        <option>-e</option>.
+        <option>-e</option>. If set to <literal>errors</literal> then only
+        failed queries are displayed on standard error output. The switch
+        for this is <option>-b</option>.
         </para>
         </listitem>
       </varlistentry>
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index c08c813..676e268 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -995,6 +995,9 @@ SendQuery(const char *query)
 		results = NULL;			/* PQclear(NULL) does nothing */
 	}
 
+	if (!OK && pset.echo == PSQL_ECHO_ERRORS)
+		psql_error("STATEMENT:  %s\n", query);
+
 	/* If we made a temporary savepoint, possibly release/rollback */
 	if (on_error_rollback_savepoint)
 	{
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index 3aa3c16..f8f000f 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -87,6 +87,7 @@ usage(void)
 
 	printf(_("\nInput and output options:\n"));
 	printf(_("  -a, --echo-all           echo all input from script\n"));
+	printf(_("  -b, --echo-errors        echo failed commands\n"));
 	printf(_("  -e, --echo-queries       echo commands sent to server\n"));
 	printf(_("  -E, --echo-hidden        display queries that internal commands generate\n"));
 	printf(_("  -L, --log-file=FILENAME  send session log to file\n"));
diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h
index 0a60e68..453d6c8 100644
--- a/src/bin/psql/settings.h
+++ b/src/bin/psql/settings.h
@@ -31,6 +31,7 @@ typedef enum
 {
 	PSQL_ECHO_NONE,
 	PSQL_ECHO_QUERIES,
+	PSQL_ECHO_ERRORS,
 	PSQL_ECHO_ALL
 } PSQL_ECHO;
 
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 45653a1..5a397e8 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -354,6 +354,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
 		{"command", required_argument, NULL, 'c'},
 		{"dbname", required_argument, NULL, 'd'},
 		{"echo-queries", no_argument, NULL, 'e'},
+		{"echo-errors", no_argument, NULL, 'b'},
 		{"echo-hidden", no_argument, NULL, 'E'},
 		{"file", required_argument, NULL, 'f'},
 		{"field-separator", required_argument, NULL, 'F'},
@@ -391,7 +392,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
 
 	memset(options, 0, sizeof *options);
 
-	while ((c = getopt_long(argc, argv, "aAc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
+	while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
 							long_options, &optindex)) != -1)
 	{
 		switch (c)
@@ -402,6 +403,9 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
 			case 'A':
 				pset.popt.topt.format = PRINT_UNALIGNED;
 				break;
+			case 'b':
+				SetVariable(pset.vars, "ECHO", "errors");
+				break;
 			case 'c':
 				options->action_string = pg_strdup(optarg);
 				if (optarg[0] == '\\')
@@ -720,6 +724,8 @@ echo_hook(const char *newval)
 		pset.echo = PSQL_ECHO_NONE;
 	else if (strcmp(newval, "queries") == 0)
 		pset.echo = PSQL_ECHO_QUERIES;
+	else if (strcmp(newval, "errors") == 0)
+		pset.echo = PSQL_ECHO_ERRORS;
 	else if (strcmp(newval, "all") == 0)
 		pset.echo = PSQL_ECHO_ALL;
 	else
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index bab0357..482a368 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -3592,6 +3592,16 @@ psql_completion(const char *text, int start, int end)
 	{
 		matches = complete_from_variables(text, "", "");
 	}
+	else if (strcmp(prev2_wd, "\\set") == 0)
+	{
+		if (strcmp(prev_wd, "ECHO") == 0)
+		{
+			static const char *const my_list[] =
+			{"none", "errors", "queries", "all", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+	}
 	else if (strcmp(prev_wd, "\\sf") == 0 || strcmp(prev_wd, "\\sf+") == 0)
 		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
 	else if (strcmp(prev_wd, "\\cd") == 0 ||
#20Fujii Masao
masao.fujii@gmail.com
In reply to: Pavel Stehule (#19)
Re: psql: show only failed queries

On Wed, Jul 9, 2014 at 9:06 PM, Pavel Stehule <pavel.stehule@gmail.com> wrote:

Hi

2014-07-09 7:07 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Mon, Jun 30, 2014 at 8:33 PM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

2014-06-30 13:01 GMT+02:00 Abhijit Menon-Sen <ams@2ndquadrant.com>:

At 2014-06-30 12:48:30 +0200, pavel.stehule@gmail.com wrote:

+      <para>
+      Print a failed SQL commands to standard error output. This is
+      equivalent to setting the variable <varname>ECHO</varname> to
+      <literal>errors</literal>.

No "a", just "Print failed SQL commands …".

-        <option>-e</option>.
+        <option>-e</option>. If set to <literal>error</literal> then
only
+        failed queries are displayed.

Should be "errors" here, not "error".

printf(_(" -a, --echo-all echo all input from
script\n"));
+ printf(_(" -b --echo-errors echo failed commands sent
to
server\n"));
printf(_(" -e, --echo-queries echo commands sent to
server\n"));

Should have a comma after -b to match other options. Also I would
remove
"sent to server" from the description: "echo failed commands" is fine.

fixed

$ psql -b
bin/psql: invalid option -- 'b'
Try "psql --help" for more information.

I got this error. ISTM you forgot to add 'b' into the third argument of
getopt_long in startup.c.

fixed

<application>psql</application> merely prints all queries as
they are sent to the server. The switch for this is
-        <option>-e</option>.
+        <option>-e</option>. If set to <literal>errors</literal> then
only
+        failed queries are displayed.

I think that where failed queries are output should be documented here.
Otherwise users might misunderstand they are output to standard output
like ECHO=all and queries do.

It's better to add "The switch for this is <option>-b</option>." into the
doc.

fixed

+    else if (strcmp(prev2_wd, "\\set") == 0)
+    {
+        if (strcmp(prev_wd, "ECHO") == 0)
+        {
+            static const char *const my_list[] =
+            {"none", "errors", "queries", "all", NULL};
+
+            COMPLETE_WITH_LIST_CS(my_list);
+        }
+        else if (strcmp(prev_wd, "ECHO_HIDDEN") == 0)
+        {
+            static const char *const my_list[] =
+            {"noexec", "off", "on", NULL};
+
+            COMPLETE_WITH_LIST_CS(my_list);
+        }
+    }

I think that adding tab-completions of psql variables is good, but
adding those of only ECHO and ECHO_HIDDEN seems half-baked.
Probably this part should be split into separate patch.

fixed

please, see updated patch in attachment

Thanks for updating the patch!

Barring any objection, I will commit this patch except tab-completion part.

I'm not against adding tab-completion support for psql variables, but I'm
not sure if it's good idea or not to treat only one or two variables special
and add tab-completions for them. There are other variables which accept
special argument (e.g., COMP_KEYWORD_CASE) and it's basically worth
adding tab-completion support for them.

Regards,

--
Fujii Masao

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#21Pavel Stehule
pavel.stehule@gmail.com
In reply to: Fujii Masao (#20)
Re: psql: show only failed queries

2014-07-09 14:39 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Wed, Jul 9, 2014 at 9:06 PM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

Hi

2014-07-09 7:07 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Mon, Jun 30, 2014 at 8:33 PM, Pavel Stehule <pavel.stehule@gmail.com

wrote:

2014-06-30 13:01 GMT+02:00 Abhijit Menon-Sen <ams@2ndquadrant.com>:

At 2014-06-30 12:48:30 +0200, pavel.stehule@gmail.com wrote:

+      <para>
+      Print a failed SQL commands to standard error output. This

is

+ equivalent to setting the variable <varname>ECHO</varname>

to

+ <literal>errors</literal>.

No "a", just "Print failed SQL commands …".

-        <option>-e</option>.
+        <option>-e</option>. If set to <literal>error</literal>

then

only
+ failed queries are displayed.

Should be "errors" here, not "error".

printf(_(" -a, --echo-all echo all input from
script\n"));
+ printf(_(" -b --echo-errors echo failed commands

sent

to
server\n"));
printf(_(" -e, --echo-queries echo commands sent to
server\n"));

Should have a comma after -b to match other options. Also I would
remove
"sent to server" from the description: "echo failed commands" is

fine.

fixed

$ psql -b
bin/psql: invalid option -- 'b'
Try "psql --help" for more information.

I got this error. ISTM you forgot to add 'b' into the third argument of
getopt_long in startup.c.

fixed

<application>psql</application> merely prints all queries as
they are sent to the server. The switch for this is
-        <option>-e</option>.
+        <option>-e</option>. If set to <literal>errors</literal> then
only
+        failed queries are displayed.

I think that where failed queries are output should be documented here.
Otherwise users might misunderstand they are output to standard output
like ECHO=all and queries do.

It's better to add "The switch for this is <option>-b</option>." into

the

doc.

fixed

+    else if (strcmp(prev2_wd, "\\set") == 0)
+    {
+        if (strcmp(prev_wd, "ECHO") == 0)
+        {
+            static const char *const my_list[] =
+            {"none", "errors", "queries", "all", NULL};
+
+            COMPLETE_WITH_LIST_CS(my_list);
+        }
+        else if (strcmp(prev_wd, "ECHO_HIDDEN") == 0)
+        {
+            static const char *const my_list[] =
+            {"noexec", "off", "on", NULL};
+
+            COMPLETE_WITH_LIST_CS(my_list);
+        }
+    }

I think that adding tab-completions of psql variables is good, but
adding those of only ECHO and ECHO_HIDDEN seems half-baked.
Probably this part should be split into separate patch.

fixed

please, see updated patch in attachment

Thanks for updating the patch!

Barring any objection, I will commit this patch except tab-completion part.

Thank you

I'm not against adding tab-completion support for psql variables, but I'm
not sure if it's good idea or not to treat only one or two variables
special
and add tab-completions for them. There are other variables which accept
special argument (e.g., COMP_KEYWORD_CASE) and it's basically worth
adding tab-completion support for them.

It can be a second discussion, but I am thinking so anywhere when we can
use autocomplete, then we should it - Although it should not to substitute
documentation, it is fast help about available options, mainly in situation
where variables can hold a relative different content. I can prepare, if
there will not any objection addition patch with complete autocomplete for
all psql variables described in doc.

Regards

Pavel

Show quoted text

Regards,

--
Fujii Masao

#22Fujii Masao
masao.fujii@gmail.com
In reply to: Pavel Stehule (#21)
Re: psql: show only failed queries

On Wed, Jul 9, 2014 at 9:44 PM, Pavel Stehule <pavel.stehule@gmail.com> wrote:

Barring any objection, I will commit this patch except tab-completion
part.

Committed.

It can be a second discussion, but I am thinking so anywhere when we can use
autocomplete, then we should it - Although it should not to substitute
documentation, it is fast help about available options, mainly in situation
where variables can hold a relative different content. I can prepare, if
there will not any objection addition patch with complete autocomplete for
all psql variables described in doc.

I have no objection. But I found that the tab-completion for psql
variables names
are not complete. For example, COMP_KEYWORD_CASE is not displayed.
Probably we should address this problem, too.

Regards,

--
Fujii Masao

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#23Pavel Stehule
pavel.stehule@gmail.com
In reply to: Fujii Masao (#22)
Re: psql: show only failed queries

2014-07-10 7:32 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Wed, Jul 9, 2014 at 9:44 PM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

Barring any objection, I will commit this patch except tab-completion
part.

Committed.

Thank you very much

It can be a second discussion, but I am thinking so anywhere when we can

use

autocomplete, then we should it - Although it should not to substitute
documentation, it is fast help about available options, mainly in

situation

where variables can hold a relative different content. I can prepare, if
there will not any objection addition patch with complete autocomplete

for

all psql variables described in doc.

I have no objection. But I found that the tab-completion for psql
variables names
are not complete. For example, COMP_KEYWORD_CASE is not displayed.
Probably we should address this problem, too.

I prepare patch for next commitfest - it is relative trivial task

Pavel

Show quoted text

Regards,

--
Fujii Masao

#24Pavel Stehule
pavel.stehule@gmail.com
In reply to: Pavel Stehule (#23)
1 attachment(s)
Re: psql: show only failed queries

Hello

here is a proposed patch - autocomplete for known psql variable content

Regards

Pavel

2014-07-10 9:50 GMT+02:00 Pavel Stehule <pavel.stehule@gmail.com>:

Show quoted text

2014-07-10 7:32 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Wed, Jul 9, 2014 at 9:44 PM, Pavel Stehule <pavel.stehule@gmail.com>

wrote:

Barring any objection, I will commit this patch except tab-completion
part.

Committed.

Thank you very much

It can be a second discussion, but I am thinking so anywhere when we

can use

autocomplete, then we should it - Although it should not to substitute
documentation, it is fast help about available options, mainly in

situation

where variables can hold a relative different content. I can prepare, if
there will not any objection addition patch with complete autocomplete

for

all psql variables described in doc.

I have no objection. But I found that the tab-completion for psql
variables names
are not complete. For example, COMP_KEYWORD_CASE is not displayed.
Probably we should address this problem, too.

I prepare patch for next commitfest - it is relative trivial task

Pavel

Regards,

--
Fujii Masao

Attachments:

psql-var-complete.patchtext/x-patch; charset=US-ASCII; name=psql-var-complete.patchDownload
commit 7cba776aea228165c5b65f7077515328a0efa039
Author: root <root@localhost.localdomain>
Date:   Thu Jul 10 14:54:36 2014 +0200

    initial concept

diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 5a397e8..f05dfe2 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -147,6 +147,8 @@ main(int argc, char *argv[])
 	SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2);
 	SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);
 
+	SetVariable(pset.vars, "COMP_KEYWORD_CASE", "preserve-upper");
+
 	parse_psql_options(argc, argv, &options);
 
 	/*
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index bab0357..41ae499 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -3592,6 +3592,79 @@ psql_completion(const char *text, int start, int end)
 	{
 		matches = complete_from_variables(text, "", "");
 	}
+	else if (strcmp(prev2_wd, "\\set") == 0)
+	{
+		if (strcmp(prev_wd, "AUTOCOMMIT") == 0)
+		{
+			static const char *const my_list[] =
+			{"on", "off", "interactive", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "COMP_KEYWORD_CASE") == 0)
+		{
+			static const char *const my_list[] =
+			{"lower", "upper", "preserve-lower", "preserve-upper", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "ECHO") == 0)
+		{
+			static const char *const my_list[] =
+			{"none", "errors", "queries", "all", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "ECHO_HIDDEN") == 0)
+		{
+			static const char *const my_list[] =
+			{"noexec", "off", "on", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "ON_ERROR_ROLLBACK") == 0)
+		{
+			static const char *const my_list[] =
+			{"on", "off", "interactive", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "ON_ERROR_STOP") == 0)
+		{
+			static const char *const my_list[] =
+			{"on", "off", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "QUIET") == 0)
+		{
+			static const char *const my_list[] =
+			{"on", "off", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "SINGLELINE") == 0)
+		{
+			static const char *const my_list[] =
+			{"on", "off", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "SINGLESTEP") == 0)
+		{
+			static const char *const my_list[] =
+			{"on", "off", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "VERBOSITY") == 0)
+		{
+			static const char *const my_list[] =
+			{"default", "verbose", "terse", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+	}
 	else if (strcmp(prev_wd, "\\sf") == 0 || strcmp(prev_wd, "\\sf+") == 0)
 		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
 	else if (strcmp(prev_wd, "\\cd") == 0 ||
#25Fujii Masao
masao.fujii@gmail.com
In reply to: Pavel Stehule (#24)
Re: psql: show only failed queries

On Thu, Jul 10, 2014 at 9:56 PM, Pavel Stehule <pavel.stehule@gmail.com> wrote:

Hello

here is a proposed patch - autocomplete for known psql variable content

Even after applying the patch, the following psql variables were not displayed
on the tab-completion of \set command.

HISTFILE
HISTSIZE
HOST
IGNOREEOF
LASTOID

I'm not sure if it's worth displaying all of them on the tab-completion of \set
because it seems strange to change some of them by \set command, for example
HOST, though.

Regards,

--
Fujii Masao

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#26Pavel Stehule
pavel.stehule@gmail.com
In reply to: Fujii Masao (#25)
Re: psql: show only failed queries

2014-07-15 11:29 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Thu, Jul 10, 2014 at 9:56 PM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

Hello

here is a proposed patch - autocomplete for known psql variable content

Even after applying the patch, the following psql variables were not
displayed
on the tab-completion of \set command.

HISTFILE
HISTSIZE
HOST
IGNOREEOF
LASTOID

I'm not sure if it's worth displaying all of them on the tab-completion of
\set
because it seems strange to change some of them by \set command, for
example
HOST, though.

For these variables are not default - so doesn't exist and cannot be
completed by default.

there are two fix:

a) fix autocomplete source - preferred
b) create default - but it is probably impossible

Regards

Pavel

Show quoted text

Regards,

--
Fujii Masao

#27Fujii Masao
masao.fujii@gmail.com
In reply to: Pavel Stehule (#26)
Re: psql: show only failed queries

On Tue, Jul 15, 2014 at 7:01 PM, Pavel Stehule <pavel.stehule@gmail.com> wrote:

2014-07-15 11:29 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Thu, Jul 10, 2014 at 9:56 PM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

Hello

here is a proposed patch - autocomplete for known psql variable content

Even after applying the patch, the following psql variables were not
displayed
on the tab-completion of \set command.

HISTFILE
HISTSIZE
HOST
IGNOREEOF
LASTOID

I'm not sure if it's worth displaying all of them on the tab-completion of
\set
because it seems strange to change some of them by \set command, for
example
HOST, though.

For these variables are not default - so doesn't exist and cannot be
completed by default.

there are two fix:

a) fix autocomplete source - preferred

+1

Regards,

--
Fujii Masao

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#28Pavel Stehule
pavel.stehule@gmail.com
In reply to: Fujii Masao (#27)
1 attachment(s)
Re: psql: show only failed queries

2014-07-15 12:07 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Tue, Jul 15, 2014 at 7:01 PM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

2014-07-15 11:29 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Thu, Jul 10, 2014 at 9:56 PM, Pavel Stehule <pavel.stehule@gmail.com

wrote:

Hello

here is a proposed patch - autocomplete for known psql variable

content

Even after applying the patch, the following psql variables were not
displayed
on the tab-completion of \set command.

HISTFILE
HISTSIZE
HOST
IGNOREEOF
LASTOID

I'm not sure if it's worth displaying all of them on the tab-completion

of

\set
because it seems strange to change some of them by \set command, for
example
HOST, though.

For these variables are not default - so doesn't exist and cannot be
completed by default.

there are two fix:

a) fix autocomplete source - preferred

+1

here is the patch

Regards

Pavel

Show quoted text

Regards,

--
Fujii Masao

Attachments:

psql-var-complete-2.patchtext/x-patch; charset=US-ASCII; name=psql-var-complete-2.patchDownload
commit 1d0ef57d2774f37c464f25fe4e0d3be75bd36aeb
Author: root <root@localhost.localdomain>
Date:   Thu Jul 10 14:54:36 2014 +0200

    all known psql variables are in list now

diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index bab0357..e45027e 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -3592,6 +3592,79 @@ psql_completion(const char *text, int start, int end)
 	{
 		matches = complete_from_variables(text, "", "");
 	}
+	else if (strcmp(prev2_wd, "\\set") == 0)
+	{
+		if (strcmp(prev_wd, "AUTOCOMMIT") == 0)
+		{
+			static const char *const my_list[] =
+			{"on", "off", "interactive", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "COMP_KEYWORD_CASE") == 0)
+		{
+			static const char *const my_list[] =
+			{"lower", "upper", "preserve-lower", "preserve-upper", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "ECHO") == 0)
+		{
+			static const char *const my_list[] =
+			{"none", "errors", "queries", "all", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "ECHO_HIDDEN") == 0)
+		{
+			static const char *const my_list[] =
+			{"noexec", "off", "on", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "ON_ERROR_ROLLBACK") == 0)
+		{
+			static const char *const my_list[] =
+			{"on", "off", "interactive", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "ON_ERROR_STOP") == 0)
+		{
+			static const char *const my_list[] =
+			{"on", "off", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "QUIET") == 0)
+		{
+			static const char *const my_list[] =
+			{"on", "off", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "SINGLELINE") == 0)
+		{
+			static const char *const my_list[] =
+			{"on", "off", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "SINGLESTEP") == 0)
+		{
+			static const char *const my_list[] =
+			{"on", "off", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "VERBOSITY") == 0)
+		{
+			static const char *const my_list[] =
+			{"default", "verbose", "terse", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+	}
 	else if (strcmp(prev_wd, "\\sf") == 0 || strcmp(prev_wd, "\\sf+") == 0)
 		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
 	else if (strcmp(prev_wd, "\\cd") == 0 ||
@@ -4046,28 +4119,61 @@ complete_from_variables(const char *text, const char *prefix, const char *suffix
 {
 	char	  **matches;
 	char	  **varnames;
+	static const char **known_varname;
 	int			nvars = 0;
 	int			maxvars = 100;
 	int			i;
 	struct _variable *ptr;
 
+	static const char *const known_varnames[] = {
+		"AUTOCOMMIT", "COMP_KEYWORD_CASE", "DBNAME", "ECHO", "ECHO_HIDDEN", "ENCODING",
+		"FETCH_COUNT", "HISTCONTROL", "HISTFILE", "HISTSIZE", "HOST", "IGNOREOFF",
+		"LASTOID", "ON_ERROR_ROLLBACK", "ON_ERROR_STOP", "PORT", "PROMPT1", "PROMPT2",
+		"PROMPT3", "QUIET", "SINGLELINE", "SINGLESTEP", "USER", "VERBOSITY",
+		NULL
+	};
+
 	varnames = (char **) pg_malloc((maxvars + 1) * sizeof(char *));
 
+	/* all psql known variables are included in list by default */
+	for (known_varname = known_varnames; *known_varname; known_varname++)
+		varnames[nvars++] = pg_strdup(*known_varname);
+
 	for (ptr = pset.vars->next; ptr; ptr = ptr->next)
 	{
-		if (nvars >= maxvars)
+		char *varname;
+		bool is_known_varname = false;
+
+		varname = psprintf("%s%s%s", prefix, ptr->name, suffix);
+
+		/* is it known varname? */
+		for (known_varname = known_varnames; *known_varname; known_varname++)
 		{
-			maxvars *= 2;
-			varnames = (char **) realloc(varnames,
-										 (maxvars + 1) * sizeof(char *));
-			if (!varnames)
+			if (strcmp(*known_varname, varname) == 0)
 			{
-				psql_error("out of memory\n");
-				exit(EXIT_FAILURE);
+				free(varname);
+				is_known_varname = true;
+				break;
 			}
 		}
 
-		varnames[nvars++] = psprintf("%s%s%s", prefix, ptr->name, suffix);
+		/* append only unknown varnames, known varnames are in list already */
+		if (!is_known_varname)
+		{
+			if (nvars >= maxvars)
+			{
+				maxvars *= 2;
+				varnames = (char **) realloc(varnames,
+										 (maxvars + 1) * sizeof(char *));
+				if (!varnames)
+				{
+					psql_error("out of memory\n");
+					exit(EXIT_FAILURE);
+				}
+			}
+
+			varnames[nvars++] = psprintf("%s%s%s", prefix, ptr->name, suffix);
+		}
 	}
 
 	varnames[nvars] = NULL;
#29Fujii Masao
masao.fujii@gmail.com
In reply to: Pavel Stehule (#28)
Re: psql: show only failed queries

On Wed, Jul 16, 2014 at 4:34 AM, Pavel Stehule <pavel.stehule@gmail.com> wrote:

2014-07-15 12:07 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Tue, Jul 15, 2014 at 7:01 PM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

2014-07-15 11:29 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Thu, Jul 10, 2014 at 9:56 PM, Pavel Stehule
<pavel.stehule@gmail.com>
wrote:

Hello

here is a proposed patch - autocomplete for known psql variable
content

Even after applying the patch, the following psql variables were not
displayed
on the tab-completion of \set command.

HISTFILE
HISTSIZE
HOST
IGNOREEOF
LASTOID

I'm not sure if it's worth displaying all of them on the tab-completion
of
\set
because it seems strange to change some of them by \set command, for
example
HOST, though.

For these variables are not default - so doesn't exist and cannot be
completed by default.

there are two fix:

a) fix autocomplete source - preferred

+1

here is the patch

Thanks for the patch!

I got the following compiler warnings.

tab-complete.c:4155: warning: assignment discards qualifiers from
pointer target type
tab-complete.c:4166: warning: assignment discards qualifiers from
pointer target type

+ "FETCH_COUNT", "HISTCONTROL", "HISTFILE", "HISTSIZE", "HOST",
"IGNOREOFF",

Typo: IGNOREOFF -> IGNOREEOF

+    /* all psql known variables are included in list by default */
+    for (known_varname = known_varnames; *known_varname; known_varname++)
+        varnames[nvars++] = pg_strdup(*known_varname);

Don't we need to append both prefix and suffix to even known variables?

+    else if (strcmp(prev2_wd, "\\set") == 0)
+    {
+        if (strcmp(prev_wd, "AUTOCOMMIT") == 0)

ISTM that some psql variables like IGNOREEOF are not there. Why not?

Regards,

--
Fujii Masao

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#30Pavel Stehule
pavel.stehule@gmail.com
In reply to: Fujii Masao (#29)
Re: psql: show only failed queries

Hello

updated version patch in attachment

2014-08-05 13:31 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Wed, Jul 16, 2014 at 4:34 AM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

2014-07-15 12:07 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Tue, Jul 15, 2014 at 7:01 PM, Pavel Stehule <pavel.stehule@gmail.com

wrote:

2014-07-15 11:29 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Thu, Jul 10, 2014 at 9:56 PM, Pavel Stehule
<pavel.stehule@gmail.com>
wrote:

Hello

here is a proposed patch - autocomplete for known psql variable
content

Even after applying the patch, the following psql variables were not
displayed
on the tab-completion of \set command.

HISTFILE
HISTSIZE
HOST
IGNOREEOF
LASTOID

I'm not sure if it's worth displaying all of them on the

tab-completion

of
\set
because it seems strange to change some of them by \set command, for
example
HOST, though.

For these variables are not default - so doesn't exist and cannot be
completed by default.

there are two fix:

a) fix autocomplete source - preferred

+1

here is the patch

Thanks for the patch!

I got the following compiler warnings.

tab-complete.c:4155: warning: assignment discards qualifiers from
pointer target type
tab-complete.c:4166: warning: assignment discards qualifiers from
pointer target type

fixed

+ "FETCH_COUNT", "HISTCONTROL", "HISTFILE", "HISTSIZE", "HOST",
"IGNOREOFF",

Typo: IGNOREOFF -> IGNOREEOF

fixed

+    /* all psql known variables are included in list by default */
+    for (known_varname = known_varnames; *known_varname; known_varname++)
+        varnames[nvars++] = pg_strdup(*known_varname);

Don't we need to append both prefix and suffix to even known variables?

??? I am not sure if I understand well - probably system "read only"
variables as DBNAME, USER, VERSION should not be there

+    else if (strcmp(prev2_wd, "\\set") == 0)
+    {
+        if (strcmp(prev_wd, "AUTOCOMMIT") == 0)

ISTM that some psql variables like IGNOREEOF are not there. Why not?

yes, there are not complete for DBNAME, ENCODING, FETCH_COUNT, HISTCONTROL,
HISTFILE, HISTSIZE, HOST, IGNOREEOFF, PROMPT*,USER, VERSION

There are more reasons:

* paremeter is not a enum (string, number or both): FETCH_COUNT, PROMPT,
HISTSIZE, ..

* variable is pseudo read only - change has not any effect: DBNAME,
ENCODING, VERSION

Regards

Pavel

Show quoted text

Regards,

--
Fujii Masao

#31Fujii Masao
masao.fujii@gmail.com
In reply to: Pavel Stehule (#30)
Re: psql: show only failed queries

On Thu, Aug 7, 2014 at 6:26 AM, Pavel Stehule <pavel.stehule@gmail.com> wrote:

Hello

updated version patch in attachment

Thanks! But ISTM you forgot to attached the patch.

+    /* all psql known variables are included in list by default */
+    for (known_varname = known_varnames; *known_varname; known_varname++)
+        varnames[nvars++] = pg_strdup(*known_varname);

Don't we need to append both prefix and suffix to even known variables?

??? I am not sure if I understand well - probably system "read only"
variables as DBNAME, USER, VERSION should not be there

I had that question because complete_from_variables() is also called by the
tab-completion of "\echo :" and it shows half-baked variables list. So
I thought probably we need to change complete_from_variables() more to
fix the problem.

+    else if (strcmp(prev2_wd, "\\set") == 0)
+    {
+        if (strcmp(prev_wd, "AUTOCOMMIT") == 0)

ISTM that some psql variables like IGNOREEOF are not there. Why not?

yes, there are not complete for DBNAME, ENCODING, FETCH_COUNT, HISTCONTROL,
HISTFILE, HISTSIZE, HOST, IGNOREEOFF, PROMPT*,USER, VERSION

There are more reasons:

* paremeter is not a enum (string, number or both): FETCH_COUNT, PROMPT,
HISTSIZE, ..

* variable is pseudo read only - change has not any effect: DBNAME,
ENCODING, VERSION

So HISTCONTROL should be there because it doesn't have such reasons at all?

Regards,

--
Fujii Masao

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#32Pavel Stehule
pavel.stehule@gmail.com
In reply to: Fujii Masao (#31)
1 attachment(s)
Re: psql: show only failed queries

2014-08-07 7:10 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Thu, Aug 7, 2014 at 6:26 AM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

Hello

updated version patch in attachment

Thanks! But ISTM you forgot to attached the patch.

grr .. I am sorry

+    /* all psql known variables are included in list by default */
+    for (known_varname = known_varnames; *known_varname;

known_varname++)

+ varnames[nvars++] = pg_strdup(*known_varname);

Don't we need to append both prefix and suffix to even known variables?

??? I am not sure if I understand well - probably system "read only"
variables as DBNAME, USER, VERSION should not be there

I had that question because complete_from_variables() is also called by the
tab-completion of "\echo :" and it shows half-baked variables list. So
I thought probably we need to change complete_from_variables() more to
fix the problem.

I understand now.

I fixed it.

I have a question.\echo probably should not to show empty known variable.

data for autocomplete for \echo should be same as result of "\set"

+    else if (strcmp(prev2_wd, "\\set") == 0)
+    {
+        if (strcmp(prev_wd, "AUTOCOMMIT") == 0)

ISTM that some psql variables like IGNOREEOF are not there. Why not?

yes, there are not complete for DBNAME, ENCODING, FETCH_COUNT,

HISTCONTROL,

HISTFILE, HISTSIZE, HOST, IGNOREEOFF, PROMPT*,USER, VERSION

There are more reasons:

* paremeter is not a enum (string, number or both): FETCH_COUNT, PROMPT,
HISTSIZE, ..

* variable is pseudo read only - change has not any effect: DBNAME,
ENCODING, VERSION

So HISTCONTROL should be there because it doesn't have such reasons at all?

yes

Pavel

Show quoted text

Regards,

--
Fujii Masao

Attachments:

psql-var-complete-3.patchtext/x-patch; charset=US-ASCII; name=psql-var-complete-3.patchDownload
commit bacfdc0e03219265aeee34b78f7ec9d272d49f72
Author: Pavel Stehule <pavel.stehule@gooddata.com>
Date:   Wed Aug 6 23:05:42 2014 +0200

    warnings and typo fixed

diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 24e60b7..b94ed63 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -3608,6 +3608,79 @@ psql_completion(const char *text, int start, int end)
 	{
 		matches = complete_from_variables(text, "", "");
 	}
+	else if (strcmp(prev2_wd, "\\set") == 0)
+	{
+		if (strcmp(prev_wd, "AUTOCOMMIT") == 0)
+		{
+			static const char *const my_list[] =
+			{"on", "off", "interactive", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "COMP_KEYWORD_CASE") == 0)
+		{
+			static const char *const my_list[] =
+			{"lower", "upper", "preserve-lower", "preserve-upper", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "ECHO") == 0)
+		{
+			static const char *const my_list[] =
+			{"none", "errors", "queries", "all", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "ECHO_HIDDEN") == 0)
+		{
+			static const char *const my_list[] =
+			{"noexec", "off", "on", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "ON_ERROR_ROLLBACK") == 0)
+		{
+			static const char *const my_list[] =
+			{"on", "off", "interactive", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "ON_ERROR_STOP") == 0)
+		{
+			static const char *const my_list[] =
+			{"on", "off", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "QUIET") == 0)
+		{
+			static const char *const my_list[] =
+			{"on", "off", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "SINGLELINE") == 0)
+		{
+			static const char *const my_list[] =
+			{"on", "off", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "SINGLESTEP") == 0)
+		{
+			static const char *const my_list[] =
+			{"on", "off", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+		else if (strcmp(prev_wd, "VERBOSITY") == 0)
+		{
+			static const char *const my_list[] =
+			{"default", "verbose", "terse", NULL};
+
+			COMPLETE_WITH_LIST_CS(my_list);
+		}
+	}
 	else if (strcmp(prev_wd, "\\sf") == 0 || strcmp(prev_wd, "\\sf+") == 0)
 		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
 	else if (strcmp(prev_wd, "\\cd") == 0 ||
@@ -4062,28 +4135,61 @@ complete_from_variables(const char *text, const char *prefix, const char *suffix
 {
 	char	  **matches;
 	char	  **varnames;
+	static const char **known_varname;
 	int			nvars = 0;
 	int			maxvars = 100;
 	int			i;
 	struct _variable *ptr;
 
+	static const char *known_varnames[] = {
+		"AUTOCOMMIT", "COMP_KEYWORD_CASE", "DBNAME", "ECHO", "ECHO_HIDDEN", "ENCODING",
+		"FETCH_COUNT", "HISTCONTROL", "HISTFILE", "HISTSIZE", "HOST", "IGNOREEOFF",
+		"LASTOID", "ON_ERROR_ROLLBACK", "ON_ERROR_STOP", "PORT", "PROMPT1", "PROMPT2",
+		"PROMPT3", "QUIET", "SINGLELINE", "SINGLESTEP", "USER", "VERBOSITY",
+		NULL
+	};
+
 	varnames = (char **) pg_malloc((maxvars + 1) * sizeof(char *));
 
+	/* all psql known variables are included in list by default */
+	for (known_varname = known_varnames; *known_varname; known_varname++)
+		varnames[nvars++] = psprintf("%s%s%s", prefix, *known_varname, suffix);
+
 	for (ptr = pset.vars->next; ptr; ptr = ptr->next)
 	{
-		if (nvars >= maxvars)
+		char *varname;
+		bool is_known_varname = false;
+
+		varname = psprintf("%s%s%s", prefix, ptr->name, suffix);
+
+		/* is it known varname? */
+		for (known_varname = known_varnames; *known_varname; known_varname++)
 		{
-			maxvars *= 2;
-			varnames = (char **) realloc(varnames,
-										 (maxvars + 1) * sizeof(char *));
-			if (!varnames)
+			if (strcmp(*known_varname, varname) == 0)
 			{
-				psql_error("out of memory\n");
-				exit(EXIT_FAILURE);
+				free(varname);
+				is_known_varname = true;
+				break;
 			}
 		}
 
-		varnames[nvars++] = psprintf("%s%s%s", prefix, ptr->name, suffix);
+		/* append only unknown varnames, known varnames are in list already */
+		if (!is_known_varname)
+		{
+			if (nvars >= maxvars)
+			{
+				maxvars *= 2;
+				varnames = (char **) realloc(varnames,
+										 (maxvars + 1) * sizeof(char *));
+				if (!varnames)
+				{
+					psql_error("out of memory\n");
+					exit(EXIT_FAILURE);
+				}
+			}
+
+			varnames[nvars++] = psprintf("%s%s%s", prefix, ptr->name, suffix);
+		}
 	}
 
 	varnames[nvars] = NULL;
#33Fujii Masao
masao.fujii@gmail.com
In reply to: Pavel Stehule (#32)
1 attachment(s)
Re: psql: show only failed queries

On Fri, Aug 8, 2014 at 4:31 AM, Pavel Stehule <pavel.stehule@gmail.com> wrote:

2014-08-07 7:10 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Thu, Aug 7, 2014 at 6:26 AM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

Hello

updated version patch in attachment

Thanks! But ISTM you forgot to attached the patch.

grr .. I am sorry

No problem. Thanks for the patch! Attached is the revised version of the patch.

+    /* all psql known variables are included in list by default */
+    for (known_varname = known_varnames; *known_varname;
known_varname++)
+        varnames[nvars++] = pg_strdup(*known_varname);

Don't we need to append both prefix and suffix to even known variables?

??? I am not sure if I understand well - probably system "read only"
variables as DBNAME, USER, VERSION should not be there

I had that question because complete_from_variables() is also called by
the
tab-completion of "\echo :" and it shows half-baked variables list. So
I thought probably we need to change complete_from_variables() more to
fix the problem.

I understand now.

I fixed it.

I have a question.\echo probably should not to show empty known variable.

data for autocomplete for \echo should be same as result of "\set"

Agreed. I think that only the variables having the set values should be
displayed in "\echo :" case. So I modified complete_from_variables()
so that the unset variables are not shown in that case but all the variables
are shown in the tab-completion of "\set".

+    else if (strcmp(prev2_wd, "\\set") == 0)
+    {
+        if (strcmp(prev_wd, "AUTOCOMMIT") == 0)

ISTM that some psql variables like IGNOREEOF are not there. Why not?

yes, there are not complete for DBNAME, ENCODING, FETCH_COUNT,
HISTCONTROL,
HISTFILE, HISTSIZE, HOST, IGNOREEOFF, PROMPT*,USER, VERSION

There are more reasons:

* paremeter is not a enum (string, number or both): FETCH_COUNT, PROMPT,
HISTSIZE, ..

* variable is pseudo read only - change has not any effect: DBNAME,
ENCODING, VERSION

So HISTCONTROL should be there because it doesn't have such reasons at
all?

yes

ISTM that you forgot to add HISTCONTROL to your patch. So I just added that.

I added the tab-completion for "\unset" command. Like "\echo :", only
the variables having the set values should be displayed in "\unset" case.

I changed complete_from_variables() so that it checks the memory size of
the variable array even when appending the known variables. If the memory
size is not enough, it's dynamically increased. Practically this check would
not be required because the initial size of the array is enough larger than
the number of the known variables. I added this as the safe-guard.

Typo: IGNOREEOFF -> IGNOREEOF

I removed the value "none" from the value list of "ECHO" because it's not
documented and a user might get confused when he or she sees the undocumented
value "none". Thought?

Regards,

--
Fujii Masao

Attachments:

psql-var-complete-4_fujii.patchtext/x-patch; charset=US-ASCII; name=psql-var-complete-4_fujii.patchDownload
*** a/src/bin/psql/tab-complete.c
--- b/src/bin/psql/tab-complete.c
***************
*** 813,820 **** static char *_complete_from_query(int is_schema_query,
  					 const char *text, int state);
  static char *complete_from_list(const char *text, int state);
  static char *complete_from_const(const char *text, int state);
  static char **complete_from_variables(const char *text,
! 						const char *prefix, const char *suffix);
  static char *complete_from_files(const char *text, int state);
  
  static char *pg_strdup_keyword_case(const char *s, const char *ref);
--- 813,823 ----
  					 const char *text, int state);
  static char *complete_from_list(const char *text, int state);
  static char *complete_from_const(const char *text, int state);
+ static void append_variable_names(char ***varnames, int *nvars,
+ 								  int *maxvars, const char *varname,
+ 								  const char *prefix, const char *suffix);
  static char **complete_from_variables(const char *text,
! 					  const char *prefix, const char *suffix, bool need_value);
  static char *complete_from_files(const char *text, int state);
  
  static char *pg_strdup_keyword_case(const char *s, const char *ref);
***************
*** 925,935 **** psql_completion(const char *text, int start, int end)
  	else if (text[0] == ':' && text[1] != ':')
  	{
  		if (text[1] == '\'')
! 			matches = complete_from_variables(text, ":'", "'");
  		else if (text[1] == '"')
! 			matches = complete_from_variables(text, ":\"", "\"");
  		else
! 			matches = complete_from_variables(text, ":", "");
  	}
  
  	/* If no previous word, suggest one of the basic sql commands */
--- 928,938 ----
  	else if (text[0] == ':' && text[1] != ':')
  	{
  		if (text[1] == '\'')
! 			matches = complete_from_variables(text, ":'", "'", true);
  		else if (text[1] == '"')
! 			matches = complete_from_variables(text, ":\"", "\"", true);
  		else
! 			matches = complete_from_variables(text, ":", "", true);
  	}
  
  	/* If no previous word, suggest one of the basic sql commands */
***************
*** 3604,3612 **** psql_completion(const char *text, int start, int end)
  			COMPLETE_WITH_LIST_CS(my_list);
  		}
  	}
  	else if (strcmp(prev_wd, "\\set") == 0)
  	{
! 		matches = complete_from_variables(text, "", "");
  	}
  	else if (strcmp(prev_wd, "\\sf") == 0 || strcmp(prev_wd, "\\sf+") == 0)
  		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
--- 3607,3677 ----
  			COMPLETE_WITH_LIST_CS(my_list);
  		}
  	}
+ 	else if (strcmp(prev_wd, "\\unset") == 0)
+ 	{
+ 		matches = complete_from_variables(text, "", "", true);
+ 	}
  	else if (strcmp(prev_wd, "\\set") == 0)
  	{
! 		matches = complete_from_variables(text, "", "", false);
! 	}
! 	else if (strcmp(prev2_wd, "\\set") == 0)
! 	{
! 		static const char *const boolean_value_list[] =
! 		{"on", "off", NULL};
! 
! 		if (strcmp(prev_wd, "AUTOCOMMIT") == 0)
! 			COMPLETE_WITH_LIST_CS(boolean_value_list);
! 		else if (strcmp(prev_wd, "COMP_KEYWORD_CASE") == 0)
! 		{
! 			static const char *const my_list[] =
! 			{"lower", "upper", "preserve-lower", "preserve-upper", NULL};
! 
! 			COMPLETE_WITH_LIST_CS(my_list);
! 		}
! 		else if (strcmp(prev_wd, "ECHO") == 0)
! 		{
! 			static const char *const my_list[] =
! 			{"errors", "queries", "all", NULL};
! 
! 			COMPLETE_WITH_LIST_CS(my_list);
! 		}
! 		else if (strcmp(prev_wd, "ECHO_HIDDEN") == 0)
! 		{
! 			static const char *const my_list[] =
! 			{"noexec", "off", "on", NULL};
! 
! 			COMPLETE_WITH_LIST_CS(my_list);
! 		}
! 		else if (strcmp(prev_wd, "HISTCONTROL") == 0)
! 		{
! 			static const char *const my_list[] =
! 			{"ignorespace", "ignoredups", "ignoreboth", NULL};
! 
! 			COMPLETE_WITH_LIST_CS(my_list);
! 		}
! 		else if (strcmp(prev_wd, "ON_ERROR_ROLLBACK") == 0)
! 		{
! 			static const char *const my_list[] =
! 			{"on", "off", "interactive", NULL};
! 
! 			COMPLETE_WITH_LIST_CS(my_list);
! 		}
! 		else if (strcmp(prev_wd, "ON_ERROR_STOP") == 0)
! 			COMPLETE_WITH_LIST_CS(boolean_value_list);
! 		else if (strcmp(prev_wd, "QUIET") == 0)
! 			COMPLETE_WITH_LIST_CS(boolean_value_list);
! 		else if (strcmp(prev_wd, "SINGLELINE") == 0)
! 			COMPLETE_WITH_LIST_CS(boolean_value_list);
! 		else if (strcmp(prev_wd, "SINGLESTEP") == 0)
! 			COMPLETE_WITH_LIST_CS(boolean_value_list);
! 		else if (strcmp(prev_wd, "VERBOSITY") == 0)
! 		{
! 			static const char *const my_list[] =
! 			{"default", "verbose", "terse", NULL};
! 
! 			COMPLETE_WITH_LIST_CS(my_list);
! 		}
  	}
  	else if (strcmp(prev_wd, "\\sf") == 0 || strcmp(prev_wd, "\\sf+") == 0)
  		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
***************
*** 4053,4064 **** complete_from_const(const char *text, int state)
  
  
  /*
   * This function supports completion with the name of a psql variable.
   * The variable names can be prefixed and suffixed with additional text
!  * to support quoting usages.
   */
  static char **
! complete_from_variables(const char *text, const char *prefix, const char *suffix)
  {
  	char	  **matches;
  	char	  **varnames;
--- 4118,4156 ----
  
  
  /*
+  * This function appends the variable name with prefix and suffix to
+  * the variable names array.
+  */
+ static void
+ append_variable_names(char ***varnames, int *nvars,
+ 					  int *maxvars, const char *varname,
+ 					  const char *prefix, const char *suffix)
+ {
+ 	if (*nvars >= *maxvars)
+ 	{
+ 		*maxvars *= 2;
+ 		*varnames = (char **) realloc(*varnames,
+ 									  ((*maxvars) + 1) * sizeof(char *));
+ 		if (!(*varnames))
+ 		{
+ 			psql_error("out of memory\n");
+ 			exit(EXIT_FAILURE);
+ 		}
+ 	}
+ 
+ 	(*varnames)[(*nvars)++] = psprintf("%s%s%s", prefix, varname, suffix);
+ }
+ 
+ 
+ /*
   * This function supports completion with the name of a psql variable.
   * The variable names can be prefixed and suffixed with additional text
!  * to support quoting usages. If need_value is true, only the variables
!  * that have the set values are picked up.
   */
  static char **
! complete_from_variables(const char *text, const char *prefix, const char *suffix,
! 						bool need_value)
  {
  	char	  **matches;
  	char	  **varnames;
***************
*** 4067,4089 **** complete_from_variables(const char *text, const char *prefix, const char *suffix
  	int			i;
  	struct _variable *ptr;
  
  	varnames = (char **) pg_malloc((maxvars + 1) * sizeof(char *));
  
! 	for (ptr = pset.vars->next; ptr; ptr = ptr->next)
  	{
! 		if (nvars >= maxvars)
! 		{
! 			maxvars *= 2;
! 			varnames = (char **) realloc(varnames,
! 										 (maxvars + 1) * sizeof(char *));
! 			if (!varnames)
! 			{
! 				psql_error("out of memory\n");
! 				exit(EXIT_FAILURE);
! 			}
! 		}
  
! 		varnames[nvars++] = psprintf("%s%s%s", prefix, ptr->name, suffix);
  	}
  
  	varnames[nvars] = NULL;
--- 4159,4187 ----
  	int			i;
  	struct _variable *ptr;
  
+ 	static const char *const known_varnames[] = {
+ 		"AUTOCOMMIT", "COMP_KEYWORD_CASE", "DBNAME", "ECHO", "ECHO_HIDDEN",
+ 		"ENCODING", "FETCH_COUNT", "HISTCONTROL", "HISTFILE", "HISTSIZE",
+ 		"HOST", "IGNOREEOF", "LASTOID", "ON_ERROR_ROLLBACK", "ON_ERROR_STOP",
+ 		"PORT", "PROMPT1", "PROMPT2", "PROMPT3", "QUIET", "SINGLELINE",
+ 		"SINGLESTEP", "USER", "VERBOSITY",	NULL
+ 	};
+ 
  	varnames = (char **) pg_malloc((maxvars + 1) * sizeof(char *));
  
! 	if (!need_value)
  	{
! 		for (i = 0; known_varnames[i] && nvars < maxvars; i++)
! 			append_variable_names(&varnames, &nvars, &maxvars,
! 								  known_varnames[i], prefix, suffix);
! 	}
  
! 	for (ptr = pset.vars->next; ptr; ptr = ptr->next)
! 	{
! 		if (need_value && !(ptr->value))
! 			continue;
! 		append_variable_names(&varnames, &nvars, &maxvars, ptr->name,
! 							  prefix, suffix);
  	}
  
  	varnames[nvars] = NULL;
#34Pavel Stehule
pavel.stehule@gmail.com
In reply to: Fujii Masao (#33)
Re: psql: show only failed queries

Hi

2014-08-08 13:58 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Fri, Aug 8, 2014 at 4:31 AM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

2014-08-07 7:10 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Thu, Aug 7, 2014 at 6:26 AM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

Hello

updated version patch in attachment

Thanks! But ISTM you forgot to attached the patch.

grr .. I am sorry

No problem. Thanks for the patch! Attached is the revised version of the
patch.

+    /* all psql known variables are included in list by default */
+    for (known_varname = known_varnames; *known_varname;
known_varname++)
+        varnames[nvars++] = pg_strdup(*known_varname);

Don't we need to append both prefix and suffix to even known

variables?

??? I am not sure if I understand well - probably system "read only"
variables as DBNAME, USER, VERSION should not be there

I had that question because complete_from_variables() is also called by
the
tab-completion of "\echo :" and it shows half-baked variables list. So
I thought probably we need to change complete_from_variables() more to
fix the problem.

I understand now.

I fixed it.

I have a question.\echo probably should not to show empty known variable.

data for autocomplete for \echo should be same as result of "\set"

Agreed. I think that only the variables having the set values should be
displayed in "\echo :" case. So I modified complete_from_variables()
so that the unset variables are not shown in that case but all the
variables
are shown in the tab-completion of "\set".

+    else if (strcmp(prev2_wd, "\\set") == 0)
+    {
+        if (strcmp(prev_wd, "AUTOCOMMIT") == 0)

ISTM that some psql variables like IGNOREEOF are not there. Why not?

yes, there are not complete for DBNAME, ENCODING, FETCH_COUNT,
HISTCONTROL,
HISTFILE, HISTSIZE, HOST, IGNOREEOFF, PROMPT*,USER, VERSION

There are more reasons:

* paremeter is not a enum (string, number or both): FETCH_COUNT,

PROMPT,

HISTSIZE, ..

* variable is pseudo read only - change has not any effect: DBNAME,
ENCODING, VERSION

So HISTCONTROL should be there because it doesn't have such reasons at
all?

yes

ISTM that you forgot to add HISTCONTROL to your patch. So I just added
that.

I added the tab-completion for "\unset" command. Like "\echo :", only
the variables having the set values should be displayed in "\unset" case.

perfect

I changed complete_from_variables() so that it checks the memory size of
the variable array even when appending the known variables. If the memory
size is not enough, it's dynamically increased. Practically this check
would
not be required because the initial size of the array is enough larger than
the number of the known variables. I added this as the safe-guard.

Typo: IGNOREEOFF -> IGNOREEOF

I removed the value "none" from the value list of "ECHO" because it's not
documented and a user might get confused when he or she sees the
undocumented
value "none". Thought?

isn't better to fix doc? I don't know any reason why we should not to
support "none"

I looked to code, you removed a check against duplicate varname in list. Is
it ok?

Regards

Pavel

Show quoted text

Regards,

--
Fujii Masao

#35Fujii Masao
masao.fujii@gmail.com
In reply to: Pavel Stehule (#34)
1 attachment(s)
Re: psql: show only failed queries

On Sat, Aug 9, 2014 at 4:16 AM, Pavel Stehule <pavel.stehule@gmail.com> wrote:

Hi

2014-08-08 13:58 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Fri, Aug 8, 2014 at 4:31 AM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

2014-08-07 7:10 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Thu, Aug 7, 2014 at 6:26 AM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

Hello

updated version patch in attachment

Thanks! But ISTM you forgot to attached the patch.

grr .. I am sorry

No problem. Thanks for the patch! Attached is the revised version of the
patch.

+    /* all psql known variables are included in list by default */
+    for (known_varname = known_varnames; *known_varname;
known_varname++)
+        varnames[nvars++] = pg_strdup(*known_varname);

Don't we need to append both prefix and suffix to even known
variables?

??? I am not sure if I understand well - probably system "read only"
variables as DBNAME, USER, VERSION should not be there

I had that question because complete_from_variables() is also called by
the
tab-completion of "\echo :" and it shows half-baked variables list. So
I thought probably we need to change complete_from_variables() more to
fix the problem.

I understand now.

I fixed it.

I have a question.\echo probably should not to show empty known
variable.

data for autocomplete for \echo should be same as result of "\set"

Agreed. I think that only the variables having the set values should be
displayed in "\echo :" case. So I modified complete_from_variables()
so that the unset variables are not shown in that case but all the
variables
are shown in the tab-completion of "\set".

+    else if (strcmp(prev2_wd, "\\set") == 0)
+    {
+        if (strcmp(prev_wd, "AUTOCOMMIT") == 0)

ISTM that some psql variables like IGNOREEOF are not there. Why not?

yes, there are not complete for DBNAME, ENCODING, FETCH_COUNT,
HISTCONTROL,
HISTFILE, HISTSIZE, HOST, IGNOREEOFF, PROMPT*,USER, VERSION

There are more reasons:

* paremeter is not a enum (string, number or both): FETCH_COUNT,
PROMPT,
HISTSIZE, ..

* variable is pseudo read only - change has not any effect: DBNAME,
ENCODING, VERSION

So HISTCONTROL should be there because it doesn't have such reasons at
all?

yes

ISTM that you forgot to add HISTCONTROL to your patch. So I just added
that.

I added the tab-completion for "\unset" command. Like "\echo :", only
the variables having the set values should be displayed in "\unset" case.

perfect

I changed complete_from_variables() so that it checks the memory size of
the variable array even when appending the known variables. If the memory
size is not enough, it's dynamically increased. Practically this check
would
not be required because the initial size of the array is enough larger
than
the number of the known variables. I added this as the safe-guard.

Typo: IGNOREEOFF -> IGNOREEOF

I removed the value "none" from the value list of "ECHO" because it's not
documented and a user might get confused when he or she sees the
undocumented
value "none". Thought?

isn't better to fix doc? I don't know any reason why we should not to
support "none"

I'm OK with this. The attached patch adds the support of "none" value both
in ECHO and HISTCONTROL variables (because HISTCONTROL had the same
problem as ECHO had), and also adds the description of that value into
the document.

I looked to code, you removed a check against duplicate varname in list. Is
it ok?

Oh, just revived that code.

Regards,

--
Fujii Masao

Attachments:

psql-var-complete-5_fujii.patchtext/x-patch; charset=US-ASCII; name=psql-var-complete-5_fujii.patchDownload
*** a/doc/src/sgml/ref/psql-ref.sgml
--- b/doc/src/sgml/ref/psql-ref.sgml
***************
*** 2827,2833 **** bar
          they are sent to the server. The switch for this is
          <option>-e</option>. If set to <literal>errors</literal> then only
          failed queries are displayed on standard error output. The switch
!         for this is <option>-b</option>.
          </para>
          </listitem>
        </varlistentry>
--- 2827,2835 ----
          they are sent to the server. The switch for this is
          <option>-e</option>. If set to <literal>errors</literal> then only
          failed queries are displayed on standard error output. The switch
!         for this is <option>-b</option>. If unset, or if set to
!         <literal>none</literal> (or any other value than those above) then
!         no queries are displayed.
          </para>
          </listitem>
        </varlistentry>
***************
*** 2892,2899 **** bar
           list. If set to a value of <literal>ignoredups</literal>, lines
           matching the previous history line are not entered. A value of
           <literal>ignoreboth</literal> combines the two options. If
!          unset, or if set to any other value than those above, all lines
!          read in interactive mode are saved on the history list.
          </para>
          <note>
          <para>
--- 2894,2902 ----
           list. If set to a value of <literal>ignoredups</literal>, lines
           matching the previous history line are not entered. A value of
           <literal>ignoreboth</literal> combines the two options. If
!          unset, or if set to <literal>none</literal> (or any other value
!          than those above), all lines read in interactive mode are
!          saved on the history list.
          </para>
          <note>
          <para>
*** a/src/bin/psql/tab-complete.c
--- b/src/bin/psql/tab-complete.c
***************
*** 813,820 **** static char *_complete_from_query(int is_schema_query,
  					 const char *text, int state);
  static char *complete_from_list(const char *text, int state);
  static char *complete_from_const(const char *text, int state);
  static char **complete_from_variables(const char *text,
! 						const char *prefix, const char *suffix);
  static char *complete_from_files(const char *text, int state);
  
  static char *pg_strdup_keyword_case(const char *s, const char *ref);
--- 813,823 ----
  					 const char *text, int state);
  static char *complete_from_list(const char *text, int state);
  static char *complete_from_const(const char *text, int state);
+ static void append_variable_names(char ***varnames, int *nvars,
+ 								  int *maxvars, const char *varname,
+ 								  const char *prefix, const char *suffix);
  static char **complete_from_variables(const char *text,
! 					  const char *prefix, const char *suffix, bool need_value);
  static char *complete_from_files(const char *text, int state);
  
  static char *pg_strdup_keyword_case(const char *s, const char *ref);
***************
*** 925,935 **** psql_completion(const char *text, int start, int end)
  	else if (text[0] == ':' && text[1] != ':')
  	{
  		if (text[1] == '\'')
! 			matches = complete_from_variables(text, ":'", "'");
  		else if (text[1] == '"')
! 			matches = complete_from_variables(text, ":\"", "\"");
  		else
! 			matches = complete_from_variables(text, ":", "");
  	}
  
  	/* If no previous word, suggest one of the basic sql commands */
--- 928,938 ----
  	else if (text[0] == ':' && text[1] != ':')
  	{
  		if (text[1] == '\'')
! 			matches = complete_from_variables(text, ":'", "'", true);
  		else if (text[1] == '"')
! 			matches = complete_from_variables(text, ":\"", "\"", true);
  		else
! 			matches = complete_from_variables(text, ":", "", true);
  	}
  
  	/* If no previous word, suggest one of the basic sql commands */
***************
*** 3604,3612 **** psql_completion(const char *text, int start, int end)
  			COMPLETE_WITH_LIST_CS(my_list);
  		}
  	}
  	else if (strcmp(prev_wd, "\\set") == 0)
  	{
! 		matches = complete_from_variables(text, "", "");
  	}
  	else if (strcmp(prev_wd, "\\sf") == 0 || strcmp(prev_wd, "\\sf+") == 0)
  		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
--- 3607,3677 ----
  			COMPLETE_WITH_LIST_CS(my_list);
  		}
  	}
+ 	else if (strcmp(prev_wd, "\\unset") == 0)
+ 	{
+ 		matches = complete_from_variables(text, "", "", true);
+ 	}
  	else if (strcmp(prev_wd, "\\set") == 0)
  	{
! 		matches = complete_from_variables(text, "", "", false);
! 	}
! 	else if (strcmp(prev2_wd, "\\set") == 0)
! 	{
! 		static const char *const boolean_value_list[] =
! 		{"on", "off", NULL};
! 
! 		if (strcmp(prev_wd, "AUTOCOMMIT") == 0)
! 			COMPLETE_WITH_LIST_CS(boolean_value_list);
! 		else if (strcmp(prev_wd, "COMP_KEYWORD_CASE") == 0)
! 		{
! 			static const char *const my_list[] =
! 			{"lower", "upper", "preserve-lower", "preserve-upper", NULL};
! 
! 			COMPLETE_WITH_LIST_CS(my_list);
! 		}
! 		else if (strcmp(prev_wd, "ECHO") == 0)
! 		{
! 			static const char *const my_list[] =
! 			{"errors", "queries", "all", "none", NULL};
! 
! 			COMPLETE_WITH_LIST_CS(my_list);
! 		}
! 		else if (strcmp(prev_wd, "ECHO_HIDDEN") == 0)
! 		{
! 			static const char *const my_list[] =
! 			{"noexec", "off", "on", NULL};
! 
! 			COMPLETE_WITH_LIST_CS(my_list);
! 		}
! 		else if (strcmp(prev_wd, "HISTCONTROL") == 0)
! 		{
! 			static const char *const my_list[] =
! 			{"ignorespace", "ignoredups", "ignoreboth", "none", NULL};
! 
! 			COMPLETE_WITH_LIST_CS(my_list);
! 		}
! 		else if (strcmp(prev_wd, "ON_ERROR_ROLLBACK") == 0)
! 		{
! 			static const char *const my_list[] =
! 			{"on", "off", "interactive", NULL};
! 
! 			COMPLETE_WITH_LIST_CS(my_list);
! 		}
! 		else if (strcmp(prev_wd, "ON_ERROR_STOP") == 0)
! 			COMPLETE_WITH_LIST_CS(boolean_value_list);
! 		else if (strcmp(prev_wd, "QUIET") == 0)
! 			COMPLETE_WITH_LIST_CS(boolean_value_list);
! 		else if (strcmp(prev_wd, "SINGLELINE") == 0)
! 			COMPLETE_WITH_LIST_CS(boolean_value_list);
! 		else if (strcmp(prev_wd, "SINGLESTEP") == 0)
! 			COMPLETE_WITH_LIST_CS(boolean_value_list);
! 		else if (strcmp(prev_wd, "VERBOSITY") == 0)
! 		{
! 			static const char *const my_list[] =
! 			{"default", "verbose", "terse", NULL};
! 
! 			COMPLETE_WITH_LIST_CS(my_list);
! 		}
  	}
  	else if (strcmp(prev_wd, "\\sf") == 0 || strcmp(prev_wd, "\\sf+") == 0)
  		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
***************
*** 4053,4064 **** complete_from_const(const char *text, int state)
  
  
  /*
   * This function supports completion with the name of a psql variable.
   * The variable names can be prefixed and suffixed with additional text
!  * to support quoting usages.
   */
  static char **
! complete_from_variables(const char *text, const char *prefix, const char *suffix)
  {
  	char	  **matches;
  	char	  **varnames;
--- 4118,4156 ----
  
  
  /*
+  * This function appends the variable name with prefix and suffix to
+  * the variable names array.
+  */
+ static void
+ append_variable_names(char ***varnames, int *nvars,
+ 					  int *maxvars, const char *varname,
+ 					  const char *prefix, const char *suffix)
+ {
+ 	if (*nvars >= *maxvars)
+ 	{
+ 		*maxvars *= 2;
+ 		*varnames = (char **) realloc(*varnames,
+ 									  ((*maxvars) + 1) * sizeof(char *));
+ 		if (!(*varnames))
+ 		{
+ 			psql_error("out of memory\n");
+ 			exit(EXIT_FAILURE);
+ 		}
+ 	}
+ 
+ 	(*varnames)[(*nvars)++] = psprintf("%s%s%s", prefix, varname, suffix);
+ }
+ 
+ 
+ /*
   * This function supports completion with the name of a psql variable.
   * The variable names can be prefixed and suffixed with additional text
!  * to support quoting usages. If need_value is true, only the variables
!  * that have the set values are picked up.
   */
  static char **
! complete_from_variables(const char *text, const char *prefix, const char *suffix,
! 						bool need_value)
  {
  	char	  **matches;
  	char	  **varnames;
***************
*** 4067,4089 **** complete_from_variables(const char *text, const char *prefix, const char *suffix
  	int			i;
  	struct _variable *ptr;
  
  	varnames = (char **) pg_malloc((maxvars + 1) * sizeof(char *));
  
  	for (ptr = pset.vars->next; ptr; ptr = ptr->next)
  	{
! 		if (nvars >= maxvars)
  		{
! 			maxvars *= 2;
! 			varnames = (char **) realloc(varnames,
! 										 (maxvars + 1) * sizeof(char *));
! 			if (!varnames)
! 			{
! 				psql_error("out of memory\n");
! 				exit(EXIT_FAILURE);
! 			}
  		}
! 
! 		varnames[nvars++] = psprintf("%s%s%s", prefix, ptr->name, suffix);
  	}
  
  	varnames[nvars] = NULL;
--- 4159,4192 ----
  	int			i;
  	struct _variable *ptr;
  
+ 	static const char *const known_varnames[] = {
+ 		"AUTOCOMMIT", "COMP_KEYWORD_CASE", "DBNAME", "ECHO", "ECHO_HIDDEN",
+ 		"ENCODING", "FETCH_COUNT", "HISTCONTROL", "HISTFILE", "HISTSIZE",
+ 		"HOST", "IGNOREEOF", "LASTOID", "ON_ERROR_ROLLBACK", "ON_ERROR_STOP",
+ 		"PORT", "PROMPT1", "PROMPT2", "PROMPT3", "QUIET", "SINGLELINE",
+ 		"SINGLESTEP", "USER", "VERBOSITY",	NULL
+ 	};
+ 
  	varnames = (char **) pg_malloc((maxvars + 1) * sizeof(char *));
  
+ 	if (!need_value)
+ 	{
+ 		for (i = 0; known_varnames[i] && nvars < maxvars; i++)
+ 			append_variable_names(&varnames, &nvars, &maxvars,
+ 								  known_varnames[i], prefix, suffix);
+ 	}
+ 
  	for (ptr = pset.vars->next; ptr; ptr = ptr->next)
  	{
! 		if (need_value && !(ptr->value))
! 			continue;
! 		for (i = 0; known_varnames[i]; i++)	/* remove duplicate entry */
  		{
! 			if (strcmp(ptr->name, known_varnames[i]) == 0)
! 				continue;
  		}
! 		append_variable_names(&varnames, &nvars, &maxvars, ptr->name,
! 							  prefix, suffix);
  	}
  
  	varnames[nvars] = NULL;
#36Pavel Stehule
pavel.stehule@gmail.com
In reply to: Fujii Masao (#35)
Re: psql: show only failed queries

2014-08-11 14:59 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Sat, Aug 9, 2014 at 4:16 AM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

Hi

2014-08-08 13:58 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Fri, Aug 8, 2014 at 4:31 AM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

2014-08-07 7:10 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Thu, Aug 7, 2014 at 6:26 AM, Pavel Stehule <

pavel.stehule@gmail.com>

wrote:

Hello

updated version patch in attachment

Thanks! But ISTM you forgot to attached the patch.

grr .. I am sorry

No problem. Thanks for the patch! Attached is the revised version of the
patch.

+ /* all psql known variables are included in list by default

*/

+    for (known_varname = known_varnames; *known_varname;
known_varname++)
+        varnames[nvars++] = pg_strdup(*known_varname);

Don't we need to append both prefix and suffix to even known
variables?

??? I am not sure if I understand well - probably system "read

only"

variables as DBNAME, USER, VERSION should not be there

I had that question because complete_from_variables() is also called

by

the
tab-completion of "\echo :" and it shows half-baked variables list.

So

I thought probably we need to change complete_from_variables() more

to

fix the problem.

I understand now.

I fixed it.

I have a question.\echo probably should not to show empty known
variable.

data for autocomplete for \echo should be same as result of "\set"

Agreed. I think that only the variables having the set values should be
displayed in "\echo :" case. So I modified complete_from_variables()
so that the unset variables are not shown in that case but all the
variables
are shown in the tab-completion of "\set".

+    else if (strcmp(prev2_wd, "\\set") == 0)
+    {
+        if (strcmp(prev_wd, "AUTOCOMMIT") == 0)

ISTM that some psql variables like IGNOREEOF are not there. Why

not?

yes, there are not complete for DBNAME, ENCODING, FETCH_COUNT,
HISTCONTROL,
HISTFILE, HISTSIZE, HOST, IGNOREEOFF, PROMPT*,USER, VERSION

There are more reasons:

* paremeter is not a enum (string, number or both): FETCH_COUNT,
PROMPT,
HISTSIZE, ..

* variable is pseudo read only - change has not any effect:

DBNAME,

ENCODING, VERSION

So HISTCONTROL should be there because it doesn't have such reasons

at

all?

yes

ISTM that you forgot to add HISTCONTROL to your patch. So I just added
that.

I added the tab-completion for "\unset" command. Like "\echo :", only
the variables having the set values should be displayed in "\unset"

case.

perfect

I changed complete_from_variables() so that it checks the memory size of
the variable array even when appending the known variables. If the

memory

size is not enough, it's dynamically increased. Practically this check
would
not be required because the initial size of the array is enough larger
than
the number of the known variables. I added this as the safe-guard.

Typo: IGNOREEOFF -> IGNOREEOF

I removed the value "none" from the value list of "ECHO" because it's

not

documented and a user might get confused when he or she sees the
undocumented
value "none". Thought?

isn't better to fix doc? I don't know any reason why we should not to
support "none"

I'm OK with this. The attached patch adds the support of "none" value both
in ECHO and HISTCONTROL variables (because HISTCONTROL had the same
problem as ECHO had), and also adds the description of that value into
the document.

I looked to code, you removed a check against duplicate varname in list.

Is

it ok?

Oh, just revived that code.

yes, It is looking well

regards

Pavel

Show quoted text

Regards,

--
Fujii Masao

#37Fujii Masao
masao.fujii@gmail.com
In reply to: Pavel Stehule (#36)
Re: psql: show only failed queries

On Tue, Aug 12, 2014 at 4:41 AM, Pavel Stehule <pavel.stehule@gmail.com> wrote:

2014-08-11 14:59 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Sat, Aug 9, 2014 at 4:16 AM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

Hi

2014-08-08 13:58 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Fri, Aug 8, 2014 at 4:31 AM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

2014-08-07 7:10 GMT+02:00 Fujii Masao <masao.fujii@gmail.com>:

On Thu, Aug 7, 2014 at 6:26 AM, Pavel Stehule
<pavel.stehule@gmail.com>
wrote:

Hello

updated version patch in attachment

Thanks! But ISTM you forgot to attached the patch.

grr .. I am sorry

No problem. Thanks for the patch! Attached is the revised version of
the
patch.

+    /* all psql known variables are included in list by default
*/
+    for (known_varname = known_varnames; *known_varname;
known_varname++)
+        varnames[nvars++] = pg_strdup(*known_varname);

Don't we need to append both prefix and suffix to even known
variables?

??? I am not sure if I understand well - probably system "read
only"
variables as DBNAME, USER, VERSION should not be there

I had that question because complete_from_variables() is also called
by
the
tab-completion of "\echo :" and it shows half-baked variables list.
So
I thought probably we need to change complete_from_variables() more
to
fix the problem.

I understand now.

I fixed it.

I have a question.\echo probably should not to show empty known
variable.

data for autocomplete for \echo should be same as result of "\set"

Agreed. I think that only the variables having the set values should be
displayed in "\echo :" case. So I modified complete_from_variables()
so that the unset variables are not shown in that case but all the
variables
are shown in the tab-completion of "\set".

+    else if (strcmp(prev2_wd, "\\set") == 0)
+    {
+        if (strcmp(prev_wd, "AUTOCOMMIT") == 0)

ISTM that some psql variables like IGNOREEOF are not there. Why
not?

yes, there are not complete for DBNAME, ENCODING, FETCH_COUNT,
HISTCONTROL,
HISTFILE, HISTSIZE, HOST, IGNOREEOFF, PROMPT*,USER, VERSION

There are more reasons:

* paremeter is not a enum (string, number or both): FETCH_COUNT,
PROMPT,
HISTSIZE, ..

* variable is pseudo read only - change has not any effect:
DBNAME,
ENCODING, VERSION

So HISTCONTROL should be there because it doesn't have such reasons
at
all?

yes

ISTM that you forgot to add HISTCONTROL to your patch. So I just added
that.

I added the tab-completion for "\unset" command. Like "\echo :", only
the variables having the set values should be displayed in "\unset"
case.

perfect

I changed complete_from_variables() so that it checks the memory size
of
the variable array even when appending the known variables. If the
memory
size is not enough, it's dynamically increased. Practically this check
would
not be required because the initial size of the array is enough larger
than
the number of the known variables. I added this as the safe-guard.

Typo: IGNOREEOFF -> IGNOREEOF

I removed the value "none" from the value list of "ECHO" because it's
not
documented and a user might get confused when he or she sees the
undocumented
value "none". Thought?

isn't better to fix doc? I don't know any reason why we should not to
support "none"

I'm OK with this. The attached patch adds the support of "none" value both
in ECHO and HISTCONTROL variables (because HISTCONTROL had the same
problem as ECHO had), and also adds the description of that value into
the document.

I looked to code, you removed a check against duplicate varname in list.
Is
it ok?

Oh, just revived that code.

yes, It is looking well

Ok, committed!

Regards,

--
Fujii Masao

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#38Pavel Stehule
pavel.stehule@gmail.com
In reply to: Fujii Masao (#37)
Re: psql: show only failed queries

Oh, just revived that code.

yes, It is looking well

Ok, committed!

super

thank you very much

Regards

Pavel

Show quoted text

Regards,

--
Fujii Masao