Skip to content

Commit 7467939

Browse files
committed
Fix overflow with pgstats DSA reference count
When pgstats is initialized for a backend, it uses dsa_attach_in_place() without a "segment" provided. Hence, no callback is registered to automatically release the DSA attached once a backend exits. Not doing any cleanup causes the reference count of the pgstats DSA to continuously increment, at some point overflowing it (the more the number of connections, the faster it is to reach this state). Once the reference count overflows and then gets back to 0, new backends are not able to attach to the pgstats DSA, failing startup. This issue is resolved by adding in the pgstats shutdown hook a call to dsa_release_in_place(), ensuring that the DSA attached at backend startup is correctly released, keeping the reference count at bay. The author of this patch has been able to see this issue on a server with a long uptime and a high connection turnover. Issue introduced by 5891c7a, so backpatch down to 15. Author: Anthonin Bonnefoy Discussion: https://postgr.es/m/CAO6_XqqJbJBL=M7Ym13TcB4Xnq58vRa2jcC+gwEPBgbAda6B1Q@mail.gmail.com Backpatch-through: 15
1 parent b1ffe3f commit 7467939

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/backend/utils/activity/pgstat_shmem.c

+8
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,14 @@ pgstat_detach_shmem(void)
246246
pgStatLocal.shared_hash = NULL;
247247

248248
dsa_detach(pgStatLocal.dsa);
249+
250+
/*
251+
* dsa_detach() does not decrement the DSA reference count as no segment
252+
* was provided to dsa_attach_in_place(), causing no cleanup callbacks to
253+
* be registered. Hence, release it manually now.
254+
*/
255+
dsa_release_in_place(pgStatLocal.shmem->raw_dsa_area);
256+
249257
pgStatLocal.dsa = NULL;
250258
}
251259

0 commit comments

Comments
 (0)