Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0b13b2a

Browse files
committed
Rethink behavior of pg_import_system_collations().
Marco Atzeri reported that initdb would fail if "locale -a" reported the same locale name more than once. All previous versions of Postgres implicitly de-duplicated the results of "locale -a", but the rewrite to move the collation import logic into C had lost that property. It had also lost the property that locale names matching built-in collation names were silently ignored. The simplest way to fix this is to make initdb run the function in if-not-exists mode, which means that there's no real use-case for non if-not-exists mode; we might as well just drop the boolean argument and simplify the function's definition to be "add any collations not already known". This change also gets rid of some odd corner cases caused by the fact that aliases were added in if-not-exists mode even if the function argument said otherwise. While at it, adjust the behavior so that pg_import_system_collations() doesn't spew "collation foo already exists, skipping" messages during a re-run; that's completely unhelpful, especially since there are often hundreds of them. And make it return a count of the number of collations it did add, which seems like it might be helpful. Also, re-integrate the previous coding's property that it would make a deterministic selection of which alias to use if there were conflicting possibilities. This would only come into play if "locale -a" reports multiple equivalent locale names, say "de_DE.utf8" and "de_DE.UTF-8", but that hardly seems out of the question. In passing, fix incorrect behavior in pg_import_system_collations()'s ICU code path: it neglected CommandCounterIncrement, which would result in failures if ICU returns duplicate names, and it would try to create comments even if a new collation hadn't been created. Also, reorder operations in initdb so that the 'ucs_basic' collation is created before calling pg_import_system_collations() not after. This prevents a failure if "locale -a" were to report a locale named that. There's no reason to think that that ever happens in the wild, but the old coding would have survived it, so let's be equally robust. Discussion: https://postgr.es/m/[email protected]
1 parent 9ea3c64 commit 0b13b2a

File tree

7 files changed

+254
-138
lines changed

7 files changed

+254
-138
lines changed

doc/src/sgml/func.sgml

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19711,9 +19711,9 @@ postgres=# SELECT * FROM pg_walfile_name_offset(pg_stop_backup());
1971119711
<row>
1971219712
<entry>
1971319713
<indexterm><primary>pg_import_system_collations</primary></indexterm>
19714-
<literal><function>pg_import_system_collations(<parameter>if_not_exists</> <type>boolean</>, <parameter>schema</> <type>regnamespace</>)</function></literal>
19714+
<literal><function>pg_import_system_collations(<parameter>schema</> <type>regnamespace</>)</function></literal>
1971519715
</entry>
19716-
<entry><type>void</type></entry>
19716+
<entry><type>integer</type></entry>
1971719717
<entry>Import operating system collations</entry>
1971819718
</row>
1971919719
</tbody>
@@ -19730,18 +19730,20 @@ postgres=# SELECT * FROM pg_walfile_name_offset(pg_stop_backup());
1973019730
</para>
1973119731

1973219732
<para>
19733-
<function>pg_import_system_collations</> populates the system
19734-
catalog <literal>pg_collation</literal> with collations based on all the
19735-
locales it finds on the operating system. This is
19733+
<function>pg_import_system_collations</> adds collations to the system
19734+
catalog <literal>pg_collation</literal> based on all the
19735+
locales it finds in the operating system. This is
1973619736
what <command>initdb</command> uses;
1973719737
see <xref linkend="collation-managing"> for more details. If additional
1973819738
locales are installed into the operating system later on, this function
19739-
can be run again to add collations for the new locales. In that case, the
19740-
parameter <parameter>if_not_exists</parameter> should be set to true to
19741-
skip over existing collations. The <parameter>schema</parameter>
19742-
parameter would typically be <literal>pg_catalog</literal>, but that is
19743-
not a requirement. (Collation objects based on locales that are no longer
19744-
present on the operating system are never removed by this function.)
19739+
can be run again to add collations for the new locales. Locales that
19740+
match existing entries in <literal>pg_collation</literal> will be skipped.
19741+
(But collation objects based on locales that are no longer
19742+
present in the operating system are not removed by this function.)
19743+
The <parameter>schema</parameter> parameter would typically
19744+
be <literal>pg_catalog</literal>, but that is not a requirement;
19745+
the collations could be installed into some other schema as well.
19746+
The function returns the number of new collation objects it created.
1974519747
</para>
1974619748

1974719749
</sect2>

src/backend/catalog/pg_collation.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
* CollationCreate
3838
*
3939
* Add a new tuple to pg_collation.
40+
*
41+
* if_not_exists: if true, don't fail on duplicate name, just print a notice
42+
* and return InvalidOid.
43+
* quiet: if true, don't fail on duplicate name, just silently return
44+
* InvalidOid (overrides if_not_exists).
4045
*/
4146
Oid
4247
CollationCreate(const char *collname, Oid collnamespace,
@@ -45,7 +50,8 @@ CollationCreate(const char *collname, Oid collnamespace,
4550
int32 collencoding,
4651
const char *collcollate, const char *collctype,
4752
const char *collversion,
48-
bool if_not_exists)
53+
bool if_not_exists,
54+
bool quiet)
4955
{
5056
Relation rel;
5157
TupleDesc tupDesc;
@@ -77,7 +83,9 @@ CollationCreate(const char *collname, Oid collnamespace,
7783
Int32GetDatum(collencoding),
7884
ObjectIdGetDatum(collnamespace)))
7985
{
80-
if (if_not_exists)
86+
if (quiet)
87+
return InvalidOid;
88+
else if (if_not_exists)
8189
{
8290
ereport(NOTICE,
8391
(errcode(ERRCODE_DUPLICATE_OBJECT),
@@ -119,7 +127,12 @@ CollationCreate(const char *collname, Oid collnamespace,
119127
Int32GetDatum(-1),
120128
ObjectIdGetDatum(collnamespace))))
121129
{
122-
if (if_not_exists)
130+
if (quiet)
131+
{
132+
heap_close(rel, NoLock);
133+
return InvalidOid;
134+
}
135+
else if (if_not_exists)
123136
{
124137
heap_close(rel, NoLock);
125138
ereport(NOTICE,

0 commit comments

Comments
 (0)