diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
index 434e9838947a573f4151c96fb0c25a362c2e5868..ec0d02be6b6de11f17faadd78dc5174f1ab7e9ac 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -13,7 +13,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.200 2007/10/12 18:55:12 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.201 2007/10/13 20:18:41 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -178,12 +178,12 @@ createdb(const CreatedbStmt *stmt)
 		else if (IsA(dencoding->arg, String))
 		{
 			encoding_name = strVal(dencoding->arg);
-			if (pg_valid_server_encoding(encoding_name) < 0)
+			encoding = pg_valid_server_encoding(encoding_name);
+			if (encoding < 0)
 				ereport(ERROR,
 						(errcode(ERRCODE_UNDEFINED_OBJECT),
 						 errmsg("%s is not a valid encoding name",
 								encoding_name)));
-			encoding = pg_char_to_encoding(encoding_name);
 		}
 		else
 			elog(ERROR, "unrecognized node type: %d",
diff --git a/src/backend/utils/adt/ascii.c b/src/backend/utils/adt/ascii.c
index 3bd449dcc0454a2bf3ede24764ed394583d7bb53..ce4bb6f00442a19bfdfa15635a9d2ebc4731031d 100644
--- a/src/backend/utils/adt/ascii.c
+++ b/src/backend/utils/adt/ascii.c
@@ -5,7 +5,7 @@
  *	 Portions Copyright (c) 1999-2007, PostgreSQL Global Development Group
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/utils/adt/ascii.c,v 1.30 2007/01/05 22:19:40 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/utils/adt/ascii.c,v 1.31 2007/10/13 20:18:41 tgl Exp $
  *
  *-----------------------------------------------------------------------
  */
@@ -117,7 +117,13 @@ Datum
 to_ascii_encname(PG_FUNCTION_ARGS)
 {
 	text	   *data = PG_GETARG_TEXT_P_COPY(0);
-	int			enc = pg_char_to_encoding(NameStr(*PG_GETARG_NAME(1)));
+	char	   *encname = NameStr(*PG_GETARG_NAME(1));
+	int			enc = pg_char_to_encoding(encname);
+
+	if (enc < 0)
+		ereport(ERROR,
+				(errcode(ERRCODE_UNDEFINED_OBJECT),
+				 errmsg("%s is not a valid encoding name", encname)));
 
 	PG_RETURN_TEXT_P(encode_to_ascii(data, enc));
 }
@@ -132,6 +138,11 @@ to_ascii_enc(PG_FUNCTION_ARGS)
 	text	   *data = PG_GETARG_TEXT_P_COPY(0);
 	int			enc = PG_GETARG_INT32(1);
 
+	if (!PG_VALID_ENCODING(enc))
+		ereport(ERROR,
+				(errcode(ERRCODE_UNDEFINED_OBJECT),
+				 errmsg("%d is not a valid encoding code", enc)));
+
 	PG_RETURN_TEXT_P(encode_to_ascii(data, enc));
 }
 
diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c
index 0e656b22884d9bf91da27fabc684cdbce3112719..537340811cb2cd0ba0d10822a676f435ce236a48 100644
--- a/src/backend/utils/adt/xml.c
+++ b/src/backend/utils/adt/xml.c
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.47 2007/09/23 21:36:42 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.48 2007/10/13 20:18:41 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -127,6 +127,24 @@ XmlOptionType xmloption;
 #define NAMESPACE_SQLXML "http://standards.iso.org/iso/9075/2003/sqlxml"
 
 
