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 1sDsoA-00FoQn-CV for pgsql-bugs@arkaria.postgresql.org; Sun, 02 Jun 2024 21:32:08 +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 1sDsoA-0083Bb-DI for pgsql-bugs@arkaria.postgresql.org; Sun, 02 Jun 2024 21:32:06 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sDso9-0083BS-VI for pgsql-bugs@lists.postgresql.org; Sun, 02 Jun 2024 21:32:06 +0000 Received: from mx0b-00134001.pphosted.com ([205.220.172.129]) by makus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sDso6-0039rA-VN for pgsql-bugs@lists.postgresql.org; Sun, 02 Jun 2024 21:32:04 +0000 Received: from pps.filterd (m0267962.ppops.net [127.0.0.1]) by mx0a-00134001.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 452LR8aN002067; Sun, 2 Jun 2024 21:31:58 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=optiver.com; h= cc:content-type:date:from:in-reply-to:message-id:mime-version :references:subject:to; s=mail; bh=sS/JxDbTvV0IwdxkbaH7KJ9fT5nJi 2oWAf3StFFcUtI=; b=vsDWtrzytNI6UQt/ViYMp0Bm5x9rjIbD5dkD06SWIYUbn W18SEYqj9HdgMXzQQ3kZWyoY/YCCSQDevHBPXXqe33WZ3LvQ+/q5nrXf7R5UzcNk gJXq7K4K7yuPVWNtv6aPP3H5s5IU6ZcTsBnoQv2TCOYl4cMqdc1B1q+wgzPGpQXB JsHQzvKJNwJ5P+YZRjvg8y+coDpcYW+7WrCeNpN1YISy6jd5g5xh6MgFBOWdz7Cy jSARIpZtZd2T/vuT0jbLBI/ZQ4Kp9Z8JIDQy/J6zVvMBXQhtgzE6oj1JjHNaPXBp FuRECsrk1wNgvRhWj45ZQryujqRZxQqyi2PYq+taw== Received: from opammb1270.comp.optiver.com ([195.246.114.78]) by mx0a-00134001.pphosted.com (PPS) with ESMTPS id 3yfw21rttm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 02 Jun 2024 21:31:58 +0000 (GMT) Received: from opammb1267.comp.optiver.com (100.68.11.12) by opammb1270.comp.optiver.com (100.68.11.18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Sun, 2 Jun 2024 23:31:57 +0200 Received: from opammb1267.comp.optiver.com ([fe80::5b6f:1976:98cd:68a7]) by opammb1267.comp.optiver.com ([fe80::5b6f:1976:98cd:68a7%13]) with mapi id 15.02.1118.026; Sun, 2 Jun 2024 23:31:56 +0200 From: Floris Van Nee To: Andres Freund , Kyotaro Horiguchi CC: "exclusion@gmail.com" , "pgsql-bugs@lists.postgresql.org" Thread-Topic: [External] Re: BUG #17947: Combination of replslots pgstat issues causes error/assertion failure [External] Thread-Index: AQHZlBmhKd6ff+auW0KpzehwxH4Xw7GK7OSAgCxGHIA= Date: Sun, 2 Jun 2024 21:31:56 +0000 Message-ID: References: <17947-b9554521ad963c9c@postgresql.org> <20230601.084245.1524588196504400333.horikyota.ntt@gmail.com> <20240505183741.5tefii3razzefvtc@awork3.anarazel.de> In-Reply-To: <20240505183741.5tefii3razzefvtc@awork3.anarazel.de> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: yes X-MS-TNEF-Correlator: x-originating-ip: [100.68.31.122] Content-Type: multipart/mixed; boundary="_002_dd4c68beba5c40c5a7946569d450ec59Optivercom_" MIME-Version: 1.0 Subject: RE: BUG #17947: Combination of replslots pgstat issues causes error/assertion failure X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.650,FMLib:17.12.28.16 definitions=2024-06-02_15,2024-05-30_01,2024-05-17_01 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --_002_dd4c68beba5c40c5a7946569d450ec59Optivercom_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable >=20 > I think the problem might actually be much simpler - when > pgstat_drop_entry() can't free the entry, we don't call > pgstat_request_entry_refs_gc(), in contrast to > pgstat_drop_database_and_contents(), pgstat_drop_all_entries(). > Because of that, entry refs to dropped stats entries can survive an effec= tively > unbound amount of time. If pgstat_request_entry_refs_gc() had been > called, the local entry would have been released, before we reused the sl= ot's > stats entry. There's no race around the entry being dropped concurrently > with acquiring an entry references, as external locking needs to prevent = an > entry from being dropped while new references are being established. This sounds like a reasonable root cause for this thread. > I can't explain why pgstat_drop_entry() doesn't call > pgstat_request_entry_refs_gc(). It seems fairly obvious that it ought to? =20 It looks like pgstat_drop_entry expects callers to take care of this. All c= allers in pgstat_xact.c (AtEOXact_PgStat_DroppedStats for example) are drop= ping stats for several relations at once, and then at the end they call pgs= tat_request_entry_refs_gc once if needed. I'm guessing this was an optimiza= tion. However, pgstat_drop_replslot didn't get that memo. Attached patch adds a c= all to gc in pgstat_drop_replslot to bring it in line with usage in pgstat_= xact.c. On my setup this fixes the issue that occurs on this thread. >=20 > I think there would be a leftover edge-case, namely that > pgstat_gc_entry_refs() doesn't gc entries with pending data. But we could > just do that - and it should be correct, afaict. I wonder if the reason w= e didn't > is that we introduced PgStat_KindInfo.delete_pending_cb later? Can you clarify why this would be safe to do? Looking at commit history, it= looks like they were added in the same commit 5891c7a8e which introduced t= he shared memory stats. I can reason why it *should* be safe, but I can't f= ind a discussion around why every single function except pgstat_drop_entry = uses discard_pending=3Dfalse if it would be safe to pass true everywhere. I= t seems strange that it'd have been chosen without reason. This does mean it's unfortunately unrelated to the issue I reported here ht= tps://www.postgresql.org/message-id/flat/20240505223546.6yvjzgqifuoiii3e%40= awork3.anarazel.de#1dfe3ecf6a75ace833444bdc0d268f4a , because the issue in = the current thread is fixed by changing a replication-slot call (which is n= ot used in the database for which I reported it). -Floris --_002_dd4c68beba5c40c5a7946569d450ec59Optivercom_ Content-Type: application/octet-stream; name="0001-Notify-stats-gc-when-dropping-logical-replication-sl.patch" Content-Description: 0001-Notify-stats-gc-when-dropping-logical-replication-sl.patch Content-Disposition: attachment; filename="0001-Notify-stats-gc-when-dropping-logical-replication-sl.patch"; size=2059; creation-date="Sun, 02 Jun 2024 20:44:42 GMT"; modification-date="Sun, 02 Jun 2024 20:43:50 GMT" Content-Transfer-Encoding: base64 RnJvbSAzZmM0ODIzMTQyZWUxMmY4NTMyNDdjY2QxYzk2ZDg5Njc0ODE3MGI1IE1vbiBTZXAgMTcg MDA6MDA6MDAgMjAwMQpGcm9tOiBGbG9yaXMgdmFuIE5lZSA8ZmxvcmlzdmFubmVlQG9wdGl2ZXIu Y29tPgpEYXRlOiBTdW4sIDIgSnVuIDIwMjQgMjI6MzU6NDQgKzAyMDAKU3ViamVjdDogW1BBVENI XSBOb3RpZnkgc3RhdHMgZ2Mgd2hlbiBkcm9wcGluZyBsb2dpY2FsIHJlcGxpY2F0aW9uIHNsb3QK CnBnc3RhdF9kcm9wX3JlcGxzbG90IGNhbGxzIHBnc3RhdF9kcm9wX2VudHJ5IHdpdGhvdXQgY2hl Y2tpbmcgdGhlIHJlc3VsdC4gSWYgcGdzdGF0X2Ryb3BfZW50cnkgY2Fubm90IGZyZWUgdGhlIGVu dHJ5IGZyb20gc2hhcmVkIG1lbW9yeSBjb21wbGV0ZWx5LCB0aGUgZ2MgZnVuY3Rpb24gbXVzdCBi ZSBjYWxsZWQgdG8gZmxhZyBpdCBmb3Igb3RoZXIgYmFja2VuZHMuIFRoaXMgaXMgc2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