 src/backend/utils/mmgr/aset.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/src/backend/utils/mmgr/aset.c b/src/backend/utils/mmgr/aset.c
index e3d2c4e..8835db8 100644
--- a/src/backend/utils/mmgr/aset.c
+++ b/src/backend/utils/mmgr/aset.c
@@ -134,8 +134,12 @@ typedef struct AllocSetContext
 	AllocBlock	keeper;			/* keep this block over resets */
 	/* freelist this context could be put in, or -1 if not a candidate: */
 	int			freeListIndex;	/* index in context_freelists[], or -1 */
+	Size		currentAllocSize;	/* KG: current allocation size */
+	Size		nextWatermarkSize;	/* KG: next watermark limitation */
 } AllocSetContext;
 
+#define WATERMARK_UNITSIZE		(8UL << 30)
+
 typedef AllocSetContext *AllocSet;
 
 /*
@@ -321,8 +325,21 @@ static const unsigned char LogTable256[256] =
 			fprintf(stderr, "AllocAlloc: %s: %p, %zu\n", \
 				(_cxt)->header.name, (_chunk), (_chunk)->size)
 #else
-#define AllocFreeInfo(_cxt, _chunk)
-#define AllocAllocInfo(_cxt, _chunk)
+#define AllocFreeInfo(_cxt, _chunk)									\
+	do {															\
+		(_cxt)->currentAllocSize -= (_chunk)->size;					\
+	} while(0)
+#define AllocAllocInfo(_cxt, _chunk)								\
+	do {															\
+		(_cxt)->currentAllocSize += (_chunk)->size;					\
+		if ((_cxt)->currentAllocSize > (_cxt)->nextWatermarkSize)	\
+		{															\
+			elog(WARNING, "memory context '%s' grows up %zu bytes",	\
+				 (_cxt)->header.name,								\
+				 (_cxt)->currentAllocSize);							\
+			(_cxt)->nextWatermarkSize += WATERMARK_UNITSIZE;		\
+		}															\
+	} while(0)
 #endif
 
 /* ----------
@@ -512,6 +529,8 @@ AllocSetContextCreateExtended(MemoryContext parent,
 	set->maxBlockSize = maxBlockSize;
 	set->nextBlockSize = initBlockSize;
 	set->freeListIndex = freeListIndex;
+	set->currentAllocSize = 0;
+	set->nextWatermarkSize = WATERMARK_UNITSIZE;
 
 	/*
 	 * Compute the allocation chunk size limit for this context.  It can't be
@@ -612,6 +631,9 @@ AllocSetReset(MemoryContext context)
 
 	/* Reset block size allocation sequence, too */
 	set->nextBlockSize = set->initBlockSize;
+
+	set->currentAllocSize = 0;
+	set->nextWatermarkSize = WATERMARK_UNITSIZE;
 }
 
 /*
