Re: Indexes in PL/SQL - Mailing list pgsql-novice

From Tom Lane
Subject Re: Indexes in PL/SQL
Date
Msg-id 9764.1054307805@sss.pgh.pa.us
Whole thread Raw
In response to Indexes in PL/SQL  (Tony Holmes <tony@crosswinds.net>)
Responses Re: Indexes in PL/SQL
List pgsql-novice
Tony Holmes <tony@crosswinds.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

pgsql-novice by date:

Previous
From: Joe Conway
Date:
Subject: Re: Indexes in PL/SQL
Next
From: Tony Holmes
Date:
Subject: Re: Indexes in PL/SQL