diff --git a/config/c-library.m4 b/config/c-library.m4
index 1d28c45..8140a03 100644
--- a/config/c-library.m4
+++ b/config/c-library.m4
@@ -317,3 +317,40 @@ if test "$pgac_cv_type_locale_t" = 'yes (in xlocale.h)'; then
   AC_DEFINE(LOCALE_T_IN_XLOCALE, 1,
             [Define to 1 if `locale_t' requires <xlocale.h>.])
 fi])])# PGAC_HEADER_XLOCALE
+
+# PGAC_C_WCSTOMBS_L_IN_XLOCALE
+# -------------------------
+# Check if wcstombs_l, mbstowcs_l, etc are declared in xlocale.h
+# and define WCSTOMBS_L_IN_XLOCALE if so.
+#
+AC_DEFUN([PGAC_C_WCSTOMBS_L_IN_XLOCALE],
+[
+saved_cflags=$CFLAGS
+CFLAGS=-Werror
+
+AC_CACHE_CHECK(whether wcstombs_l is available if stdlib.h is included, pgac_cv_wcstombs_l_stdlib_compiles,
+[AC_LINK_IFELSE([AC_LANG_PROGRAM(
+[#include <stdlib.h>],
+[size_t tmp = wcstombs_l(NULL, NULL, 0, 0);])],
+[pgac_cv_wcstombs_l_stdlib_compiles=yes],
+[pgac_cv_wcstombs_l_stdlib_compiles=no])]
+)
+
+AC_CACHE_CHECK(whether wcstombs_l is available if stdlib.h and xlocale.h are included, pgac_cv_wcstombs_l_xlocale_compiles,
+[AC_LINK_IFELSE([AC_LANG_PROGRAM(
+[#include <stdlib.h>
+#include <xlocale.h>],
+[size_t tmp = wcstombs_l(NULL, NULL, 0, 0);])],
+[pgac_cv_wcstombs_l_xlocale_compiles=yes],
+[pgac_cv_wcstombs_l_xlocale_compiles=no])]
+)
+
+CFLAGS=$saved_cflags
+
+if test x"$pgac_cv_wcstombs_l_xlocale_compiles" = xyes ; then
+if test x"$pgac_cv_wcstombs_l_stdlib_compiles" = xno ; then
+AC_DEFINE(WCSTOMBS_L_IN_XLOCALE, 1,
+          [Define to 1 if wcstombs_l, mbstowcs_l, etc are declared in xlocale.h.])
+fi
+fi])# PGAC_C_WCSTOMBS_L_IN_XLOCALE
+
diff --git a/configure b/configure
index 0e51ac7..033224a 100755
--- a/configure
+++ b/configure
@@ -11132,6 +11132,76 @@ $as_echo "#define FLEXIBLE_ARRAY_MEMBER /**/" >>confdefs.h
 
   fi
 
+
+saved_cflags=$CFLAGS
+CFLAGS=-Werror
+
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether wcstombs_l is available if stdlib.h is included" >&5
+$as_echo_n "checking whether wcstombs_l is available if stdlib.h is included... " >&6; }
+if ${pgac_cv_wcstombs_l_stdlib_compiles+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <stdlib.h>
+int
+main ()
+{
+size_t tmp = wcstombs_l(NULL, NULL, 0, 0);
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+  pgac_cv_wcstombs_l_stdlib_compiles=yes
+else
+  pgac_cv_wcstombs_l_stdlib_compiles=no
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
+
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv_wcstombs_l_stdlib_compiles" >&5
+$as_echo "$pgac_cv_wcstombs_l_stdlib_compiles" >&6; }
+
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether wcstombs_l is available if stdlib.h and xlocale.h are included" >&5
+$as_echo_n "checking whether wcstombs_l is available if stdlib.h and xlocale.h are included... " >&6; }
+if ${pgac_cv_wcstombs_l_xlocale_compiles+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <stdlib.h>
+#include <xlocale.h>
+int
+main ()
+{
+size_t tmp = wcstombs_l(NULL, NULL, 0, 0);
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+  pgac_cv_wcstombs_l_xlocale_compiles=yes
+else
+  pgac_cv_wcstombs_l_xlocale_compiles=no
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
+
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv_wcstombs_l_xlocale_compiles" >&5
+$as_echo "$pgac_cv_wcstombs_l_xlocale_compiles" >&6; }
+
+CFLAGS=$saved_cflags
+
+if test x"$pgac_cv_wcstombs_l_xlocale_compiles" = xyes ; then
+if test x"$pgac_cv_wcstombs_l_stdlib_compiles" = xno ; then
+
+$as_echo "#define WCSTOMBS_L_IN_XLOCALE 1" >>confdefs.h
+
+fi
+fi
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for signed types" >&5
 $as_echo_n "checking for signed types... " >&6; }
 if ${pgac_cv_c_signed+:} false; then :
@@ -11986,7 +12056,7 @@ else
     We can't simply define LARGE_OFF_T to be 9223372036854775807,
     since some C++ compilers masquerading as C compilers
     incorrectly reject 9223372036854775807.  */
-#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
+#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31))
   int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
 		       && LARGE_OFF_T % 2147483647 == 1)
 		      ? 1 : -1];
@@ -12032,7 +12102,7 @@ else
     We can't simply define LARGE_OFF_T to be 9223372036854775807,
     since some C++ compilers masquerading as C compilers
     incorrectly reject 9223372036854775807.  */
-#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
+#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31))
   int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
 		       && LARGE_OFF_T % 2147483647 == 1)
 		      ? 1 : -1];
@@ -12056,7 +12126,7 @@ rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
     We can't simply define LARGE_OFF_T to be 9223372036854775807,
     since some C++ compilers masquerading as C compilers
     incorrectly reject 9223372036854775807.  */
-#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
+#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31))
   int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
 		       && LARGE_OFF_T % 2147483647 == 1)
 		      ? 1 : -1];
@@ -12101,7 +12171,7 @@ else
     We can't simply define LARGE_OFF_T to be 9223372036854775807,
     since some C++ compilers masquerading as C compilers
     incorrectly reject 9223372036854775807.  */
-#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
+#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31))
   int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
 		       && LARGE_OFF_T % 2147483647 == 1)
 		      ? 1 : -1];
@@ -12125,7 +12195,7 @@ rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
     We can't simply define LARGE_OFF_T to be 9223372036854775807,
     since some C++ compilers masquerading as C compilers
     incorrectly reject 9223372036854775807.  */
-#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
+#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31))
   int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
 		       && LARGE_OFF_T % 2147483647 == 1)
 		      ? 1 : -1];
diff --git a/configure.in b/configure.in
index 0bd90d7..6910615 100644
--- a/configure.in
+++ b/configure.in
@@ -1325,6 +1325,7 @@ AC_C_BIGENDIAN
 AC_C_INLINE
 PGAC_PRINTF_ARCHETYPE
 AC_C_FLEXIBLE_ARRAY_MEMBER
+PGAC_C_WCSTOMBS_L_IN_XLOCALE
 PGAC_C_SIGNED
 PGAC_C_FUNCNAME_SUPPORT
 PGAC_C_STATIC_ASSERT
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index b3ceea5..73690e1 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -851,6 +851,9 @@
 /* Define to select Win32-style shared memory. */
 #undef USE_WIN32_SHARED_MEMORY
 
+/* Define to 1 if wcstombs_l, mbstowcs_l, etc are declared in xlocale.h. */
+#undef WCSTOMBS_L_IN_XLOCALE
+
 /* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most
    significant byte first (like Motorola and SPARC, unlike Intel). */
 #if defined AC_APPLE_UNIVERSAL_BUILD
diff --git a/src/include/utils/pg_locale.h b/src/include/utils/pg_locale.h
index 2e6dba1..d4879f5 100644
--- a/src/include/utils/pg_locale.h
+++ b/src/include/utils/pg_locale.h
@@ -15,6 +15,10 @@
 #include <locale.h>
 #ifdef LOCALE_T_IN_XLOCALE
 #include <xlocale.h>
+#else
+#ifdef WCSTOMBS_L_IN_XLOCALE
+#include <xlocale.h>
+#endif
 #endif
 
 #include "utils/guc.h"
