Re: Trigger on Insert to Update only newly inserted fields?

Поиск
Список
Период
Сортировка
От Michael Fuhr
Тема Re: Trigger on Insert to Update only newly inserted fields?
Дата
Msg-id 20060829103857.GA20419@winnie.fuhr.org
обсуждение исходный текст
Ответ на Re: Trigger on Insert to Update only newly inserted fields?  ("Henry Ortega" <juandelacruz@gmail.com>)
Список pgsql-sql
On Mon, Aug 28, 2006 at 11:53:36AM -0400, Henry Ortega wrote:
> CREATE FUNCTION updated_end_date() RETURNS trigger AS '
> BEGIN
>    update table set end_date=(select effective-1 from table t2 where
> t2.employee=table.employee and t2.effective>table.effective order by
> t2.effective limit 1);
>    RETURN NEW;
> END;
> ' LANGUAGE 'plpgsql';
> 
> That updates ALL of the records in the table which takes so long.
> Should I be doing things like this? Or is the update query on my trigger
> function so wrong?

You're updating the same table that has the trigger?  Beware of
endless trigger recursion.

You're not restricting the UPDATE with a WHERE clause, which explains
why it updates the entire table.  Maybe you meant this:
 update table set end_date = (...) where employee = new.employee;

The subselect for each row also slows down the update, although you
might not be able to avoid that if requirements demand a potentially
distinct end_date for each row.

-- 
Michael Fuhr


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

Предыдущее
От: "Manso Gomez, Ramon"
Дата:
Сообщение: dinamic sql
Следующее
От: "Jaime Casanova"
Дата:
Сообщение: Re: dinamic sql