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 1ssu3J-00BozU-5H for pgsql-bugs@arkaria.postgresql.org; Tue, 24 Sep 2024 01:09:18 +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 1ssu3I-00Hb1f-4T for pgsql-bugs@arkaria.postgresql.org; Tue, 24 Sep 2024 01:09:16 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ssu3G-00Haz3-SQ for pgsql-bugs@lists.postgresql.org; Tue, 24 Sep 2024 01:09:15 +0000 Received: from fout-a5-smtp.messagingengine.com ([103.168.172.148]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ssu3B-000kc7-3A for pgsql-bugs@lists.postgresql.org; Tue, 24 Sep 2024 01:09:14 +0000 Received: from phl-compute-12.internal (phl-compute-12.phl.internal [10.202.2.52]) by mailfout.phl.internal (Postfix) with ESMTP id C91471380465; Mon, 23 Sep 2024 21:09:07 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-12.internal (MEProxy); Mon, 23 Sep 2024 21:09:07 -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:subject :subject:to:to; s=fm2; t=1727140147; x=1727226547; bh=grWz4XAVT8 oLIOpJnUeBY/FPaB6KSgSHX0raC56ewyw=; b=xWHh/Qw68PDmpDsC1WHl6UlWyv Ut7GHgeb0CHW44E4bVUufH9JC0hd/wqhkc1I0LlWFvvcIgicmrYqndsqmr07OdLb j/pX3OUOBkb2PYEoLsU7gQRK0UL8jcbI84IBb8cUxrPu9GZv/uMpiyH2FdOZq7Yx 1SeU1LojFMRIgsQfnf/3iiL/0JhAZyKxrU4WdpqZyovNg6OvBIRtNKWwNOO/HOPZ sLEsn1zrr/gC3Um+hEyvIKPrSw6/7Ibo01Xis/VMzOjfuxhRBrFn6Ci4a/dWg+BZ wmYPCJcMzB0JBHmGecoy9+El0opoxMfcWSv1ZzRn8d1OcbUa9U39uhJDiqSw== 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:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727140147; x=1727226547; bh=grWz4XAVT8oLIOpJnUeBY/FPaB6K SgSHX0raC56ewyw=; b=XdN+h6ULKP/T6i/M32189K4/48EwHz5abSv87mXYw/nT 2JpILSwQDFb33aIo+a6Zv5BvWF0CZCa+AiNS2eq05+x7v/R/VKAAV6346LktYwOA b4MBWv+dxaoRInq0zbJ/PBr8dtVQYfvkUjDCChqFD/497eyZ/Z2JW/tAGY7slIhZ rQSh0p5dKE1Vi4rnsJlIL3w8zgkMJoRZHlr+q3AhGmGY5C5Xxoiar+RGfa9y1jgq nSLD1CbxFZbh4Fr/Q7YT7x2DzpZhHfkOlDEs7Ygfo9zo67vQV32q2kDAnreKC3gz O4LO4FVwFKnxEpv7iEl5H4XHZ2dZXtQcT3L6cXm1nQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvddtuddgudduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnegfrhhlucfvnfffucdljedtmdenucfjughrpeffhffvvefukfhf gggtuggjsehgtderredttddvnecuhfhrohhmpefoihgthhgrvghlucfrrghquhhivghruc eomhhitghhrggvlhesphgrqhhuihgvrhdrgiihiieqnecuggftrfgrthhtvghrnhepteel ieefudffhffhtdetleeggeegfffhkeeuveetiefgudduvedutefggeeivdejnecuvehluh hsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepmhhitghhrggvlhes phgrqhhuihgvrhdrgiihiidpnhgspghrtghpthhtohepkedpmhhouggvpehsmhhtphhouh htpdhrtghpthhtohepgihofhesthhhvggsuhhilhgurdgtohhmpdhrtghpthhtohepnhgr thhhrghnuggsohhsshgrrhhtsehgmhgrihhlrdgtohhmpdhrtghpthhtohepphhoshhtgh hrvghsvhgrihgshhgrvhgvsehgmhgrihhlrdgtohhmpdhrtghpthhtohepthhglhesshhs shdrphhghhdrphgrrdhushdprhgtphhtthhopehmrghsrghordhfuhhjihhisehoshhsrd hnthhtuggrthgrrdgtohhmpdhrtghpthhtohepnhhorghhsehlvggruggsohgrthdrtgho mhdprhgtphhtthhopehpghhsqhhlqdgsuhhgsheslhhishhtshdrphhoshhtghhrvghsqh hlrdhorhhgpdhrtghpthhtohepvhhsvghkrghrsehmihgtrhhoshhofhhtrdgtohhm X-ME-Proxy: Feedback-ID: i0fe9450f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 23 Sep 2024 21:09:03 -0400 (EDT) Date: Tue, 24 Sep 2024 10:08:43 +0900 From: Michael Paquier To: Christophe Pettus Cc: Nathan Bossart , vaibhave postgres , Tom Lane , Fujii Masao , Noah Misch , pgsql-bugs@lists.postgresql.org, vsekar@microsoft.com Subject: Re: vacuumdb: permission denied for schema "pg_temp_7" Message-ID: References: <20240920190731.37.nmisch@google.com> <20240920215030.8d.nmisch@google.com> <2603142.1726873660@sss.pgh.pa.us> <5f37ee56-14fb-44fe-9150-9eb97e10538b@oss.nttdata.com> <2649165.1726898595@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="Fdh9MWMOUSwLFW/E" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Fdh9MWMOUSwLFW/E Content-Type: multipart/mixed; boundary="d4nbwX7/OWSDUVul" Content-Disposition: inline --d4nbwX7/OWSDUVul Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Mon, Sep 23, 2024 at 11:48:21AM -0700, Christophe Pettus wrote: > I'm happy to pick it up iff the current patch submitter doesn't want > to continue with it. Somewhat missed this thread, thanks for the latest activity. If we apply a restriction on the temporary persistence, then we know that vacuumdb will always have a WHERE clause so we can simplify the code and remove the business with has_where like in the attached. About the permission restrictions depending on the objects listed, the filtering query uses currently a list of VALUES in a CTE. Perhaps it would be more elegant to switch that to a SELECT with some has_schema_privilege() for the cases where OBJFILTER_SCHEMA is used? There permission checks with USAGE and MAINTAIN are broader, so I'd choose to add a skip on the temp persistence first and backpatch it down to 12 as there is also a performance argument. Then tackle the rest by reworking the VALUES part in the CTE. The REINDEX one is really something that we need to care about? One needs database ownership for a database-level REINDEX, or schema-level ownership for the schema-level one. Stricter restrictions apply compared to vacuum_rel(). Side note: we could use more CppAsString2() for relpersistence in src/bin/, like in pg_amcheck, if we go this way. -- Michael --d4nbwX7/OWSDUVul Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v2-0001-vacuumdb-Skip-temporary-tables-in-filtering-query.patch" Content-Transfer-Encoding: quoted-printable =46rom a234eb7a0ad24a9d5641168346ce608515f484c2 Mon Sep 17 00:00:00 2001 =46rom: Michael Paquier Date: Tue, 24 Sep 2024 10:00:29 +0900 Subject: [PATCH v2] vacuumdb: Skip temporary tables in filtering query Running vacuumdb with a non-superuser when another user has created a temporary table would lead to a mid-flight failure. There are other issues related to schema-level USAGE, which will be tackled later. Author: VaibhaveS Discussion: https://postgr.es/m/CAM_eQjwfAR=3Dy3G1fGyS1U9FTmc+FyJm9amNfY2QC= ZBnDDbNPZg@mail.gmail.com Backpatch-through: 12 --- src/bin/scripts/vacuumdb.c | 40 ++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/bin/scripts/vacuumdb.c b/src/bin/scripts/vacuumdb.c index 7c33e13e1a..249622068a 100644 --- a/src/bin/scripts/vacuumdb.c +++ b/src/bin/scripts/vacuumdb.c @@ -491,7 +491,6 @@ vacuum_one_database(ConnParams *cparams, int ntups; bool failed =3D false; bool objects_listed =3D false; - bool has_where =3D false; const char *initcmd; const char *stage_commands[] =3D { "SET default_statistics_target=3D1; SET vacuum_cost_delay=3D0;", @@ -665,7 +664,10 @@ vacuum_one_database(ConnParams *cparams, " LEFT JOIN pg_catalog.pg_class t" " ON c.reltoastrelid OPERATOR(pg_catalog.=3D) t.oid\n"); =20 - /* Used to match the tables or schemas listed by the user */ + /* + * Used to match the tables or schemas listed by the user, completing the + * JOIN clause. + */ if (objects_listed) { appendPQExpBufferStr(&catalog_query, " LEFT JOIN listed_objects" @@ -676,14 +678,26 @@ vacuum_one_database(ConnParams *cparams, appendPQExpBufferStr(&catalog_query, "c.oid\n"); else appendPQExpBufferStr(&catalog_query, "ns.oid\n"); + } =20 + /* + * Exclude temporary tables, beginning the WHERE clause. + */ + appendPQExpBufferStr(&catalog_query, + " WHERE c.relpersistence !=3D " CppAsString2(RELPERSISTENCE_TEMP)); + + /* + * Used to match the tables or schemas listed by the user, for the WHERE + * clause. + */ + if (objects_listed) + { if (objfilter & OBJFILTER_SCHEMA_EXCLUDE) appendPQExpBuffer(&catalog_query, - " WHERE listed_objects.object_oid IS NULL\n"); + " AND listed_objects.object_oid IS NULL\n"); else appendPQExpBuffer(&catalog_query, - " WHERE listed_objects.object_oid IS NOT NULL\n"); - has_where =3D true; + " AND listed_objects.object_oid IS NOT NULL\n"); } =20 /* @@ -695,11 +709,9 @@ vacuum_one_database(ConnParams *cparams, if ((objfilter & OBJFILTER_TABLE) =3D=3D 0) { appendPQExpBuffer(&catalog_query, - " %s c.relkind OPERATOR(pg_catalog.=3D) ANY (array[" + " AND c.relkind OPERATOR(pg_catalog.=3D) ANY (array[" CppAsString2(RELKIND_RELATION) ", " - CppAsString2(RELKIND_MATVIEW) "])\n", - has_where ? "AND" : "WHERE"); - has_where =3D true; + CppAsString2(RELKIND_MATVIEW) "])\n"); } =20 /* @@ -712,25 +724,23 @@ vacuum_one_database(ConnParams *cparams, if (vacopts->min_xid_age !=3D 0) { appendPQExpBuffer(&catalog_query, - " %s GREATEST(pg_catalog.age(c.relfrozenxid)," + " AND GREATEST(pg_catalog.age(c.relfrozenxid)," " pg_catalog.age(t.relfrozenxid)) " " OPERATOR(pg_catalog.>=3D) '%d'::pg_catalog.int4\n" " AND c.relfrozenxid OPERATOR(pg_catalog.!=3D)" " '0'::pg_catalog.xid\n", - has_where ? "AND" : "WHERE", vacopts->min_xid_age); - has_where =3D true; + vacopts->min_xid_age); } =20 if (vacopts->min_mxid_age !=3D 0) { appendPQExpBuffer(&catalog_query, - " %s GREATEST(pg_catalog.mxid_age(c.relminmxid)," + " AND GREATEST(pg_catalog.mxid_age(c.relminmxid)," " pg_catalog.mxid_age(t.relminmxid)) OPERATOR(pg_catalog.>=3D)" " '%d'::pg_catalog.int4\n" " AND c.relminmxid OPERATOR(pg_catalog.!=3D)" " '0'::pg_catalog.xid\n", - has_where ? "AND" : "WHERE", vacopts->min_mxid_age); - has_where =3D true; + vacopts->min_mxid_age); } =20 /* --=20 2.45.2 --d4nbwX7/OWSDUVul-- --Fdh9MWMOUSwLFW/E Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmbyERsACgkQnvQgOdby QH02Ww/+Otwswoicguiy8MhfkWINN/tK8koavsN2nXjvi3haKRsBjjuvtrMxsOT7 xwVugJWqkdGDoc7KkKbvi/rXg8EIGl1GaZAsGds8VqmE7V765XKjZbL2mL3o9AgF 79VZ2Sqg72fnhXMD/PiF9/VJqpXrYrbyigOFsKjLkUEtNZYr1gEdroYvcO75ley2 rAoJil/1ZgRp/O1b2TlHIBOuimvfDkiNWMRq49iL+QhSa7i5fEus3/8/5bL4EgDn w0WKz6yDk9t43hhI5e2AINvG+wvDYs0FhzVotPkejy90vMve/a6+Fg5Po892Iygp FZKA8UhkfZD4cFsCqtaiVXPMcUMmS9OpL0puGI4grX5iHZzlPqJA2Otee6N09rqI hDu69lGxu0DjA28K4DirjEBfhB4mT3r41sYbQZgYPrRTC1/kOzRrJ/QJBFTaqWZS mT8gLDeKRm8h1NJp2hDihyEYy5pG0gb66QYfKottiyEpdPTfD1Ek5yN089BTS6E9 zJ4oJsRNuB6S52mK4FgoxncQQjH/BjNXi9bdJ/l8h2ddS0rtIdwMoTp7b575ueeo lG+ImsUstLfqFkEcZ+FGezTw3plyQ6a0Z2eLlrb4wX6xh/a9iB00OwtLFXAo9T1B 07KR/m+1YjBnvqQ6h8cA6SUWGwDRR1fK1oyDjEHaIYjmGfn14QI= =AhGb -----END PGP SIGNATURE----- --Fdh9MWMOUSwLFW/E--