ERROR: record "old" is not assigned yet
<p>I'm having difficulties in this simple trigger. My purpose is to verify before inserting a new register if is there's a register with the same field content which is "tag_id". If NEW tag_id is the same tag_id of any register on my table "coordenadas", then it updates, if not, it inserts a new one. When I try to insert sth, I get the error: </p>

<pre><code>ERROR:  record "old" is not assigned yet
DETAIL:  The tuple structure of a not-yet-assigned record is indeterminate.
CONTEXT:  PL/pgSQL function verifica_coo() line 7 at IF
</code></pre>

<p>I have this table:</p>

<pre><code>    CREATE TABLE public.coordenadas
    (
      id bigint NOT NULL,
      pos_data timestamp without time zone,
      pos_latitude double precision,
      pos_longitude double precision,
      tag_id bigint NOT NULL,
      gado_id bigint NOT NULL,
      CONSTRAINT coordenadas_pkey PRIMARY KEY (id),
      CONSTRAINT coordenadas_gado_id_fkey FOREIGN KEY (gado_id)
      REFERENCES public.gado (gado_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
      CONSTRAINT fkj14dwmpa6g037ardymqc2q4lj FOREIGN KEY (tag_id)
      REFERENCES public.tag (tag_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
      CONSTRAINT fktawrw6tlliq4ace5p7io87c5p FOREIGN KEY (gado_id)
      REFERENCES public.gado (gado_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
</code></pre>

<p>This trigger:</p>

<pre><code>CREATE TRIGGER verifica_coo
BEFORE INSERT OR UPDATE ON coordenadas
   FOR EACH ROW EXECUTE PROCEDURE verifica_coo();
</code></pre>

<p>This function:</p>

<pre><code>CREATE OR REPLACE FUNCTION public.verifica_coo()
  RETURNS trigger AS $verifica_coo$

    BEGIN
        --
        -- Verifica se é para inserir ou atualizar os dados na tabela.
        --
       IF (NEW.tag_id != OLD.tag_id ) THEN

            INSERT INTO coordenadas (pos_data,pos_latitude,pos_longitude,tag_id,gado_id) 
            VALUES (NEW.pos_data,NEW.pos_latitude,NEW.pos_longitude,NEW.tag_id,NEW.gado_id);
        ELSE 
            UPDATE coordenadas SET pos_data = NEW.pos_data, pos_latitude = NEW.pos_latitude, pos_longitude = NEW.pos_longitude WHERE tag_id = NEW.tag_id;
        END IF;
            RETURN NEW;

    END;
$verifica_coo$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION public.verifica_coo()
  OWNER TO postgres;
</code></pre>

<p>My insert:</p>

<pre><code>INSERT INTO coordenadas (pos_data,pos_latitude,pos_longitude,tag_id,gado_id) VALUES ('21/08/2016', '-23.563844' ,'-46.322525', '2','2');
</code></pre>