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 1x3aid-006WqU-1Q for pgsql-hackers@arkaria.postgresql.org; Mon, 07 Sep 2026 14:53:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x3aic-0024KO-07 for pgsql-hackers@arkaria.postgresql.org; Mon, 07 Sep 2026 14:53:10 +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 1x3aib-0024KG-2H for pgsql-hackers@lists.postgresql.org; Mon, 07 Sep 2026 14:53:09 +0000 Received: from email.dnscdc.tech ([194.226.250.15]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1x3aiY-00000003QFP-2TAo for pgsql-hackers@lists.postgresql.org; Mon, 07 Sep 2026 14:53:09 +0000 Received: with id 187292806E0; Mon, 7 Sep 2026 21:53:04 +0700 (+07) Received: with id 8A476280037; Mon, 7 Sep 2026 21:53:01 +0700 (+07) From: Grigorev Jurij To: PostgreSQL Hackers CC: "michael@paquier.xyz" Subject: Missing dshash cleanup in pgstat_read_statsfile() after OOM Thread-Topic: Missing dshash cleanup in pgstat_read_statsfile() after OOM Thread-Index: AQHdPtabWHv5amcxkE+FqwDZO8FF4Q== Date: Mon, 7 Sep 2026 14:52:59 +0000 Message-ID: Accept-Language: ru-RU, en-US Content-Language: ru-RU X-MS-Has-Attach: yes X-MS-TNEF-Correlator: Content-Type: multipart/mixed; boundary="_002_d55ecaf911844d53bd0a931751dce582ftdataru_" MIME-Version: 1.0 X-KLMS-Rule-ID: 1 X-KLMS-Message-Action: clean X-KLMS-AntiSpam-Lua-Profiles: 205788 [Sep 07 2026] X-KLMS-AntiSpam-Version: 6.1.1.27 X-KLMS-AntiSpam-Envelope-From: ju.grigorev@ftdata.ru X-KLMS-AntiSpam-Rate: 0 X-KLMS-AntiSpam-Status: not_detected X-KLMS-AntiSpam-Method: none X-KLMS-AntiSpam-Auth: dkim=none X-MS-Exchange-Organization-SCL: -1 X-KLMS-AntiSpam-Interceptor-Info: scan successful X-KLMS-AntiPhishing: Clean, bases: 2026/09/07 14:13:00 X-KLMS-AntiVirus: Kaspersky Security for Linux Mail Server, version 8.0.3.30, bases: 2026/09/07 13:23:00 #28548040 X-KLMS-AntiVirus-Status: Clean, skipped List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --_002_d55ecaf911844d53bd0a931751dce582ftdataru_ Content-Type: text/plain; charset="koi8-r" Content-Transfer-Encoding: quoted-printable Hi, While investigating an AddressSanitizer crash in pgstat_acquire_entry_ref(), I found what looks like an incomplete part of the OOM cleanup added by 8191e0c16a03. This is a follow-up to the September 2025 discussion started here: https://postgr.es/m/CAAi9E7jELo5_-sBENftnc2E8XhW2PKZJWfTC3i2y-GMQd2bcqQ@mai= l.gmail.com The original problem was that pgstat_init_entry() could fail while allocating its DSA object after an entry had already been inserted into the shared hash. This left a half-initialized entry in the hash. The discussion considered changing the order of initialization or having pgstat_init_entry() report the allocation failure to its callers: https://postgr.es/m/aLfpyYaQ0g8i4R_m@paquier.xyz https://postgr.es/m/aLlAym4DHW4PM8Gg@paquier.xyz The latter approach was committed as 8191e0c16a03. pgstat_init_entry() now uses DSA_ALLOC_NO_OOM and returns NULL when the allocation fails, so that its caller can remove the newly inserted hash entry before raising an error. There are two callers of pgstat_init_entry(). The normal entry creation path in pgstat_get_entry_ref() performs that cleanup: shheader =3D pgstat_init_entry(kind, shhashent); if (shheader =3D=3D NULL) { ... dshash_delete_entry(pgStatLocal.shared_hash, shhashent); ereport(ERROR, ...); } However, the stats-file restore path in pgstat_read_statsfile() releases the dshash lock and raises ERROR without deleting the entry: header =3D pgstat_init_entry(key.kind, p); dshash_release_lock(pgStatLocal.shared_hash, p); if (header =3D=3D NULL) elog(ERROR, ...); At that point, the entry has already been inserted and initialized with dropped=3Dfalse, refcount=3D1 and generation=3D0, but its body is still InvalidDsaPointer. After the lock is released, that half-initialized entry remains in the shared hash rather than being removed on the allocation failure. The ERROR-versus-WARNING behavior of pgstat_read_statsfile() was discussed explicitly: https://postgr.es/m/aLuH5D1xqHl3TJoA@paquier.xyz I agree with the decision to keep ERROR here. The issue is only that the newly inserted hash entry should be removed before raising it, as already done by the other caller. The attached patch changes the failure path to: header =3D pgstat_init_entry(key.kind, p); if (header =3D=3D NULL) { dshash_delete_entry(pgStatLocal.shared_hash, p); elog(ERROR, ...); } dshash_release_lock(pgStatLocal.shared_hash, p); dshash_delete_entry() releases the partition lock itself, so the normal dshash_release_lock() is reached only after successful initialization. I originally noticed this while investigating an ASan SEGV where gdb showed a live shared hash entry with body =3D=3D InvalidDsaPointer. The affected entries were shared relation statistics for pg_authid and pg_database. The resulting NULL from dsa_get_address() was later dereferenced in pgstat_acquire_entry_ref(). I cannot prove that pgstat_read_statsfile() created those particular entries. On a clean startup, an ERROR while restoring the stats file normally prevents the cluster from continuing. The same hash-entry state could also be left if a process were terminated inside pgstat_init_entry(), after the entry had been marked live but before its body was assigned. The normal creation path does remove the entry when pgstat_init_entry() returns NULL. The ASan crash was therefore what led me to inspect this invariant, rather than a reproducer specifically for the stats-file restore path. Still, pgstat_read_statsfile() leaves the shared hash inconsistent if pgstat_init_entry() returns NULL, contrary to the cleanup model introduced by 8191e0c. The patch is intentionally limited to making the restore caller match the existing creation path. It does not change the ERROR behavior or add defensive NULL checks to readers. Thoughts? Thanks, Yuriy Grigoryev= --_002_d55ecaf911844d53bd0a931751dce582ftdataru_ Content-Type: application/octet-stream; name="0001-Clean-up-pgstats-hash-entry-after-restore-OOM.patch" Content-Description: 0001-Clean-up-pgstats-hash-entry-after-restore-OOM.patch Content-Disposition: attachment; filename="0001-Clean-up-pgstats-hash-entry-after-restore-OOM.patch"; size=1726; creation-date="Mon, 07 Sep 2026 14:52:10 GMT"; modification-date="Mon, 07 Sep 2026 14:52:10 GMT" Content-Transfer-Encoding: base64 RnJvbSAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwIE1vbiBTZXAgMTcg MDA6MDA6MDAgMjAwMQpGcm9tOiBZdXJpeSBHcmlnb3J5ZXYgPGp1LmdyaWdvcmV2QGZ0ZGF0YS5y dT4KRGF0ZTogTW9uLCA3IFNlcCAyMDI2IDIxOjQ0OjIyICswNzAwClN1YmplY3Q6IFtQQVRDSF0g Q2xlYW4gdXAgcGdzdGF0cyBoYXNoIGVudHJ5IGFmdGVyIHJlc3RvcmUgT09NCgpwZ3N0YXRfaW5p dF9lbnRyeSgpIGNhbiBmYWlsIHRvIGFsbG9jYXRlIHRoZSBEU0Egb2JqZWN0IGFmdGVyIHRoZSBz aGFyZWQKaGFzaCBlbnRyeSBoYXMgYmVlbiBpbnNlcnRlZC4gIEl0cyBjYWxsZXIgaW4gcGdzdGF0 X3JlYWRfc3RhdHNmaWxlKCkKcmVsZWFzZWQgdGhlIGhhc2ggbG9jayBiZWZvcmUgcmVwb3J0aW5n IHRoZSBhbGxvY2F0aW9uIGZhaWx1cmUsIGxlYXZpbmcKYW4gZW50cnkgd2hvc2UgYm9keSBpcyBJ bnZhbGlkRHNhUG9pbnRlci4KCkRlbGV0ZSB0aGUgaW5jb21wbGV0ZSBlbnRyeSBiZWZvcmUgcmFp c2luZyBFUlJPUiwgYXMgdGhlIG90aGVyIGNhbGxlciBpbgpwZ3N0YXRfZ2V0X2VudHJ5X3JlZigp IGFscmVhZHkgZG9lcy4gIGRzaGFzaF9kZWxldGVfZW50cnkoKSByZWxlYXNlcyB0aGUKcGFydGl0 aW9uIGxvY2ssIHNvIHJlbGVhc2UgaXQgZXhwbGljaXRseSBvbmx5IGFmdGVy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