+#ifdef USE_LIBXML
+
+static int
+xmlChar_to_encoding(xmlChar *encoding_name)
+{
+	int		encoding = pg_char_to_encoding((char *) encoding_name);
+
+	if (encoding < 0)
+		ereport(ERROR,
+				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+				 errmsg("invalid encoding name \"%s\"",
+						(char *) encoding_name)));
+	return encoding;
+}
+
+#endif
+
+
 Datum
 xml_in(PG_FUNCTION_ARGS)
 {
@@ -263,7 +281,9 @@ xml_recv(PG_FUNCTION_ARGS)
 	/* Now that we know what we're dealing with, convert to server encoding */
 	newstr = (char *) pg_do_encoding_conversion((unsigned char *) str,
 												nbytes,
-												encoding ? pg_char_to_encoding((char *) encoding) : PG_UTF8,
+												encoding ?
+												xmlChar_to_encoding(encoding) :
+												PG_UTF8,
 												GetDatabaseEncoding());
 
 	if (newstr != str)
@@ -1084,9 +1104,9 @@ xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace, xml
 
 	utf8string = pg_do_encoding_conversion(string,
 										   len,
-										   encoding
-										   ? pg_char_to_encoding((char *) encoding)
-										   : GetDatabaseEncoding(),
+										   encoding ?
+										   xmlChar_to_encoding(encoding) :
+										   GetDatabaseEncoding(),
 										   PG_UTF8);
 
 	xml_init();
diff --git a/src/backend/utils/mb/encnames.c b/src/backend/utils/mb/encnames.c
index 7e884fd4c55d29392edf3404b531b0520cc49350..f7ef85511c920a6e83c3e82c17e3afdae9f53b6c 100644
--- a/src/backend/utils/mb/encnames.c
+++ b/src/backend/utils/mb/encnames.c
@@ -2,7 +2,7 @@
  * Encoding names and routines for work with it. All
  * in this file is shared bedween FE and BE.
  *
- * $PostgreSQL: pgsql/src/backend/utils/mb/encnames.c,v 1.34 2007/04/16 18:50:49 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/mb/encnames.c,v 1.35 2007/10/13 20:18:41 tgl Exp $
  */
 #ifdef FRONTEND
 #include "postgres_fe.h"
@@ -12,10 +12,11 @@
 #include "utils/builtins.h"
 #endif
 
+#include <ctype.h>
 #include <unistd.h>
 
 #include "mb/pg_wchar.h"
-#include <ctype.h>
+
 
 /* ----------
  * All encoding names, sorted:		 *** A L P H A B E T I C ***
@@ -313,6 +314,9 @@ pg_enc2name pg_enc2name_tbl[] =
 	{
 		"EUC_TW", PG_EUC_TW
 	},
+	{
+		"EUC_JIS_2004", PG_EUC_JIS_2004
+	},
 	{
 		"UTF8", PG_UTF8
 	},
@@ -397,9 +401,6 @@ pg_enc2name pg_enc2name_tbl[] =
 	{
 		"WIN1257", PG_WIN1257
 	},
-	{
-		"EUC_JIS_2004", PG_EUC_JIS_2004
-	},
 	{
 		"SJIS", PG_SJIS
 	},
@@ -413,10 +414,10 @@ pg_enc2name pg_enc2name_tbl[] =
 		"UHC", PG_UHC
 	},
 	{
-		"JOHAB", PG_JOHAB
+		"GB18030", PG_GB18030
 	},
 	{
-		"GB18030", PG_GB18030
+		"JOHAB", PG_JOHAB
 	},
 	{
 		"SHIFT_JIS_2004", PG_SHIFT_JIS_2004
@@ -455,6 +456,12 @@ pg_valid_server_encoding(const char *name)
 	return enc;
 }
 
+int
+pg_valid_server_encoding_id(int encoding)
+{
+	return PG_VALID_BE_ENCODING(encoding);
+}
+
 /* ----------
  * Remove irrelevant chars from encoding name
  * ----------
@@ -533,14 +540,14 @@ pg_char_to_encname_struct(const char *name)
  * Returns encoding or -1 for error
  */
 int
-pg_char_to_encoding(const char *s)
+pg_char_to_encoding(const char *name)
 {
-	pg_encname *p = NULL;
+	pg_encname *p;
 
-	if (!s)
+	if (!name)
 		return -1;
 
-	p = pg_char_to_encname_struct(s);
+	p = pg_char_to_encname_struct(name);
 	return p ? p->encoding : -1;
 }
 
diff --git a/src/backend/utils/mb/mbutils.c b/src/backend/utils/mb/mbutils.c
index 4c7a7afca0138338e845242fe119709b6f39dc08..91f65df36a675fa7da78a6c17f8e8213c63f5557 100644
--- a/src/backend/utils/mb/mbutils.c
+++ b/src/backend/utils/mb/mbutils.c
@@ -4,7 +4,7 @@
  * (currently mule internal code (mic) is used)
  * Tatsuo Ishii
  *
- * $PostgreSQL: pgsql/src/backend/utils/mb/mbutils.c,v 1.66 2007/09/24 16:38:24 adunstan Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/mb/mbutils.c,v 1.67 2007/10/13 20:18:41 tgl Exp $
  */
 #include "postgres.h"
 
@@ -421,6 +421,12 @@ length_in_encoding(PG_FUNCTION_ARGS)
 	int         len = VARSIZE(string) - VARHDRSZ;
 	int         retval;
 
+	if (src_encoding < 0)
+		ereport(ERROR,
+				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+				 errmsg("invalid encoding name \"%s\"",
+						src_encoding_name)));
+
 	retval = pg_verify_mbstr_len(src_encoding, VARDATA(string), len, false);
 	PG_RETURN_INT32(retval);
 	
