From af4c59d5beabfda166d8a17f917a5f7d7281bfbf Mon Sep 17 00:00:00 2001
From: Mark Dilger <mark.dilger@enterprisedb.com>
Date: Wed, 22 Jan 2020 19:19:23 -0800
Subject: [PATCH 07/11] Adding utils/mbutils.h

Moving functions not suitable for frontend code out of common/pg_wchar.h into
new file utils/mbutils.h.
---
 contrib/btree_gist/btree_utils_var.h          |   1 +
 contrib/dblink/dblink.c                       |   1 +
 .../pg_stat_statements/pg_stat_statements.c   |   1 +
 contrib/pgcrypto/pgp-pgsql.c                  |   1 +
 contrib/postgres_fdw/connection.c             |   1 +
 src/backend/access/spgist/spgtextproc.c       |   1 +
 src/backend/catalog/namespace.c               |   1 +
 src/backend/catalog/pg_collation.c            |   1 +
 src/backend/catalog/pg_proc.c                 |   1 +
 src/backend/commands/collationcmds.c          |   1 +
 src/backend/commands/copy.c                   |   1 +
 src/backend/commands/extension.c              |   1 +
 src/backend/commands/indexcmds.c              |   1 +
 src/backend/commands/variable.c               |   1 +
 src/backend/executor/execMain.c               |   1 +
 src/backend/executor/execPartition.c          |   1 +
 src/backend/executor/execUtils.c              |   1 +
 src/backend/parser/parse_node.c               |   1 +
 src/backend/parser/parser.c                   |   1 +
 src/backend/parser/scan.l                     |   1 +
 src/backend/parser/scansup.c                  |   1 +
 src/backend/postmaster/pgstat.c               |   1 +
 .../libpqwalreceiver/libpqwalreceiver.c       |   1 +
 .../replication/logical/logicalfuncs.c        |   1 +
 src/backend/utils/adt/ascii.c                 |   1 +
 src/backend/utils/adt/format_type.c           |   1 +
 src/backend/utils/adt/formatting.c            |   1 +
 src/backend/utils/adt/genfile.c               |   1 +
 src/backend/utils/adt/json.c                  |   1 +
 src/backend/utils/adt/jsonpath_scan.l         |   1 +
 src/backend/utils/adt/like.c                  |   1 +
 src/backend/utils/adt/like_support.c          |   1 +
 src/backend/utils/adt/name.c                  |   1 +
 src/backend/utils/adt/oracle_compat.c         |   1 +
 src/backend/utils/adt/pg_locale.c             |   1 +
 src/backend/utils/adt/ruleutils.c             |   1 +
 src/backend/utils/adt/tsvector_op.c           |   1 +
 src/backend/utils/adt/varchar.c               |   1 +
 src/backend/utils/adt/xml.c                   |   1 +
 src/backend/utils/error/elog.c                |   1 +
 src/backend/utils/init/postinit.c             |   1 +
 src/backend/utils/mb/conv.c                   |   1 +
 .../cyrillic_and_mic/cyrillic_and_mic.c       |   1 +
 .../euc2004_sjis2004/euc2004_sjis2004.c       |   1 +
 .../euc_cn_and_mic/euc_cn_and_mic.c           |   1 +
 .../euc_jp_and_sjis/euc_jp_and_sjis.c         |   1 +
 .../euc_kr_and_mic/euc_kr_and_mic.c           |   1 +
 .../conversion_procs/euc_tw_and_big5/big5.c   |   1 +
 .../euc_tw_and_big5/euc_tw_and_big5.c         |   1 +
 .../latin2_and_win1250/latin2_and_win1250.c   |   1 +
 .../latin_and_mic/latin_and_mic.c             |   1 +
 .../utf8_and_big5/utf8_and_big5.c             |   1 +
 .../utf8_and_cyrillic/utf8_and_cyrillic.c     |   1 +
 .../utf8_and_euc2004/utf8_and_euc2004.c       |   1 +
 .../utf8_and_euc_cn/utf8_and_euc_cn.c         |   1 +
 .../utf8_and_euc_jp/utf8_and_euc_jp.c         |   1 +
 .../utf8_and_euc_kr/utf8_and_euc_kr.c         |   1 +
 .../utf8_and_euc_tw/utf8_and_euc_tw.c         |   1 +
 .../utf8_and_gb18030/utf8_and_gb18030.c       |   1 +
 .../utf8_and_gbk/utf8_and_gbk.c               |   1 +
 .../utf8_and_iso8859/utf8_and_iso8859.c       |   1 +
 .../utf8_and_iso8859_1/utf8_and_iso8859_1.c   |   1 +
 .../utf8_and_johab/utf8_and_johab.c           |   1 +
 .../utf8_and_sjis/utf8_and_sjis.c             |   1 +
 .../utf8_and_sjis2004/utf8_and_sjis2004.c     |   1 +
 .../utf8_and_uhc/utf8_and_uhc.c               |   1 +
 .../utf8_and_win/utf8_and_win.c               |   1 +
 src/backend/utils/mb/mbutils.c                |   1 +
 src/backend/utils/mb/stringinfo_mb.c          |   1 +
 src/backend/utils/misc/guc-file.l             |   1 +
 src/backend/utils/mmgr/mcxt.c                 |   1 +
 src/common/wchar.c                            |   1 +
 src/include/common/pg_wchar.h                 | 109 ---------------
 src/include/libpq/pqformat.h                  |   1 +
 src/include/regex/regcustom.h                 |   2 +
 src/include/regex/regex.h                     |   1 +
 src/include/tsearch/ts_locale.h               |   1 +
 src/include/utils/mbutils.h                   | 124 ++++++++++++++++++
 src/interfaces/libpq/fe-protocol3.c           |   1 +
 src/pl/plperl/plperl.c                        |   1 +
 src/pl/plperl/plperl_helpers.h                |   2 +-
 src/pl/plpgsql/src/pl_scanner.c               |   1 +
 src/pl/plpython/plpy_cursorobject.c           |   1 +
 src/pl/plpython/plpy_plpymodule.c             |   1 +
 src/pl/plpython/plpy_spi.c                    |   1 +
 src/pl/plpython/plpy_typeio.c                 |   1 +
 src/pl/plpython/plpy_util.c                   |   1 +
 87 files changed, 210 insertions(+), 110 deletions(-)
 create mode 100644 src/include/utils/mbutils.h

diff --git a/contrib/btree_gist/btree_utils_var.h b/contrib/btree_gist/btree_utils_var.h
index 4ae273d767..a63e6ace2f 100644
--- a/contrib/btree_gist/btree_utils_var.h
+++ b/contrib/btree_gist/btree_utils_var.h
@@ -7,6 +7,7 @@
 #include "access/gist.h"
 #include "btree_gist.h"
 #include "common/pg_wchar.h"
+#include "utils/mbutils.h"
 
 /* Variable length key */
 typedef bytea GBT_VARKEY;
