[PATCH] Add \ns command to psql

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

Hi,

Here's a new version of a small patch to psql I'm using locally.

It adds a command \ns to psql which is a shortcut to set the
SEARCH_PATH variable.

I'd like to make a case for including this patch as it makes use of
schemas/namespaces much easier. There was resistance to including this
before just because some developers don't use schemas very much. But
we use a lot of them. And I'm sure we're not alone.

Previously I used just \n but there was some resistance to this
because the single letter commands are becoming scarce.

I've also added tab completion making this command much more useful. I
don't think tab completition would be possible if this command was
defined as a variable (which was another suggestion offered at the
time).

Cheers,

Colin

Attachments:

command.c.diffapplication/octet-stream; name=command.c.diffDownload
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 09939fd..1c4af37 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -901,6 +901,41 @@ exec_command(const char *cmd,
 		free(opt2);
 	}
 
+        /* \ns -- shortcut for set search_path */
+        else if (strcmp(cmd, "ns") == 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)
tab-complete.c.diffapplication/octet-stream; name=tab-complete.c.diffDownload
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 7d2c812..e993ccf 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -3313,6 +3313,8 @@ psql_completion(char *text, int start, int end)
 		COMPLETE_WITH_QUERY(Query_for_list_of_encodings);
 	else if (strcmp(prev_wd, "\\h") == 0 || strcmp(prev_wd, "\\help") == 0)
 		COMPLETE_WITH_LIST(sql_commands);
+        else if (strcmp(prev_wd, "\\ns") == 0)
+                COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
 	else if (strcmp(prev_wd, "\\password") == 0)
 		COMPLETE_WITH_QUERY(Query_for_list_of_roles);
 	else if (strcmp(prev_wd, "\\pset") == 0)
#2Robert Haas
robertmhaas@gmail.com
In reply to: Colin 't Hart (#1)
Re: [PATCH] Add \ns command to psql

On Tue, Apr 16, 2013 at 5:40 AM, Colin 't Hart <colin@sharpheart.org> wrote:

Here's a new version of a small patch to psql I'm using locally.

It adds a command \ns to psql which is a shortcut to set the
SEARCH_PATH variable.

I've also added tab completion making this command much more useful. I
don't think tab completition would be possible if this command was
defined as a variable (which was another suggestion offered at the
time).

It's possible that the tab completion argument is a sufficient reason
for including this, but I'm kinda skeptical. The amount of typing
saved is pretty minimal, considering that set sea<tab> completes to
set search_path. Assuming we had proper tab completion for set
search_path = (and off-hand, it doesn't look like that does anything
useful), this would be saving 5 keystrokes every time you want to
change the search path (set sea<tab> is eight keystrokes, where
\ns<space> is four... but it also saves you the semicolon at the end).
I'm sure some people would find that worthwhile, but personally, I
don't. Short commands are cryptic, and IMHO psql is already an
impenetrable thicket of difficult-to-remember abbreviations. I've
been using it for more than 10 years now and I still have to to run \?
on a semi-regular basis. I think that if we start adding things like
this, that help message is going to rapidly fill up with a whole lot
more abbreviations for things that are quite a bit incrementally less
useful than what's there right now.

After all, if we're going to have \ns to set the search path, why not
have something similar for work_mem, or random_page_cost? I set both
of those variables more often than I set search_path; and there could
easily be someone else out there whose favorite GUC is client_encoding
or whatever. And, for that matter, why stop with GUCs? \ct for
CREATE TABLE would save lots of typing, too....

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers