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 1tiikJ-001Y5D-34 for pgsql-general@arkaria.postgresql.org; Thu, 13 Feb 2025 23:35:51 +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 1tiikH-00GR5o-3T for pgsql-general@arkaria.postgresql.org; Thu, 13 Feb 2025 23:35:49 +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 1tiikG-00GR5g-7G for pgsql-general@lists.postgresql.org; Thu, 13 Feb 2025 23:35:49 +0000 Received: from fhigh-b3-smtp.messagingengine.com ([202.12.124.154]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tiikE-000fU8-2k for pgsql-general@lists.postgresql.org; Thu, 13 Feb 2025 23:35:47 +0000 Received: from phl-compute-05.internal (phl-compute-05.phl.internal [10.202.2.45]) by mailfhigh.stl.internal (Postfix) with ESMTP id 88FF42540131; Thu, 13 Feb 2025 18:35:45 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-05.internal (MEProxy); Thu, 13 Feb 2025 18:35:45 -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=1739489745; x=1739576145; bh=ZK01xj30lH4kivZZpT/CDdZqYkIxBlRJ9shI1FDO5zk=; b= jT0690Isb6iyR09owFSgjeEMiespOvXSp7+KmPxP3+Gb6maEuwkWswzkx5cUKV+M BX04XIWclh+ezRccOgr1LAkVeOcKrkJOGmBM58jLeO/wnKU8EeSjvOuqr9MVAH0Z AzTTK6MtTAXNnbs/FuWCMqqrUn0rq8ZM9i635uCl46QsOaf42CKiLoCYRA6U5/8G PW6DXanTTC0+VTPlBRzV7CJXH3brUlOcOHN4QttVTQgVTuTrZIYq1yjNXJQk4d1d kRt7cwrI/a/UDg8o74zFi0JnMwWzKPWdRhzgC+rZitKV8qxZagdAGGQOsncOIaJD Mx9oX8KhsI31M9EqCxXfEw== 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=1739489745; x= 1739576145; bh=ZK01xj30lH4kivZZpT/CDdZqYkIxBlRJ9shI1FDO5zk=; b=p tyH5N+G4YSXrMmx0chkcOCij8pNHo2y2IjR8oAaVLsTktumN/iK6f7wBDXdRnXY/ YPA8I30uJPPyXe/Hsp0LmBSQY47yM5GzIN3s3ndFgxtfkFubDEUAAOzYcMVKumvF KrncuGzG9YKqXKYbwextHfN6RqdGWsJqeJARsyO5R+jEz8TPKAaTipJMZA4dN8p1 DJMXjJVUEoBK/cPVfrhXX3KorXHNCVRrRSM/11zGE8AUzdbxmdgckEwCDjTFI1JY kZcHs4ZV0a5L0r0e6lGfqQPSb+5PDG9hlPY3ofWgphXm7OmWntMz4zIj2VgcXR9Q dW1vAQcKTVfG31BaLGt1Q== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdegkedutdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpefkffggfgfuvfevfhfhjggtgfesthekredttddv jeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvg hrsegrkhhlrghvvghrrdgtohhmqeenucggtffrrghtthgvrhhnpeefgeefieeutdfggfet gefgheekjeehteeileeigfetieekjedvieeviefgheevtdenucevlhhushhtvghrufhiii gvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegr khhlrghvvghrrdgtohhmpdhnsggprhgtphhtthhopeegpdhmohguvgepshhmthhpohhuth dprhgtphhtthhopehmrghrtggvfhgvrhhnjeesghhmrghilhdrtghomhdprhgtphhtthho peguuggvvhhivghnnhgvsehgmhgrihhlrdgtohhmpdhrtghpthhtohephhhtrghmfhhiug hssehgmhgrihhlrdgtohhmpdhrtghpthhtohepphhgshhqlhdqghgvnhgvrhgrlheslhhi shhtshdrphhoshhtghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 13 Feb 2025 18:35:44 -0500 (EST) Message-ID: <380a6684-86d5-4ef6-9120-820d09c52a2b@aklaver.com> Date: Thu, 13 Feb 2025 15:35:43 -0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Best Approach for Swapping a Table with its Copy To: Marcelo Fernandes Cc: Dominique Devienne , Greg Sabino Mullane , pgsql-general@lists.postgresql.org References: <8ac6a1dd-db82-4ea7-8186-0c354d79049f@aklaver.com> <9fa75099-1ee9-43de-ac6e-945841b184eb@aklaver.com> <22406cdf-83f5-4b98-b07e-7827afe97f93@aklaver.com> <94e1205e-8ddf-403b-8477-02d7939f2905@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/13/25 14:59, Marcelo Fernandes wrote: > On Thu, Feb 13, 2025 at 7:37 PM Laurenz Albe wrote: >> Yes, but only if you are willing to write C code that runs inside the >> database server. That way, you can do anything (and cause arbitrary >> damage). >> >> The big challenge here would be to do the swap in a safe way. How do >> you intend to guarantee that the foreign keys are valid without a table >> scan? How do you handle concurrent data modifications? > > Exactly! This is part of what I am trying to figure out (-: > > The plan that I have in mind so far to swap a table safely is: > > 1. Run the following query to grab information about the toast and the toast > index of the tables involved in the swapping: > > SELECT > X.oid, > X.reltoastrelid, > X.relowner, > TOAST_X.indexrelid > FROM pg_catalog.pg_class X > LEFT JOIN > pg_catalog.pg_index TOAST_X ON X.reltoastrelid = > TOAST_X.indrelid AND TOAST_X.indisvalid > WHERE X.oid = ('my_sweet_table')::regclass; > > 2. Open a transaction and acquire an access exclusive lock. > 3. Ensure that the cloned table has the same owner as the original table > > ALTER TABLE copy_table OWNER TO owner_of_the_original_table; > > 4. Now I need to swap all the data in the catalogue tables that point to the > old table and the toast to point to the new one and vice-versa (in case I > need to rollback). > 5. Commit > > Step 4 is what I am stuck on. What bits of the catalogue do I need to change? > And for what reasons? 1) In a previous post you said: "Yes, in this scenario the copy is already created, and triggers keep the copy in sync with the original table." In that case the copy will already have TOAST tables associated with it. 2) What are the FK relationships and how many? Also could you just not add the FK's as NOT VALID? > > It may be a risky operation, as you say and I might decide not to do pursue it > in the end, but first I must understand (-: > > Regards, > - Marcelo -- Adrian Klaver adrian.klaver@aklaver.com