diff --git a/contrib/dblink/dblink.c b/contrib/dblink/dblink.c
index a202772fad..1eda3c63de 100644
--- a/contrib/dblink/dblink.c
+++ b/contrib/dblink/dblink.c
@@ -57,6 +57,7 @@
 #include "utils/fmgroids.h"
 #include "utils/guc.h"
 #include "utils/lsyscache.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 #include "utils/rel.h"
 #include "utils/varlena.h"
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 53fa917e95..37f1516922 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -78,6 +78,7 @@
 #include "utils/acl.h"
 #include "utils/builtins.h"
 #include "utils/hashutils.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 
 PG_MODULE_MAGIC;
diff --git a/contrib/pgcrypto/pgp-pgsql.c b/contrib/pgcrypto/pgp-pgsql.c
index 960fa1c3e0..3ac659e4f0 100644
--- a/contrib/pgcrypto/pgp-pgsql.c
+++ b/contrib/pgcrypto/pgp-pgsql.c
@@ -40,6 +40,7 @@
 #include "px.h"
 #include "utils/array.h"
 #include "utils/builtins.h"
+#include "utils/mbutils.h"
 
 /*
  * public functions
diff --git a/contrib/postgres_fdw/connection.c b/contrib/postgres_fdw/connection.c
index db897e7819..6c0b13a80c 100644
--- a/contrib/postgres_fdw/connection.c
+++ b/contrib/postgres_fdw/connection.c
@@ -23,6 +23,7 @@
 #include "storage/latch.h"
 #include "utils/hsearch.h"
 #include "utils/inval.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 #include "utils/syscache.h"
 
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index d166a6352c..f56424ef5c 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -44,6 +44,7 @@
 #include "common/pg_wchar.h"
 #include "utils/builtins.h"
 #include "utils/datum.h"
+#include "utils/mbutils.h"
 #include "utils/pg_locale.h"
 #include "utils/varlena.h"
 
diff --git a/src/backend/catalog/namespace.c b/src/backend/catalog/namespace.c
index f7fd77ed1a..8d08a60e56 100644
--- a/src/backend/catalog/namespace.c
+++ b/src/backend/catalog/namespace.c
@@ -54,6 +54,7 @@
 #include "utils/guc.h"
 #include "utils/inval.h"
 #include "utils/lsyscache.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 #include "utils/syscache.h"
 #include "utils/varlena.h"
diff --git a/src/backend/catalog/pg_collation.c b/src/backend/catalog/pg_collation.c
index a5dd02d2ee..2f07f45fdc 100644
--- a/src/backend/catalog/pg_collation.c
+++ b/src/backend/catalog/pg_collation.c
@@ -27,6 +27,7 @@
 #include "common/pg_wchar.h"
 #include "utils/builtins.h"
 #include "utils/fmgroids.h"
+#include "utils/mbutils.h"
 #include "utils/pg_locale.h"
 #include "utils/rel.h"
 #include "utils/syscache.h"
diff --git a/src/backend/catalog/pg_proc.c b/src/backend/catalog/pg_proc.c
index d9bb3ebc2a..b7def351fa 100644
--- a/src/backend/catalog/pg_proc.c
+++ b/src/backend/catalog/pg_proc.c
@@ -38,6 +38,7 @@
 #include "utils/acl.h"
 #include "utils/builtins.h"
 #include "utils/lsyscache.h"
+#include "utils/mbutils.h"
 #include "utils/regproc.h"
 #include "utils/rel.h"
 #include "utils/syscache.h"
diff --git a/src/backend/commands/collationcmds.c b/src/backend/commands/collationcmds.c
index 59c0abeea5..2869cb68a4 100644
--- a/src/backend/commands/collationcmds.c
+++ b/src/backend/commands/collationcmds.c
@@ -31,6 +31,7 @@
 #include "miscadmin.h"
 #include "utils/builtins.h"
 #include "utils/lsyscache.h"
+#include "utils/mbutils.h"
 #include "utils/pg_locale.h"
 #include "utils/rel.h"
 #include "utils/syscache.h"
diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c
index 02cb2d9718..58d177829f 100644
--- a/src/backend/commands/copy.c
+++ b/src/backend/commands/copy.c
@@ -51,6 +51,7 @@
 #include "tcop/tcopprot.h"
 #include "utils/builtins.h"
 #include "utils/lsyscache.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 #include "utils/partcache.h"
 #include "utils/portal.h"
diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c
index b17bc69a65..cf82cd49d8 100644
--- a/src/backend/commands/extension.c
+++ b/src/backend/commands/extension.c
@@ -60,6 +60,7 @@
 #include "utils/builtins.h"
 #include "utils/fmgroids.h"
 #include "utils/lsyscache.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 #include "utils/rel.h"
 #include "utils/snapmgr.h"
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index e27ead40bd..365d610e39 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -59,6 +59,7 @@
 #include "utils/fmgroids.h"
 #include "utils/inval.h"
 #include "utils/lsyscache.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 #include "utils/partcache.h"
 #include "utils/pg_rusage.h"
diff --git a/src/backend/commands/variable.c b/src/backend/commands/variable.c
index a197cebb84..e27957bc5b 100644
--- a/src/backend/commands/variable.c
+++ b/src/backend/commands/variable.c
@@ -28,6 +28,7 @@
 #include "miscadmin.h"
 #include "utils/acl.h"
 #include "utils/builtins.h"
+#include "utils/mbutils.h"
 #include "utils/snapmgr.h"
 #include "utils/syscache.h"
 #include "utils/timestamp.h"
diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c
index 5275f439c0..0ee1163d21 100644
--- a/src/backend/executor/execMain.c
+++ b/src/backend/executor/execMain.c
@@ -59,6 +59,7 @@
 #include "tcop/utility.h"
 #include "utils/acl.h"
 #include "utils/lsyscache.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 #include "utils/partcache.h"
 #include "utils/rls.h"
diff --git a/src/backend/executor/execPartition.c b/src/backend/executor/execPartition.c
index 169ff50407..e67fb8698a 100644
--- a/src/backend/executor/execPartition.c
+++ b/src/backend/executor/execPartition.c
@@ -29,6 +29,7 @@
 #include "partitioning/partprune.h"
 #include "rewrite/rewriteManip.h"
 #include "utils/lsyscache.h"
+#include "utils/mbutils.h"
 #include "utils/partcache.h"
 #include "utils/rls.h"
 #include "utils/ruleutils.h"
diff --git a/src/backend/executor/execUtils.c b/src/backend/executor/execUtils.c
index 673f2db641..eddb6c7560 100644
--- a/src/backend/executor/execUtils.c
+++ b/src/backend/executor/execUtils.c
@@ -58,6 +58,7 @@
 #include "partitioning/partdesc.h"
 #include "storage/lmgr.h"
 #include "utils/builtins.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 #include "utils/rel.h"
 #include "utils/typcache.h"
diff --git a/src/backend/parser/parse_node.c b/src/backend/parser/parse_node.c
index a075354805..c5993d01c5 100644
--- a/src/backend/parser/parse_node.c
+++ b/src/backend/parser/parse_node.c
@@ -27,6 +27,7 @@
 #include "utils/builtins.h"
 #include "utils/int8.h"
 #include "utils/lsyscache.h"
+#include "utils/mbutils.h"
 #include "utils/syscache.h"
 #include "utils/varbit.h"
 
diff --git a/src/backend/parser/parser.c b/src/backend/parser/parser.c
index 0922e9436f..6d27d1c5b7 100644
--- a/src/backend/parser/parser.c
+++ b/src/backend/parser/parser.c
@@ -25,6 +25,7 @@
 #include "parser/gramparse.h"
 #include "parser/parser.h"
 #include "parser/scansup.h"
+#include "utils/mbutils.h"
 
 static bool check_uescapechar(unsigned char escape);
 static char *str_udeescape(const char *str, char escape,
diff --git a/src/backend/parser/scan.l b/src/backend/parser/scan.l
index 6bade98a30..d25ed1ddc4 100644
--- a/src/backend/parser/scan.l
+++ b/src/backend/parser/scan.l
@@ -40,6 +40,7 @@
 #include "parser/gramparse.h"
 #include "parser/parser.h"		/* only needed for GUC variables */
 #include "parser/scansup.h"
