Subject: Correct SQLSTATE for ENOMEM in file access

From: Alexander Kuzmenkov <[email-masked]>
Date: Fri, 2 Feb 2024 20:02:37 +0100

Looking through the logs of some server that were experiencing out of
memory errors, I noticed that errcode_for_file_access() reports
ERRCODE_INTERNAL_ERROR for ENOMEM, while the correct SQLSTATE for this
should probably be ERRCODE_OUT_OF_MEMORY. Attached is a small patch to
fix this.

---
Alexander Kuzmenkov
Timescale
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index 2c7a20e3d3..9860bf079a 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -922,6 +922,10 @@ errcode_for_file_access(void)
 			edata->sqlerrcode = ERRCODE_DISK_FULL;
 			break;
 
+		case ENOMEM:	        /* Out of memory */
+			edata->sqlerrcode = ERRCODE_OUT_OF_MEMORY;
+			break;
+
 		case ENFILE:			/* File table overflow */
 		case EMFILE:			/* Too many open files */
 			edata->sqlerrcode = ERRCODE_INSUFFICIENT_RESOURCES;

---

From: Tom Lane <[email-masked]>
Date: Fri, 02 Feb 2024 14:12:13 -0500

> should probably be ERRCODE_OUT_OF_MEMORY. Attached is a small patch to

Hmm, do you think this is actually reachable?  AFAIK we should only be
calling errcode_for_file_access() after functions that are unlikely to
report ENOMEM.

			regards, tom lane