Re: example of insert timestamp

Re: example of insert timestamp

am 26.02.2008 02:19:56 von Gregory Williamson

This is a multi-part message in MIME format.

------_=_NextPart_001_01C87816.5DDCD56D
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

David --

>=20
> I want to have a table with a timestamp field that automatically gets =
the=20
> value of now() on insert. The timestamp will never be updated. I =
assume I=20
> need to create a trigger to do this. If there is an easier way, please =

> advise.

Perhaps, make the column so it has a default value of now, land then =
insert without specifying the timestamp column so it gets the default =
value:

create table foo (q_i_time timestamp with time zone not null default =
now(), someval int);

CREATE TABLE
billing=3D# insert into foo (someval) values (22);
INSERT 0 1
billing=3D# insert into foo (someval) values (26);
INSERT 0 1
billing=3D# insert into foo (someval) values (1);
INSERT 0 1
billing=3D# select * from foo;
q_i_time | someval
-------------------------------+---------
2008-02-25 17:23:03.247619-08 | 22
2008-02-25 17:23:07.43922-08 | 26
2008-02-25 17:23:10.111189-08 | 1
(3 rows)

HTH,

Greg Williamson
Senior DBA
GlobeXplorer LLC, a DigitalGlobe company

Confidentiality Notice: This e-mail message, including any attachments, =
is for the sole use of the intended recipient(s) and may contain =
confidential and privileged information and must be protected in =
accordance with those provisions. Any unauthorized review, use, =
disclosure or distribution is prohibited. If you are not the intended =
recipient, please contact the sender by reply e-mail and destroy all =
copies of the original message.

(My corporate masters made me say this.)

------_=_NextPart_001_01C87816.5DDCD56D
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable




charset=3Diso-8859-1">
6.5.7652.24">
RE: [ADMIN] example of insert timestamp




David --



>

> I want to have a table with a timestamp field that automatically =
gets the

> value of now() on insert. The timestamp will never be updated. I =
assume I

> need to create a trigger to do this. If there is an easier way, =
please

> advise.



Perhaps, make the column so it has a default value of now, land then =
insert without specifying the timestamp column so it gets the default =
value:



create table foo (q_i_time timestamp with time zone not null default =
now(), someval int);



CREATE TABLE

billing=3D# insert into foo (someval) values (22);

INSERT 0 1

billing=3D# insert into foo (someval) values (26);

INSERT 0 1

billing=3D# insert into foo (someval) values (1);

INSERT 0 1

billing=3D# select * from foo;

           =
q_i_time           =
; | someval

-------------------------------+---------

 2008-02-25 17:23:03.247619-08 |      =
22

 2008-02-25 17:23:07.43922-08  |      =
26

 2008-02-25 17:23:10.111189-08 =
|       1

(3 rows)



HTH,



Greg Williamson

Senior DBA

GlobeXplorer LLC, a DigitalGlobe company



Confidentiality Notice: This e-mail message, including any attachments, =
is for the sole use of the intended recipient(s) and may contain =
confidential and privileged information and must be protected in =
accordance with those provisions. Any unauthorized review, use, =
disclosure or distribution is prohibited. If you are not the intended =
recipient, please contact the sender by reply e-mail and destroy all =
copies of the original message.



(My corporate masters made me say this.)






------_=_NextPart_001_01C87816.5DDCD56D--

example of insert timestamp

am 26.02.2008 02:21:19 von David Bear

I've been reading about triggers on insert and found the page at
http://www.postgresql.org/docs/8.1/interactive/triggers.html with some sample
code in the comments.

I'm thinking what I want can't really be this involved.

I want to have a table with a timestamp field that automatically gets the
value of now() on insert. The timestamp will never be updated. I assume I
need to create a trigger to do this. If there is an easier way, please
advise.

Otherwise, is there any sample code that would should be how to do this? The
sample on the page above looks like overkill.

--

David Bear
College of Public Programs/ASU
411 N Central, Phoenix, AZ 85004

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

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

Re: example of insert timestamp

am 26.02.2008 02:21:21 von Aaron Bono

------=_Part_3429_24598798.1203988881103
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On Mon, Feb 25, 2008 at 7:21 PM, David Bear wrote:

> I've been reading about triggers on insert and found the page at
> http://www.postgresql.org/docs/8.1/interactive/triggers.html with some
> sample
> code in the comments.
>
> I'm thinking what I want can't really be this involved.
>
> I want to have a table with a timestamp field that automatically gets the
> value of now() on insert. The timestamp will never be updated. I assume I
> need to create a trigger to do this. If there is an easier way, please
> advise.
>
> Otherwise, is there any sample code that would should be how to do this?
> The
> sample on the page above looks like overkill.
>

