Re: Indexes in PL/SQL

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: Indexes in PL/SQL
Дата
Msg-id 9764.1054307805@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Indexes in PL/SQL  (Tony Holmes <tony@crosswinds.net>)
Ответы Re: Indexes in PL/SQL  (Tony Holmes <tony@crosswinds.net>)
Список 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 по дате отправления:

Предыдущее
От: Joe Conway
Дата:
Сообщение: Re: Indexes in PL/SQL
Следующее
От: Tony Holmes
Дата:
Сообщение: Re: Indexes in PL/SQL