Re: BUG #19520: PANIC when concurrently manipulating stored procedures with pg_stat_statements and track_functions =
Sami Imseih <samimseih@gmail.com>
From: Sami Imseih <samimseih@gmail.com>
To: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Cc: Michael Paquier <michael@paquier.xyz>, zlh21343@163.com,
pgsql-bugs@lists.postgresql.org, Andres Freund <andres@anarazel.de>,
Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Date: 2026-06-17T20:26:33Z
Lists: pgsql-bugs
> This patch is very close to what Sami has posted on his PGSS thread, > v3-0002, using a missing_ok instead of a skip_dropped: > https://www.postgresql.org/message-id/CAA5RZ0uoxiQ2_=xHGRnyc4WdM9aR0fzdMhBubnw97po==--yGQ@mail.gmail.com > I didn't suspect that we would need something like that for a > backpatch, but well. Right, my intention was just adding infrastructure to make tolerating a dropped entry possible and to be used by an extension in the future. But, this bug report is timely and now it looks like we need this for race conditions that are possible in core. > A couple of things which I'm not clear about (these are not blockers > just questions for my understanding): > > - With the check moved into the wrapper, pgstat_drop_entry_internal() > still keeps its own "already dropped" elog(). Every path into > _internal now seems to guarantee the entry isn't dropped, so > _internal's copy looks unreachable after the patch > ,and it's the one with the richer refcount/generation detail. Right. Michael's approach of moving the ERROR into the wrapper is better than keeping it in _internal. Since after the patch no caller enters pgstat_drop_entry_internal() with a dropped entry (pgstat_drop_database_and_contents() and pgstat_drop_matching_entries() already filtered them out, and now the wrapper does too) > Was the idea to leave it as a backstop, or would folding the handling into > one place (or making _internal's an Assert) be cleaner? The check in _internal should be converted to an Assert. This documents that callers must only pass "live" entries, which will be the case for all callers after the patch > - In the missing_ok path the wrapper returns true, so the post-commit > caller skips the not_freed_count++/GC request that a "real" not-freed > drop would do. That seems harmless since the entry self-heals > but was returning true there a deliberate choice over mirroring > the not-freed/false path? I need to take a look again at this, maybe > I missed something. Finding an already dropped entry tells me that the first caller to drop the entry also triggered a gc request, so we should not request it again. -- Sami Imseih Amazon Web Services (AWS)
Commits
-
Re-introduce pgstat_drop_entry(), keeping ABI compatibility
- 9e47718250d8 15 (unreleased) landed
- e34b1ff5d894 16 (unreleased) landed
- afb076b2977c 17 (unreleased) landed
- fe464e9e6863 18 (unreleased) landed
-
Update .abi-compliance-history for pgstat_drop_entry()
- 75aca8b932e8 15 (unreleased) landed
- 7c457ea15c6d 16 (unreleased) landed
- 39e649d44a1d 17 (unreleased) landed
- 8a4f389ddce5 18 (unreleased) landed
-
Fix PANIC with track_functions due to concurrent drop of pgstats entries
- 1e9e62193c3f 15 (unreleased) landed
- bf4616b598a3 16 (unreleased) landed
- 2e0c61aed624 17 (unreleased) landed
- 5cc59834b860 18 (unreleased) landed
- 850b9218c8e4 19 (unreleased) landed