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 1tAGEq-00EdgB-40 for pgsql-general@arkaria.postgresql.org; Sun, 10 Nov 2024 22:16:55 +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 1tAGEn-009Ujw-8X for pgsql-general@arkaria.postgresql.org; Sun, 10 Nov 2024 22:16:53 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tAGEm-009Ujb-CB for pgsql-general@lists.postgresql.org; Sun, 10 Nov 2024 22:16:53 +0000 Received: from fhigh-b7-smtp.messagingengine.com ([202.12.124.158]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tAGEk-001C0D-2s for pgsql-general@lists.postgresql.org; Sun, 10 Nov 2024 22:16:51 +0000 Received: from phl-compute-06.internal (phl-compute-06.phl.internal [10.202.2.46]) by mailfhigh.stl.internal (Postfix) with ESMTP id 4305325400FC; Sun, 10 Nov 2024 17:16:48 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-06.internal (MEProxy); Sun, 10 Nov 2024 17:16:48 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm2; t=1731277008; x=1731363408; bh=JxVgUzGTDw17LCXKotZcRHhFCCtyh5H62V7e6Yr+BjQ=; b= OJjfcPOIIdxHvM8iZdS92iLgIzM0jlk0uDeDoWgWh252cLIYNeCAF2N8m+F6+Hcg MQaVwncTTx+lWLirKaElrVfwL2vq+1b8uFHVDGLD1Yb/iOld0tL9iyJ34TtuAL4c AlY6jKUeUNeWWd3Y+05ykF+NWkH77twDgPU5GtmtfrscjRwgIDQCBckbHLT6zZrF QVeqiVUeOkMw8/avvQvvD/exNHf4sQ3cxM/z03bDG58SuTvGi+SimkdopLcW37WL 6NqqHMUecpZiy8TomDIpgfkIqm8y/eeKXG6ZK9rayl1TNi6ApwbeMy/M5rtE3L+G ocrjZzNdSzLuvMsnBFMkOg== 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 :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1731277008; x= 1731363408; bh=JxVgUzGTDw17LCXKotZcRHhFCCtyh5H62V7e6Yr+BjQ=; b=X qWEa3BfjzWH/DHoTCM5iwxE+wLhaq9F5ols29bkvGCjj6Y9zxjLkINuEEhDcZcX9 M9Z0V/EgXc0k7X7LR2QWKYmNDsFrUj9cx0PpUTOBNg0mMgzehJ7X0/BaY/KDnrip n/6CARM6+EWe/7+rnq8uj9wyMGjXG2qylfpdCKSE1YbxMGOVr30l2+OXinAYfwVS rokwIm+rMgIBcQyEX20kUmTOvGcBhjtagfTOmd4uVtcCcMfImYmuB1v3ced8LKDV O/U1A/iCL9zsbcwE9E9sJaKOJN0us9FzHGPKx8umKW6FTNY/PRAVtnRJS+gtov+O hSf4EBUNGuZKsCTkuIf5g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddruddtgdduheejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpefkffggfg fuvfevfhfhjggtgfesthekredttddvjeenucfhrhhomheptegurhhirghnucfmlhgrvhgv rhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeenucggtffrrg htthgvrhhnpeefgeefieeutdfggfetgefgheekjeehteeileeigfetieekjedvieeviefg heevtdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpe grughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmpdhnsggprhgtphhtthho pedvpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehushgvrhesphhiughurdguvg hvpdhrtghpthhtohepphhgshhqlhdqghgvnhgvrhgrlheslhhishhtshdrphhoshhtghhr vghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 10 Nov 2024 17:16:47 -0500 (EST) Message-ID: <19dbfe75-8b98-49af-bf03-0b7d72aa02e8@aklaver.com> Date: Sun, 10 Nov 2024 14:16:46 -0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Fwd: Fwd: Postgres attach partition: AccessExclusive lock set on different tables depending on how attaching is performed To: user@pidu.dev Cc: pgsql-general@lists.postgresql.org References: <254c31c2-ff15-4720-a4fc-f0969d677d72@aklaver.com> <4ebbbcf5-9c35-4711-bd7c-06a56a31aeff@aklaver.com> Content-Language: en-US From: Adrian Klaver In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 11/10/24 11:52, user wrote: > Thank you for an answer! > So reparenting of a constraint required that additional lock. > I made some measurements and can see that even that reparenting (and > additional lock) is required, the time it takes to make the attach is > smaller than when the foreign constraint hasn't been created beforehand. > > So, to summarise, there is a tradeoff. > 1. Create constraint before attach, but during attach additional tables > will be locked with AccessExculive. The time of an attach will be > minimalĀ  (for large tables it is still tens of ms in our db) but there > is a higher chance of deadlocks (as more tables locked with restrictive > locks) > 2. Just proceed with attach. The constraint will be created because the > parent table has the constraint in its definition. Because no > reparenting is required, no additional exclusive lock is held. But this > process will take more time to finish as a constraint is created from > scratch. > > Are these the only options? > Basically I want to add partitions dynamically to db while app is > running. I want to minimise the duration of "attach" command but also > the amount of locks held on several tables at once (to avoid deadlocks). > > Once again, thanks for an answer. It is now clear to me why such > behaviour occurs. Just to be clear: 1) I had nothing to do with writing this code. 2) I am not a C programmer, so what you got was my creative interpretation of what I think is going on. 3) Because of 1 & 2, this needs further analysis by someone or someones more knowledgeable. > Regards > -- Adrian Klaver adrian.klaver@aklaver.com