v1-0002-Make-GetSnapshotData-more-resilient-against-OOM-e.patch

application/octet-stream

Filename: v1-0002-Make-GetSnapshotData-more-resilient-against-OOM-e.patch
Type: application/octet-stream
Part: 0
Message: Re: Unexpected behavior after OOM errors

Patch

Format: format-patch
Series: patch v1-0002
Subject: Make GetSnapshotData more resilient against OOM errors
File+
src/backend/storage/ipc/procarray.c 11 0
From 286a9c26b55a9201ef69463d33b6c8db397ada5f Mon Sep 17 00:00:00 2001
From: Matthias van de Meent <boekewurm+postgres@gmail.com>
Date: Tue, 16 Jun 2026 15:07:40 +0200
Subject: [PATCH v1 2/2] Make GetSnapshotData more resilient against OOM errors

---
 src/backend/storage/ipc/procarray.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/procarray.c
index f540bb6b23f..d23ce3800e4 100644
--- a/src/backend/storage/ipc/procarray.c
+++ b/src/backend/storage/ipc/procarray.c
@@ -2158,9 +2158,20 @@ GetSnapshotData(Snapshot snapshot)
 		snapshot->subxip = (TransactionId *)
 			malloc(GetMaxSnapshotSubxidCount() * sizeof(TransactionId));
 		if (snapshot->subxip == NULL)
+		{
+			/*
+			 * Clean up Snapshot's state ahead of throwing the error -- it's
+			 * not guaranteed that this Snapshot is not reused.
+			 * Additionally, we'd possibly leak the xip allocation, so
+			 * better make sure to clean this up.
+			 */
+			free(snapshot->xip);
+			snapshot->xip = NULL;
+
 			ereport(ERROR,
 					(errcode(ERRCODE_OUT_OF_MEMORY),
 					 errmsg("out of memory")));
+		}
 	}
 
 	/*
-- 
2.50.1 (Apple Git-155)