Re: add line number as prompt option to psql

Lists: pgsql-hackers
From: Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: add line number as prompt option to psql
Date: 2014-06-12 17:16:52
Message-ID: CAD21AoBCcSEmXvB4eF3Z+Bo+2buqK9ecidtAzV=gAniK5x0puQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi all,

The attached IWP patch is one prompt option for psql, which shows
current line number.
If the user made syntax error with too long SQL then psql outputs
message as following.

ERROR: syntax error at or near "a"
LINE 250: hoge
^
psql teaches me where syntax error is occurred, but it is not kind
when SQL is too long.
We can use write SQL with ¥e(editor) command(e.g., emacs) , and we can
know line number.
but it would make terminal log dirty . It will make analyzing of log
difficult after error is occurred.
(I think that we usually use copy & paste)

After attached this patch, we will be able to use %l option as prompting option.

e.g.,
$ cat ~/.psqlrc
\set PROMPT2 '%/[%l]%R%# '
\set PROMPT1 '%/[%l]%R%# '
$ psql -d postgres
postgres[1]=# select
postgres[2]-# *
postgres[3]-# from
postgres[4]-# hoge;

The past discussion is following.
<http://www.postgresql.org/message-id/CAFj8pRC1ruPk6+chA1jpxPh3uS_zipaBDOvmcEex4wPbp2kZMQ@mail.gmail.com>

Please give me feedback.

Regards,

-------
Sawada Masahiko

Attachment Content-Type Size
psql-line-number_v1.patch application/octet-stream 1.7 KB

From: Jeevan Chalke <jeevan(dot)chalke(at)enterprisedb(dot)com>
To: Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: add line number as prompt option to psql
Date: 2014-06-20 10:17:00
Message-ID: CAM2+6=U1uuy33Y+sW7y4AtnQz5VEo+Xst1=PyHsTsyE=i_y-TA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi Sawada Masahiko,

I liked this feature. So I have reviewed it.

Changes are straight forward and looks perfect.
No issues found with make/make install/initdb/regression.

However I would suggest removing un-necessary braces at if, as we have only
one statement into it.

if (++cur_line >= INT_MAX)
{
cur_line = 1;
}

Also following looks wrong:

postgres[1]=# select
postgres[2]-# *
postgres[3]-# from
postgres[4]-# tab;
a
---
(0 rows)

postgres[1]=# select
*
from
tab
postgres[2]-# where t > 10;
ERROR: column "t" does not exist
LINE 5: where t > 10;
^

Line number in ERROR is 5 which is correct.
But line number in psql prompt is wrong.

To get first 4 lines I have simply used up arrow followed by an enter for
which I was expecting 5 in psql prompt.
But NO it was 2 which is certainly wrong.

Need to handle above carefully.

Thanks

On Thu, Jun 12, 2014 at 10:46 PM, Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>
wrote:

> Hi all,
>
> The attached IWP patch is one prompt option for psql, which shows
> current line number.
> If the user made syntax error with too long SQL then psql outputs
> message as following.
>
> ERROR: syntax error at or near "a"
> LINE 250: hoge
> ^
> psql teaches me where syntax error is occurred, but it is not kind
> when SQL is too long.
> We can use write SQL with ¥e(editor) command(e.g., emacs) , and we can
> know line number.
> but it would make terminal log dirty . It will make analyzing of log
> difficult after error is occurred.
> (I think that we usually use copy & paste)
>
> After attached this patch, we will be able to use %l option as prompting
> option.
>
> e.g.,
> $ cat ~/.psqlrc
> \set PROMPT2 '%/[%l]%R%# '
> \set PROMPT1 '%/[%l]%R%# '
> $ psql -d postgres
> postgres[1]=# select
> postgres[2]-# *
> postgres[3]-# from
> postgres[4]-# hoge;
>
> The past discussion is following.
> <
> http://www.postgresql.org/message-id/CAFj8pRC1ruPk6+chA1jpxPh3uS_zipaBDOvmcEex4wPbp2kZMQ@mail.gmail.com
> >
>
> Please give me feedback.
>
> Regards,
>
> -------
> Sawada Masahiko
>
>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers
>
>

--
Jeevan B Chalke
Principal Software Engineer, Product Development
EnterpriseDB Corporation
The Enterprise PostgreSQL Company

Phone: +91 20 30589500

Website: www.enterprisedb.com
EnterpriseDB Blog: http://blogs.enterprisedb.com/
Follow us on Twitter: http://www.twitter.com/enterprisedb

This e-mail message (and any attachment) is intended for the use of the
individual or entity to whom it is addressed. This message contains
information from EnterpriseDB Corporation that may be privileged,
confidential, or exempt from disclosure under applicable law. If you are
not the intended recipient or authorized to receive this for the intended
recipient, any use, dissemination, distribution, retention, archiving, or
copying of this communication is strictly prohibited. If you have received
this e-mail in error, please notify the sender immediately by reply e-mail
and delete this message.


From: Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>
To: Jeevan Chalke <jeevan(dot)chalke(at)enterprisedb(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: add line number as prompt option to psql
Date: 2014-07-06 17:18:38
Message-ID: CAD21AoDrLjy-=UVCHp64NtvCJzcPDn5ZK1Z7BBGibmhDZm3fww@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Jun 20, 2014 at 7:17 PM, Jeevan Chalke
<jeevan(dot)chalke(at)enterprisedb(dot)com> wrote:
> Hi Sawada Masahiko,
>
> I liked this feature. So I have reviewed it.
>
> Changes are straight forward and looks perfect.
> No issues found with make/make install/initdb/regression.
>
> However I would suggest removing un-necessary braces at if, as we have only
> one statement into it.
>
> if (++cur_line >= INT_MAX)
> {
> cur_line = 1;
> }
>
> Also following looks wrong:
>
> postgres[1]=# select
> postgres[2]-# *
> postgres[3]-# from
> postgres[4]-# tab;
> a
> ---
> (0 rows)
>
> postgres[1]=# select
> *
> from
> tab
> postgres[2]-# where t > 10;
> ERROR: column "t" does not exist
> LINE 5: where t > 10;
> ^
>
> Line number in ERROR is 5 which is correct.
> But line number in psql prompt is wrong.
>
> To get first 4 lines I have simply used up arrow followed by an enter for
> which I was expecting 5 in psql prompt.
> But NO it was 2 which is certainly wrong.
>
> Need to handle above carefully.
>
> Thanks
>
>
> On Thu, Jun 12, 2014 at 10:46 PM, Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>
> wrote:
>>
>> Hi all,
>>
>> The attached IWP patch is one prompt option for psql, which shows
>> current line number.
>> If the user made syntax error with too long SQL then psql outputs
>> message as following.
>>
>> ERROR: syntax error at or near "a"
>> LINE 250: hoge
>> ^
>> psql teaches me where syntax error is occurred, but it is not kind
>> when SQL is too long.
>> We can use write SQL with ¥e(editor) command(e.g., emacs) , and we can
>> know line number.
>> but it would make terminal log dirty . It will make analyzing of log
>> difficult after error is occurred.
>> (I think that we usually use copy & paste)
>>
>> After attached this patch, we will be able to use %l option as prompting
>> option.
>>
>> e.g.,
>> $ cat ~/.psqlrc
>> \set PROMPT2 '%/[%l]%R%# '
>> \set PROMPT1 '%/[%l]%R%# '
>> $ psql -d postgres
>> postgres[1]=# select
>> postgres[2]-# *
>> postgres[3]-# from
>> postgres[4]-# hoge;
>>
>> The past discussion is following.
>>
>> <http://www.postgresql.org/message-id/CAFj8pRC1ruPk6+chA1jpxPh3uS_zipaBDOvmcEex4wPbp2kZMQ@mail.gmail.com>
>>
>> Please give me feedback.
>>
>> Regards,
>>
>> -------
>> Sawada Masahiko
>>
>>
>> --
>> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-hackers
>>
>
>
>
> --
> Jeevan B Chalke
> Principal Software Engineer, Product Development
> EnterpriseDB Corporation
> The Enterprise PostgreSQL Company
>
> Phone: +91 20 30589500
>
> Website: www.enterprisedb.com
> EnterpriseDB Blog: http://blogs.enterprisedb.com/
> Follow us on Twitter: http://www.twitter.com/enterprisedb
>
> This e-mail message (and any attachment) is intended for the use of the
> individual or entity to whom it is addressed. This message contains
> information from EnterpriseDB Corporation that may be privileged,
> confidential, or exempt from disclosure under applicable law. If you are not
> the intended recipient or authorized to receive this for the intended
> recipient, any use, dissemination, distribution, retention, archiving, or
> copying of this communication is strictly prohibited. If you have received
> this e-mail in error, please notify the sender immediately by reply e-mail
> and delete this message.

Thank you for reviewing patch, and sorry for late response.

I have updated this patch, and attached it.

> postgres[1]=# select
> *
> from
> tab
> postgres[2]-# where t > 10;
> ERROR: column "t" does not exist
> LINE 5: where t > 10;
Attached patch can handle this case.

Please give me feedback.

Regards,

-------
Sawada Masahiko

Attachment Content-Type Size
psql-line-number_v2.patch application/octet-stream 3.2 KB

From: Jeevan Chalke <jeevan(dot)chalke(at)enterprisedb(dot)com>
To: Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: add line number as prompt option to psql
Date: 2014-07-07 11:33:37
Message-ID: CAM2+6=Wwc=yq78hjBBtx8oUO+-dRqE7suR_OvJ7525wDr31_qg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi,

Found two (A and B) issues with latest patch:

A:
-- Set prompts
postgres=# \set PROMPT1 '%/[%l]%R%# '
postgres[1]=# \set PROMPT2 '%/[%l]%R%# '

postgres[1]=#
postgres[1]=# select
postgres[2]-# *
postgres[3]-# from
postgres[4]-# abc;
ERROR: relation "abc" does not exist
LINE 4: abc;
^

Now I used \e to edit the source. Deleted last line i.e. "abc;" and
returned to prompt, landed at line 4, typed "abc;"

postgres[1]=# \e
postgres[4]-# abc;
ERROR: relation "abc" does not exist
LINE 5: abc;
^

In above steps, error message says "LINE 5", where as on prompt "abc" is at
line 4.

postgres[1]=# select
*
from
abc;
ERROR: relation "abc" does not exist
LINE 4: abc;
^

Here I again see error at line 4. Something fishy. Please investigate.
Looks like bug in LINE numbering in error message, not sure though.
But with prompt line number feature, it should be sync to each other.

B:
However, I see that you have removed the code changes related to INT_MAX.
Why?
I have set cur_line to INT_MAX - 2 and then observed that after 2 lines I
start getting negative numbers.

Thanks

On Sun, Jul 6, 2014 at 10:48 PM, Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>
wrote:

> On Fri, Jun 20, 2014 at 7:17 PM, Jeevan Chalke
> <jeevan(dot)chalke(at)enterprisedb(dot)com> wrote:
> > Hi Sawada Masahiko,
> >
> > I liked this feature. So I have reviewed it.
> >
> > Changes are straight forward and looks perfect.
> > No issues found with make/make install/initdb/regression.
> >
> > However I would suggest removing un-necessary braces at if, as we have
> only
> > one statement into it.
> >
> > if (++cur_line >= INT_MAX)
> > {
> > cur_line = 1;
> > }
> >
> > Also following looks wrong:
> >
> > postgres[1]=# select
> > postgres[2]-# *
> > postgres[3]-# from
> > postgres[4]-# tab;
> > a
> > ---
> > (0 rows)
> >
> > postgres[1]=# select
> > *
> > from
> > tab
> > postgres[2]-# where t > 10;
> > ERROR: column "t" does not exist
> > LINE 5: where t > 10;
> > ^
> >
> > Line number in ERROR is 5 which is correct.
> > But line number in psql prompt is wrong.
> >
> > To get first 4 lines I have simply used up arrow followed by an enter for
> > which I was expecting 5 in psql prompt.
> > But NO it was 2 which is certainly wrong.
> >
> > Need to handle above carefully.
> >
> > Thanks
> >
> >
> > On Thu, Jun 12, 2014 at 10:46 PM, Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com
> >
> > wrote:
> >>
> >> Hi all,
> >>
> >> The attached IWP patch is one prompt option for psql, which shows
> >> current line number.
> >> If the user made syntax error with too long SQL then psql outputs
> >> message as following.
> >>
> >> ERROR: syntax error at or near "a"
> >> LINE 250: hoge
> >> ^
> >> psql teaches me where syntax error is occurred, but it is not kind
> >> when SQL is too long.
> >> We can use write SQL with ¥e(editor) command(e.g., emacs) , and we can
> >> know line number.
> >> but it would make terminal log dirty . It will make analyzing of log
> >> difficult after error is occurred.
> >> (I think that we usually use copy & paste)
> >>
> >> After attached this patch, we will be able to use %l option as prompting
> >> option.
> >>
> >> e.g.,
> >> $ cat ~/.psqlrc
> >> \set PROMPT2 '%/[%l]%R%# '
> >> \set PROMPT1 '%/[%l]%R%# '
> >> $ psql -d postgres
> >> postgres[1]=# select
> >> postgres[2]-# *
> >> postgres[3]-# from
> >> postgres[4]-# hoge;
> >>
> >> The past discussion is following.
> >>
> >> <
> http://www.postgresql.org/message-id/CAFj8pRC1ruPk6+chA1jpxPh3uS_zipaBDOvmcEex4wPbp2kZMQ@mail.gmail.com
> >
> >>
> >> Please give me feedback.
> >>
> >> Regards,
> >>
> >> -------
> >> Sawada Masahiko
> >>
> >>
> >> --
> >> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> >> To make changes to your subscription:
> >> http://www.postgresql.org/mailpref/pgsql-hackers
> >>
> >
> >
> >
> > --
> > Jeevan B Chalke
> > Principal Software Engineer, Product Development
> > EnterpriseDB Corporation
> > The Enterprise PostgreSQL Company
> >
> > Phone: +91 20 30589500
> >
> > Website: www.enterprisedb.com
> > EnterpriseDB Blog: http://blogs.enterprisedb.com/
> > Follow us on Twitter: http://www.twitter.com/enterprisedb
> >
> > This e-mail message (and any attachment) is intended for the use of the
> > individual or entity to whom it is addressed. This message contains
> > information from EnterpriseDB Corporation that may be privileged,
> > confidential, or exempt from disclosure under applicable law. If you are
> not
> > the intended recipient or authorized to receive this for the intended
> > recipient, any use, dissemination, distribution, retention, archiving, or
> > copying of this communication is strictly prohibited. If you have
> received
> > this e-mail in error, please notify the sender immediately by reply
> e-mail
> > and delete this message.
>
> Thank you for reviewing patch, and sorry for late response.
>
> I have updated this patch, and attached it.
>
> > postgres[1]=# select
> > *
> > from
> > tab
> > postgres[2]-# where t > 10;
> > ERROR: column "t" does not exist
> > LINE 5: where t > 10;
> Attached patch can handle this case.
>
> Please give me feedback.
>
> Regards,
>
> -------
> Sawada Masahiko
>

--
Jeevan B Chalke
Principal Software Engineer, Product Development
EnterpriseDB Corporation
The Enterprise PostgreSQL Company

Phone: +91 20 30589500

Website: www.enterprisedb.com
EnterpriseDB Blog: http://blogs.enterprisedb.com/
Follow us on Twitter: http://www.twitter.com/enterprisedb

This e-mail message (and any attachment) is intended for the use of the
individual or entity to whom it is addressed. This message contains
information from EnterpriseDB Corporation that may be privileged,
confidential, or exempt from disclosure under applicable law. If you are
not the intended recipient or authorized to receive this for the intended
recipient, any use, dissemination, distribution, retention, archiving, or
copying of this communication is strictly prohibited. If you have received
this e-mail in error, please notify the sender immediately by reply e-mail
and delete this message.


From: Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>
To: Jeevan Chalke <jeevan(dot)chalke(at)enterprisedb(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: add line number as prompt option to psql
Date: 2014-07-09 17:32:35
Message-ID: CAD21AoBt6jb3pXAX1_Cf33FPHwX+a__-OpGndbTcb7QSKRLybQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mon, Jul 7, 2014 at 8:33 PM, Jeevan Chalke
<jeevan(dot)chalke(at)enterprisedb(dot)com> wrote:
> Hi,
>
> Found two (A and B) issues with latest patch:
>
> A:
> -- Set prompts
> postgres=# \set PROMPT1 '%/[%l]%R%# '
> postgres[1]=# \set PROMPT2 '%/[%l]%R%# '
>
> postgres[1]=#
> postgres[1]=# select
> postgres[2]-# *
> postgres[3]-# from
> postgres[4]-# abc;
> ERROR: relation "abc" does not exist
> LINE 4: abc;
> ^
>
> Now I used \e to edit the source. Deleted last line i.e. "abc;" and
> returned to prompt, landed at line 4, typed "abc;"
>
> postgres[1]=# \e
> postgres[4]-# abc;
> ERROR: relation "abc" does not exist
> LINE 5: abc;
> ^
>
> In above steps, error message says "LINE 5", where as on prompt "abc" is at
> line 4.
>
>
> postgres[1]=# select
> *
> from
> abc;
> ERROR: relation "abc" does not exist
> LINE 4: abc;
> ^
>
> Here I again see error at line 4. Something fishy. Please investigate.
> Looks like bug in LINE numbering in error message, not sure though.
> But with prompt line number feature, it should be sync to each other.
>
>
> B:
> However, I see that you have removed the code changes related to INT_MAX.
> Why?
> I have set cur_line to INT_MAX - 2 and then observed that after 2 lines I
> start getting negative numbers.
>

Thank you for reviewing the patch.
I have revised the patch, and attached.

> A:
> But with prompt line number feature, it should be sync to each other.

This patch can handle this case.

> B:
> However, I see that you have removed the code changes related to INT_MAX.
> Why?

I had mistake to remove them. I added them to latest patch.

Regards,

-------
Sawada Masahiko

Attachment Content-Type Size
psql-line-number_v3.patch application/octet-stream 4.2 KB

From: Jeevan Chalke <jeevan(dot)chalke(at)enterprisedb(dot)com>
To: Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: add line number as prompt option to psql
Date: 2014-07-10 11:35:23
Message-ID: CAM2+6=VgWpZFxgoA5uujYwSOKc9sBy7GRAVd5=dOPrpG4MAD1w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi,

Found few more bugs in new code:

A:
This got bad:

jeevan(at)ubuntu:~/pg_master$ ./install/bin/psql postgres
psql (9.5devel)
Type "help" for help.

postgres=# \set PROMPT1 '%/[%l]%R%# '
postgres[1]=# \set PROMPT2 '%/[%l]%R%# '
postgres[1]=# select
postgres[2]-# *
postgres[3]-# from
postgres[4]-# abc;
ERROR: syntax error at or near "fromabc"
LINE 1: select*fromabc;
^
postgres[1]=#
postgres[1]=#
postgres[1]=# \e
ERROR: syntax error at or near "fromabc"
LINE 1: select*fromabc;
^
postgres[1]=# select*fromabc;
ERROR: syntax error at or near "fromabc"
LINE 1: select*fromabc;
^
postgres[1]=#

See query text in LINE 1:. This is because, you have removed addition of
newline character. Related added_nl_pos. Need more investigation here.
However I don't think these changes are relevant to what you wanted in this
feature.
Will you please explain the idea behind these changes ?

Moreover, if you don't want to add newline character, then I guess entire
logic related to added_nl_pos is NO more required. You may remove this
variable and its logic altogether, not sure though. Also make sure you
update the relevant comments while doing so. Here you have removed the code
which is adding the newline but the comment there still reads:
/* insert newlines into query buffer between source lines */

Need more thoughts on this.

B:
postgres=# \set PROMPT1 '%/[%l]%R%# '
postgres[1]=# \set PROMPT2 '%/[%l]%R%# '
postgres[1]=# \e
postgres[-2147483645]-# limit 1;
relname
--------------
pg_statistic
(1 row)

postgres[1]=#
postgres[1]=# select
relname
from
pg_class
limit 1;

Logic related to wrapping around the cur_line counter is wrong. Actually
issue is with newline variable. If number of lines in \e editor goes beyond
INT_MAX (NOT sure about the practical use), then newline will be -ve which
then enforces cur_line to be negative. To mimic this I have initialized
newline = INT_MAX - 1.

Thanks

--
Jeevan B Chalke
Principal Software Engineer, Product Development
EnterpriseDB Corporation
The Enterprise PostgreSQL Company


From: Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>
To: Jeevan Chalke <jeevan(dot)chalke(at)enterprisedb(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: add line number as prompt option to psql
Date: 2014-07-10 13:53:33
Message-ID: CAD21AoCzfte7h2-xpzmv0BQffU3+QomhKjpMm5b_cRxW5eE4mA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Jul 10, 2014 at 8:35 PM, Jeevan Chalke
<jeevan(dot)chalke(at)enterprisedb(dot)com> wrote:
> Hi,
>
> Found few more bugs in new code:
>
> A:
> This got bad:
>
> jeevan(at)ubuntu:~/pg_master$ ./install/bin/psql postgres
> psql (9.5devel)
> Type "help" for help.
>
>
> postgres=# \set PROMPT1 '%/[%l]%R%# '
> postgres[1]=# \set PROMPT2 '%/[%l]%R%# '
> postgres[1]=# select
> postgres[2]-# *
> postgres[3]-# from
> postgres[4]-# abc;
> ERROR: syntax error at or near "fromabc"
> LINE 1: select*fromabc;
>
> ^
> postgres[1]=#
> postgres[1]=#
> postgres[1]=# \e
> ERROR: syntax error at or near "fromabc"
> LINE 1: select*fromabc;
> ^
> postgres[1]=# select*fromabc;
> ERROR: syntax error at or near "fromabc"
> LINE 1: select*fromabc;
> ^
> postgres[1]=#
>
>
> See query text in LINE 1:. This is because, you have removed addition of
> newline character. Related added_nl_pos. Need more investigation here.
> However I don't think these changes are relevant to what you wanted in this
> feature.
> Will you please explain the idea behind these changes ?
>
> Moreover, if you don't want to add newline character, then I guess entire
> logic related to added_nl_pos is NO more required. You may remove this
> variable and its logic altogether, not sure though. Also make sure you
> update the relevant comments while doing so. Here you have removed the code
> which is adding the newline but the comment there still reads:
> /* insert newlines into query buffer between source lines */
>
> Need more thoughts on this.
>
>
> B:
>
> postgres=# \set PROMPT1 '%/[%l]%R%# '
> postgres[1]=# \set PROMPT2 '%/[%l]%R%# '
> postgres[1]=# \e
> postgres[-2147483645]-# limit 1;
> relname
> --------------
> pg_statistic
> (1 row)
>
>
> postgres[1]=#
> postgres[1]=# select
> relname
> from
> pg_class
> limit 1;
>
> Logic related to wrapping around the cur_line counter is wrong. Actually
> issue is with newline variable. If number of lines in \e editor goes beyond
> INT_MAX (NOT sure about the practical use), then newline will be -ve which
> then enforces cur_line to be negative. To mimic this I have initialized
> newline = INT_MAX - 1.
>

Thank you for reviewing the patch with variable cases.
I have revised the patch, and attached latest patch.

> A:
> Will you please explain the idea behind these changes ?
I thought wrong about adding new to tail of query_buf.
The latest patch does not change related to them.

> B:
I added the condition of cur_line < 0.

Regards,

-------
Sawada Masahiko

Attachment Content-Type Size
psql-line-number_v4.patch application/octet-stream 3.5 KB

From: Jeevan Chalke <jeevan(dot)chalke(at)enterprisedb(dot)com>
To: Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: add line number as prompt option to psql
Date: 2014-07-11 07:23:46
Message-ID: CAM2+6=XWdJb_Nfk8zGCaLgc2NCQgWWWmL0Zh+-DodQT58+21VQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi,

Found new issues with latest patch:

> Thank you for reviewing the patch with variable cases.
> I have revised the patch, and attached latest patch.
>
> > A:
> > Will you please explain the idea behind these changes ?
> I thought wrong about adding new to tail of query_buf.
> The latest patch does not change related to them.
>
> Thanks.

> > B:
> I added the condition of cur_line < 0.
>
>
A.

However, this introduced new bug. As I told, when editor number of lines
reaches INT_MAX it starts giving negative number. You tried overcoming this
issue by adding "< 0" check. But I guess you again fumbled in setting that
correctly. You are setting it to INT_MAX - 1. This enforces each new line
to show line number as INT_MAX - 1 which is incorrect.

postgres=# \set PROMPT1 '%/[%l]%R%# '
postgres[1]=# \set PROMPT2 '%/[%l]%R%# '
postgres[1]=# \e
postgres[2147483646]-# limit
postgres[2147483646]-# 1;
relname
--------------
pg_statistic
(1 row)

postgres[1]=# \e
postgres[2147483646]-# =
postgres[2147483646]-# '
postgres[2147483646]'# abc
postgres[2147483646]'# '
postgres[2147483646]-# ;
relname
---------
(0 rows)

postgres[1]=# select
relname
from
pg_class
where
relname
=
'
abc
'
;

Again to mimic that, I have simply intialized newline to INT_MAX - 2.
Please don't take me wrong, but it seems that your unit testing is not
enough. Above issue I discovered by doing exactly same steps I did in
reviewing previous patch. If you had tested your new patch with those steps
I guess you have caught that yourself.

B.

+ /* Calculate the line number */
+ if (scan_result != PSCAN_INCOMPLETE)
+ {
+ /* The one new line is always added to tail of query_buf
*/
+ newline = (newline != 0) ? (newline + 1) : 1;
+ cur_line += newline;
+ }

Here in above code changes, in any case you are adding 1 to newline. i.e.
when it is 0 you are setting it 1 (+1) and when > 0 you are setting nl + 1
(again +1).
So why can't you simply use"
if (scan_result != PSCAN_INCOMPLETE)
cur_line += (newline + 1);

Or better, why can't you initialize newline with 1 itself and then directly
assign cur_line with newline. That will eliminate above entire code block,
isn't it?
Or much better, simply get rid of newline, and use cur_line itself, will
this work well for you?

C. Typos:
1.
/* Count the number of new line for calculate ofline number */

Missing space between 'of' and 'line'.
However better improve that to something like (just suggestion):
"Count the number of new lines to correctly adjust current line number"

2.
/* Avoid cur_line and newline exceeds the INT_MAX */

You are saying avoid cur_line AND newline, but there is no adjustment for
newline in the code following the comment.

Thanks
--
Jeevan B Chalke
Principal Software Engineer, Product Development
EnterpriseDB Corporation
The Enterprise PostgreSQL Company


From: Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>
To: Jeevan Chalke <jeevan(dot)chalke(at)enterprisedb(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: add line number as prompt option to psql
Date: 2014-07-11 09:43:43
Message-ID: CAD21AoDtaBEA4rM5N8-ELrA_RxeGjoqXH=oaqGKHKVqi06r+WA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Jul 11, 2014 at 4:23 PM, Jeevan Chalke
<jeevan(dot)chalke(at)enterprisedb(dot)com> wrote:
> Hi,
>
> A.
>
> However, this introduced new bug. As I told, when editor number of lines
> reaches INT_MAX it starts giving negative number. You tried overcoming this
> issue by adding "< 0" check. But I guess you again fumbled in setting that
> correctly. You are setting it to INT_MAX - 1. This enforces each new line to
> show line number as INT_MAX - 1 which is incorrect.
>
> postgres=# \set PROMPT1 '%/[%l]%R%# '
> postgres[1]=# \set PROMPT2 '%/[%l]%R%# '
> postgres[1]=# \e
> postgres[2147483646]-# limit
> postgres[2147483646]-# 1;
>
> relname
> --------------
> pg_statistic
> (1 row)
>
> postgres[1]=# \e
> postgres[2147483646]-# =
> postgres[2147483646]-# '
> postgres[2147483646]'# abc
> postgres[2147483646]'# '
> postgres[2147483646]-# ;
> relname
> ---------
> (0 rows)
>
>
> postgres[1]=# select
> relname
> from
> pg_class
> where
> relname
> =
> '
> abc
> '
> ;
>
>
> Again to mimic that, I have simply intialized newline to INT_MAX - 2.
> Please don't take me wrong, but it seems that your unit testing is not
> enough. Above issue I discovered by doing exactly same steps I did in
> reviewing previous patch. If you had tested your new patch with those steps
> I guess you have caught that yourself.
>

To my understating cleanly, you means that line number is not changed
when newline has reached to INT_MAX, is incorrect?
And the line number should be switched to 1 when line number has
reached to INT_MAX?

>
> B.
>
> + /* Calculate the line number */
> + if (scan_result != PSCAN_INCOMPLETE)
> + {
> + /* The one new line is always added to tail of query_buf
> */
> + newline = (newline != 0) ? (newline + 1) : 1;
> + cur_line += newline;
> + }
>
> Here in above code changes, in any case you are adding 1 to newline. i.e.
> when it is 0 you are setting it 1 (+1) and when > 0 you are setting nl + 1
> (again +1).
> So why can't you simply use"
> if (scan_result != PSCAN_INCOMPLETE)
> cur_line += (newline + 1);
>
> Or better, why can't you initialize newline with 1 itself and then directly
> assign cur_line with newline. That will eliminate above entire code block,
> isn't it?
> Or much better, simply get rid of newline, and use cur_line itself, will
> this work well for you?

this is better. I will change code to this.

>
> C. Typos:
> 1.
> /* Count the number of new line for calculate ofline number */
>
> Missing space between 'of' and 'line'.
> However better improve that to something like (just suggestion):
> "Count the number of new lines to correctly adjust current line number"
>
> 2.
> /* Avoid cur_line and newline exceeds the INT_MAX */
>
> You are saying avoid cur_line AND newline, but there is no adjustment for
> newline in the code following the comment.
>
> Thanks
> --
> Jeevan B Chalke
> Principal Software Engineer, Product Development
> EnterpriseDB Corporation
> The Enterprise PostgreSQL Company
>

Thanks.
I will fix it.

--
Regards,

-------
Sawada Masahiko


From: Jeevan Chalke <jeevan(dot)chalke(at)enterprisedb(dot)com>
To: Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: add line number as prompt option to psql
Date: 2014-07-11 12:40:14
Message-ID: CAM2+6=UXN9wRFSFnZpov7LZzqzssZDoG=U=wLXx1Qu0hH+uEuQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi,

On Fri, Jul 11, 2014 at 3:13 PM, Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>
wrote:

> >
> >
>
> To my understating cleanly, you means that line number is not changed
> when newline has reached to INT_MAX, is incorrect?
>

As per my thinking yes.

> And the line number should be switched to 1 when line number has
> reached to INT_MAX?
>

Yes, when it goes beyond INT_MAX, wrap around to 1.

BTW, I wonder, can't we simply use unsigned int instead?

Also, what is the behaviour on LINE n, in error message in case of such
wrap-around?

>
> >
> > Or much better, simply get rid of newline, and use cur_line itself, will
> > this work well for you?
>
> this is better. I will change code to this.
> Thanks.
> I will fix it.
>

Meanwhile I have tried this. Attached patch to have your suggestion on
that.

Thanks

--
Jeevan B Chalke
Principal Software Engineer, Product Development
EnterpriseDB Corporation
The Enterprise PostgreSQL Company

Attachment Content-Type Size
psql-line-number_v5_jeevan.patch text/x-diff 3.7 KB

From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: Jeevan Chalke <jeevan(dot)chalke(at)enterprisedb(dot)com>
Cc: Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: add line number as prompt option to psql
Date: 2014-07-11 14:01:35
Message-ID: 20140711140135.GA6390@eldon.alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Jeevan Chalke wrote:

> On Fri, Jul 11, 2014 at 3:13 PM, Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>
> wrote:

> > And the line number should be switched to 1 when line number has
> > reached to INT_MAX?
>
> Yes, when it goes beyond INT_MAX, wrap around to 1.
>
> BTW, I wonder, can't we simply use unsigned int instead?

That was my thought also: let the variable be unsigned, and have it wrap
around normally. So once you reach UINT_MAX, the next line number is
zero (instead of getting stuck at UINT_MAX, which would be rather
strange). Anyway I don't think anyone is going to reach the UINT_MAX
limit ... I mean that would be one hell of a query, wouldn't it. If
your query is upwards of a million lines, surely you are in deep trouble
already.

Does your text editor handle files longer than 4 billion lines?

--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services


From: Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>
To: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
Cc: Jeevan Chalke <jeevan(dot)chalke(at)enterprisedb(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: add line number as prompt option to psql
Date: 2014-07-11 17:01:35
Message-ID: CAD21AoB24Jdm8400yNUKWzXomNZCOD1gXXMr2R4cP_EE3YTyJg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Jul 11, 2014 at 11:01 PM, Alvaro Herrera
<alvherre(at)2ndquadrant(dot)com> wrote:
> Jeevan Chalke wrote:
>
>> On Fri, Jul 11, 2014 at 3:13 PM, Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>
>> wrote:
>
>> > And the line number should be switched to 1 when line number has
>> > reached to INT_MAX?
>>
>> Yes, when it goes beyond INT_MAX, wrap around to 1.
>>
>> BTW, I wonder, can't we simply use unsigned int instead?
>
> That was my thought also: let the variable be unsigned, and have it wrap
> around normally. So once you reach UINT_MAX, the next line number is
> zero (instead of getting stuck at UINT_MAX, which would be rather
> strange). Anyway I don't think anyone is going to reach the UINT_MAX
> limit ... I mean that would be one hell of a query, wouldn't it. If
> your query is upwards of a million lines, surely you are in deep trouble
> already.
>
> Does your text editor handle files longer than 4 billion lines?
>

As you said, if line number reached UINT_MAX then I think that this
case is too strange.
I think INT_MAX is enough for line number.

The v5 patch which Jeevan is created seems to good.
But one point, I got hunk when patch is applied to HEAD. (doc file)
So I have revised it and attached.

Regards,

-------
Sawada Masahiko

Attachment Content-Type Size
psql-line-number_v5.patch application/octet-stream 3.7 KB

From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>
Cc: Jeevan Chalke <jeevan(dot)chalke(at)enterprisedb(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: add line number as prompt option to psql
Date: 2014-07-11 17:19:35
Message-ID: 20140711171935.GB6390@eldon.alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Sawada Masahiko wrote:

> As you said, if line number reached UINT_MAX then I think that this
> case is too strange.
> I think INT_MAX is enough for line number.

My point is not whether 2 billion is a better number than 4 billion as a
maximum value. My point is that wraparound of signed int is, I think,
not even defined in C, whereas wraparound of unsigned int is well
defined. cur_line should be declared as unsigned int. I don't trust
that INT_MAX+2 arithmetic.

Please don't use cur_line as a name for a global variable. Something
like PSQLLineNumber seems more appropriate if it's going to be exposed
through prompt.h. However, note that MainLoop() keeps state in local
variables and notes that it is reentrant; what happens to your cur_line
when a file is read by \i and similar? I wonder if it should be part of
PsqlScanStateData instead ...

--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services


From: Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>
To: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
Cc: Jeevan Chalke <jeevan(dot)chalke(at)enterprisedb(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: add line number as prompt option to psql
Date: 2014-08-14 12:07:11
Message-ID: CAD21AoAJqB=e_fBHzz3pg0qROAacPTgxUw=8ZdHQA-DjVdutNQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Sat, Jul 12, 2014 at 2:19 AM, Alvaro Herrera
<alvherre(at)2ndquadrant(dot)com> wrote:
> Sawada Masahiko wrote:
>
>> As you said, if line number reached UINT_MAX then I think that this
>> case is too strange.
>> I think INT_MAX is enough for line number.
>
> My point is not whether 2 billion is a better number than 4 billion as a
> maximum value. My point is that wraparound of signed int is, I think,
> not even defined in C, whereas wraparound of unsigned int is well
> defined. cur_line should be declared as unsigned int. I don't trust
> that INT_MAX+2 arithmetic.
>
> Please don't use cur_line as a name for a global variable. Something
> like PSQLLineNumber seems more appropriate if it's going to be exposed
> through prompt.h. However, note that MainLoop() keeps state in local
> variables and notes that it is reentrant; what happens to your cur_line
> when a file is read by \i and similar? I wonder if it should be part of
> PsqlScanStateData instead ...
>

Thank you for comment.
I restarted to make this patch again.

Attached patch is new version patch, and rebased.
pset structure has cur_lineno variable which shows current line number
as unsigned int64.

Regards,

-------
Sawada Masahiko

Attachment Content-Type Size
psql-line-number_v6.patch application/octet-stream 3.5 KB

From: Jeevan Chalke <jeevan(dot)chalke(at)enterprisedb(dot)com>
To: Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>
Cc: 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-20 12:00:49
Message-ID: CAM2+6=UWXoy4-Laowy8mvt3sk4fjGxNkM0Yp4Z-RgtkdgnP_Kw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

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.

Thanks

--
Jeevan B Chalke
Principal Software Engineer, Product Development
EnterpriseDB Corporation
The Enterprise PostgreSQL Company


From: Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>
To: Jeevan Chalke <jeevan(dot)chalke(at)enterprisedb(dot)com>
Cc: 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-21 02:43:48
Message-ID: CAD21AoD_VhOQZFeiYhgMLNEN0akqkoN9JRVUTgdCkfmT8X14bg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

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.

Regards,

-------
Sawada Masahiko

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

From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: Jeevan Chalke <jeevan(dot)chalke(at)enterprisedb(dot)com>
Cc: Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: add line number as prompt option to psql
Date: 2014-08-21 14:14:51
Message-ID: 20140821141450.GC6343@eldon.alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Jeevan Chalke wrote:

> 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.

Yeah, most likely you will run out of memory before reaching that point,
or out of patience.

--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services


From: Jeevan Chalke <jeevan(dot)chalke(at)enterprisedb(dot)com>
To: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
Cc: Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: add line number as prompt option to psql
Date: 2014-08-22 11:55:15
Message-ID: CAM2+6=UccNMZA0=97fz1EVPTfsWd2vHZqxtakb17KncvjkC5cw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

> 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.
>
> Yeah, most likely you will run out of memory before reaching that point,
> or out of patience.
>
> Yep.

BTW, I have marked this as "waiting for committer".

Thanks
--
Jeevan B Chalke
Principal Software Engineer, Product Development
EnterpriseDB Corporation
The Enterprise PostgreSQL Company


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
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

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
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

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-09-02 02:12:52
Message-ID: 20140902021252.GC2335@awork2.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 2014-08-31 12:06:31 +0900, Sawada Masahiko wrote:
> 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.

Hm. Right. That's clearly wrong.

> Attached patch is resolve above behavior based on your version patch.

I've looked a bit further and found two more broken things.

1)
postgres[1]=# SELECT 1; SELECT 2
postgres[1]=#

Note the 1 in the second line. Obviously wrong.

The fix for this is easy: Don't count a newline if there isn't one. But
check for PSCAN_EOL. That also gets rid of inconsistent pset.stmt_lineno
initializations (sometimes to 0, sometimes to 1).

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

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

postgres[3]=#

Obviously the three in the last line is wrong.

The fix is slightly nontrivial. It's wrong to look at 'line' when
determining the number of lines to add - it may already be
executed. The, it seems to me, correct thing is to look at the data
that's appended to the query buffer. Alternatively we could always count
all lines in the query buffer, but that'd be O(lines^2)...

I've done both in the appended patch.

I've now used up a perfectly good glass of wine for this, so this is it
for today ;)

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.2 KB

From: Michael Paquier <michael(dot)paquier(at)gmail(dot)com>
To: Andres Freund <andres(at)2ndquadrant(dot)com>
Cc: Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>, 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-09-02 02:19:09
Message-ID: CAB7nPqQC5A_-6Xo4wvfyLfR96ACAYxbB-oWOke90nBx1ryEqVQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Sep 2, 2014 at 11:12 AM, Andres Freund <andres(at)2ndquadrant(dot)com> wrote:
> I've now used up a perfectly good glass of wine for this.
Red or white? From where? Useful tips for hacking in this area are
always useful.
--
Michael


From: Andres Freund <andres(at)2ndquadrant(dot)com>
To: Michael Paquier <michael(dot)paquier(at)gmail(dot)com>
Cc: Sawada Masahiko <sawada(dot)mshk(at)gmail(dot)com>, 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-09-02 02:27:19
Message-ID: 20140902022719.GE2335@awork2.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 2014-09-02 11:19:09 +0900, Michael Paquier wrote:
> On Tue, Sep 2, 2014 at 11:12 AM, Andres Freund <andres(at)2ndquadrant(dot)com> wrote:
> > I've now used up a perfectly good glass of wine for this.
> Red or white? From where? Useful tips for hacking in this area are
> always useful.

Hah ;). Nothing special, but I rather like it: Bordeaux Superieur Cuvee,
Chateau Couronneau 2011.

Greetings,

Andres Freund

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


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-09-02 03:34:12
Message-ID: CAD21AoBf-u3R-LY7VXBrLrcs-23N1KaaSBaHYXQ9dRgKo-TXuw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Sep 2, 2014 at 11:12 AM, Andres Freund <andres(at)2ndquadrant(dot)com> wrote:
> On 2014-08-31 12:06:31 +0900, Sawada Masahiko wrote:
>> 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.
>
> Hm. Right. That's clearly wrong.
>
>> Attached patch is resolve above behavior based on your version patch.
>
> I've looked a bit further and found two more broken things.
>
> 1)
> postgres[1]=# SELECT 1; SELECT 2
> postgres[1]=#
>
> Note the 1 in the second line. Obviously wrong.
>
> The fix for this is easy: Don't count a newline if there isn't one. But
> check for PSCAN_EOL. That also gets rid of inconsistent pset.stmt_lineno
> initializations (sometimes to 0, sometimes to 1).
>
> 2)
> postgres[1]=# SELECT 1,
> postgres[2]-# 2,
> postgres[3]-# 3;
> ┌──────────┬──────────┬──────────┐
> │ ?column? │ ?column? │ ?column? │
> ├──────────┼──────────┼──────────┤
> │ 1 │ 2 │ 3 │
> └──────────┴──────────┴──────────┘
> (1 row)
>
> postgres[1]=# SELECT 1,
> 2,
> 3;
> ┌──────────┬──────────┬──────────┐
> │ ?column? │ ?column? │ ?column? │
> ├──────────┼──────────┼──────────┤
> │ 1 │ 2 │ 3 │
> └──────────┴──────────┴──────────┘
> (1 row)
>
> postgres[3]=#
>
> Obviously the three in the last line is wrong.
>
> The fix is slightly nontrivial. It's wrong to look at 'line' when
> determining the number of lines to add - it may already be
> executed. The, it seems to me, correct thing is to look at the data
> that's appended to the query buffer. Alternatively we could always count
> all lines in the query buffer, but that'd be O(lines^2)...
>
>
> I've done both in the appended patch.
>
>
> I've now used up a perfectly good glass of wine for this, so this is it
> for today ;)
>

Thank you for updating the patch!
I tested it.
These fix looks good to me :)

Regards,

-------
Sawada Masahiko


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-09-02 11:20:46
Message-ID: 20140902112046.GB27095@awork2.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 2014-09-02 12:34:12 +0900, Sawada Masahiko wrote:
> On Tue, Sep 2, 2014 at 11:12 AM, Andres Freund <andres(at)2ndquadrant(dot)com> wrote:
> > I've now used up a perfectly good glass of wine for this, so this is it
> > for today ;)
> >
>
> Thank you for updating the patch!
> I tested it.
> These fix looks good to me :)

Committed. Thanks for the patch!

Greetings,

Andres Freund

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