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 1tioRv-002aEA-9N for pgsql-general@arkaria.postgresql.org; Fri, 14 Feb 2025 05:41:15 +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 1tioRs-003Ix6-8P for pgsql-general@arkaria.postgresql.org; Fri, 14 Feb 2025 05:41:12 +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 1tioRr-003Iwx-FI for pgsql-general@lists.postgresql.org; Fri, 14 Feb 2025 05:41:12 +0000 Received: from mail-ed1-x536.google.com ([2a00:1450:4864:20::536]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tioRq-000i8f-0P for pgsql-general@lists.postgresql.org; Fri, 14 Feb 2025 05:41:10 +0000 Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-5deb1266031so2911267a12.2 for ; Thu, 13 Feb 2025 21:41:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1739511669; x=1740116469; darn=lists.postgresql.org; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=ANaes+JkBNVLxE2EvqHz+6rUxSayM+rwMS4TidpRtOA=; b=RVODp6wC8xxbd3dD/yj+7OxIKQY1Ks5NIVLJSUGno5rDWwGZDldMqIQZ7OYLe73QdK 4M9KBwOOB7pkAmObrq5+Lhlm0TMsRwS/h1+Vl/owzocIkpaYD5MAMoVn6S59IEYw9qU7 2+cRBtlHIu4Nm/jfS3b6+S8fLnxT0juN1Bcg84V1+VV+BcCh7ntqQ5kxmV0vvjh0hESM 12Wh6aMWSDAPRD5TJblJwm6yeDmYFF4sxtfnNx6HTWGHMEcYILcIaNjtwM2PT5kNoa4a zIFXdrNZpVb4uyJurdvM0T8d5nZK8m4ofEnxi28sLjHOB+2MqA83m3O2Z8hniHpe5pqU gEGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739511669; x=1740116469; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=ANaes+JkBNVLxE2EvqHz+6rUxSayM+rwMS4TidpRtOA=; b=ieCJoySyyEPyWydTwC5gZgGp9hMXslMqdoyUV9Gv0bvTKIyqWJYK1RPbkFCZ0pUuI4 jYVTgvzkuodRvr/FcMKVOhzitfagqrDlpckgYiummwSvufRTAz2LXCupiW9ST41KEMzy MaOVy46Uu6YOv0XvQ3WxZWtHDagUMLkHUnAbX2oNwd3yjc5OPKVQsvV5NqqcYhy8l40/ aQJKQd4PoGngzEY3tpsPhJOPBDxLeN/Hcj5xNelNAy/VRL243bbPVXuG2fPqriyCGolI 0YkSUA1KDLkddQRhdRwlev+F8DLLaGSZtPm+cTkTr4wVnhHmeHmh4xW4t2oFVU3manGp EIQA== X-Forwarded-Encrypted: i=1; AJvYcCXyVHrEXIglVtgBY/WQn196lL+E3w5qBTe9W8anTNDvGhk7UoAyKNlQF3oPD2r+S+CMHdRVQ8r6SEg0Ohj8@lists.postgresql.org X-Gm-Message-State: AOJu0YxuKY4+NzW19tOX8B8LPgklLpnIY0tIapRfUpyXtozWUFSrFkJU KqJ7FURrJOJVytrso8N23dxv3qdrupsB1EsJFgPM3Rx8o7pAW1IGFFqRWqYYGv1Hv5oGhIHUwYK cKUpnXzX7K4Z3C/+UP/Dsnf5xvXeo2atR9tWb3xSVSXmhcf7dT3WBo1FOtghR25zQ X-Gm-Gg: ASbGnctkHwLJ2/woMr9KHMz9oF9Z43MpT+yzLIjc1roFvIlmPsv7v2M8P08mKhHuKhp aRubmXGXB3rbXLkgNSDbo/iadWSii7Pf+4N4KaI5wRUDWGCqb7NEwu0WPz2R3btsUix9jfkIQI4 mxcq/WFSDqWSRp5gk+Fs22ZzF/EedroCa2NhFBseBqxhfhfIVf5mFFWzQFPt93tZ+3KZOVffGjE by6xfTSpru1vhUprZib5AV/IjK2SCfO3+I33c6qXoTfp8aLT4XK85VdXZkOp8TLs7L09g5XN/J7 IN+UQq7TFD2uabkzuiBnz6nuESFu26u+Ugf+ X-Google-Smtp-Source: AGHT+IElTsV1Zyte4BSntpladA45RwlweyXxMle6GIjokdwhMxMTC4Ra0TeU2Lzzb7RgmOhFCUn2vA== X-Received: by 2002:a05:6402:40c1:b0:5dc:1289:7f1c with SMTP id 4fb4d7f45d1cf-5deade0a57fmr10078796a12.29.1739511668643; Thu, 13 Feb 2025 21:41:08 -0800 (PST) Received: from localhost.localdomain ([2001:871:255:52d3:3ac8:61ab:b634:342]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5dece1c4483sm2326099a12.25.2025.02.13.21.41.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Feb 2025 21:41:08 -0800 (PST) Message-ID: Subject: Re: Best Approach for Swapping a Table with its Copy From: Laurenz Albe To: Marcelo Fernandes , Adrian Klaver Cc: Dominique Devienne , Greg Sabino Mullane , pgsql-general@lists.postgresql.org Date: Fri, 14 Feb 2025 06:41:07 +0100 In-Reply-To: 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> User-Agent: Evolution 3.54.3 (3.54.3-1.fc41) MIME-Version: 1.0 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, 2025-02-14 at 11:59 +1300, Marcelo Fernandes wrote: > On Thu, Feb 13, 2025 at 7:37=E2=80=AFPM Laurenz Albe wrote: > > 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? >=20 > Exactly! This is part of what I am trying to figure out (-: >=20 > The plan that I have in mind so far to swap a table safely is: >=20 > 1. Run the following query to grab information about the toast and the to= ast > index of the tables involved in the swapping: >=20 > 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; >=20 > 2. Open a transaction and acquire an access exclusive lock. > 3. Ensure that the cloned table has the same owner as the original table >=20 > ALTER TABLE copy_table OWNER TO owner_of_the_original_table; >=20 > 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 >=20 > Step 4 is what I am stuck on. What bits of the catalogue do I need to cha= nge? > And for what reasons? >=20 > 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 (-: In your steps, you carefully avoid the question of whether the foreign key = is valid or not. So you are trusting the user to have made sure that everythi= ng is fine with the foreign key... I may forget something, but I'd say that swapping out the files from under = a table's butt is just a question of updating the "pg_class.relfilenode" of t= he tables and the TOAST tables. But if you just UPDATE the catalogs, concurre= nt statements that are using the tables will be in trouble, so you need to tak= e an ACCESS EXCLUSIVE lock. Moreover, you have to make sure to send out invalidation messages so that every session that caches statistics or execution plans for the tables discards them. Yours, Laurenz Albe --=20 *E-Mail Disclaimer* Der Inhalt dieser E-Mail ist ausschliesslich fuer den=20 bezeichneten Adressaten bestimmt. Wenn Sie nicht der vorgesehene Adressat= =20 dieser E-Mail oder dessen Vertreter sein sollten, so beachten Sie bitte,=20 dass jede Form der Kenntnisnahme, Veroeffentlichung, Vervielfaeltigung oder= =20 Weitergabe des Inhalts dieser E-Mail unzulaessig ist. Wir bitten Sie, sich= =20 in diesem Fall mit dem Absender der E-Mail in Verbindung zu setzen. *CONFIDENTIALITY NOTICE & DISCLAIMER *This message and any attachment are=20 confidential and may be privileged or otherwise protected from disclosure= =20 and solely for the use of the person(s) or entity to whom it is intended.= =20 If you have received this message in error and are not the intended=20 recipient, please notify the sender immediately and delete this message and= =20 any attachment from your system. If you are not the intended recipient, be= =20 advised that any use of this message is prohibited and may be unlawful, and= =20 you must not copy this message or attachment or disclose the contents to=20 any other person.