Re: BUG #5035: cast 'text' to 'name' doesnt work in plpgsqlfunction

Lists: pgsql-bugs
From: "" <tkarlik(at)ultimo(dot)pl>
To: pgsql-bugs(at)postgresql(dot)org
Subject: BUG #5035: cast 'text' to 'name' doesnt work in plpgsql function
Date: 2009-09-04 10:27:44
Message-ID: 200909041027.n84ARiNB078986@wwwmaster.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs


The following bug has been logged online:

Bug reference: 5035
Logged by:
Email address: tkarlik(at)ultimo(dot)pl
PostgreSQL version: 8.3.6
Operating system: Linux
Description: cast 'text' to 'name' doesnt work in plpgsql function
Details:

Comparing 'text' to 'name' in plpgsl function needs explicit casting to
name:

CREATE OR REPLACE FUNCTION table_exists(tblname text) RETURNS boolean AS '
DECLARE
exists boolean;
BEGIN
SELECT 1 INTO exists FROM pg_class WHERE relname = name($1);
RETURN exists;
END;
' LANGUAGE 'plpgsql' IMMUTABLE;

test_db=# select table_exists('test_table');
table_exists
--------------

(1 row)

Time: 0,561 ms

test_db=# select 1 from pg_class where relname = 'test_table';
?column?
----------
(0 rows)

Time: 0,337 ms

Without casting function executes much slower:

CREATE OR REPLACE FUNCTION table_exists(tblname text) RETURNS boolean AS '
DECLARE
exists boolean;
BEGIN
SELECT 1 INTO exists FROM pg_class WHERE relname = name($1);
RETURN exists;
END;
' LANGUAGE 'plpgsql' IMMUTABLE;

test_db=# select table_exists('test_table');
table_exists
--------------

(1 row)

Time: 15,022 ms

Database contains more than 20 000 pg_class tuples.


From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: tkarlik(at)ultimo(dot)pl
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #5035: cast 'text' to 'name' doesnt work in plpgsql function
Date: 2009-09-04 17:23:06
Message-ID: 20090904172306.GJ5603@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

tkarlik(at)ultimo(dot)pl wrote:

> Without casting function executes much slower:
>
> CREATE OR REPLACE FUNCTION table_exists(tblname text) RETURNS boolean AS '
> DECLARE
> exists boolean;
> BEGIN
> SELECT 1 INTO exists FROM pg_class WHERE relname = name($1);
> RETURN exists;
> END;
> ' LANGUAGE 'plpgsql' IMMUTABLE;

If you're looking for a speedy answer, try a SQL function, not plpgsql.

--
Alvaro Herrera http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: tkarlik(at)ultimo(dot)pl, pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #5035: cast 'text' to 'name' doesnt work in plpgsql function
Date: 2009-09-04 18:04:58
Message-ID: 16769.1252087498@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

Alvaro Herrera <alvherre(at)commandprompt(dot)com> writes:
> tkarlik(at)ultimo(dot)pl wrote:
>> Without casting function executes much slower:

> If you're looking for a speedy answer, try a SQL function, not plpgsql.

He's still going to need the cast to name. It's not a bug, it's just
how things work: the indexes on pg_class support name = name equality
tests, not text = text.

regards, tom lane


From: "Tomasz Karlik" <Tomasz(dot)Karlik(at)ultimo(dot)pl>
To: "Alvaro Herrera" <alvherre(at)commandprompt(dot)com>, <pgsql-bugs(at)postgresql(dot)org>
Subject: Odp: Re: BUG #5035: cast 'text' to 'name' doesnt work in plpgsqlfunction
Date: 2009-09-07 06:28:45
Message-ID: 4AA4C43D0200002D0000321B@mars.ultimo.pl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

>>> Alvaro Herrera <alvherre(at)commandprompt(dot)com> 9/4/2009 7:23 PM >>>
tkarlik(at)ultimo(dot)pl wrote:

> Without casting function executes much slower:
>
> CREATE OR REPLACE FUNCTION table_exists(tblname text) RETURNS boolean AS '
> DECLARE
> exists boolean;
> BEGIN
> SELECT 1 INTO exists FROM pg_class WHERE relname = name($1);
> RETURN exists;
> END;
> ' LANGUAGE 'plpgsql' IMMUTABLE;

If you're looking for a speedy answer, try a SQL function, not plpgsql.

The same issue when using SQL function... However other casting (for example int4->int8) works properly.


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Tomasz Karlik <Tomasz(dot)Karlik(at)ultimo(dot)pl>
Cc: Alvaro Herrera <alvherre(at)commandprompt(dot)com>, pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #5035: cast 'text' to 'name' doesnt work in plpgsqlfunction
Date: 2009-09-07 13:47:19
Message-ID: 162867790909070647k18d7024dh53497a040edb7055@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

Hello

