473,899 Members | 3,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9513
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.somethi ng" FOR EACH ROW
EXECUTE PROCEDURE "public"."set_u pdate_ts"();

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

CREATE FUNCTION "public"."set_u pdate_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.c om



---------------------------(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_u pdate_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*******@postg resql.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_u pdate_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 YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 12 '05 #4

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

Similar topics

1
1789
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 colum are dynamically driven maybe Iam missing something really simple? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://wwww3.org/TR/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
2
1686
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 command for writing some text is as follows if I am correct: document.write('This is some text');
6
5079
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 do with the 1st column ? (Below I have a "1" in place for now). Also, Does the datase.AcceptChanges(); updates the changes to the database? Which command do I use to update the changes in dataset back to the Access database table? Thanks, Alpha...
3
2990
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> <textarea onkeydown='if(event.keyCode==13 && event.ctrlKey) return document.form2.submit()'> </textarea> </form>
24
6358
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 have on his site, a Javascript Calculator for working out the cost of what they want, for example: 1 widget and 2 widglets = £5.00
3
2114
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 w/o relaxing the 3px from the top constraint. Can someone offer a solution to this problem?
2
2633
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"/> <BldItem name="Item2" module="mod2" tier="B" buildtype="auto"/> </BldArea> <BldArea name="Core" product="ABC" area="C"> <BldItem name="Item5" module="mod5" tier="A" buildtype="auto"/> <BldItem name="Item6" ...
4
1989
by: Arun | last post by:
I came across a message sometime back in 2002. Here's a link: http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/f10e3748f6f260c5/b0b1ac5840e445e5?lnk=gst&q=color+picker#b0b1ac5840e445e5. The author tries to build a colour picker from scratch but his overuse of <tdtags puts me off the solution. Here is something I just created. It uses prototype, just because it makes life easier for me. .... <head> <script...
10
1783
by: Anze | last post by:
Hmmm... anyone? :) Anze Anze wrote:
1
2634
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 straight - can you see what I have missed ?
0
9997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10863
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10971
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8039
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7201
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5887
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4720
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3317
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.