trigger: NEW/OLD-error or nothing happens - Mailing list pgsql-general

From Knut Suebert
Subject trigger: NEW/OLD-error or nothing happens
Date
Msg-id 20010707232342.A9030@cascal.vtb
Whole thread Raw
List pgsql-general
Hello,

I'm trying to anaylze a net-acct-log.

so:

    create table traf ( id serial primary key, time timestamp, proto int2,
    sip inet, sport int4, dip inet, dport int4, size int4, device text,
    minport int4, maxport int4);

minport is to group by the service, it (and maxport for completeness)
should be filled by

    create function nacmin(int4,int4) returns int as'
    begin
        if $1<$2 then
           return $1;
        end if;
        return $2;
    end;
    ' language 'plpgsql';

    create function nacmax(int4,int4) returns int as'
    begin
        if $1>$2 then
           return $1;
        end if;
        return $2;
    end;
    ' language 'plpgsql';

these functions work.

the automatic execution should be done by a function with a stupid name:

    create function nac_viceversa() returns opaque as'
    begin
        NEW.minport := nacmin(OLD.sport,OLD.dport);
        NEW.maxport := nacmax(OLD.sport,OLD.dport);
    return NEW;
    end;
    'language 'plpgsql';

    create trigger nac_update after insert or update on traf
    for each row execute procedure nac_viceversa();

but than

    ERROR:  record old is unassigned yet

if i take NEW. instead of OLD.: no errors -- but minport and maxport
are empty after insert. (could it be that OLD makes no sense on
insert? I'd lie if I'd say that I understood the conecpt, but
databases are hard stuff for me even in my native language)

I searched documentation and archives[1], tried some tricks after
'DECLARE', tried with and without OLD and NEW, but all i get are
errors or nothinng happens.

Thanks a lot for your help,
Knut Suebert

[1] everything found was in -general or -hackers. So I decided to send
    this to -general and not to -novice, sorry if i'm wrong.

pgsql-general by date:

Previous
From: Ryan Mahoney
Date:
Subject: Re: RedHat Open Database?
Next
From: "Joe Conway"
Date:
Subject: Re: trigger: NEW/OLD-error or nothing happens