BUG #5035: cast 'text' to 'name' doesnt work in plpgsql function - Mailing list pgsql-bugs

From
Subject BUG #5035: cast 'text' to 'name' doesnt work in plpgsql function
Date
Msg-id 200909041027.n84ARiNB078986@wwwmaster.postgresql.org
Whole thread Raw
Responses Re: BUG #5035: cast 'text' to 'name' doesnt work in plpgsql function  (Alvaro Herrera <alvherre@commandprompt.com>)
List pgsql-bugs
The following bug has been logged online:

Bug reference:      5035
Logged by:
Email address:      tkarlik@ultimo.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.

pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: BUG #5028: CASE returns ELSE value always when type is "char"
Next
From: "Dennis Seran"
Date:
Subject: BUG #5036: Advisory locks have unexpected behavior