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 1x3pPk-006h1U-2g for pgsql-hackers@arkaria.postgresql.org; Tue, 08 Sep 2026 06:34:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x3pPj-004xEv-1O for pgsql-hackers@arkaria.postgresql.org; Tue, 08 Sep 2026 06:34:39 +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 1x3pPj-004xEm-0R for pgsql-hackers@lists.postgresql.org; Tue, 08 Sep 2026 06:34:39 +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 1x3pPg-00000003WtQ-1zbx for pgsql-hackers@lists.postgresql.org; Tue, 08 Sep 2026 06:34:38 +0000 Received: with id 404FF281061; Tue, 8 Sep 2026 13:34:32 +0700 (+07) Received: with id EBB0A28002F; Tue, 8 Sep 2026 13:34:29 +0700 (+07) From: Grigorev Jurij To: Michael Paquier CC: PostgreSQL Hackers Subject: Re: Missing dshash cleanup in pgstat_read_statsfile() after OOM Thread-Topic: Missing dshash cleanup in pgstat_read_statsfile() after OOM Thread-Index: AQHdPtabWHv5amcxkE+FqwDZO8FF4bbDo/SAgACUamA= Date: Tue, 8 Sep 2026 06:34:29 +0000 Message-ID: <548422d5c62f471a80fb9d0a2c07050c@localhost.localdomain> References: , In-Reply-To: Accept-Language: ru-RU, en-US Content-Language: ru-RU X-MS-Has-Attach: X-MS-TNEF-Correlator: Content-Type: text/plain; charset="koi8-r" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-KLMS-Rule-ID: 1 X-KLMS-Message-Action: clean X-KLMS-AntiSpam-Lua-Profiles: 205792 [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/08 04:57:00 X-KLMS-AntiVirus: Kaspersky Security for Linux Mail Server, version 8.0.3.30, bases: 2026/09/08 02:21:00 #28552787 X-KLMS-AntiVirus-Status: Clean, skipped List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi Michael, Thanks, agreed. I missed the important consequence that ERROR in the startup process is promoted to FATAL and that shared memory is then reinitialized. Therefore, the half-initialized entry left by pgstat_read_statsfile() is not observable by later backends with the code as it stands on HEAD, and this path cannot explain the ASan crash. I have since traced the ASan failure to a different path: an error raised while creating a new DSM segment can escape dsa_allocate_extended(..., DSA_ALLOC_NO_OOM) after pgstat_init_entry() has marked the hash entry live but before its body is assigned. I will report that separately with the logs and a patch. The pgstat_read_statsfile() change remains a consistency and defensive cleanup improvement, particularly if the restore ERROR is ever changed to a WARNING. I see that you have committed the patch. Thanks for taking care of it; I'm glad the report was useful :) Thanks, Yuriy ________________________________________ =EF=D4: Michael Paquier =EF=D4=D0=D2=C1=D7=CC=C5=CE=CF: 8 =D3=C5=CE=D4=D1=C2=D2=D1 2026 =C7. 11:35:= 35 =EB=CF=CD=D5: =E7=D2=C9=C7=CF=D2=D8=C5=D7 =E0=D2=C9=CA =EB=CF=D0=C9=D1: PostgreSQL Hackers =F4=C5=CD=C1: Re: Missing dshash cleanup in pgstat_read_statsfile() after O= OM On Mon, Sep 07, 2026 at 02:52:59PM +0000, Grigorev Jurij wrote: > 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. Well, this addition is not really necessary. When we read the stats from disk, we are in the startup process and an ERROR would upgrade to a FATAL, and shared memory gets reinitialized. So it is not possible to observe a half-baked entry as far as I know for this path, at least as coded on HEAD. > 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. That points to a different issue to me. If you see something going on, please feel free, but I have not heard of other similar reports yet. > 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. Saying that, there is I guess an argument of consistency with the entry creation path. If someone has the idea to change the ERROR to a WARNING, then the post-read cleanup doing all the discard would see a half-baked entry, due to pgstat_drop_entry_internal() in pgstat_reset_after_failure -> pgstat_drop_all_entries() if I follow that right. That would be surprising, still that's not something worth more than a HEAD-only change. -- Michael