Re: add line number as prompt option to psql

From: Andres Freund <andres(at)2ndquadrant(dot)com>
To: Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>
Cc: Jeevan Chalke <jeevan(dot)chalke(at)enterprisedb(dot)com>, Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: add line number as prompt option to psql
Date: 2014-08-26 13:23:20
Message-ID: 20140826132320.GM21544@awork2.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 2014-08-21 11:43:48 +0900, Sawada Masahiko wrote:
> On Wed, Aug 20, 2014 at 9:00 PM, Jeevan Chalke
> <jeevan(dot)chalke(at)enterprisedb(dot)com> wrote:
> > Hi,
> >
> > I have reviewed this:
> >
> > I have initialize cur_lineno to UINTMAX - 2. And then observed following
> > behaviour to check wrap-around.
> >
> > postgres=# \set PROMPT1 '%/[%l]%R%# '
> > postgres[18446744073709551613]=# \set PROMPT2 '%/[%l]%R%# '
> > postgres[18446744073709551613]=# select
> > postgres[18446744073709551614]-# a
> > postgres[18446744073709551615]-# ,
> > postgres[0]-# b
> > postgres[1]-# from
> > postgres[2]-# dual;
> >
> > It is wrapping to 0, where as line number always start with 1. Any issues?
> >
> > I would like to ignore this as UINTMAX lines are too much for a input
> > buffer to hold. It is almost NIL chances to hit this.
> >
> >
> > However, I think you need to use UINT64_FORMAT while printing uint64
> > number. Currently it is %u which wrap-around at UINT_MAX.
> > See how pset.lineno is displayed.
> >
> > Apart from this, I didn't see any issues in my testing.
> >
> > Patch applies cleanly. make/make install/initdb/make check all are well.
> >
>
> Thank you for reviewing the patch!
> Attached patch is latest version patch.
> I modified the output format of cur_lineno.

I like the feature - and I wanted to commit it, but enough stuff turned
up that I needed to fix that it warrants some new testing.

Stuff I've changed:
* removed include of limits.h - that probably was a rememnant from a
previous version
* removed a trailing whitespace
* expanded the documentation about %l. "The current line number" isn't
very clear. Of a file? Of all lines ever entered in psql? It's now
"The line number inside the current statement, starting from
<literal>1</>."
* Correspondingly I've changed the variable's name to stmt_lineno.
* COPY FROM ... STDIN/PROMPT3 was broken because a) the promp was only generated
once b) the lineno wasn't incremented.
* CTRL-C didn't reset the line number.
* Unfortunately I've notice here that the prompting is broken in some
common cases:

postgres[1]=# SELECT 1,
postgres[2]-# '2
postgres[2]'# 2b
postgres[2]'# 2c
postgres[2]'# 2d',
postgres[3]-# 3;
┌──────────┬──────────┬──────────┐
│ ?column? │ ?column? │ ?column? │
├──────────┼──────────┼──────────┤
│ 1 │ 2 ↵│ 3 │
│ │ 2b ↵│ │
│ │ 2c ↵│ │
│ │ 2d │ │
└──────────┴──────────┴──────────┘
(1 row)

postgres[1]=# SELECT 1,
'2
2b
2c
2d',
3
postgres[7]-#

That's rather inconsistent...

I've attached my version of the patch. Note that I've got rid of all the
PSCAN_INCOMPLETE checks... Please test!

Greetings,

Andres Freund

--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services

Attachment Content-Type Size
0001-Add-psql-PROMPT-variable-showing-which-line-of-a-sta.patch text/x-patch 6.1 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Fabrízio de Royes Mello 2014-08-26 13:25:50 Re: Patch to add support of "IF NOT EXISTS" to others "CREATE" statements
Previous Message Heikki Linnakangas 2014-08-26 13:20:41 Re: Patch to add support of "IF NOT EXISTS" to others "CREATE" statements