In your create table statement:

"create_dt" TIMESTAMP WITHOUT TIME ZONE DEFAULT now() NOT NULL

--
============================================================ ======
Aaron Bono
Aranya Software Technologies, Inc.
http://www.aranya.com
http://codeelixir.com
============================================================ ======

------=_Part_3429_24598798.1203988881103
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On Mon, Feb 25, 2008 at 7:21 PM, David Bear <> wrote:


I've been reading about triggers on insert and found the page at

with some sample

code in the comments.



I'm thinking what I want can't really be this involved.



I want to have a table with a timestamp field that automatically gets the

value of now() on insert. The timestamp will never be updated. I assume I

need to create a trigger to do this. If there is an easier way, please

advise.



Otherwise, is there any sample code that would should be how to do this? The

sample on the page above looks like overkill.


In your create table statement:

"create_dt" TIMESTAMP WITHOUT TIME ZONE DEFAULT now() NOT NULL

--
============================================================ ======

  Aaron Bono
  Aranya Software Technologies, Inc.
 
============================================================ ======

------=_Part_3429_24598798.1203988881103--

Re: example of insert timestamp

am 26.02.2008 02:26:29 von Steve Holdoway

--Signature=_Tue__26_Feb_2008_14_26_29_+1300_5fENYs2vAXYvm1o p
Content-Type: text/plain; charset=US-ASCII
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Mon, 25 Feb 2008 18:21:19 -0700
David Bear wrote:

> I've been reading about triggers on insert and found the page at=20
> http://www.postgresql.org/docs/8.1/interactive/triggers.html with some sa=
mple=20
> code in the comments.
>=20
> I'm thinking what I want can't really be this involved.
>=20
> I want to have a table with a timestamp field that automatically gets the=
=20
> value of now() on insert. The timestamp will never be updated. I assume I=
=20
> need to create a trigger to do this. If there is an easier way, please=20
> advise.
>=20
> Otherwise, is there any sample code that would should be how to do this? =
The=20
> sample on the page above looks like overkill.
>=20
> --=20
>=20
> David Bear
> College of Public Programs/ASU
> 411 N Central, Phoenix, AZ 85004
>=20
> ---------------------------(end of broadcast)---------------------------
> TIP 3: Have you checked our extensive FAQ?
>=20
> http://www.postgresql.org/docs/faq
Here's a quick example which sets column dateadded to now on insert... can'=
t get much simpler!


create or replace function surbl.AddUrl()
returns TRIGGER AS
$$
BEGIN
-- Must have an url to add
IF ( NEW.url IS NULL )
THEN
RETURN NULL;
END IF;
NEW.dateadded :=3D now();
RETURN NEW;
END;
$$ language 'plpgsql';

drop trigger addurl_trig ON surbl.rawdata;
create trigger addurl_trig BEFORE INSERT ON surbl.rawdata
FOR EACH ROW
EXECUTE PROCEDURE surbl.AddUrl();

--Signature=_Tue__26_Feb_2008_14_26_29_+1300_5fENYs2vAXYvm1o p
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFHw2rFDN07rEXM5z0RArMiAJ0fgD9hXLMucEl93XfoeWOdk1iNzACf TaU7
k62nImJfm1g8mBbBjkI6Ing=
=whVK
-----END PGP SIGNATURE-----

--Signature=_Tue__26_Feb_2008_14_26_29_+1300_5fENYs2vAXYvm1o p--

Re: example of insert timestamp

am 26.02.2008 02:46:37 von David Bear

On Monday 25 February 2008 18:21, Aaron Bono wrote:
> On Mon, Feb 25, 2008 at 7:21 PM, David Bear wrote:
> > I've been reading about triggers on insert and found the page at
> > http://www.postgresql.org/docs/8.1/interactive/triggers.html with some
> > sample
> > code in the comments.
> >
> > I'm thinking what I want can't really be this involved.
> >
> > I want to have a table with a timestamp field that automatically gets the
> > value of now() on insert. The timestamp will never be updated. I assume I
> > need to create a trigger to do this. If there is an easier way, please
> > advise.
> >
> > Otherwise, is there any sample code that would should be how to do this?
> > The
> > sample on the page above looks like
> > overkill.
>
> In your create table statement:
>
> "create_dt" TIMESTAMP WITHOUT TIME ZONE DEFAULT now() NOT NULL

