Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tBBtm-002NKp-6r for pgsql-general@arkaria.postgresql.org; Wed, 13 Nov 2024 11:51:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tBBsk-00Dk6W-Uf for pgsql-general@arkaria.postgresql.org; Wed, 13 Nov 2024 11:49:59 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tBBsj-00Dk6O-Ta for pgsql-general@lists.postgresql.org; Wed, 13 Nov 2024 11:49:59 +0000 Received: from fout-a6-smtp.messagingengine.com ([103.168.172.149]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tBBsg-001iGT-65 for pgsql-general@lists.postgresql.org; Wed, 13 Nov 2024 11:49:57 +0000 Received: from phl-compute-12.internal (phl-compute-12.phl.internal [10.202.2.52]) by mailfout.phl.internal (Postfix) with ESMTP id EA6681380129; Wed, 13 Nov 2024 06:49:52 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-12.internal (MEProxy); Wed, 13 Nov 2024 06:49:52 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1731498592; x=1731584992; bh=7 jtaknOzC9+hi27ACZH7ZR7j5EvTEfyB/WI4u0ahb/w=; b=I3BY23hdnfA1fugt3 d4A3wwIHdy5S3XLB0zNteuMtw8tpERDXpHqWoFYVFVWkqE+BCqGWr4Kt5MBFpUtf ecMMKAK6lZYnCFptYJkKiesCUyRIsvOhgTm7HsWbVhomFaWyqc+fnb1GadNctNNU LO/M0uQ9t/htWifYwoSAj2Sq7ugmjmBCkR69VoWOj1Tq/YHfI1h+d1wCsdmYm7Xa d4PcKRV4pMzGCVYNUxY/qqVUH98CjjA3H+jYco9v1LXNw0xd5ba34r7Txy/2mpaK zHRcOiy511cRFZ2I2ZTOuR5Dg3SJ2hzuqADcDcTCmQITXZQSwbFL+VM9BFKcuqs0 dE02g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddrvddtgdefvdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdpuffr tefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnth hsucdlqddutddtmdenucfjughrpeffhffvvefukfggtggugfgjsehtkeertddttdejnecu hfhrohhmpeetlhhvrghrohcujfgvrhhrvghrrgcuoegrlhhvhhgvrhhrvgesrghlvhhhrd hnohdqihhprdhorhhgqeenucggtffrrghtthgvrhhnpedvkedtffduffdtffffheffhfej jefhgfeiueeukeejkeffgfdufffhudffffeuveenucffohhmrghinhepvghnthgvrhhprh hishgvuggsrdgtohhmnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghi lhhfrhhomheprghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrghdpnhgspghrtg hpthhtohepgedpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtoheprggurhhirghnrdhk lhgrvhgvrhesrghklhgrvhgvrhdrtghomhdprhgtphhtthhopehpghhsqhhlqdhgvghnvg hrrghlsehlihhsthhsrdhpohhsthhgrhgvshhqlhdrohhrghdprhgtphhtthhopehushgv rhesphhiughurdguvghvpdhrtghpthhtohepthhglhesshhsshdrphhghhdrphgrrdhush X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 13 Nov 2024 06:49:52 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1731498589; bh=A/13Wfu8DxjwkgXUntppe9/gMLcozCmHRzm6HBKckXI=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=mUW3fftkaK0PwYl7q4jrqDc7qM1IiCN1rWrbljVm4kLAOUNfyWx4wzlsHgJAAz9F+ PChdynElZwyW9Q+B2Qq+axC246Ee/wWIgqFJguzz1KMvPNRxBVKku82H+YIePX6AIA HCMnWT9I8Dp+5DuGLY+Y4ru1dj+uF0bz9C+XweMWepZFut2BO/2XDeQsttxZ1z7A3u LnlSOX940z+PL5MevKweRikrLAESFPgCOwJ+AH9EVMss7CjyTmM186RKnU+ZTh+99s a+aP3wBdLDJJouK6fx+umB9nRQr+MX036NvLS47OVMTvLCuu+mRYyNSwBcihbuGpCn Xld/HNn+NZdpA== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id 3D0E695; Wed, 13 Nov 2024 12:49:49 +0100 (CET) Date: Wed, 13 Nov 2024 12:49:49 +0100 From: Alvaro Herrera To: Tom Lane Cc: Adrian Klaver , user@pidu.dev, pgsql-general@lists.postgresql.org Subject: Re: Fwd: Fwd: Postgres attach partition: AccessExclusive lock set on different tables depending on how attaching is performed Message-ID: <202411131149.rax7xn7gnkxi@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <174130.1731282279@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2024-Nov-10, Tom Lane wrote: > This surprised me a bit too, because I thought we took a > slightly-less-than-exclusive lock for FK additions or deletions. > Tracing through it, I find that CloneFkReferencing opens the > referenced relation with ShareRowExclusiveLock as I expected. > But then we conclude that we can drop the existing FK enforcement > triggers for the table being attached. That causes us to take > AccessExclusiveLock on the trigger itself, which is fine because > nobody's really paying attention to that. But then RemoveTriggerById > takes AccessExclusiveLock on the trigger's table. We already had > that on the table being attached, but not on the other table. Oooh. > I wonder whether it'd be all right for RemoveTriggerById to take > only ShareRowExclusiveLock on the trigger's table. This seems > OK in terms of basic semantics: that's enough to lock out > anything that might want to fire triggers on the table. However, > this comment for AlterTableGetLockLevel gives me pause: > > * Also note that pg_dump uses only an AccessShareLock, meaning that anything > * that takes a lock less than AccessExclusiveLock can change object definitions > * while pg_dump is running. Be careful to check that the appropriate data is > * derived by pg_dump using an MVCC snapshot, rather than syscache lookups, > * otherwise we might end up with an inconsistent dump that can't restore. > > I think pg_dump uses pg_get_triggerdef, which is probably not > safe in these terms. Looking at pg_get_triggerdef_worker, it is not using syscache but a systable scan, which uses the catalog snapshot. A catalog snapshot is indeed implemented as an MVCC snapshot (so strictly speaking it _is_ an MVCC snapshot), but the invalidation rules are different from a normal MVCC snapshot, so AFAIU it's still unsafe. > An alternative answer might be what Alvaro was muttering about > the other day: redesign FKs for partitioned tables so that we > do not have to change the set of triggers when attaching/detaching. Hmm, I hadn't thought about this idea in those terms, but perhaps we could reimplement this by not having one trigger for each RI check, but instead a single trigger which internally determines which FK constraints exist on the table and does the necessary work in a single pass. Then we don't need to add/drop triggers all the time, but we just add it with the first FK in the table, and remove it when dropping the last FK. For tables with many FKs, this could be a win, because we'd only go through the trigger machinery once. If a table has both outgoing and incoming FKs, maybe we could have _one_ single trigger. (I think this would be orthogonal with the project to stop using SPI for RI triggers.) -- Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/