postgres.git / summary / log / commit / refs
commit bdae2f8eaab09222dc4e3562d712e6580711ccf9
Author: Michael Paquier <michael@paquier.xyz>
Date: Thu Jun 18 05:05:27 2026 +0000
Make GetSnapshotData() more resilient on out-of-memory errors
If the allocation of Snapshot->subxip fails, a follow-up call of
GetSnapshotData() would see a partially-initialized snapshot, causing a
NULL dereference on reentry when using "subxip" because only "xip" would
be allocated. In the event of an out-of-memory error when allocating
"subxip", "xip" is now reset before throwing an ERROR, so as Snapshots
can be allocated and handled gracefully on retry.
Note: This has been originally applied in v19~ as of 29fb598b9cad, and
was initially not backpatched. This issue has been seen in the field on
older stable branches, hence the backpatch now.
Reported-by: Alexander Lakhin <exclusion@gmail.com>
Author: Matthias van de Meent <boekewurm+postgres@gmail.com>
Discussion: https://postgr.es/m/e77acaac-a1b3-40b3-99ee-5769b4e453e4@gmail.com
Discussion: https://postgr.es/m/CAJ7c6TO+7rrN5H_Odd8PLdmrEa4MgeoJ2AqnTydOcy=AyZvDsQ@mail.gmail.com
Backpatch-through: 15
src/backend/storage/ipc/procarray.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/procarray.c
index cfb0fcee440..d469879f039 100644
--- a/src/backend/storage/ipc/procarray.c
+++ b/src/backend/storage/ipc/procarray.c
@@ -2221,9 +2221,17 @@ GetSnapshotData(Snapshot snapshot)
snapshot->subxip = (TransactionId *)
malloc(GetMaxSnapshotSubxidCount() * sizeof(TransactionId));
if (snapshot->subxip == NULL)
+ {
+ /*
+ * Clean up the Snapshot state before throwing the error, so that
+ * a retry does not see a partially-initialized snapshot.
+ */
+ free(snapshot->xip);
+ snapshot->xip = NULL;
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
+ }
}
/*
[parent: 1fd94839ea87]