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 1glP9T-0005Ha-Gs for pgsql-bugs@arkaria.postgresql.org; Mon, 21 Jan 2019 02:17:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1glP9S-0000kE-55 for pgsql-bugs@arkaria.postgresql.org; Mon, 21 Jan 2019 02:17:26 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1glP9R-0000k6-RX for pgsql-bugs@lists.postgresql.org; Mon, 21 Jan 2019 02:17:26 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1glP9O-0000wH-Fb for pgsql-bugs@lists.postgresql.org; Mon, 21 Jan 2019 02:17:24 +0000 Received: from [192.168.127.1] (port=24565 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1glP9K-000E2V-Mw; Mon, 21 Jan 2019 02:17:18 +0000 Received: from [127.0.0.1] (port=27492 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1glP9K-000FKM-2J; Mon, 21 Jan 2019 02:17:18 +0000 From: Andrew Gierth To: =?utf-8?B?QW5kcsOpIEjDpG5zZWw=?= Cc: Subject: Re: Deferred foreign key and two updates block ALTER TABLE In-Reply-To: <04ab01d4b128$fa63f510$ef2bdf30$@webkr.de> (=?utf-8?Q?=22Andr?= =?utf-8?Q?=C3=A9_H=C3=A4nsel=22's?= message of "Mon, 21 Jan 2019 02:31:04 +0100") Message-ID: <87y37e4wkd.fsf@news-spur.riddles.org.uk> References: <04ab01d4b128$fa63f510$ef2bdf30$@webkr.de> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Mon, 21 Jan 2019 02:17:17 +0000 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Andr=C3=A9" =3D=3D Andr=C3=A9 H=C3=A4nsel writes: Andr=C3=A9> Hi list, Andr=C3=A9> this looks like a bug to me, forgive me if it's expected Andr=C3=A9> behavior: It is expected behavior. Andr=C3=A9> Actual result: ERROR: cannot ALTER TABLE "test" because it has Andr=C3=A9> pending trigger events SQL state: 55006 What this means is that there are pending trigger events on the table, and calling those triggers after altering the column type would not be possible, for example because rewriting the table would change the TIDs of rows making it impossible for the queued event to locate them. Andr=C3=A9> - There is no error if there is just one UPDATE instead of two Andr=C3=A9> (this makes me think this might be a bug) That's actually an optimization; there are cases where a foreign-key check can be optimized away, which is happening on the first update, but one of the current conditions for that optimization is that the row or row version being updated is not new in the current transaction, so a second or subsequent update, or an update after an insert, will require the check to actually be queued. Andr=C3=A9> - There is no error if both UPDATEs lead to the same result Andr=C3=A9> (when the second UPDATE is a no-op, like setting some_column = =3D 1 Andr=C3=A9> again) This one I can't currently explain without digging into the code. Andr=C3=A9> - There is no error if the foreign key is non-deferrable or Andr=C3=A9> currently immediate This is because these cases don't leave trigger events on the queue between statements - they fire at the end of the statement. BTW, this kind of restriction is anticipated in the SQL spec (in SQL 2016, see 4.41.1 "General description of SQL-transactions"): It is implementation-defined whether or not the execution of an SQL-data statement is permitted to occur within the same SQL-transaction as the execution of an SQL-schema statement. If it does occur, then the effect on any open cursor or deferred constraint is implementation-defined. There may be additional implementation- defined restrictions, requirements, and conditions. If any such restrictions, requirements, or conditions are violated, then an implementation-defined exception condition or a completion condition warning with an implementation-defined subclass code is raised. PG tries hard to allow mixed DDL and DML/queries in transactions, but where one would invalidate information needed by the other, it has to throw an error. --=20 Andrew (irc:RhodiumToad)