Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit148f66d

Browse files
committed
Don't allow creation of database with ICU locale with unsupported encoding
Check in CREATE DATABASE and initdb that the selected encoding issupported by ICU. Before, they would pass but users would later getan error from the server when they tried to use the database.Also document that initdb sets the encoding to UTF8 by default if theICU locale provider is chosen.Author: Marina Polyakova <m.polyakova@postgrespro.ru>Reviewed-by: Kyotaro Horiguchi <horikyota.ntt@gmail.com>Discussion:https://www.postgresql.org/message-id/6dd6db0984d86a51b7255ba79f111971@postgrespro.ru
1 parent4e2a889 commit148f66d

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

‎doc/src/sgml/ref/initdb.sgml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,9 @@ PostgreSQL documentation
209209
<para>
210210
Selects the encoding of the template databases. This will also
211211
be the default encoding of any database you create later,
212-
unless you override it then. The default is derived from the locale, or
213-
<literal>SQL_ASCII</literal> if that does not work. The character sets supported by
212+
unless you override it then. The default is derived from the locale,
213+
if the libc locale provider is used, or <literal>UTF8</literal> if the
214+
ICU locale provider is used. The character sets supported by
214215
the <productname>PostgreSQL</productname> server are described
215216
in <xref linkend="multibyte-charset-supported"/>.
216217
</para>

‎src/backend/commands/dbcommands.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,12 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
10331033

10341034
if (dblocprovider==COLLPROVIDER_ICU)
10351035
{
1036+
if (!(is_encoding_supported_by_icu(encoding)))
1037+
ereport(ERROR,
1038+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1039+
errmsg("encoding \"%s\" is not supported with ICU provider",
1040+
pg_encoding_to_char(encoding))));
1041+
10361042
/*
10371043
* This would happen if template0 uses the libc provider but the new
10381044
* database uses icu.
@@ -1041,10 +1047,9 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
10411047
ereport(ERROR,
10421048
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
10431049
errmsg("ICU locale must be specified")));
1044-
}
10451050

1046-
if (dblocprovider==COLLPROVIDER_ICU)
10471051
check_icu_locale(dbiculocale);
1052+
}
10481053

10491054
/*
10501055
* Check that the new encoding and locale settings match the source

‎src/bin/initdb/initdb.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,6 +2091,27 @@ check_locale_encoding(const char *locale, int user_enc)
20912091
return true;
20922092
}
20932093

2094+
/*
2095+
* check if the chosen encoding matches is supported by ICU
2096+
*
2097+
* this should match the similar check in the backend createdb() function
2098+
*/
2099+
staticbool
2100+
check_icu_locale_encoding(intuser_enc)
2101+
{
2102+
if (!(is_encoding_supported_by_icu(user_enc)))
2103+
{
2104+
pg_log_error("encoding mismatch");
2105+
pg_log_error_detail("The encoding you selected (%s) is not supported with the ICU provider.",
2106+
pg_encoding_to_char(user_enc));
2107+
pg_log_error_hint("Rerun %s and either do not specify an encoding explicitly, "
2108+
"or choose a matching combination.",
2109+
progname);
2110+
return false;
2111+
}
2112+
return true;
2113+
}
2114+
20942115
/*
20952116
* set up the locale variables
20962117
*
@@ -2359,7 +2380,11 @@ setup_locale_encoding(void)
23592380
}
23602381

23612382
if (!encoding&&locale_provider==COLLPROVIDER_ICU)
2383+
{
23622384
encodingid=PG_UTF8;
2385+
printf(_("The default database encoding has been set to \"%s\".\n"),
2386+
pg_encoding_to_char(encodingid));
2387+
}
23632388
elseif (!encoding)
23642389
{
23652390
intctype_enc;
@@ -2411,6 +2436,10 @@ setup_locale_encoding(void)
24112436
if (!check_locale_encoding(lc_ctype,encodingid)||
24122437
!check_locale_encoding(lc_collate,encodingid))
24132438
exit(1);/* check_locale_encoding printed the error */
2439+
2440+
if (locale_provider==COLLPROVIDER_ICU&&
2441+
!check_icu_locale_encoding(encodingid))
2442+
exit(1);
24142443
}
24152444

24162445

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@
118118
],
119119
qr/FATAL: could not open collator for locale/,
120120
'fails for invalid ICU locale');
121+
122+
command_fails_like(
123+
[
124+
'initdb','--no-sync',
125+
'--locale-provider=icu','--encoding=SQL_ASCII',
126+
'--icu-locale=en',"$tempdir/dataX"
127+
],
128+
qr/error: encoding mismatch/,
129+
'fails for encoding not supported by ICU');
121130
}
122131
else
123132
{

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@
5050
],
5151
'fails for invalid ICU locale');
5252

53+
$node->command_fails_like(
54+
[
55+
'createdb','-T',
56+
'template0','--locale-provider=icu',
57+
'--encoding=SQL_ASCII','foobarX'
58+
],
59+
qr/ERROR: encoding "SQL_ASCII" is not supported with ICU provider/,
60+
'fails for encoding not supported by ICU');
61+
5362
# additional node, which uses the icu provider
5463
my$node2 = PostgreSQL::Test::Cluster->new('icu');
5564
$node2->init(extra=> ['--locale-provider=icu','--icu-locale=en']);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp