title: Using the now() function and executing triggers
body: <p>I am trying to create a trigger function in PostgreSQL that should check records with the same <code>id</code> (i.e. comparison by <code>id</code> with existing records)  before inserting or updating the records. If the function finds records that have the same <code>id</code>, then that entry is set to be the <code>time_dead</code>. Let me explain with this example:</p>
  
  <pre><code>INSERT INTO persons (id, time_create, time_dead, name)
  VALUES (1, 'now();', ' ', 'james');
  </code></pre>
  
  <p>I want to have a table like this:</p>
  
  <pre><code> id  time_create  time-dead  name
   1   06:12                   henry   
   2   07:12                   muka
  </code></pre>
  
  <p><code>id 1</code> had a <code>time_create 06.12</code> but the <code>time_dead</code> was <code>NULL</code>. This is the same as <code>id 2</code> but next time I try to run the <code>insert</code> query with same id but different names I should get a table like this:</p>
  
  <pre><code> id  time_create  time-dead  name
   1   06:12        14:35      henry   
   2   07:12                   muka
   1   14:35                   waks
  </code></pre>
  
  <p>henry and waks share the same <code>id</code> <code>1</code>. After running an <code>insert</code> query henry's <code>time_dead</code> is equal to waks' <code>time_create</code>. If another entry was to made with id 1, lets say for james, the time entry for james will be equal to the <code>time_dead</code> for waks. And so on.</p>
  
  <p>So far my function looks like this. But it's not working:</p>
  
  <pre><code>CREATE FUNCTION tr_function() RETURNS trigger AS '
  BEGIN
    IF tg_op = ''UPDATE'' THEN
       UPDATE persons
       SET time_dead = NEW.time_create
       Where
           id = NEW.id
           AND time_dead IS NULL
           ;
  
    END IF;
    RETURN new;
  END
  ' LANGUAGE plpgsql;
  
  CREATE TRIGGER sofgr BEFORE INSERT OR UPDATE
          ON persons FOR each ROW
          EXECUTE PROCEDURE tr_function();
  </code></pre>
  
  <p>When I run this its say <code>time_dead</code> is not supposed to be <code>null</code>. Is there a way I can write a <code>trigger function</code> that will automatically enter the time upon <code>inserting or updating</code> but give me results like the above tables when I run a <code>select</code> query?</p>
  
  <p>What am I doing wrong?</p>
  
  <p>My two tables:</p>
  
  <pre><code>CREATE TABLE temporary_object
  (
    id integer NOT NULL,
    time_create timestamp without time zone NOT NULL,
    time_dead timestamp without time zone,
    PRIMARY KEY (id, time_create)
  );
  
  CREATE TABLE persons
  (
    name text
  )
  INHERITS (temporary_object);
  </code></pre>