Re: [GENERAL] Not able to create collation on Windows

Tom Lane <tgl@sss.pgh.pa.us>

From: Tom Lane <tgl@sss.pgh.pa.us>
To: Murtuza Zabuawala <murtuza.zabuawala@enterprisedb.com>
Cc: PostgreSQL mailing lists <pgsql-general@postgresql.org>, pgsql-hackers@postgresql.org, Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
Date: 2017-08-01T14:53:15Z
Lists: pgsql-hackers

Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Revert test case added by commit 1e165d05fe06a9072867607886f818bc255507db.

  2. Second try at getting useful errors out of newlocale/_create_locale.

  3. Try to deliver a sane message for _create_locale() failure on Windows.

  4. Allow creation of C/POSIX collations without depending on libc behavior.

Murtuza Zabuawala <murtuza.zabuawala@enterprisedb.com> writes:
> I am trying to create collation on windows using default POSIX collation
> with pgAdmin3 but I am getting error as shown in screenshot, Can someone
> suggest how to fix this?

> *Syntax:*
> CREATE COLLATION public.test from pg_catalog."POSIX";

> *Error:*
> ERROR: could not create locale "POSIX". No error

Hmm.  Evidently Windows' _create_locale() doesn't accept "POSIX".
You might find that "C" works instead, don't know for sure.

I think this is actually a bug, because the collations code clearly
means to allow clones of the C/POSIX locales --- see eg lc_collate_is_c,
which could be noticeably simpler if that case weren't contemplated.
However, DefineCollation checks validity of the new collation by
unconditionally calling pg_newlocale_from_collation().  That violates
the advice in pg_newlocale_from_collation's header comment:

 * Also, callers should avoid calling this before going down a C/POSIX
 * fastpath, because such a fastpath should work even on platforms without
 * locale_t support in the C library.

Every other call site honors that.

So I think what we ought to do is change DefineCollation more or
less like this:

-	(void) pg_newlocale_from_collation(newoid);
+	if (!lc_collate_is_c(newoid) || !lc_ctype_is_c(newoid))
+		(void) pg_newlocale_from_collation(newoid);

Another issue exposed by this report is that we aren't reporting
_create_locale() failures in a useful way.  It's possible this
could be improved by inserting

#ifdef WIN32
	_dosmaperr(GetLastError());
#endif

into report_newlocale_failure in pg_locale.c, but I'm not really
sure.  Microsoft's man page for _create_locale() fails to say much
of anything about its error-case behavior, and definitely does not
say that it sets the GetLastError indicator.  Still, there's certainly
no chance that printing errno without doing this will be useful.
I would suggest that we do that for starters, and if we hear that
we're still getting silly errors, just hot-wire the code to assume
ENOENT on Windows.

			regards, tom lane