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 1tiNAN-00FBJl-E3 for pgsql-general@arkaria.postgresql.org; Thu, 13 Feb 2025 00:33:19 +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 1tiNAL-00H6GW-GC for pgsql-general@arkaria.postgresql.org; Thu, 13 Feb 2025 00:33:18 +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 1tiNAK-00H6GO-Mm for pgsql-general@lists.postgresql.org; Thu, 13 Feb 2025 00:33:17 +0000 Received: from fout-b4-smtp.messagingengine.com ([202.12.124.147]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tiNAJ-000UNe-13 for pgsql-general@lists.postgresql.org; Thu, 13 Feb 2025 00:33:16 +0000 Received: from phl-compute-06.internal (phl-compute-06.phl.internal [10.202.2.46]) by mailfout.stl.internal (Postfix) with ESMTP id 952C411401A8; Wed, 12 Feb 2025 19:33:14 -0500 (EST) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-06.internal (MEProxy); Wed, 12 Feb 2025 19:33:14 -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=1739406794; x=1739493194; bh=6im4AkmKWwiEcOkYyAz443deD5MdVY7daCKj5Tmfow4=; b= KGjDo303SXUzstTxdvoY3t/phhCN2p5OG3C4I8ym/k4V9GGZ7EUAx3ryAZDZ0dgc eM/Xezw/fkSt5G85MW0BAgp/eNU/Ol2j5uu0NYKdG9ls3ETk/gsvo0Zsu1bYB4lD MbCmlgRsMFguJmziZq7wO89grRb+CfLeiquZe2LluvRVsH2XucWWtUBfmQeto9Wj DshIxznMAqw2Jyqg6mFAcAQ+4+87iHoGqsTKhtIiGjeRhPlzXtvJZlI/LGllVET3 sVyNG9jJexe8TzVZB8110soLm+RFb1VrhxGuZ9/C0WbPWd0sK/Ki93OMWoS3qjv/ 4w2h52kYcCXJPPkMzG5WHQ== 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=1739406794; x= 1739493194; bh=6im4AkmKWwiEcOkYyAz443deD5MdVY7daCKj5Tmfow4=; b=V gZkA11Oj53dCQPhhvSHPSWnWTJVo3Tz1BxwcTMtThGU8tHPtjmVAMk9S1QdJpwLj v4xb2SdezojSn0ASoRSH8joPyfR0WeL19AKiG5lfSRC9zoZF92KLWE5n5wjw8lEH OLdc3ST94Wk3Vmr+J0+KNJszbRGgVaK8ePtOvn7binym3uCRZGaN5dYhz4Ef64pt Ivjt9oueVbbVsraNUEX+wkXaHwByy/kgq8Jbb1TLfpOOmy4qm8ddW2cXEwrRXXKU +74a8FJSvlQbSI2Fd0pmOHKCokL4lhf2col2Oq6al8nJ9kudPmB8WTLNix0Gjk4F 2+iG8gbLwAOifwavFpiIg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdegheeffecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenogfuuhhsphgvtghtffhomhgrihhnucdlgeelmdenucfjughr pefkffggfgfuvfevfhfhjggtgfesthekredttddvjeenucfhrhhomheptegurhhirghnuc fmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeen ucggtffrrghtthgvrhhnpeffteevleegleeggeehvdekleegjeevieekfffhtdefuefhgf dvgeekleefkeekteenucffohhmrghinhepghhithhhuhgsrdhiohenucevlhhushhtvghr ufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvg hrsegrkhhlrghvvghrrdgtohhmpdhnsggprhgtphhtthhopedvpdhmohguvgepshhmthhp ohhuthdprhgtphhtthhopehmrghrtggvfhgvrhhnjeesghhmrghilhdrtghomhdprhgtph htthhopehpghhsqhhlqdhgvghnvghrrghlsehlihhsthhsrdhpohhsthhgrhgvshhqlhdr ohhrgh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 12 Feb 2025 19:33:13 -0500 (EST) Message-ID: <22406cdf-83f5-4b98-b07e-7827afe97f93@aklaver.com> Date: Wed, 12 Feb 2025 16:33:12 -0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Best Approach for Swapping a Table with its Copy To: Marcelo Fernandes Cc: pgsql-general@lists.postgresql.org References: <8ac6a1dd-db82-4ea7-8186-0c354d79049f@aklaver.com> <9fa75099-1ee9-43de-ac6e-945841b184eb@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 2/12/25 14:04, Marcelo Fernandes wrote: > On Thu, Feb 13, 2025 at 10:40 AM Adrian Klaver > wrote: >> Do you know this will not fail on the existing data? > > Yes, all the current data in the original table respects the constraint. > >> Do you have room for a complete copy of the table? > > Yes, in this scenario the copy is already created, and triggers keep the copy > in sync with the original table. To confirm, this copy has the exclusion constraint defined? > >> I am not seeing how this can be done without some outage for that table. > > Swapping tables without an outage is possible. I believe that this something > involved in the process repacking a table when using pg_repack as it relies on > copying the whole table and then swapping it for the original. Not seeing it: https://reorg.github.io/pg_repack/ "Details Full Table Repacks [...] pg_repack will only hold an ACCESS EXCLUSIVE lock for a short period during initial setup (steps 1 and 2 above) and during the final swap-and-drop phase (steps 6 and 7). For the rest of its time, pg_repack only needs to hold an ACCESS SHARE lock on the original table, meaning INSERTs, UPDATEs, and DELETEs may proceed as usual." During the ACCESS EXCLUSIVE stages you will not have access. Not only that with pg_repack you are not changing the table definition, whereas in your case you are introducing a new constraint and associated index. > > My main question is "How?". I know that the pg_repack involves swapping the > relfilenode values and something about TOAST tables, but I am not super > acquainted with pg_repack code or debugging tools to verify precisely what it > does. > >> What sort of time frame is acceptable? > > The scan phase in this table is very slow, on top of it the exclusion > constraint needs to create the underlying index to service the constraint. > > Anything that takes more than 10s in this system is prohibitive, in this sense > creating the constraint without having a table copy is not viable for the size > of this table. Do you have a dev setup where you can test alternatives with a test sample of data? > > Regards, -- Adrian Klaver adrian.klaver@aklaver.com