pg.ddx.io pgsql-committers@postgresql.org mailing list archivehelp / color / mirror / Atom feed
pgsql: Allocate pgstats entry body before insert into shared hashtable 6+ messages / 1 participants [nested] [flat]
* pgsql: Allocate pgstats entry body before insert into shared hashtable @ 2026-09-23 23:51 Michael Paquier <michael@paquier.xyz> 0 siblings, 0 replies; 6+ messages in thread From: Michael Paquier @ 2026-09-23 23:51 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Allocate pgstats entry body before insert into shared hashtable 8191e0c16a03 made pgstat_init_entry() return NULL on DSA allocation failure so callers could delete a just-inserted hash entry. That works when dsa_allocate_extended(DSA_ALLOC_NO_OOM) returns InvalidDsaPointer, but not when the creation of a new DSM segment raises ERROR. Under this type of failure, pgstat_init_entry() would mark the hash entry live before allocating the body and cause an inconsistent state in shared memory. The next backend trying to acquire the existing, partially-initialized entry in pgstat_acquire_entry_ref() would crash, taking down the server. Some of the public reports mentioned ENOSPC on POSIX shared memory as one initial cause. This commit refactors the pgstats entry initialization to happen in two phases: - Allocate the DSA chunk. - Insert the hash entry with a valid segment. If a concurrent insert wins, the allocation of the first phase needs an extra set of dsa_free() calls (pulling back a DSM into its free list). As this changes the signature of pgstat_init_entry(), no backpatch is done. Author: Yuriy Grigoryev <ju.grigorev@ftdata.ru> Author: Michael Paquier <michael@paquier.xyz> Discussion: https://postgr.es/m/ddc3ecfb01ce4e9698b23cc59767f016@localhost.localdomain Branch ------ master Details ------- https://git.postgresql.org/pg/commitdiff/d9de60c5e47a7f7a5dcee050d119bda08ea85d55 Modified Files -------------- src/backend/utils/activity/pgstat.c | 46 ++++++++++++++------ src/backend/utils/activity/pgstat_shmem.c | 72 ++++++++++++++++++------------- src/include/utils/pgstat_internal.h | 4 +- 3 files changed, 76 insertions(+), 46 deletions(-) ^ permalink raw reply [nested|flat] 6+ messages in thread
* pgsql: Allocate pgstats entry body before insert into shared hashtable @ 2026-11-24 03:27 Michael Paquier <michael@paquier.xyz> 0 siblings, 0 replies; 6+ messages in thread From: Michael Paquier @ 2026-11-24 03:27 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Allocate pgstats entry body before insert into shared hashtable 8191e0c16a03 made pgstat_init_entry() return NULL on DSA allocation failure so callers could delete a just-inserted hash entry. That works when dsa_allocate_extended(DSA_ALLOC_NO_OOM) returns InvalidDsaPointer, but not when the creation of a new DSM segment raises ERROR. Under this type of failure, pgstat_init_entry() would mark the hash entry live before allocating the body and cause an inconsistent state in shared memory. The next backend trying to acquire the existing, partially-initialized entry in pgstat_acquire_entry_ref() would crash, taking down the server. Some of the public reports mentioned ENOSPC on POSIX shared memory as one initial cause. This commit refactors the pgstats entry initialization to happen in two phases: - Allocate the DSA chunk. - Insert the hash entry with a valid segment. If a concurrent insert wins, the allocation of the first phase needs an extra set of dsa_free() calls (pulling back a DSM into its free list). This change has been done on HEAD as of d9de60c5e47a, without a backpatch as the API signature of pgstat_init_entry() has changed, and is now backpatched following more complaints that this code leads to a corrupted shared memory state, which is likely to crash a server at the end. This backpatch guarantees ABI compatibility with a new pgstat_init_entry_ext(), pgstat_init_entry() remaining as it is. Colin has authored this part, while I have adjusted a few things to be more consistent with HEAD. There are no more direct callers of pgstat_init_entry() in all the stable branches. It is recommended that out-of-core code adjusts to pgstat_init_entry_ext(), pgstat_init_entry() should not be used when dealing with versions of Postgres up to v19. Author: Yuriy Grigoryev <ju.grigorev@ftdata.ru> Author: Michael Paquier <michael@paquier.xyz> Author: Colin McDonnell <colinmcd94@gmail.com> Discussion: https://postgr.es/m/ddc3ecfb01ce4e9698b23cc59767f016@localhost.localdomain Discussion: https://postgr.es/m/CALdYYtTg2x4nNLsjX99acjbQj=oTo40CBXgDM1qzdzG7UbwzGQ@mail.gmail.com Backpatch-through: 15 Branch ------ REL_19_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/20d6f4a22e552f458155bad000dc13ec046ba71c Modified Files -------------- src/backend/utils/activity/pgstat.c | 46 ++++++++++++------ src/backend/utils/activity/pgstat_shmem.c | 78 +++++++++++++++++++++---------- src/include/utils/pgstat_internal.h | 4 ++ 3 files changed, 90 insertions(+), 38 deletions(-) ^ permalink raw reply [nested|flat] 6+ messages in thread
* pgsql: Allocate pgstats entry body before insert into shared hashtable @ 2026-11-24 03:27 Michael Paquier <michael@paquier.xyz> 0 siblings, 0 replies; 6+ messages in thread From: Michael Paquier @ 2026-11-24 03:27 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Allocate pgstats entry body before insert into shared hashtable 8191e0c16a03 made pgstat_init_entry() return NULL on DSA allocation failure so callers could delete a just-inserted hash entry. That works when dsa_allocate_extended(DSA_ALLOC_NO_OOM) returns InvalidDsaPointer, but not when the creation of a new DSM segment raises ERROR. Under this type of failure, pgstat_init_entry() would mark the hash entry live before allocating the body and cause an inconsistent state in shared memory. The next backend trying to acquire the existing, partially-initialized entry in pgstat_acquire_entry_ref() would crash, taking down the server. Some of the public reports mentioned ENOSPC on POSIX shared memory as one initial cause. This commit refactors the pgstats entry initialization to happen in two phases: - Allocate the DSA chunk. - Insert the hash entry with a valid segment. If a concurrent insert wins, the allocation of the first phase needs an extra set of dsa_free() calls (pulling back a DSM into its free list). This change has been done on HEAD as of d9de60c5e47a, without a backpatch as the API signature of pgstat_init_entry() has changed, and is now backpatched following more complaints that this code leads to a corrupted shared memory state, which is likely to crash a server at the end. This backpatch guarantees ABI compatibility with a new pgstat_init_entry_ext(), pgstat_init_entry() remaining as it is. Colin has authored this part, while I have adjusted a few things to be more consistent with HEAD. There are no more direct callers of pgstat_init_entry() in all the stable branches. It is recommended that out-of-core code adjusts to pgstat_init_entry_ext(), pgstat_init_entry() should not be used when dealing with versions of Postgres up to v19. Author: Yuriy Grigoryev <ju.grigorev@ftdata.ru> Author: Michael Paquier <michael@paquier.xyz> Author: Colin McDonnell <colinmcd94@gmail.com> Discussion: https://postgr.es/m/ddc3ecfb01ce4e9698b23cc59767f016@localhost.localdomain Discussion: https://postgr.es/m/CALdYYtTg2x4nNLsjX99acjbQj=oTo40CBXgDM1qzdzG7UbwzGQ@mail.gmail.com Backpatch-through: 15 Branch ------ REL_18_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/8efeedc7380db8449a1e5f580c99166a437ba9db Modified Files -------------- src/backend/utils/activity/pgstat.c | 30 +++++++----- src/backend/utils/activity/pgstat_shmem.c | 78 +++++++++++++++++++++---------- src/include/utils/pgstat_internal.h | 4 ++ 3 files changed, 75 insertions(+), 37 deletions(-) ^ permalink raw reply [nested|flat] 6+ messages in thread
* pgsql: Allocate pgstats entry body before insert into shared hashtable @ 2026-11-24 03:27 Michael Paquier <michael@paquier.xyz> 0 siblings, 0 replies; 6+ messages in thread From: Michael Paquier @ 2026-11-24 03:27 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Allocate pgstats entry body before insert into shared hashtable 8191e0c16a03 made pgstat_init_entry() return NULL on DSA allocation failure so callers could delete a just-inserted hash entry. That works when dsa_allocate_extended(DSA_ALLOC_NO_OOM) returns InvalidDsaPointer, but not when the creation of a new DSM segment raises ERROR. Under this type of failure, pgstat_init_entry() would mark the hash entry live before allocating the body and cause an inconsistent state in shared memory. The next backend trying to acquire the existing, partially-initialized entry in pgstat_acquire_entry_ref() would crash, taking down the server. Some of the public reports mentioned ENOSPC on POSIX shared memory as one initial cause. This commit refactors the pgstats entry initialization to happen in two phases: - Allocate the DSA chunk. - Insert the hash entry with a valid segment. If a concurrent insert wins, the allocation of the first phase needs an extra set of dsa_free() calls (pulling back a DSM into its free list). This change has been done on HEAD as of d9de60c5e47a, without a backpatch as the API signature of pgstat_init_entry() has changed, and is now backpatched following more complaints that this code leads to a corrupted shared memory state, which is likely to crash a server at the end. This backpatch guarantees ABI compatibility with a new pgstat_init_entry_ext(), pgstat_init_entry() remaining as it is. Colin has authored this part, while I have adjusted a few things to be more consistent with HEAD. There are no more direct callers of pgstat_init_entry() in all the stable branches. It is recommended that out-of-core code adjusts to pgstat_init_entry_ext(), pgstat_init_entry() should not be used when dealing with versions of Postgres up to v19. Author: Yuriy Grigoryev <ju.grigorev@ftdata.ru> Author: Michael Paquier <michael@paquier.xyz> Author: Colin McDonnell <colinmcd94@gmail.com> Discussion: https://postgr.es/m/ddc3ecfb01ce4e9698b23cc59767f016@localhost.localdomain Discussion: https://postgr.es/m/CALdYYtTg2x4nNLsjX99acjbQj=oTo40CBXgDM1qzdzG7UbwzGQ@mail.gmail.com Backpatch-through: 15 Branch ------ REL_17_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/6a806d254f30b8ac8feceb933063db00669a3510 Modified Files -------------- src/backend/utils/activity/pgstat.c | 28 ++++++----- src/backend/utils/activity/pgstat_shmem.c | 78 +++++++++++++++++++++---------- src/include/utils/pgstat_internal.h | 4 ++ 3 files changed, 74 insertions(+), 36 deletions(-) ^ permalink raw reply [nested|flat] 6+ messages in thread
* pgsql: Allocate pgstats entry body before insert into shared hashtable @ 2026-11-24 03:27 Michael Paquier <michael@paquier.xyz> 0 siblings, 0 replies; 6+ messages in thread From: Michael Paquier @ 2026-11-24 03:27 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Allocate pgstats entry body before insert into shared hashtable 8191e0c16a03 made pgstat_init_entry() return NULL on DSA allocation failure so callers could delete a just-inserted hash entry. That works when dsa_allocate_extended(DSA_ALLOC_NO_OOM) returns InvalidDsaPointer, but not when the creation of a new DSM segment raises ERROR. Under this type of failure, pgstat_init_entry() would mark the hash entry live before allocating the body and cause an inconsistent state in shared memory. The next backend trying to acquire the existing, partially-initialized entry in pgstat_acquire_entry_ref() would crash, taking down the server. Some of the public reports mentioned ENOSPC on POSIX shared memory as one initial cause. This commit refactors the pgstats entry initialization to happen in two phases: - Allocate the DSA chunk. - Insert the hash entry with a valid segment. If a concurrent insert wins, the allocation of the first phase needs an extra set of dsa_free() calls (pulling back a DSM into its free list). This change has been done on HEAD as of d9de60c5e47a, without a backpatch as the API signature of pgstat_init_entry() has changed, and is now backpatched following more complaints that this code leads to a corrupted shared memory state, which is likely to crash a server at the end. This backpatch guarantees ABI compatibility with a new pgstat_init_entry_ext(), pgstat_init_entry() remaining as it is. Colin has authored this part, while I have adjusted a few things to be more consistent with HEAD. There are no more direct callers of pgstat_init_entry() in all the stable branches. It is recommended that out-of-core code adjusts to pgstat_init_entry_ext(), pgstat_init_entry() should not be used when dealing with versions of Postgres up to v19. Author: Yuriy Grigoryev <ju.grigorev@ftdata.ru> Author: Michael Paquier <michael@paquier.xyz> Author: Colin McDonnell <colinmcd94@gmail.com> Discussion: https://postgr.es/m/ddc3ecfb01ce4e9698b23cc59767f016@localhost.localdomain Discussion: https://postgr.es/m/CALdYYtTg2x4nNLsjX99acjbQj=oTo40CBXgDM1qzdzG7UbwzGQ@mail.gmail.com Backpatch-through: 15 Branch ------ REL_16_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/978888fafaccc4ebd404898004c828d18bcf121b Modified Files -------------- src/backend/utils/activity/pgstat.c | 28 ++++++----- src/backend/utils/activity/pgstat_shmem.c | 78 +++++++++++++++++++++---------- src/include/utils/pgstat_internal.h | 4 ++ 3 files changed, 74 insertions(+), 36 deletions(-) ^ permalink raw reply [nested|flat] 6+ messages in thread
* pgsql: Allocate pgstats entry body before insert into shared hashtable @ 2026-11-24 03:27 Michael Paquier <michael@paquier.xyz> 0 siblings, 0 replies; 6+ messages in thread From: Michael Paquier @ 2026-11-24 03:27 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Allocate pgstats entry body before insert into shared hashtable 8191e0c16a03 made pgstat_init_entry() return NULL on DSA allocation failure so callers could delete a just-inserted hash entry. That works when dsa_allocate_extended(DSA_ALLOC_NO_OOM) returns InvalidDsaPointer, but not when the creation of a new DSM segment raises ERROR. Under this type of failure, pgstat_init_entry() would mark the hash entry live before allocating the body and cause an inconsistent state in shared memory. The next backend trying to acquire the existing, partially-initialized entry in pgstat_acquire_entry_ref() would crash, taking down the server. Some of the public reports mentioned ENOSPC on POSIX shared memory as one initial cause. This commit refactors the pgstats entry initialization to happen in two phases: - Allocate the DSA chunk. - Insert the hash entry with a valid segment. If a concurrent insert wins, the allocation of the first phase needs an extra set of dsa_free() calls (pulling back a DSM into its free list). This change has been done on HEAD as of d9de60c5e47a, without a backpatch as the API signature of pgstat_init_entry() has changed, and is now backpatched following more complaints that this code leads to a corrupted shared memory state, which is likely to crash a server at the end. This backpatch guarantees ABI compatibility with a new pgstat_init_entry_ext(), pgstat_init_entry() remaining as it is. Colin has authored this part, while I have adjusted a few things to be more consistent with HEAD. There are no more direct callers of pgstat_init_entry() in all the stable branches. It is recommended that out-of-core code adjusts to pgstat_init_entry_ext(), pgstat_init_entry() should not be used when dealing with versions of Postgres up to v19. Author: Yuriy Grigoryev <ju.grigorev@ftdata.ru> Author: Michael Paquier <michael@paquier.xyz> Author: Colin McDonnell <colinmcd94@gmail.com> Discussion: https://postgr.es/m/ddc3ecfb01ce4e9698b23cc59767f016@localhost.localdomain Discussion: https://postgr.es/m/CALdYYtTg2x4nNLsjX99acjbQj=oTo40CBXgDM1qzdzG7UbwzGQ@mail.gmail.com Backpatch-through: 15 Branch ------ REL_15_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/5e7f7f5ec963fe99d3e7bdc4e9e2321a7171b4c2 Modified Files -------------- src/backend/utils/activity/pgstat.c | 28 ++++++----- src/backend/utils/activity/pgstat_shmem.c | 78 +++++++++++++++++++++---------- src/include/utils/pgstat_internal.h | 4 ++ 3 files changed, 74 insertions(+), 36 deletions(-) ^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2026-11-24 03:27 UTC | newest] Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2026-09-23 23:51 pgsql: Allocate pgstats entry body before insert into shared hashtable Michael Paquier <michael@paquier.xyz> 2026-11-24 03:27 pgsql: Allocate pgstats entry body before insert into shared hashtable Michael Paquier <michael@paquier.xyz> 2026-11-24 03:27 pgsql: Allocate pgstats entry body before insert into shared hashtable Michael Paquier <michael@paquier.xyz> 2026-11-24 03:27 pgsql: Allocate pgstats entry body before insert into shared hashtable Michael Paquier <michael@paquier.xyz> 2026-11-24 03:27 pgsql: Allocate pgstats entry body before insert into shared hashtable Michael Paquier <michael@paquier.xyz> 2026-11-24 03:27 pgsql: Allocate pgstats entry body before insert into shared hashtable Michael Paquier <michael@paquier.xyz>
This inbox is served by DDX for PostgreSQL; see mirroring instructions for how to clone and mirror all data and code used for this inbox