[PATCH] psql \n shortcut for set search_path =

Started by Colin 't Hartover 13 years ago9 messages
#1Colin 't Hart
colin@sharpheart.org
1 attachment(s)

Hi,

Attached please find a trivial patch for psql which adds a \n meta command
as a shortcut for typing set search_path =.

This allows you to use psql as follows:

\dn

\n my_schema

\d

\d my_table

etc.

Not yet done: updating documentation (psql internal help, psql man page,
main documentation).

If this is something that is desired (I hope so as this is something I now
use a lot), I will update the documentation and resubmit.

Cheers,

Colin

Attachments:

psql_slash_n.patchapplication/octet-stream; name=psql_slash_n.patchDownload
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index c5ec981..e4a5310 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -868,6 +868,41 @@ exec_command(const char *cmd,
 		free(opt2);
 	}
 
+	/* \n -- shortcut for set search_path */
+	else if (strcmp(cmd, "n") == 0)
+	{
+		char	*search_path;
+		char	*opt;
+
+		PQExpBufferData	buf;
+		PGresult	*res;
+
+		opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
+		search_path = pg_strdup(opt ? opt : "");
+		free(opt);
+
+		while ((opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false)))
+		{
+			search_path = realloc(search_path, strlen(search_path) + strlen(opt) + 1);
+			if (!search_path)
+			{
+				psql_error("out of memory\n");
+				exit(EXIT_FAILURE);
+			}
+			strcat(search_path, opt);
+			free(opt);
+		}
+
+		initPQExpBuffer(&buf);
+		printfPQExpBuffer(&buf, "SET search_path = %s", search_path);
+		res = PSQLexec(buf.data, false);
+		termPQExpBuffer(&buf);
+		if (!res)
+			success = false;
+		else
+			PQclear(res);
+		free(search_path);
+	}
 
 	/* \o -- set query output */
 	else if (strcmp(cmd, "o") == 0 || strcmp(cmd, "out") == 0)
#2Josh Kupershmidt
schmiddy@gmail.com
In reply to: Colin 't Hart (#1)
Re: [PATCH] psql \n shortcut for set search_path =

On Tue, Jul 10, 2012 at 2:09 AM, Colin 't Hart <colin@sharpheart.org> wrote:

Attached please find a trivial patch for psql which adds a \n meta command
as a shortcut for typing set search_path =.

