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 1tiiIK-001TLE-5V for pgsql-general@arkaria.postgresql.org; Thu, 13 Feb 2025 23:06:56 +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 1tiiII-00FwsL-2W for pgsql-general@arkaria.postgresql.org; Thu, 13 Feb 2025 23:06:54 +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 1tiiIH-00Fwp5-Nt for pgsql-general@lists.postgresql.org; Thu, 13 Feb 2025 23:06:54 +0000 Received: from mail-io1-xd41.google.com ([2607:f8b0:4864:20::d41]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tiiIG-000fDh-1y for pgsql-general@lists.postgresql.org; Thu, 13 Feb 2025 23:06:53 +0000 Received: by mail-io1-xd41.google.com with SMTP id ca18e2360f4ac-8551fae84a1so35246639f.0 for ; Thu, 13 Feb 2025 15:06:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1739488012; x=1740092812; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=MjmFXcpjTEaykh0TufI6aDttJeFBrzzehEbbeVelfiU=; b=WWVUHaZ3Tb+PaFr2axtlcvVWmzwnpZWxh1zfXxsHnLXGH6JwHksZsJjP4CaW6xoCcc hkvZxxS1CQ1x3Fa8B+lh8bkfFc/Wk1/l0xzuVigC1SFl5NwgrEOvNxb0p5u+zoB2EU+t il3drUArOSKNipASS2e2cY6y79OgQu+5dLi3tkPkKQ/ubjwp2UNAP0zLj8hx8CvJSOdw xr567FnIAWgIaK4hHP8R/WLomJuPZM3jYnu3M/AxfaFKmkZHFPIx+9eoLM+B89+32SRO TQo43QnMCFcY8TsgFUhO2bWbZyaBsy7j7n9tfOFVAb6SQZNt/eifkE8r4QhUy6WaEOgl am0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739488012; x=1740092812; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MjmFXcpjTEaykh0TufI6aDttJeFBrzzehEbbeVelfiU=; b=VPkWGR/uR4nue+EuG7BKNDfhzunfkFIO4hr9NRTNCalSKnolB6OnqIuT3Xb0809H09 1O9cs9YtCkZBD1h0Bapn4KplubyT9IlFOM+A8mgavebSbv7OpRvb8bkuh6jXWVGRNAR+ 12vW/YXCTWzvilVgxLpL7r0PVPe2veYw21uxatPwECaYpWw8g7s1xvkeyfXRDdvs07lA PIIkvIHqzJWldzIVSOoY4mKY5ZLEisc++1Jnva18aKimV2TogYA7jMDenHOIeBnJXyaq I8xGyerwKkiAUCXowg/1ZtvfV0HCNz8VqVzEoApZ+r/GnPE2C35cVv8oZlB87QfjfeH2 rJ/A== X-Forwarded-Encrypted: i=1; AJvYcCXwcGYIWJL7vc50zjjWqhvyP0M0ryynuV5Akplor0EwvCp8dm9lMy3lcG0fYqVYHowN/VOBx87PvDx/F39n@lists.postgresql.org X-Gm-Message-State: AOJu0Yyn3RslbGdGBsxN3orEtQRsCgDirtzka8UnAPYujmuBaUny7pdz l3Rh5kAAOipcSIZwz0pSjOAfLczq0/c21QM7wjV26Qy+5Q0vBkemiW8D5crJkAzwFD6xrKPvgyO L1u795qmRDNoYBSRr3d1D91NMSDo= X-Gm-Gg: ASbGncvYt7R2TZ4VP8ttjAYIIUnDfpiqtpy6hDWfRs13Jdo3lIJEI2hs1Obx7YrS0uo H++vrYQlG7Q0MMmGIhg+7hih/EhIqlgPLA3s0Lrdcbu837W/7TGW9Cddxvx4YNsAxQOjKBWmR X-Google-Smtp-Source: AGHT+IGvs37n531d617GiMmc6+caEgyATqWKjXVMvXkcUo1VZoPCZ8U6oEGVLuPLwUE46W13NzSOqVkL9PIo5p17ero= X-Received: by 2002:a05:6602:2c95:b0:855:6d7a:1820 with SMTP id ca18e2360f4ac-8556d7a1ebdmr304752939f.4.1739488011854; Thu, 13 Feb 2025 15:06:51 -0800 (PST) MIME-Version: 1.0 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> In-Reply-To: From: Marcelo Fernandes Date: Fri, 14 Feb 2025 12:06:40 +1300 X-Gm-Features: AWEUYZle9J3gAIKlO9eCKBFLNMjOmtXT-C-nzbpssWIWn-s5xIcMJm__5O4Lg0w Message-ID: Subject: Re: Best Approach for Swapping a Table with its Copy To: Adrian Klaver Cc: Dominique Devienne , Greg Sabino Mullane , pgsql-general@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Feb 14, 2025 at 4:09=E2=80=AFAM Greg Sabino Mullane wrote: > It is surgically replacing all pointers to the old data with pointers to = the new data. Yes, with lots of system catalog shenanigans. Love your analogy Greg, thanks for that. > It's technically possible to do something similar for your use case, but = it's not trivial. All the cab to trailer wires must be precisely changed. E= verything directly related to the data must be swapped: heap, indexes, toas= t. I'd really appreciate to know more about how I can do this, as I think this= is the crux of what I am trying to solve. If you have pointers, thoughts, or resources where I can better understand what's involved, that would be much appreciated. In terms of where I am at currently, I summarised in my previous reply: On Fri, Feb 14, 2025 at 11:59=E2=80=AFAM Marcelo Fernandes wrote: > 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 to= ast > 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 =3D > TOAST_X.indrelid AND TOAST_X.indisvalid > WHERE X.oid =3D ('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 cas= e I > need to rollback). > 5. Commit > > Step 4 is what I am stuck on. What bits of the catalogue do I need to cha= nge? > And for what reasons? > > It may be a risky operation, as you say and I might decide not to do purs= ue it > in the end, but first I must understand (-: Regards, - Marcelo