dbTalk Databases Forums  

[BUGS] pl/pgsql problem with search_path

mailing.database.pgsql-bugs mailing.database.pgsql-bugs


Discuss [BUGS] pl/pgsql problem with search_path in the mailing.database.pgsql-bugs forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Eugene Chow
 
Posts: n/a

Default [BUGS] pl/pgsql problem with search_path - 09-06-2003 , 01:34 PM






My plpgsql function seems to be ignoring search_path when looking for
the right table to select from. I'm running 7.3.4. Below is my test
code. Am I doing something wrong?

TIA, Gene Chow

test=> create or replace function getval() returns varchar as '
test'> declare val varchar;
test'> begin
test'> select into val value from bar limit 1;
test'> return val;
test'> end;' language 'plpgsql';
CREATE FUNCTION

test=> create table public.bar ( value varchar );
CREATE TABLE

test=> insert into public.bar values ('public value');
INSERT 4012748 1

test=> create schema foo;
CREATE SCHEMA

test=> create table foo.bar ( value varchar );
CREATE TABLE

test=> insert into foo.bar values ('foo value');
INSERT 4012754 1

test=> set search_path to foo, public;
SET

test=> select *, getval() from bar;
value | getval
-----------+-----------
foo value | foo value
(1 row)

test=> set search_path to public;
SET

test=> select *, getval() from bar;
value | getval
--------------+-----------
public value | foo value
(1 row)


---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Reply With Quote
  #2  
Old   
Bruce Momjian
 
Posts: n/a

Default Re: [BUGS] pl/pgsql problem with search_path - 09-06-2003 , 06:24 PM







I think the problem is that the first time the function is called, it is
compiled and cached for later use. At that time the function is bound
to the table oid, so even though you change the search path, the cached
copy still calls the old table.

If you exit psql and re-enter, or change the schema _before_ you call
the function for the first time, it should be fine.

This highlights another problem with our plpgsql function caching.

---------------------------------------------------------------------------

Eugene Chow wrote:
Quote:
My plpgsql function seems to be ignoring search_path when looking for
the right table to select from. I'm running 7.3.4. Below is my test
code. Am I doing something wrong?

TIA, Gene Chow

test=> create or replace function getval() returns varchar as '
test'> declare val varchar;
test'> begin
test'> select into val value from bar limit 1;
test'> return val;
test'> end;' language 'plpgsql';
CREATE FUNCTION

test=> create table public.bar ( value varchar );
CREATE TABLE

test=> insert into public.bar values ('public value');
INSERT 4012748 1

test=> create schema foo;
CREATE SCHEMA

test=> create table foo.bar ( value varchar );
CREATE TABLE

test=> insert into foo.bar values ('foo value');
INSERT 4012754 1

test=> set search_path to foo, public;
SET

test=> select *, getval() from bar;
value | getval
-----------+-----------
foo value | foo value
(1 row)

test=> set search_path to public;
SET

test=> select *, getval() from bar;
value | getval
--------------+-----------
public value | foo value
(1 row)


---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

--
Bruce Momjian | http://candle.pha.pa.us
pgman (AT) candle (DOT) pha.pa.us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org


Reply With Quote
  #3  
Old   
Tom Lane
 
Posts: n/a

Default Re: [BUGS] pl/pgsql problem with search_path - 09-06-2003 , 08:29 PM



