pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Best practice question
3+ messages / 3 participants
[nested] [flat]

* Best practice question
@ 2014-04-22 01:19 Tory M Blue <tmblue@gmail.com>
  2014-04-22 01:31 ` Re: Best practice question David G Johnston <david.g.johnston@gmail.com>
  2014-04-22 02:01 ` Re: Best practice question Sergey Konoplev <gray.ru@gmail.com>
  0 siblings, 2 replies; 3+ messages in thread

From: Tory M Blue @ 2014-04-22 01:19 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

Tory Blue

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Best practice question
  2014-04-22 01:19 Best practice question Tory M Blue <tmblue@gmail.com>
@ 2014-04-22 01:31 ` David G Johnston <david.g.johnston@gmail.com>
  1 sibling, 0 replies; 3+ messages in thread

From: David G Johnston @ 2014-04-22 01:31 UTC (permalink / raw)
  To: pgsql-performance

Tory M Blue wrote
> 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
> 
> Tory Blue

Not sure about the Slony trade-off but a before trigger will intercept
before any physical writes and so appears to be a single action.  I would
generally use a trigger so that you know the updared value is recorded (and
can readily add logic for the no-changes situation - if desired).  Not all
table activity has to be initiated by "the application" and since forgetting
to do so is not going to result in any kind of error the probability of the
field becoming useless is non-zero.

It will be slower than doing it native but whether or not that is
significant enough to discard the advantages of triggers is something only
you can decide - ideally after testing.

David J.




--
View this message in context: http://postgresql.1045698.n5.nabble.com/Best-practice-question-tp5801010p5801011.html
Sent from the PostgreSQL - performance mailing list archive at Nabble.com.


-- 
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Best practice question
  2014-04-22 01:19 Best practice question Tory M Blue <tmblue@gmail.com>
@ 2014-04-22 02:01 ` Sergey Konoplev <gray.ru@gmail.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Sergey Konoplev @ 2014-04-22 02:01 UTC (permalink / raw)
  To: Tory M Blue <tmblue@gmail.com>; +Cc: pgsql-performance

On Mon, Apr 21, 2014 at 6:19 PM, Tory M Blue <tmblue@gmail.com> 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?

In addition to the David's answer I would like to add the below.

AFAIK Slony does not make any difference here. No, trigger doesn't
mean 2 updates. It supplies its function with a NEW row variable where
you can change necessary columns and return the modified one as a
resulting one. Another risk is the case when you need to update 2
tables on different servers and have their modified_timestamp fields
in sync. Here you need to determine the new value of the column in the
application.

-- 
Kind regards,
Sergey Konoplev
PostgreSQL Consultant and DBA

http://www.linkedin.com/in/grayhemp
+1 (415) 867-9984, +7 (901) 903-0499, +7 (988) 888-1979
gray.ru@gmail.com


-- 
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2014-04-22 02:01 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-04-22 01:19 Best practice question Tory M Blue <tmblue@gmail.com>
2014-04-22 01:31 ` David G Johnston <david.g.johnston@gmail.com>
2014-04-22 02:01 ` Sergey Konoplev <gray.ru@gmail.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