*** src.orig/backend/utils/mb/mbutils.c	2009-02-25 18:53:43.000000000 +0300
--- src/backend/utils/mb/mbutils.c	2009-02-25 21:19:53.000000000 +0300
***************
*** 601,607 ****
--- 601,610 ----
  	}
  	else
  #endif   /* WIN32 */
+ 	{
+ 		Assert( !lc_ctype_is_c() );
  		result = wcstombs(to, from, tolen);
+ 	}
  	return result;
  }
  
***************
*** 647,668 ****
  	else
  #endif   /* WIN32 */
  	{
! 		if (lc_ctype_is_c())
! 		{
! 			/*
! 			 * pg_mb2wchar_with_len always adds trailing '\0', so 'to' should be
! 			 * allocated with sufficient space
! 			 */
! 			result = pg_mb2wchar_with_len(from, (pg_wchar *) to, fromlen);
! 		}
! 		else
! 		{
! 			/* mbstowcs requires ending '\0' */
! 			char	   *str = pnstrdup(from, fromlen);
  
! 			result = mbstowcs(to, str, tolen);
! 			pfree(str);
! 		}
  	}
  
  	if (result == -1)
--- 650,661 ----
  	else
  #endif   /* WIN32 */
  	{
! 		/* mbstowcs requires ending '\0' */
! 		char	   *str = pnstrdup(from, fromlen);
  
! 		Assert( !lc_ctype_is_c() );
! 		result = mbstowcs(to, str, tolen);
! 		pfree(str);
  	}
  
  	if (result == -1)
*** src.orig/backend/tsearch/wparser_def.c	2009-02-25 18:53:43.000000000 +0300
--- src/backend/tsearch/wparser_def.c	2009-02-25 21:34:19.000000000 +0300
***************
*** 240,251 ****
  	int			lenstr;			/* length of mbstring */
  #ifdef USE_WIDE_UPPER_LOWER
  	wchar_t    *wstr;			/* wide character string */
! 	int			lenwstr;		/* length of wsting */
  #endif
  
  	/* State of parse */
  	int			charmaxlen;
- 	bool		usewide;
  	TParserPosition *state;
  	bool		ignore;
  	bool		wanthost;
--- 240,251 ----
  	int			lenstr;			/* length of mbstring */
  #ifdef USE_WIDE_UPPER_LOWER
  	wchar_t    *wstr;			/* wide character string */
! 	pg_wchar   *pgwstr;			/* wide character string for C-locale */
! 	bool		usewide;
  #endif
  
  	/* State of parse */
  	int			charmaxlen;
  	TParserPosition *state;
  	bool		ignore;
  	bool		wanthost;
***************
*** 299,311 ****
  	if (prs->charmaxlen > 1)
  	{
  		prs->usewide = true;
! 		prs->wstr = (wchar_t *) palloc(sizeof(wchar_t) * (prs->lenstr + 1));
! 		prs->lenwstr = char2wchar(prs->wstr, prs->lenstr + 1,
! 								  prs->str, prs->lenstr);
  	}
  	else
- #endif
  		prs->usewide = false;
  
  	prs->state = newTParserPosition(NULL);
  	prs->state->state = TPS_Base;
--- 299,322 ----
  	if (prs->charmaxlen > 1)
  	{
  		prs->usewide = true;
! 		if ( lc_ctype_is_c() )
! 		{
! 			/*
! 			 * char2wchar doesn't work for C-locale and
! 			 * sizeof(pg_wchar) could be not equal to sizeof(wchar_t)
! 			 */
! 			prs->pgwstr = (pg_wchar*) palloc(sizeof(pg_wchar) * (prs->lenstr + 1));
! 			pg_mb2wchar_with_len(prs->str, prs->pgwstr, prs->lenstr);
! 		}
! 		else
! 		{
! 			prs->wstr = (wchar_t *) palloc(sizeof(wchar_t) * (prs->lenstr + 1));
! 			char2wchar(prs->wstr, prs->lenstr + 1, prs->str, prs->lenstr);
! 		}
  	}
  	else
  		prs->usewide = false;
+ #endif
  
  	prs->state = newTParserPosition(NULL);
  	prs->state->state = TPS_Base;
***************
*** 331,336 ****
--- 342,349 ----
  #ifdef USE_WIDE_UPPER_LOWER
  	if (prs->wstr)
  		pfree(prs->wstr);
+ 	if (prs->pgwstr)
+ 		pfree(prs->pgwstr);
  #endif
  
  	pfree(prs);
***************
*** 338,347 ****
  
  /*
   * Character-type support functions, equivalent to is* macros, but
!  * working with any possible encodings and locales. Note,
!  * that with multibyte encoding and C-locale isw* function may fail
!  * or give wrong result. Note 2: multibyte encoding and C-locale
!  * often are used for Asian languages
   */
  
  #ifdef USE_WIDE_UPPER_LOWER
--- 351,362 ----
  
  /*
   * Character-type support functions, equivalent to is* macros, but
!  * working with any possible encodings and locales. Notes:
!  *  - with multibyte encoding and C-locale isw* function may fail
!  *    or give wrong result. 
!  *  - multibyte encoding and C-locale often are used for 
!  *    Asian languages.
!  *  - if locale is C the we use pgwstr instead of wstr
   */
  
  #ifdef USE_WIDE_UPPER_LOWER
***************
*** 352,365 ****
  	Assert( prs->state );													\
  	if ( prs->usewide )														\
  	{																		\
! 		if ( lc_ctype_is_c() )												\
! 			return is##type( 0xff & *( prs->wstr + prs->state->poschar) );	\
  																			\
  		return isw##type( *(wint_t*)( prs->wstr + prs->state->poschar ) );	\
  	}																		\
  																			\
  	return is##type( *(unsigned char*)( prs->str + prs->state->posbyte ) ); \
! }	\
  																			\
  static int																	\
  p_isnot##type(TParser *prs) {												\
--- 367,380 ----
  	Assert( prs->state );													\
  	if ( prs->usewide )														\
  	{																		\
! 		if ( prs->pgwstr )													\
! 			return is##type( 0xff & *( prs->pgwstr + prs->state->poschar) );\
  																			\
  		return isw##type( *(wint_t*)( prs->wstr + prs->state->poschar ) );	\
  	}																		\
  																			\
  	return is##type( *(unsigned char*)( prs->str + prs->state->posbyte ) ); \
! }																			\
  																			\
  static int																	\
  p_isnot##type(TParser *prs) {												\
***************
*** 373,381 ****
  
  	if (prs->usewide)
  	{
! 		if (lc_ctype_is_c())
  		{
! 			unsigned int c = *(prs->wstr + prs->state->poschar);
  
  			/*
  			 * any non-ascii symbol with multibyte encoding with C-locale is
--- 388,396 ----
  
  	if (prs->usewide)
  	{
! 		if (prs->pgwstr)
  		{
! 			unsigned int c = *(prs->pgwstr + prs->state->poschar);
  
  			/*
  			 * any non-ascii symbol with multibyte encoding with C-locale is
***************
*** 405,413 ****
  
  	if (prs->usewide)
  	{
! 		if (lc_ctype_is_c())
  		{
! 			unsigned int c = *(prs->wstr + prs->state->poschar);
  
  			/*
  			 * any non-ascii symbol with multibyte encoding with C-locale is
--- 420,428 ----
  
  	if (prs->usewide)
  	{
! 		if (prs->pgwstr)
  		{
! 			unsigned int c = *(prs->pgwstr + prs->state->poschar);
  
  			/*
  			 * any non-ascii symbol with multibyte encoding with C-locale is
