pg.ddx.io pgsql-performance@postgresql.org mailing list archive
help / color / mirror / Atom feedBest practices for update timestamp with/without triggers
2+ messages / 2 participants
[nested] [flat]
* Best practices for update timestamp with/without triggers
@ 2014-04-22 01:16 Verghese, Riya <rverghese@cobalt.com>
0 siblings, 1 reply; 2+ messages in thread
From: Verghese, Riya @ 2014-04-22 01:16 UTC (permalink / raw)
To: pgsql-performance
Hi
I am going to add a new column to a table for modify_date that needs to be updated every time the table is updated. Is it better to just update application code to set the modify_date to current_time, or create a Before-Update trigger on the table that will update the modify_date column to current_timestamp when the table is updated? I also have slony in place, so the trigger will need to be on master and slave. Slony will take care of suppressing it on the slave and enabling in the event of a switchover, but it is additional overhead and validation to make sure nothing failed on switchover.
So considering that we have slony, is it better to use application code to update the modify_date or use a trigger?Is a trigger essentially 2 updates to the table? Are there any other risks in using the trigger?
Thanks
Riya Verghese
This message and any attachments are intended only for the use of the addressee and may contain information that is privileged and confidential. If the reader of the message is not the intended recipient or an authorized representative of the intended recipient, you are hereby notified that any dissemination of this communication is strictly prohibited. If you have received this communication in error, please notify us immediately by e-mail and delete the message and any attachments from your system.
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: Best practices for update timestamp with/without triggers
@ 2014-04-23 10:28 hubert depesz lubaczewski <depesz@depesz.com>
parent: Verghese, Riya <rverghese@cobalt.com>
0 siblings, 0 replies; 2+ messages in thread
From: hubert depesz lubaczewski @ 2014-04-23 10:28 UTC (permalink / raw)
To: Verghese, Riya <rverghese@cobalt.com>; +Cc: pgsql-performance
On Tue, Apr 22, 2014 at 01:16:15AM +0000, Verghese, Riya wrote:
> I am going to add a new column to a table for modify_date that needs
> to be updated every time the table is updated. Is it better to just
> update application code to set the modify_date to current_time, or
> create a Before-Update trigger on the table that will update the
> modify_date column to current_timestamp when the table is updated?
> I also have slony in place, so the trigger will need to be on master
> and slave. Slony will take care of suppressing it on the slave and
> enabling in the event of a switchover, but it is additional overhead
> and validation to make sure nothing failed on switchover.
> So considering that we have slony, is it better to use application
> code to update the modify_date or use a trigger?Is a trigger
> essentially 2 updates to the table? Are there any other risks in using
> the trigger?
It's better (in my opinion) to use trigger. And it's not two updates.
Just make your trigger function like:
create function sample_trigger() returns trigger as $$
BEGIN
NEW.modify_date := clock_timestamp();
RETURN NEW;
END;
$$ language plpgsql;
and that's all.
Best regards,
depesz
--
The best thing about modern society is how easy it is to avoid contact with it.
http://depesz.com/
Attachments:
signature.asc (application/pgp-signature, 800B) download
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2014-04-23 10:28 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-04-22 01:16 Best practices for update timestamp with/without triggers Verghese, Riya <rverghese@cobalt.com>
2014-04-23 10:28 ` hubert depesz lubaczewski <depesz@depesz.com>
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