Re: Dynamic update of a date field

From: "Musial, Jan (GIUB)" <jan(dot)musial(at)giub(dot)unibe(dot)ch>
To: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Dynamic update of a date field
Date: 2012-02-16 16:45:49
Message-ID: 9255BE145568B443A366804DA0BA275F1BF22B59@aai-exch-mbx4.campus.unibe.ch
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Dear Adrian & Andreas,

Thank you very much for this pieces of advice. I end up with creating a insert-triger function, which does the trick.
As far as the redundant time information within my database is concerned this is an data import issue. Simply it is easier for me to import year,month day separately than create a timestamp and drop the columns.
All the best,

Jan
________________________________________
Von: Adrian Klaver [adrian(dot)klaver(at)gmail(dot)com]
Gesendet: Donnerstag, 16. Februar 2012 15:56
An: pgsql-general(at)postgresql(dot)org
Cc: Musial, Jan (GIUB)
Betreff: Re: [GENERAL] Dynamic update of a date field

On Thursday, February 16, 2012 6:05:40 am Musial, Jan (GIUB) wrote:
> Dear all,
>
> I have a question concerning default value/trigger function which supposed
> to update/fill field called time_stamp whenever a row is inserted. Let say
> that we have a table: CREATE TABLE dummy (year smallint,month smallint,day
> smallint,time_stamp date); I would like to update "time_stamp" dynamically
> without knowledge of a table name and using the values placed in the
> columns: year,month,day. The trick is that I have ~2000 tables which I
> populate with some time information, so either I could somehow fetch it
> to_timestamp() function in the Default definition of the field (while
> creating a table) or create a trigger function which doesn't require the
> table name (or retrieve it dynamically) and which is executed whenever a
> row is added to any table.
>
> As I am new to postgres/plpgsql any suggestions are more than welcome.

I would agree with Andreas, there is no need to replicate the date/time
information across fields. You can pull that information out of a timestamp. If
the timestamp field name is going to be the same on all the tables you can create
a generic function in plpgsl and point a trigger on each table to it. The INSERT
value can be handled by a default value on the field. It is the update you will
need a function for. So something like:

CREATE OR REPLACE FUNCTION public.ts_update()
RETURNS trigger AS

$Body$
BEGIN
new.ts_update:=now();
RETURN NEW;
END;
$Body$
LANGUAGE 'plpgsql' VOLATILE;

and associated trigger

CREATE TRIGGER some_table_ts_update
BEFORE UPDATE
ON some_table
FOR EACH ROW
EXECUTE PROCEDURE public.ts_update()

>
> Thank you in advance,
>
> Jan Musial

--
Adrian Klaver
adrian(dot)klaver(at)gmail(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message sodik 2012-02-16 16:53:14 Check if backup is in progress
Previous Message ChoonSoo Park 2012-02-16 15:48:58 How to dereference 2 dimensional array?