Unix Technical Forum

Update timestamp on update

This is a discussion on Update timestamp on update within the pgsql Sql forums, part of the PostgreSQL category; --> I have a table like: CREATE TABLE products ( id int, status int, last_status_change timestamp DEFAULT now() ); What ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Sql

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 11:48 AM
Jeff Williams
 
Posts: n/a
Default Update timestamp on update

I have a table like:

CREATE TABLE products (
id int,
status int,
last_status_change timestamp DEFAULT now()
);

What I would like is that whenever the status is changed the
last_status_change timestamp is updated to the current time. I have had
a look at the rules and what I want would be similar to:

CREATE RULE last_status_change AS ON UPDATE
TO products WHERE NEW.status <> OLD.status
DO UPDATE products SET last_status_change = now() WHERE id = OLD.id;

Except of course that the above is recursive and doesn't work.

How can I do this?

Jeff

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-19-2008, 11:48 AM
Tom Lane
 
Posts: n/a
Default Re: Update timestamp on update

Jeff Williams <jeffw@globaldial.com> writes:
> last_status_change timestamp DEFAULT now()


> What I would like is that whenever the status is changed the
> last_status_change timestamp is updated to the current time.


For this you use an ON UPDATE trigger; rules are not a good way to solve
it. See the documentation about triggers. The first example on this
page does it along with a few other things:
http://developer.postgresql.org/docs...l-trigger.html

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-19-2008, 11:49 AM
Jeff Williams
 
Posts: n/a
Default Re: Update timestamp on update

Tom Lane wrote:

>Jeff Williams <jeffw@globaldial.com> writes:
>
>
>> last_status_change timestamp DEFAULT now()
>>
>>

>
>
>
>>What I would like is that whenever the status is changed the
>>last_status_change timestamp is updated to the current time.
>>
>>

>
>For this you use an ON UPDATE trigger; rules are not a good way to solve
>it. See the documentation about triggers. The first example on this
>page does it along with a few other things:
>http://developer.postgresql.org/docs...l-trigger.html
>
>

Thanks. Triggers was my first thought, but chapter 35 on Triggers didn't
really indicate a way I could do this easily and scared me with a lot of
c code. Maybe it is a good idea to present some of the more common
things you would want to do with triggers in the triggers chapter?

Jeff

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-19-2008, 11:49 AM
Michael Glaesemann
 
Posts: n/a
Default Re: Update timestamp on update


On Oct 13, 2005, at 11:52 , Tom Lane wrote:

> Jeff Williams <jeffw@globaldial.com> writes:
>
>> Thanks. Triggers was my first thought, but chapter 35 on Triggers
>> didn't
>> really indicate a way I could do this easily and scared me with a
>> lot of
>> c code.
>>

>
> Yeah. This is a documentation issue that's bothered me for awhile.
> The problem is that we treat the PL languages as add-ons and therefore
> the documentation of the "core" system shouldn't rely on them ... but
> that leaves us presenting C-code triggers as the only examples in
> chapter 35. There is a paragraph in there suggesting you go look at
> the PL languages first, but obviously it's not getting the job done.


I think examples in PL/pgSQL would perhaps be most appropriate, as
it's a PostgreSQL-specific language, arguably easy to read, and a PL
that doesn't rely on any external dependencies, so it should be
relatively easy for people to install it if they want. (IIRC, there's
even been discussion in the past of whether or not PL/pgSQL should be
installed by default.)


Michael Glaesemann
grzm myrealbox com




---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-19-2008, 11:49 AM
Andrew Sullivan
 
Posts: n/a
Default Re: Update timestamp on update

On Wed, Oct 12, 2005 at 10:52:04PM -0400, Tom Lane wrote:
> the documentation of the "core" system shouldn't rely on them ... but
> that leaves us presenting C-code triggers as the only examples in
> chapter 35. There is a paragraph in there suggesting you go look at
> the PL languages first, but obviously it's not getting the job done.
>
> Anybody have a better idea?


It could just be made a little friendlier, I think. At the beginning
of the trigger chapter is this:

--snip--
This chapter describes how to write trigger functions. Trigger
functions can be written in C or in some of the available procedural
languages. It is not currently possible to write a SQL-language
trigger function.
--snip--

We could just add a little note by way of modification. Here's a
(somewhat verbose, I fear) suggestion:

--snip--
This chapter describes how to write trigger functions. Trigger
functions can be written in C or in some of the available procedural
languages. This chapter deals only with functions that are written in
C. If you are unfamiliar with C, you may want also to look at the
chapters on procedural languages, because there are some examples
there that may be easier for you to understand. To use a procedural
language for a trigger, you will need to install that language; see
the relevant chapter for instructions on how to do so. It is not
currently possible to write a SQL-language trigger function.
--snip--

A

--
Andrew Sullivan | ajs@crankycanuck.ca
The plural of anecdote is not data.
--Roger Brinner

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-19-2008, 11:49 AM
Mike Diehl
 
Posts: n/a
Default Re: [DOCS] Update timestamp on update

Is a working example something that people would like to see? Or is this
considered a good use of research time?

On Thursday 13 October 2005 11:20 am, Jim C. Nasby wrote:
> On Wed, Oct 12, 2005 at 10:52:04PM -0400, Tom Lane wrote:
> > Jeff Williams <jeffw@globaldial.com> writes:
> > > Thanks. Triggers was my first thought, but chapter 35 on Triggers
> > > didn't really indicate a way I could do this easily and scared me with
> > > a lot of c code.

> >
> > Yeah. This is a documentation issue that's bothered me for awhile.
> > The problem is that we treat the PL languages as add-ons and therefore
> > the documentation of the "core" system shouldn't rely on them ... but
> > that leaves us presenting C-code triggers as the only examples in
> > chapter 35. There is a paragraph in there suggesting you go look at
> > the PL languages first, but obviously it's not getting the job done.

>
> Chapter 35 is plpgsql.. do you mean chapter 32.4?
>
> > Anybody have a better idea?

>
> What about a See Also section ala man pages that links to trigger info
> for other languages?


--
Mike Diehl,
Network Monitoring Tool Devl.
SAIC at Sandia National Laboratories.
(505) 284-3137

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:17 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com