@@ -781,13 +781,21 @@ MemoryContextAlloc(MemoryContext context, Size size)
781781context -> isReset = false;
782782
783783ret = context -> methods -> alloc (context ,size );
784- if (ret == NULL )
784+ if (unlikely ( ret == NULL ) )
785785{
786786MemoryContextStats (TopMemoryContext );
787+
788+ /*
789+ * Here, and elsewhere in this module, we show the target context's
790+ * "name" but not its "ident" (if any) in user-visible error messages.
791+ * The "ident" string might contain security-sensitive data, such as
792+ * values in SQL commands.
793+ */
787794ereport (ERROR ,
788795(errcode (ERRCODE_OUT_OF_MEMORY ),
789796errmsg ("out of memory" ),
790- errdetail ("Failed on request of size %zu." ,size )));
797+ errdetail ("Failed on request of size %zu in memory context \"%s\"." ,
798+ size ,context -> name )));
791799}
792800
793801VALGRIND_MEMPOOL_ALLOC (context ,ret ,size );
@@ -816,13 +824,14 @@ MemoryContextAllocZero(MemoryContext context, Size size)
816824context -> isReset = false;
817825
818826ret = context -> methods -> alloc (context ,size );
819- if (ret == NULL )
827+ if (unlikely ( ret == NULL ) )
820828{
821829MemoryContextStats (TopMemoryContext );
822830ereport (ERROR ,
823831(errcode (ERRCODE_OUT_OF_MEMORY ),
824832errmsg ("out of memory" ),
825- errdetail ("Failed on request of size %zu." ,size )));
833+ errdetail ("Failed on request of size %zu in memory context \"%s\"." ,
834+ size ,context -> name )));
826835}
827836
828837VALGRIND_MEMPOOL_ALLOC (context ,ret ,size );
@@ -853,13 +862,14 @@ MemoryContextAllocZeroAligned(MemoryContext context, Size size)
853862context -> isReset = false;
854863
855864ret = context -> methods -> alloc (context ,size );
856- if (ret == NULL )
865+ if (unlikely ( ret == NULL ) )
857866{
858867MemoryContextStats (TopMemoryContext );
859868ereport (ERROR ,
860869(errcode (ERRCODE_OUT_OF_MEMORY ),
861870errmsg ("out of memory" ),
862- errdetail ("Failed on request of size %zu." ,size )));
871+ errdetail ("Failed on request of size %zu in memory context \"%s\"." ,
872+ size ,context -> name )));
863873}
864874
865875VALGRIND_MEMPOOL_ALLOC (context ,ret ,size );
@@ -888,15 +898,16 @@ MemoryContextAllocExtended(MemoryContext context, Size size, int flags)
888898context -> isReset = false;
889899
890900ret = context -> methods -> alloc (context ,size );
891- if (ret == NULL )
901+ if (unlikely ( ret == NULL ) )
892902{
893903if ((flags & MCXT_ALLOC_NO_OOM )== 0 )
894904{
895905MemoryContextStats (TopMemoryContext );
896906ereport (ERROR ,
897907(errcode (ERRCODE_OUT_OF_MEMORY ),
898908errmsg ("out of memory" ),
899- errdetail ("Failed on request of size %zu." ,size )));
909+ errdetail ("Failed on request of size %zu in memory context \"%s\"." ,
910+ size ,context -> name )));
900911}
901912return NULL ;
902913}
@@ -914,26 +925,28 @@ palloc(Size size)
914925{
915926/* duplicates MemoryContextAlloc to avoid increased overhead */
916927void * ret ;
928+ MemoryContext context = CurrentMemoryContext ;
917929
918- AssertArg (MemoryContextIsValid (CurrentMemoryContext ));
919- AssertNotInCriticalSection (CurrentMemoryContext );
930+ AssertArg (MemoryContextIsValid (context ));
931+ AssertNotInCriticalSection (context );
920932
921933if (!AllocSizeIsValid (size ))
922934elog (ERROR ,"invalid memory alloc request size %zu" ,size );
923935
924- CurrentMemoryContext -> isReset = false;
936+ context -> isReset = false;
925937
926- ret = CurrentMemoryContext -> methods -> alloc (CurrentMemoryContext ,size );
927- if (ret == NULL )
938+ ret = context -> methods -> alloc (context ,size );
939+ if (unlikely ( ret == NULL ) )
928940{
929941MemoryContextStats (TopMemoryContext );
930942ereport (ERROR ,
931943(errcode (ERRCODE_OUT_OF_MEMORY ),
932944errmsg ("out of memory" ),
933- errdetail ("Failed on request of size %zu." ,size )));
945+ errdetail ("Failed on request of size %zu in memory context \"%s\"." ,
946+ size ,context -> name )));
934947}
935948
936- VALGRIND_MEMPOOL_ALLOC (CurrentMemoryContext ,ret ,size );
949+ VALGRIND_MEMPOOL_ALLOC (context ,ret ,size );
937950
938951return ret ;
939952}
@@ -943,26 +956,28 @@ palloc0(Size size)
943956{
944957/* duplicates MemoryContextAllocZero to avoid increased overhead */
945958void * ret ;
959+ MemoryContext context = CurrentMemoryContext ;
946960
947- AssertArg (MemoryContextIsValid (CurrentMemoryContext ));
948- AssertNotInCriticalSection (CurrentMemoryContext );
961+ AssertArg (MemoryContextIsValid (context ));
962+ AssertNotInCriticalSection (context );
949963
950964if (!AllocSizeIsValid (size ))
951965elog (ERROR ,"invalid memory alloc request size %zu" ,size );
952966
953- CurrentMemoryContext -> isReset = false;
967+ context -> isReset = false;
954968
955- ret = CurrentMemoryContext -> methods -> alloc (CurrentMemoryContext ,size );
956- if (ret == NULL )
969+ ret = context -> methods -> alloc (context ,size );
970+ if (unlikely ( ret == NULL ) )
957971{
958972MemoryContextStats (TopMemoryContext );
959973ereport (ERROR ,
960974(errcode (ERRCODE_OUT_OF_MEMORY ),
961975errmsg ("out of memory" ),
962- errdetail ("Failed on request of size %zu." ,size )));
976+ errdetail ("Failed on request of size %zu in memory context \"%s\"." ,
977+ size ,context -> name )));
963978}
964979
965- VALGRIND_MEMPOOL_ALLOC (CurrentMemoryContext ,ret ,size );
980+ VALGRIND_MEMPOOL_ALLOC (context ,ret ,size );
966981
967982MemSetAligned (ret ,0 ,size );
968983
@@ -974,31 +989,33 @@ palloc_extended(Size size, int flags)
974989{
975990/* duplicates MemoryContextAllocExtended to avoid increased overhead */
976991void * ret ;
992+ MemoryContext context = CurrentMemoryContext ;
977993
978- AssertArg (MemoryContextIsValid (CurrentMemoryContext ));
979- AssertNotInCriticalSection (CurrentMemoryContext );
994+ AssertArg (MemoryContextIsValid (context ));
995+ AssertNotInCriticalSection (context );
980996
981997if (((flags & MCXT_ALLOC_HUGE )!= 0 && !AllocHugeSizeIsValid (size ))||
982998((flags & MCXT_ALLOC_HUGE )== 0 && !AllocSizeIsValid (size )))
983999elog (ERROR ,"invalid memory alloc request size %zu" ,size );
9841000
985- CurrentMemoryContext -> isReset = false;
1001+ context -> isReset = false;
9861002
987- ret = CurrentMemoryContext -> methods -> alloc (CurrentMemoryContext ,size );
988- if (ret == NULL )
1003+ ret = context -> methods -> alloc (context ,size );
1004+ if (unlikely ( ret == NULL ) )
9891005{
9901006if ((flags & MCXT_ALLOC_NO_OOM )== 0 )
9911007{
9921008MemoryContextStats (TopMemoryContext );
9931009ereport (ERROR ,
9941010(errcode (ERRCODE_OUT_OF_MEMORY ),
9951011errmsg ("out of memory" ),
996- errdetail ("Failed on request of size %zu." ,size )));
1012+ errdetail ("Failed on request of size %zu in memory context \"%s\"." ,
1013+ size ,context -> name )));
9971014}
9981015return NULL ;
9991016}
10001017
1001- VALGRIND_MEMPOOL_ALLOC (CurrentMemoryContext ,ret ,size );
1018+ VALGRIND_MEMPOOL_ALLOC (context ,ret ,size );
10021019
10031020if ((flags & MCXT_ALLOC_ZERO )!= 0 )
10041021MemSetAligned (ret ,0 ,size );
@@ -1038,13 +1055,14 @@ repalloc(void *pointer, Size size)
10381055Assert (!context -> isReset );
10391056
10401057ret = context -> methods -> realloc (context ,pointer ,size );
1041- if (ret == NULL )
1058+ if (unlikely ( ret == NULL ) )
10421059{
10431060MemoryContextStats (TopMemoryContext );
10441061ereport (ERROR ,
10451062(errcode (ERRCODE_OUT_OF_MEMORY ),
10461063errmsg ("out of memory" ),
1047- errdetail ("Failed on request of size %zu." ,size )));
1064+ errdetail ("Failed on request of size %zu in memory context \"%s\"." ,
1065+ size ,context -> name )));
10481066}
10491067
10501068VALGRIND_MEMPOOL_CHANGE (context ,pointer ,ret ,size );
@@ -1072,13 +1090,14 @@ MemoryContextAllocHuge(MemoryContext context, Size size)
10721090context -> isReset = false;
10731091
10741092ret = context -> methods -> alloc (context ,size );
1075- if (ret == NULL )
1093+ if (unlikely ( ret == NULL ) )
10761094{
10771095MemoryContextStats (TopMemoryContext );
10781096ereport (ERROR ,
10791097(errcode (ERRCODE_OUT_OF_MEMORY ),
10801098errmsg ("out of memory" ),
1081- errdetail ("Failed on request of size %zu." ,size )));
1099+ errdetail ("Failed on request of size %zu in memory context \"%s\"." ,
1100+ size ,context -> name )));
10821101}
10831102
10841103VALGRIND_MEMPOOL_ALLOC (context ,ret ,size );
@@ -1106,13 +1125,14 @@ repalloc_huge(void *pointer, Size size)
11061125Assert (!context -> isReset );
11071126
11081127ret = context -> methods -> realloc (context ,pointer ,size );
1109- if (ret == NULL )
1128+ if (unlikely ( ret == NULL ) )
11101129{
11111130MemoryContextStats (TopMemoryContext );
11121131ereport (ERROR ,
11131132(errcode (ERRCODE_OUT_OF_MEMORY ),
11141133errmsg ("out of memory" ),
1115- errdetail ("Failed on request of size %zu." ,size )));
1134+ errdetail ("Failed on request of size %zu in memory context \"%s\"." ,
1135+ size ,context -> name )));
11161136}
11171137
11181138VALGRIND_MEMPOOL_CHANGE (context ,pointer ,ret ,size );