Re: NEW in after insert trugger contained incorrect data

From: Jim Nasby <Jim(dot)Nasby(at)BlueTreble(dot)com>
To: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>, <brilliantov(at)byterg(dot)ru>, Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: NEW in after insert trugger contained incorrect data
Date: 2014-11-15 04:57:27
Message-ID: 5466DD37.1020906@BlueTreble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 11/14/14, 10:09 AM, Adrian Klaver wrote:
>> Trigger update_cpu_load_stat added to table trassa.cpu_load:
>> CREATE TRIGGER update_cpu_load_stat_trigger
>> AFTER INSERT
>> ON trassa.cpu_load_stat
>> FOR EACH ROW
>> EXECUTE PROCEDURE trassa.update_cpu_load_stat();
>
> Another run through showed that the issue is above. You have declared the trigger on trassa.cpu_load_stat instead of trassa.cpu_load. trassa.cpu_load_stat has no value field, hence the error.

Something else to consider: using FOUND to decide whether to INSERT vs UPDATE is a race condition: you can do the SELECT, someone else can then insert or delete, and then you attempt to do the wrong thing.

To handle this correctly, you need an appropriate UNIQUE constraint or primary key, and to follow the pattern in table 40-2 at http://www.postgresql.org/docs/9.3/interactive/plpgsql-control-structures.html#PLPGSQL-ERROR-TRAPPING

If you don't, and you have concurrent activity you can end up losing data (and in the case of a DELETE after your SELECT, the data loss will be completely silent).
--
Jim Nasby, Data Architect, Blue Treble Consulting
Data in Trouble? Get it in Treble! http://BlueTreble.com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Seamus Abshere 2014-11-16 23:52:35 Comparing results of regexp_matches
Previous Message Peter Eisentraut 2014-11-14 20:57:42 Re: pg_upgrade and ubuntu