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

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Reply via email to