title: creating triggers and functions in postgresql
body: <p>how can i create a <code>trigger function</code> before <code>adding/updating</code>,the <code>function</code> should check records that have the same <code>id</code> (i.e comparison by <code>id</code> with existing <code>objects</code> that have the same property as the <code>temporary_object</code>)If a record with the id is found, then that entry is set to the <code>time_dead</code>, and then it adds an entry containing the corresponding values ​​of the attributes found in that record (except those that are set for a new record), when <code>time_dead</code> is empty then <code>time_create</code> of a new time is equal to that time at the current moment . Thus,a new record <code>time_create</code> is like the <code>time_dead's</code> ancestor.
  If a record with that id is found then it is added to the database with the establishment as the <code>time_create</code> of the current time.</p>
  
  <p>for example here is a simple explanation(just for explanation purposes)</p>
  
  <pre><code>id time_create   time-dead  student  amount
  
   1  06.12           07.12    henry   500
  
   1  07.12                    henry   1000
  </code></pre>
  
  <p>so if a student called henry with id 1 entered a room at 06.12  and left at 07.12 the next time he enters another room again time_dead will be equal to time_create(so time_dead of old entry and time_create of new entry - will be equal)
  these are my tables below in sql format</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,
    CONSTRAINT temporary_object_pkey PRIMARY KEY (id, time_create)
  )
  
  CREATE TABLE persons
  (
    fname text,
    fsurname text,
  )
  INHERITS (temporary_object)
  
  
  CREATE TABLE rooms
  (
    roomnum integer,
    course integer,
    passport text,
    students_number text
  )
  INHERITS (temporary_object)
  </code></pre>
  
  <p>this is what i am trying to do but im afraid i do not know how to finish it but im 100% not right  may some help out</p>
  
  <pre><code>CREATE TRIGGER trigger2
   BEFORE INSERT OR UPDATE
   ON persons
   FOR EACH ROW
   EXECUTE PROCEDURE func1();
  </code></pre>
  
  <p>and this is the function</p>
  
  <pre><code> CREATE OR REPLACE FUNCTION func1() RETURNS TRIGGER AS $persons$
   DECLARE
          time_create          integer;
          time_dead     timestamp;
          id       timestamp;
      BEGIN
  
   IF (TG_OP = 'INSERT') THEN
  
              time_create=
  </code></pre>