A tiny improvement of psql

Started by Kevin Wangover 2 years ago6 messageshackers
Jump to latest
#1Kevin Wang
kevinpgcloud@gmail.com

Hello hackers!

I am an Oracle/PostgreSQL DBA, I am not a PG hacker. During my daily job,
I find a pain that should be fixed.

As you know, we can use the UP arrow key to get the previous command to
avoid extra typing. This is a wonderful feature to save the lives of every
DBA. However, if I type the commands like this sequence: A, B, B, B, B, B,
B, as you can see, B is the last command I execute.

But if I try to get command A, I have to press the UP key 7 times. I think
the best way is: when you press the UP key, plsql should show the command
that is different from the previous command, so the recall sequence should
be B -> A, not B -> B -> ... -> A. Then I only press the UP key 2 times to
get command A.

I think this should change little code in psql, but it will make all DBA's
lives much easier. This is a strong requirement from the real DBA. Hope to
get some feedback on this.

Another requirement is: could we use / to repeat executing the last command
in plsql just like sqlplus in Oracle?

I will try to learn how to fix it sooner or later, but if some
proficient hacker focuses on this, it can be fixed quickly, I guess.

Thoughts?

Regards,

Kevin

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Kevin Wang (#1)
Re: A tiny improvement of psql

Kevin Wang <kevinpgcloud@gmail.com> writes:

As you know, we can use the UP arrow key to get the previous command to
avoid extra typing. This is a wonderful feature to save the lives of every
DBA. However, if I type the commands like this sequence: A, B, B, B, B, B,
B, as you can see, B is the last command I execute.

But if I try to get command A, I have to press the UP key 7 times. I think
the best way is: when you press the UP key, plsql should show the command
that is different from the previous command, so the recall sequence should
be B -> A, not B -> B -> ... -> A. Then I only press the UP key 2 times to
get command A.

This is driven by libreadline, not anything we control. I have
seen the behavior you describe in some other programs, so I wonder
whether it's configurable.

Another requirement is: could we use / to repeat executing the last command
in plsql just like sqlplus in Oracle?

I'm pretty certain you can configure this for yourself with readline.

regards, tom lane

#3Vik Fearing
vik@postgresfriends.org
In reply to: Tom Lane (#2)
Re: A tiny improvement of psql

On 12/26/23 17:36, Tom Lane wrote:

Kevin Wang <kevinpgcloud@gmail.com> writes:

As you know, we can use the UP arrow key to get the previous command to
avoid extra typing. This is a wonderful feature to save the lives of every
DBA. However, if I type the commands like this sequence: A, B, B, B, B, B,
B, as you can see, B is the last command I execute.

But if I try to get command A, I have to press the UP key 7 times. I think
the best way is: when you press the UP key, plsql should show the command
that is different from the previous command, so the recall sequence should
be B -> A, not B -> B -> ... -> A. Then I only press the UP key 2 times to
get command A.

This is driven by libreadline, not anything we control. I have
seen the behavior you describe in some other programs, so I wonder
whether it's configurable.

It is kind of something we control. Per the psql docs, setting

HISTCONTROL=ignoredups

will do the trick.

https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-VARIABLES-HISTCONTROL
--
Vik Fearing

#4Jelte Fennema-Nio
postgres@jeltef.nl
In reply to: Vik Fearing (#3)
Re: A tiny improvement of psql

On Tue, 26 Dec 2023 at 22:45, Vik Fearing <vik@postgresfriends.org> wrote:

It is kind of something we control. Per the psql docs, setting

HISTCONTROL=ignoredups

will do the trick.

Yeah, the easiest "fix" (that I know of) for a user is to set
HISTCONTROL in ~/.psqlrc to ignoredups using:

\set HISTCONTROL ignoredups

But honestly, I think that should probably be made the default. I
can't really think of a reason who would actually want the current
default of "none". And while we're at it maybe there are some other
defaults in psql that are worth changing. The main ones from my psqlrc
that seem like good defaults for pretty much everyone:

\x auto
\pset linestyle unicode

And maybe fixing the major pitfall I always run into with psql: Having
ON_ERROR_STOP default to on when a script is passed in using -f/--file

#5Deepak M
mahtodeepak05@gmail.com
In reply to: Kevin Wang (#1)
Re: A tiny improvement of psql

On repeating the execution of last command in psql, we can always use below
command to send current query buffer to server.

\g
\gx (with expanded output mode, that always come handy.)

On Tue, Dec 26, 2023 at 9:56 PM Kevin Wang <kevinpgcloud@gmail.com> wrote:

Show quoted text

Hello hackers!

I am an Oracle/PostgreSQL DBA, I am not a PG hacker. During my daily job,
I find a pain that should be fixed.

As you know, we can use the UP arrow key to get the previous command to
avoid extra typing. This is a wonderful feature to save the lives of every
DBA. However, if I type the commands like this sequence: A, B, B, B, B, B,
B, as you can see, B is the last command I execute.

But if I try to get command A, I have to press the UP key 7 times. I
think the best way is: when you press the UP key, plsql should show the
command that is different from the previous command, so the recall sequence
should be B -> A, not B -> B -> ... -> A. Then I only press the UP key 2
times to get command A.

I think this should change little code in psql, but it will make all DBA's
lives much easier. This is a strong requirement from the real DBA. Hope to
get some feedback on this.

Another requirement is: could we use / to repeat executing the last
command in plsql just like sqlplus in Oracle?

I will try to learn how to fix it sooner or later, but if some
proficient hacker focuses on this, it can be fixed quickly, I guess.

Thoughts?

Regards,

Kevin

#6Kirk Wolak
wolakk@gmail.com
In reply to: Kevin Wang (#1)
Re: A tiny improvement of psql

On Tue, Dec 26, 2023 at 11:26 AM Kevin Wang <kevinpgcloud@gmail.com> wrote:

Hello hackers!

I am an Oracle/PostgreSQL DBA, I am not a PG hacker. During my daily job,
I find a pain that should be fixed.

As you know, we can use the UP arrow key to get the previous command to
avoid extra typing. This is a wonderful feature to save the lives of every
DBA. However, if I type the commands like this sequence: A, B, B, B, B, B,
B, as you can see, B is the last command I execute.

But if I try to get command A, I have to press the UP key 7 times. I
think the best way is: when you press the UP key, plsql should show the
command that is different from the previous command, so the recall sequence
should be B -> A, not B -> B -> ... -> A. Then I only press the UP key 2
times to get command A.

I think this should change little code in psql, but it will make all DBA's
lives much easier. This is a strong requirement from the real DBA. Hope to
get some feedback on this.

Kevin,

with readline, I use ctrl-r (incremental search backwards).
but if you are willing to modify your .inputrc you can enable the "windows
cmd F5/F8 keys... Search Fwd/Bwd".
where you type B<F8>

inputrc:
# Map F8 (back) F5(forward) search like CMD
"\e[19~": history-search-backward
"\e[15~": history-search-forward

There are commented out lines tying them to Page Up/Page Down... But 30
yrs in a CMD prompt...

The upside is that this works in bash and other programs as well...

HTH

Kirk Out!

Show quoted text