Thread: BUG #8094: psql prompt variable and command substitution broken

BUG #8094: psql prompt variable and command substitution broken

From
nyetter@gmail.com
Date:
The following bug has been logged on the website:

Bug reference:      8094
Logged by:          Noah Yetter
Email address:      nyetter@gmail.com
PostgreSQL version: 9.2.4
Operating system:   Linux (Amazon AMI Linux x64, kernel 3.2.39)
Description:        =


My psql PROMPT1, set through /etc/sysconfig/pgsql/psqlrc, is the following:
h:%M%040d:%/%040U:%n%040AC:%:AUTOCOMMIT:%040TX:%x%012%R%#%040

Here is the behavior I got under 9.1.5:
$ psql -U postgres
Timing is on.
psql (9.1.5)
Type "help" for help.

h:[local] d:postgres U:postgres AC:on TX:
=3D# \set AUTOCOMMIT off
h:[local] d:postgres U:postgres AC:off TX:
=3D# \set AUTOCOMMIT on
h:[local] d:postgres U:postgres AC:on TX:
=3D#

As intended, the variable substitution %:AUTOCOMMIT: causes the prompt to
show the current value of AUTOCOMMIT.

Here is the behavior under 9.2.4:
$ psql -U postgres
Timing is on.
psql (9.2.3)
Type "help" for help.

h:[local] d:postgres U:postgres AC:on: TX:
=3D# \set AUTOCOMMIT off
h:[local] d:postgres U:postgres AC:on: TX:
=3D# \set AUTOCOMMIT on
h:[local] d:postgres U:postgres AC:on: TX:
=3D#

Here you can see the variable substitution is not functioning, in that
changes to the variable value are not reflected in the prompt.  Additionally
the trailing colon of the variable substitution pattern is leaking into the
prompt.  Re-setting the prompt variable does cause the substitution though:
h:[local] d:postgres U:postgres AC:on: TX:
=3D# \set AUTOCOMMIT off
h:[local] d:postgres U:postgres AC:on: TX:
=3D# \set PROMPT1
h:%M%040d:%/%040U:%n%040AC:%:AUTOCOMMIT:%040TX:%x%012%R%#%040
h:[local] d:postgres U:postgres AC:off: TX:
=3D#

Also, inspecting the value of PROMPT1 suggests that the substitution may be
occurring when the variable is set rather than when it is drawn:
h:[local] d:postgres U:postgres AC:off: TX:
=3D# \echo :PROMPT1
h:%M%040d:%/%040U:%n%040AC:%off:%040TX:%x%012%R%#%040

Note that it says "%off:" instead of "%:AUTOCOMMIT:" which is what was set.

Command substitution using %`command` is broken in precisely the same way.

This bug also manifests in the 9.2.2 Windows x64 version of psql which I
have on my workstation.

Output of version():
PostgreSQL 9.2.3 on x86_64-redhat-linux-gnu, compiled by gcc (GCC) 4.6.2
20111027 (Red Hat 4.6.2-2), 64-bit

Output of uname -a:
Linux host.name.redacted 3.2.39-6.88.amzn1.x86_64 #1 SMP Sat Mar 2 05:13:37
UTC 2013 x86_64 x86_64 x86_64 GNU/Linux


--
Noah Yetter
Data Architect @ Craftsy

Re: BUG #8094: psql prompt variable and command substitution broken

From
Tom Lane
Date:
nyetter@gmail.com writes:
> My psql PROMPT1, set through /etc/sysconfig/pgsql/psqlrc, is the following:
> h:%M%040d:%/%040U:%n%040AC:%:AUTOCOMMIT:%040TX:%x%012%R%#%040

You did not show us exactly how you set that, but I speculate that you
weren't bothering to quote it.  9.2 is more consistent than prior
versions were about expanding :variable references in backslash
commands, which means that you'd better quote the reference to
:AUTOCOMMIT if you don't want it expanded on-the-spot before it goes
into the prompt string.  I'd suggest putting single quotes around the
argument of \set PROMPT1, viz

\set PROMPT1 'h:%M%040d:%/%040U:%n%040AC:%:AUTOCOMMIT:%040TX:%x%012%R%#%040'

            regards, tom lane

Re: BUG #8094: psql prompt variable and command substitution broken

From
Noah Yetter
Date:
Thanks, quoting does indeed fix it.  It's worth noting that comparing the
9.1 and 9.2 documentation, there is no hint that quoting/substitution
behavior changed.  Or put another way, the docs say very little about
quoting/substitution behavior in general, aside from one highly confusing
"Note:" block.  I do see the quoting change in the 9.2 release notes now
that I look.


On Thu, Apr 18, 2013 at 12:48 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

> nyetter@gmail.com writes:
> > My psql PROMPT1, set through /etc/sysconfig/pgsql/psqlrc, is the
> following:
> > h:%M%040d:%/%040U:%n%040AC:%:AUTOCOMMIT:%040TX:%x%012%R%#%040
>
> You did not show us exactly how you set that, but I speculate that you
> weren't bothering to quote it.  9.2 is more consistent than prior
> versions were about expanding :variable references in backslash
> commands, which means that you'd better quote the reference to
> :AUTOCOMMIT if you don't want it expanded on-the-spot before it goes
> into the prompt string.  I'd suggest putting single quotes around the
> argument of \set PROMPT1, viz
>
> \set PROMPT1
> 'h:%M%040d:%/%040U:%n%040AC:%:AUTOCOMMIT:%040TX:%x%012%R%#%040'
>
>                         regards, tom lane
>