simple auto-updating timestamp ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andreas

    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

  • D. Dante Lorenso

    #2
    Re: simple auto-updating timestamp ?

    Andreas wrote:
    [color=blue]
    > 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.[/color]

    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
    dante@lorenso.c om







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

    Comment

    • Andreas

      #3
      Re: simple auto-updating timestamp ?

      D. Dante Lorenso wrote:
      [color=blue]
      > 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: [...][/color]

      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.
      [color=blue]
      > 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;[/color]

      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 ?
      [color=blue]
      > 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.[/color]

      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 majordomo@postg resql.org

      Comment

      • D. Dante Lorenso

        #4
        Re: simple auto-updating timestamp ?

        Andreas wrote:
        [color=blue]
        > D. Dante Lorenso wrote:
        >[color=green]
        >> 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: [...][/color]
        >
        >
        > 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.[/color]

        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.
        [color=blue][color=green]
        >> 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;[/color]
        >
        >
        > 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 ?[/color]

        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:



        Dante



        ---------------------------(end of broadcast)---------------------------
        TIP 2: you can get off all lists at once with the unregister command
        (send "unregister YourEmailAddres sHere" to majordomo@postg resql.org)

        Comment

        Working...