diff --git a/src/bin/initdb/Makefile b/src/bin/initdb/Makefile
index 138d5ec7a3a48ad3d2102af20dc5b9a32b726b69..afbae7e837b7c411f40611e5ab8961c6a95e6d24 100644
--- a/src/bin/initdb/Makefile
+++ b/src/bin/initdb/Makefile
@@ -5,7 +5,7 @@
 # Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
 # Portions Copyright (c) 1994, Regents of the University of California
 #
-# $PostgreSQL: pgsql/src/bin/initdb/Makefile,v 1.53 2007/09/27 19:53:43 tgl Exp $
+# $PostgreSQL: pgsql/src/bin/initdb/Makefile,v 1.54 2007/10/13 20:18:41 tgl Exp $
 #
 #-------------------------------------------------------------------------
 
@@ -14,14 +14,24 @@ subdir = src/bin/initdb
 top_builddir = ../../..
 include $(top_builddir)/src/Makefile.global
 
-override CPPFLAGS := -I$(libpq_srcdir) $(CPPFLAGS)
+override CPPFLAGS := -DFRONTEND -I$(libpq_srcdir) $(CPPFLAGS)
 
-OBJS=	initdb.o $(WIN32RES)
+OBJS=	initdb.o encnames.o pqsignal.o $(WIN32RES)
 
-all: submake-libpq submake-libpgport initdb
+all: submake-libpgport initdb
 
-initdb: $(OBJS) $(libpq_builddir)/libpq.a
-	$(CC) $(CFLAGS) $(OBJS) $(libpq_pgport) $(LDFLAGS) $(LIBS) -o $@$(X)
+initdb: $(OBJS)
+	$(CC) $(CFLAGS) $(OBJS) $(LDFLAGS) $(LIBS) -o $@$(X)
+
+# We used to pull in all of libpq to get encnames and pqsignal, but that
+# exposes us to risks of version skew if we link to a shared library.
+# Do it the hard way, instead, so that we're statically linked.
+
+encnames.c: % : $(top_srcdir)/src/backend/utils/mb/%
+	rm -f $@ && $(LN_S) $< .
+
+pqsignal.c: % : $(libpq_srcdir)/%
+	rm -f $@ && $(LN_S) $< .
 
 install: all installdirs
 	$(INSTALL_PROGRAM) initdb$(X) '$(DESTDIR)$(bindir)/initdb$(X)'
@@ -33,7 +43,7 @@ uninstall:
 	rm -f '$(DESTDIR)$(bindir)/initdb$(X)'
 
 clean distclean maintainer-clean:
-	rm -f initdb$(X) $(OBJS)
+	rm -f initdb$(X) $(OBJS) encnames.c pqsignal.c
 
 
 # ensure that changes in datadir propagate into object file
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 6f51eb1367760d579c09f3f810428044a99285ef..cee895f82b979a7f9b104e72063baf9c5d4fa884 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -42,7 +42,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  * Portions taken from FreeBSD.
  *
- * $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.144 2007/09/29 00:14:40 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.145 2007/10/13 20:18:41 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -2808,7 +2808,7 @@ main(int argc, char *argv[])
 					progname);
 			exit(1);
 		}
