pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
To: Emanuel Araújo <eacshm@gmail.com>
Cc: pgsql-general@postgresql.org
Subject: Re: disable and enable trigger all when a foreign keys
Date: Fri, 12 Jul 2019 07:26:23 -0700
Message-ID: <92dd264a-d273-a057-1c1b-3bf419235232@aklaver.com> (raw)
In-Reply-To: <26583.1562940265@sss.pgh.pa.us>
References: <CAMuTAkaqysXHaGgQH3NBv-Rh-01RDGJQEz6wyuGBpu+Y1kDNxQ@mail.gmail.com>
	<26583.1562940265@sss.pgh.pa.us>

On 7/12/19 7:04 AM, Tom Lane wrote:
> =?UTF-8?Q?Emanuel_Ara=C3=BAjo?= <eacshm@gmail.com> writes:
>> This is a situation when we needed fill a new store data in a family env.
>> When I run "alter table a disable trigger all;" ... the foreign key
>> "a_id_b_fkey" was disabled to, its ok, but I needed run a load data into
>> tables "a"and "b" and table a exists a tuple that not exists in column id_b
>> -> (references b(id)).
> 
>> When finished, the command "alter table a enable trigger all" was executed
>> but not alert or broken, why? Cause orphan record is there.
> 
>> This behavior is common or when doing "enable trigger all" PostgreSQL
>> whould show me a error or a warning?
> 
> Well, yeah, DISABLE TRIGGER ALL defeats enforcement of FK constraints.
> That's why you have to be superuser to use it[1].  Perhaps disabling
> only user triggers would have been the way to do what you want.

The OP is probably trying to understand why the below happens:

create table if not exists a (id int primary key , id_b int, descr text);

insert into a values (1,1,'house sold');
insert into a values (2,1,'house 1 not sold');
insert into a values (3,2,'apartment 1 not sold');
insert into a values (4,null,'house to buy');
insert into a values (5,3,'car to sell');

create table if not exists b (id int primary key , descr text);

insert into b values (1, 'house');
insert into b values (2, 'apartment');

alter table a add constraint a_id_b_fkey foreign key (id_b) references 
b(id);
ERROR:  insert or update on table "a" violates foreign key constraint 
"a_id_b_fkey"
DETAIL:  Key (id_b)=(3) is not present in table "b".

Versus the above not happening when you re-enable a trigger. I know it 
is documented:

https://www.postgresql.org/docs/11/sql-altertable.html
" Disabling or enabling internally generated constraint triggers 
requires superuser privileges; it should be done with caution since of 
course the integrity of the constraint cannot be guaranteed if the 
triggers are not executed."

Still it has caught me before and I would be interested in knowing why 
the difference?


> 
> 			regards, tom lane
> 
> [1] The general assumption in PG is that superusers know what they're
> doing.
> 
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





view thread (6+ messages)  latest in thread

Message-ID: <92dd264a-d273-a057-1c1b-3bf419235232@aklaver.com>
Permalink:  ../92dd264a-d273-a057-1c1b-3bf419235232@aklaver.com/
Also on:    postgresql.org/message-id/92dd264a-d273-a057-1c1b-3bf419235232@aklaver.com

 · 

reply

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, tgl@sss.pgh.pa.us, eacshm@gmail.com
  Subject: Re: disable and enable trigger all when a foreign keys
  In-Reply-To: <92dd264a-d273-a057-1c1b-3bf419235232@aklaver.com>
  References: <CAMuTAkaqysXHaGgQH3NBv-Rh-01RDGJQEz6wyuGBpu+Y1kDNxQ@mail.gmail.com> <26583.1562940265@sss.pgh.pa.us> <92dd264a-d273-a057-1c1b-3bf419235232@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