Skip to content

Commit 9046a05

Browse files
committed
Disallow creating an ICU collation if the DB encoding won't support it.
Previously this was allowed, but the collation effectively vanished into the ether because of the way lookup_collation() works: you could not use the collation, nor even drop it. Seems better to give an error up front than to leave the user wondering why it doesn't work. (Because this test is in DefineCollation not CreateCollation, it does not prevent pg_import_system_collations from creating ICU collations, regardless of the initially-chosen encoding.) Per bug #17170 from Andrew Bille. Back-patch to v10 where ICU support was added. Discussion: https://postgr.es/m/17170-95845cf3f0a9c36d@postgresql.org
1 parent 1fab33c commit 9046a05

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/backend/commands/collationcmds.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,26 @@ DefineCollation(ParseState *pstate, List *names, List *parameters, bool if_not_e
206206
if (!fromEl)
207207
{
208208
if (collprovider == COLLPROVIDER_ICU)
209+
{
210+
#ifdef USE_ICU
211+
/*
212+
* We could create ICU collations with collencoding == database
213+
* encoding, but it seems better to use -1 so that it matches the
214+
* way initdb would create ICU collations. However, only allow
215+
* one to be created when the current database's encoding is
216+
* supported. Otherwise the collation is useless, plus we get
217+
* surprising behaviors like not being able to drop the collation.
218+
*
219+
* Skip this test when !USE_ICU, because the error we want to
220+
* throw for that isn't thrown till later.
221+
*/
222+
if (!is_encoding_supported_by_icu(GetDatabaseEncoding()))
223+
ereport(ERROR,
224+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
225+
errmsg("current database's encoding is not supported with this provider")));
226+
#endif
209227
collencoding = -1;
228+
}
210229
else
211230
{
212231
collencoding = GetDatabaseEncoding();

0 commit comments

Comments
 (0)