From: Adrian Klaver <adrian.klaver@aklaver.com>
To: PegoraroF10 <marcos@f10.com.br>
To: pgsql-general@postgresql.org
Subject: Re: Problem with trigger makes Detail record be invalid
Date: Thu, 19 Apr 2018 11:24:43 -0700
Message-ID: <7cb5806e-7371-5c19-1c7b-b8ac556bdfb5@aklaver.com> (raw)
In-Reply-To: <1524160534707-0.post@n3.nabble.com>
References: <1524160534707-0.post@n3.nabble.com>
On 04/19/2018 10:55 AM, PegoraroF10 wrote:
> I´m using Postgres 10 on ubuntu.
>
> suppose a simple Master/Detail structure like this:
>
> create table Master(ID integer primary key, name text);
> create table Detail(ID integer primary key, Master_ID Integer, OtherInfo
> text);
> alter table Detail add constraint FKMasterDetail foreign key (Master_ID)
> references Master(ID) on update cascade on delete cascade;
>
> Then insert some records on it:
> insert into Master(ID, Name) values(1,'First'), values(2,'Second');
> insert into Detail(ID, Master_ID, OtherInfo) values(1,1,'Detail
> Information'), (2,2,'Detail Information2');
>
> Then, if I delete on Master will delete on detail too. Fine.
> delete from Master where ID=1;
>
> But now, suppose I have a complex trigger before update or delete that runs
> on Detail table.
> create function DoAComplexJobOnDetail() returns trigger as $$
> begin
> -- Do lots of things then
> Return new; --This is the error, because I´m returning new even for
> delete;
That can be dealt with using TG_OP value to conditionally change what is
RETURNed:
https://www.postgresql.org/docs/10/static/plpgsql-trigger.html
"TG_OP
Data type text; a string of INSERT, UPDATE, DELETE, or TRUNCATE
telling for which operation the trigger was fired.
"
See Example 42.4. A PL/pgSQL Trigger Procedure For Auditing at bottom of
page.
> end;$$ language plpgsql;
> create trigger DetailDoAComplexJob before update or delete on Detail for
> each row execute procedure DoAComplexJobOnDetail();
>
> Then try to delete the other Master record. It will be deleted on Master but
> Detail record doesn´t and will obviously become invalid because the foreign
> key.
> delete from Master where ID=2;
> select * from Master; --will show no records.
> select * from Detail; --will show one record pointing to Master_ID=2, that
> doesn´t exist anymore.
>
> Is this a bug or it´s mine responsability to check that trigger result ?
Without seeing exactly what the trigger function on Detail is doing that
is not answerable.
> If that trigger responds incorrectly I think that no information could be
> executed.
>
>
>
> --
> Sent from: http://www.postgresql-archive.org/PostgreSQL-general-f1843780.html
>
--
Adrian Klaver
adrian.klaver@aklaver.com
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: adrian.klaver@aklaver.com, marcos@f10.com.br
Subject: Re: Problem with trigger makes Detail record be invalid
In-Reply-To: <7cb5806e-7371-5c19-1c7b-b8ac556bdfb5@aklaver.com>
References: <1524160534707-0.post@n3.nabble.com> <7cb5806e-7371-5c19-1c7b-b8ac556bdfb5@aklaver.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox