Re: Indexes in PL/SQL

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Tony Holmes <tony(at)crosswinds(dot)net>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Indexes in PL/SQL
Date: 2003-05-30 15:16:45
Message-ID: 9764.1054307805@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Tony Holmes <tony(at)crosswinds(dot)net> writes:
> user_main (
> username character varying(32),
> );

> CREATE FUNCTION valid_user(text) RETURNS text AS '
> DECLARE
> _user ALIAS FOR $1;
> _uid user_main.uid%TYPE;
> BEGIN
> SELECT uid INTO _uid FROM user_main WHERE username=_user;

This won't use the index because it's a cross-datatype comparison
(varchar versus text). You could change the declared type of the
function argument to varchar, or leave the function signature alone
and cast the argument to varchar in the SELECT:

SELECT uid INTO _uid FROM user_main WHERE username = _user::varchar;

7.4 will be more forgiving about text-versus-varchar discrepancies, but
you can still get burnt by this problem on most other cross-datatype
cases --- including char(n) versus varchar(n). Integer versus bigint
is another common gotcha for newbies.

regards, tom lane

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Tony Holmes 2003-05-30 15:30:35 Re: Indexes in PL/SQL
Previous Message Joe Conway 2003-05-30 15:02:33 Re: Indexes in PL/SQL