postgresql/src/backend
Michael Paquier f256a7bba7 Fix corruption of pgstats shared hashtable due to OOM failures
A new pgstats entry is created as a two-step process:
- The entry is looked at in the shared hashtable of pgstats, and is
inserted if not found.
- When not found and inserted, its fields are then initialized.  This
part include a DSA chunk allocation for the stats data of the new entry.

As currently coded, if the DSA chunk allocation fails due to an
out-of-memory failure, an ERROR is generated, leaving in the pgstats
shared hashtable an inconsistent entry due to the first step, as the
entry has already been inserted in the hashtable.  These broken entries
can then be found by other backends, crashing them.

There are only two callers of pgstat_init_entry(), when loading the
pgstats file at startup and when creating a new pgstats entry.  This
commit changes pgstat_init_entry() so as we use dsa_allocate_extended()
with DSA_ALLOC_NO_OOM, making it return NULL on allocation failure
instead of failing.  This way, a backend failing an entry creation can
take appropriate cleanup actions in the shared hashtable before throwing
an error.  Currently, this means removing the entry from the shared
hashtable before throwing the error for the allocation failure.

Out-of-memory errors unlikely happen in the wild, and we do not bother
with back-patches when these are fixed, usually.  However, the problem
dealt with here is a degree worse as it breaks the shared memory state
of pgstats, impacting other processes that may look at an inconsistent
entry that a different process has failed to create.

Author: Mikhail Kot <mikhail.kot@databricks.com>
Discussion: https://postgr.es/m/CAAi9E7jELo5_-sBENftnc2E8XhW2PKZJWfTC3i2y-GMQd2bcqQ@mail.gmail.com
Backpatch-through: 15
2025-09-08 15:52:48 +09:00
..
access Fix outdated comments in slru.c 2025-09-05 14:10:24 +09:00
archive Update copyright for 2025 2025-01-01 11:21:55 -05:00
backup Use XLOG_CONTROL_FILE macro consistently for control file name. 2025-04-07 09:27:33 +09:00
bootstrap Remove unnecessary (char *) casts [mem] 2025-02-12 08:50:13 +01:00
catalog Improve objectNamesToOids() comment 2025-08-27 17:33:54 +02:00
commands Fix replica identity check for INSERT ON CONFLICT DO UPDATE. 2025-09-04 11:32:00 +01:00
executor Fix concurrent update issue with MERGE. 2025-09-05 08:21:35 +01:00
foreign Track the number of presorted outer pathkeys in MergePath 2025-05-08 18:21:32 +09:00
jit Don't put library-supplied -L/-I switches before user-supplied ones. 2025-07-29 15:17:40 -04:00
lib Correct list of files in src/backend/lib/README 2025-06-27 09:31:23 +09:00
libpq Fix build breakage on Solaris-alikes with late-model GCC. 2025-07-23 15:44:29 -04:00
main Use thread-safe strftime_l() instead of strftime(). 2025-03-28 07:13:43 +01:00
nodes Adapt REL_18_STABLE to its new status as a stable branch 2025-06-29 23:00:00 -04:00
optimizer Fix planner error when estimating SubPlan cost 2025-09-03 16:03:43 +09:00
parser Update parser README to include parse_jsontable.c 2025-09-08 10:07:18 +09:00
partitioning Fix bug in cbc127917 to handle nested Append correctly 2025-02-25 09:24:42 +09:00
po Translation updates 2025-09-01 13:07:49 +02:00
port Don't treat EINVAL from semget() as a hard failure. 2025-08-13 11:59:47 -04:00
postmaster Use LW_SHARED in walsummarizer.c for WALSummarizerLock lock where possible. 2025-08-28 17:06:44 -07:00
regex Another unintentional behavior change in commit e9931bfb75. 2025-04-16 16:49:42 -07:00
replication Fix use-after-free issue in slot synchronization. 2025-09-03 06:17:08 +00:00
rewrite Change internal queryid type from uint64 to int64 2025-05-30 22:59:39 +12:00
snowball Use PG_MODULE_MAGIC_EXT in our installable shared libraries. 2025-03-26 11:11:02 -04:00
statistics Message style improvements 2025-08-28 09:09:05 +02:00
storage aio: Stop using enum bitfields due to bad code generation 2025-08-27 19:12:50 -04:00
tcop CREATE STATISTICS: improve misleading error message 2025-08-29 14:43:47 +02:00
tsearch Clear errno before calling strtol() in spell.c. 2025-03-08 11:24:25 -05:00
utils Fix corruption of pgstats shared hashtable due to OOM failures 2025-09-08 15:52:48 +09:00
.gitignore Add .gitignore entries for AIX-specific intermediate build artifacts. 2015-07-08 20:44:22 -04:00
common.mk Blind attempt to fix LLVM dependency in the backend 2022-09-15 10:53:48 +07:00
Makefile aio: Add liburing dependency 2025-03-26 19:45:32 -04:00
meson.build Update copyright for 2025 2025-01-01 11:21:55 -05:00
nls.mk Return yyparse() result not via global variable 2025-01-24 06:55:39 +01:00