From 7ca80f33b806e2b322e9631e01046173b276e87e Mon Sep 17 00:00:00 2001
From: Pierre Forstmann <pierre.forstmann@gmail.com>
Date: Fri, 21 Aug 2026 13:56:29 +0200
Subject: [PATCH] v1-0001-Fix-memory-allocation-check

---
 src/backend/partitioning/partbounds.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/backend/partitioning/partbounds.c b/src/backend/partitioning/partbounds.c
index 865a92c8561..15ab0f715d4 100644
--- a/src/backend/partitioning/partbounds.c
+++ b/src/backend/partitioning/partbounds.c
@@ -2918,6 +2918,14 @@ check_new_partition_bound(char *relname, Relation parent,
 				Assert(spec->strategy == PARTITION_STRATEGY_HASH);
 				Assert(spec->remainder >= 0 && spec->remainder < spec->modulus);
 
+                                /* Check needed memory that will be allocated in create_hash_bounds() */
+                                if (spec->modulus * sizeof(int) > MaxAllocSize)
+                                        ereport(ERROR, (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                                                (errmsg("hash partitions bounds are too large"),
+                                                 errdetail("Creating hash partitions for modulus %d would require too much memory.", spec->modulus),
+                                                 errhint("Reduce the number of partitions."))));
+
+
 				if (partdesc->nparts > 0)
 				{
 					int			greatest_modulus;
@@ -3030,8 +3038,10 @@ check_new_partition_bound(char *relname, Relation parent,
 						}
 						remainder += spec->modulus;
 					} while (remainder < greatest_modulus);
+
 				}
 
+
 				break;
 			}
 
-- 
2.47.3

