Tom Lane wrote:
> Bruce Momjian <br...@momjian.us> writes:
> > Greg Sabino Mullane wrote:
> >> Specifically, LANGUAGE changes the headers of pg_controldata 
> >> (but not the actual output, LC_ALL does that). Thanks for the 
> >> nudge, I'll get to rewriting some code.
> 
> > pg_upgrade does this in controldata.c for this exact reason:
> 
> >         /*
> >          * Because we test the pg_resetxlog output strings, it has to be in
> >          * English.
> >          */
> >         if (getenv("LANG"))
> >             lang = pg_strdup(ctx, getenv("LANG"));
> >     #ifndef WIN32
> >         putenv(pg_strdup(ctx, "LANG=C"));
> >     #else
> >         SetEnvironmentVariableA("LANG", "C");
> >     #endif
> 
> You do realize that's far from bulletproof?  To be sure that that does
> anything, you'd need to set (or unset) LC_ALL and LC_MESSAGES as well.
> And I thought Windows spelled it LANGUAGE not LANG ...

I have implemented your suggestion of setting LANG, LANGUAGE, and
LC_MESSAGES based on code in pg_regress.c;  that is the only place I see
where we force English, and it certainly has received more testing.

Should I set LC_ALL too?  The other variables mentioned in pg_regress.c?
Is this something I should patch to 9.0.X?

Patch attached.

-- 
  Bruce Momjian  <br...@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + It's impossible for everything to be true. +
Index: contrib/pg_upgrade/controldata.c
===================================================================
RCS file: /cvsroot/pgsql/contrib/pg_upgrade/controldata.c,v
retrieving revision 1.9
diff -c -c -r1.9 controldata.c
*** contrib/pg_upgrade/controldata.c	6 Jul 2010 19:18:55 -0000	1.9
--- contrib/pg_upgrade/controldata.c	1 Sep 2010 20:30:45 -0000
***************
*** 11,16 ****
--- 11,17 ----
  
  #include <ctype.h>
  
+ static void putenv2(migratorContext *ctx, const char *var, const char *val);
  
  /*
   * get_control_data()
***************
*** 52,69 ****
  	bool		got_date_is_int = false;
  	bool		got_float8_pass_by_value = false;
  	char	   *lang = NULL;
  
  	/*
! 	 * Because we test the pg_resetxlog output strings, it has to be in
! 	 * English.
  	 */
  	if (getenv("LANG"))
  		lang = pg_strdup(ctx, getenv("LANG"));
  #ifndef WIN32
! 	putenv(pg_strdup(ctx, "LANG=C"));
  #else
! 	SetEnvironmentVariableA("LANG", "C");
  #endif
  	snprintf(cmd, sizeof(cmd), SYSTEMQUOTE "\"%s/%s \"%s\"" SYSTEMQUOTE,
  			 cluster->bindir,
  			 live_check ? "pg_controldata\"" : "pg_resetxlog\" -n",
--- 53,82 ----
  	bool		got_date_is_int = false;
  	bool		got_float8_pass_by_value = false;
  	char	   *lang = NULL;
+ 	char	   *language = NULL;
+ 	char	   *lc_messages = NULL;
  
  	/*
! 	 * Because we test the pg_resetxlog output as strings, it has to be in
! 	 * English.  Copied from pg_regress.c.
  	 */
  	if (getenv("LANG"))
  		lang = pg_strdup(ctx, getenv("LANG"));
+ 	if (getenv("LANGUAGE"))
+ 		language = pg_strdup(ctx, getenv("LANGUAGE"));
+ 	if (getenv("LC_MESSAGES"))
+ 		lc_messages = pg_strdup(ctx, getenv("LC_MESSAGES"));
+ 
+ 	putenv2(ctx, "LANG",
  #ifndef WIN32
! 			NULL);
  #else
! 			/* On Windows the default locale cannot be English, so force it */
! 			"en");
  #endif
+ 	putenv2(ctx, "LANGUAGE", NULL);
+ 	putenv2(ctx, "LC_MESSAGES", "C");
+ 
  	snprintf(cmd, sizeof(cmd), SYSTEMQUOTE "\"%s/%s \"%s\"" SYSTEMQUOTE,
  			 cluster->bindir,
  			 live_check ? "pg_controldata\"" : "pg_resetxlog\" -n",
***************
*** 334,361 ****
  	if (output)
  		pclose(output);
  
! 	/* restore LANG */
! 	if (lang)
! 	{
! #ifndef WIN32
! 		char	   *envstr = (char *) pg_malloc(ctx, strlen(lang) + 6);
! 
! 		sprintf(envstr, "LANG=%s", lang);
! 		putenv(envstr);
! #else
! 		SetEnvironmentVariableA("LANG", lang);
! #endif
! 		pg_free(lang);
! 	}
! 	else
! 	{
! #ifndef WIN32
! 		unsetenv("LANG");
! #else
! 		SetEnvironmentVariableA("LANG", "");
! #endif
! 	}
! 
  	/* verify that we got all the mandatory pg_control data */
  	if (!got_xid || !got_oid ||
  		(!live_check && !got_log_id) ||
--- 347,360 ----
  	if (output)
  		pclose(output);
  
! 	/* restore environment variable settings */
! 	putenv2(ctx, "LANG", lang);
! 	putenv2(ctx, "LANGUAGE", language);
! 	putenv2(ctx, "LC_MESSAGES", lc_messages);
! 	pg_free(lang);
! 	pg_free(language);
! 	pg_free(lc_messages);
!  
  	/* verify that we got all the mandatory pg_control data */
  	if (!got_xid || !got_oid ||
  		(!live_check && !got_log_id) ||
***************
*** 492,494 ****
--- 491,524 ----
  		pg_log(ctx, PG_FATAL, "Unable to rename %s to %s.\n", old_path, new_path);
  	check_ok(ctx);
  }
+ 
+ 
+ /*
+  *	This is like putenv(), but takes two arguments
+  *	It also does unsetenv() if val is NULL.
+  */
+ static void
+ putenv2(migratorContext *ctx, const char *var, const char *val)
+ {
+ 	if (val)
+ 	{
+ #ifndef WIN32
+ 		char	   *envstr = (char *) pg_malloc(ctx, strlen(var) +
+ 												strlen(val) + 1);
+ 
+ 		sprintf(envstr, "%s=%s", var, val);
+ 		putenv(envstr);
+ 		pg_free(envstr);
+ #else
+ 		SetEnvironmentVariableA(var, val);
+ #endif
+ 	}
+ 	else
+ 	{
+ #ifndef WIN32
+ 		unsetenv(var);
+ #else
+ 		SetEnvironmentVariableA(var, "");
+ #endif
+ 	}
+ }
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to