Re: BUG #3599: Wrong search_path inside a function - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #3599: Wrong search_path inside a function
Date
Msg-id 26403.1188923274@sss.pgh.pa.us
Whole thread Raw
In response to BUG #3599: Wrong search_path inside a function  ("Alexis Beuraud" <alexis@siatel.com>)
List pgsql-bugs
"Alexis Beuraud" <alexis@siatel.com> writes:
> EXECUTE (' set search_path to ' || p_schemaName ); ---- setting the search
> path here!
> FOR result in
>   select i
>   from TableT
> loop
> return next result;
>  END LOOP;

The reason that doesn't do what you expect is that the plan for the
SELECT is cached the first time through.  You'll need to use FOR IN
EXECUTE to make this work.  Rather than explicitly setting search_path
like that, which is likely to have unpleasant consequences all over the
place (hint: the effects persist after your function exits), you might
want to just insert the schema name into the EXECUTE string:

FOR result IN EXECUTE
    'select i from ' || quote_ident(p_schemaName) || '.TableT'
LOOP ...

            regards, tom lane

pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: BUG #3598: Strange behaviour of character columns in select with views
Next
From: Reece Hart
Date:
Subject: Re: BUG #3597: CREATE OR REPLACE VIEW