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.96) (envelope-from ) id 1wa4Vg-001VmZ-2l for pgsql-hackers@arkaria.postgresql.org; Thu, 18 Jun 2026 04:37:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wa4Ve-009NxY-2R for pgsql-hackers@arkaria.postgresql.org; Thu, 18 Jun 2026 04:37:46 +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.96) (envelope-from ) id 1wa4Vd-009NxQ-2T for pgsql-hackers@lists.postgresql.org; Thu, 18 Jun 2026 04:37:46 +0000 Received: from fhigh-a7-smtp.messagingengine.com ([103.168.172.158]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wa4Va-0000000126E-3Pxb for pgsql-hackers@postgresql.org; Thu, 18 Jun 2026 04:37:45 +0000 Received: from phl-compute-04.internal (phl-compute-04.internal [10.202.2.44]) by mailfhigh.phl.internal (Postfix) with ESMTP id 8729414000AE; Thu, 18 Jun 2026 00:37:40 -0400 (EDT) Received: from phl-frontend-04 ([10.202.2.163]) by phl-compute-04.internal (MEProxy); Thu, 18 Jun 2026 00:37:40 -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=fm1; t=1781757460; x=1781843860; bh=qmJrxm71zp HgVtqWP1Ux5R4mfLUlrmdHN4suoN+5JYg=; b=bY1CUADmccjHfgBRMPUgXduyij rG4kzbU35VJ+qGMCwXCWgduPWHxbXW1Hs4XDh96cpbp8VohmVaTj+TW7XBP7Bx9N 4J3zirhdqgXZyl+Ty9BVUeoEMchzWEiHzf1uF+8XqZlkzM1tJYxWCQmS0M9eF7km smFjqQ+BOl60ZBDwpOF0VyHYEGzr6AkvjDbgv5rKBy7q42ZS1gDdcuZp4ww70cjB z0IJy6REbIwBC3RIphLZfd7B73s2thRWxCg612kbfyulI9P2lopXLL8l6IRMvz3i xiIjWHD+X5IvVuxh+DM7HZSyj7Iggm0nxybi8Zm1OnfSAV8teik/7Hh7S0yA== 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-sender:x-me-sender:x-sasl-enc; s=fm1; t= 1781757460; x=1781843860; bh=qmJrxm71zpHgVtqWP1Ux5R4mfLUlrmdHN4s uoN+5JYg=; b=aDNfzjpPiVpekRpWuo9vAhzoEglAngPCvk0OoP7KH5fQpwr06Yo Azg3gRZ6EiE+5Y+2a1weitfI0E2EsaMMOLLLcUF/Tadj70h7Ux+w10Cn01kN77PS pfR/Hp2bM/W8rWGW65UFmnABQWeKibx63QM5SjM0239esJslGprr+Og0ACCZEW3+ g+7h8pWp8wF3ZEsYJGwThO4qhzPAytZNgZuKht+6tFSUVonWA/tlD0kot2stjXtq feaAZI92LJXA40g8UxGuV3qSe7ctz5OzUeIyiNmU/psAkr+RCycx13MxoL7IOzBz vanCSXiB2B9uWy4IB3Yg2sxMDMHCqV2i1VQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: dmFkZTE19O3nrqj8sH8FCHSARH4YXD+r9aEpw/AZBnIju9Pn8rBJQlgK1jfC+KDSz/A81o 1d0VxxozvohafQ2EgwCfiP5khcuCPTu0utuLyZ/7hb3I9t4yhwK7Kp4ilc5qQ5mg+11Vvq rbzkswcxZndXU+pKaJ7BugmMweOr6OAVEn1urLG3XjxZMSnVWre298ta9SW0WWudkUnEZ5 6gZKy6Eia/DbUfOIUTLJfXOWVzLbGjZEhC2NJY7nfRfBkefD86KnUOD2xsPPU0KphnsAnX RTvn/uLkKpCkP4eBuK1r9tqAFNdi+ev2Rn4f0KyKtOxvRo0cIrArMY03HIj7eehtavtTmk nTR+wgaRDaZ3IosXlYzVlcn21K2VBVdS21avbyhqlSB7461OhDHHjNgMo6QH8WKwZG5KWH ljSHE+AT1VoId6MSx/i+M1zD1s3IBMBSxo+4ru6Fn3RzPaQA+YdPYoFYTs8XNaNYz5Hlhv 4mIxORFCz6+7fgR8V+XRMBqY2V03XGIffnOsvb0hjLIajK2+Efrt089S0EXDi5KEswq0S5 k+wtPeZLLUEP85DttNJ2AE5yaUUUr6FHL+1c+so00rzXAW0n/kwdmJXwddMCrP5jyJtoU9 rwNEOasYDCvCY17drOj8FoWH8nhbPyRTPh1yjj2nyz3+GWSzNBmfQgPNxaEQ X-ME-Proxy: Feedback-ID: i0fe9450f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 18 Jun 2026 00:37:39 -0400 (EDT) Date: Thu, 18 Jun 2026 13:37:34 +0900 From: Michael Paquier To: Matthias van de Meent Cc: Alexander Lakhin , pgsql-hackers Subject: Re: Unexpected behavior after OOM errors Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="JtcwYP9qOAnnkIT/" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --JtcwYP9qOAnnkIT/ Content-Type: multipart/mixed; boundary="U1jz0ahZtayZ0HSJ" Content-Disposition: inline --U1jz0ahZtayZ0HSJ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Jun 17, 2026 at 02:27:25PM +0200, Matthias van de Meent wrote: > On Wed, 17 Jun 2026 at 08:00, Alexander Lakhin wrot= e: >> 1) An issue in lookup_type_cache() >=20 > I believe this is caused by partial subsystem initialization. Attached > patch 0001 should address this failure without causing the server to > restart on OOM. Hmm. I think that this is an ordering problem. We could make the callbacks be registered last, once we are sure that the two hash=20 tables and the in-progress list have been initialized. I am not sure that this requires a new facility; it is also an advantage to keep the initialization sequence in a one code path, without an abstraction. RelIdToTypeIdCacheHash and RelIdToTypeIdCacheHash are in the TopMemoryContext, static to the process, so we could just check them for NULL-ness to make the initialization repeatable. That gives me the attached v2. Reusing Alexander's randomness trick, that looks stable here. >> 2) An issue in GetSnapshotData() >=20 > Again, caused by partial initialization, though in this case it's of a > SnapshotData* which is later checked again. Attached patch 0002 should > address this failure. Yeah, that seems right to make repeated calls of GetSnapshotData() able to work. LGTM. >> 3) An issue in StandbyAcquireAccessExclusiveLock() > >=20 > I'm not sure how to solve this correctly; I think ideally the > StandbyAcquireAccessExclusiveLock() hash code would be wrapped by a > critical section, but I'm not 100% sure if that will be a sufficient > approach; and it'd definitely need some code to allow the various > hashmaps' memctxs to alloc during critical sections. Not checked this one yet. Thoughts about the first part? -- Michael --U1jz0ahZtayZ0HSJ Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v2-0001-typcache-Make-initialization-more-resilient-on-OO.patch Content-Transfer-Encoding: quoted-printable =46rom 8b2ecd4df82750ca98cf7e67244cb46b1e4239ed Mon Sep 17 00:00:00 2001 =46rom: Michael Paquier Date: Thu, 18 Jun 2026 13:24:47 +0900 Subject: [PATCH v2] typcache: Make initialization more resilient on OOM --- src/backend/utils/cache/typcache.c | 57 +++++++++++++++++------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/t= ypcache.c index da91a2ff1dd8..b96554a4b32e 100644 --- a/src/backend/utils/cache/typcache.c +++ b/src/backend/utils/cache/typcache.c @@ -392,50 +392,59 @@ lookup_type_cache(Oid type_id, int flags) bool found; int in_progress_offset; =20 - if (TypeCacheHash =3D=3D NULL) + if (in_progress_list =3D=3D NULL) { /* First time through: initialize the hash table */ HASHCTL ctl; int allocsize; =20 - ctl.keysize =3D sizeof(Oid); - ctl.entrysize =3D sizeof(TypeCacheEntry); + if (TypeCacheHash =3D=3D NULL) + { + ctl.keysize =3D sizeof(Oid); + ctl.entrysize =3D sizeof(TypeCacheEntry); =20 - /* - * TypeCacheEntry takes hash value from the system cache. For - * TypeCacheHash we use the same hash in order to speedup search by - * hash value. This is used by hash_seq_init_with_hash_value(). - */ - ctl.hash =3D type_cache_syshash; + /* + * TypeCacheEntry takes hash value from the system cache. For + * TypeCacheHash we use the same hash in order to speedup search + * by hash value. This is used by + * hash_seq_init_with_hash_value(). + */ + ctl.hash =3D type_cache_syshash; =20 - TypeCacheHash =3D hash_create("Type information cache", 64, - &ctl, HASH_ELEM | HASH_FUNCTION); + TypeCacheHash =3D hash_create("Type information cache", 64, + &ctl, HASH_ELEM | HASH_FUNCTION); + } =20 - Assert(RelIdToTypeIdCacheHash =3D=3D NULL); - - ctl.keysize =3D sizeof(Oid); - ctl.entrysize =3D sizeof(RelIdToTypeIdCacheEntry); - RelIdToTypeIdCacheHash =3D hash_create("Map from relid to OID of cached = composite type", 64, - &ctl, HASH_ELEM | HASH_BLOBS); - - /* Also set up callbacks for SI invalidations */ - CacheRegisterRelcacheCallback(TypeCacheRelCallback, (Datum) 0); - CacheRegisterSyscacheCallback(TYPEOID, TypeCacheTypCallback, (Datum) 0); - CacheRegisterSyscacheCallback(CLAOID, TypeCacheOpcCallback, (Datum) 0); - CacheRegisterSyscacheCallback(CONSTROID, TypeCacheConstrCallback, (Datum= ) 0); + if (RelIdToTypeIdCacheHash =3D=3D NULL) + { + ctl.keysize =3D sizeof(Oid); + ctl.entrysize =3D sizeof(RelIdToTypeIdCacheEntry); + RelIdToTypeIdCacheHash =3D hash_create("Map from relid to OID of cached= composite type", 64, + &ctl, HASH_ELEM | HASH_BLOBS); + } =20 /* Also make sure CacheMemoryContext exists */ if (!CacheMemoryContext) CreateCacheMemoryContext(); =20 /* - * reserve enough in_progress_list slots for many cases + * Reserve enough in_progress_list slots for many cases. This is + * the last allocation on purpose, done after the two others. */ allocsize =3D 4; in_progress_list =3D MemoryContextAlloc(CacheMemoryContext, allocsize * sizeof(*in_progress_list)); in_progress_list_maxlen =3D allocsize; + + /* + * Set up callbacks for SI invalidations. These are done last, + * once all the other initializations are done. + */ + CacheRegisterRelcacheCallback(TypeCacheRelCallback, (Datum) 0); + CacheRegisterSyscacheCallback(TYPEOID, TypeCacheTypCallback, (Datum) 0); + CacheRegisterSyscacheCallback(CLAOID, TypeCacheOpcCallback, (Datum) 0); + CacheRegisterSyscacheCallback(CONSTROID, TypeCacheConstrCallback, (Datum= ) 0); } =20 Assert(TypeCacheHash !=3D NULL && RelIdToTypeIdCacheHash !=3D NULL); --=20 2.54.0 --U1jz0ahZtayZ0HSJ-- --JtcwYP9qOAnnkIT/ Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmozdg4ACgkQnvQgOdby QH1wlhAAimgZ3R7/sc518EqORHtWpzj0hDw944iHjQE8cbOuu2My4D0+eJFThViY sb6jBhRbuZvSUzf9UWfgee+edohhLO8aK0GAq7RYtGJFoUzKKW50mZ1ThXJxITwV fQPSq9lFqFnmNSYBF687bx+u9CBjtJHn5xRZUnlWymNMl+m+9LA29CRv6bntq+0a +RK4+1Ska2pIYRDax4eUE0mLFJJezo5Za53m11nRYGYngamR0rGvMJXN5F7w9QeN i+Pll5nFlH5aDfgz4ihNklwUbXIVBcf+dTQuzZfDP2efVK7Uo8Xj8tRLJ4myqM9W ucg5O6RosjVNNuu8HkyTcOaHw0Q/iLHR0E+SaZTwNZ5OR0QCVG1unAzIPbJHZhQ9 Mw+Yfw3OkU4ZRPCw65/61pdeaXSSc1WEIEAqs5icvKOa2dxLHdZ+hfz6S+Je1UYZ 4ssl7DxSHs66Op81/QyF9K5/ZaUXLZp5PD9pC0CslzYHfDidYkRVx0mEhBlolgWj CzV4yhiiL/OvW4OK1qsiEADVTllf7ddGGYdMuIkpXh4LoRt/q5P5P5LfWZOZPOGO KLSt3vbqTZwX2rEFD7uby3KR9LZk5lBYs8OQarec7BSpGuWIUiOEtmto7hWkPXet sppZSGIIDti9QdtiyWj5n3PJdq/hHKPVH7mYLZNXrtlRnnvd0/k= =Y4Uv -----END PGP SIGNATURE----- --JtcwYP9qOAnnkIT/--