Re: add line number as prompt option to psql

From: Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>
To: Andres Freund <andres(at)2ndquadrant(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-31 03:06:31
Message-ID: CAD21AoApS4B7vqs4xu7fc9c8kmvy0MeUuU4quJfioqm8jYDn3g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Aug 26, 2014 at 10:23 PM, Andres Freund <andres(at)2ndquadrant(dot)com> wrote:
> 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!
>

Thank you for review comment and improving the patch!
I tested it.
Your patch always increment line number even if there is no input line
as follows.

postgres[1]=#
postgres[2]=# select
postgres[3]-# ,
postgres[4]-# from
postgres[5]-# hoge;
ERROR: syntax error at or near "," at character 8
STATEMENT: select
,
from
hoge;
ERROR: syntax error at or near ","
LINE 2: ,
^
Actually error syntax is in line 2 as postgres reported.
But it is inconsistent.
Attached patch is resolve above behavior based on your version patch.

Regards,

-------
Sawada Masahiko

Attachment Content-Type Size
psql-line-number_v7.patch application/octet-stream 5.3 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Eisentraut 2014-08-31 03:12:18 Re: pg_dump refactor patch to remove global variables
Previous Message Michael Paquier 2014-08-31 02:59:07 Re: PATCH: Allow distdir to be overridden on make command line