Using schemas is fun. Setting the search_path every time I use psql
isn't. This patch modifies startup.c in psql to allow the SEARCH_PATH
to be set to whatever the PG_SCHEMA_SEARCH_PATH environment variable is
set to. If the var is not defined or is empty, no action is taken and
the search path is what it would be without this patch. If
PG_SCHEMA_SEARCH_PATH is set to garbage, psql simply reports the
database error about non-existent schemas and continues running with
the search_path it would have without this patch. The patch is against
PG 7.4.1.
Here's an example session:
powerbook> PG_SCHEMA_SEARCH_PATH="'public','core','objects'"
powerbook> export PG_SCHEMA_SEARCH_PATH
powerbook> ./psql -d opencce
SET
Welcome to psql 7.4.1, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help on internal slash commands
\g or terminate with semicolon to execute query
\q to quit
opencce=# show search_path;
search_path
-----------------------
public, core, objects
(1 row)
opencce=#
The patch is small enough to copy and paste here; I've also attached it
to this message.
thanks,
/s.
PATCH:
diff -c -r postgresql-7.4.1/src/bin/psql/startup.c
postgresql-7.4.1.mod/src/bin/psql/startup.c
*** postgresql-7.4.1/src/bin/psql/startup.c Mon Sep 29 13:21:33 2003
--- postgresql-7.4.1.mod/src/bin/psql/startup.c Mon Feb 9 17:52:08 2004
***************
*** 98,103 ****
--- 98,105 ----
char *username = NULL;
char *password = NULL;
+ char *potential_SearchPath = NULL;
+ char *searchpath = NULL;
bool need_pass;
setlocale(LC_ALL, "");
***************
*** 209,214 ****
--- 211,227 ----
PQsetNoticeProcessor(pset.db, NoticeProcessor, NULL);
SyncVariables();
+
+ /*
+ * Set schema search path from environment
+ */
+
+ potential_SearchPath = getenv("PG_SCHEMA_SEARCH_PATH");
+ if (potential_SearchPath != NULL) {
+ searchpath = (char *) palloc(sizeof("SET SEARCH_PATH TO ") +
strlen(potential_SearchPath));
+ sprintf(searchpath, "SET SEARCH_PATH TO %s",
potential_SearchPath);
+ successResult = SendQuery(searchpath) ? EXIT_SUCCESS :
EXIT_FAILURE;
+ }
if (options.action == ACT_LIST_DB)
{