BUG #5035: cast 'text' to 'name' doesnt work in plpgsql function

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
Thread:
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.

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Dennis Seran 2009-09-04 14:15:23 BUG #5036: Advisory locks have unexpected behavior
Previous Message Peter Eisentraut 2009-09-04 06:16:08 Re: BUG #5033: Fallback using Warm Standby