From: Michael Fuhr <mike@fuhr.org>
To: Henry Ortega <juandelacruz@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Trigger on Insert to Update only newly inserted fields?
Date: Tue, 29 Aug 2006 04:38:58 -0600
Message-ID: <20060829103857.GA20419@winnie.fuhr.org> (raw)
In-Reply-To: <2bffcc330608280853x50b1034nca246a5dcc935e7@mail.gmail.com>
References: <2bffcc330608280702y16c25090i816589e20163e7e8@mail.gmail.com>
<20060828145737.GA5004@winnie.fuhr.org>
<2bffcc330608280853x50b1034nca246a5dcc935e7@mail.gmail.com>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: mike@fuhr.org, juandelacruz@gmail.com
Subject: Re: Trigger on Insert to Update only newly inserted fields?
In-Reply-To: <20060829103857.GA20419@winnie.fuhr.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox