From a4e0846e34ed4c8255a6bab1ca675c73535466fd Mon Sep 17 00:00:00 2001
From: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Date: Sat, 12 Sep 2026 13:10:18 +0530
Subject: [PATCH v1 2/2] Avoid core dumps for WAL disk-full failures

Use errnocoredump_on_errno(ENOSPC) for selected WAL and control-file I/O
errors that report or may be promoted to PANIC.  They retain existing
PANIC handling, while ENOSPC terminates without asking the operating
system for a core file.  Other errno values, corruption failures, and
internal-invariant PANICs still use abort().

Cover WAL segment initialization, copies, writes, syncs, opens and
closes, control-file creation and updates, and WAL reads during
recovery.

Control-file I/O can run in the postmaster.  postgresql.conf has already
been read before its initial control-file read, so the override is
available there.  During crash reinitialization, the postmaster reaches
the control-file read only after all child processes have exited.
---
 src/backend/access/transam/xlog.c         | 21 +++++++++++++++++++++
 src/backend/access/transam/xlogrecovery.c |  2 ++
 src/common/controldata_utils.c            |  4 ++++
 3 files changed, 27 insertions(+)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index c7a8d64c1f7..5c8dae6deaa 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2477,6 +2477,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 					errno = save_errno;
 					ereport(PANIC,
 							(errcode_for_file_access(),
+							 errnocoredump_on_errno(ENOSPC),
 							 errmsg("could not write to log file \"%s\" at offset %u, length %zu: %m",
 									xlogfname, startoffset, nleft)));
 				}
@@ -3273,6 +3274,7 @@ XLogFileInitInternal(XLogSegNo logsegno, TimeLineID logtli,
 		if (errno != ENOENT)
 			ereport(ERROR,
 					(errcode_for_file_access(),
+					 errnocoredump_on_errno(ENOSPC),
 					 errmsg("could not open file \"%s\": %m", path)));
 	}
 	else
@@ -3298,6 +3300,7 @@ XLogFileInitInternal(XLogSegNo logsegno, TimeLineID logtli,
 	if (fd < 0)
 		ereport(ERROR,
 				(errcode_for_file_access(),
+				 errnocoredump_on_errno(ENOSPC),
 				 errmsg("could not create file \"%s\": %m", tmppath)));
 
 	/* Measure I/O timing when initializing segment */
@@ -3351,6 +3354,7 @@ XLogFileInitInternal(XLogSegNo logsegno, TimeLineID logtli,
 
 		ereport(ERROR,
 				(errcode_for_file_access(),
+				 errnocoredump_on_errno(ENOSPC),
 				 errmsg("could not write to file \"%s\": %m", tmppath)));
 	}
 
@@ -3373,6 +3377,7 @@ XLogFileInitInternal(XLogSegNo logsegno, TimeLineID logtli,
 		errno = save_errno;
 		ereport(ERROR,
 				(errcode_for_file_access(),
+				 errnocoredump_on_errno(ENOSPC),
 				 errmsg("could not fsync file \"%s\": %m", tmppath)));
 	}
 	pgstat_report_wait_end();
@@ -3383,6 +3388,7 @@ XLogFileInitInternal(XLogSegNo logsegno, TimeLineID logtli,
 	if (close(fd) != 0)
 		ereport(ERROR,
 				(errcode_for_file_access(),
+				 errnocoredump_on_errno(ENOSPC),
 				 errmsg("could not close file \"%s\": %m", tmppath)));
 
 	/*
@@ -3453,6 +3459,7 @@ XLogFileInit(XLogSegNo logsegno, TimeLineID logtli)
 	if (fd < 0)
 		ereport(ERROR,
 				(errcode_for_file_access(),
+				 errnocoredump_on_errno(ENOSPC),
 				 errmsg("could not open file \"%s\": %m", path)));
 	return fd;
 }
@@ -3571,6 +3578,7 @@ XLogFileCopy(TimeLineID destTLI, XLogSegNo destsegno,
 	if (pg_fsync(fd) != 0)
 		ereport(data_sync_elevel(ERROR),
 				(errcode_for_file_access(),
+				 errnocoredump_on_errno(ENOSPC),
 				 errmsg("could not fsync file \"%s\": %m", tmppath)));
 	pgstat_report_wait_end();
 
@@ -3685,6 +3693,7 @@ XLogFileOpen(XLogSegNo segno, TimeLineID tli)
 	if (fd < 0)
 		ereport(PANIC,
 				(errcode_for_file_access(),
+				 errnocoredump_on_errno(ENOSPC),
 				 errmsg("could not open file \"%s\": %m", path)));
 
 	return fd;
@@ -3718,6 +3727,7 @@ XLogFileClose(void)
 		errno = save_errno;
 		ereport(PANIC,
 				(errcode_for_file_access(),
+				 errnocoredump_on_errno(ENOSPC),
 				 errmsg("could not close file \"%s\": %m", xlogfname)));
 	}
 
@@ -4378,6 +4388,7 @@ WriteControlFile(void)
 	if (fd < 0)
 		ereport(PANIC,
 				(errcode_for_file_access(),
+				 errnocoredump_on_errno(ENOSPC),
 				 errmsg("could not create file \"%s\": %m",
 						XLOG_CONTROL_FILE)));
 
@@ -4390,6 +4401,7 @@ WriteControlFile(void)
 			errno = ENOSPC;
 		ereport(PANIC,
 				(errcode_for_file_access(),
+				 errnocoredump_on_errno(ENOSPC),
 				 errmsg("could not write to file \"%s\": %m",
 						XLOG_CONTROL_FILE)));
 	}
@@ -4399,6 +4411,7 @@ WriteControlFile(void)
 	if (pg_fsync(fd) != 0)
 		ereport(PANIC,
 				(errcode_for_file_access(),
+				 errnocoredump_on_errno(ENOSPC),
 				 errmsg("could not fsync file \"%s\": %m",
 						XLOG_CONTROL_FILE)));
 	pgstat_report_wait_end();
@@ -4406,6 +4419,7 @@ WriteControlFile(void)
 	if (close(fd) != 0)
 		ereport(PANIC,
 				(errcode_for_file_access(),
+				 errnocoredump_on_errno(ENOSPC),
 				 errmsg("could not close file \"%s\": %m",
 						XLOG_CONTROL_FILE)));
 }
@@ -4426,6 +4440,7 @@ ReadControlFile(void)
 	if (fd < 0)
 		ereport(PANIC,
 				(errcode_for_file_access(),
+				 errnocoredump_on_errno(ENOSPC),
 				 errmsg("could not open file \"%s\": %m",
 						XLOG_CONTROL_FILE)));
 
@@ -4436,6 +4451,7 @@ ReadControlFile(void)
 		if (r < 0)
 			ereport(PANIC,
 					(errcode_for_file_access(),
+					 errnocoredump_on_errno(ENOSPC),
 					 errmsg("could not read file \"%s\": %m",
 							XLOG_CONTROL_FILE)));
 		else
@@ -5602,6 +5618,7 @@ BootStrapXLOG(uint32 data_checksum_version)
 			errno = ENOSPC;
 		ereport(PANIC,
 				(errcode_for_file_access(),
+				 errnocoredump_on_errno(ENOSPC),
 				 errmsg("could not write bootstrap write-ahead log file: %m")));
 	}
 	pgstat_report_wait_end();
@@ -5610,12 +5627,14 @@ BootStrapXLOG(uint32 data_checksum_version)
 	if (pg_fsync(openLogFile) != 0)
 		ereport(PANIC,
 				(errcode_for_file_access(),
+				 errnocoredump_on_errno(ENOSPC),
 				 errmsg("could not fsync bootstrap write-ahead log file: %m")));
 	pgstat_report_wait_end();
 
 	if (close(openLogFile) != 0)
 		ereport(PANIC,
 				(errcode_for_file_access(),
+				 errnocoredump_on_errno(ENOSPC),
 				 errmsg("could not close bootstrap write-ahead log file: %m")));
 
 	openLogFile = -1;
@@ -9407,6 +9426,7 @@ assign_wal_sync_method(int new_wal_sync_method, void *extra)
 				errno = save_errno;
 				ereport(PANIC,
 						(errcode_for_file_access(),
+						 errnocoredump_on_errno(ENOSPC),
 						 errmsg("could not fsync file \"%s\": %m", xlogfname)));
 			}
 
@@ -9485,6 +9505,7 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 		errno = save_errno;
 		ereport(PANIC,
 				(errcode_for_file_access(),
+				 errnocoredump_on_errno(ENOSPC),
 				 errmsg(msg, xlogfname)));
 	}
 
diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c
index fff8d57ac61..3e80cea942a 100644
--- a/src/backend/access/transam/xlogrecovery.c
+++ b/src/backend/access/transam/xlogrecovery.c
@@ -3415,6 +3415,7 @@ retry:
 			errno = save_errno;
 			ereport(emode_for_corrupt_record(emode, targetPagePtr + reqLen),
 					(errcode_for_file_access(),
+					 errnocoredump_on_errno(ENOSPC),
 					 errmsg("could not read from WAL segment %s, LSN %X/%08X, offset %u: %m",
 							fname, LSN_FORMAT_ARGS(targetPagePtr),
 							readOff)));
@@ -4287,6 +4288,7 @@ XLogFileRead(XLogSegNo segno, TimeLineID tli,
 	if (errno != ENOENT || !notfoundOk) /* unexpected failure? */
 		ereport(PANIC,
 				(errcode_for_file_access(),
+				 errnocoredump_on_errno(ENOSPC),
 				 errmsg("could not open file \"%s\": %m", path)));
 	return -1;
 }
diff --git a/src/common/controldata_utils.c b/src/common/controldata_utils.c
index 0e8e03c566c..4c566b46b88 100644
--- a/src/common/controldata_utils.c
+++ b/src/common/controldata_utils.c
@@ -223,6 +223,7 @@ update_controlfile(const char *DataDir,
 	if ((fd = BasicOpenFile(ControlFilePath, O_RDWR | PG_BINARY)) < 0)
 		ereport(PANIC,
 				(errcode_for_file_access(),
+				 errnocoredump_on_errno(ENOSPC),
 				 errmsg("could not open file \"%s\": %m",
 						ControlFilePath)));
 #else
@@ -244,6 +245,7 @@ update_controlfile(const char *DataDir,
 #ifndef FRONTEND
 		ereport(PANIC,
 				(errcode_for_file_access(),
+				 errnocoredump_on_errno(ENOSPC),
 				 errmsg("could not write file \"%s\": %m",
 						ControlFilePath)));
 #else
@@ -261,6 +263,7 @@ update_controlfile(const char *DataDir,
 		if (pg_fsync(fd) != 0)
 			ereport(PANIC,
 					(errcode_for_file_access(),
+					 errnocoredump_on_errno(ENOSPC),
 					 errmsg("could not fsync file \"%s\": %m",
 							ControlFilePath)));
 		pgstat_report_wait_end();
@@ -275,6 +278,7 @@ update_controlfile(const char *DataDir,
 #ifndef FRONTEND
 		ereport(PANIC,
 				(errcode_for_file_access(),
+				 errnocoredump_on_errno(ENOSPC),
 				 errmsg("could not close file \"%s\": %m",
 						ControlFilePath)));
 #else
-- 
2.34.1

