Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1c9x6I-0001oN-7x for pgsql-bugs@arkaria.postgresql.org; Thu, 24 Nov 2016 16:42:18 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1c9x6H-000568-R2 for pgsql-bugs@arkaria.postgresql.org; Thu, 24 Nov 2016 16:42:17 +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 1c9x5c-0003bA-Aw for pgsql-bugs@postgresql.org; Thu, 24 Nov 2016 16:41:36 +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 1c9x5Y-0003hI-HE for pgsql-bugs@postgresql.org; Thu, 24 Nov 2016 16:41:35 +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 uAOGfPKI017846; Thu, 24 Nov 2016 11:41:26 -0500 From: Tom Lane To: Vik Fearing cc: benjie@jemjie.com, pgsql-bugs@postgresql.org Subject: Re: BUG #14431: ERROR: XX000: relation 5022917 has no triggers ("should not happen") In-reply-to: <680a86c6-b9f1-09f6-02de-ed6b15fff6e8@2ndquadrant.fr> References: <20161124114911.6530.31200@wrigleys.postgresql.org> <680a86c6-b9f1-09f6-02de-ed6b15fff6e8@2ndquadrant.fr> Comments: In-reply-to Vik Fearing message dated "Thu, 24 Nov 2016 13:37:46 +0100" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <17606.1480005504.0@sss.pgh.pa.us> Date: Thu, 24 Nov 2016 11:41:25 -0500 Message-ID: <17845.1480005685@sss.pgh.pa.us> X-Pg-Spam-Score: -4.8 (----) 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 ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <17606.1480005504.1@sss.pgh.pa.us> Vik Fearing writes: > For the archives, the test case at that link is: Thanks for copying that into the archives. The "bar" trigger is actually irrelevant and can be dropped from the example. The problem is that because the FK constraint on "b" is deferred, there is an FK-check trigger event waiting to be fired at the end of the transaction, and we're deleting the pg_trigger row out from under it. Ordinarily, we'd prevent you from dropping a trigger that has unfired events, but the code in tablecmds.c is only checking the "b" table, and the event in question is for "a"'s trigger. With the attached patch, you get ERROR: cannot ALTER TABLE "a" because it has pending trigger events which I think is a sane behavior, although maybe the precise wording of the error could be quibbled with. (But improving that would require fooling with the API of CheckTableNotInUse, which does not seem worthwhile.) The other approach we could take is to allow the DROP CONSTRAINT and silently ignore queued trigger events if we can't find their pg_trigger row ... but that seems pretty scary in terms of its ability to mask bugs, and it's not consistent with past choices. Barring objection, I'll add a test case to this and push it. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="disallow-activity-on-referenced-table.patch"; charset="us-ascii" Content-ID: <17606.1480005504.2@sss.pgh.pa.us> Content-Description: disallow-activity-on-referenced-table.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablec= mds.c index f97bee5..6322fa7 100644 *** a/src/backend/commands/tablecmds.c --- b/src/backend/commands/tablecmds.c *************** ATExecDropConstraint(Relation rel, const *** 7723,7728 **** --- 7723,7746 ---- is_no_inherit_constraint =3D con->connoinherit; =20=20 /* + * If it's a foreign-key constraint, we'd better lock the referenced + * table and check that that's not in use, just as we've already done + * for the constrained table (else we might, eg, be dropping a trigger + * that has unfired events). But we can/must skip that in the + * self-referential case. + */ + if (con->contype =3D=3D CONSTRAINT_FOREIGN && + con->confrelid !=3D RelationGetRelid(rel)) + { + Relation frel; +=20 + /* Must match lock taken by RemoveTriggerById: */ + frel =3D heap_open(con->confrelid, AccessExclusiveLock); + CheckTableNotInUse(frel, "ALTER TABLE"); + heap_close(frel, NoLock); + } +=20 + /* * Perform the actual constraint deletion */ conobj.classId =3D ConstraintRelationId; ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ------- =_aaaaaaaaaa0--