proposal - assign result of query to psql variable

Started by Pavel Stehuleover 13 years ago60 messageshackers
Jump to latest
#1Pavel Stehule
pavel.stehule@gmail.com

Hello

we cannot actually store result of query to psql variable

I propose a new slash statement "\eset for this purpose

Syntax:

\eset variable [, variable [..]] query -- it raise exception when
more than one row is returned or when no row is returned

Usage:

\eset var1, var2 select version(), current_date

Current workaround is not friendly and it is not usable for more than
one target variable

http://stackoverflow.com/questions/11654244/how-to-bind-a-sql-query-return-value-to-a-psql-variable/11654676#11654676

postgres=# \set myvar `psql -A -t -c "select version()" postgres `
postgres=# \echo :myvar
PostgreSQL 9.1.4 on x86_64-unknown-linux-gnu, compiled by gcc (GCC)
4.7.0 20120507 (Red Hat 4.7.0-5), 64-bit

Regards

Pavel

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Pavel Stehule (#1)
Re: proposal - assign result of query to psql variable

Pavel Stehule <pavel.stehule@gmail.com> writes:

\eset variable [, variable [..]] query -- it raise exception when
more than one row is returned or when no row is returned

Better would be a variant on \g, that is you type in the query and
then tell it where to put the result. We have learned the hard way
that putting SQL commands into the arguments of backslash commands
is a horrid idea. Maybe

select x,y,... from ...
\gset var1 var2 ...

regards, tom lane

#3Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#2)
Re: proposal - assign result of query to psql variable

2012/7/26 Tom Lane <tgl@sss.pgh.pa.us>:

Pavel Stehule <pavel.stehule@gmail.com> writes:

\eset variable [, variable [..]] query -- it raise exception when
more than one row is returned or when no row is returned

Better would be a variant on \g, that is you type in the query and
then tell it where to put the result. We have learned the hard way
that putting SQL commands into the arguments of backslash commands
is a horrid idea. Maybe

select x,y,... from ...
\gset var1 var2 ...

it could be

Pavel

Show quoted text

regards, tom lane

#4David Fetter
david@fetter.org
In reply to: Tom Lane (#2)
Re: proposal - assign result of query to psql variable

On Thu, Jul 26, 2012 at 01:36:17AM -0400, Tom Lane wrote:

Pavel Stehule <pavel.stehule@gmail.com> writes:

\eset variable [, variable [..]] query -- it raise exception when
more than one row is returned or when no row is returned

Better would be a variant on \g, that is you type in the query and
then tell it where to put the result. We have learned the hard way
that putting SQL commands into the arguments of backslash commands
is a horrid idea. Maybe

select x,y,... from ...
\gset var1 var2 ...

How about

\gset var1,,,var2,var3...

The above shows how one would skip assigning variables in the target
list, which one might want to do.

Cheers,
David.
--
David Fetter <david@fetter.org> http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david.fetter@gmail.com
iCal: webcal://www.tripit.com/feed/ical/people/david74/tripit.ics

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate

#5Pavel Stehule
pavel.stehule@gmail.com
In reply to: David Fetter (#4)
Re: proposal - assign result of query to psql variable

2012/7/26 David Fetter <david@fetter.org>:

On Thu, Jul 26, 2012 at 01:36:17AM -0400, Tom Lane wrote:

Pavel Stehule <pavel.stehule@gmail.com> writes:

\eset variable [, variable [..]] query -- it raise exception when
more than one row is returned or when no row is returned

Better would be a variant on \g, that is you type in the query and
then tell it where to put the result. We have learned the hard way
that putting SQL commands into the arguments of backslash commands
is a horrid idea. Maybe

select x,y,... from ...
\gset var1 var2 ...

How about

\gset var1,,,var2,var3...

I don't like this - you can use fake variable - and ignoring some
variable has no big effect on client

Pavel

Show quoted text

The above shows how one would skip assigning variables in the target
list, which one might want to do.

Cheers,
David.
--
David Fetter <david@fetter.org> http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david.fetter@gmail.com
iCal: webcal://www.tripit.com/feed/ical/people/david74/tripit.ics

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate

#6David Fetter
david@fetter.org
In reply to: Pavel Stehule (#5)
Re: proposal - assign result of query to psql variable

On Thu, Jul 26, 2012 at 08:31:13AM +0200, Pavel Stehule wrote:

2012/7/26 David Fetter <david@fetter.org>:

On Thu, Jul 26, 2012 at 01:36:17AM -0400, Tom Lane wrote:

Pavel Stehule <pavel.stehule@gmail.com> writes:

\eset variable [, variable [..]] query -- it raise exception when
more than one row is returned or when no row is returned

Better would be a variant on \g, that is you type in the query and
then tell it where to put the result. We have learned the hard way
that putting SQL commands into the arguments of backslash commands
is a horrid idea. Maybe

select x,y,... from ...
\gset var1 var2 ...

How about

\gset var1,,,var2,var3...

I don't like this - you can use fake variable - and ignoring some
variable has no big effect on client

Why assign to a variable you'll never use?

Cheers,
David.

P.S. The bike shed should be puce with blaze orange pin-striping.
--
David Fetter <david@fetter.org> http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david.fetter@gmail.com
iCal: webcal://www.tripit.com/feed/ical/people/david74/tripit.ics

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate

#7Pavel Stehule
pavel.stehule@gmail.com
In reply to: David Fetter (#6)
Re: proposal - assign result of query to psql variable

2012/7/26 David Fetter <david@fetter.org>:

On Thu, Jul 26, 2012 at 08:31:13AM +0200, Pavel Stehule wrote:

2012/7/26 David Fetter <david@fetter.org>:

On Thu, Jul 26, 2012 at 01:36:17AM -0400, Tom Lane wrote:

Pavel Stehule <pavel.stehule@gmail.com> writes:

\eset variable [, variable [..]] query -- it raise exception when
more than one row is returned or when no row is returned

Better would be a variant on \g, that is you type in the query and
then tell it where to put the result. We have learned the hard way
that putting SQL commands into the arguments of backslash commands
is a horrid idea. Maybe

select x,y,... from ...
\gset var1 var2 ...

How about

\gset var1,,,var2,var3...

I don't like this - you can use fake variable - and ignoring some
variable has no big effect on client

Why assign to a variable you'll never use?

so why you get data from server, when you would not to use it ?

no offence, probably it is not hard to implement it - because we use
own parser, but I see this proposal little bit obscure

Tom - your proposal release of stored dataset just before next
statement, not like now on the end of statement?

Regards

Pavel

Show quoted text

Cheers,
David.

P.S. The bike shed should be puce with blaze orange pin-striping.
--
David Fetter <david@fetter.org> http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david.fetter@gmail.com
iCal: webcal://www.tripit.com/feed/ical/people/david74/tripit.ics

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate

#8Tom Lane
tgl@sss.pgh.pa.us
In reply to: Pavel Stehule (#7)
Re: proposal - assign result of query to psql variable

Pavel Stehule <pavel.stehule@gmail.com> writes:

2012/7/26 David Fetter <david@fetter.org>:

How about
\gset var1,,,var2,var3...

I don't like this - you can use fake variable - and ignoring some
variable has no big effect on client

Why assign to a variable you'll never use?

so why you get data from server, when you would not to use it ?

Yeah. I don't see why you'd be likely to write a select that computes
columns you don't actually want.

Tom - your proposal release of stored dataset just before next
statement, not like now on the end of statement?

Huh? I think you'd assign the values to the variables and then PQclear
the result right away.

regards, tom lane

#9Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#8)
Re: proposal - assign result of query to psql variable

Hello

2012/7/27 Tom Lane <tgl@sss.pgh.pa.us>:

Pavel Stehule <pavel.stehule@gmail.com> writes:

2012/7/26 David Fetter <david@fetter.org>:

How about
\gset var1,,,var2,var3...

I don't like this - you can use fake variable - and ignoring some
variable has no big effect on client

Why assign to a variable you'll never use?

so why you get data from server, when you would not to use it ?

Yeah. I don't see why you'd be likely to write a select that computes
columns you don't actually want.

Tom - your proposal release of stored dataset just before next
statement, not like now on the end of statement?

Huh? I think you'd assign the values to the variables and then PQclear
the result right away.

yes - I didn't understand \g mechanism well.

Here is patch - it is not nice at this moment and it is little bit
longer than I expected - but it works

It supports David's syntax

postgres=# select 'Hello', 'World' \gset a,b
postgres=# \echo :'a' :'b'
'Hello' 'World'
postgres=# select 'Hello', 'World';
?column? │ ?column?
──────────┼──────────
Hello │ World
(1 row)

postgres=# \gset a
to few target variables
postgres=# \gset a,
postgres=# \echo :'a'
'Hello'

Regards

Pavel

Show quoted text

regards, tom lane

Attachments:

gset.patchapplication/octet-stream; name=gset.patchDownload+307-7
#10David Fetter
david@fetter.org
In reply to: Pavel Stehule (#9)
Re: proposal - assign result of query to psql variable

On Sat, Jul 28, 2012 at 06:11:21PM +0200, Pavel Stehule wrote:

Hello

2012/7/27 Tom Lane <tgl@sss.pgh.pa.us>:

Pavel Stehule <pavel.stehule@gmail.com> writes:

2012/7/26 David Fetter <david@fetter.org>:

How about
\gset var1,,,var2,var3...

I don't like this - you can use fake variable - and ignoring some
variable has no big effect on client

Why assign to a variable you'll never use?

so why you get data from server, when you would not to use it ?

Yeah. I don't see why you'd be likely to write a select that computes
columns you don't actually want.

Tom - your proposal release of stored dataset just before next
statement, not like now on the end of statement?

Huh? I think you'd assign the values to the variables and then PQclear
the result right away.

yes - I didn't understand \g mechanism well.

Here is patch - it is not nice at this moment and it is little bit
longer than I expected - but it works

It supports David's syntax

postgres=# select 'Hello', 'World' \gset a,b
postgres=# \echo :'a' :'b'
'Hello' 'World'
postgres=# select 'Hello', 'World';
?column? │ ?column?
──────────┼──────────
Hello │ World
(1 row)

postgres=# \gset a
to few target variables
postgres=# \gset a,
postgres=# \echo :'a'
'Hello'

Regards

Pavel

Teensy code cleanup (trailing space) and SGML docs added.

Cheers,
David.
--
David Fetter <david@fetter.org> http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david.fetter@gmail.com
iCal: webcal://www.tripit.com/feed/ical/people/david74/tripit.ics

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate

Attachments:

gset_02.difftext/plain; charset=us-asciiDownload+328-25
#11Pavel Stehule
pavel.stehule@gmail.com
In reply to: David Fetter (#10)
Re: proposal - assign result of query to psql variable

Hello

there is new version of this patch

* cleaned var list parser
* new regress tests
* support FETCH_COUNT > 0

Regards

Pavel Stehule

2012/8/1 David Fetter <david@fetter.org>:

Show quoted text

On Sat, Jul 28, 2012 at 06:11:21PM +0200, Pavel Stehule wrote:

Hello

2012/7/27 Tom Lane <tgl@sss.pgh.pa.us>:

Pavel Stehule <pavel.stehule@gmail.com> writes:

2012/7/26 David Fetter <david@fetter.org>:

How about
\gset var1,,,var2,var3...

I don't like this - you can use fake variable - and ignoring some
variable has no big effect on client

Why assign to a variable you'll never use?

so why you get data from server, when you would not to use it ?

Yeah. I don't see why you'd be likely to write a select that computes
columns you don't actually want.

Tom - your proposal release of stored dataset just before next
statement, not like now on the end of statement?

Huh? I think you'd assign the values to the variables and then PQclear
the result right away.

yes - I didn't understand \g mechanism well.

Here is patch - it is not nice at this moment and it is little bit
longer than I expected - but it works

It supports David's syntax

postgres=# select 'Hello', 'World' \gset a,b
postgres=# \echo :'a' :'b'
'Hello' 'World'
postgres=# select 'Hello', 'World';
?column? │ ?column?
──────────┼──────────
Hello │ World
(1 row)

postgres=# \gset a
to few target variables
postgres=# \gset a,
postgres=# \echo :'a'
'Hello'

Regards

Pavel

Teensy code cleanup (trailing space) and SGML docs added.

Cheers,
David.
--
David Fetter <david@fetter.org> http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david.fetter@gmail.com
iCal: webcal://www.tripit.com/feed/ical/people/david74/tripit.ics

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate

Attachments:

gset_03.diffapplication/octet-stream; name=gset_03.diffDownload+452-42
#12Shigeru Hanada
shigeru.hanada@gmail.com
In reply to: Pavel Stehule (#11)
Re: proposal - assign result of query to psql variable

On Fri, Aug 10, 2012 at 3:21 AM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

there is new version of this patch

* cleaned var list parser
* new regress tests
* support FETCH_COUNT > 0

Here are my review comments.

Submission
==========
The patch is formatted in context diff style, and it could be applied
cleanly against latest master. This patch include document and tests,
but IMO they need some enhancement.

Usability
=========
This patch provides new psql command \gset which sends content of query
buffer to server, and stores result of the query into psql variables.
The name "\gset" is mixture of \g, which sends result to file or pipe,
and \set, which sets variable to some value, so it would sound natural
to psql users.

Freature test
=============
Compile completed without warning. Regression tests for \gset passed,
but I have some comments on them.

- Other regression tests have comment "-- ERROR" just after queries
which should fail. It would be nice to follow this manner.
- Typo "to few" in expected file and source file.
- How about adding testing "\gset" (no variable list) to "should fail"?
- Is it intentional that \gset can set special variables such as
AUTOCOMMIT and HOST? I don't see any downside for this behavior,
because \set also can do that, but it is not documented nor tested at all.

Document
========
- Adding some description of \gset command, especially about limitation
of variable list, seems necessary.
- In addition to the meta-command section, "Advanced features" section
mentions how to set psql's variables, so we would need some mention
there too.
- The term "target list" might not be familiar to users, since it
appears in only sections mentioning PG internal relatively. I think
that the feature described in the section "Retrieving Query Results" in
ECPG document is similar to this feature.
http://www.postgresql.org/docs/devel/static/ecpg-variables.html

Coding
======
The code follows our coding conventions. Here are comments for coding.

- Some typo found in comments, please see attached patch.
- There is a code path which doesn't print error message even if libpq
reported error (PGRES_BAD_RESPONSE, PGRES_NONFATAL_ERROR,
PGRES_FATAL_ERROR) in StoreQueryResult. Is this intentional? FYI, ecpg
prints "bad response" message for those errors.

Although I'll look the code more closely later, but anyway I marked the
patch "Waiting on Author" for comments above.

Regards,
--
Shigeru HANADA

Attachments:

fix_typo.patchtext/plain; charset=Shift_JIS; name=fix_typo.patchDownload+5-5
#13Pavel Stehule
pavel.stehule@gmail.com
In reply to: Shigeru Hanada (#12)
Re: proposal - assign result of query to psql variable

Hello

2012/9/19 Shigeru HANADA <shigeru.hanada@gmail.com>:

On Fri, Aug 10, 2012 at 3:21 AM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

there is new version of this patch

* cleaned var list parser
* new regress tests
* support FETCH_COUNT > 0

Here are my review comments.

Submission
==========
The patch is formatted in context diff style, and it could be applied
cleanly against latest master. This patch include document and tests,
but IMO they need some enhancement.

Usability
=========
This patch provides new psql command \gset which sends content of query
buffer to server, and stores result of the query into psql variables.
The name "\gset" is mixture of \g, which sends result to file or pipe,
and \set, which sets variable to some value, so it would sound natural
to psql users.

Freature test
=============
Compile completed without warning. Regression tests for \gset passed,
but I have some comments on them.

- Other regression tests have comment "-- ERROR" just after queries
which should fail. It would be nice to follow this manner.
- Typo "to few" in expected file and source file.
- How about adding testing "\gset" (no variable list) to "should fail"?
- Is it intentional that \gset can set special variables such as
AUTOCOMMIT and HOST? I don't see any downside for this behavior,
because \set also can do that, but it is not documented nor tested at all.

I use a same "SetVariable" function, so a behave should be same

Document
========
- Adding some description of \gset command, especially about limitation
of variable list, seems necessary.
- In addition to the meta-command section, "Advanced features" section
mentions how to set psql's variables, so we would need some mention
there too.
- The term "target list" might not be familiar to users, since it
appears in only sections mentioning PG internal relatively. I think
that the feature described in the section "Retrieving Query Results" in
ECPG document is similar to this feature.
http://www.postgresql.org/docs/devel/static/ecpg-variables.html

I invite any proposals about enhancing documentation. Personally I am
a PostgreSQL developer, so I don't known any different term other than
"target list" - but any user friendly description is welcome.

Coding
======
The code follows our coding conventions. Here are comments for coding.

- Some typo found in comments, please see attached patch.
- There is a code path which doesn't print error message even if libpq
reported error (PGRES_BAD_RESPONSE, PGRES_NONFATAL_ERROR,
PGRES_FATAL_ERROR) in StoreQueryResult. Is this intentional? FYI, ecpg
prints "bad response" message for those errors.

yes - it is question. I use same pattern like PrintQueryResult, but
"bad response" message should be used.

I am sending updated patch

Show quoted text

Although I'll look the code more closely later, but anyway I marked the
patch "Waiting on Author" for comments above.

Regards,
--
Shigeru HANADA

Attachments:

gset_04.diffapplication/octet-stream; name=gset_04.diffDownload+453-42
#14Shigeru Hanada
shigeru.hanada@gmail.com
In reply to: Pavel Stehule (#13)
Re: proposal - assign result of query to psql variable

Hi Pavel,

(2012/09/21 2:01), Pavel Stehule wrote:

- Is it intentional that \gset can set special variables such as
AUTOCOMMIT and HOST? I don't see any downside for this behavior,
because \set also can do that, but it is not documented nor tested at all.

I use a same "SetVariable" function, so a behave should be same

It seems reasonable.

Document
========
- Adding some description of \gset command, especially about limitation
of variable list, seems necessary.
- In addition to the meta-command section, "Advanced features" section
mentions how to set psql's variables, so we would need some mention
there too.
- The term "target list" might not be familiar to users, since it
appears in only sections mentioning PG internal relatively. I think
that the feature described in the section "Retrieving Query Results" in
ECPG document is similar to this feature.
http://www.postgresql.org/docs/devel/static/ecpg-variables.html

I invite any proposals about enhancing documentation. Personally I am
a PostgreSQL developer, so I don't known any different term other than
"target list" - but any user friendly description is welcome.

How about to say "stores the query's result output into variable"?
Please see attached file for my proposal. I also mentioned about 1-row
limit and omit of variable.

Coding
======
The code follows our coding conventions. Here are comments for coding.

- Some typo found in comments, please see attached patch.
- There is a code path which doesn't print error message even if libpq
reported error (PGRES_BAD_RESPONSE, PGRES_NONFATAL_ERROR,
PGRES_FATAL_ERROR) in StoreQueryResult. Is this intentional? FYI, ecpg
prints "bad response" message for those errors.

yes - it is question. I use same pattern like PrintQueryResult, but
"bad response" message should be used.

I am sending updated patch

It seems ok.

BTW, as far as I see, no psql backslash command including \setenv (it
was added in 9.2) has regression test in core (I mean src/test/regress).
Is there any convention about this issue? If psql backslash commands
(or any psql feature else) don't need regression test, we can remove
psql.(sql|out).
# Of course we need to test new feature by hand.

Anyway, IMO the name psql impresses larger area than the patch
implements. How about to rename psql to psql_cmd or backslash_cmd than
psql as regression test name?

--
Shigeru HANADA

Attachments:

gset_05.difftext/x-patch; name=gset_05.diffDownload+15-5
#15Pavel Stehule
pavel.stehule@gmail.com
In reply to: Shigeru Hanada (#14)
Re: proposal - assign result of query to psql variable

Hello

2012/9/21 Shigeru HANADA <shigeru.hanada@gmail.com>:

Hi Pavel,

(2012/09/21 2:01), Pavel Stehule wrote:

- Is it intentional that \gset can set special variables such as
AUTOCOMMIT and HOST? I don't see any downside for this behavior,
because \set also can do that, but it is not documented nor tested at all.

I use a same "SetVariable" function, so a behave should be same

It seems reasonable.

Document
========
- Adding some description of \gset command, especially about limitation
of variable list, seems necessary.
- In addition to the meta-command section, "Advanced features" section
mentions how to set psql's variables, so we would need some mention
there too.
- The term "target list" might not be familiar to users, since it
appears in only sections mentioning PG internal relatively. I think
that the feature described in the section "Retrieving Query Results" in
ECPG document is similar to this feature.
http://www.postgresql.org/docs/devel/static/ecpg-variables.html

I invite any proposals about enhancing documentation. Personally I am
a PostgreSQL developer, so I don't known any different term other than
"target list" - but any user friendly description is welcome.

How about to say "stores the query's result output into variable"?
Please see attached file for my proposal. I also mentioned about 1-row
limit and omit of variable.

should be

Coding
======
The code follows our coding conventions. Here are comments for coding.

- Some typo found in comments, please see attached patch.
- There is a code path which doesn't print error message even if libpq
reported error (PGRES_BAD_RESPONSE, PGRES_NONFATAL_ERROR,
PGRES_FATAL_ERROR) in StoreQueryResult. Is this intentional? FYI, ecpg
prints "bad response" message for those errors.

yes - it is question. I use same pattern like PrintQueryResult, but
"bad response" message should be used.

I am sending updated patch

It seems ok.

BTW, as far as I see, no psql backslash command including \setenv (it
was added in 9.2) has regression test in core (I mean src/test/regress).
Is there any convention about this issue? If psql backslash commands
(or any psql feature else) don't need regression test, we can remove
psql.(sql|out).
# Of course we need to test new feature by hand.

It is question for Tom or David - only server side functionalities has
regress tests. But result of some backslash command is verified in
other regress tests. I would to see some regression tests for this
functionality.

Anyway, IMO the name psql impresses larger area than the patch
implements. How about to rename psql to psql_cmd or backslash_cmd than
psql as regression test name?

I have no idea - psql_cmd is good name

Regards

Pavel

Show quoted text

--
Shigeru HANADA

#16Pavel Stehule
pavel.stehule@gmail.com
In reply to: Pavel Stehule (#15)
Re: proposal - assign result of query to psql variable

Hello

here is updated version of gset patch.

* merge Shigeru's doc patch
* rename psql regression test from "psql" to "psql_cmd"

Regards

Pavel Stehule

2012/9/27 Pavel Stehule <pavel.stehule@gmail.com>:

Show quoted text

Hello

2012/9/21 Shigeru HANADA <shigeru.hanada@gmail.com>:

Hi Pavel,

(2012/09/21 2:01), Pavel Stehule wrote:

- Is it intentional that \gset can set special variables such as
AUTOCOMMIT and HOST? I don't see any downside for this behavior,
because \set also can do that, but it is not documented nor tested at all.

I use a same "SetVariable" function, so a behave should be same

It seems reasonable.

Document
========
- Adding some description of \gset command, especially about limitation
of variable list, seems necessary.
- In addition to the meta-command section, "Advanced features" section
mentions how to set psql's variables, so we would need some mention
there too.
- The term "target list" might not be familiar to users, since it
appears in only sections mentioning PG internal relatively. I think
that the feature described in the section "Retrieving Query Results" in
ECPG document is similar to this feature.
http://www.postgresql.org/docs/devel/static/ecpg-variables.html

I invite any proposals about enhancing documentation. Personally I am
a PostgreSQL developer, so I don't known any different term other than
"target list" - but any user friendly description is welcome.

How about to say "stores the query's result output into variable"?
Please see attached file for my proposal. I also mentioned about 1-row
limit and omit of variable.

should be

Coding
======
The code follows our coding conventions. Here are comments for coding.

- Some typo found in comments, please see attached patch.
- There is a code path which doesn't print error message even if libpq
reported error (PGRES_BAD_RESPONSE, PGRES_NONFATAL_ERROR,
PGRES_FATAL_ERROR) in StoreQueryResult. Is this intentional? FYI, ecpg
prints "bad response" message for those errors.

yes - it is question. I use same pattern like PrintQueryResult, but
"bad response" message should be used.

I am sending updated patch

It seems ok.

BTW, as far as I see, no psql backslash command including \setenv (it
was added in 9.2) has regression test in core (I mean src/test/regress).
Is there any convention about this issue? If psql backslash commands
(or any psql feature else) don't need regression test, we can remove
psql.(sql|out).
# Of course we need to test new feature by hand.

It is question for Tom or David - only server side functionalities has
regress tests. But result of some backslash command is verified in
other regress tests. I would to see some regression tests for this
functionality.

Anyway, IMO the name psql impresses larger area than the patch
implements. How about to rename psql to psql_cmd or backslash_cmd than
psql as regression test name?

I have no idea - psql_cmd is good name

Regards

Pavel

--
Shigeru HANADA

Attachments:

gset_06.diffapplication/octet-stream; name=gset_06.diffDownload+465-44
#17Shigeru Hanada
shigeru.hanada@gmail.com
In reply to: Pavel Stehule (#16)
Re: proposal - assign result of query to psql variable

Hi Pavel,

On Sat, Oct 13, 2012 at 12:58 AM, Pavel Stehule
<pavel.stehule@gmail.com> wrote:

* merge Shigeru's doc patch
* rename psql regression test from "psql" to "psql_cmd"

Those changes seem good.

Besides, I found an error message which doesn't end with '¥n' in
common.c. In general, messages passed to psql_error end with '¥n',
unless additional information follows. Please see attached patch for
additional change.

After you determine whether it's ok or unnecessary, I'll mark this patch
as "Ready for committer".

Regards,
--
Shigeru HANADA

Attachments:

gset_07.difftext/plain; charset=UTF-8; name=gset_07.diff; x-mac-creator=0; x-mac-type=0Download+465-44
#18Pavel Stehule
pavel.stehule@gmail.com
In reply to: Shigeru Hanada (#17)
Re: proposal - assign result of query to psql variable

2012/10/13 Shigeru HANADA <shigeru.hanada@gmail.com>:

Hi Pavel,

On Sat, Oct 13, 2012 at 12:58 AM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

* merge Shigeru's doc patch
* rename psql regression test from "psql" to "psql_cmd"

Those changes seem good.

Besides, I found an error message which doesn't end with '¥n' in common.c.
In general, messages passed to psql_error end with '¥n', unless additional
information follows. Please see attached patch for additional change.

After you determine whether it's ok or unnecessary, I'll mark this patch as
"Ready for committer".

it is ok, thank you

Pavel

Show quoted text

Regards,
--
Shigeru HANADA

#19Erik Rijkers
er@xs4all.nl
In reply to: Pavel Stehule (#18)
Re: proposal - assign result of query to psql variable

On Sat, October 13, 2012 19:26, Pavel Stehule wrote:

2012/10/13 Shigeru HANADA <shigeru.hanada@gmail.com>:

After you determine whether it's ok or unnecessary, I'll mark this patch as
"Ready for committer".

I found this behaviour which I think must count as a bug.
\gset doesn't allow more \\-separated lines behind it:

Only the last of these commands is problematic, and giving the syntax error

$ psql
psql (9.3devel-psql_var-20121012_2345-8b728e5c6e0ce6b6d6f54b92b390f14aa1aca6db)
Type "help" for help.

testdb=# select 1,2 \gset x,y
testdb=# \echo :x
1
testdb=# \echo :y
2
testdb=# \echo :x \\ \echo :y
1
2
testdb=# select 1,2 \gset x,y \\ \echo :x
\gset: syntax error
testdb=#

It'd be nice if it could be made to work

Thanks

Erik Rijkers

#20Pavel Stehule
pavel.stehule@gmail.com
In reply to: Erik Rijkers (#19)
Re: proposal - assign result of query to psql variable

Hello

2012/10/14 Erik Rijkers <er@xs4all.nl>:

On Sat, October 13, 2012 19:26, Pavel Stehule wrote:

2012/10/13 Shigeru HANADA <shigeru.hanada@gmail.com>:

After you determine whether it's ok or unnecessary, I'll mark this patch as
"Ready for committer".

I found this behaviour which I think must count as a bug.
\gset doesn't allow more \\-separated lines behind it:

Only the last of these commands is problematic, and giving the syntax error

$ psql
psql (9.3devel-psql_var-20121012_2345-8b728e5c6e0ce6b6d6f54b92b390f14aa1aca6db)
Type "help" for help.

testdb=# select 1,2 \gset x,y
testdb=# \echo :x
1
testdb=# \echo :y
2
testdb=# \echo :x \\ \echo :y
1
2
testdb=# select 1,2 \gset x,y \\ \echo :x
\gset: syntax error
testdb=#

It'd be nice if it could be made to work

done

Regards

Pavel

Show quoted text

Thanks

Erik Rijkers

Attachments:

gset_08.diffapplication/octet-stream; name=gset_08.diffDownload+475-44
#21Tom Lane
tgl@sss.pgh.pa.us
In reply to: Pavel Stehule (#20)
#22Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#21)
#23Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#21)
#24Pavel Stehule
pavel.stehule@gmail.com
In reply to: Pavel Stehule (#23)
#25Shigeru Hanada
shigeru.hanada@gmail.com
In reply to: Pavel Stehule (#22)
#26Pavel Stehule
pavel.stehule@gmail.com
In reply to: Shigeru Hanada (#25)
#27Pavel Stehule
pavel.stehule@gmail.com
In reply to: Pavel Stehule (#26)
#28Pavel Stehule
pavel.stehule@gmail.com
In reply to: Pavel Stehule (#27)
#29Shigeru Hanada
shigeru.hanada@gmail.com
In reply to: Pavel Stehule (#28)
#30Pavel Stehule
pavel.stehule@gmail.com
In reply to: Shigeru Hanada (#29)
#31Phil Sorber
phil@omniti.com
In reply to: Pavel Stehule (#30)
#32Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Phil Sorber (#31)
#33Tom Lane
tgl@sss.pgh.pa.us
In reply to: Alvaro Herrera (#32)
#34Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Tom Lane (#33)
#35Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#33)
#36Tom Lane
tgl@sss.pgh.pa.us
In reply to: Pavel Stehule (#35)
#37Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#36)
#38Pavel Stehule
pavel.stehule@gmail.com
In reply to: Phil Sorber (#31)
#39Pavel Stehule
pavel.stehule@gmail.com
In reply to: Phil Sorber (#31)
#40Shigeru Hanada
shigeru.hanada@gmail.com
In reply to: Pavel Stehule (#39)
#41Pavel Stehule
pavel.stehule@gmail.com
In reply to: Shigeru Hanada (#40)
#42Shigeru Hanada
shigeru.hanada@gmail.com
In reply to: Pavel Stehule (#41)
#43Pavel Stehule
pavel.stehule@gmail.com
In reply to: Shigeru Hanada (#42)
#44Tom Lane
tgl@sss.pgh.pa.us
In reply to: Pavel Stehule (#41)
#45Tom Lane
tgl@sss.pgh.pa.us
In reply to: Tom Lane (#44)
#46Andrew Dunstan
andrew@dunslane.net
In reply to: Tom Lane (#45)
#47Tom Lane
tgl@sss.pgh.pa.us
In reply to: Andrew Dunstan (#46)
#48Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#45)
#49Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#47)
#50Pavel Stehule
pavel.stehule@gmail.com
In reply to: Pavel Stehule (#49)
#51Tom Lane
tgl@sss.pgh.pa.us
In reply to: Pavel Stehule (#49)
#52Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#51)
#53Shigeru Hanada
shigeru.hanada@gmail.com
In reply to: Pavel Stehule (#52)
#54Pavel Stehule
pavel.stehule@gmail.com
In reply to: Shigeru Hanada (#53)
#55Tom Lane
tgl@sss.pgh.pa.us
In reply to: Shigeru Hanada (#53)
#56Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#55)
#57Tom Lane
tgl@sss.pgh.pa.us
In reply to: Pavel Stehule (#56)
#58Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#55)
#59Tom Lane
tgl@sss.pgh.pa.us
In reply to: Pavel Stehule (#58)
#60Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#59)