pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rodrigo De León <rdeleonp@gmail.com>
To: pgsql-sql@postgresql.org
Subject: Re: autoupdating mtime column
Date: Fri, 4 Aug 2006 09:39:07 -0600
Message-ID: <a55915760608040839w1a7caad9vd0aeb4e5e5271905@mail.gmail.com> (raw)
In-Reply-To: <7c33d060608040811s6dcaa3abia137188076a76781@mail.gmail.com>
References: <7c33d060608040516n799af906sf74e1c645a47caff@mail.gmail.com>
	<44D34242.6000901@archonet.com>
	<7c33d060608040811s6dcaa3abia137188076a76781@mail.gmail.com>

On 8/4/06, David Garamond <davidgaramond@gmail.com> wrote:
> On 8/4/06, Richard Huxton <dev@archonet.com> wrote:
> > David Garamond wrote:
> > > Dear all,
> > >
> > > Please see SQL below. I'm already satisfied with everything except I
> > > wish in
> > > #4, mtime got automatically updated to NOW() if not explicitly SET in
> > > UPDATE
> > > statement. Is there a way to make the mtime column behave more like I
> > > wanted? Thanks in advance.
> >
> > Just check for OLD.mtime = NEW.mtime, or am I missing something here?
> >
>
>
> How do I differentiate between:
>
> UPDATE t SET mtime=mtime ...;
>
> in which mtime is specifically set and should not change, and
>
> UPDATE t SET foo=bar ...;
>
> in which mtime is not mentioned and should be updated automagically to
> NOW().

How about:

create or replace function
update_times()
returns trigger as $$
	begin
		if TG_OP='INSERT' then
			new.ctime = coalesce(new.ctime,now());
			new.mtime = coalesce(new.mtime,now());
		elsif TG_OP='UPDATE' then
			new.ctime = old.ctime;
			new.mtime = now();
		end if;
		return new;
	end;
$$ language plpgsql;

Regards,

Rodrigo



view thread (10+ messages)  latest in thread

Message-ID: <a55915760608040839w1a7caad9vd0aeb4e5e5271905@mail.gmail.com>
Permalink:  ../a55915760608040839w1a7caad9vd0aeb4e5e5271905@mail.gmail.com/
Also on:    postgresql.org/message-id/a55915760608040839w1a7caad9vd0aeb4e5e5271905@mail.gmail.com

 · 

reply

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: rdeleonp@gmail.com
  Subject: Re: autoupdating mtime column
  In-Reply-To: <a55915760608040839w1a7caad9vd0aeb4e5e5271905@mail.gmail.com>

* 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