Thanks. I knew it must be easier. This is exactly what I want.

--

David Bear
College of Public Programs/ASU
411 N Central, Phoenix, AZ 85004

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Re: example of insert timestamp

am 26.02.2008 03:05:09 von Jamie A Lawrence

On Feb 25, 2008, at 8:19 PM, Gregory Williamson wrote:
>
> Perhaps, make the column so it has a default value of now, land then
> insert without specifying the timestamp column so it gets the
> default value:
>
> create table foo (q_i_time timestamp with time zone not null default
> now(), someval int);
>

This is what I do. For many tables, I also add a mod_time column with
an ON UPDATE trigger similar to the one previously posted. Even when
not needed for the table itself, in development, it can be very handy
to compare dodgy inserts/updates with commit logs when bug hunting...

-j

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

Re: example of insert timestamp

am 26.02.2008 03:41:42 von Aaron Bono

------=_Part_3695_11891604.1203993702158
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On Mon, Feb 25, 2008 at 8:05 PM, Jamie A Lawrence wrote:

>
> On Feb 25, 2008, at 8:19 PM, Gregory Williamson wrote:
> >
> > Perhaps, make the column so it has a default value of now, land then
> > insert without specifying the timestamp column so it gets the
> > default value:
> >
> > create table foo (q_i_time timestamp with time zone not null default
> > now(), someval int);
> >
>
> This is what I do. For many tables, I also add a mod_time column with
> an ON UPDATE trigger similar to the one previously posted. Even when
> not needed for the table itself, in development, it can be very handy
> to compare dodgy inserts/updates with commit logs when bug hunting...
>
>
>
I know you asked for something more like a create timestamp, but if you also
want a modify timestamp, that would definitely need a trigger:

CREATE OR REPLACE FUNCTION "public"."modify_date_stamp_fn" () RETURNS SETOF
opaque AS
'
BEGIN
-- if a trigger insert or update operation occurs
IF TG_OP = ''INSERT'' OR TG_OP = ''UPDATE'' THEN
-- assigns the current timestamp
-- into the modify date and time column
NEW.modify_dt := now();

-- displays the new row on an insert/update
RETURN NEW;
END IF;
END;
'
LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;

CREATE TRIGGER "mytable_modify_dt_tr" BEFORE UPDATE
ON "public"."mytable" FOR EACH ROW
EXECUTE PROCEDURE "public"."modify_date_stamp_fn"();

This function is very generic and can be used for any and all of your tables
so you don't have to create a function for each table as long as the column
name is the same.

-Aaron

--
============================================================ ======
Aaron Bono
Aranya Software Technologies, Inc.
http://www.aranya.com
http://codeelixir.com
============================================================ ======

------=_Part_3695_11891604.1203993702158
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On Mon, Feb 25, 2008 at 8:05 PM, Jamie A Lawrence <> wrote:




On Feb 25, 2008, at 8:19 PM, Gregory Williamson wrote:

>

> Perhaps, make the column so it has a default value of now, land then

> insert without specifying the timestamp column so it gets the

> default value:

>

> create table foo (q_i_time timestamp with time zone not null default

> now(), someval int);

>



This is what I do. For many tables, I also add a mod_time column with

an ON UPDATE trigger similar to the one previously posted. Even when

not needed for the table itself, in development, it can be very handy

to compare dodgy inserts/updates with commit logs when bug hunting...





I know you asked for something more like a create timestamp, but if you also want a modify timestamp, that would definitely need a trigger:

CREATE OR REPLACE FUNCTION "public"."modify_date_stamp_fn" () RETURNS SETOF opaque AS

'
BEGIN
    -- if a trigger insert or update operation occurs
    IF TG_OP = ''INSERT'' OR TG_OP = ''UPDATE'' THEN
        -- assigns the current timestamp
        -- into the modify date and time column

        NEW.modify_dt := now();
       
        -- displays the new row on an insert/update
        RETURN NEW;
    END IF;
END;
'
LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;


CREATE TRIGGER "mytable_modify_dt_tr" BEFORE UPDATE
    ON "public"."mytable" FOR EACH ROW
    EXECUTE PROCEDURE "public"."modify_date_stamp_fn"();

This function is very generic and can be used for any and all of your tables so you don't have to create a function for each table as long as the column name is the same.


-Aaron

--
============================================================ ======
  Aaron Bono
  Aranya Software Technologies, Inc.
 

 
============================================================ ======

------=_Part_3695_11891604.1203993702158--