HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

Postgres: modify existing search_path (preserve current values)

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
valuespostgrespreservesearch_pathcurrentexistingmodify

Problem

Sometimes I would like to extend the existing search path, rather than replace it, say:

To start with say it is already set like so:

SET search_path TO schema_b, schema_c, public;


I want to add my schema to the front of the line:

SET search_path TO schema_a + search_path;   --doesn't work


I'm thinking in analogy to what I'd do in BASH:

PATH=path_a:$PATH


Bonus question, perhaps related: is there a way I can store the current path temporarily, so I can change it to something totally different, and then restore it without having to know what it was?

Solution

SELECT set_config('search_path', 'fred,'||current_setting('search_path'), false);


The false says it's not a transaction-LOCAL setting.

For the bonus question, you can store the value in a custom setting:

SELECT set_config('tmp.search_path', current_setting('search_path'), false);


From version 9.2 on, you don't even have to define this setting in postgresql.conf.

Code Snippets

SELECT set_config('search_path', 'fred,'||current_setting('search_path'), false);
SELECT set_config('tmp.search_path', current_setting('search_path'), false);

Context

StackExchange Database Administrators Q#81406, answer score: 13

Revisions (0)

No revisions yet.