Generating unique session ids

Поиск
Список
Период
Сортировка
От Antimon
Тема Generating unique session ids
Дата
Msg-id 1153950887.912685.40210@s13g2000cwa.googlegroups.com
обсуждение исходный текст
Ответы Re: Generating unique session ids  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-general
Hi, I need to generate sessions for logged in users to my website which
uses pgsql. So i decided to write a function which is this:

-------------------------------
CREATE OR REPLACE FUNCTION session_createsession(int4, "varchar")
RETURNS text AS
$BODY$
DECLARE
    sid TEXT;
BEGIN
    sid := md5(random());
    INSERT INTO sessions (id, accountid, ipaddress) VALUES (sid, $1, $2);
    return sid;
EXCEPTION
    WHEN unique_violation THEN
        return session_createsession($1, $2);
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE;
-------------------------------

As the id field is primary key, it should generate a unique violation
if duplicate ids created, might be seen rarely but wanted to solve it
anyway. So i decided to check it by changing "sid := md5(random());" to
"sid := extract(minute from now());" When i run it, returns the minute
as session key and inserts an entryy for it, but if i call it again in
the same minute, it never ends execution. I expected it to return the
minute when system clock minute changes but it runs forever.

Am i doing something wrong? I mean, there might be some implemendation
to have now() to return same value in a trancastion or something but
could not be sure. If there is something like that, is that function
safe to create session ids? Because if it goes into an infinite loop
like it does with the minute thing, it'd drain all system resources.

Thanks.


В списке pgsql-general по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: What's special about 1916-10-01 02:25:20? Odd jump in internal timestamptz representation
Следующее
От: Tom Lane
Дата:
Сообщение: Re: Generating unique session ids