title: PostgreSQL UPSERT Trigger returning id
body: <p>I've an <code>UPSERT</code> trigger on a table that may update instead of inserting while doing insert operation. I've function doing insert on that table and <code>returning id</code> However it doesn't return an id when it updates instead of inserting. I want to get the id in both cases.</p>
  
  <p>Trigger Code</p>
  
  <pre><code>perform 1 from tera_subject 
  where id = new.subject_id and owner = new.user_id;
  
  if found then
     return null;
  else
     select id into vote_id from tera_votes where 
    user_id = new.user_id and
    subject_id = new.subject_id;
     if not found then
    return new;
     else
        -- raise notice 'vote_id: % ; vote: %',vote_id,new.vote;
        if(tg_op = 'INSERT') then
            begin
            -- raise notice 'redirecting to update';
            update tera_votes
                set vote=new.vote
                WHERE id = vote_id;
            end;
        elsif(tg_op = 'UPDATE') then
            -- raise notice 'in update';
            return new;
        end if;
        -- raise notice 'end of trigger %',tg_op;
        return null;
     end if;
  end if;
  end;
  </code></pre>