Bruce Momjian <pgman (AT) candle (DOT) pha.pa.us> writes:
Quote:
This highlights another problem with our plpgsql function caching.
It's a little disturbing to think that any change in SEARCH_PATH might
force us to discard all cached plans. That could be expensive; and
consider a function that deliberately sets SEARCH_PATH to ensure that
it gets the tables it wants. You wouldn't want such a function to be
unable to cache any plans across calls (not to mention blowing away
every other function's plans, too).

We'd probably better record with each plan the SEARCH_PATH it was
generated with. Then, as long as that matches the current setting,
we can re-use the plan.

Of course, none of this is going to happen until someone gets around to
creating infrastructure for flushing cached plans at need. Right at the
moment the answer is going to have to be "don't do that".

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org


Reply With Quote
  #4  
Old   
Bruce Momjian
 
Posts: n/a

Default Re: [BUGS] pl/pgsql problem with search_path - 09-06-2003 , 08:40 PM



Eugene Chow wrote:
Quote:
Thanks. If I had only read the manual before posting. I solved it by
using 'execute' instead of 'select into' since the search_path is not
static in my system.
Yep, that's the workaround mentioned in the FAQ.

--
Bruce Momjian | http://candle.pha.pa.us
pgman (AT) candle (DOT) pha.pa.us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html


Reply With Quote
  #5  
Old   
Bruce Momjian
 
Posts: n/a

Default Re: [BUGS] pl/pgsql problem with search_path - 09-06-2003 , 08:43 PM



Tom Lane wrote:
Quote:
Bruce Momjian <pgman (AT) candle (DOT) pha.pa.us> writes:
This highlights another problem with our plpgsql function caching.

It's a little disturbing to think that any change in SEARCH_PATH might
force us to discard all cached plans. That could be expensive; and
consider a function that deliberately sets SEARCH_PATH to ensure that
it gets the tables it wants. You wouldn't want such a function to be
unable to cache any plans across calls (not to mention blowing away
every other function's plans, too).

We'd probably better record with each plan the SEARCH_PATH it was
generated with. Then, as long as that matches the current setting,
we can re-use the plan.

Of course, none of this is going to happen until someone gets around to
creating infrastructure for flushing cached plans at need. Right at the
moment the answer is going to have to be "don't do that".
Yep. I was just surprised it highlighted another failure of cached
plans.

--
Bruce Momjian | http://candle.pha.pa.us
pgman (AT) candle (DOT) pha.pa.us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings


Reply With Quote
  #6  
Old   
Gaetano Mendola
 
Posts: n/a

Default Re: [BUGS] pl/pgsql problem with search_path - 09-07-2003 , 12:25 PM



"Bruce Momjian" <pgman (AT) candle (DOT) pha.pa.us> wrote:
Quote:
Tom Lane wrote:
Bruce Momjian <pgman (AT) candle (DOT) pha.pa.us> writes:
This highlights another problem with our plpgsql function caching.

It's a little disturbing to think that any change in SEARCH_PATH might
force us to discard all cached plans. That could be expensive; and
consider a function that deliberately sets SEARCH_PATH to ensure that
it gets the tables it wants. You wouldn't want such a function to be
unable to cache any plans across calls (not to mention blowing away
every other function's plans, too).

We'd probably better record with each plan the SEARCH_PATH it was
generated with. Then, as long as that matches the current setting,
we can re-use the plan.

Of course, none of this is going to happen until someone gets around to
creating infrastructure for flushing cached plans at need. Right at the
moment the answer is going to have to be "don't do that".

Yep. I was just surprised it highlighted another failure of cached
plans.
There is already a TODO for it ?


Regards
Gaetano Mendola



---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match


Reply With Quote
  #7  
Old   
Bruce Momjian
 
Posts: n/a

Default Re: [BUGS] pl/pgsql problem with search_path - 09-07-2003 , 05:02 PM



Gaetano Mendola wrote:
Quote:
"Bruce Momjian" <pgman (AT) candle (DOT) pha.pa.us> wrote:
Tom Lane wrote:
Bruce Momjian <pgman (AT) candle (DOT) pha.pa.us> writes:
This highlights another problem with our plpgsql function caching.

It's a little disturbing to think that any change in SEARCH_PATH might
force us to discard all cached plans. That could be expensive; and
consider a function that deliberately sets SEARCH_PATH to ensure that
it gets the tables it wants. You wouldn't want such a function to be
unable to cache any plans across calls (not to mention blowing away
every other function's plans, too).

We'd probably better record with each plan the SEARCH_PATH it was
generated with. Then, as long as that matches the current setting,
we can re-use the plan.

Of course, none of this is going to happen until someone gets around to
creating infrastructure for flushing cached plans at need. Right at the
moment the answer is going to have to be "don't do that".

Yep. I was just surprised it highlighted another failure of cached
plans.

There is already a TODO for it ?
Yep:

o Fix problems with complex temporary table creation/destruction
without using PL/PgSQL EXECUTE, needs cache prevention/invalidation

--
Bruce Momjian | http://candle.pha.pa.us
pgman (AT) candle (DOT) pha.pa.us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.