Re: Defining and Using variables in a postgres function

Поиск
Список
Период
Сортировка
От Jim Nasby
Тема Re: Defining and Using variables in a postgres function
Дата
Msg-id 9894F93D-9B82-45C2-B135-258D150622C1@decibel.org
обсуждение исходный текст
Ответ на Defining and Using variables in a postgres function  ("Harpreet Dhaliwal" <harpreet.dhaliwal01@gmail.com>)
Список pgsql-general
On Feb 1, 2007, at 12:09 PM, Harpreet Dhaliwal wrote:
> For inserting the id, i need to query a table xyz, fetch the
> maximum id in it, increment it by 1 and store it in tbl_email.
> Right after BEGIN in my function I have a commnet where in I need
> to query the xyz table, fetch the max id and store it in a variable
> and then I can increment this variable and store it in tbl_email.

That's not safe unless you get an exclusive lock on the table before
the select.

Why don't you just use a sequence/serial?

> How should i define this variable first and how to push the result
> of the query fired on table xyz.

SELECT INTO v_max_id
    max(id)
    FROM table
;
--
Jim Nasby                                            jim@nasby.net
EnterpriseDB      http://enterprisedb.com      512.569.9461 (cell)



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

Предыдущее
От: Jim Nasby
Дата:
Сообщение: Re: VACUUM ANALYZE taking a long time, %I/O and %CPU very low
Следующее
От: Jim Nasby
Дата:
Сообщение: Re: SQL to get a table columns comments?