Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cgCz5-0003dQ-IS for pgsql-bugs@arkaria.postgresql.org; Tue, 21 Feb 2017 16:08:11 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cgCz5-0008Po-5g for pgsql-bugs@arkaria.postgresql.org; Tue, 21 Feb 2017 16:08:11 +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_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cgCz4-0008NR-10 for pgsql-bugs@postgresql.org; Tue, 21 Feb 2017 16:08:10 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cgCz1-0002Ai-6X for pgsql-bugs@postgresql.org; Tue, 21 Feb 2017 16:08:09 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v1LG84Xc009001; Tue, 21 Feb 2017 11:08:04 -0500 From: Tom Lane To: Alvaro Herrera cc: amos_operation@swiss-as.com, pgsql-bugs@postgresql.org Subject: Re: BUG #14560: FK not valid are ignored during transactions In-reply-to: <20170221155133.owzj7exrno5f3vht@alvherre.pgsql> References: <20170221155133.owzj7exrno5f3vht@alvherre.pgsql> Comments: In-reply-to Alvaro Herrera message dated "Tue, 21 Feb 2017 12:51:33 -0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <8999.1487693284.1@sss.pgh.pa.us> Date: Tue, 21 Feb 2017 11:08:04 -0500 Message-ID: <9000.1487693284@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org Alvaro Herrera writes: > Tom Lane wrote: >> I think that the fact that you don't see an error when you >> do only one row update per transaction is because we optimize away the >> FK check entirely in that case (if the FK columns aren't changed, >> obviously). I don't recall exactly why that optimization can't be used >> for repeat updates in a single transaction. > I would expect a failure if the value changes from a matching value to a > mismatching value; or from a mismatching value to another mismatching > value. But if the value does not change, why are we checking it at all? Well, if you're going to make me go look it up ... RI_FKey_fk_upd_check_required explains: /* * If the original row was inserted by our own transaction, we * must fire the trigger whether or not the keys are equal. This * is because our UPDATE will invalidate the INSERT so that the * INSERT RI trigger will not do anything; so we had better do the * UPDATE check. (We could skip this if we knew the INSERT * trigger already fired, but there is no easy way to know that.) */ So the point is basically that we don't know if the previous row version was a fresh INSERT or an UPDATE, and in the former case there might not have been any previous check that the existing FK values satisfy the constraint. I don't think we can weaken this logic for a NOT VALID constraint, because the contract is still that transactions aren't allowed to create any new violations of the constraint. regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs