Thread: \set \e and newline

\set \e and newline

From
Wim Bertels
Date:
Hello,

a quick question about
https://www.postgresql.org/docs/current/app-psql.html
and the \set option

it seems that \set does not interpret an 'enter' interactively the same
as an 'enter' in a short script made with \e 

###
* case 1:
postgres=# \set x 1
postgres=# select :x;
 ?column? 
----------
        1
###


###
* case 2:
postgres=# \e

-- enter the following the editor (the same code as above)

\set x 1
select :x;

-- save and quit

postgres=#

-- no output
-- curiosly: again \e

postgres=#\e

-- shows select 1; in the editor in v14
-- shows nothing in the editor in v13 (or recursive the content being
cut off)
###


###
variation of case 2:
postgres=# \e

-- enter the following the editor (the same code as above)

\set x 1
select :x;

-- save and quit

postgres=# select :x;
 select1select1 
----------------
              1

###


Doing the same thing with \i instead of \e does behave like i would
expect, ie the same as case 1.

This is referred to as meta-commands in de manual which are to be
affected when using \e, but \g instead of ; seems to work (while it
should be the same?)


###
variation of case 2 using \g instead of ; :
postgres=# \e

-- enter the following the editor (the same code as above)

\set x 1
select :x \g

-- save and quit

 ?column? 
----------
        1
(1 row)

postgres=# 
###


-- 
mvg,
Wim





Re: \set \e and newline

From
Laurenz Albe
Date:
On Thu, 2022-02-24 at 18:16 +0100, Wim Bertels wrote:
> it seems that \set does not interpret an 'enter' interactively the same
> as an 'enter' in a short script made with \e 
> 
> * case 1:
> postgres=# \set x 1
> postgres=# select :x;
>  ?column? 
> ----------
>         1

Ok.

> * case 2:
> postgres=# \e
> 
> -- enter the following the editor (the same code as above)
> 
> \set x 1
> select :x;
> 
> -- save and quit
> 
> postgres=#
> 
> -- no output

This was interpreted as if you had entered it in a single line:

\set x 1 select :x

So "x" is now "1select1;".

> -- curiosly: again \e
> 
> postgres=#\e
> 
> -- shows select 1; in the editor in v14

That was the last executed SQL statement.

> -- shows nothing in the editor in v13 (or recursive the content being
> cut off)

I don't quite understand what you mean, but the behavior of \e changed
(got more sane) in v14.

> variation of case 2:
> postgres=# \e
> 
> -- enter the following the editor (the same code as above)
> 
> \set x 1
> select :x;
> 
> -- save and quit
> 
> postgres=# select :x;
>  select1select1 
> ----------------
>               1

Now "x" is "1select1select1;;", so you are running

 select 1select1select1;;;

Here "select1select1" is interpreted as alias, so you get that column heading.

You can use "\echo :x" to see the value of a variable.

Yours,
Laurenz Albe
-- 
Cybertec | https://www.cybertec-postgresql.com




Re: \set \e and newline

From
Wim Bertels
Date:
Laurenz Albe schreef op vr 25-02-2022 om 10:33 [+0100]:
> On Thu, 2022-02-24 at 18:16 +0100, Wim Bertels wrote:
> This was interpreted as if you had entered it in a single line:
> 
> \set x 1 select :x
> 
> So "x" is now "1select1;".

yes

> 
> I don't quite understand what you mean, but the behavior of \e
> changed
> (got more sane) in v14.

yes,
(this is just a summary of different cases)

> 
> > variation of case 2:
> > postgres=# \e
> > 
> > -- enter the following the editor (the same code as above)
> > 
> > \set x 1
> > select :x;
> > 
> > -- save and quit
> > 
> > postgres=# select :x;
> >  select1select1 
> > ----------------
> >               1
> 
> Now "x" is "1select1select1;;", so you are running
> 
>  select 1select1select1;;;
> 
> Here "select1select1" is interpreted as alias, so you get that column
> heading.
> 
> You can use "\echo :x" to see the value of a variable.
> 

Thanks for the feedback Laurenz

I guess the main remark is, it not so intuitive that \e behaves
differently then \i

From https://www.postgresql.org/docs/current/app-psql.html
"
\e..
Type semicolon or \g to send it, or \r to cancel it by clearing the
query buffer.

Treating the buffer as a single line primarily affects meta-commands:
whatever is in the buffer after a meta-command will be taken as
argument(s) to the meta-command, even if it spans multiple lines. (Thus
you cannot make meta-command-using scripts this way. Use \i for that.)
"

### case 1: (\e)
\set x 1
select :x ;

### case 2: (\e)
\set x 1
select :x \g

resulting in the same value for x (\echo :x), but different
intermediate output with case 1 and case 2,

"
\g..
If the current query buffer is empty, the most recently sent query is
re-executed instead. Except for that behavior, \g without any arguments
is essentially equivalent to a semicolon.
"

mvg,
Wim






Re: \set \e and newline

From
Laurenz Albe
Date:
On Mon, 2022-02-28 at 12:34 +0100, Wim Bertels wrote:
> I guess the main remark is, it not so intuitive that \e behaves
> differently then \i

Ok, but if the differences are well documented, it should be ok.

You could submit a patch that changes the behavior, but people
might be unhappy if established, well documented behavior changes...

Yours,
Laurenz Albe