2009/9/7 Tomasz Karlik <Tomasz(dot)Karlik(at)ultimo(dot)pl>:
>
>
>>>> Alvaro Herrera <alvherre(at)commandprompt(dot)com> 9/4/2009 7:23 PM >>>
> tkarlik(at)ultimo(dot)pl wrote:
>
>> Without casting function executes much slower:
>>
>> CREATE OR REPLACE FUNCTION table_exists(tblname text) RETURNS boolean AS '
>> DECLARE
>> exists boolean;
>> BEGIN
>>     SELECT 1 INTO exists FROM pg_class WHERE relname = name($1);
>>     RETURN exists;
>> END;
>> ' LANGUAGE 'plpgsql' IMMUTABLE;
>

it some strange. What version do you use?

on 5.4

postgres=# explain select * from pg_class where relname='aaa';
QUERY PLAN

--------------------------------------------------------------------------------
-------------
Index Scan using pg_class_relname_nsp_index on pg_class (cost=0.00..8.27 rows=
1 width=185)
Index Cond: (relname = 'aaa'::name)
(2 rows)

the casting is implicit.

regards
Pavel Stehule

> If you're looking for a speedy answer, try a SQL function, not plpgsql.
>
>
> The same issue when using SQL function... However other casting (for example
> int4->int8) works properly.
>


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Tomasz Karlik <Tomasz(dot)Karlik(at)ultimo(dot)pl>
Cc: Alvaro Herrera <alvherre(at)commandprompt(dot)com>, pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #5035: cast 'text' to 'name' doesnt work in plpgsqlfunction
Date: 2009-09-07 13:47:51
Message-ID: 162867790909070647g5c7a731ag5c17695f11e8e4d5@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

2009/9/7 Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>:
> Hello
>
> 2009/9/7 Tomasz Karlik <Tomasz(dot)Karlik(at)ultimo(dot)pl>:
>>
>>
>>>>> Alvaro Herrera <alvherre(at)commandprompt(dot)com> 9/4/2009 7:23 PM >>>
>> tkarlik(at)ultimo(dot)pl wrote:
>>
>>> Without casting function executes much slower:
>>>
>>> CREATE OR REPLACE FUNCTION table_exists(tblname text) RETURNS boolean AS '
>>> DECLARE
>>> exists boolean;
>>> BEGIN
>>>     SELECT 1 INTO exists FROM pg_class WHERE relname = name($1);
>>>     RETURN exists;
>>> END;
>>> ' LANGUAGE 'plpgsql' IMMUTABLE;
>>
>
> it some strange. What version do you use?
>
> on 5.4

sorry 8.4

>
> postgres=# explain select * from pg_class where relname='aaa';
>                                         QUERY PLAN
>
> --------------------------------------------------------------------------------
> -------------
>  Index Scan using pg_class_relname_nsp_index on pg_class  (cost=0.00..8.27 rows=
> 1 width=185)
>   Index Cond: (relname = 'aaa'::name)
> (2 rows)
>
> the casting is implicit.
>
> regards
> Pavel Stehule
>
>> If you're looking for a speedy answer, try a SQL function, not plpgsql.
>>
>>
>> The same issue when using SQL function... However other casting (for example
>> int4->int8) works properly.
>>
>


From: "Tomasz Karlik" <Tomasz(dot)Karlik(at)ultimo(dot)pl>
To: "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>, <pgsql-bugs(at)postgresql(dot)org>
Subject: Odp: Re: Re: BUG #5035: cast 'text' to 'name' doesnt work in plpgsqlfunction
Date: 2009-09-08 05:48:09
Message-ID: 4AA60C390200002D00003261@mars.ultimo.pl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

>>> Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> 9/7/2009 3:47 PM >>>
Hello

2009/9/7 Tomasz Karlik <Tomasz(dot)Karlik(at)ultimo(dot)pl>:
>
>
>>>> Alvaro Herrera <alvherre(at)commandprompt(dot)com> 9/4/2009 7:23 PM >>>
> tkarlik(at)ultimo(dot)pl wrote:
>
>> Without casting function executes much slower:
>>
>> CREATE OR REPLACE FUNCTION table_exists(tblname text) RETURNS boolean AS '
>> DECLARE
>> exists boolean;
>> BEGIN
>> SELECT 1 INTO exists FROM pg_class WHERE relname = name($1);
>> RETURN exists;
>> END;
>> ' LANGUAGE 'plpgsql' IMMUTABLE;
>

it some strange. What version do you use?

on 5.4

postgres=# explain select * from pg_class where relname='aaa';
QUERY PLAN

--------------------------------------------------------------------------------
-------------
Index Scan using pg_class_relname_nsp_index on pg_class (cost=0.00..8.27 rows=
1 width=185)
Index Cond: (relname = 'aaa'::name)
(2 rows)

the casting is implicit.


It does'nt work only inside function. Look for execution times in my first post. Maybe the planner treats SQL SELECT query other than procedural SELECT INTO?