Re: plperl vs LC_COLLATE (was Re: Possible savepoint bug) - Mailing list pgsql-hackers

From Andrew Dunstan
Subject Re: plperl vs LC_COLLATE (was Re: Possible savepoint bug)
Date
Msg-id 43C144C5.1020305@dunslane.net
Whole thread Raw
In response to Re: plperl vs LC_COLLATE (was Re: Possible savepoint bug)  (Andrew Dunstan <andrew@dunslane.net>)
Responses Re: plperl vs LC_COLLATE (was Re: Possible savepoint bug)  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers

I wrote:

>
> Further testing shows the problem persisting. Back to the drawing board.
>
>

The attached patch against cvs tip does seem to work. Instead of playing
with the environment, we simply allow perl to do its worst and then put
things back the way we wanted them.

Comments?

cheers

andrew
Index: plperl.c
===================================================================
RCS file: /cvsroot/pgsql/src/pl/plperl/plperl.c,v
retrieving revision 1.98
diff -c -r1.98 plperl.c
*** plperl.c    29 Dec 2005 14:28:31 -0000    1.98
--- plperl.c    8 Jan 2006 16:54:53 -0000
***************
*** 45,50 ****
--- 45,51 ----
  #include <ctype.h>
  #include <fcntl.h>
  #include <unistd.h>
+ #include <locale.h>

  /* postgreSQL stuff */
  #include "commands/trigger.h"
***************
*** 263,268 ****
--- 264,303 ----
          "", "-e", PERLBOOT
      };

+ #ifdef WIN32
+
+     /*
+      * The perl library on startup does horrible things like call
+      * setlocale(LC_ALL,""). We have protected against that on most
+      * platforms by setting the environment appropriately. However, on
+      * Windows, setlocale() does not consult the environment, so we need
+      * to save the excisting locale settings before perl has a chance to
+      * mangle them and restore them after its dirty deeds are done.
+      *
+      * MSDN ref:
+      * http://msdn.microsoft.com/library/en-us/vclib/html/_crt_locale.asp
+      *
+      * It appaers that we only need to do this on interpreter startup, and
+      * subsequent calls to the interpreter don't mess with the locale
+      * settings.
+      */
+
+     char *loc;
+     char *save_collate, *save_ctype, *save_monetary, *save_numeric, *save_time;
+
+     loc = setlocale(LC_COLLATE,NULL);
+     save_collate = loc ? pstrdup(loc) : NULL;
+     loc = setlocale(LC_CTYPE,NULL);
+     save_ctype = loc ? pstrdup(loc) : NULL;
+     loc = setlocale(LC_MONETARY,NULL);
+     save_monetary = loc ? pstrdup(loc) : NULL;
+     loc = setlocale(LC_NUMERIC,NULL);
+     save_numeric = loc ? pstrdup(loc) : NULL;
+     loc = setlocale(LC_TIME,NULL);
+     save_time = loc ? pstrdup(loc) : NULL;
+
+ #endif
+
      plperl_interp = perl_alloc();
      if (!plperl_interp)
          elog(ERROR, "could not allocate Perl interpreter");
***************
*** 272,277 ****
--- 307,328 ----
      perl_run(plperl_interp);

      plperl_proc_hash = newHV();
+
+ #ifdef WIN32
+
+     if (save_collate != NULL)
+       setlocale(LC_COLLATE,save_collate);
+     if (save_ctype != NULL)
+       setlocale(LC_CTYPE,save_ctype);
+     if (save_monetary != NULL)
+       setlocale(LC_MONETARY,save_monetary);
+     if (save_numeric != NULL)
+       setlocale(LC_NUMERIC,save_numeric);
+     if (save_time != NULL)
+       setlocale(LC_TIME,save_time);
+
+ #endif
+
  }



pgsql-hackers by date:

Previous
From: Greg Stark
Date:
Subject: Re: Stats collector performance improvement
Next
From: "Luke Lonergan"
Date:
Subject: Re: Libpq COPY optimization