473,320 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

simple auto-updating timestamp ?

Hello list,

I suspect, this is a common issue for newbies.
Is there a simple way to have an auto-updating timestamp like mysql has ?

create table something (
id int4,
sometext text,
update_ts timestamp(0),
primary key (id)
);

Everytime this table gets updated the timestamp should be automatically
refreshed to NOW() ?
I hope someone could point me to an example.
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 12 '05 #1
3 9462
Andreas wrote:
I suspect, this is a common issue for newbies.
Is there a simple way to have an auto-updating timestamp like mysql has ?

create table something (
id int4,
sometext text,
update_ts timestamp(0),
primary key (id)
);

Everytime this table gets updated the timestamp should be
automatically refreshed to NOW() ?
I hope someone could point me to an example.


You can do this by adding a trigger to your table. Just define the trigger
to be invoked on INSERT and UPDATE for your table. The trigger definition
would look something like this:

CREATE TRIGGER "trg_set_update_ts" BEFORE INSERT OR UPDATE
ON "public.something" FOR EACH ROW
EXECUTE PROCEDURE "public"."set_update_ts"();

Then, your function in PL/PGSQL that sets the update_ts to NOW() would look
something like this:

CREATE FUNCTION "public"."set_update_ts" () RETURNS trigger AS'
BEGIN
NEW.update_ts = NOW();
RETURN NEW;
END;
'LANGUAGE 'plpgsql' IMMUTABLE CALLED ON NULL INPUT SECURITY INVOKER;

Of course, this function would end up setting the update_ts to NOW() every
time you insert or update your table. And you could never set the value
to anything other than NOW() because your trigger would catch it and set it
back to NOW() again. If that's not exact, it'll at least point you in
the right direction.

Dante

----------
D. Dante Lorenso
da***@lorenso.com



---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #2
D. Dante Lorenso wrote:
You can do this by adding a trigger to your table. Just define the
trigger
to be invoked on INSERT and UPDATE for your table. The trigger
definition
would look something like this: [...]
Thanks.
So far that works for one table.

Can I have this behaviour somehow inherited by child-tables ?
Like:
CREATE TABLE objects (
id integer primary key,
created_ts timestamp(0) DEFAULT LOCALTIMESTAMP,
update_ts timestamp(0),
deleted_ts timestamp(0), -- things get ignored in normal
processing
....
);

Then create a trigger as in your example that updates this timestamp.

Every other table in the db would inherit (objects) to get those
standard fields that I'd like to have everywhere. It'd be nice not
having to bother about the "methods" of the objects-class for every
child-class.
CREATE FUNCTION "public"."set_update_ts" () RETURNS trigger AS'
BEGIN
NEW.update_ts = NOW();
RETURN NEW;
END; 'LANGUAGE 'plpgsql' IMMUTABLE CALLED ON NULL INPUT SECURITY
INVOKER;
I entered your code into psql and checked it afterwards with pgadmin3.
pgadmin shows some parts different to the code that I pushed through psql :
1) create OR REPLACE ...
2) immuntable; <-- End of line What does this part behind
"immutable" do ?
Of course, this function would end up setting the update_ts to NOW()
every
time you insert or update your table. And you could never set the value
to anything other than NOW() because your trigger would catch it and
set it
back to NOW() again.


That is what I had in mind. I'd like to see when there was the last
update to a record.
.... Andreas

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 12 '05 #3
Andreas wrote:
D. Dante Lorenso wrote:
You can do this by adding a trigger to your table. Just define the
trigger
to be invoked on INSERT and UPDATE for your table. The trigger
definition
would look something like this: [...]

Thanks.
So far that works for one table.

Can I have this behaviour somehow inherited by child-tables ?
Like:
CREATE TABLE objects (
id integer primary key,
created_ts timestamp(0) DEFAULT LOCALTIMESTAMP,
update_ts timestamp(0),
deleted_ts timestamp(0), -- things get ignored in normal
processing
...
);

Then create a trigger as in your example that updates this timestamp.
Every other table in the db would inherit (objects) to get those
standard fields that I'd like to have everywhere. It'd be nice not
having to bother about the "methods" of the objects-class for every
child-class.


Yeah I know what you mean. Someone jump in here and correct me if I'm
wrong,
but I don't believe that triggers are inherited in PG. Of course, you
already
have the 'set_update_ts' function defined, so you would only have to declare
the trigger for every child table (not the function).

Verify that this is true. Last time I checked i think that's how it worked.
CREATE FUNCTION "public"."set_update_ts" () RETURNS trigger AS'
BEGIN
NEW.update_ts = NOW();
RETURN NEW;
END; 'LANGUAGE 'plpgsql' IMMUTABLE CALLED ON NULL INPUT SECURITY
INVOKER;

I entered your code into psql and checked it afterwards with pgadmin3.
pgadmin shows some parts different to the code that I pushed through
psql :
1) create OR REPLACE ...
2) immuntable; <-- End of line What does this part behind
"immutable" do ?


You probably want to remove the 'IMMUTABLE CALLED ON NULL INPUT SECURITY
INVOKER'.
That was my cut-and-paste error. I meant to strip that off for you.
Here's the
page that explains what all those do, though:

http://www.postgresql.org/docs/7.4/s...efunction.html

Dante

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Silky | last post by:
Iam trying to get the green footer div to sit on the bottom of all three divs so as the divs vary in height it sits off the bottom of the lowest one I cannot use absolutes because the three...
2
by: MK | last post by:
Hi, I don't have much experience with Javascript and have a simple question. Suppose one wants to write some text in the web page which needs to be center aligned, then how to do it? The...
6
by: Alpha | last post by:
I retrieve a table with only 2 columns. One is a auto-generated primary key column and the 2nd is a string. When I add a new row to the dataset to be updated back to the database. What should I...
3
by: yuelinniao | last post by:
hi, I have got a simple way to make "textarea" support "auto-submit" when pressing Ctrl+Enter, and tested under both IE and Firefox. The common old method is like this: <form name=form2>...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
3
by: hzgt9b | last post by:
Using CSS I want to center a DIV on the page and always keep it 3px from the top. Seems simple but I cannot find a cross-browser solution to this problem. I can get to work in IE or FF but not both...
2
by: samsquared | last post by:
I have an xml file that looks like this: <DocBuild name="ABC"> <BldArea name="Components" product="ABC" area="C"> <BldItem name="Item1" module="mod1" tier="A" buildtype="auto"/> ...
4
by: Arun | last post by:
I came across a message sometime back in 2002. Here's a link:...
10
by: Anze | last post by:
Hmmm... anyone? :) Anze Anze wrote:
1
by: jeddiki | last post by:
Hello, I have made a nice opt-in form and tested it in Moz FF and it looks fine. But in IE the elements don't line up properly. I think I am nearly there but can not get these elements...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.