+#include "utils/mbutils.h"
 }
 
 %{
diff --git a/src/backend/parser/scansup.c b/src/backend/parser/scansup.c
index ff1e6ab8d5..6fc2b59fa3 100644
--- a/src/backend/parser/scansup.c
+++ b/src/backend/parser/scansup.c
@@ -19,6 +19,7 @@
 
 #include "common/pg_wchar.h"
 #include "parser/scansup.h"
+#include "utils/mbutils.h"
 
 /* ----------------
  *		scanstr
diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c
index 7913fa67f0..041bf18365 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -63,6 +63,7 @@
 #include "storage/sinvaladt.h"
 #include "utils/ascii.h"
 #include "utils/guc.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 #include "utils/ps_status.h"
 #include "utils/rel.h"
diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index c8f8dd178c..535c551629 100644
--- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
+++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
@@ -29,6 +29,7 @@
 #include "pqexpbuffer.h"
 #include "replication/walreceiver.h"
 #include "utils/builtins.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 #include "utils/pg_lsn.h"
 #include "utils/tuplestore.h"
diff --git a/src/backend/replication/logical/logicalfuncs.c b/src/backend/replication/logical/logicalfuncs.c
index 9bd38a936e..0a89a353bc 100644
--- a/src/backend/replication/logical/logicalfuncs.c
+++ b/src/backend/replication/logical/logicalfuncs.c
@@ -35,6 +35,7 @@
 #include "utils/builtins.h"
 #include "utils/inval.h"
 #include "utils/lsyscache.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 #include "utils/pg_lsn.h"
 #include "utils/regproc.h"
diff --git a/src/backend/utils/adt/ascii.c b/src/backend/utils/adt/ascii.c
index 7ac3536705..b692aed409 100644
--- a/src/backend/utils/adt/ascii.c
+++ b/src/backend/utils/adt/ascii.c
@@ -14,6 +14,7 @@
 #include "common/pg_wchar.h"
 #include "utils/ascii.h"
 #include "utils/builtins.h"
+#include "utils/mbutils.h"
 
 static void pg_to_ascii(unsigned char *src, unsigned char *src_end,
 						unsigned char *dest, int enc);
diff --git a/src/backend/utils/adt/format_type.c b/src/backend/utils/adt/format_type.c
index edbded16d5..1e8f1a2189 100644
--- a/src/backend/utils/adt/format_type.c
+++ b/src/backend/utils/adt/format_type.c
@@ -23,6 +23,7 @@
 #include "common/pg_wchar.h"
 #include "utils/builtins.h"
 #include "utils/lsyscache.h"
+#include "utils/mbutils.h"
 #include "utils/numeric.h"
 #include "utils/syscache.h"
 
diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c
index ff026f410c..cadee997a1 100644
--- a/src/backend/utils/adt/formatting.c
+++ b/src/backend/utils/adt/formatting.c
@@ -94,6 +94,7 @@
 #include "utils/float.h"
 #include "utils/formatting.h"
 #include "utils/int8.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 #include "utils/numeric.h"
 #include "utils/pg_locale.h"
diff --git a/src/backend/utils/adt/genfile.c b/src/backend/utils/adt/genfile.c
index 3da50444ab..338cf7437b 100644
--- a/src/backend/utils/adt/genfile.c
+++ b/src/backend/utils/adt/genfile.c
@@ -31,6 +31,7 @@
 #include "postmaster/syslogger.h"
 #include "storage/fd.h"
 #include "utils/builtins.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 #include "utils/syscache.h"
 #include "utils/timestamp.h"
diff --git a/src/backend/utils/adt/json.c b/src/backend/utils/adt/json.c
index f6cd2b9911..743dafea4a 100644
--- a/src/backend/utils/adt/json.c
+++ b/src/backend/utils/adt/json.c
@@ -25,6 +25,7 @@
 #include "utils/json.h"
 #include "utils/jsonfuncs.h"
 #include "utils/lsyscache.h"
+#include "utils/mbutils.h"
 #include "utils/typcache.h"
 
 typedef enum					/* type categories for datum_to_json */
diff --git a/src/backend/utils/adt/jsonpath_scan.l b/src/backend/utils/adt/jsonpath_scan.l
index bde5539aed..d522b18097 100644
--- a/src/backend/utils/adt/jsonpath_scan.l
+++ b/src/backend/utils/adt/jsonpath_scan.l
@@ -19,6 +19,7 @@
 
 #include "common/pg_wchar.h"
 #include "nodes/pg_list.h"
+#include "utils/mbutils.h"
 
 static JsonPathString scanstring;
 
diff --git a/src/backend/utils/adt/like.c b/src/backend/utils/adt/like.c
index 1314fce1b4..ef80cdbf15 100644
--- a/src/backend/utils/adt/like.c
+++ b/src/backend/utils/adt/like.c
@@ -23,6 +23,7 @@
 #include "common/pg_wchar.h"
 #include "miscadmin.h"
 #include "utils/builtins.h"
+#include "utils/mbutils.h"
 #include "utils/pg_locale.h"
 
 
diff --git a/src/backend/utils/adt/like_support.c b/src/backend/utils/adt/like_support.c
index 7d20abc9be..837b7dc153 100644
--- a/src/backend/utils/adt/like_support.c
+++ b/src/backend/utils/adt/like_support.c
@@ -50,6 +50,7 @@
 #include "utils/builtins.h"
 #include "utils/datum.h"
 #include "utils/lsyscache.h"
+#include "utils/mbutils.h"
 #include "utils/pg_locale.h"
 #include "utils/selfuncs.h"
 #include "utils/varlena.h"
diff --git a/src/backend/utils/adt/name.c b/src/backend/utils/adt/name.c
index 9e81df701c..07811bdf2b 100644
--- a/src/backend/utils/adt/name.c
+++ b/src/backend/utils/adt/name.c
@@ -29,6 +29,7 @@
 #include "utils/array.h"
 #include "utils/builtins.h"
 #include "utils/lsyscache.h"
+#include "utils/mbutils.h"
 #include "utils/varlena.h"
 
 
diff --git a/src/backend/utils/adt/oracle_compat.c b/src/backend/utils/adt/oracle_compat.c
index 9c4131e6f2..7ff845e735 100644
--- a/src/backend/utils/adt/oracle_compat.c
+++ b/src/backend/utils/adt/oracle_compat.c
@@ -19,6 +19,7 @@
 #include "common/pg_wchar.h"
 #include "utils/builtins.h"
 #include "utils/formatting.h"
+#include "utils/mbutils.h"
 
 static text *dotrim(const char *string, int stringlen,
 					const char *set, int setlen,
diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c
index 5900443039..c7174e33ad 100644
--- a/src/backend/utils/adt/pg_locale.c
+++ b/src/backend/utils/adt/pg_locale.c
@@ -62,6 +62,7 @@
 #include "utils/formatting.h"
 #include "utils/hsearch.h"
 #include "utils/lsyscache.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 #include "utils/pg_locale.h"
 #include "utils/syscache.h"
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 5ae53968f1..71beb499a4 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -66,6 +66,7 @@
 #include "utils/guc.h"
 #include "utils/hsearch.h"
 #include "utils/lsyscache.h"
+#include "utils/mbutils.h"
 #include "utils/partcache.h"
 #include "utils/rel.h"
 #include "utils/ruleutils.h"
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index f68d87cb35..14809b4f82 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -29,6 +29,7 @@
 #include "utils/array.h"
 #include "utils/builtins.h"
 #include "utils/lsyscache.h"
+#include "utils/mbutils.h"
 #include "utils/regproc.h"
 #include "utils/rel.h"
 
diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c
index 69f6d9d8c1..7b8186243b 100644
--- a/src/backend/utils/adt/varchar.c
+++ b/src/backend/utils/adt/varchar.c
@@ -25,6 +25,7 @@
 #include "utils/builtins.h"
 #include "utils/hashutils.h"
 #include "utils/lsyscache.h"
+#include "utils/mbutils.h"
 #include "utils/pg_locale.h"
 #include "utils/varlena.h"
 
diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c
index 74c8a268f2..3545f1f6fa 100644
--- a/src/backend/utils/adt/xml.c
+++ b/src/backend/utils/adt/xml.c
@@ -87,6 +87,7 @@
 #include "utils/date.h"
 #include "utils/datetime.h"
 #include "utils/lsyscache.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 #include "utils/rel.h"
 #include "utils/syscache.h"
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index 61ad9d8f89..c9aa4f207d 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -78,6 +78,7 @@
 #include "storage/proc.h"
 #include "tcop/tcopprot.h"
 #include "utils/guc.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 #include "utils/ps_status.h"
 
diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c
index 1a9bb9b98d..44e0be3817 100644
--- a/src/backend/utils/init/postinit.c
+++ b/src/backend/utils/init/postinit.c
@@ -56,6 +56,7 @@
 #include "utils/acl.h"
 #include "utils/fmgroids.h"
 #include "utils/guc.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 #include "utils/pg_locale.h"
 #include "utils/portal.h"
diff --git a/src/backend/utils/mb/conv.c b/src/backend/utils/mb/conv.c
index 4b0fc23285..3b68dd4821 100644
--- a/src/backend/utils/mb/conv.c
+++ b/src/backend/utils/mb/conv.c
@@ -12,6 +12,7 @@
  */
 #include "postgres.h"
 #include "common/pg_wchar.h"
+#include "utils/mbutils.h"
 
 
 /*
diff --git a/src/backend/utils/mb/conversion_procs/cyrillic_and_mic/cyrillic_and_mic.c b/src/backend/utils/mb/conversion_procs/cyrillic_and_mic/cyrillic_and_mic.c
index 5500dd4fed..35204ae530 100644
--- a/src/backend/utils/mb/conversion_procs/cyrillic_and_mic/cyrillic_and_mic.c
+++ b/src/backend/utils/mb/conversion_procs/cyrillic_and_mic/cyrillic_and_mic.c
@@ -14,6 +14,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 
 PG_MODULE_MAGIC;
 
diff --git a/src/backend/utils/mb/conversion_procs/euc2004_sjis2004/euc2004_sjis2004.c b/src/backend/utils/mb/conversion_procs/euc2004_sjis2004/euc2004_sjis2004.c
index 26051530b7..5b9ca9d3c0 100644
--- a/src/backend/utils/mb/conversion_procs/euc2004_sjis2004/euc2004_sjis2004.c
+++ b/src/backend/utils/mb/conversion_procs/euc2004_sjis2004/euc2004_sjis2004.c
@@ -13,6 +13,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 
 PG_MODULE_MAGIC;
 
diff --git a/src/backend/utils/mb/conversion_procs/euc_cn_and_mic/euc_cn_and_mic.c b/src/backend/utils/mb/conversion_procs/euc_cn_and_mic/euc_cn_and_mic.c
index d1cb25ea92..4703c4ef0a 100644
--- a/src/backend/utils/mb/conversion_procs/euc_cn_and_mic/euc_cn_and_mic.c
+++ b/src/backend/utils/mb/conversion_procs/euc_cn_and_mic/euc_cn_and_mic.c
@@ -14,6 +14,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 
 PG_MODULE_MAGIC;
 
diff --git a/src/backend/utils/mb/conversion_procs/euc_jp_and_sjis/euc_jp_and_sjis.c b/src/backend/utils/mb/conversion_procs/euc_jp_and_sjis/euc_jp_and_sjis.c
index ffc3896c9d..929b5d78b0 100644
--- a/src/backend/utils/mb/conversion_procs/euc_jp_and_sjis/euc_jp_and_sjis.c
+++ b/src/backend/utils/mb/conversion_procs/euc_jp_and_sjis/euc_jp_and_sjis.c
@@ -14,6 +14,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 
 /*
  * SJIS alternative code.
diff --git a/src/backend/utils/mb/conversion_procs/euc_kr_and_mic/euc_kr_and_mic.c b/src/backend/utils/mb/conversion_procs/euc_kr_and_mic/euc_kr_and_mic.c
index 2183cf183a..bb42246c15 100644
--- a/src/backend/utils/mb/conversion_procs/euc_kr_and_mic/euc_kr_and_mic.c
+++ b/src/backend/utils/mb/conversion_procs/euc_kr_and_mic/euc_kr_and_mic.c
@@ -14,6 +14,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 
 PG_MODULE_MAGIC;
 
diff --git a/src/backend/utils/mb/conversion_procs/euc_tw_and_big5/big5.c b/src/backend/utils/mb/conversion_procs/euc_tw_and_big5/big5.c
index 7601bd65f2..33079708bf 100644
--- a/src/backend/utils/mb/conversion_procs/euc_tw_and_big5/big5.c
+++ b/src/backend/utils/mb/conversion_procs/euc_tw_and_big5/big5.c
@@ -14,6 +14,7 @@
 #include "postgres_fe.h"
 
 #include "common/pg_wchar.h"
+#include "utils/mbutils.h"
 
 typedef struct
 {
diff --git a/src/backend/utils/mb/conversion_procs/euc_tw_and_big5/euc_tw_and_big5.c b/src/backend/utils/mb/conversion_procs/euc_tw_and_big5/euc_tw_and_big5.c
index e209e9a545..ef887961f2 100644
--- a/src/backend/utils/mb/conversion_procs/euc_tw_and_big5/euc_tw_and_big5.c
+++ b/src/backend/utils/mb/conversion_procs/euc_tw_and_big5/euc_tw_and_big5.c
@@ -14,6 +14,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 
 #define ENCODING_GROWTH_RATE 4
 
diff --git a/src/backend/utils/mb/conversion_procs/latin2_and_win1250/latin2_and_win1250.c b/src/backend/utils/mb/conversion_procs/latin2_and_win1250/latin2_and_win1250.c
index 72ab071d87..5e38c186e0 100644
--- a/src/backend/utils/mb/conversion_procs/latin2_and_win1250/latin2_and_win1250.c
+++ b/src/backend/utils/mb/conversion_procs/latin2_and_win1250/latin2_and_win1250.c
@@ -14,6 +14,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 
 PG_MODULE_MAGIC;
 
diff --git a/src/backend/utils/mb/conversion_procs/latin_and_mic/latin_and_mic.c b/src/backend/utils/mb/conversion_procs/latin_and_mic/latin_and_mic.c
index 5868b66715..20898d9040 100644
--- a/src/backend/utils/mb/conversion_procs/latin_and_mic/latin_and_mic.c
+++ b/src/backend/utils/mb/conversion_procs/latin_and_mic/latin_and_mic.c
@@ -14,6 +14,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 
 PG_MODULE_MAGIC;
 
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_big5/utf8_and_big5.c b/src/backend/utils/mb/conversion_procs/utf8_and_big5/utf8_and_big5.c
index 542e055ea2..13b9e2e881 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_big5/utf8_and_big5.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_big5/utf8_and_big5.c
@@ -14,6 +14,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 #include "../../Unicode/big5_to_utf8.map"
 #include "../../Unicode/utf8_to_big5.map"
 
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_cyrillic/utf8_and_cyrillic.c b/src/backend/utils/mb/conversion_procs/utf8_and_cyrillic/utf8_and_cyrillic.c
index d145da093f..d738275c9d 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_cyrillic/utf8_and_cyrillic.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_cyrillic/utf8_and_cyrillic.c
@@ -14,6 +14,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 #include "../../Unicode/utf8_to_koi8r.map"
 #include "../../Unicode/koi8r_to_utf8.map"
 #include "../../Unicode/utf8_to_koi8u.map"
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_euc2004/utf8_and_euc2004.c b/src/backend/utils/mb/conversion_procs/utf8_and_euc2004/utf8_and_euc2004.c
index 8d56d2343c..20b2f9ab1f 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_euc2004/utf8_and_euc2004.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_euc2004/utf8_and_euc2004.c
@@ -14,6 +14,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 #include "../../Unicode/euc_jis_2004_to_utf8.map"
 #include "../../Unicode/utf8_to_euc_jis_2004.map"
 
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_euc_cn/utf8_and_euc_cn.c b/src/backend/utils/mb/conversion_procs/utf8_and_euc_cn/utf8_and_euc_cn.c
index 41f2f9a5ca..3e98899ec5 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_euc_cn/utf8_and_euc_cn.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_euc_cn/utf8_and_euc_cn.c
@@ -14,6 +14,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 #include "../../Unicode/euc_cn_to_utf8.map"
 #include "../../Unicode/utf8_to_euc_cn.map"
 
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_euc_jp/utf8_and_euc_jp.c b/src/backend/utils/mb/conversion_procs/utf8_and_euc_jp/utf8_and_euc_jp.c
index 5a9f8ecfd4..84c6f5ed69 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_euc_jp/utf8_and_euc_jp.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_euc_jp/utf8_and_euc_jp.c
@@ -14,6 +14,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 #include "../../Unicode/euc_jp_to_utf8.map"
 #include "../../Unicode/utf8_to_euc_jp.map"
 
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_euc_kr/utf8_and_euc_kr.c b/src/backend/utils/mb/conversion_procs/utf8_and_euc_kr/utf8_and_euc_kr.c
index d09f9dc78a..4462554199 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_euc_kr/utf8_and_euc_kr.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_euc_kr/utf8_and_euc_kr.c
@@ -14,6 +14,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 #include "../../Unicode/euc_kr_to_utf8.map"
 #include "../../Unicode/utf8_to_euc_kr.map"
 
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_euc_tw/utf8_and_euc_tw.c b/src/backend/utils/mb/conversion_procs/utf8_and_euc_tw/utf8_and_euc_tw.c
index 8e0250e603..b3b7ff87e4 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_euc_tw/utf8_and_euc_tw.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_euc_tw/utf8_and_euc_tw.c
@@ -14,6 +14,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 #include "../../Unicode/euc_tw_to_utf8.map"
 #include "../../Unicode/utf8_to_euc_tw.map"
 
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_gb18030/utf8_and_gb18030.c b/src/backend/utils/mb/conversion_procs/utf8_and_gb18030/utf8_and_gb18030.c
index 6b5b326c6f..49062c5b0d 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_gb18030/utf8_and_gb18030.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_gb18030/utf8_and_gb18030.c
@@ -14,6 +14,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 #include "../../Unicode/gb18030_to_utf8.map"
 #include "../../Unicode/utf8_to_gb18030.map"
 
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_gbk/utf8_and_gbk.c b/src/backend/utils/mb/conversion_procs/utf8_and_gbk/utf8_and_gbk.c
index 4e2246da7a..8e60cc1fa6 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_gbk/utf8_and_gbk.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_gbk/utf8_and_gbk.c
@@ -14,6 +14,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 #include "../../Unicode/gbk_to_utf8.map"
 #include "../../Unicode/utf8_to_gbk.map"
 
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_iso8859/utf8_and_iso8859.c b/src/backend/utils/mb/conversion_procs/utf8_and_iso8859/utf8_and_iso8859.c
index 8051eefed9..673e079060 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_iso8859/utf8_and_iso8859.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_iso8859/utf8_and_iso8859.c
@@ -14,6 +14,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 #include "../../Unicode/iso8859_10_to_utf8.map"
 #include "../../Unicode/iso8859_13_to_utf8.map"
 #include "../../Unicode/iso8859_14_to_utf8.map"
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_iso8859_1/utf8_and_iso8859_1.c b/src/backend/utils/mb/conversion_procs/utf8_and_iso8859_1/utf8_and_iso8859_1.c
index b9e127cc5e..62d5d5feee 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_iso8859_1/utf8_and_iso8859_1.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_iso8859_1/utf8_and_iso8859_1.c
@@ -14,6 +14,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 
 PG_MODULE_MAGIC;
 
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_johab/utf8_and_johab.c b/src/backend/utils/mb/conversion_procs/utf8_and_johab/utf8_and_johab.c
index bece993e1d..ef2879150b 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_johab/utf8_and_johab.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_johab/utf8_and_johab.c
@@ -14,6 +14,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 #include "../../Unicode/johab_to_utf8.map"
 #include "../../Unicode/utf8_to_johab.map"
 
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_sjis/utf8_and_sjis.c b/src/backend/utils/mb/conversion_procs/utf8_and_sjis/utf8_and_sjis.c
index 8307d4ceb4..428bdc5c27 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_sjis/utf8_and_sjis.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_sjis/utf8_and_sjis.c
@@ -14,6 +14,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 #include "../../Unicode/sjis_to_utf8.map"
 #include "../../Unicode/utf8_to_sjis.map"
 
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_sjis2004/utf8_and_sjis2004.c b/src/backend/utils/mb/conversion_procs/utf8_and_sjis2004/utf8_and_sjis2004.c
index c913ba0866..ed97196d37 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_sjis2004/utf8_and_sjis2004.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_sjis2004/utf8_and_sjis2004.c
@@ -14,6 +14,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 #include "../../Unicode/shift_jis_2004_to_utf8.map"
 #include "../../Unicode/utf8_to_shift_jis_2004.map"
 
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_uhc/utf8_and_uhc.c b/src/backend/utils/mb/conversion_procs/utf8_and_uhc/utf8_and_uhc.c
index 87ae61ee85..d52107ed7f 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_uhc/utf8_and_uhc.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_uhc/utf8_and_uhc.c
@@ -14,6 +14,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 #include "../../Unicode/uhc_to_utf8.map"
 #include "../../Unicode/utf8_to_uhc.map"
 
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_win/utf8_and_win.c b/src/backend/utils/mb/conversion_procs/utf8_and_win/utf8_and_win.c
index 200fd7f23a..a29c45e746 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_win/utf8_and_win.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_win/utf8_and_win.c
@@ -14,6 +14,7 @@
 #include "postgres.h"
 #include "common/pg_wchar.h"
 #include "fmgr.h"
+#include "utils/mbutils.h"
 #include "../../Unicode/utf8_to_win1250.map"
 #include "../../Unicode/utf8_to_win1251.map"
 #include "../../Unicode/utf8_to_win1252.map"
diff --git a/src/backend/utils/mb/mbutils.c b/src/backend/utils/mb/mbutils.c
index 4cec7ec73a..bd3c5b9442 100644
--- a/src/backend/utils/mb/mbutils.c
+++ b/src/backend/utils/mb/mbutils.c
@@ -38,6 +38,7 @@
 #include "catalog/namespace.h"
 #include "common/pg_wchar.h"
 #include "utils/builtins.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 #include "utils/syscache.h"
 
diff --git a/src/backend/utils/mb/stringinfo_mb.c b/src/backend/utils/mb/stringinfo_mb.c
index e7462a09b5..26cf8c715f 100644
--- a/src/backend/utils/mb/stringinfo_mb.c
+++ b/src/backend/utils/mb/stringinfo_mb.c
@@ -21,6 +21,7 @@
 
 #include "common/pg_wchar.h"
 #include "mb/stringinfo_mb.h"
+#include "utils/mbutils.h"
 
 
 /*
diff --git a/src/backend/utils/misc/guc-file.l b/src/backend/utils/misc/guc-file.l
index 6c9e60ef64..0738360aa7 100644
--- a/src/backend/utils/misc/guc-file.l
+++ b/src/backend/utils/misc/guc-file.l
@@ -18,6 +18,7 @@
 #include "miscadmin.h"
 #include "storage/fd.h"
 #include "utils/guc.h"
+#include "utils/mbutils.h"
 
 
 /*
diff --git a/src/backend/utils/mmgr/mcxt.c b/src/backend/utils/mmgr/mcxt.c
index 71701de091..b47439c52c 100644
--- a/src/backend/utils/mmgr/mcxt.c
+++ b/src/backend/utils/mmgr/mcxt.c
@@ -23,6 +23,7 @@
 
 #include "common/pg_wchar.h"
 #include "miscadmin.h"
+#include "utils/mbutils.h"
 #include "utils/memdebug.h"
 #include "utils/memutils.h"
 
diff --git a/src/common/wchar.c b/src/common/wchar.c
index 53006115d2..c9c07a14ba 100644
--- a/src/common/wchar.c
+++ b/src/common/wchar.c
@@ -13,6 +13,7 @@
 #include "c.h"
 
 #include "common/pg_wchar.h"
+#include "utils/mbutils.h"
 
 
 /*
diff --git a/src/include/common/pg_wchar.h b/src/include/common/pg_wchar.h
index a1b3a27b9f..7b98453b7b 100644
--- a/src/include/common/pg_wchar.h
+++ b/src/include/common/pg_wchar.h
@@ -488,20 +488,6 @@ typedef struct
  */
 typedef uint32 (*utf_local_conversion_func) (uint32 code);
 
-/*
- * Support macro for encoding conversion functions to validate their
- * arguments.  (This could be made more compact if we included fmgr.h
- * here, but we don't want to do that because this header file is also
- * used by frontends.)
- */
-#define CHECK_ENCODING_CONVERSION_ARGS(srcencoding,destencoding) \
-	check_encoding_conversion_args(PG_GETARG_INT32(0), \
-								   PG_GETARG_INT32(1), \
-								   PG_GETARG_INT32(4), \
-								   (srcencoding), \
-								   (destencoding))
-
-
 /*
  * Some handy functions for Unicode-specific tests.
  */
@@ -552,104 +538,9 @@ extern bool pg_utf8_islegal(const unsigned char *source, int length);
 extern int	pg_utf_mblen(const unsigned char *s);
 extern int	pg_mule_mblen(const unsigned char *s);
 
-/*
- * The remaining functions are backend-only.
- */
-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,
-										  const char *from, pg_wchar *to, int len);
-extern int	pg_wchar2mb(const pg_wchar *from, char *to);
-extern int	pg_wchar2mb_with_len(const pg_wchar *from, char *to, int len);
-extern int	pg_encoding_wchar2mb_with_len(int encoding,
-										  const pg_wchar *from, char *to, int len);
 extern int	pg_char_and_wchar_strcmp(const char *s1, const pg_wchar *s2);
 extern int	pg_wchar_strncmp(const pg_wchar *s1, const pg_wchar *s2, size_t n);
 extern int	pg_char_and_wchar_strncmp(const char *s1, const pg_wchar *s2, size_t n);
 extern size_t pg_wchar_strlen(const pg_wchar *wstr);
-extern int	pg_mblen(const char *mbstr);
-extern int	pg_dsplen(const char *mbstr);
-extern int	pg_mbstrlen(const char *mbstr);
-extern int	pg_mbstrlen_with_len(const char *mbstr, int len);
-extern int	pg_mbcliplen(const char *mbstr, int len, int limit);
-extern int	pg_encoding_mbcliplen(int encoding, const char *mbstr,
-								  int len, int limit);
-extern int	pg_mbcharcliplen(const char *mbstr, int len, int limit);
-extern int	pg_database_encoding_max_length(void);
-extern mbcharacter_incrementer pg_database_encoding_character_incrementer(void);
-
-extern int	PrepareClientEncoding(int encoding);
-extern int	SetClientEncoding(int encoding);
-extern void InitializeClientEncoding(void);
-extern int	pg_get_client_encoding(void);
-extern const char *pg_get_client_encoding_name(void);
-
-extern void SetDatabaseEncoding(int encoding);
-extern int	GetDatabaseEncoding(void);
-extern const char *GetDatabaseEncodingName(void);
-extern void SetMessageEncoding(int encoding);
-extern int	GetMessageEncoding(void);
-
-#ifdef ENABLE_NLS
-extern int	pg_bind_textdomain_codeset(const char *domainname);
-#endif
-
-extern unsigned char *pg_do_encoding_conversion(unsigned char *src, int len,
-												int src_encoding,
-												int dest_encoding);
-
-extern char *pg_client_to_server(const char *s, int len);
-extern char *pg_server_to_client(const char *s, int len);
-extern char *pg_any_to_server(const char *s, int len, int encoding);
-extern char *pg_server_to_any(const char *s, int len, int encoding);
-
-extern unsigned short BIG5toCNS(unsigned short big5, unsigned char *lc);
-extern unsigned short CNStoBIG5(unsigned short cns, unsigned char lc);
-
-extern void UtfToLocal(const unsigned char *utf, int len,
-					   unsigned char *iso,
-					   const pg_mb_radix_tree *map,
-					   const pg_utf_to_local_combined *cmap, int cmapsize,
-					   utf_local_conversion_func conv_func,
-					   int encoding);
-extern void LocalToUtf(const unsigned char *iso, int len,
-					   unsigned char *utf,
-					   const pg_mb_radix_tree *map,
-					   const pg_local_to_utf_combined *cmap, int cmapsize,
-					   utf_local_conversion_func conv_func,
-					   int encoding);
-
-extern bool pg_verifymbstr(const char *mbstr, int len, bool noError);
-extern bool pg_verify_mbstr(int encoding, const char *mbstr, int len,
-							bool noError);
-extern int	pg_verify_mbstr_len(int encoding, const char *mbstr, int len,
-								bool noError);
-
-extern void check_encoding_conversion_args(int src_encoding,
-										   int dest_encoding,
-										   int len,
-										   int expected_src_encoding,
-										   int expected_dest_encoding);
-
-extern void report_invalid_encoding(int encoding, const char *mbstr, int len) pg_attribute_noreturn();
-extern void report_untranslatable_char(int src_encoding, int dest_encoding,
-									   const char *mbstr, int len) pg_attribute_noreturn();
-
-extern void local2local(const unsigned char *l, unsigned char *p, int len,
-						int src_encoding, int dest_encoding, const unsigned char *tab);
-extern void latin2mic(const unsigned char *l, unsigned char *p, int len,
-					  int lc, int encoding);
-extern void mic2latin(const unsigned char *mic, unsigned char *p, int len,
-					  int lc, int encoding);
-extern void latin2mic_with_table(const unsigned char *l, unsigned char *p,
-								 int len, int lc, int encoding,
-								 const unsigned char *tab);
-extern void mic2latin_with_table(const unsigned char *mic, unsigned char *p,
-								 int len, int lc, int encoding,
-								 const unsigned char *tab);
-
-#ifdef WIN32
-extern WCHAR *pgwin32_message_to_UTF16(const char *str, int len, int *utf16len);
-#endif
 
 #endif							/* PG_WCHAR_H */
diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h
index daeba6d5c9..9672e654bf 100644
--- a/src/include/libpq/pqformat.h
+++ b/src/include/libpq/pqformat.h
@@ -16,6 +16,7 @@
 #include "common/pg_wchar.h"
 #include "lib/stringinfo.h"
 #include "port/pg_bswap.h"
+#include "utils/mbutils.h"
 
 extern void pq_beginmessage(StringInfo buf, char msgtype);
 extern void pq_beginmessage_reuse(StringInfo buf, char msgtype);
diff --git a/src/include/regex/regcustom.h b/src/include/regex/regcustom.h
index 2506ac8268..ce0700c7b1 100644
--- a/src/include/regex/regcustom.h
+++ b/src/include/regex/regcustom.h
@@ -56,6 +56,8 @@
 
 #include "miscadmin.h"			/* needed by rcancelrequested/rstacktoodeep */
 
+#include "utils/mbutils.h"
+
 
 /* overrides for regguts.h definitions, if any */
 #define FUNCPTR(name, args) (*name) args
diff --git a/src/include/regex/regex.h b/src/include/regex/regex.h
index 739cc7d14b..3c53687c1b 100644
--- a/src/include/regex/regex.h
+++ b/src/include/regex/regex.h
@@ -36,6 +36,7 @@
  * Add your own defines, if needed, here.
  */
 #include "common/pg_wchar.h"
+#include "utils/mbutils.h"
 
 /*
  * interface types etc.
diff --git a/src/include/tsearch/ts_locale.h b/src/include/tsearch/ts_locale.h
index f77eb23c8a..00dcef3f80 100644
--- a/src/include/tsearch/ts_locale.h
+++ b/src/include/tsearch/ts_locale.h
@@ -16,6 +16,7 @@
 #include <limits.h>
 
 #include "common/pg_wchar.h"
+#include "utils/mbutils.h"
 #include "utils/pg_locale.h"
 
 /*
diff --git a/src/include/utils/mbutils.h b/src/include/utils/mbutils.h
new file mode 100644
index 0000000000..b8da6ce0a9
--- /dev/null
+++ b/src/include/utils/mbutils.h
@@ -0,0 +1,124 @@
+/*-------------------------------------------------------------------------
+ *
+ * mbutils.h
+ *	  backend-only multibyte-character support
+ *
+ * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/include/utils/mbutils.h
+ *
+ *	NOTES
+ *		TODO: write some notes
+ *
+ *-------------------------------------------------------------------------
+ */
+#ifndef MBUTILS_H
+#define MBUTILS_H
+
+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,
+										  const char *from, pg_wchar *to, int len);
+extern int	pg_wchar2mb(const pg_wchar *from, char *to);
+extern int	pg_wchar2mb_with_len(const pg_wchar *from, char *to, int len);
+extern int	pg_encoding_wchar2mb_with_len(int encoding,
+										  const pg_wchar *from, char *to, int len);
+extern int	pg_mblen(const char *mbstr);
+extern int	pg_dsplen(const char *mbstr);
+extern int	pg_mbstrlen(const char *mbstr);
+extern int	pg_mbstrlen_with_len(const char *mbstr, int len);
+extern int	pg_mbcliplen(const char *mbstr, int len, int limit);
+extern int	pg_encoding_mbcliplen(int encoding, const char *mbstr,
+								  int len, int limit);
+extern int	pg_mbcharcliplen(const char *mbstr, int len, int limit);
+extern int	pg_database_encoding_max_length(void);
+extern mbcharacter_incrementer pg_database_encoding_character_incrementer(void);
+
+extern int	PrepareClientEncoding(int encoding);
+extern int	SetClientEncoding(int encoding);
+extern void InitializeClientEncoding(void);
+extern int	pg_get_client_encoding(void);
+extern const char *pg_get_client_encoding_name(void);
+
+extern void SetDatabaseEncoding(int encoding);
+extern int	GetDatabaseEncoding(void);
+extern const char *GetDatabaseEncodingName(void);
+extern void SetMessageEncoding(int encoding);
+extern int	GetMessageEncoding(void);
+
+#ifdef ENABLE_NLS
+extern int	pg_bind_textdomain_codeset(const char *domainname);
+#endif
+
+extern unsigned char *pg_do_encoding_conversion(unsigned char *src, int len,
+												int src_encoding,
+												int dest_encoding);
+
+extern char *pg_client_to_server(const char *s, int len);
+extern char *pg_server_to_client(const char *s, int len);
+extern char *pg_any_to_server(const char *s, int len, int encoding);
+extern char *pg_server_to_any(const char *s, int len, int encoding);
+
+extern unsigned short BIG5toCNS(unsigned short big5, unsigned char *lc);
+extern unsigned short CNStoBIG5(unsigned short cns, unsigned char lc);
+
+extern void UtfToLocal(const unsigned char *utf, int len,
+					   unsigned char *iso,
+					   const pg_mb_radix_tree *map,
+					   const pg_utf_to_local_combined *cmap, int cmapsize,
+					   utf_local_conversion_func conv_func,
+					   int encoding);
+extern void LocalToUtf(const unsigned char *iso, int len,
+					   unsigned char *utf,
+					   const pg_mb_radix_tree *map,
+					   const pg_local_to_utf_combined *cmap, int cmapsize,
+					   utf_local_conversion_func conv_func,
+					   int encoding);
+
+extern bool pg_verifymbstr(const char *mbstr, int len, bool noError);
+extern bool pg_verify_mbstr(int encoding, const char *mbstr, int len,
+							bool noError);
+extern int	pg_verify_mbstr_len(int encoding, const char *mbstr, int len,
+								bool noError);
+
+extern void check_encoding_conversion_args(int src_encoding,
+										   int dest_encoding,
+										   int len,
+										   int expected_src_encoding,
+										   int expected_dest_encoding);
+
+extern void report_invalid_encoding(int encoding, const char *mbstr, int len) pg_attribute_noreturn();
+extern void report_untranslatable_char(int src_encoding, int dest_encoding,
+									   const char *mbstr, int len) pg_attribute_noreturn();
+
+extern void local2local(const unsigned char *l, unsigned char *p, int len,
+						int src_encoding, int dest_encoding, const unsigned char *tab);
+extern void latin2mic(const unsigned char *l, unsigned char *p, int len,
+					  int lc, int encoding);
+extern void mic2latin(const unsigned char *mic, unsigned char *p, int len,
+					  int lc, int encoding);
+extern void latin2mic_with_table(const unsigned char *l, unsigned char *p,
+								 int len, int lc, int encoding,
+								 const unsigned char *tab);
+extern void mic2latin_with_table(const unsigned char *mic, unsigned char *p,
+								 int len, int lc, int encoding,
+								 const unsigned char *tab);
+
+#ifdef WIN32
+extern WCHAR *pgwin32_message_to_UTF16(const char *str, int len, int *utf16len);
+#endif
+
+/*
+ * Support macro for encoding conversion functions to validate their
+ * arguments.
+ */
+#define CHECK_ENCODING_CONVERSION_ARGS(srcencoding,destencoding) \
+	check_encoding_conversion_args(PG_GETARG_INT32(0), \
+								   PG_GETARG_INT32(1), \
+								   PG_GETARG_INT32(4), \
+								   (srcencoding), \
+								   (destencoding))
+
+
+#endif							/* MBUTILS_H */
diff --git a/src/interfaces/libpq/fe-protocol3.c b/src/interfaces/libpq/fe-protocol3.c
index 799a1538da..9ca04d9781 100644
--- a/src/interfaces/libpq/fe-protocol3.c
+++ b/src/interfaces/libpq/fe-protocol3.c
@@ -30,6 +30,7 @@
 #include "libpq-fe.h"
 #include "libpq-int.h"
 #include "port/pg_bswap.h"
