Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ippEv-0003OI-PA for pgsql-hackers@arkaria.postgresql.org; Fri, 10 Jan 2020 08:01:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ippEt-00038u-J6 for pgsql-hackers@arkaria.postgresql.org; Fri, 10 Jan 2020 08:01:51 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ippEr-00038n-Q2 for pgsql-hackers@lists.postgresql.org; Fri, 10 Jan 2020 08:01:51 +0000 Received: from [66.111.4.25] (helo=out1-smtp.messagingengine.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ippEk-0003Kw-2X for pgsql-hackers@lists.postgresql.org; Fri, 10 Jan 2020 08:01:49 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 2B17121EC3; Fri, 10 Jan 2020 03:01:30 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Fri, 10 Jan 2020 03:01:30 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= date:from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm3; bh=NTg9QW+qhOcgJsAI9KXMX/Wl9CW N2KH5z2iH197XDGY=; b=OV6QZ938+nnbQ+s0RwxuRTQ60C76/G4TPE2UWeYCojL f6t4IEip+7wl/S7pe8o8bq40mRMRdxX6udSvyryyJcNbIFctx3Fs9mJGwVE/tyJe l9FP7fXJd8FqxhGG5rtABBoaJQMSZkZkxArQzqwveEVUgTCa1rivJeg8/3Babsbx 5DdDm2RPGvcDOcB8pU7UVX310TO5Gv3jS87+yRMmBD8o9120qok5LQc2U4B5DkrV wR7S6aEtBUDQ2G/MC7BNgtg4ma1p+RzXV26efeOQmg+gJmGEKFHX//RAs/WuQqaK EE06lheRwkcGALrj6RAlq5jG616pQuvAmYuYOuHkRLg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=NTg9QW +qhOcgJsAI9KXMX/Wl9CWN2KH5z2iH197XDGY=; b=VLvQ+XYeNA365Kudm0nUwD s2JXWPv4Z7Kfqcg4aP4Q9JD2gvBIupIDLa64QKFI8xMGO6vXoc9Hh5jn0JT/Bo8H g2cUFW6LPFh1yP4nrSXkfCkCBKRjphRD0C1POr2sdYnunytKNksdX1f5G6tter9D nVv3pUmGMJZLwucs/+swV2hhekrwpi0GzoyR0E1Y94okL8wGempqjW8f863obNRh csBv6ZRsFyYYmLIeM9y5WQiDe8jYx1TEubBsD/8qiYHiptR2J4IDUegp6XFOVMkF aR0eywneI1ilCu6BrLdr94yISaRRhODUSZRb5ls22rHQVCTeLMYY57yzdW7E7qfQ == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrvdeivddguddtlecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enfghrlhcuvffnffculdejtddmnecujfgurhepfffhvffukfhfgggtuggjsehgtderredt tddvnecuhfhrohhmpefoihgthhgrvghlucfrrghquhhivghruceomhhitghhrggvlhesph grqhhuihgvrhdrgiihiieqnecukfhppeduuddurddutddvrddukedtrddukeehnecurfgr rhgrmhepmhgrihhlfhhrohhmpehmihgthhgrvghlsehprghquhhivghrrdighiiinecuve hluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from paquier.xyz (ee0822lan1.rev.em-net.ne.jp [111.102.180.185]) by mail.messagingengine.com (Postfix) with ESMTPA id DF98330607CD; Fri, 10 Jan 2020 03:01:27 -0500 (EST) Date: Fri, 10 Jan 2020 17:01:25 +0900 From: Michael Paquier To: Mahendra Singh Thalor Cc: Tom Lane , Robert Haas , Kyotaro Horiguchi , PostgreSQL Hackers Subject: Re: Assert failure due to "drop schema pg_temp_3 cascade" for temporary tables and \d+ is not showing any info after drooping temp table schema Message-ID: <20200110080125.GA250447@paquier.xyz> References: <31319.1578332027@sss.pgh.pa.us> <20200107002216.GA2386@paquier.xyz> <14868.1578420368@sss.pgh.pa.us> <20200108004409.GA3413@paquier.xyz> <5602.1578444917@sss.pgh.pa.us> <20200108015601.GB3413@paquier.xyz> <20200109040612.GG2251@paquier.xyz> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="5I6of5zJg18YgZEa" Content-Disposition: inline In-Reply-To: X-Host-Lookup-Failed: Reverse DNS lookup failed for 66.111.4.25 (deferred) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --5I6of5zJg18YgZEa Content-Type: multipart/mixed; boundary="DocE+STaALJfprDB" Content-Disposition: inline --DocE+STaALJfprDB Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Fri, Jan 10, 2020 at 11:56:37AM +0530, Mahendra Singh Thalor wrote: > I reviewed and tested the patch. After applying patch, I am getting other > assert failure. > > I think, before committing 1st patch, we should fix this crash also and > then we should commit all the patches. I have somewhat managed to break my environment for a couple of days so as I got zero testing done with assertions, so I missed this one. Thanks for the lookup! The environment is fixed since. This code path uses an assertion that would become incorrect once you are able to create in pg_class temporary relations which rely on a temporary schema that does not exist anymore, because its schema has been dropped it, and that's what you are doing. The assertion does not concern only autovacuum originally, as it would fail each time a session tries to build a relation descriptor for its cache with a relation using a non-existing namespace. I have not really dug if that's actually possible to trigger.. Anyway. So, on the one hand, saying that we allow orphaned temporary relations to be dropped even if their schema does not exist is what autovacuum does now more aggressively, so that can help to avoid having to clean up yourself orphaned entries from catalogs, following up with their toast entries, etc. And this approach makes the assertion lose its meaning for autovacuum. On the other hand keeping this assertion makes sure that we never try to load incorrect relcache entries, and just make autovacuum less aggressive by ignoring orphaned entries with incorrect namespace references, though the user experience in fixing the cluster means manual manipulation of the catalogs. This is something I understood we'd like to avoid as much as possible, while keeping autovacuum aggressive on the removal as that can ease the life of people fixing a cluster. So this would bring us back to a point intermediate of 246a6c8. This makes me wonder how much we should try to outsmart somebody which puts the catalogs in such a inconsistent state. Hmm. Perhaps at the end autovacuum should just ignore such entries and just don't help the user at all as this also comes with its own issues with the storage level as well as smgr.c uses rd_backend. And if the user plays with temporary namespaces like that with superuser rights, he likely knows what he is doing. Perhaps not :D, in which case autovacuum may not be the best thing to decide that. I still think we should make the log of autovacuum.c for orphaned relations more careful with its coding though, and fix it with the previous patch. The documentation of isTempNamespaceInUse() could gain in clarity, just a nit from me while looking at the surroundings. And actually I found an issue with its logic, as the routine would not consider a temp namespace in use for a session's own MyBackendId. As that's only used for autovacuum, this has no consequence, but let's be correct in hte long run. And this gives the attached after a closer lookup. Thoughts? -- Michael --DocE+STaALJfprDB Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="drop-temp-schema-adjust-v4.patch" Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/catalog/namespace.c b/src/backend/catalog/namespac= e.c index c82f9fc4b5..0987986a8f 100644 --- a/src/backend/catalog/namespace.c +++ b/src/backend/catalog/namespace.c @@ -3235,10 +3235,14 @@ isTempNamespaceInUse(Oid namespaceId) =20 backendId =3D GetTempNamespaceBackendId(namespaceId); =20 - if (backendId =3D=3D InvalidBackendId || - backendId =3D=3D MyBackendId) + /* No such temporary namespace? */ + if (backendId =3D=3D InvalidBackendId) return false; =20 + /* Is the namespace used by this backend? */ + if (backendId =3D=3D MyBackendId) + return true; + /* Is the backend alive? */ proc =3D BackendIdGetProc(backendId); if (proc =3D=3D NULL) diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/a= utovacuum.c index f0e40e36af..d8702d650a 100644 --- a/src/backend/postmaster/autovacuum.c +++ b/src/backend/postmaster/autovacuum.c @@ -2071,9 +2071,11 @@ do_autovacuum(void) { /* * We just ignore it if the owning backend is still active and - * using the temporary schema. + * using the temporary schema. If the namespace does not exist + * ignore the entry. */ - if (!isTempNamespaceInUse(classForm->relnamespace)) + if (!isTempNamespaceInUse(classForm->relnamespace) && + get_namespace_name(classForm->relnamespace) !=3D NULL) { /* * The table seems to be orphaned -- although it might be that @@ -2202,6 +2204,7 @@ do_autovacuum(void) Oid relid =3D lfirst_oid(cell); Form_pg_class classForm; ObjectAddress object; + char *nspname; =20 /* * Check for user-requested abort. @@ -2249,12 +2252,18 @@ do_autovacuum(void) continue; } =20 - /* OK, let's delete it */ - ereport(LOG, - (errmsg("autovacuum: dropping orphan temp table \"%s.%s.%s\"", - get_database_name(MyDatabaseId), - get_namespace_name(classForm->relnamespace), - NameStr(classForm->relname)))); + nspname =3D get_namespace_name(classForm->relnamespace); + + if (nspname !=3D NULL) + ereport(LOG, + (errmsg("autovacuum: dropping orphan temp table \"%s.%s.%s\"", + get_database_name(MyDatabaseId), + nspname, NameStr(classForm->relname)))); + else + ereport(LOG, + (errmsg("autovacuum: dropping orphan temp table \"%s\" with OID %u in= database \"%s\"", + NameStr(classForm->relname), relid, + get_database_name(MyDatabaseId)))); =20 object.classId =3D RelationRelationId; object.objectId =3D relid; --DocE+STaALJfprDB-- --5I6of5zJg18YgZEa Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAl4YL1UACgkQnvQgOdby QH3bxA//dkVY5dHe5VZft8xKp3nJd6hzGX5EnobC35yP4KDv8YMpjwe8mc+e/yz5 aAG2eKxo/JZi1JI8KWE8fkHLliXZdRWokMwJlZ5XIjZEKqM/OMheqKnBhj/VZ2yC WWfVezhkOPSO4ejpqGiBZYCEoijB3ZXWoyH/eArSx9UxJD2HfTIyDyXitADh7TBX WrxI1eB2kb6zTFXJNtnI46Q/O9uwgeyq0fA7bMuz0f78yaq2T0sZz4arzRBLEZAj IbQME45MbIl6aiVYuDPVs6pMl+EMGUOfM+X9z17ov24+bPqDF9G4/dF3DSl5xRR9 4TXWFaWpmhZeXmUeKCIKF9oxWzoE69ExozdFz3uWPaqlFjNPUq9bx+k/MtJuk39m UGCS6v4KuGEQpTPlyXehFKPj3rYNWqgLCNJjd75g8aVmTyMByU2Es867eiOYlcpt N/+tkNdLFoFDGxTHjTvjZZsfDx5cWzXf/u1sNZN4B9U+8JpAl3L26/I7JZTdKbvB VDt+1l2yjZyoI2w3syLEkvtLpRKLc17mJZ1+bmv6xatMx/ini9dwsM8KMN9f+szx BKDr6V1xXtLeq1EDgtBzOJJXA6ApkFrD8GU5XXYFswvFON3VsJ1TbmGFZ7OSwfQM GDe3+0VLc4hSUqhn0yTBHFWggWRFs/M4aZPYcFXHOkK0Xju2Urs= =vfI4 -----END PGP SIGNATURE----- --5I6of5zJg18YgZEa--