aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Paquier <michael@paquier.xyz>2024-06-27 09:44:47 +0900
committerMichael Paquier <michael@paquier.xyz>2024-06-27 09:44:47 +0900
commit7467939ea226ebc5608285486501b136b642c02b (patch)
treec15abbd957e5145f010927ca3ae75d8101f3d776 /src
parentb1ffe3ff0b7ed87b34ae0fc6eba71bf032e41b59 (diff)
downloadpostgresql-7467939ea226ebc5608285486501b136b642c02b.tar.gz
postgresql-7467939ea226ebc5608285486501b136b642c02b.zip
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 5891c7a8ed8f, so backpatch down to 15. Author: Anthonin Bonnefoy Discussion: https://postgr.es/m/CAO6_XqqJbJBL=M7Ym13TcB4Xnq58vRa2jcC+gwEPBgbAda6B1Q@mail.gmail.com Backpatch-through: 15
Diffstat (limited to 'src')
-rw-r--r--src/backend/utils/activity/pgstat_shmem.c8
1 files changed, 8 insertions, 0 deletions
diff --git a/src/backend/utils/activity/pgstat_shmem.c b/src/backend/utils/activity/pgstat_shmem.c
index fb79c5e771b..634b9678206 100644
--- a/src/backend/utils/activity/pgstat_shmem.c
+++ b/src/backend/utils/activity/pgstat_shmem.c
@@ -246,6 +246,14 @@ pgstat_detach_shmem(void)
pgStatLocal.shared_hash = NULL;
dsa_detach(pgStatLocal.dsa);
+
+ /*
+ * dsa_detach() does not decrement the DSA reference count as no segment
+ * was provided to dsa_attach_in_place(), causing no cleanup callbacks to
+ * be registered. Hence, release it manually now.
+ */
+ dsa_release_in_place(pgStatLocal.shmem->raw_dsa_area);
+
pgStatLocal.dsa = NULL;
}