diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c
index 502ca44..4438ed8 100644
*** a/src/backend/utils/adt/varchar.c
--- b/src/backend/utils/adt/varchar.c
***************
*** 19,24 ****
--- 19,25 ----
  #include "access/tuptoaster.h"
  #include "libpq/pqformat.h"
  #include "nodes/nodeFuncs.h"
+ #include "tcop/tcopprot.h"
  #include "utils/array.h"
  #include "utils/builtins.h"
  #include "mb/pg_wchar.h"
*************** varchar(PG_FUNCTION_ARGS)
*** 617,626 ****
  	{
  		for (i = maxmblen; i < len; i++)
  			if (s_data[i] != ' ')
  				ereport(ERROR,
  						(errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
  					  errmsg("value too long for type character varying(%d)",
! 							 maxlen)));
  	}
  
  	PG_RETURN_VARCHAR_P((VarChar *) cstring_to_text_with_len(s_data,
--- 618,645 ----
  	{
  		for (i = maxmblen; i < len; i++)
  			if (s_data[i] != ' ')
+ 			{
+ 				int			pos = 0;
+ 
+ 				if (debug_query_string &&
+ 					fcinfo->flinfo->fn_expr)
+ 				{
+ 					int location = exprLocation(fcinfo->flinfo->fn_expr);
+ 
+ 					if (location >= 0)
+ 					{
+ 						/* Convert offset to character number */
+ 						pos = pg_mbstrlen_with_len(debug_query_string,
+ 												   location) + 1;
+ 					}
+ 				}
+ 
  				ereport(ERROR,
  						(errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
  					  errmsg("value too long for type character varying(%d)",
! 							 maxlen),
! 						 errposition(pos)));
! 			}
  	}
  
  	PG_RETURN_VARCHAR_P((VarChar *) cstring_to_text_with_len(s_data,
