Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qEHr2-0001fc-JI for pgsql-bugs@arkaria.postgresql.org; Tue, 27 Jun 2023 23:12:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1qEHr1-0006Df-Es for pgsql-bugs@arkaria.postgresql.org; Tue, 27 Jun 2023 23:12:11 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qEHqz-0006DW-QY for pgsql-bugs@lists.postgresql.org; Tue, 27 Jun 2023 23:12:11 +0000 Received: from wout4-smtp.messagingengine.com ([64.147.123.20]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qEHqt-000rYz-CR for pgsql-bugs@lists.postgresql.org; Tue, 27 Jun 2023 23:12:09 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id E0EF1320092E; Tue, 27 Jun 2023 19:11:58 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Tue, 27 Jun 2023 19:11:59 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= cc:cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to; s=fm2; t=1687907518; x=1687993918; bh=3c BzHB9snq8sT+WnNglOMG8CtmdD+N5b7wbV53Ia19o=; b=XWG5oCSJYcPXx5xf8I oPn1LhJ5xlokwXrxDEHbj8Zt6reWN2MXo4zNhzIXv9hvHJ42Ca7wn52XcDftcZPY g39+6AZtHLnIBOmhU5xYoDSVdaaR1gu2hwl8mEdLvkWxyaNG688rCBE6Q6/85iYx /4UtO1fZu+FeH7CeIjFVHGhB6NVewdTqyuxyyCUd47quJEHWBdfZMey4UGCb+1KS OXwTO/xbb1WT6HJY3ODnlpkA2oAo7DeD5xLpBXLE33YWB+L40TZ2S2Kr4GSxpcKG F8fhoZm9u/L4HANqZ9gLNHGepNWo5FMjkNxVjpRHAPwg1u/18DeWUqMUBANPpBfF HO6w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc: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:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; t=1687907518; x=1687993918; bh=3cBzHB9snq8sT +WnNglOMG8CtmdD+N5b7wbV53Ia19o=; b=GM6akwwz6EYlmG5LiO/A4zbUkLpcd dj4+u9hhd5+dQ9qw0onnMRmqKJ6jwhYHs165cTulYE8WJqKSsryR29RakvSW5Ty2 6xvlUqzCH2FCQ8aq7jOQ2doCQa+172N5O3dMUjXmLvr5gH9niwN5X/TC47f31V8N 3Xg9HmCLxVZRzdYKCqxjfHnwJJPI6CTq18IKMH8OHoQahkPq0XhuKk6JAIjilwVJ 4RCx+mIJQhJa8Do1VCAIFK+eO2vX0MKgOsPQqIjAafLhcLEoLv51Wc9etZLFYt0J EOwcOrZFNuPgkrFw+deWmfkXPCWXyaMCVm4x1ocdgYuoXxyp4q062Htjw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrtddugddulecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenfg hrlhcuvffnffculdejtddmnecujfgurhepfffhvfevuffkfhggtggujgesghdtreertddt vdenucfhrhhomhepofhitghhrggvlhcurfgrqhhuihgvrhcuoehmihgthhgrvghlsehprg hquhhivghrrdighiiiqeenucggtffrrghtthgvrhhnpeetleeifedufffhhfdtteelgeeg geffhfekueevteeigfduudevudetgfegiedvjeenucevlhhushhtvghrufhiiigvpedtne curfgrrhgrmhepmhgrihhlfhhrohhmpehmihgthhgrvghlsehprghquhhivghrrdighiii X-ME-Proxy: Feedback-ID: i0fe9450f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 27 Jun 2023 19:11:56 -0400 (EDT) Date: Wed, 28 Jun 2023 08:11:51 +0900 From: Michael Paquier To: Andres Freund Cc: exclusion@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18000: Access method used by matview can be dropped leaving broken matview Message-ID: References: <18000-9145c25b1af475ca@postgresql.org> <20230627182332.uq6afduhvdip7pol@awork3.anarazel.de> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="3D3xTvw5EuFvTiVS" Content-Disposition: inline In-Reply-To: <20230627182332.uq6afduhvdip7pol@awork3.anarazel.de> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --3D3xTvw5EuFvTiVS Content-Type: multipart/mixed; boundary="PG4vCoX/zS2ujY8l" Content-Disposition: inline --PG4vCoX/zS2ujY8l Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, Jun 27, 2023 at 11:23:32AM -0700, Andres Freund wrote: > Should we check changeDependencyFor()'s return value? On a first glance it > looks like it would be an error to return 0 in this case? Agreed. I was wondering if this was worth having in this path (other code paths are rather lax about that), but yes let's do it as there should not be more than one record in pg_depend. -- Michael --PG4vCoX/zS2ujY8l Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v2-0001-Fix-dependencies-under-ALTER-TABLE-SET-ACCESS-MET.patch" Content-Transfer-Encoding: quoted-printable =46rom 6ef48042e1eefd30b16438c03e584b41733b3e49 Mon Sep 17 00:00:00 2001 =46rom: Michael Paquier Date: Wed, 28 Jun 2023 08:06:31 +0900 Subject: [PATCH v2] Fix dependencies under ALTER TABLE SET ACCESS METHOD --- src/backend/commands/cluster.c | 21 ++++++++++++++++++ src/test/regress/expected/create_am.out | 29 +++++++++++++++++++++++++ src/test/regress/sql/create_am.sql | 18 +++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c index 3bfabb6d10..e84460a43e 100644 --- a/src/backend/commands/cluster.c +++ b/src/backend/commands/cluster.c @@ -1070,6 +1070,8 @@ swap_relation_files(Oid r1, Oid r2, bool target_is_pg= _class, relfilenumber2; RelFileNumber swaptemp; char swptmpchr; + Oid relam1, + relam2; =20 /* We need writable copies of both pg_class tuples. */ relRelation =3D table_open(RelationRelationId, RowExclusiveLock); @@ -1086,6 +1088,8 @@ swap_relation_files(Oid r1, Oid r2, bool target_is_pg= _class, =20 relfilenumber1 =3D relform1->relfilenode; relfilenumber2 =3D relform2->relfilenode; + relam1 =3D relform1->relam; + relam2 =3D relform2->relam; =20 if (RelFileNumberIsValid(relfilenumber1) && RelFileNumberIsValid(relfilenumber2)) @@ -1257,6 +1261,23 @@ swap_relation_files(Oid r1, Oid r2, bool target_is_p= g_class, CacheInvalidateRelcacheByTuple(reltup2); } =20 + /* + * Now that pg_class has been updated with its relevant information for + * the swap, update the dependency of the relation to point to its new + * table AM, if it has changed. + */ + if (relam1 !=3D relam2) + { + if (changeDependencyFor(RelationRelationId, + r1, + AccessMethodRelationId, + relam1, + relam2) !=3D 1) + elog(ERROR, "failed to change access method dependency for relation \"%= s.%s\"", + get_namespace_name(get_rel_namespace(r1)), + get_rel_name(r1)); + } + /* * Post alter hook for modified relations. The change to r2 is always * internal, but r1 depends on the invocation context. diff --git a/src/test/regress/expected/create_am.out b/src/test/regress/exp= ected/create_am.out index e9a9283d7a..4eec292e65 100644 --- a/src/test/regress/expected/create_am.out +++ b/src/test/regress/expected/create_am.out @@ -240,6 +240,35 @@ SELECT amname FROM pg_class c, pg_am am heap (1 row) =20 +-- Switching to heap2 adds new dependency entry to the AM. +ALTER TABLE heaptable SET ACCESS METHOD heap2; +SELECT pg_describe_object(classid, objid, objsubid) as obj, + pg_describe_object(refclassid,refobjid,refobjsubid) as objref, + deptype + FROM pg_depend + WHERE classid =3D 'pg_class'::regclass AND + objid =3D 'heaptable'::regclass + ORDER BY 1, 2; + obj | objref | deptype=20 +-----------------+---------------------+--------- + table heaptable | access method heap2 | n + table heaptable | schema public | n +(2 rows) + +-- Switching to heap should not have a dependency entry to the AM. +ALTER TABLE heaptable SET ACCESS METHOD heap; +SELECT pg_describe_object(classid, objid, objsubid) as obj, + pg_describe_object(refclassid,refobjid,refobjsubid) as objref, + deptype + FROM pg_depend + WHERE classid =3D 'pg_class'::regclass AND + objid =3D 'heaptable'::regclass + ORDER BY 1, 2; + obj | objref | deptype=20 +-----------------+---------------+--------- + table heaptable | schema public | n +(1 row) + ALTER TABLE heaptable SET ACCESS METHOD heap2; SELECT amname FROM pg_class c, pg_am am WHERE c.relam =3D am.oid AND c.oid =3D 'heaptable'::regclass; diff --git a/src/test/regress/sql/create_am.sql b/src/test/regress/sql/crea= te_am.sql index 256884c959..f1c809b8e0 100644 --- a/src/test/regress/sql/create_am.sql +++ b/src/test/regress/sql/create_am.sql @@ -166,6 +166,24 @@ CREATE TABLE heaptable USING heap AS SELECT a, repeat(a::text, 100) FROM generate_series(1,9) AS a; SELECT amname FROM pg_class c, pg_am am WHERE c.relam =3D am.oid AND c.oid =3D 'heaptable'::regclass; +-- Switching to heap2 adds new dependency entry to the AM. +ALTER TABLE heaptable SET ACCESS METHOD heap2; +SELECT pg_describe_object(classid, objid, objsubid) as obj, + pg_describe_object(refclassid,refobjid,refobjsubid) as objref, + deptype + FROM pg_depend + WHERE classid =3D 'pg_class'::regclass AND + objid =3D 'heaptable'::regclass + ORDER BY 1, 2; +-- Switching to heap should not have a dependency entry to the AM. +ALTER TABLE heaptable SET ACCESS METHOD heap; +SELECT pg_describe_object(classid, objid, objsubid) as obj, + pg_describe_object(refclassid,refobjid,refobjsubid) as objref, + deptype + FROM pg_depend + WHERE classid =3D 'pg_class'::regclass AND + objid =3D 'heaptable'::regclass + ORDER BY 1, 2; ALTER TABLE heaptable SET ACCESS METHOD heap2; SELECT amname FROM pg_class c, pg_am am WHERE c.relam =3D am.oid AND c.oid =3D 'heaptable'::regclass; --=20 2.40.1 --PG4vCoX/zS2ujY8l-- --3D3xTvw5EuFvTiVS Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmSbbLcACgkQnvQgOdby QH2UFg//Tvm/yLx1rpOrrU684iVgcHeYvYas9/cF4pRPbgU5xgCoSb6u/rXzQU0Q 1znZio0gP7C7lKDHQTFrfI4D2/Zpm6OZqzrDRslOxZALxSRe2ho4CeZK7a/ath7C ZL9mWDtJio0e+PIVksgqpF1EAnc0GuyYS80xXV8srMScd00SY6iCSqkRYDF8nQBZ BybbyCTGw0NdiITis80/6dEm3kq3fKffcTv0HRTZYYFiClSD94G9LkE1qWl2lUMe xj+SOktlGUDNxMiG4lNC5k8GWLQxxJTLVktYRznhHHwgkv4NDQJdkCEbrdGzeN4D GeWnLRauxkOgB0vX6CqLT4C/5E5W3UodMNgHtLeQ1flaCMfA1zetwitsaLmvR3mG 2Eygusqe2fcKQ/Z/Nyy+kBWVWRz8SlVg3prBihjDcCP5vviGBwBCNyAFAmNToCuc lQ1oDgyVOUylAgX0jSgPOJ511ssT7TlCe/2ODjwGYKtShhGH3RGntGuOBCYVZejb 4Xdo77xoKWMamkFODYBIYU++74CuzqDwoSPDRwPrYebZWulbtBJ8jgUIfUnfoTWj 6LBcPcPHRg2aPwxRmQ8pV3mV660EDJqBCPxH8NRTJx7g2NoT6ClMXGjtlZruYJRa nHQTPfiMmO2E7tQd2ZCgHM0vTEGuTzQay11nvRbnJZ2NPFqCTW8= =unqF -----END PGP SIGNATURE----- --3D3xTvw5EuFvTiVS--