Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hlwVO-0000bH-9r for pgsql-general@arkaria.postgresql.org; Fri, 12 Jul 2019 14:26:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hlwVN-0001Co-4M for pgsql-general@arkaria.postgresql.org; Fri, 12 Jul 2019 14:26:33 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hlwVL-00017w-Bd for pgsql-general@lists.postgresql.org; Fri, 12 Jul 2019 14:26:32 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hlwVG-0005bX-Tl for pgsql-general@postgresql.org; Fri, 12 Jul 2019 14:26:30 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id F36DE2233F; Fri, 12 Jul 2019 10:26:24 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Fri, 12 Jul 2019 10:26:25 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=k CGMyRMsZV1a2CzRJWpKZza1APejfGi/eGG8zvUiJC0=; b=CJNSCPD3o+Gv5BpTF OnmDK828+jquqv2B4GdMHC6R/c0p8UNLkD22sPYafQDEVv68GbFtwHiGeYrmefAl 0mUzO5mjeOJv6WNZkpHwbcfo3ULktqk0kou971elTpuYYUG9WqPD5/9Euj9RUiL4 D/0m1VHJvZOe5CWxShyzI4bOzk0yRboE1tyAGHGjmTwjDAg/kSp0w9KisEs3lvtN JGhf+VUK6JfV2JgTbvGG7YHYz3of/Ix+B7iNK2rPpO6HJDuGCpeP/RPbmqtKsnqv /kGblQaI/aYfXbzrgJBlhV14XOO7mt42z2/uI2U7dLV/+NxrXb1o4FZ9IbqZQZCb YB13Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=kCGMyRMsZV1a2CzRJWpKZza1APejfGi/eGG8zvUiJ C0=; b=miaqceF97+ZdkyqlVsMznUQ2FXHgqBUrIk888dOuNNs2WMnniUoTZBaeR SqXBwkf/Lk+xNTGkYHdz0uvTBjEY/gWAhiWoe4Pi5ZGqYLsj4xQIFzVskuJ+FWdy GB6J05cIEh4Dhw2+uBEPJwq20X+2pxHVictf/fGhARbRzyTjCZls5hpGgWpARXYE vGQGneu2B/N8nhO07gf6IfMO0Mc2+Ek9zg6gft66lpfSkS/QM9aOSFfKP+ssft5Y QkGspSx15GkYhEVdtuH/QzSv1vCt6OTR0ttM/A/b0/dTQSmUnGvLoGDm0j0rTyRB ZGhr5deHJ6NA1jKr3/xq8Wx8PYfcQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduvddrhedtgdejiecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesthejre dttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhmqeenucffohhmrghinhepphhoshhtghhrvghsqh hlrdhorhhgnecukfhppeejuddrvdduvddrudehtddrvddtvdenucfrrghrrghmpehmrghi lhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhenucevlh hushhtvghrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id 1B056380084; Fri, 12 Jul 2019 10:26:24 -0400 (EDT) Subject: Re: disable and enable trigger all when a foreign keys To: Tom Lane , =?UTF-8?Q?Emanuel_Ara=c3=bajo?= Cc: pgsql-general@postgresql.org References: <26583.1562940265@sss.pgh.pa.us> From: Adrian Klaver Message-ID: <92dd264a-d273-a057-1c1b-3bf419235232@aklaver.com> Date: Fri, 12 Jul 2019 07:26:23 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: <26583.1562940265@sss.pgh.pa.us> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 7/12/19 7:04 AM, Tom Lane wrote: > =?UTF-8?Q?Emanuel_Ara=C3=BAjo?= 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