handing created and updated fields

Ask a Question related to PostgreSQL / PGSQL, Design and Development.

  1. #1

    Default handing created and updated fields

    I think I saw something posted about this recently, but I can't find it
    in the archives now. :(

    I want to have created and updated fields in a table that are kept
    up-to-date by the database and can't be changed accidentally. I think
    this can be done with rules, but I'm not sure of the best way to do it.
    Basically:

    ON INSERT: force created and updated to be current_timestamp
    ON UPDATE: deny updated created. force updated to be set to
    current_timestamp

    I first thought of doing an ON INSERT INSTEAD rule that would ignore
    NEW.created and NEW.updated, but it seems inconvenient to have to change
    the rule every time the table definition, and I'm not sure if this would
    properly handle the SERIAL that I have defined (the rule would need to
    include the serial in the insert, but then would the default work?). So
    now I'm thinking of doing an ON INSERT INSTEAD UPDATE SET created =
    current_timestamp WHERE id = NEW.id, though again I'm not sure if the
    serial field (id) would be handled properly.

    Does anyone have an example of the best way to handle this scenario?
    --
    Jim C. Nasby, Database Consultant [email]decibel@decibel.org[/email]
    Give your computer some brain candy! [url]www.distributed.net[/url] Team #1828

    Windows: "Where do you want to go today?"
    Linux: "Where do you want to go tomorrow?"
    FreeBSD: "Are you guys coming, or what?"

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

    Jim C. Nasby Guest

  2. Similar Questions and Discussions

    1. Managing ViewState of a dynamically created Custom Composite Server Control -(where the original is also dynamically created)
      Ok here's my scenario. I have a Custom Composite Server Control (CCSC) consisting of a TextBox, Button & Panel. (And some other code - which I...
    2. Question on handing over original files
      A question for you graphic designers out there. After you've completed a project for a client and you give them the finished product, do you also...
    3. #26351 [NEW]: Incorrect handling of Null Fields/Numerical Fields with '0'
      From: jabberwocky at ibplanet dot com Operating system: Any PHP version: 4.3.4 PHP Bug Type: MSSQL related Bug description: ...
    4. Datagrid not updated during delete, but updated during insert and update
      Hello everyone. A test webform here, single datagrid bound to one table through dataset, and controls to delete, update and insert data. The code...
    5. Imported + updated symbols don't stay updated
      Hi everybody. I apologize for posting a question that probably any newbie should know, but ... I'm a rank Fireworks newbie. My problem is that the...
  3. #2

    Default Re: handing created and updated fields

    On Mon, Jan 10, 2005 at 05:28:47AM -0600, Jim C. Nasby wrote:
    > I think I saw something posted about this recently, but I can't find it
    > in the archives now. :(
    >
    > I want to have created and updated fields in a table that are kept
    > up-to-date by the database and can't be changed accidentally. I think
    > this can be done with rules, but I'm not sure of the best way to do it.
    > Basically:
    >
    > ON INSERT: force created and updated to be current_timestamp
    > ON UPDATE: deny updated created. force updated to be set to
    > current_timestamp
    Nope, you want triggers. I don't remember the syntax, but the basic
    structure would be...

    ON INSERT DO TRIGGER set_timestamp
    ON UPDATE DO TRIGGER update_timestamp

    set_timestamp()
    NEW.created = now()
    NEW.updated = now()

    update_timestamp()
    if OLD.created <> NEW.created then ERROR
    NEW.updated = now()

    Hope this helps,
    --
    Martijn van Oosterhout <kleptog@svana.org> [url]http://svana.org/kleptog/[/url]
    > Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
    > tool for doing 5% of the work and then sitting around waiting for someone
    > else to do the other 95% so you can sue them.
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.0.6 (GNU/Linux)
    Comment: For info see [url]http://www.gnupg.org[/url]

    iD8DBQFB4nHfY5Twig3Ge+YRAmJpAJ9RtKCJITYfBbjGQQi7/9apa++1dQCdHfL4
    PpzXHxGXupsLyr9yHw9jt9U=
    =Bc/Z
    -----END PGP SIGNATURE-----

    Martijn van Oosterhout Guest

  4. #3

    Default Re: handing created and updated fields

    Hi,

    Citing "Jim C. Nasby" <decibel@decibel.org>:
    > ON INSERT: force created and updated to be current_timestamp
    > ON UPDATE: deny updated created. force updated to be set to
    > current_timestamp
    [snip]
    > Does anyone have an example of the best way to handle this scenario?
    Something along the lines of the following should work (but test first
    anyways, though I have copied smaller parts of this from the definitions
    in one of my databases here, I have made modifications to fit your
    specific task, so typos/errors might have sneaked in):

    create function update_trigger() returns trigger as
    'begin
    new.created := old.created;
    new.updated := CURRENT_TIMESTAMP;
    return new;
    end;'
    language 'plpgsql';

    create trigger update_trigger BEFORE UPDATE ON your_table_name
    FOR EACH ROW EXECUTE PROCEDURE update_trigger();

    create function insert_trigger() returns trigger as
    'begin
    new.created := CURRENT_TIMESTAMP;
    new.updated := CURRENT_TIMESTAMP;
    return new;
    end;'
    language 'plpgsql';

    create trigger insert_trigger BEFORE INSERT ON your_table_name
    FOR EACH ROW EXECUTE PROCEDURE insert_trigger();

    HTH,
    Regards,
    Daniel

    ---------------------------(end of broadcast)---------------------------
    TIP 9: the planner will ignore your desire to choose an index scan if your
    joining column's datatypes do not match

    Daniel Martini Guest

  5. #4

    Default Re: handing created and updated fields

    On Mon, 2005-01-10 at 15:45 +0100, Daniel Martini wrote:
    > Hi,
    >
    > Citing "Jim C. Nasby" <decibel@decibel.org>:
    > > ON INSERT: force created and updated to be current_timestamp
    > > ON UPDATE: deny updated created. force updated to be set to
    > > current_timestamp
    > [snip]
    > > Does anyone have an example of the best way to handle this scenario?
    >
    > Something along the lines of the following should work (but test first
    > anyways, though I have copied smaller parts of this from the definitions
    > in one of my databases here, I have made modifications to fit your
    > specific task, so typos/errors might have sneaked in):
    >
    > create function update_trigger() returns trigger as
    > 'begin
    > new.created := old.created;
    > new.updated := CURRENT_TIMESTAMP;
    > return new;
    > end;'
    > language 'plpgsql';
    >
    > create trigger update_trigger BEFORE UPDATE ON your_table_name
    > FOR EACH ROW EXECUTE PROCEDURE update_trigger();
    >
    > create function insert_trigger() returns trigger as
    > 'begin
    > new.created := CURRENT_TIMESTAMP;
    > new.updated := CURRENT_TIMESTAMP;
    > return new;
    > end;'
    > language 'plpgsql';
    >
    > create trigger insert_trigger BEFORE INSERT ON your_table_name
    > FOR EACH ROW EXECUTE PROCEDURE insert_trigger();
    >
    > HTH,
    > Regards,
    > Daniel
    These could also be combined into one trigger since they are nearly
    identical anyway:

    CREATE FUNCTION combined_trigger() RETURNS TRIGGER AS '
    BEGIN
    NEW.update := CURRENT_TIMESTAMP;
    IF TG_OP = ''INSERT'' THEN
    NEW.created := CURRENT_TIMESTAMP;
    ELSE
    NEW.created := OLD.created;
    END IF;
    RETURN NEW;
    END;
    ' LANGUAGE plpgsql;

    CREATE TRIGGER combined_trigger BEFORE INSERT OR UPDATE on
    your_table_name FOR EACH ROW EXECUTE PROCEDURE combined_trigger();

    Sven


    ---------------------------(end of broadcast)---------------------------
    TIP 1: subscribe and unsubscribe commands go to [email]majordomo@postgresql.org[/email]

    Sven Willenberger Guest

  6. #5

    Default Re: handing created and updated fields

    On Mon, Jan 10, 2005 at 11:16:03AM -0500, Sven Willenberger wrote:
    > These could also be combined into one trigger since they are nearly
    > identical anyway:
    >
    > CREATE FUNCTION combined_trigger() RETURNS TRIGGER AS '
    > BEGIN
    > NEW.update := CURRENT_TIMESTAMP;
    > IF TG_OP = ''INSERT'' THEN
    > NEW.created := CURRENT_TIMESTAMP;
    > ELSE
    > NEW.created := OLD.created;
    > END IF;
    > RETURN NEW;
    > END;
    > ' LANGUAGE plpgsql;
    Excellent; any idea which would perform better (combined v. separate
    trigger function)?
    --
    Jim C. Nasby, Database Consultant [email]decibel@decibel.org[/email]
    Give your computer some brain candy! [url]www.distributed.net[/url] Team #1828

    Windows: "Where do you want to go today?"
    Linux: "Where do you want to go tomorrow?"
    FreeBSD: "Are you guys coming, or what?"

    ---------------------------(end of broadcast)---------------------------
    TIP 7: don't forget to increase your free space map settings

    Jim C. Nasby Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139