Unix Technical Forum

BUG #3628: Wrong schema picked

This is a discussion on BUG #3628: Wrong schema picked within the pgsql Bugs forums, part of the PostgreSQL category; --> The following bug has been logged online: Bug reference: 3628 Logged by: Pedro Gimeno Email address: pgsql-001@personal.formauri.es PostgreSQL version: ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Bugs

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-10-2008, 12:08 PM
Pedro Gimeno
 
Posts: n/a
Default BUG #3628: Wrong schema picked


The following bug has been logged online:

Bug reference: 3628
Logged by: Pedro Gimeno
Email address: pgsql-001@personal.formauri.es
PostgreSQL version: 8.2.4
Operating system: Linux (Debian stable + backports)
Description: Wrong schema picked
Details:

When a function has a SQL statement to execute that has an unqualified
table, that SQL statement doesn't always pick the table from a schema in the
search_path. The following script is an example:

-- begin script
create database test2;
\c test2
create language 'plpgsql';
create schema schema1;
create schema schema2;
create table schema1.table1 (column1 varchar(10));
create table schema2.table1 (column1 varchar(10));
create function public.f1() returns void as $$
begin
insert into table1 values (current_schema());
end;
$$ language plpgsql;
set search_path=schema1,public;
select f1();
set search_path=schema2,public;
select f1();
select * from schema1.table1;
select * from schema2.table1;
-- end script

The output of the last couple of SELECT statements is:

column1
---------
schema1
schema2
(2 rows)

column1
---------
(0 rows)

while the expected result would be:

column1
---------
schema1
(1 row)

column1
---------
schema2
(1 row)

-- Pedro Gimeno

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-10-2008, 12:08 PM
Heikki Linnakangas
 
Posts: n/a
Default Re: BUG #3628: Wrong schema picked

Pedro Gimeno wrote:
> When a function has a SQL statement to execute that has an unqualified
> table, that SQL statement doesn't always pick the table from a schema in the
> search_path.


The first time the function is run, all the statements in it are planned
and the schema used for the insert is resolved. Subsequent calls will
reuse the same plan. The obvious fix is to always explicitly specify the
schema in the INSERT. Or if if you want it to depend on the search_path,
you can execute it dynamically using EXECUTE
(http://www.postgresql.org/docs/8.2/s...EXECUTING-DYN).

--
Heikki Linnakangas
EnterpriseDB http://www.enterprisedb.com

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

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-10-2008, 12:08 PM
Pedro Gimeno
 
Posts: n/a
Default Re: BUG #3628: Wrong schema picked


Heikki Linnakangas wrote:

> Pedro Gimeno wrote:
> > When a function has a SQL statement to execute that has an
> > unqualified table, that SQL statement doesn't always pick the table
> > from a schema in the search_path.

>
> The first time the function is run, all the statements in it are
> planned and the schema used for the insert is resolved. Subsequent
> calls will reuse the same plan. The obvious fix is to always
> explicitly specify the schema in the INSERT. Or if if you want it to
> depend on the search_path, you can execute it dynamically using
> EXECUTE
> (http://www.postgresql.org/docs/8.2/s...EXECUTING-DYN).


That's the workaround I'm using, yet I find this behaviour quite
unexpected. Using EXECUTE prevents the benefit of using prepared
statements until search_path changes or a schema is deleted or renamed,
not to mention the impact on readability.

What I expect is that when search_path changes, either explicitly or
implicitly (e.g. by creating a temporary table), or a schema is deleted
or renamed, the affected prepared queries are parsed again so that the
right schema is picked.

This would allow using a common schema for functions instead of
defining them once in every schema in which to apply them, in cases
(like mine) where the same operations have to be applied to different
schemas.

-- Pedro Gimeno

---------------------------(end of broadcast)---------------------------
TIP 7: You can help support the PostgreSQL project by donating at

http://www.postgresql.org/about/donate

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-10-2008, 12:08 PM
Pedro Gimeno
 
Posts: n/a
Default Re: BUG #3628: Wrong schema picked

Heikki Linnakangas wrote:

> The first time the function is run, all the statements in it are
> planned and the schema used for the insert is resolved. Subsequent
> calls will reuse the same plan.


This post is apparently related:

http://archives.postgresql.org/pgsql...2/msg00933.php

-- Pedro Gimeno

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:07 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com