From 29745f26a097381b3bad32aaf773b33b4c5fc775 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@enterprisedb.com>
Date: Sun, 23 Sep 2018 21:55:21 +1200
Subject: [PATCH 6/6] Use querylocale() to get collation versions on FreeBSD.

Proposed feature for FreeBSD 13: https://reviews.freebsd.org/D17166
Details may change...

Author: Thomas Munro
Reviewed-by:
Discussion: https://postgr.es/m/CAEepm%3D0uEQCpfq_%2BLYFBdArCe4Ot98t1aR4eYiYTe%3DyavQygiQ%40mail.gmail.com
---
 configure                         |  2 +-
 configure.in                      |  2 +-
 src/backend/utils/adt/pg_locale.c | 10 ++++++++++
 src/include/pg_config.h.in        |  3 +++
 4 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/configure b/configure
index 7949f721b72..5071499f2a7 100755
--- a/configure
+++ b/configure
@@ -15093,7 +15093,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 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 fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate pstat pthread_is_threaded_np querylocale 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 d978438318c..3a473d3cb0e 100644
--- a/configure.in
+++ b/configure.in
@@ -1562,7 +1562,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 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 fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate pstat pthread_is_threaded_np querylocale 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 45faf088c1a..38cd94495c9 100644
--- a/src/backend/utils/adt/pg_locale.c
+++ b/src/backend/utils/adt/pg_locale.c
@@ -1446,6 +1446,16 @@ get_collation_actual_version(char collprovider, const char *collcollate)
 	{
 #ifdef HAVE_GNU_LIBC_VERSION_H
 		collversion = gnu_get_libc_version();
+#elif defined(HAVE_QUERYLOCALE) && defined(LC_VERSION_MASK)
+		/* FreeBSD 13 */
+		locale_t loc = newlocale(LC_COLLATE, collcollate, NULL);
+		if (loc)
+			collversion =
+				pstrdup(querylocale(LC_COLLATE_MASK | LC_VERSION_MASK, loc));
+		else
+			ereport(ERROR,
+					(errmsg("could not load locale \"%s\"", collcollate)));
+		freelocale(loc);
 #else
 		collversion = NULL;
 #endif
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 9f05c24f7bc..9410793bd19 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -461,6 +461,9 @@
 /* Have PTHREAD_PRIO_INHERIT. */
 #undef HAVE_PTHREAD_PRIO_INHERIT
 
+/* Define to 1 if you have the `querylocale' function. */
+#undef HAVE_QUERYLOCALE
+
 /* Define to 1 if you have the `random' function. */
 #undef HAVE_RANDOM
 
-- 
2.17.0