I think the use-case is a bit narrow: saving a few characters typing
on a command not everyone uses very often (I don't), at the expense of
adding yet another command to remember. Plus it opens the floodgates
to people wanting yet more separate commands for other possibly
commonly-used SET commands. There are a lot of GUCs, after all, even
counting only those changeable at runtime.

Josh

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Josh Kupershmidt (#2)
Re: [PATCH] psql \n shortcut for set search_path =

Josh Kupershmidt <schmiddy@gmail.com> writes:

On Tue, Jul 10, 2012 at 2:09 AM, Colin 't Hart <colin@sharpheart.org> wrote:

Attached please find a trivial patch for psql which adds a \n meta command
as a shortcut for typing set search_path =.

I think the use-case is a bit narrow: saving a few characters typing
on a command not everyone uses very often (I don't), at the expense of
adding yet another command to remember.

Another point here is that we are running low on single-letter backslash
command names in psql. I'm not sure that "SET SEARCH_PATH" is so useful
as to justify using up one of the ones that are left.

ISTM there was some discussion awhile back about user-definable typing
shortcuts in psql. I don't recall any details, but being able to set
up "SET SEARCH_PATH" as a user-definable shortcut if it's useful to you
would eliminate the question about whether it's useful to everyone.

regards, tom lane

#4David Fetter
david@fetter.org
In reply to: Tom Lane (#3)
Re: [PATCH] psql \n shortcut for set search_path =

On Tue, Jul 10, 2012 at 12:00:06PM -0400, Tom Lane wrote:

Josh Kupershmidt <schmiddy@gmail.com> writes:

On Tue, Jul 10, 2012 at 2:09 AM, Colin 't Hart <colin@sharpheart.org> wrote:

Attached please find a trivial patch for psql which adds a \n
meta command as a shortcut for typing set search_path =.

I think the use-case is a bit narrow: saving a few characters
typing on a command not everyone uses very often (I don't), at the
expense of adding yet another command to remember.

Another point here is that we are running low on single-letter
backslash command names in psql. I'm not sure that "SET
SEARCH_PATH" is so useful as to justify using up one of the ones
that are left.

ISTM there was some discussion awhile back about user-definable
typing shortcuts in psql.

In some sense, we already have them:

\set FOO 'SELECT * FROM pg_stat_activity;'
...
:FOO

Was there more to it?

Cheers,
David.
--
David Fetter <david@fetter.org> http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david.fetter@gmail.com
iCal: webcal://www.tripit.com/feed/ical/people/david74/tripit.ics

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate

#5Colin 't Hart
colin@sharpheart.org
In reply to: Tom Lane (#3)
Re: [PATCH] psql \n shortcut for set search_path =

On 10 July 2012 18:00, Tom Lane <tgl@sss.pgh.pa.us> wrote:

Josh Kupershmidt <schmiddy@gmail.com> writes:

On Tue, Jul 10, 2012 at 2:09 AM, Colin 't Hart <colin@sharpheart.org>

wrote:

Attached please find a trivial patch for psql which adds a \n meta

command

as a shortcut for typing set search_path =.

I think the use-case is a bit narrow: saving a few characters typing
on a command not everyone uses very often (I don't), at the expense of
adding yet another command to remember.

Another point here is that we are running low on single-letter backslash
command names in psql. I'm not sure that "SET SEARCH_PATH" is so useful
as to justify using up one of the ones that are left.

ISTM there was some discussion awhile back about user-definable typing
shortcuts in psql. I don't recall any details, but being able to set
up "SET SEARCH_PATH" as a user-definable shortcut if it's useful to you
would eliminate the question about whether it's useful to everyone.

And these could be setup to be available on psql startup by adding them to
.psqlrc

While I like my \n idea (heck, I thought of it :-) ), this would be a very
good generic solution.

I did a quick search but couldn't find the relevant discussion: do you
remember roughly when it was?
If I find it I could have a go at trying to implement it, but it might
exceed my ability in C...

Cheers,

Colin

#6Colin 't Hart
colin@sharpheart.org
In reply to: David Fetter (#4)
Re: [PATCH] psql \n shortcut for set search_path =

On 10 July 2012 18:24, David Fetter <david@fetter.org> wrote:

On Tue, Jul 10, 2012 at 12:00:06PM -0400, Tom Lane wrote:

Josh Kupershmidt <schmiddy@gmail.com> writes:

On Tue, Jul 10, 2012 at 2:09 AM, Colin 't Hart <colin@sharpheart.org>

wrote:

Attached please find a trivial patch for psql which adds a \n
meta command as a shortcut for typing set search_path =.

I think the use-case is a bit narrow: saving a few characters
typing on a command not everyone uses very often (I don't), at the
expense of adding yet another command to remember.

Another point here is that we are running low on single-letter
backslash command names in psql. I'm not sure that "SET
SEARCH_PATH" is so useful as to justify using up one of the ones
that are left.

ISTM there was some discussion awhile back about user-definable
typing shortcuts in psql.

In some sense, we already have them:

\set FOO 'SELECT * FROM pg_stat_activity;'
...
:FOO

Was there more to it?

Can I pass a parameter to ":FOO" ?

Cheers,

Colin

#7David Fetter
david@fetter.org
In reply to: Colin 't Hart (#6)
Re: [PATCH] psql \n shortcut for set search_path =

On Tue, Jul 10, 2012 at 06:26:22PM +0200, Colin 't Hart wrote:

On 10 July 2012 18:24, David Fetter <david@fetter.org> wrote:

On Tue, Jul 10, 2012 at 12:00:06PM -0400, Tom Lane wrote:

Josh Kupershmidt <schmiddy@gmail.com> writes:

On Tue, Jul 10, 2012 at 2:09 AM, Colin 't Hart <colin@sharpheart.org>

wrote:

Attached please find a trivial patch for psql which adds a \n
meta command as a shortcut for typing set search_path =.

I think the use-case is a bit narrow: saving a few characters
typing on a command not everyone uses very often (I don't), at the
expense of adding yet another command to remember.

Another point here is that we are running low on single-letter
backslash command names in psql. I'm not sure that "SET
SEARCH_PATH" is so useful as to justify using up one of the ones
that are left.

ISTM there was some discussion awhile back about user-definable
typing shortcuts in psql.

In some sense, we already have them:

\set FOO 'SELECT * FROM pg_stat_activity;'
...
:FOO

Was there more to it?

Can I pass a parameter to ":FOO" ?

That'd be the "more," I suppose.

Cheers,
David.
--
David Fetter <david@fetter.org> http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david.fetter@gmail.com
iCal: webcal://www.tripit.com/feed/ical/people/david74/tripit.ics

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate

#8Tom Lane
tgl@sss.pgh.pa.us
In reply to: David Fetter (#4)
Re: [PATCH] psql \n shortcut for set search_path =

David Fetter <david@fetter.org> writes:

On Tue, Jul 10, 2012 at 12:00:06PM -0400, Tom Lane wrote:

ISTM there was some discussion awhile back about user-definable
typing shortcuts in psql.

In some sense, we already have them:

Good point:

regression=# show search_path ;
search_path
----------------
"$user",public
(1 row)

regression=# \set n 'set search_path ='
regression=# :n foo;
SET
regression=# show search_path ;
search_path
-------------
foo
(1 row)

So maybe what's needed here is a documentation example showing how you
can use a \set in ~/.psqlrc to provide this sort of functionality.

regards, tom lane

#9Björn Häuser
bjoernhaeuser@gmail.com
In reply to: Tom Lane (#8)
Re: [PATCH] psql \n shortcut for set search_path =

Am 10.07.2012 18:44, schrieb Tom Lane:

David Fetter <david@fetter.org> writes:

On Tue, Jul 10, 2012 at 12:00:06PM -0400, Tom Lane wrote:

ISTM there was some discussion awhile back about user-definable
typing shortcuts in psql.

In some sense, we already have them:

Good point:

regression=# show search_path ;
search_path
----------------
"$user",public
(1 row)

regression=# \set n 'set search_path ='
regression=# :n foo;
SET
regression=# show search_path ;
search_path
-------------
foo
(1 row)

Well, a separate command would be mandatory to have tab-completion?
Maybe not a single-letter one, but I really would appreciate such an
command.
Setting the search_path is a thing I do several times a day.

Bjᅵrn

Show quoted text

So maybe what's needed here is a documentation example showing how you
can use a \set in ~/.psqlrc to provide this sort of functionality.

regards, tom lane