Skip to content

Commit ce341f6

Browse files
jeff-davishackorum
authored andcommitted
1 parent 388f2db commit ce341f6

4 files changed

Lines changed: 55 additions & 26 deletions

File tree

‎src/bin/initdb/initdb.c‎

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
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 */
8788
extern const char *select_default_timezone(const char *share_path);
@@ -144,7 +145,7 @@ static char *lc_monetary = NULL;
144145
static char *lc_numeric = NULL;
145146
static char *lc_time = NULL;
146147
static char *lc_messages = NULL;
147-
static char locale_provider = COLLPROVIDER_LIBC;
148+
static char locale_provider = COLLPROVIDER_BUILTIN;
148149
static bool builtin_locale_specified = false;
149150
static char *datlocale = NULL;
150151
static 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
}

‎src/bin/initdb/t/001_initdb.pl‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,15 @@
200200
'locale provider ICU fails since no ICU support');
201201
}
202202

203-
command_fails(
203+
command_like(
204204
[
205205
'initdb', '--no-sync',
206+
'--auth' => 'trust',
206207
'--locale-provider' => 'builtin',
207208
"$tempdir/data6"
208209
],
209-
'locale provider builtin fails without --locale');
210+
qr/^\s+default collation:\s+C.UTF-8\n/ms,
211+
'locale provider builtin defaults to C.UTF-8');
210212

211213
command_ok(
212214
[

‎src/bin/scripts/t/020_createdb.pl‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,6 @@
130130
'create database with ICU fails since no ICU support');
131131
}
132132

133-
$node->command_fails(
134-
[
135-
'createdb',
136-
'--template' => 'template0',
137-
'--locale-provider' => 'builtin',
138-
'tbuiltin1',
139-
],
140-
'create database with provider "builtin" fails without --locale');
141-
142133
$node->command_ok(
143134
[
144135
'createdb',
@@ -223,11 +214,21 @@
223214
[
224215
'createdb',
225216
'--template' => 'template1',
226-
'--locale-provider' => 'builtin',
217+
'--locale-provider' => 'icu',
218+
'--locale' => 'C',
219+
'tbuiltin9',
220+
],
221+
'create database with provider "icu" not matching template');
222+
223+
$node->command_fails(
224+
[
225+
'createdb',
226+
'--template' => 'template1',
227+
'--locale-provider' => 'libc',
227228
'--locale' => 'C',
228229
'tbuiltin9',
229230
],
230-
'create database with provider "builtin" not matching template');
231+
'create database with provider "libc" not matching template');
231232

232233
$node->command_fails([ 'createdb', 'foobar1' ],
233234
'fails if database already exists');

‎src/test/modules/test_escape/t/001_test_escape.pl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
$node->start();
1313

1414
$node->safe_psql('postgres',
15-
q(CREATE DATABASE db_sql_ascii ENCODING "sql_ascii" TEMPLATE template0;));
15+
q(CREATE DATABASE db_sql_ascii LOCALE "C" ENCODING "sql_ascii" TEMPLATE template0;));
1616

1717
my $cmd =
1818
[ 'test_escape', '--conninfo', $node->connstr . " dbname=db_sql_ascii" ];

0 commit comments

Comments
 (0)