+#include "utils/mbutils.h"
 
 /*
  * This macro lists the backend message types that could be "long" (more
diff --git a/src/pl/plperl/plperl.c b/src/pl/plperl/plperl.c
index ac7144e99f..4293f58360 100644
--- a/src/pl/plperl/plperl.c
+++ b/src/pl/plperl/plperl.c
@@ -34,6 +34,7 @@
 #include "utils/guc.h"
 #include "utils/hsearch.h"
 #include "utils/lsyscache.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 #include "utils/rel.h"
 #include "utils/syscache.h"
diff --git a/src/pl/plperl/plperl_helpers.h b/src/pl/plperl/plperl_helpers.h
index cec942c280..2359930805 100644
--- a/src/pl/plperl/plperl_helpers.h
+++ b/src/pl/plperl/plperl_helpers.h
@@ -2,8 +2,8 @@
 #define PL_PERL_HELPERS_H
 
 #include "common/pg_wchar.h"
-
 #include "plperl.h"
+#include "utils/mbutils.h"
 
 
 /*
diff --git a/src/pl/plpgsql/src/pl_scanner.c b/src/pl/plpgsql/src/pl_scanner.c
index a5bb7c474c..0b758db69c 100644
--- a/src/pl/plpgsql/src/pl_scanner.c
+++ b/src/pl/plpgsql/src/pl_scanner.c
@@ -20,6 +20,7 @@
 
 #include "plpgsql.h"
 #include "pl_gram.h"			/* must be after parser/scanner.h */
