Thread: Collation versions on Windows (help wanted, apply within)

Collation versions on Windows (help wanted, apply within)

From
Thomas Munro
Date:
Hello hackers,

Here's a draft patch that teaches PostgreSQL how to ask for collation
versions on Windows.  It receives a pair of DWORDs, which, it displays
as hex.  The values probably have an internal structure that is
displayed in a user-friendly way by software like Active Directory and
SQL Server (I'm pretty sure they both track collation versions and
reindex), but I don't know.  It is based on the documentation at:

https://docs.microsoft.com/en-us/windows/win32/win7appqual/nls-sorting-changes

My understanding of our OS and tool chain version strategy on that
platform is limited, but it looks like we only allow ourselves to use
Vista (and later) APIs if the compiler is Visual Studio 2015 (aka
14.0) or later.  So I tested that this builds cleanly on AppVeyor
using that compiler (see attached CI patch).  The regression tests
failed with Windows error code 87 before I added in the check to skip
"C" and "POSIX", so I know the new code is reached.  I don't have an
environment to test it beyond that.

The reason for returning an empty string for "C" and "POSIX" is the
following comment for get_collation_actual_version():

 * A particular provider must always either return a non-NULL string or return
 * NULL (if it doesn't support versions).  It must not return NULL for some
 * collcollate and not NULL for others.

I'm not sure why, or if that really makes sense.

Do any Windows hackers want to help get it into shape?  Some things to
do: test it, verify that the _WIN32_WINNT >= 0x0600 stuff makes sense
(why do we target such ancient Windows releases anyway?), see if there
is way we could use GetNLSVersion() (no "Ex") to make this work on
older Windows system, check if it makes sense to assume that
collcollate is encoded with CP_ACP ("the system default Windows ANSI
code page", used elsewhere in the PG source tree for a similar
purpose, but this seems likely to go wrong for locale names that have
non-ASCII characters, and indeed we see complaints on the lists
involving the word "Bokmål"), and recommend a better way to display
the collation version as text.  I'll add this to the next commitfest
to attract some eyeballs (but note that when cfbot compiles it, it
will be using an older tool chain and Win32 target, so the new code
will be ifdef'd out and regression test success means nothing).

To test that it works, you'd need to look at the contents of
pg_collation to confirm that you see the new version strings, create
an index on a column that explicitly uses a collation that has a
version, update the pg_collation table by hand to have a to a
different value, and then open a new session and to access the index
to check that you get a warning about the version changing.  The
warning can be cleared by using ALTER COLLATION ... REFRESH VERSION.

Attachment

Re: Collation versions on Windows (help wanted, apply within)

From
Juan José Santamaría Flecha
Date:

On Fri, Nov 8, 2019 at 12:44 AM Thomas Munro <thomas.munro@gmail.com> wrote:

Do any Windows hackers want to help get it into shape?  Some things to
do: test it, verify that the _WIN32_WINNT >= 0x0600 stuff makes sense
(why do we target such ancient Windows releases anyway?)

You have to keep in mind that  _WIN32_WINNT also applies to MinGW, so any build with those tools will use a value of 0x0501 and this code will be ifdef'd out.

As from where this value comes, my take is that it has not been revised in a long time [1]. Windows 7 , Server 2008 and 2008 R2 support will end next year [2] [3], maybe you can make a case for updating this value.
 
see if there is way we could use GetNLSVersion() (no "Ex") to make this work on
older Windows system

Older systems is just Windows Server 2003, not sure if it is worth any effort.
 
check if it makes sense to assume that
collcollate is encoded with CP_ACP ("the system default Windows ANSI
code page", used elsewhere in the PG source tree for a similar
purpose, but this seems likely to go wrong for locale names that have
non-ASCII characters, and indeed we see complaints on the lists
involving the word "Bokmål"), and recommend a better way to display
the collation version as text.

The GetNLSVersionEx() function uses a "Language tag" value, check Language Code Identifier (LCID) [4], and these tags are plain ascii.
 
To test that it works, you'd need to look at the contents of
pg_collation to confirm that you see the new version strings, create
an index on a column that explicitly uses a collation that has a
version, update the pg_collation table by hand to have a to a
different value, and then open a new session and to access the index
to check that you get a warning about the version changing.  The
warning can be cleared by using ALTER COLLATION ... REFRESH VERSION.

The code works as expected with this collation:

postgres=# CREATE COLLATION en_US (LC_COLLATE = 'en-US', LC_CTYPE = 'en-US');
CREATE COLLATION
postgres=# select * from pg_collation;
  oid  | collname  | collnamespace | collowner | collprovider | collisdeterministic | collencoding | collcollate | collctype | collversion
-------+-----------+---------------+-----------+--------------+---------------------+--------------+-------------+-----------+-------------
   100 | default   |            11 |        10 | d            | t                   |           -1 |             |           |
   950 | C         |            11 |        10 | c            | t                   |           -1 | C           | C         |
   951 | POSIX     |            11 |        10 | c            | t                   |           -1 | POSIX       | POSIX     |
 12326 | ucs_basic |            11 |        10 | c            | t                   |            6 | C           | C         |
 16387 | en_us     |          2200 |        10 | c            | t                   |           24 | en-US       | en-US     | 6020f,6020f
(5 rows)

The error code 87 is an ERROR_INVALID_PARAMETER that is raised when the collate input does not match a valid tag, I would suggest not returning it directly.

Regards,

Juan José Santamaría Flecha 


Regards,

Juan José Santamaría Flecha 

Re: Collation versions on Windows (help wanted, apply within)

From
Juan José Santamaría Flecha
Date:

On Fri, Nov 8, 2019 at 12:44 AM Thomas Munro <thomas.munro@gmail.com> wrote:

recommend a better way to display the collation version as text.


There is a major and a minor version. The attached patch applies on top the previous.

Regards,

Juan José Santamaría Flecha 
Attachment

Re: Collation versions on Windows (help wanted, apply within)

From
Thomas Munro
Date:
On Sat, Nov 9, 2019 at 10:20 AM Juan José Santamaría Flecha
<juanjo.santamaria@gmail.com> wrote:
>> Do any Windows hackers want to help get it into shape?  Some things to
>> do: test it, verify that the _WIN32_WINNT >= 0x0600 stuff makes sense
>> (why do we target such ancient Windows releases anyway?)
>
> You have to keep in mind that  _WIN32_WINNT also applies to MinGW, so any build with those tools will use a value of
0x0501and this code will be ifdef'd out. 
>
> As from where this value comes, my take is that it has not been revised in a long time [1]. Windows 7 , Server 2008
and2008 R2 support will end next year [2] [3], maybe you can make a case for updating this value. 

Ah, I see, thanks.  I think what I have is OK for now then.  If
someone else who is closer to the matter wants to argue that we should
always target Vista+ (for example on MinGW) in order to access this
functionality, I'll let them do that separately.

>> see if there is way we could use GetNLSVersion() (no "Ex") to make this work on
>> older Windows system
>
> Older systems is just Windows Server 2003, not sure if it is worth any effort.

Cool.  Nothing to do here then.

>  16387 | en_us     |          2200 |        10 | c            | t                   |           24 | en-US       |
en-US    | 6020f,6020f 

Thanks for testing!

On Sat, Nov 9, 2019 at 11:04 PM Juan José Santamaría Flecha
<juanjo.santamaria@gmail.com> wrote:
> There is a major and a minor version. The attached patch applies on top the previous.

Perfect.  I've merged this into the patch.

It's interesting that minor version changes mean no order changed but
new code points were added; that must be useful if your system
prevents you from using code points before you add them, I guess (?).
I don't understand the difference between the NLS and "defined"
versions, but at this stage I don't think we can try to be too fancy
here, think we're just going to have to assume we need both of them
and treat this the same way across all providers: if it moves, reindex
it.

Attachment

Re: Collation versions on Windows (help wanted, apply within)

From
Thomas Munro
Date:
On Fri, Nov 8, 2019 at 12:44 PM Thomas Munro <thomas.munro@gmail.com> wrote:
> The reason for returning an empty string for "C" and "POSIX" is the
> following comment for get_collation_actual_version():
>
>  * A particular provider must always either return a non-NULL string or return
>  * NULL (if it doesn't support versions).  It must not return NULL for some
>  * collcollate and not NULL for others.
>
> I'm not sure why, or if that really makes sense.

Peter E, do you have any thoughts on this question?



Re: Collation versions on Windows (help wanted, apply within)

From
Peter Eisentraut
Date:
On 2019-11-26 21:39, Thomas Munro wrote:
> On Fri, Nov 8, 2019 at 12:44 PM Thomas Munro <thomas.munro@gmail.com> wrote:
>> The reason for returning an empty string for "C" and "POSIX" is the
>> following comment for get_collation_actual_version():
>>
>>   * A particular provider must always either return a non-NULL string or return
>>   * NULL (if it doesn't support versions).  It must not return NULL for some
>>   * collcollate and not NULL for others.
>>
>> I'm not sure why, or if that really makes sense.
> 
> Peter E, do you have any thoughts on this question?

Doesn't make sense to me either.

We need to handle the various combinations of null and non-null stored 
and actual versions, which we do, but that only applies within a given 
collcollate.  I don't think we need the property that that comment calls 
for.

-- 
Peter Eisentraut              http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services



Re: Collation versions on Windows (help wanted, apply within)

From
Thomas Munro
Date:
On Wed, Nov 27, 2019 at 10:38 AM Peter Eisentraut
<peter.eisentraut@2ndquadrant.com> wrote:
> On 2019-11-26 21:39, Thomas Munro wrote:
> > On Fri, Nov 8, 2019 at 12:44 PM Thomas Munro <thomas.munro@gmail.com> wrote:
> >> The reason for returning an empty string for "C" and "POSIX" is the
> >> following comment for get_collation_actual_version():
> >>
> >>   * A particular provider must always either return a non-NULL string or return
> >>   * NULL (if it doesn't support versions).  It must not return NULL for some
> >>   * collcollate and not NULL for others.
> >>
> >> I'm not sure why, or if that really makes sense.
> >
> > Peter E, do you have any thoughts on this question?
>
> Doesn't make sense to me either.
>
> We need to handle the various combinations of null and non-null stored
> and actual versions, which we do, but that only applies within a given
> collcollate.  I don't think we need the property that that comment calls
> for.

While wondering about that, I noticed the "C.UTF-8" encoding (here on
a glibc system):

postgres=# \pset null <NULL>
Null display is "<NULL>".
postgres=# select collname, collprovider, collencoding, collcollate,
collctype, collversion from pg_collation ;
  collname  | collprovider | collencoding | collcollate | collctype  |
collversion
------------+--------------+--------------+-------------+------------+-------------
 default    | d            |           -1 |             |            | <NULL>
 C          | c            |           -1 | C           | C          | <NULL>
 POSIX      | c            |           -1 | POSIX       | POSIX      | <NULL>
 ucs_basic  | c            |            6 | C           | C          | <NULL>
 C.UTF-8    | c            |            6 | C.UTF-8     | C.UTF-8    | 2.28
 en_NZ.utf8 | c            |            6 | en_NZ.utf8  | en_NZ.utf8 | 2.28
 en_NZ      | c            |            6 | en_NZ.utf8  | en_NZ.utf8 | 2.28
(7 rows)

I wonder if we should do something to give that no collversion, since
we know that it's really just another way of spelling "binary sort
please", or if we'd be better off not trying to interpret the names
locale -a spits out.

Juan Jose,

Would you mind posting the full output of the above query (with <NULL>
showing) on a Windows system after running initdb with the -v2 patch,
so we can see how the collations look?



Re: Collation versions on Windows (help wanted, apply within)

From
Peter Eisentraut
Date:
On 2019-12-16 01:26, Thomas Munro wrote:
> While wondering about that, I noticed the "C.UTF-8" encoding (here on
> a glibc system):
> 
> postgres=# \pset null <NULL>
> Null display is "<NULL>".
> postgres=# select collname, collprovider, collencoding, collcollate,
> collctype, collversion from pg_collation ;
>    collname  | collprovider | collencoding | collcollate | collctype  |
> collversion
> ------------+--------------+--------------+-------------+------------+-------------
>   default    | d            |           -1 |             |            | <NULL>
>   C          | c            |           -1 | C           | C          | <NULL>
>   POSIX      | c            |           -1 | POSIX       | POSIX      | <NULL>
>   ucs_basic  | c            |            6 | C           | C          | <NULL>
>   C.UTF-8    | c            |            6 | C.UTF-8     | C.UTF-8    | 2.28
>   en_NZ.utf8 | c            |            6 | en_NZ.utf8  | en_NZ.utf8 | 2.28
>   en_NZ      | c            |            6 | en_NZ.utf8  | en_NZ.utf8 | 2.28
> (7 rows)
> 
> I wonder if we should do something to give that no collversion, since
> we know that it's really just another way of spelling "binary sort
> please", or if we'd be better off not trying to interpret the names
> locale -a spits out.

I think it's worth handling that separately.  If we want to give it an 
air of generality and not just hard-code that one locale name, we could 
strip off the encoding and compare the rest with the well-known encoding 
names.

-- 
Peter Eisentraut              http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services



Re: Collation versions on Windows (help wanted, apply within)

From
Juan José Santamaría Flecha
Date:


On Mon, Dec 16, 2019 at 1:26 AM Thomas Munro <thomas.munro@gmail.com> wrote:
On Wed, Nov 27, 2019 at 10:38 AM Peter Eisentraut

Would you mind posting the full output of the above query (with <NULL>
showing) on a Windows system after running initdb with the -v2 patch,
so we can see how the collations look?


Sure, you can find attached the full output with ICU.

This is a resume to illustrate an issue with locale = 'C':

postgres=#   CREATE COLLATION c_test (locale = 'C');
CREATE COLLATION
postgres=# select collname, collprovider, collencoding, collcollate, collctype, collversion from pg_collation ;
        collname        | collprovider | collencoding |   collcollate    |    collctype     | collversion
------------------------+--------------+--------------+------------------+------------------+-------------
 default                | d            |           -1 |                  |                  | <NULL>
 C                      | c            |           -1 | C                | C                | <NULL>
 POSIX                  | c            |           -1 | POSIX            | POSIX            | <NULL>
 ucs_basic              | c            |            6 | C                | C                | <NULL>
 und-x-icu              | i            |           -1 | und              | und              | 153.97
[... resumed ...]
 c_test                 | c            |            6 | C                | C                |
(757 rows)

 Shouldn't it be NULL?  

Regards,

Juan José Santamaría Flecha
Attachment

Re: Collation versions on Windows (help wanted, apply within)

From
Thomas Munro
Date:
On Tue, Dec 17, 2019 at 1:40 AM Juan José Santamaría Flecha
<juanjo.santamaria@gmail.com> wrote:
> On Mon, Dec 16, 2019 at 1:26 AM Thomas Munro <thomas.munro@gmail.com> wrote:
>> On Wed, Nov 27, 2019 at 10:38 AM Peter Eisentraut
>>
>> Would you mind posting the full output of the above query (with <NULL>
>> showing) on a Windows system after running initdb with the -v2 patch,
>> so we can see how the collations look?
>
> Sure, you can find attached the full output with ICU.

Oh, I didn't realise until now that on Windows, initdb doesn't do
anything like "locale -a" to populate the system locales (which should
perhaps be done with EnumSystemLocalesEx(), as shown in Noah's nearby
problem report).  So you have to define them manually, and otherwise
most people probably just use the default.

> This is a resume to illustrate an issue with locale = 'C':
>
> postgres=#   CREATE COLLATION c_test (locale = 'C');
> CREATE COLLATION
> postgres=# select collname, collprovider, collencoding, collcollate, collctype, collversion from pg_collation ;
>         collname        | collprovider | collencoding |   collcollate    |    collctype     | collversion
> ------------------------+--------------+--------------+------------------+------------------+-------------
>  default                | d            |           -1 |                  |                  | <NULL>
>  C                      | c            |           -1 | C                | C                | <NULL>
>  POSIX                  | c            |           -1 | POSIX            | POSIX            | <NULL>
>  ucs_basic              | c            |            6 | C                | C                | <NULL>
>  und-x-icu              | i            |           -1 | und              | und              | 153.97
> [... resumed ...]
>  c_test                 | c            |            6 | C                | C                |
> (757 rows)
>
>  Shouldn't it be NULL?

Yeah, it should.  I'll post a new patch next week that does that and
also removes the comment about how you can't have a mixture of NULL
and non-NULL, and see if I can identify anything that depends on that.
If you'd like to do that, please go ahead.



Re: Collation versions on Windows (help wanted, apply within)

From
Thomas Munro
Date:
On Wed, Dec 18, 2019 at 11:02 PM Thomas Munro <thomas.munro@gmail.com> wrote:
> On Tue, Dec 17, 2019 at 1:40 AM Juan José Santamaría Flecha
> <juanjo.santamaria@gmail.com> wrote:
> > This is a resume to illustrate an issue with locale = 'C':
> >
> > postgres=#   CREATE COLLATION c_test (locale = 'C');
> > CREATE COLLATION
> > postgres=# select collname, collprovider, collencoding, collcollate, collctype, collversion from pg_collation ;
> >         collname        | collprovider | collencoding |   collcollate    |    collctype     | collversion
> > ------------------------+--------------+--------------+------------------+------------------+-------------
> >  default                | d            |           -1 |                  |                  | <NULL>
> >  C                      | c            |           -1 | C                | C                | <NULL>
> >  POSIX                  | c            |           -1 | POSIX            | POSIX            | <NULL>
> >  ucs_basic              | c            |            6 | C                | C                | <NULL>
> >  und-x-icu              | i            |           -1 | und              | und              | 153.97
> > [... resumed ...]
> >  c_test                 | c            |            6 | C                | C                |
> > (757 rows)
> >
> >  Shouldn't it be NULL?

Done in this new 0002 patch (untested).  0001 removes the comment that
individual collations can't have a NULL version, reports NULL for
Linux/glibc collations like C.UTF-8 by stripping the suffix and
comparing with C and POSIX as suggested by Peter E.

Attachment

Re: Collation versions on Windows (help wanted, apply within)

From
Juan José Santamaría Flecha
Date:


On Mon, Mar 23, 2020 at 5:59 AM Thomas Munro <thomas.munro@gmail.com> wrote:

Done in this new 0002 patch (untested).  0001 removes the comment that
individual collations can't have a NULL version, reports NULL for
Linux/glibc collations like C.UTF-8 by stripping the suffix and
comparing with C and POSIX as suggested by Peter E.

 It applies and passes tests without a problem in Windows, and works as expected.

Regards

Re: Collation versions on Windows (help wanted, apply within)

From
Thomas Munro
Date:
On Tue, Mar 24, 2020 at 7:56 AM Juan José Santamaría Flecha
<juanjo.santamaria@gmail.com> wrote:
> On Mon, Mar 23, 2020 at 5:59 AM Thomas Munro <thomas.munro@gmail.com> wrote:
>> Done in this new 0002 patch (untested).  0001 removes the comment that
>> individual collations can't have a NULL version, reports NULL for
>> Linux/glibc collations like C.UTF-8 by stripping the suffix and
>> comparing with C and POSIX as suggested by Peter E.
>
>  It applies and passes tests without a problem in Windows, and works as expected.

Thanks!  Pushed.

From the things we learned in this thread, I think there is an open
item for someone to write a patch to call EnumSystemLocalesEx() and
populate the initial set of collations, where we use "locale -a" on
Unix.  I'm not sure where the encoding is supposed to come from
though, which is why I didn't try to write a patch myself.



Re: Collation versions on Windows (help wanted, apply within)

From
Juan José Santamaría Flecha
Date:

On Wed, Mar 25, 2020 at 4:18 AM Thomas Munro <thomas.munro@gmail.com> wrote:

Thanks!  Pushed.

Great!
 
From the things we learned in this thread, I think there is an open
item for someone to write a patch to call EnumSystemLocalesEx() and
populate the initial set of collations, where we use "locale -a" on
Unix.  I'm not sure where the encoding is supposed to come from
though, which is why I didn't try to write a patch myself.

I will take a look at this when the current commitfest is over.

Regards,

Juan José Santamaría Flecha