8282#include "mb/pg_wchar.h"
8383#include "miscadmin.h"
8484
85+ #define DEFAULT_BUILTIN_LOCALE "C.UTF-8"
8586
8687/* Ideally this would be in a .h file, but it hardly seems worth the trouble */
8788extern const char * select_default_timezone (const char * share_path );
@@ -144,7 +145,7 @@ static char *lc_monetary = NULL;
144145static char * lc_numeric = NULL ;
145146static char * lc_time = NULL ;
146147static char * lc_messages = NULL ;
147- static char locale_provider = COLLPROVIDER_LIBC ;
148+ static char locale_provider = COLLPROVIDER_BUILTIN ;
148149static bool builtin_locale_specified = false;
149150static char * datlocale = NULL ;
150151static bool icu_locale_specified = false;
@@ -2476,12 +2477,11 @@ setlocales(void)
24762477 lc_messages = canonname ;
24772478#endif
24782479
2479- if (locale_provider != COLLPROVIDER_LIBC && datlocale == NULL )
2480- pg_fatal ("locale must be specified if provider is %s" ,
2481- collprovider_name (locale_provider ));
2482-
24832480 if (locale_provider == COLLPROVIDER_BUILTIN )
24842481 {
2482+ if (!datlocale )
2483+ datlocale = DEFAULT_BUILTIN_LOCALE ;
2484+
24852485 if (strcmp (datlocale , "C" ) == 0 )
24862486 canonname = "C" ;
24872487 else if (strcmp (datlocale , "C.UTF-8" ) == 0 ||
@@ -2499,6 +2499,9 @@ setlocales(void)
24992499 {
25002500 char * langtag ;
25012501
2502+ if (!datlocale )
2503+ pg_fatal ("locale must be specified if provider is icu" );
2504+
25022505 /* canonicalize to a language tag */
25032506 langtag = icu_language_tag (datlocale );
25042507 printf (_ ("Using language tag \"%s\" for ICU locale \"%s\".\n" ),
@@ -2694,6 +2697,29 @@ setup_locale_encoding(void)
26942697{
26952698 setlocales ();
26962699
2700+ /*
2701+ * For the builtin provider (other than the "C" locale), default encoding
2702+ * to UTF-8. If lc_ctype is not compatible with UTF-8, also force lc_ctype
2703+ * to "C". On windows, all locales are compatible with UTF-8.
2704+ */
2705+ if (!encoding && locale_provider == COLLPROVIDER_BUILTIN &&
2706+ strcmp (datlocale , "C" ) != 0 )
2707+ {
2708+ #ifndef WIN32
2709+ int ctype_enc = pg_get_encoding_from_locale (lc_ctype , false);
2710+ if (!(ctype_enc == PG_UTF8 ||
2711+ ctype_enc == PG_SQL_ASCII ))
2712+ {
2713+ pg_log_warning ("setting LC_CTYPE to \"C\"" );
2714+ pg_log_warning_detail ("Encoding of LC_CTYPE locale \"%s\" does not match encoding required by builtin locale \"%s\"." ,
2715+ lc_ctype , datlocale );
2716+ pg_log_warning_hint ("Specify a UTF-8 compatible locale with --lc-ctype, or choose a different locale provider." );
2717+ lc_ctype = "C" ;
2718+ }
2719+ #endif
2720+ encoding = "UTF-8" ;
2721+ }
2722+
26972723 if (locale_provider == COLLPROVIDER_LIBC &&
26982724 strcmp (lc_ctype , lc_collate ) == 0 &&
26992725 strcmp (lc_ctype , lc_time ) == 0 &&
@@ -2729,10 +2755,11 @@ setup_locale_encoding(void)
27292755 ctype_enc = pg_get_encoding_from_locale (lc_ctype , true);
27302756
27312757 /*
2732- * If ctype_enc=SQL_ASCII, it's compatible with any encoding. ICU does
2733- * not support SQL_ASCII, so select UTF-8 instead.
2758+ * If ctype_enc=SQL_ASCII, it's compatible with any encoding. Neither
2759+ * ICU nor the builtin provider support SQL_ASCII, so select UTF-8
2760+ * instead.
27342761 */
2735- if (locale_provider == COLLPROVIDER_ICU && ctype_enc == PG_SQL_ASCII )
2762+ if (locale_provider != COLLPROVIDER_LIBC && ctype_enc == PG_SQL_ASCII )
27362763 ctype_enc = PG_UTF8 ;
27372764
27382765 if (ctype_enc == -1 )
@@ -2781,11 +2808,10 @@ setup_locale_encoding(void)
27812808 !check_locale_encoding (lc_collate , encodingid ))
27822809 exit (1 ); /* check_locale_encoding printed the error */
27832810
2784- if (locale_provider == COLLPROVIDER_BUILTIN )
2811+ if (locale_provider == COLLPROVIDER_BUILTIN &&
2812+ strcmp (datlocale , "C" ) != 0 )
27852813 {
2786- if ((strcmp (datlocale , "C.UTF-8" ) == 0 ||
2787- strcmp (datlocale , "PG_UNICODE_FAST" ) == 0 ) &&
2788- encodingid != PG_UTF8 )
2814+ if (encodingid != PG_UTF8 )
27892815 pg_fatal ("builtin provider locale \"%s\" requires encoding \"%s\"" ,
27902816 datlocale , "UTF-8" );
27912817 }
0 commit comments