+#include "utils/mbutils.h"
 
 
 /* Klugy flag to tell scanner how to look up identifiers */
diff --git a/src/pl/plpython/plpy_cursorobject.c b/src/pl/plpython/plpy_cursorobject.c
index 01ad7cf3bc..5dd1079002 100644
--- a/src/pl/plpython/plpy_cursorobject.c
+++ b/src/pl/plpython/plpy_cursorobject.c
@@ -19,6 +19,7 @@
 #include "plpy_resultobject.h"
 #include "plpy_spi.h"
 #include "plpython.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 
 static PyObject *PLy_cursor_query(const char *query);
diff --git a/src/pl/plpython/plpy_plpymodule.c b/src/pl/plpython/plpy_plpymodule.c
index b079afc000..d691def5bb 100644
--- a/src/pl/plpython/plpy_plpymodule.c
+++ b/src/pl/plpython/plpy_plpymodule.c
@@ -18,6 +18,7 @@
 #include "plpy_subxactobject.h"
 #include "plpython.h"
 #include "utils/builtins.h"
+#include "utils/mbutils.h"
 #include "utils/snapmgr.h"
 
 HTAB	   *PLy_spi_exceptions = NULL;
diff --git a/src/pl/plpython/plpy_spi.c b/src/pl/plpython/plpy_spi.c
index 8b7cfbf18c..a6cf592974 100644
--- a/src/pl/plpython/plpy_spi.c
+++ b/src/pl/plpython/plpy_spi.c
@@ -22,6 +22,7 @@
 #include "plpy_resultobject.h"
 #include "plpy_spi.h"
 #include "plpython.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 #include "utils/syscache.h"
 
diff --git a/src/pl/plpython/plpy_typeio.c b/src/pl/plpython/plpy_typeio.c
index baf45526c6..e4fd090228 100644
--- a/src/pl/plpython/plpy_typeio.c
+++ b/src/pl/plpython/plpy_typeio.c
@@ -19,6 +19,7 @@
 #include "utils/builtins.h"
 #include "utils/fmgroids.h"
 #include "utils/lsyscache.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 
 /* conversion from Datums to Python objects */
diff --git a/src/pl/plpython/plpy_util.c b/src/pl/plpython/plpy_util.c
index 873afcfbb8..b9ad3f7000 100644
--- a/src/pl/plpython/plpy_util.c
+++ b/src/pl/plpython/plpy_util.c
@@ -10,6 +10,7 @@
 #include "plpy_elog.h"
 #include "plpy_util.h"
 #include "plpython.h"
+#include "utils/mbutils.h"
 #include "utils/memutils.h"
 
 /*
-- 
2.21.1 (Apple Git-122.3)