-		else if (!PG_VALID_BE_ENCODING(ctype_enc))
+		else if (!pg_valid_server_encoding_id(ctype_enc))
 		{
 			/* We recognized it, but it's not a legal server encoding */
 			fprintf(stderr,
@@ -2968,7 +2968,7 @@ main(int argc, char *argv[])
 	{
 		char	*linkloc;
 
-		linkloc = (char *) palloc(strlen(pg_data) + 8 + 2);
+		linkloc = (char *) pg_malloc(strlen(pg_data) + 8 + 2);
 		sprintf(linkloc, "%s/pg_xlog", pg_data);
 
 		/* check if the specified xlog directory is empty */
diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c
index 01439a180739c84f547cfd4fc008f51900ce91bb..c35d5436fdbd1b0a2551261ecf8ba1b192d17d71 100644
--- a/src/bin/pg_dump/pg_backup_archiver.c
+++ b/src/bin/pg_dump/pg_backup_archiver.c
@@ -15,7 +15,7 @@
  *
  *
  * IDENTIFICATION
- *		$PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.146 2007/08/21 01:11:20 tgl Exp $
+ *		$PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.147 2007/10/13 20:18:41 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -32,7 +32,6 @@
 #endif
 
 #include "libpq/libpq-fs.h"
-#include "mb/pg_wchar.h"
 
 
 const char *progname;
@@ -1639,7 +1638,7 @@ _allocAH(const char *FileSpec, const ArchiveFormat fmt,
 	AH->vrev = K_VERS_REV;
 
 	/* initialize for backwards compatible string processing */
-	AH->public.encoding = PG_SQL_ASCII;
+	AH->public.encoding = 0;	/* PG_SQL_ASCII */
 	AH->public.std_strings = false;
 
 	/* sql error handling */
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 6444c0bc9e7f23333aa856027730f9794d002f6d..54eebfebf275b0e6275738cfe059741ac95e6470 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -12,7 +12,7 @@
  *	by PostgreSQL
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.472 2007/09/03 00:39:19 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.473 2007/10/13 20:18:41 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -47,7 +47,6 @@ int			optreset;
 #include "catalog/pg_type.h"
 #include "commands/sequence.h"
 #include "libpq/libpq-fs.h"
-#include "mb/pg_wchar.h"
 
 #include "pg_backup_archiver.h"
 #include "dumputils.h"
diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c
index f4eb74ec34d37d7c3be262f435e2b0a2cc8844b3..59f1ddbcfbaba96463db181a06293f58cff7ef2b 100644
--- a/src/bin/pg_dump/pg_dumpall.c
+++ b/src/bin/pg_dump/pg_dumpall.c
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *
- * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.92 2007/07/08 19:07:38 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.93 2007/10/13 20:18:41 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -27,7 +27,6 @@ int			optreset;
 #endif
 
 #include "dumputils.h"
-#include "mb/pg_wchar.h"
 
 
 /* version string we expect back from pg_dump */
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 04e5cce63615c76d6a5b2f9c77ed64ddbd729a01..b8cbbbb6da5c7151dbad5840c3b37768a3f80811 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2007, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.181 2007/08/21 01:11:22 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.182 2007/10/13 20:18:41 tgl Exp $
  */
 #include "postgres_fe.h"
 #include "command.h"
@@ -45,7 +45,6 @@
 #include "psqlscan.h"
 #include "settings.h"
 #include "variables.h"
-#include "mb/pg_wchar.h"
 
 
 /* functions for use in this file */
@@ -295,7 +294,7 @@ exec_command(const char *cmd,
 		}
 
 		if (pset.dirname)
-			pfree(pset.dirname);
+			free(pset.dirname);
 		pset.dirname = pg_strdup(dir);
 		canonicalize_path(pset.dirname);
 
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index a19739dca1306cefdcd5485df3deb0fe66d48687..aa395703b6cb2d03455a67d13e7c3f7e18e5e0f7 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2007, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.135 2007/06/22 01:09:28 neilc Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.136 2007/10/13 20:18:41 tgl Exp $
  */
 #include "postgres_fe.h"
 #include "common.h"
@@ -22,7 +22,6 @@
 #include "settings.h"
 #include "command.h"
 #include "copy.h"
-#include "mb/pg_wchar.h"
 #include "mbprint.h"
 
 
diff --git a/src/bin/psql/mbprint.c b/src/bin/psql/mbprint.c
index 5a61d9671e4e4b343406ecaee88fe292694bd0b5..e57e2673f5127892e3ed5d33d5b696a10884079f 100644
--- a/src/bin/psql/mbprint.c
+++ b/src/bin/psql/mbprint.c
@@ -3,15 +3,45 @@
  *
  * Copyright (c) 2000-2007, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/mbprint.c,v 1.25 2007/01/05 22:19:49 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/mbprint.c,v 1.26 2007/10/13 20:18:41 tgl Exp $
+ *
+ * XXX this file does not really belong in psql/.  Perhaps move to libpq?
+ * It also seems that the mbvalidate function is redundant with existing
+ * functionality.
  */
 
 #include "postgres_fe.h"
+#include "mbprint.h"
+#include "libpq-fe.h"
 #ifndef PGSCRIPTS
 #include "settings.h"
 #endif
-#include "mbprint.h"
-#include "mb/pg_wchar.h"
+
+/*
+ * To avoid version-skew problems, this file must not use declarations
+ * from pg_wchar.h: the encoding IDs we are dealing with are determined
+ * by the libpq.so we are linked with, and that might not match the
+ * numbers we see at compile time.  (If this file were inside libpq,
+ * the problem would go away...)
+ *
+ * Hence, we have our own definition of pg_wchar, and we get the values
+ * of any needed encoding IDs on-the-fly.
+ */
+
+typedef unsigned int pg_wchar;
+
+static int
+get_utf8_id(void)
+{
+	static int	utf8_id = -1;
+
+	if (utf8_id < 0)
+		utf8_id = pg_char_to_encoding("utf8");
+	return utf8_id;
+}
+
+#define PG_UTF8		get_utf8_id()
+
 
 static pg_wchar
 utf2ucs(const unsigned char *c)
diff --git a/src/bin/psql/psqlscan.l b/src/bin/psql/psqlscan.l
index 4be4ed389d673b02b6dfec76dafa074c37b77004..09b4c83d74dfb3b21b536b5bd9adc103331e7c68 100644
--- a/src/bin/psql/psqlscan.l
+++ b/src/bin/psql/psqlscan.l
@@ -33,7 +33,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/bin/psql/psqlscan.l,v 1.21 2007/01/05 22:19:49 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/bin/psql/psqlscan.l,v 1.22 2007/10/13 20:18:41 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -43,8 +43,6 @@
 
 #include <ctype.h>
 
-#include "mb/pg_wchar.h"
-
 #include "common.h"
 #include "settings.h"
 #include "variables.h"
@@ -1021,7 +1019,7 @@ psql_scan_setup(PsqlScanState state,
 
 	/* Do we need to hack the character set encoding? */
 	state->encoding = pset.encoding;
-	state->safe_encoding = PG_VALID_BE_ENCODING(state->encoding);
+	state->safe_encoding = pg_valid_server_encoding_id(state->encoding);
 
 	/* needed for prepare_buffer */
 	cur_state = state;
diff --git a/src/bin/scripts/createdb.c b/src/bin/scripts/createdb.c
index 5004f3c63e6460cdf69e96bdc08e95c4b3d9c148..71f81aa2c8e34267ccb1296bb82351c46c2f4cc6 100644
--- a/src/bin/scripts/createdb.c
+++ b/src/bin/scripts/createdb.c
@@ -5,17 +5,15 @@
  * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/bin/scripts/createdb.c,v 1.23 2007/06/04 10:02:40 petere Exp $
+ * $PostgreSQL: pgsql/src/bin/scripts/createdb.c,v 1.24 2007/10/13 20:18:41 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
-
 #include "postgres_fe.h"
+
 #include "common.h"
 #include "dumputils.h"
 
-#include "mb/pg_wchar.h"
-
 
 static void help(const char *progname);
 
diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h
index bd11f0896dfa69c578b839c4e9d3b24ba298e79f..4c9e6ea868ebac17b67dee61ea920176278c461d 100644
--- a/src/include/catalog/catversion.h
+++ b/src/include/catalog/catversion.h
@@ -37,7 +37,7 @@
  * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.430 2007/09/30 19:54:58 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.431 2007/10/13 20:18:41 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -53,6 +53,6 @@
  */
 
 /*							yyyymmddN */
-#define CATALOG_VERSION_NO	200709301
+#define CATALOG_VERSION_NO	200710131
 
 #endif
diff --git a/src/include/mb/pg_wchar.h b/src/include/mb/pg_wchar.h
index 19b5fe1e3a7d5c13b890a73d309896c61c90c6e9..7660a7d58516fe317f73491774d3ac1de1a1e68c 100644
--- a/src/include/mb/pg_wchar.h
+++ b/src/include/mb/pg_wchar.h
@@ -1,19 +1,28 @@
-/* $PostgreSQL: pgsql/src/include/mb/pg_wchar.h,v 1.73 2007/09/18 17:41:17 adunstan Exp $ */
-
+/*-------------------------------------------------------------------------
+ *
+ * pg_wchar.h
+ *	  multibyte-character support
+ *
+ * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * $PostgreSQL: pgsql/src/include/mb/pg_wchar.h,v 1.74 2007/10/13 20:18:41 tgl Exp $
+ *
+ *	NOTES
+ *		This is used both by the backend and by libpq, but should not be
+ *		included by libpq client programs.  In particular, a libpq client
+ *		should not assume that the encoding IDs used by the version of libpq
+ *		it's linked to match up with the IDs declared here.
+ *
+ *-------------------------------------------------------------------------
+ */
 #ifndef PG_WCHAR_H
 #define PG_WCHAR_H
 
 #include <sys/types.h>
 
-#ifdef FRONTEND
-#undef palloc
-#define palloc malloc
-#undef pfree
-#define pfree free
-#endif
-
 /*
- * The pg_wchar
+ * The pg_wchar type
  */
 typedef unsigned int pg_wchar;
 
@@ -149,7 +158,12 @@ typedef unsigned int pg_wchar;
  *			If you add some encoding don't forget to check
  *			PG_ENCODING_BE_LAST macro.
  *
- * The PG_SQL_ASCII is default encoding and must be = 0.
+ * PG_SQL_ASCII is default encoding and must be = 0.
+ *
+ * XXX  We must avoid renumbering any backend encoding until libpq's major
+ * version number is increased beyond 5; it turns out that the backend
+ * encoding IDs are effectively part of libpq's ABI as far as 8.2 initdb and
+ * psql are concerned.
  */
 typedef enum pg_enc
 {
@@ -158,6 +172,7 @@ typedef enum pg_enc
 	PG_EUC_CN,					/* EUC for Chinese */
 	PG_EUC_KR,					/* EUC for Korean */
 	PG_EUC_TW,					/* EUC for Taiwan */
+	PG_EUC_JIS_2004,			/* EUC-JIS-2004 */
 	PG_UTF8,					/* Unicode UTF8 */
 	PG_MULE_INTERNAL,			/* Mule internal code */
 	PG_LATIN1,					/* ISO-8859-1 Latin 1 */
@@ -186,7 +201,6 @@ typedef enum pg_enc
 	PG_WIN1254,					/* windows-1254 */
 	PG_WIN1255,					/* windows-1255 */
 	PG_WIN1257,					/* windows-1257 */
-	PG_EUC_JIS_2004,			/* EUC-JIS-2004 */
 	/* PG_ENCODING_BE_LAST points to the above entry */
 
 	/* followings are for client encoding only */
@@ -194,14 +208,14 @@ typedef enum pg_enc
 	PG_BIG5,					/* Big5 (Windows-950) */
 	PG_GBK,						/* GBK (Windows-936) */
 	PG_UHC,						/* UHC (Windows-949) */
-	PG_JOHAB,					/* EUC for Korean JOHAB */
 	PG_GB18030,					/* GB18030 */
+	PG_JOHAB,					/* EUC for Korean JOHAB */
 	PG_SHIFT_JIS_2004,			/* Shift-JIS-2004 */
 	_PG_LAST_ENCODING_			/* mark only */
 
 } pg_enc;
 
-#define PG_ENCODING_BE_LAST PG_EUC_JIS_2004
+#define PG_ENCODING_BE_LAST PG_WIN1257
 
 /*
  * Please use these tests before access to pg_encconv_tbl[]
@@ -245,11 +259,6 @@ typedef struct pg_enc2name
 
 extern pg_enc2name pg_enc2name_tbl[];
 
-extern pg_encname *pg_char_to_encname_struct(const char *name);
-
-extern int	pg_char_to_encoding(const char *s);
-extern const char *pg_encoding_to_char(int encoding);
-
 /*
  * pg_wchar stuff
  */
@@ -315,6 +324,21 @@ typedef struct
 	uint32 utf2;			/* UTF-8 code 2 */
 } pg_local_to_utf_combined;
 
+
+/*
+ * These functions are considered part of libpq's exported API and
+ * are also declared in libpq-fe.h.
+ */
+extern int	pg_char_to_encoding(const char *name);
+extern const char *pg_encoding_to_char(int encoding);
+extern int	pg_valid_server_encoding_id(int encoding);
+
+/*
+ * Remaining functions are not considered part of libpq's API, though many
+ * of them do exist inside libpq.
+ */
+extern pg_encname *pg_char_to_encname_struct(const char *name);
+
 extern int	pg_mb2wchar(const char *from, pg_wchar *to);
 extern int	pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len);
 extern int	pg_encoding_mb2wchar_with_len(int encoding,
diff --git a/src/interfaces/libpq/exports.txt b/src/interfaces/libpq/exports.txt
index a0ead41ace52b5c54f658226b06e87b718fe119a..222a6595b43971fadca5d2d4f5c5e1ee31d5980f 100644
--- a/src/interfaces/libpq/exports.txt
+++ b/src/interfaces/libpq/exports.txt
@@ -1,4 +1,4 @@
-# $PostgreSQL: pgsql/src/interfaces/libpq/exports.txt,v 1.16 2007/07/08 17:11:51 joe Exp $
+# $PostgreSQL: pgsql/src/interfaces/libpq/exports.txt,v 1.17 2007/10/13 20:18:41 tgl Exp $
 # Functions to be exported by libpq DLLs
 PQconnectdb               1
 PQsetdbLogin              2
@@ -138,3 +138,4 @@ PQsendDescribePrepared    135
 PQsendDescribePortal      136
 lo_truncate               137
 PQconnectionUsedPassword  138
+pg_valid_server_encoding_id 139
diff --git a/src/interfaces/libpq/fe-exec.c b/src/interfaces/libpq/fe-exec.c
index 7b46c14118b2e8e804bc7a6c7cdb0f194db60346..c42ede02e8522381b748b26389fed0dc2cf7098b 100644
--- a/src/interfaces/libpq/fe-exec.c
+++ b/src/interfaces/libpq/fe-exec.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/interfaces/libpq/fe-exec.c,v 1.192 2007/01/05 22:20:01 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/interfaces/libpq/fe-exec.c,v 1.193 2007/10/13 20:18:42 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -630,11 +630,14 @@ pqSaveParameterStatus(PGconn *conn, const char *name, const char *value)
 	 * standard_conforming_strings, and convert server version to a numeric
 	 * form.  We keep the first two of these in static variables as well, so
 	 * that PQescapeString and PQescapeBytea can behave somewhat sanely (at
-	 * least in single- connection-using programs).
+	 * least in single-connection-using programs).
 	 */
 	if (strcmp(name, "client_encoding") == 0)
 	{
 		conn->client_encoding = pg_char_to_encoding(value);
+		/* if we don't recognize the encoding name, fall back to SQL_ASCII */
+		if (conn->client_encoding < 0)
+			conn->client_encoding = PG_SQL_ASCII;
 		static_client_encoding = conn->client_encoding;
 	}
 	else if (strcmp(name, "standard_conforming_strings") == 0)
diff --git a/src/interfaces/libpq/fe-misc.c b/src/interfaces/libpq/fe-misc.c
index cffbea62801807aeeaa3940e2ccb2653ee3da756..c17c2bfc54401a7e8f03a500377de3241fd4424d 100644
--- a/src/interfaces/libpq/fe-misc.c
+++ b/src/interfaces/libpq/fe-misc.c
@@ -23,7 +23,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/interfaces/libpq/fe-misc.c,v 1.131 2007/01/05 22:20:01 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/interfaces/libpq/fe-misc.c,v 1.132 2007/10/13 20:18:42 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1112,7 +1112,11 @@ PQenv2encoding(void)
 
 	str = getenv("PGCLIENTENCODING");
 	if (str && *str != '\0')
+	{
 		encoding = pg_char_to_encoding(str);
+		if (encoding < 0)
+			encoding = PG_SQL_ASCII;
+	}
 	return encoding;
 }
 
diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h
index 93dbeb37d8bd4de443f0f401f062329db2a43953..9c51c532dc677c14af73999a0281d0d2aab60ced 100644
--- a/src/interfaces/libpq/libpq-fe.h
+++ b/src/interfaces/libpq/libpq-fe.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-fe.h,v 1.138 2007/07/08 18:28:55 tgl Exp $
+ * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-fe.h,v 1.139 2007/10/13 20:18:42 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -511,6 +511,12 @@ extern int	PQenv2encoding(void);
 
 extern char *PQencryptPassword(const char *passwd, const char *user);
 
+/* === in encnames.c === */
+
+extern int	pg_char_to_encoding(const char *name);
+extern const char *pg_encoding_to_char(int encoding);
+extern int	pg_valid_server_encoding_id(int encoding);
+
 #ifdef __cplusplus
 }
 #endif