diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c
index 898c52099b..93d9bcce93 100644
--- a/src/backend/utils/adt/numeric.c
+++ b/src/backend/utils/adt/numeric.c
@@ -753,14 +753,20 @@ numeric_in(PG_FUNCTION_ARGS)
 		if (base == 10)
 		{
 			if (!set_var_from_str(str, cp, &value, &cp, escontext))
+			{
+				free_var(&value);
 				PG_RETURN_NULL();
+			}
 			value.sign = sign;
 		}
 		else
 		{
 			if (!set_var_from_non_decimal_integer_str(str, cp + 2, sign, base,
 													  &value, &cp, escontext))
+			{
+				free_var(&value);
 				PG_RETURN_NULL();
+			}
 		}
 
 		/*
@@ -775,7 +781,10 @@ numeric_in(PG_FUNCTION_ARGS)
 		}
 
 		if (!apply_typmod(&value, typmod, escontext))
+		{
+			free_var(&value);		
 			PG_RETURN_NULL();
+		}
 
 		res = make_result_opt_error(&value, &have_error);