From 94705158a009292bc0ae2f87f65c58bfc0a4901e Mon Sep 17 00:00:00 2001
From: Thomas Munro <munro@ip9.org>
Date: Sat, 11 Aug 2018 08:55:10 +1200
Subject: [PATCH] Use querylocaleversion(3) if available.

For collations provided by libc, get a version string using
querylocaleversion(3) on systems that have it.

*** THIS IS A FICTIONAL LIBC FEATURE ***

Author: Thomas Munro
---
 configure                         |  2 +-
 configure.in                      |  2 +-
 src/backend/utils/adt/pg_locale.c | 15 +++++++++++++++
 src/include/pg_config.h.in        |  3 +++
 4 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/configure b/configure
index 26652133d5..49fa863a47 100755
--- a/configure
+++ b/configure
@@ -14916,7 +14916,7 @@ fi
 LIBS_including_readline="$LIBS"
 LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
 
-for ac_func in cbrt clock_gettime dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l
+for ac_func in cbrt clock_gettime dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate pstat pthread_is_threaded_np querylocaleversion readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
diff --git a/configure.in b/configure.in
index 397f6bc765..224385f573 100644
--- a/configure.in
+++ b/configure.in
@@ -1540,7 +1540,7 @@ PGAC_FUNC_WCSTOMBS_L
 LIBS_including_readline="$LIBS"
 LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
 
-AC_CHECK_FUNCS([cbrt clock_gettime dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l])
+AC_CHECK_FUNCS([cbrt clock_gettime dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate pstat pthread_is_threaded_np querylocaleversion readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l])
 
 AC_REPLACE_FUNCS(fseeko)
 case $host_os in
diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c
index a3dc3be5a8..d89935680f 100644
--- a/src/backend/utils/adt/pg_locale.c
+++ b/src/backend/utils/adt/pg_locale.c
@@ -1475,7 +1475,22 @@ get_collation_actual_version(char collprovider, const char *collcollate)
 	}
 	else
 #endif
+#if HAVE_QUERYLOCALEVERSION
+		{
+			locale_t	loc;
+
+			loc = newlocale(LC_COLLATE, collcollate, NULL);
+			if (loc)
+			{
+				collversion = pstrdup(querylocaleversion(LC_COLLATE, loc));
+				freelocale(loc);
+			}
+			else
+				collversion = NULL;
+		}
+#else
 		collversion = NULL;
+#endif
 
 	return collversion;
 }
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index b7e469670f..d4b077424b 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -449,6 +449,9 @@
 /* Have PTHREAD_PRIO_INHERIT. */
 #undef HAVE_PTHREAD_PRIO_INHERIT
 
+/* Define to 1 if you have the `querylocaleversion' function. */
+#undef HAVE_QUERYLOCALEVERSION
+
 /* Define to 1 if you have the `random' function. */
 #undef HAVE_RANDOM
 
-- 
2.18.0

