Skip site navigation (1) Skip section navigation (2)

Patch to psql to allow SEARCH_PATH to be set from env

From: Scott Goodwin <scott(at)scottg(dot)net>
To: pgsql-patches(at)postgresql(dot)org
Subject: Patch to psql to allow SEARCH_PATH to be set from env
Date: 2004-02-10 12:34:56
Message-ID: 885E9C58-5BC5-11D8-9BF7-000A95A0910A@scottg.net (view raw or flat)
Thread:
Lists: pgsql-patches
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)
     {


Attachment: searchpath.patch
Description: application/octet-stream (1.1 KB)

Responses

pgsql-patches by date

Next:From: Bruce MomjianDate: 2004-02-10 13:22:02
Subject: Re: Dev version doesn't acknowledge cross type indexes
Previous:From: Petri JoosteDate: 2004-02-10 10:04:34
Subject: Afrikaans translation of libpq

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group