Hi,

On 2017-10-03 13:58:37 -0400, Robert Haas wrote:
> On Tue, Oct 3, 2017 at 12:23 PM, Andres Freund <and...@anarazel.de> wrote:
> > Makes sense?
> 
> Yes.

Here's an updated version of this patchset. Changes:

- renamed pq_send$type_pre to pq_write*type
- renamed pq_beginmessage_pre/pq_beginmessage_keep to the _reuse suffix
- removed unaligned memory access issues by again using memcpy - gcc and
  clang both successfully optimize it away
- moved permanent buffer for SendRowDescriptionMessage to postgres.c,
  and have other callers use already pre-existing buffers.
- replace all pq_sendint with pq_sendint$width in core
- converted applicable pq_begin/endmessage in printtup.c users to use
  DR_printtup->buf.
- added comments explaining restrict usage
- expanded commit messages considerably
- Small stuff.

The naming I'd discussed a bit back and forth with Robert over IM,
thanks!

- Andres
>From 89e301384c9fbc071de19c1517ffe29371fc6f36 Mon Sep 17 00:00:00 2001
From: Andres Freund <and...@anarazel.de>
Date: Wed, 20 Sep 2017 13:01:22 -0700
Subject: [PATCH 1/6] Add configure infrastructure to detect support for C99's
 restrict.

Will be used in later commits improving performance for a few key
routines where information about aliasing allows for significantly
better code generation.

This allows to use the C99 'restrict' keyword without breaking C89, or
for that matter C++, compilers. If not supported it's defined to be
empty.

Author: Andres Freund
Discussion: https://postgr.es/m/20170914063418.sckdzgjfrsbek...@alap3.anarazel.de
---
 configure                     | 46 +++++++++++++++++++++++++++++++++++++++++++
 configure.in                  |  1 +
 src/include/pg_config.h.in    | 14 +++++++++++++
 src/include/pg_config.h.win32 |  6 ++++++
 4 files changed, 67 insertions(+)

diff --git a/configure b/configure
index b0582657bf4..ca54242d5d7 100755
--- a/configure
+++ b/configure
@@ -11545,6 +11545,52 @@ _ACEOF
     ;;
 esac
 
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for C/C++ restrict keyword" >&5
+$as_echo_n "checking for C/C++ restrict keyword... " >&6; }
+if ${ac_cv_c_restrict+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  ac_cv_c_restrict=no
+   # The order here caters to the fact that C++ does not require restrict.
+   for ac_kw in __restrict __restrict__ _Restrict restrict; do
+     cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+typedef int * int_ptr;
+	int foo (int_ptr $ac_kw ip) {
+	return ip[0];
+       }
+int
+main ()
+{
+int s[1];
+	int * $ac_kw t = s;
+	t[0] = 0;
+	return foo(t)
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  ac_cv_c_restrict=$ac_kw
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+     test "$ac_cv_c_restrict" != no && break
+   done
+
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_c_restrict" >&5
+$as_echo "$ac_cv_c_restrict" >&6; }
+
+ case $ac_cv_c_restrict in
+   restrict) ;;
+   no) $as_echo "#define restrict /**/" >>confdefs.h
+ ;;
+   *)  cat >>confdefs.h <<_ACEOF
+#define restrict $ac_cv_c_restrict
+_ACEOF
+ ;;
+ esac
+
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for printf format archetype" >&5
 $as_echo_n "checking for printf format archetype... " >&6; }
 if ${pgac_cv_printf_archetype+:} false; then :
diff --git a/configure.in b/configure.in
index 4548db0dd3c..ab990d69f4c 100644
--- a/configure.in
+++ b/configure.in
@@ -1299,6 +1299,7 @@ fi
 m4_defun([AC_PROG_CC_STDC], []) dnl We don't want that.
 AC_C_BIGENDIAN
 AC_C_INLINE
+AC_C_RESTRICT
 PGAC_PRINTF_ARCHETYPE
 AC_C_FLEXIBLE_ARRAY_MEMBER
 PGAC_C_SIGNED
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index b0298cca19c..80ee37dd622 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -923,6 +923,20 @@
    if such a type exists, and if the system does not define it. */
 #undef intptr_t
 
+/* Define to the equivalent of the C99 'restrict' keyword, or to
+   nothing if this is not supported.  Do not define if restrict is
+   supported directly.  */
+#undef restrict
+/* Work around a bug in Sun C++: it does not support _Restrict or
+   __restrict__, even though the corresponding Sun C compiler ends up with
+   "#define restrict _Restrict" or "#define restrict __restrict__" in the
+   previous line.  Perhaps some future version of Sun C++ will work with
+   restrict; if so, hopefully it defines __RESTRICT like Sun C does.  */
+#if defined __SUNPRO_CC && !defined __RESTRICT
+# define _Restrict
+# define __restrict__
+#endif
+
 /* Define to empty if the C compiler does not understand signed types. */
 #undef signed
 
diff --git a/src/include/pg_config.h.win32 b/src/include/pg_config.h.win32
index b76aad02676..b96e93328fe 100644
--- a/src/include/pg_config.h.win32
+++ b/src/include/pg_config.h.win32
@@ -681,6 +681,12 @@
 #define inline __inline
 #endif
 
+/* Define to the equivalent of the C99 'restrict' keyword, or to
+   nothing if this is not supported.  Do not define if restrict is
+   supported directly.  */
+/* works for C and C++ in msvc */
+#define restrict __restrict
+
 /* Define to empty if the C compiler does not understand signed types. */
 /* #undef signed */
 
-- 
2.14.1.536.g6867272d5b.dirty

>From a6f28c1da0a3eeae6748be39fa6f18a0c625bcbd Mon Sep 17 00:00:00 2001
From: Andres Freund <and...@anarazel.de>
Date: Mon, 9 Oct 2017 17:03:07 -0700
Subject: [PATCH 2/6] Allow to avoid NUL-byte management for stringinfos and
 use in format.c.

In a lot of the places having appendBinaryStringInfo() maintain a
trailing NUL byte wasn't actually meaningful, e.g. when appending an
integer which can contain 0 in one of its bytes.

Removing this yields some small speedup, but more importantly will be
more consistent when providing faster variants of pq_sendint etc.

Author: Andres Freund
Discussion: https://postgr.es/m/20170914063418.sckdzgjfrsbek...@alap3.anarazel.de
---
 src/backend/lib/stringinfo.c | 21 ++++++++++++++++++++-
 src/backend/libpq/pqformat.c | 18 +++++++++---------
 src/include/lib/stringinfo.h |  8 ++++++++
 3 files changed, 37 insertions(+), 10 deletions(-)

diff --git a/src/backend/lib/stringinfo.c b/src/backend/lib/stringinfo.c
index fd155671443..cb2026c3b20 100644
--- a/src/backend/lib/stringinfo.c
+++ b/src/backend/lib/stringinfo.c
@@ -202,7 +202,7 @@ appendStringInfoSpaces(StringInfo str, int count)
  * appendBinaryStringInfo
  *
  * Append arbitrary binary data to a StringInfo, allocating more space
- * if necessary.
+ * if necessary. Ensures that a trailing null byte is present.
  */
 void
 appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
@@ -224,6 +224,25 @@ appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
 	str->data[str->len] = '\0';
 }
 
+/*
+ * appendBinaryStringInfoNT
+ *
+ * Append arbitrary binary data to a StringInfo, allocating more space
+ * if necessary. Does not ensure a trailing null-byte exists.
+ */
+void
+appendBinaryStringInfoNT(StringInfo str, const char *data, int datalen)
+{
+	Assert(str != NULL);
+
+	/* Make more room if needed */
+	enlargeStringInfo(str, datalen);
+
+	/* OK, append the data */
+	memcpy(str->data + str->len, data, datalen);
+	str->len += datalen;
+}
+
 /*
  * enlargeStringInfo
  *
diff --git a/src/backend/libpq/pqformat.c b/src/backend/libpq/pqformat.c
index f27a04f8344..2414d0d8e9a 100644
--- a/src/backend/libpq/pqformat.c
+++ b/src/backend/libpq/pqformat.c
@@ -138,13 +138,13 @@ pq_sendcountedtext(StringInfo buf, const char *str, int slen,
 	{
 		slen = strlen(p);
 		pq_sendint(buf, slen + extra, 4);
-		appendBinaryStringInfo(buf, p, slen);
+		appendBinaryStringInfoNT(buf, p, slen);
 		pfree(p);
 	}
 	else
 	{
 		pq_sendint(buf, slen + extra, 4);
-		appendBinaryStringInfo(buf, str, slen);
+		appendBinaryStringInfoNT(buf, str, slen);
 	}
 }
 
@@ -191,11 +191,11 @@ pq_sendstring(StringInfo buf, const char *str)
 	if (p != str)				/* actual conversion has been done? */
 	{
 		slen = strlen(p);
-		appendBinaryStringInfo(buf, p, slen + 1);
+		appendBinaryStringInfoNT(buf, p, slen + 1);
 		pfree(p);
 	}
 	else
-		appendBinaryStringInfo(buf, str, slen + 1);
+		appendBinaryStringInfoNT(buf, str, slen + 1);
 }
 
 /* --------------------------------
@@ -242,15 +242,15 @@ pq_sendint(StringInfo buf, int i, int b)
 	{
 		case 1:
 			n8 = (unsigned char) i;
-			appendBinaryStringInfo(buf, (char *) &n8, 1);
+			appendBinaryStringInfoNT(buf, (char *) &n8, 1);
 			break;
 		case 2:
 			n16 = pg_hton16((uint16) i);
-			appendBinaryStringInfo(buf, (char *) &n16, 2);
+			appendBinaryStringInfoNT(buf, (char *) &n16, 2);
 			break;
 		case 4:
 			n32 = pg_hton32((uint32) i);
-			appendBinaryStringInfo(buf, (char *) &n32, 4);
+			appendBinaryStringInfoNT(buf, (char *) &n32, 4);
 			break;
 		default:
 			elog(ERROR, "unsupported integer size %d", b);
@@ -271,7 +271,7 @@ pq_sendint64(StringInfo buf, int64 i)
 {
 	uint64		n64 = pg_hton64(i);
 
-	appendBinaryStringInfo(buf, (char *) &n64, sizeof(n64));
+	appendBinaryStringInfoNT(buf, (char *) &n64, sizeof(n64));
 }
 
 /* --------------------------------
@@ -297,7 +297,7 @@ pq_sendfloat4(StringInfo buf, float4 f)
 	swap.f = f;
 	swap.i = pg_hton32(swap.i);
 
-	appendBinaryStringInfo(buf, (char *) &swap.i, 4);
+	appendBinaryStringInfoNT(buf, (char *) &swap.i, 4);
 }
 
 /* --------------------------------
diff --git a/src/include/lib/stringinfo.h b/src/include/lib/stringinfo.h
index 9694ea3f219..01b845db44b 100644
--- a/src/include/lib/stringinfo.h
+++ b/src/include/lib/stringinfo.h
@@ -143,6 +143,14 @@ extern void appendStringInfoSpaces(StringInfo str, int count);
 extern void appendBinaryStringInfo(StringInfo str,
 					   const char *data, int datalen);
 
+/*------------------------
+ * appendBinaryStringInfoNT
+ * Append arbitrary binary data to a StringInfo, allocating more space
+ * if necessary. Does not ensure a trailing null-byte exists.
+ */
+extern void appendBinaryStringInfoNT(StringInfo str,
+					   const char *data, int datalen);
+
 /*------------------------
  * enlargeStringInfo
  * Make sure a StringInfo's buffer can hold at least 'needed' more bytes.
-- 
2.14.1.536.g6867272d5b.dirty

>From 92e32881fd535c9e3292aae68f3331057d633492 Mon Sep 17 00:00:00 2001
From: Andres Freund <and...@anarazel.de>
Date: Wed, 13 Sep 2017 18:39:24 -0700
Subject: [PATCH 3/6] Add more efficient functions to pqformat API.

There's three prongs to achieve greater efficiency here:

1) Allow reusing a stringbuffer across pq_beginmessage/endmessage,
   with the new pq_beginmessage_reuse/endmessage_reuse. This can be
   beneficial both because it avoids allocating the initial buffer,
   and because it's more likely to already have an correctly sized
   buffer.

2) Replacing pq_sendint() with pq_sendint$width() inline
   functions. Previously unnecessary and unpredictable branches in
   pq_sendint() were needed. Additionally the replacement functions
   are implemented more efficiently.  pq_sendint is now deprecated, a
   separate commit will convert all in-tree callers.

3) Add pq_writeint$width(), pq_writestring(). These rely on sufficient
   space in the StringInfo's buffer, avoiding individual space checks
   & potential individual resizing.  To allow this to be used for
   strings, expose mbutil.c's MAX_CONVERSION_GROWTH.

Followup commits will make use of these facilities.

Author: Andres Freund
Discussion: https://postgr.es/m/20170914063418.sckdzgjfrsbek...@alap3.anarazel.de
---
 src/backend/libpq/pqformat.c   |  88 ++++++++-------------
 src/backend/utils/mb/mbutils.c |  11 ---
 src/include/libpq/pqformat.h   | 168 ++++++++++++++++++++++++++++++++++++++++-
 src/include/mb/pg_wchar.h      |  11 +++
 4 files changed, 208 insertions(+), 70 deletions(-)

diff --git a/src/backend/libpq/pqformat.c b/src/backend/libpq/pqformat.c
index 2414d0d8e9a..a5698390ae7 100644
--- a/src/backend/libpq/pqformat.c
+++ b/src/backend/libpq/pqformat.c
@@ -97,13 +97,24 @@ pq_beginmessage(StringInfo buf, char msgtype)
 }
 
 /* --------------------------------
- *		pq_sendbyte		- append a raw byte to a StringInfo buffer
+
+ *		pq_beginmessage_reuse - initialize for sending a message, reuse buffer
+ *
+ * This requires the buffer to be allocated in an sufficiently long-lived
+ * memory context.
  * --------------------------------
  */
 void
-pq_sendbyte(StringInfo buf, int byt)
+pq_beginmessage_reuse(StringInfo buf, char msgtype)
 {
-	appendStringInfoCharMacro(buf, byt);
+	resetStringInfo(buf);
+
+	/*
+	 * We stash the message type into the buffer's cursor field, expecting
+	 * that the pq_sendXXX routines won't touch it.  We could alternatively
+	 * make it the first byte of the buffer contents, but this seems easier.
+	 */
+	buf->cursor = msgtype;
 }
 
 /* --------------------------------
@@ -113,6 +124,7 @@ pq_sendbyte(StringInfo buf, int byt)
 void
 pq_sendbytes(StringInfo buf, const char *data, int datalen)
 {
+	/* use variant that maintains a trailing null-byte, out of caution */
 	appendBinaryStringInfo(buf, data, datalen);
 }
 
@@ -137,13 +149,13 @@ pq_sendcountedtext(StringInfo buf, const char *str, int slen,
 	if (p != str)				/* actual conversion has been done? */
 	{
 		slen = strlen(p);
-		pq_sendint(buf, slen + extra, 4);
+		pq_sendint32(buf, slen + extra);
 		appendBinaryStringInfoNT(buf, p, slen);
 		pfree(p);
 	}
 	else
 	{
-		pq_sendint(buf, slen + extra, 4);
+		pq_sendint32(buf, slen + extra);
 		appendBinaryStringInfoNT(buf, str, slen);
 	}
 }
@@ -227,53 +239,6 @@ pq_send_ascii_string(StringInfo buf, const char *str)
 	appendStringInfoChar(buf, '\0');
 }
 
-/* --------------------------------
- *		pq_sendint		- append a binary integer to a StringInfo buffer
- * --------------------------------
- */
-void
-pq_sendint(StringInfo buf, int i, int b)
-{
-	unsigned char n8;
-	uint16		n16;
-	uint32		n32;
-
-	switch (b)
-	{
-		case 1:
-			n8 = (unsigned char) i;
-			appendBinaryStringInfoNT(buf, (char *) &n8, 1);
-			break;
-		case 2:
-			n16 = pg_hton16((uint16) i);
-			appendBinaryStringInfoNT(buf, (char *) &n16, 2);
-			break;
-		case 4:
-			n32 = pg_hton32((uint32) i);
-			appendBinaryStringInfoNT(buf, (char *) &n32, 4);
-			break;
-		default:
-			elog(ERROR, "unsupported integer size %d", b);
-			break;
-	}
-}
-
-/* --------------------------------
- *		pq_sendint64	- append a binary 8-byte int to a StringInfo buffer
- *
- * It is tempting to merge this with pq_sendint, but we'd have to make the
- * argument int64 for all data widths --- that could be a big performance
- * hit on machines where int64 isn't efficient.
- * --------------------------------
- */
-void
-pq_sendint64(StringInfo buf, int64 i)
-{
-	uint64		n64 = pg_hton64(i);
-
-	appendBinaryStringInfoNT(buf, (char *) &n64, sizeof(n64));
-}
-
 /* --------------------------------
  *		pq_sendfloat4	- append a float4 to a StringInfo buffer
  *
@@ -295,9 +260,7 @@ pq_sendfloat4(StringInfo buf, float4 f)
 	}			swap;
 
 	swap.f = f;
-	swap.i = pg_hton32(swap.i);
-
-	appendBinaryStringInfoNT(buf, (char *) &swap.i, 4);
+	pq_sendint32(buf, swap.i);
 }
 
 /* --------------------------------
@@ -341,6 +304,21 @@ pq_endmessage(StringInfo buf)
 	buf->data = NULL;
 }
 
+/* --------------------------------
+ *		pq_endmessage_reuse	- send the completed message to the frontend
+ *
+ * The data buffer is *not* freed, allowing to reuse the buffer with
+ * pg_beginmessage_reuse.
+ --------------------------------
+ */
+
+void
+pq_endmessage_reuse(StringInfo buf)
+{
+	/* msgtype was saved in cursor field */
+	(void) pq_putmessage(buf->cursor, buf->data, buf->len);
+}
+
 
 /* --------------------------------
  *		pq_begintypsend		- initialize for constructing a bytea result
diff --git a/src/backend/utils/mb/mbutils.c b/src/backend/utils/mb/mbutils.c
index c4fbe0903ba..56f4dc1453c 100644
--- a/src/backend/utils/mb/mbutils.c
+++ b/src/backend/utils/mb/mbutils.c
@@ -41,17 +41,6 @@
 #include "utils/memutils.h"
 #include "utils/syscache.h"
 
-/*
- * When converting strings between different encodings, we assume that space
- * for converted result is 4-to-1 growth in the worst case. The rate for
- * currently supported encoding pairs are within 3 (SJIS JIS X0201 half width
- * kanna -> UTF8 is the worst case).  So "4" should be enough for the moment.
- *
- * Note that this is not the same as the maximum character width in any
- * particular encoding.
- */
-#define MAX_CONVERSION_GROWTH  4
-
 /*
  * We maintain a simple linked list caching the fmgr lookup info for the
  * currently selected conversion functions, as well as any that have been
diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h
index 32112547a0b..9a546b48915 100644
--- a/src/include/libpq/pqformat.h
+++ b/src/include/libpq/pqformat.h
@@ -14,20 +14,180 @@
 #define PQFORMAT_H
 
 #include "lib/stringinfo.h"
+#include "mb/pg_wchar.h"
+#include "port/pg_bswap.h"
 
 extern void pq_beginmessage(StringInfo buf, char msgtype);
-extern void pq_sendbyte(StringInfo buf, int byt);
+extern void pq_beginmessage_reuse(StringInfo buf, char msgtype);
+extern void pq_endmessage(StringInfo buf);
+extern void pq_endmessage_reuse(StringInfo buf);
+
 extern void pq_sendbytes(StringInfo buf, const char *data, int datalen);
 extern void pq_sendcountedtext(StringInfo buf, const char *str, int slen,
 				   bool countincludesself);
 extern void pq_sendtext(StringInfo buf, const char *str, int slen);
 extern void pq_sendstring(StringInfo buf, const char *str);
 extern void pq_send_ascii_string(StringInfo buf, const char *str);
-extern void pq_sendint(StringInfo buf, int i, int b);
-extern void pq_sendint64(StringInfo buf, int64 i);
 extern void pq_sendfloat4(StringInfo buf, float4 f);
 extern void pq_sendfloat8(StringInfo buf, float8 f);
-extern void pq_endmessage(StringInfo buf);
+
+extern void pq_sendfloat4(StringInfo buf, float4 f);
+extern void pq_sendfloat8(StringInfo buf, float8 f);
+
+/*
+ * Append a int8 to a StringInfo buffer, which already has enough space
+ * preallocated.
+ *
+ * The use of restrict allows the compiler to optimize the code based on the
+ * assumption that buf, buf->len, buf->data and *buf->data don't
+ * overlap. Without the annotation buf->len etc cannot be kept in a register
+ * over subsequent pq_writeint* calls.
+ */
+static inline void
+pq_writeint8(StringInfo restrict buf, int8 i)
+{
+	int8		ni = i;
+
+	Assert(buf->len + sizeof(i) <= buf->maxlen);
+	memcpy((char *restrict) (buf->data + buf->len), &ni, sizeof(ni));
+	buf->len += sizeof(i);
+}
+
+/*
+ * Append a int16 to a StringInfo buffer, which already has enough space
+ * preallocated.
+ */
+static inline void
+pq_writeint16(StringInfo restrict buf, int16 i)
+{
+	int16		ni = pg_hton16(i);
+
+	Assert(buf->len + sizeof(ni) <= buf->maxlen);
+	memcpy((char *restrict) (buf->data + buf->len), &ni, sizeof(i));
+	buf->len += sizeof(i);
+}
+
+/*
+ * Append a int32 to a StringInfo buffer, which already has enough space
+ * preallocated.
+ */
+static inline void
+pq_writeint32(StringInfo restrict buf, int32 i)
+{
+	int32		ni = pg_hton32(i);
+
+	Assert(buf->len + sizeof(i) <= buf->maxlen);
+	memcpy((char *restrict) (buf->data + buf->len), &ni, sizeof(i));
+	buf->len += sizeof(i);
+}
+
+/*
+ * Append a int64 to a StringInfo buffer, which already has enough space
+ * preallocated.
+ */
+static inline void
+pq_writeint64(StringInfo restrict buf, int64 i)
+{
+	int64		ni = pg_hton64(i);
+
+	Assert(buf->len + sizeof(i) <= buf->maxlen);
+	memcpy((char *restrict) (buf->data + buf->len), &ni, sizeof(i));
+	buf->len += sizeof(i);
+}
+
+/*
+ * Append a null-terminated text string (with conversion) to a buffer with
+ * preallocated space.
+ *
+ * NB: The pre-allocated space needs to be sufficient for the string after
+ * converting to client encoding.
+ *
+ * NB: passed text string must be null-terminated, and so is the data
+ * sent to the frontend.
+ */
+static inline void
+pq_writestring(StringInfo restrict buf, const char *restrict str)
+{
+	int			slen = strlen(str);
+	char	   *p;
+
+	p = pg_server_to_client(str, slen);
+	if (p != str)				/* actual conversion has been done? */
+		slen = strlen(p);
+
+	Assert(buf->len + slen + 1 <= buf->maxlen);
+
+	memcpy(((char *restrict) buf->data + buf->len), p, slen + 1);
+	buf->len += slen + 1;
+
+	if (p != str)
+		pfree(p);
+}
+
+/* append a binary int8 to a StringInfo buffer */
+static inline void
+pq_sendint8(StringInfo buf, int8 i)
+{
+	enlargeStringInfo(buf, sizeof(i));
+	pq_writeint8(buf, i);
+}
+
+/* append a binary int16 to a StringInfo buffer */
+static inline void
+pq_sendint16(StringInfo buf, int16 i)
+{
+	enlargeStringInfo(buf, sizeof(i));
+	pq_writeint16(buf, i);
+}
+
+/* append a binary int32 to a StringInfo buffer */
+static inline void
+pq_sendint32(StringInfo buf, int32 i)
+{
+	enlargeStringInfo(buf, sizeof(i));
+	pq_writeint32(buf, i);
+}
+
+/* append a binary int64 to a StringInfo buffer */
+static inline void
+pq_sendint64(StringInfo buf, int64 i)
+{
+	enlargeStringInfo(buf, sizeof(i));
+	pq_writeint64(buf, i);
+}
+
+/* append a binary byte to a StringInfo buffer */
+static inline void
+pq_sendbyte(StringInfo buf, int8 byt)
+{
+	pq_sendint8(buf, byt);
+}
+
+/*
+ * Append a binary integer to a StringInfo buffer
+ *
+ * This function is deprecated.
+ */
+static inline void
+pq_sendint(StringInfo buf, int i, int b)
+{
+	switch (b)
+	{
+		case 1:
+			pq_sendint8(buf, (int8) i);
+			break;
+		case 2:
+			pq_sendint16(buf, (int16) i);
+			break;
+		case 4:
+			pq_sendint32(buf, (int32) i);
+			break;
+		default:
+			elog(ERROR, "unsupported integer size %d", b);
+			break;
+	}
+}
+
 
 extern void pq_begintypsend(StringInfo buf);
 extern bytea *pq_endtypsend(StringInfo buf);
diff --git a/src/include/mb/pg_wchar.h b/src/include/mb/pg_wchar.h
index d57ef017cb4..9227d634f66 100644
--- a/src/include/mb/pg_wchar.h
+++ b/src/include/mb/pg_wchar.h
@@ -304,6 +304,17 @@ typedef enum pg_enc
 /* On FE are possible all encodings */
 #define PG_VALID_FE_ENCODING(_enc)	PG_VALID_ENCODING(_enc)
 
+/*
+ * When converting strings between different encodings, we assume that space
+ * for converted result is 4-to-1 growth in the worst case. The rate for
+ * currently supported encoding pairs are within 3 (SJIS JIS X0201 half width
+ * kanna -> UTF8 is the worst case).  So "4" should be enough for the moment.
+ *
+ * Note that this is not the same as the maximum character width in any
+ * particular encoding.
+ */
+#define MAX_CONVERSION_GROWTH  4
+
 /*
  * Table for mapping an encoding number to official encoding name and
  * possibly other subsidiary data.  Be careful to check encoding number
-- 
2.14.1.536.g6867272d5b.dirty

>From 0d91cd0a278419963e1e7dcf2cdf4d0630d1efb6 Mon Sep 17 00:00:00 2001
From: Andres Freund <and...@anarazel.de>
Date: Tue, 3 Oct 2017 00:36:42 -0700
Subject: [PATCH 4/6] Use one stringbuffer for all rows printed in printtup.c.

This avoids newly allocating, and then possibly growing, the
stringbuffer for every row. For wide rows this can substantially
reduce memory allocator overhead, at the price of not immediately
reducing memory usage after outputting an especially wide row.
---
 src/backend/access/common/printtup.c | 45 +++++++++++++++++++-----------------
 1 file changed, 24 insertions(+), 21 deletions(-)

diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c
index 20d20e623e8..bb807239b5a 100644
--- a/src/backend/access/common/printtup.c
+++ b/src/backend/access/common/printtup.c
@@ -57,6 +57,7 @@ typedef struct
 typedef struct
 {
 	DestReceiver pub;			/* publicly-known function pointers */
+	StringInfoData buf;			/* output buffer */
 	Portal		portal;			/* the Portal we are printing from */
 	bool		sendDescrip;	/* send RowDescription at startup? */
 	TupleDesc	attrinfo;		/* The attr info we are set up for */
@@ -251,6 +252,8 @@ printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
 	int16	   *formats = myState->portal->formats;
 	int			i;
 
+	initStringInfo(&myState->buf);
+
 	/* get rid of any old data */
 	if (myState->myinfo)
 		pfree(myState->myinfo);
@@ -302,7 +305,7 @@ printtup(TupleTableSlot *slot, DestReceiver *self)
 	TupleDesc	typeinfo = slot->tts_tupleDescriptor;
 	DR_printtup *myState = (DR_printtup *) self;
 	MemoryContext oldcontext;
-	StringInfoData buf;
+	StringInfo	buf = &myState->buf;
 	int			natts = typeinfo->natts;
 	int			i;
 
@@ -319,9 +322,9 @@ printtup(TupleTableSlot *slot, DestReceiver *self)
 	/*
 	 * Prepare a DataRow message (note buffer is in per-row context)
 	 */
-	pq_beginmessage(&buf, 'D');
+	pq_beginmessage_reuse(buf, 'D');
 
-	pq_sendint(&buf, natts, 2);
+	pq_sendint(buf, natts, 2);
 
 	/*
 	 * send the attributes of this tuple
@@ -333,7 +336,7 @@ printtup(TupleTableSlot *slot, DestReceiver *self)
 
 		if (slot->tts_isnull[i])
 		{
-			pq_sendint(&buf, -1, 4);
+			pq_sendint(buf, -1, 4);
 			continue;
 		}
 
@@ -354,7 +357,7 @@ printtup(TupleTableSlot *slot, DestReceiver *self)
 			char	   *outputstr;
 
 			outputstr = OutputFunctionCall(&thisState->finfo, attr);
-			pq_sendcountedtext(&buf, outputstr, strlen(outputstr), false);
+			pq_sendcountedtext(buf, outputstr, strlen(outputstr), false);
 		}
 		else
 		{
@@ -362,13 +365,13 @@ printtup(TupleTableSlot *slot, DestReceiver *self)
 			bytea	   *outputbytes;
 
 			outputbytes = SendFunctionCall(&thisState->finfo, attr);
-			pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
-			pq_sendbytes(&buf, VARDATA(outputbytes),
+			pq_sendint(buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
+			pq_sendbytes(buf, VARDATA(outputbytes),
 						 VARSIZE(outputbytes) - VARHDRSZ);
 		}
 	}
 
-	pq_endmessage(&buf);
+	pq_endmessage_reuse(buf);
 
 	/* Return to caller's context, and flush row's temporary memory */
 	MemoryContextSwitchTo(oldcontext);
@@ -387,7 +390,7 @@ printtup_20(TupleTableSlot *slot, DestReceiver *self)
 	TupleDesc	typeinfo = slot->tts_tupleDescriptor;
 	DR_printtup *myState = (DR_printtup *) self;
 	MemoryContext oldcontext;
-	StringInfoData buf;
+	StringInfo	buf = &myState->buf;
 	int			natts = typeinfo->natts;
 	int			i,
 				j,
@@ -406,7 +409,7 @@ printtup_20(TupleTableSlot *slot, DestReceiver *self)
 	/*
 	 * tell the frontend to expect new tuple data (in ASCII style)
 	 */
-	pq_beginmessage(&buf, 'D');
+	pq_beginmessage_reuse(buf, 'D');
 
 	/*
 	 * send a bitmap of which attributes are not null
@@ -420,13 +423,13 @@ printtup_20(TupleTableSlot *slot, DestReceiver *self)
 		k >>= 1;
 		if (k == 0)				/* end of byte? */
 		{
-			pq_sendint(&buf, j, 1);
+			pq_sendint(buf, j, 1);
 			j = 0;
 			k = 1 << 7;
 		}
 	}
 	if (k != (1 << 7))			/* flush last partial byte */
-		pq_sendint(&buf, j, 1);
+		pq_sendint(buf, j, 1);
 
 	/*
 	 * send the attributes of this tuple
@@ -443,10 +446,10 @@ printtup_20(TupleTableSlot *slot, DestReceiver *self)
 		Assert(thisState->format == 0);
 
 		outputstr = OutputFunctionCall(&thisState->finfo, attr);
-		pq_sendcountedtext(&buf, outputstr, strlen(outputstr), true);
+		pq_sendcountedtext(buf, outputstr, strlen(outputstr), true);
 	}
 
-	pq_endmessage(&buf);
+	pq_endmessage_reuse(buf);
 
 	/* Return to caller's context, and flush row's temporary memory */
 	MemoryContextSwitchTo(oldcontext);
@@ -572,7 +575,7 @@ printtup_internal_20(TupleTableSlot *slot, DestReceiver *self)
 	TupleDesc	typeinfo = slot->tts_tupleDescriptor;
 	DR_printtup *myState = (DR_printtup *) self;
 	MemoryContext oldcontext;
-	StringInfoData buf;
+	StringInfo	buf = &myState->buf;
 	int			natts = typeinfo->natts;
 	int			i,
 				j,
@@ -591,7 +594,7 @@ printtup_internal_20(TupleTableSlot *slot, DestReceiver *self)
 	/*
 	 * tell the frontend to expect new tuple data (in binary style)
 	 */
-	pq_beginmessage(&buf, 'B');
+	pq_beginmessage_reuse(buf, 'B');
 
 	/*
 	 * send a bitmap of which attributes are not null
@@ -605,13 +608,13 @@ printtup_internal_20(TupleTableSlot *slot, DestReceiver *self)
 		k >>= 1;
 		if (k == 0)				/* end of byte? */
 		{
-			pq_sendint(&buf, j, 1);
+			pq_sendint(buf, j, 1);
 			j = 0;
 			k = 1 << 7;
 		}
 	}
 	if (k != (1 << 7))			/* flush last partial byte */
-		pq_sendint(&buf, j, 1);
+		pq_sendint(buf, j, 1);
 
 	/*
 	 * send the attributes of this tuple
@@ -628,12 +631,12 @@ printtup_internal_20(TupleTableSlot *slot, DestReceiver *self)
 		Assert(thisState->format == 1);
 
 		outputbytes = SendFunctionCall(&thisState->finfo, attr);
-		pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
-		pq_sendbytes(&buf, VARDATA(outputbytes),
+		pq_sendint(buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
+		pq_sendbytes(buf, VARDATA(outputbytes),
 					 VARSIZE(outputbytes) - VARHDRSZ);
 	}
 
-	pq_endmessage(&buf);
+	pq_endmessage_reuse(buf);
 
 	/* Return to caller's context, and flush row's temporary memory */
 	MemoryContextSwitchTo(oldcontext);
-- 
2.14.1.536.g6867272d5b.dirty

>From 25a22cfc0f466219c875bc73d5110a538bf8b96c Mon Sep 17 00:00:00 2001
From: Andres Freund <and...@anarazel.de>
Date: Tue, 3 Oct 2017 00:39:16 -0700
Subject: [PATCH 5/6] Improve performance of SendRowDescriptionMessage.

There's three categories of changes leading to better performance:
- Splitting the per-attribute part of SendRowDescriptionMessage into a
  v2 and a v3 version allows avoiding branches for every attribute.
- Preallocating the size of the buffer to be big enough for all
  attributes and then using pq_write* avoids unnecessary buffer
  size checks & resizing.
- Reusing a persistently allocated StringInfo for all
  SendRowDescriptionMessage() invocations avoids repeated allocations
  & reallocations.

Author: Andres Freund
Discussion: https://postgr.es/m/20170914063418.sckdzgjfrsbek...@alap3.anarazel.de
---
 src/backend/access/common/printtup.c | 153 +++++++++++++++++++++++++----------
 src/backend/tcop/postgres.c          |  34 ++++++--
 src/include/access/printtup.h        |   4 +-
 3 files changed, 141 insertions(+), 50 deletions(-)

diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c
index bb807239b5a..abe0426a23b 100644
--- a/src/backend/access/common/printtup.c
+++ b/src/backend/access/common/printtup.c
@@ -32,6 +32,8 @@ static bool printtup_internal_20(TupleTableSlot *slot, DestReceiver *self);
 static void printtup_shutdown(DestReceiver *self);
 static void printtup_destroy(DestReceiver *self);
 
+static void SendRowDescriptionCols_2(StringInfo buf, TupleDesc typeinfo, List *targetlist, int16 *formats);
+static void SendRowDescriptionCols_3(StringInfo buf, TupleDesc typeinfo, List *targetlist, int16 *formats);
 
 /* ----------------------------------------------------------------
  *		printtup / debugtup support
@@ -128,6 +130,9 @@ printtup_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
 	DR_printtup *myState = (DR_printtup *) self;
 	Portal		portal = myState->portal;
 
+	/* create buffer to be used for all messages */
+	initStringInfo(&myState->buf);
+
 	/*
 	 * Create a temporary memory context that we can reset once per row to
 	 * recover palloc'd memory.  This avoids any problems with leaks inside
@@ -158,7 +163,8 @@ printtup_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
 	 * descriptor of the tuples.
 	 */
 	if (myState->sendDescrip)
-		SendRowDescriptionMessage(typeinfo,
+		SendRowDescriptionMessage(&myState->buf,
+								  typeinfo,
 								  FetchPortalTargetList(portal),
 								  portal->formats);
 
@@ -186,16 +192,109 @@ printtup_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
  * send zeroes for the format codes in that case.
  */
 void
-SendRowDescriptionMessage(TupleDesc typeinfo, List *targetlist, int16 *formats)
+SendRowDescriptionMessage(StringInfo buf, TupleDesc typeinfo,
+						  List *targetlist, int16 *formats)
 {
 	int			natts = typeinfo->natts;
 	int			proto = PG_PROTOCOL_MAJOR(FrontendProtocol);
+
+	/* tuple descriptor message type */
+	pq_beginmessage_reuse(buf, 'T');
+	/* # of attrs in tuples */
+	pq_sendint16(buf, natts);
+
+	if (proto >= 3)
+		SendRowDescriptionCols_3(buf, typeinfo, targetlist, formats);
+	else
+		SendRowDescriptionCols_2(buf, typeinfo, targetlist, formats);
+
+	pq_endmessage_reuse(buf);
+}
+
+/*
+ * Send description for each column when using v3+ protocol
+ */
+static void
+SendRowDescriptionCols_3(StringInfo buf, TupleDesc typeinfo, List *targetlist, int16 *formats)
+{
+	int			natts = typeinfo->natts;
 	int			i;
-	StringInfoData buf;
 	ListCell   *tlist_item = list_head(targetlist);
 
-	pq_beginmessage(&buf, 'T'); /* tuple descriptor message type */
-	pq_sendint(&buf, natts, 2); /* # of attrs in tuples */
+	/*
+	 * Preallocate memory for the entire message to be sent. That allows to
+	 * use the significantly faster inline pqformat.h functions and to avoid
+	 * reallocations.
+	 *
+	 * Have to overestimate the size of the column-names, to account for
+	 * character set overhead.
+	 */
+	enlargeStringInfo(buf, (NAMEDATALEN * MAX_CONVERSION_GROWTH /* attname */
+							+ sizeof(int32) /* attlen */
+							+ sizeof(int32) /* resorigtbl */
+							+ sizeof(int16) /* resorigcol */
+							+ sizeof(Oid) /* atttypid */
+							+ sizeof(int16) /* attlen */
+							+ sizeof(int32) /* attypmod */
+						  ) * natts);
+
+	for (i = 0; i < natts; ++i)
+	{
+		Form_pg_attribute att = TupleDescAttr(typeinfo, i);
+		Oid			atttypid = att->atttypid;
+		int32		atttypmod = att->atttypmod;
+		int32		resorigtbl;
+		int32		resorigcol;
+		int16		format;
+
+		/*
+		 * If column is a domain, send the base type and typmod
+		 * instead. Lookup before sending any ints, for efficiency.
+		 */
+		atttypid = getBaseTypeAndTypmod(atttypid, &atttypmod);
+
+		/* Do we have a non-resjunk tlist item? */
+		while (tlist_item &&
+			   ((TargetEntry *) lfirst(tlist_item))->resjunk)
+			tlist_item = lnext(tlist_item);
+		if (tlist_item)
+		{
+			TargetEntry *tle = (TargetEntry *) lfirst(tlist_item);
+
+			resorigtbl = tle->resorigtbl;
+			resorigcol = tle->resorigcol;
+			tlist_item = lnext(tlist_item);
+		}
+		else
+		{
+			/* No info available, so send zeroes */
+			resorigtbl = 0;
+			resorigcol = 0;
+		}
+
+		if (formats)
+			format = formats[i];
+		else
+			format = 0;
+
+		pq_writestring(buf, NameStr(att->attname));
+		pq_writeint32(buf, resorigtbl);
+		pq_writeint16(buf, resorigcol);
+		pq_writeint32(buf, atttypid);
+		pq_writeint16(buf, att->attlen);
+		pq_writeint32(buf, atttypmod);
+		pq_writeint16(buf, format);
+	}
+}
+
+/*
+ * Send description for each column when using v2 protocol
+ */
+static void
+SendRowDescriptionCols_2(StringInfo buf, TupleDesc typeinfo, List *targetlist, int16 *formats)
+{
+	int			natts = typeinfo->natts;
+	int			i;
 
 	for (i = 0; i < natts; ++i)
 	{
@@ -203,44 +302,16 @@ SendRowDescriptionMessage(TupleDesc typeinfo, List *targetlist, int16 *formats)
 		Oid			atttypid = att->atttypid;
 		int32		atttypmod = att->atttypmod;
 
-		pq_sendstring(&buf, NameStr(att->attname));
-		/* column ID info appears in protocol 3.0 and up */
-		if (proto >= 3)
-		{
-			/* Do we have a non-resjunk tlist item? */
-			while (tlist_item &&
-				   ((TargetEntry *) lfirst(tlist_item))->resjunk)
-				tlist_item = lnext(tlist_item);
-			if (tlist_item)
-			{
-				TargetEntry *tle = (TargetEntry *) lfirst(tlist_item);
-
-				pq_sendint(&buf, tle->resorigtbl, 4);
-				pq_sendint(&buf, tle->resorigcol, 2);
-				tlist_item = lnext(tlist_item);
-			}
-			else
-			{
-				/* No info available, so send zeroes */
-				pq_sendint(&buf, 0, 4);
-				pq_sendint(&buf, 0, 2);
-			}
-		}
 		/* If column is a domain, send the base type and typmod instead */
 		atttypid = getBaseTypeAndTypmod(atttypid, &atttypmod);
-		pq_sendint(&buf, (int) atttypid, sizeof(atttypid));
-		pq_sendint(&buf, att->attlen, sizeof(att->attlen));
-		pq_sendint(&buf, atttypmod, sizeof(atttypmod));
-		/* format info appears in protocol 3.0 and up */
-		if (proto >= 3)
-		{
-			if (formats)
-				pq_sendint(&buf, formats[i], 2);
-			else
-				pq_sendint(&buf, 0, 2);
-		}
+
+		pq_sendstring(buf, NameStr(att->attname));
+		/* column ID only info appears in protocol 3.0 and up */
+		pq_sendint32(buf, atttypid);
+		pq_sendint16(buf, att->attlen);
+		pq_sendint32(buf, atttypmod);
+		/* format info only appears in protocol 3.0 and up */
 	}
-	pq_endmessage(&buf);
 }
 
 /*
@@ -252,8 +323,6 @@ printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
 	int16	   *formats = myState->portal->formats;
 	int			i;
 
-	initStringInfo(&myState->buf);
-
 	/* get rid of any old data */
 	if (myState->myinfo)
 		pfree(myState->myinfo);
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index c807b00b0be..36bb8c24fb4 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -165,6 +165,10 @@ static bool RecoveryConflictPending = false;
 static bool RecoveryConflictRetryable = true;
 static ProcSignalReason RecoveryConflictReason;
 
+/* reused buffer to pass to SendRowDescriptionMessage() */
+static MemoryContext row_description_context = NULL;
+static StringInfoData row_description_buf;
+
 /* ----------------------------------------------------------------
  *		decls for routines only used in this file
  * ----------------------------------------------------------------
@@ -2371,16 +2375,17 @@ exec_describe_statement_message(const char *stmt_name)
 	/*
 	 * First describe the parameters...
 	 */
-	pq_beginmessage(&buf, 't'); /* parameter description message type */
-	pq_sendint(&buf, psrc->num_params, 2);
+	pq_beginmessage_reuse(&row_description_buf, 't'); /* parameter description
+													   * message type */
+	pq_sendint(&row_description_buf, psrc->num_params, 2);
 
 	for (i = 0; i < psrc->num_params; i++)
 	{
 		Oid			ptype = psrc->param_types[i];
 
-		pq_sendint(&buf, (int) ptype, 4);
+		pq_sendint(&row_description_buf, (int) ptype, 4);
 	}
-	pq_endmessage(&buf);
+	pq_endmessage_reuse(&row_description_buf);
 
 	/*
 	 * Next send RowDescription or NoData to describe the result...
@@ -2392,7 +2397,10 @@ exec_describe_statement_message(const char *stmt_name)
 		/* Get the plan's primary targetlist */
 		tlist = CachedPlanGetTargetList(psrc, NULL);
 
-		SendRowDescriptionMessage(psrc->resultDesc, tlist, NULL);
+		SendRowDescriptionMessage(&row_description_buf,
+								  psrc->resultDesc,
+								  tlist,
+								  NULL);
 	}
 	else
 		pq_putemptymessage('n');	/* NoData */
@@ -2444,7 +2452,8 @@ exec_describe_portal_message(const char *portal_name)
 		return;					/* can't actually do anything... */
 
 	if (portal->tupDesc)
-		SendRowDescriptionMessage(portal->tupDesc,
+		SendRowDescriptionMessage(&row_description_buf,
+								  portal->tupDesc,
 								  FetchPortalTargetList(portal),
 								  portal->formats);
 	else
@@ -3832,6 +3841,19 @@ PostgresMain(int argc, char *argv[],
 										   "MessageContext",
 										   ALLOCSET_DEFAULT_SIZES);
 
+	/*
+	 * Create memory context and buffer used for RowDescription messages. As
+	 * SendRowDescriptionMessage(), via exec_describe_statement_message(), is
+	 * frequently executed for ever single statement, we don't want to
+	 * allocate a separate buffer every time.
+	 */
+	row_description_context = AllocSetContextCreate(TopMemoryContext,
+													"RowDescriptionContext",
+													ALLOCSET_DEFAULT_SIZES);
+	MemoryContextSwitchTo(row_description_context);
+	initStringInfo(&row_description_buf);
+	MemoryContextSwitchTo(TopMemoryContext);
+
 	/*
 	 * Remember stand-alone backend startup time
 	 */
diff --git a/src/include/access/printtup.h b/src/include/access/printtup.h
index 641715e4165..cca5b1f6002 100644
--- a/src/include/access/printtup.h
+++ b/src/include/access/printtup.h
@@ -20,8 +20,8 @@ extern DestReceiver *printtup_create_DR(CommandDest dest);
 
 extern void SetRemoteDestReceiverParams(DestReceiver *self, Portal portal);
 
-extern void SendRowDescriptionMessage(TupleDesc typeinfo, List *targetlist,
-						  int16 *formats);
+extern void SendRowDescriptionMessage(StringInfo buf,
+			 TupleDesc typeinfo, List *targetlist, int16 *formats);
 
 extern void debugStartup(DestReceiver *self, int operation,
 			 TupleDesc typeinfo);
-- 
2.14.1.536.g6867272d5b.dirty

>From e344f61f7706c5342c1533559e38c41e0041ffb7 Mon Sep 17 00:00:00 2001
From: Andres Freund <and...@anarazel.de>
Date: Tue, 10 Oct 2017 17:26:40 -0700
Subject: [PATCH 6/6] Replace remaining uses of pq_sendint with pq_sendintXX.

pq_sendint() remains, so extension code doesn't unnecessarily break.

Author: Andres Freund
Discussion: https://postgr.es/m/20170914063418.sckdzgjfrsbek...@alap3.anarazel.de
---
 contrib/hstore/hstore_io.c              |  8 +--
 src/backend/access/common/printsimple.c | 18 +++----
 src/backend/access/common/printtup.c    | 16 +++---
 src/backend/access/transam/parallel.c   |  4 +-
 src/backend/commands/async.c            |  2 +-
 src/backend/commands/copy.c             |  8 +--
 src/backend/libpq/auth.c                |  2 +-
 src/backend/replication/basebackup.c    | 86 ++++++++++++++++-----------------
 src/backend/replication/logical/proto.c | 20 ++++----
 src/backend/replication/walreceiver.c   |  8 +--
 src/backend/replication/walsender.c     | 36 +++++++-------
 src/backend/tcop/fastpath.c             |  4 +-
 src/backend/tcop/postgres.c             |  8 +--
 src/backend/utils/adt/arrayfuncs.c      | 14 +++---
 src/backend/utils/adt/date.c            |  4 +-
 src/backend/utils/adt/geo_ops.c         |  4 +-
 src/backend/utils/adt/int.c             |  4 +-
 src/backend/utils/adt/jsonb.c           |  2 +-
 src/backend/utils/adt/nabstime.c        | 10 ++--
 src/backend/utils/adt/numeric.c         | 14 +++---
 src/backend/utils/adt/oid.c             |  2 +-
 src/backend/utils/adt/rangetypes.c      |  4 +-
 src/backend/utils/adt/rowtypes.c        |  8 +--
 src/backend/utils/adt/tid.c             |  6 +--
 src/backend/utils/adt/timestamp.c       |  4 +-
 src/backend/utils/adt/tsquery.c         | 13 +++--
 src/backend/utils/adt/tsvector.c        |  6 +--
 src/backend/utils/adt/txid.c            |  2 +-
 src/backend/utils/adt/varbit.c          |  2 +-
 src/backend/utils/adt/xid.c             |  4 +-
 30 files changed, 160 insertions(+), 163 deletions(-)

diff --git a/contrib/hstore/hstore_io.c b/contrib/hstore/hstore_io.c
index 6363c321c5d..d8284012d0b 100644
--- a/contrib/hstore/hstore_io.c
+++ b/contrib/hstore/hstore_io.c
@@ -1207,23 +1207,23 @@ hstore_send(PG_FUNCTION_ARGS)
 
 	pq_begintypsend(&buf);
 
-	pq_sendint(&buf, count, 4);
+	pq_sendint32(&buf, count);
 
 	for (i = 0; i < count; i++)
 	{
 		int32		keylen = HSTORE_KEYLEN(entries, i);
 
-		pq_sendint(&buf, keylen, 4);
+		pq_sendint32(&buf, keylen);
 		pq_sendtext(&buf, HSTORE_KEY(entries, base, i), keylen);
 		if (HSTORE_VALISNULL(entries, i))
 		{
-			pq_sendint(&buf, -1, 4);
+			pq_sendint32(&buf, -1);
 		}
 		else
 		{
 			int32		vallen = HSTORE_VALLEN(entries, i);
 
-			pq_sendint(&buf, vallen, 4);
+			pq_sendint32(&buf, vallen);
 			pq_sendtext(&buf, HSTORE_VAL(entries, base, i), vallen);
 		}
 	}
diff --git a/src/backend/access/common/printsimple.c b/src/backend/access/common/printsimple.c
index b3e9a26b032..872de7c3f44 100644
--- a/src/backend/access/common/printsimple.c
+++ b/src/backend/access/common/printsimple.c
@@ -34,19 +34,19 @@ printsimple_startup(DestReceiver *self, int operation, TupleDesc tupdesc)
 	int			i;
 
 	pq_beginmessage(&buf, 'T'); /* RowDescription */
-	pq_sendint(&buf, tupdesc->natts, 2);
+	pq_sendint16(&buf, tupdesc->natts);
 
 	for (i = 0; i < tupdesc->natts; ++i)
 	{
 		Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
 
 		pq_sendstring(&buf, NameStr(attr->attname));
-		pq_sendint(&buf, 0, 4); /* table oid */
-		pq_sendint(&buf, 0, 2); /* attnum */
-		pq_sendint(&buf, (int) attr->atttypid, 4);
-		pq_sendint(&buf, attr->attlen, 2);
-		pq_sendint(&buf, attr->atttypmod, 4);
-		pq_sendint(&buf, 0, 2); /* format code */
+		pq_sendint32(&buf, 0); /* table oid */
+		pq_sendint16(&buf, 0); /* attnum */
+		pq_sendint32(&buf, (int) attr->atttypid);
+		pq_sendint16(&buf, attr->attlen);
+		pq_sendint32(&buf, attr->atttypmod);
+		pq_sendint16(&buf, 0); /* format code */
 	}
 
 	pq_endmessage(&buf);
@@ -67,7 +67,7 @@ printsimple(TupleTableSlot *slot, DestReceiver *self)
 
 	/* Prepare and send message */
 	pq_beginmessage(&buf, 'D');
-	pq_sendint(&buf, tupdesc->natts, 2);
+	pq_sendint16(&buf, tupdesc->natts);
 
 	for (i = 0; i < tupdesc->natts; ++i)
 	{
@@ -76,7 +76,7 @@ printsimple(TupleTableSlot *slot, DestReceiver *self)
 
 		if (slot->tts_isnull[i])
 		{
-			pq_sendint(&buf, -1, 4);
+			pq_sendint32(&buf, -1);
 			continue;
 		}
 
diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c
index abe0426a23b..e8f98016036 100644
--- a/src/backend/access/common/printtup.c
+++ b/src/backend/access/common/printtup.c
@@ -393,7 +393,7 @@ printtup(TupleTableSlot *slot, DestReceiver *self)
 	 */
 	pq_beginmessage_reuse(buf, 'D');
 
-	pq_sendint(buf, natts, 2);
+	pq_sendint16(buf, natts);
 
 	/*
 	 * send the attributes of this tuple
@@ -405,7 +405,7 @@ printtup(TupleTableSlot *slot, DestReceiver *self)
 
 		if (slot->tts_isnull[i])
 		{
-			pq_sendint(buf, -1, 4);
+			pq_sendint32(buf, -1);
 			continue;
 		}
 
@@ -434,7 +434,7 @@ printtup(TupleTableSlot *slot, DestReceiver *self)
 			bytea	   *outputbytes;
 
 			outputbytes = SendFunctionCall(&thisState->finfo, attr);
-			pq_sendint(buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
+			pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ);
 			pq_sendbytes(buf, VARDATA(outputbytes),
 						 VARSIZE(outputbytes) - VARHDRSZ);
 		}
@@ -492,13 +492,13 @@ printtup_20(TupleTableSlot *slot, DestReceiver *self)
 		k >>= 1;
 		if (k == 0)				/* end of byte? */
 		{
-			pq_sendint(buf, j, 1);
+			pq_sendint8(buf, j);
 			j = 0;
 			k = 1 << 7;
 		}
 	}
 	if (k != (1 << 7))			/* flush last partial byte */
-		pq_sendint(buf, j, 1);
+		pq_sendint8(buf, j);
 
 	/*
 	 * send the attributes of this tuple
@@ -677,13 +677,13 @@ printtup_internal_20(TupleTableSlot *slot, DestReceiver *self)
 		k >>= 1;
 		if (k == 0)				/* end of byte? */
 		{
-			pq_sendint(buf, j, 1);
+			pq_sendint8(buf, j);
 			j = 0;
 			k = 1 << 7;
 		}
 	}
 	if (k != (1 << 7))			/* flush last partial byte */
-		pq_sendint(buf, j, 1);
+		pq_sendint8(buf, j);
 
 	/*
 	 * send the attributes of this tuple
@@ -700,7 +700,7 @@ printtup_internal_20(TupleTableSlot *slot, DestReceiver *self)
 		Assert(thisState->format == 1);
 
 		outputbytes = SendFunctionCall(&thisState->finfo, attr);
-		pq_sendint(buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
+		pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ);
 		pq_sendbytes(buf, VARDATA(outputbytes),
 					 VARSIZE(outputbytes) - VARHDRSZ);
 	}
diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/transam/parallel.c
index c6f7b7af0e1..d6830507330 100644
--- a/src/backend/access/transam/parallel.c
+++ b/src/backend/access/transam/parallel.c
@@ -1030,8 +1030,8 @@ ParallelWorkerMain(Datum main_arg)
 	 * in this case.
 	 */
 	pq_beginmessage(&msgbuf, 'K');
-	pq_sendint(&msgbuf, (int32) MyProcPid, sizeof(int32));
-	pq_sendint(&msgbuf, (int32) MyCancelKey, sizeof(int32));
+	pq_sendint32(&msgbuf, (int32) MyProcPid);
+	pq_sendint32(&msgbuf, (int32) MyCancelKey);
 	pq_endmessage(&msgbuf);
 
 	/*
diff --git a/src/backend/commands/async.c b/src/backend/commands/async.c
index bacc08eb84f..cbc1dd6db48 100644
--- a/src/backend/commands/async.c
+++ b/src/backend/commands/async.c
@@ -2081,7 +2081,7 @@ NotifyMyFrontEnd(const char *channel, const char *payload, int32 srcPid)
 		StringInfoData buf;
 
 		pq_beginmessage(&buf, 'A');
-		pq_sendint(&buf, srcPid, sizeof(int32));
+		pq_sendint32(&buf, srcPid);
 		pq_sendstring(&buf, channel);
 		if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
 			pq_sendstring(&buf, payload);
diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c
index e87588040fa..64550f9c680 100644
--- a/src/backend/commands/copy.c
+++ b/src/backend/commands/copy.c
@@ -357,9 +357,9 @@ SendCopyBegin(CopyState cstate)
 
 		pq_beginmessage(&buf, 'H');
 		pq_sendbyte(&buf, format);	/* overall format */
-		pq_sendint(&buf, natts, 2);
+		pq_sendint16(&buf, natts);
 		for (i = 0; i < natts; i++)
-			pq_sendint(&buf, format, 2);	/* per-column formats */
+			pq_sendint16(&buf, format);	/* per-column formats */
 		pq_endmessage(&buf);
 		cstate->copy_dest = COPY_NEW_FE;
 	}
@@ -390,9 +390,9 @@ ReceiveCopyBegin(CopyState cstate)
 
 		pq_beginmessage(&buf, 'G');
 		pq_sendbyte(&buf, format);	/* overall format */
-		pq_sendint(&buf, natts, 2);
+		pq_sendint16(&buf, natts);
 		for (i = 0; i < natts; i++)
-			pq_sendint(&buf, format, 2);	/* per-column formats */
+			pq_sendint16(&buf, format);	/* per-column formats */
 		pq_endmessage(&buf);
 		cstate->copy_dest = COPY_NEW_FE;
 		cstate->fe_msgbuf = makeStringInfo();
diff --git a/src/backend/libpq/auth.c b/src/backend/libpq/auth.c
index 480e344eb3a..3b3a932a7d8 100644
--- a/src/backend/libpq/auth.c
+++ b/src/backend/libpq/auth.c
@@ -613,7 +613,7 @@ sendAuthRequest(Port *port, AuthRequest areq, char *extradata, int extralen)
 	CHECK_FOR_INTERRUPTS();
 
 	pq_beginmessage(&buf, 'R');
-	pq_sendint(&buf, (int32) areq, sizeof(int32));
+	pq_sendint32(&buf, (int32) areq);
 	if (extralen > 0)
 		pq_sendbytes(&buf, extradata, extralen);
 
diff --git a/src/backend/replication/basebackup.c b/src/backend/replication/basebackup.c
index c3b9bddc8fe..75029b0def9 100644
--- a/src/backend/replication/basebackup.c
+++ b/src/backend/replication/basebackup.c
@@ -274,7 +274,7 @@ perform_base_backup(basebackup_options *opt, DIR *tblspcdir)
 			/* Send CopyOutResponse message */
 			pq_beginmessage(&buf, 'H');
 			pq_sendbyte(&buf, 0);	/* overall format */
-			pq_sendint(&buf, 0, 2); /* natts */
+			pq_sendint16(&buf, 0); /* natts */
 			pq_endmessage(&buf);
 
 			if (ti->path == NULL)
@@ -722,7 +722,7 @@ send_int8_string(StringInfoData *buf, int64 intval)
 	char		is[32];
 
 	sprintf(is, INT64_FORMAT, intval);
-	pq_sendint(buf, strlen(is), 4);
+	pq_sendint32(buf, strlen(is));
 	pq_sendbytes(buf, is, strlen(is));
 }
 
@@ -734,34 +734,34 @@ SendBackupHeader(List *tablespaces)
 
 	/* Construct and send the directory information */
 	pq_beginmessage(&buf, 'T'); /* RowDescription */
-	pq_sendint(&buf, 3, 2);		/* 3 fields */
+	pq_sendint16(&buf, 3);		/* 3 fields */
 
 	/* First field - spcoid */
 	pq_sendstring(&buf, "spcoid");
-	pq_sendint(&buf, 0, 4);		/* table oid */
-	pq_sendint(&buf, 0, 2);		/* attnum */
-	pq_sendint(&buf, OIDOID, 4);	/* type oid */
-	pq_sendint(&buf, 4, 2);		/* typlen */
-	pq_sendint(&buf, 0, 4);		/* typmod */
-	pq_sendint(&buf, 0, 2);		/* format code */
+	pq_sendint32(&buf, 0);		/* table oid */
+	pq_sendint16(&buf, 0);		/* attnum */
+	pq_sendint32(&buf, OIDOID);	/* type oid */
+	pq_sendint16(&buf, 4);		/* typlen */
+	pq_sendint32(&buf, 0);		/* typmod */
+	pq_sendint16(&buf, 0);		/* format code */
 
 	/* Second field - spcpath */
 	pq_sendstring(&buf, "spclocation");
-	pq_sendint(&buf, 0, 4);
-	pq_sendint(&buf, 0, 2);
-	pq_sendint(&buf, TEXTOID, 4);
-	pq_sendint(&buf, -1, 2);
-	pq_sendint(&buf, 0, 4);
-	pq_sendint(&buf, 0, 2);
+	pq_sendint32(&buf, 0);
+	pq_sendint16(&buf, 0);
+	pq_sendint32(&buf, TEXTOID);
+	pq_sendint16(&buf, -1);
+	pq_sendint32(&buf, 0);
+	pq_sendint16(&buf, 0);
 
 	/* Third field - size */
 	pq_sendstring(&buf, "size");
-	pq_sendint(&buf, 0, 4);
-	pq_sendint(&buf, 0, 2);
-	pq_sendint(&buf, INT8OID, 4);
-	pq_sendint(&buf, 8, 2);
-	pq_sendint(&buf, 0, 4);
-	pq_sendint(&buf, 0, 2);
+	pq_sendint32(&buf, 0);
+	pq_sendint16(&buf, 0);
+	pq_sendint32(&buf, INT8OID);
+	pq_sendint16(&buf, 8);
+	pq_sendint32(&buf, 0);
+	pq_sendint16(&buf, 0);
 	pq_endmessage(&buf);
 
 	foreach(lc, tablespaces)
@@ -770,28 +770,28 @@ SendBackupHeader(List *tablespaces)
 
 		/* Send one datarow message */
 		pq_beginmessage(&buf, 'D');
-		pq_sendint(&buf, 3, 2); /* number of columns */
+		pq_sendint16(&buf, 3); /* number of columns */
 		if (ti->path == NULL)
 		{
-			pq_sendint(&buf, -1, 4);	/* Length = -1 ==> NULL */
-			pq_sendint(&buf, -1, 4);
+			pq_sendint32(&buf, -1);	/* Length = -1 ==> NULL */
+			pq_sendint32(&buf, -1);
 		}
 		else
 		{
 			Size		len;
 
 			len = strlen(ti->oid);
-			pq_sendint(&buf, len, 4);
+			pq_sendint32(&buf, len);
 			pq_sendbytes(&buf, ti->oid, len);
 
 			len = strlen(ti->path);
-			pq_sendint(&buf, len, 4);
+			pq_sendint32(&buf, len);
 			pq_sendbytes(&buf, ti->path, len);
 		}
 		if (ti->size >= 0)
 			send_int8_string(&buf, ti->size / 1024);
 		else
-			pq_sendint(&buf, -1, 4);	/* NULL */
+			pq_sendint32(&buf, -1);	/* NULL */
 
 		pq_endmessage(&buf);
 	}
@@ -812,42 +812,42 @@ SendXlogRecPtrResult(XLogRecPtr ptr, TimeLineID tli)
 	Size		len;
 
 	pq_beginmessage(&buf, 'T'); /* RowDescription */
-	pq_sendint(&buf, 2, 2);		/* 2 fields */
+	pq_sendint16(&buf, 2);		/* 2 fields */
 
 	/* Field headers */
 	pq_sendstring(&buf, "recptr");
-	pq_sendint(&buf, 0, 4);		/* table oid */
-	pq_sendint(&buf, 0, 2);		/* attnum */
-	pq_sendint(&buf, TEXTOID, 4);	/* type oid */
-	pq_sendint(&buf, -1, 2);
-	pq_sendint(&buf, 0, 4);
-	pq_sendint(&buf, 0, 2);
+	pq_sendint32(&buf, 0);		/* table oid */
+	pq_sendint16(&buf, 0);		/* attnum */
+	pq_sendint32(&buf, TEXTOID);	/* type oid */
+	pq_sendint16(&buf, -1);
+	pq_sendint32(&buf, 0);
+	pq_sendint16(&buf, 0);
 
 	pq_sendstring(&buf, "tli");
-	pq_sendint(&buf, 0, 4);		/* table oid */
-	pq_sendint(&buf, 0, 2);		/* attnum */
+	pq_sendint32(&buf, 0);		/* table oid */
+	pq_sendint16(&buf, 0);		/* attnum */
 
 	/*
 	 * int8 may seem like a surprising data type for this, but in theory int4
 	 * would not be wide enough for this, as TimeLineID is unsigned.
 	 */
-	pq_sendint(&buf, INT8OID, 4);	/* type oid */
-	pq_sendint(&buf, -1, 2);
-	pq_sendint(&buf, 0, 4);
-	pq_sendint(&buf, 0, 2);
+	pq_sendint32(&buf, INT8OID);	/* type oid */
+	pq_sendint16(&buf, -1);
+	pq_sendint32(&buf, 0);
+	pq_sendint16(&buf, 0);
 	pq_endmessage(&buf);
 
 	/* Data row */
 	pq_beginmessage(&buf, 'D');
-	pq_sendint(&buf, 2, 2);		/* number of columns */
+	pq_sendint16(&buf, 2);		/* number of columns */
 
 	len = snprintf(str, sizeof(str),
 				   "%X/%X", (uint32) (ptr >> 32), (uint32) ptr);
-	pq_sendint(&buf, len, 4);
+	pq_sendint32(&buf, len);
 	pq_sendbytes(&buf, str, len);
 
 	len = snprintf(str, sizeof(str), "%u", tli);
-	pq_sendint(&buf, len, 4);
+	pq_sendint32(&buf, len);
 	pq_sendbytes(&buf, str, len);
 
 	pq_endmessage(&buf);
diff --git a/src/backend/replication/logical/proto.c b/src/backend/replication/logical/proto.c
index f19649b113c..9b126b29570 100644
--- a/src/backend/replication/logical/proto.c
+++ b/src/backend/replication/logical/proto.c
@@ -47,7 +47,7 @@ logicalrep_write_begin(StringInfo out, ReorderBufferTXN *txn)
 	/* fixed fields */
 	pq_sendint64(out, txn->final_lsn);
 	pq_sendint64(out, txn->commit_time);
-	pq_sendint(out, txn->xid, 4);
+	pq_sendint32(out, txn->xid);
 }
 
 /*
@@ -145,7 +145,7 @@ logicalrep_write_insert(StringInfo out, Relation rel, HeapTuple newtuple)
 		   rel->rd_rel->relreplident == REPLICA_IDENTITY_INDEX);
 
 	/* use Oid as relation identifier */
-	pq_sendint(out, RelationGetRelid(rel), 4);
+	pq_sendint32(out, RelationGetRelid(rel));
 
 	pq_sendbyte(out, 'N');		/* new tuple follows */
 	logicalrep_write_tuple(out, rel, newtuple);
@@ -189,7 +189,7 @@ logicalrep_write_update(StringInfo out, Relation rel, HeapTuple oldtuple,
 		   rel->rd_rel->relreplident == REPLICA_IDENTITY_INDEX);
 
 	/* use Oid as relation identifier */
-	pq_sendint(out, RelationGetRelid(rel), 4);
+	pq_sendint32(out, RelationGetRelid(rel));
 
 	if (oldtuple != NULL)
 	{
@@ -258,7 +258,7 @@ logicalrep_write_delete(StringInfo out, Relation rel, HeapTuple oldtuple)
 	pq_sendbyte(out, 'D');		/* action DELETE */
 
 	/* use Oid as relation identifier */
-	pq_sendint(out, RelationGetRelid(rel), 4);
+	pq_sendint32(out, RelationGetRelid(rel));
 
 	if (rel->rd_rel->relreplident == REPLICA_IDENTITY_FULL)
 		pq_sendbyte(out, 'O');	/* old tuple follows */
@@ -303,7 +303,7 @@ logicalrep_write_rel(StringInfo out, Relation rel)
 	pq_sendbyte(out, 'R');		/* sending RELATION */
 
 	/* use Oid as relation identifier */
-	pq_sendint(out, RelationGetRelid(rel), 4);
+	pq_sendint32(out, RelationGetRelid(rel));
 
 	/* send qualified relation name */
 	logicalrep_write_namespace(out, RelationGetNamespace(rel));
@@ -360,7 +360,7 @@ logicalrep_write_typ(StringInfo out, Oid typoid)
 	typtup = (Form_pg_type) GETSTRUCT(tup);
 
 	/* use Oid as relation identifier */
-	pq_sendint(out, typoid, 4);
+	pq_sendint32(out, typoid);
 
 	/* send qualified type name */
 	logicalrep_write_namespace(out, typtup->typnamespace);
@@ -402,7 +402,7 @@ logicalrep_write_tuple(StringInfo out, Relation rel, HeapTuple tuple)
 			continue;
 		nliveatts++;
 	}
-	pq_sendint(out, nliveatts, 2);
+	pq_sendint16(out, nliveatts);
 
 	/* try to allocate enough memory from the get-go */
 	enlargeStringInfo(out, tuple->t_len +
@@ -522,7 +522,7 @@ logicalrep_write_attrs(StringInfo out, Relation rel)
 			continue;
 		nliveatts++;
 	}
-	pq_sendint(out, nliveatts, 2);
+	pq_sendint16(out, nliveatts);
 
 	/* fetch bitmap of REPLICATION IDENTITY attributes */
 	replidentfull = (rel->rd_rel->relreplident == REPLICA_IDENTITY_FULL);
@@ -551,10 +551,10 @@ logicalrep_write_attrs(StringInfo out, Relation rel)
 		pq_sendstring(out, NameStr(att->attname));
 
 		/* attribute type id */
-		pq_sendint(out, (int) att->atttypid, sizeof(att->atttypid));
+		pq_sendint32(out, (int) att->atttypid);
 
 		/* attribute mode */
-		pq_sendint(out, att->atttypmod, sizeof(att->atttypmod));
+		pq_sendint32(out, att->atttypmod);
 	}
 
 	bms_free(idattrs);
diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c
index 1bf9be673b7..fe4e0859389 100644
--- a/src/backend/replication/walreceiver.c
+++ b/src/backend/replication/walreceiver.c
@@ -1272,10 +1272,10 @@ XLogWalRcvSendHSFeedback(bool immed)
 	resetStringInfo(&reply_message);
 	pq_sendbyte(&reply_message, 'h');
 	pq_sendint64(&reply_message, GetCurrentTimestamp());
-	pq_sendint(&reply_message, xmin, 4);
-	pq_sendint(&reply_message, xmin_epoch, 4);
-	pq_sendint(&reply_message, catalog_xmin, 4);
-	pq_sendint(&reply_message, catalog_xmin_epoch, 4);
+	pq_sendint32(&reply_message, xmin);
+	pq_sendint32(&reply_message, xmin_epoch);
+	pq_sendint32(&reply_message, catalog_xmin);
+	pq_sendint32(&reply_message, catalog_xmin_epoch);
 	walrcv_send(wrconn, reply_message.data, reply_message.len);
 	if (TransactionIdIsValid(xmin) || TransactionIdIsValid(catalog_xmin))
 		master_has_standby_xmin = true;
diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c
index 6ec4e631612..fa1db748b5e 100644
--- a/src/backend/replication/walsender.c
+++ b/src/backend/replication/walsender.c
@@ -444,32 +444,32 @@ SendTimeLineHistory(TimeLineHistoryCmd *cmd)
 
 	/* Send a RowDescription message */
 	pq_beginmessage(&buf, 'T');
-	pq_sendint(&buf, 2, 2);		/* 2 fields */
+	pq_sendint16(&buf, 2);		/* 2 fields */
 
 	/* first field */
 	pq_sendstring(&buf, "filename");	/* col name */
-	pq_sendint(&buf, 0, 4);		/* table oid */
-	pq_sendint(&buf, 0, 2);		/* attnum */
-	pq_sendint(&buf, TEXTOID, 4);	/* type oid */
-	pq_sendint(&buf, -1, 2);	/* typlen */
-	pq_sendint(&buf, 0, 4);		/* typmod */
-	pq_sendint(&buf, 0, 2);		/* format code */
+	pq_sendint32(&buf, 0);		/* table oid */
+	pq_sendint16(&buf, 0);		/* attnum */
+	pq_sendint32(&buf, TEXTOID);	/* type oid */
+	pq_sendint16(&buf, -1);		/* typlen */
+	pq_sendint32(&buf, 0);		/* typmod */
+	pq_sendint16(&buf, 0);		/* format code */
 
 	/* second field */
 	pq_sendstring(&buf, "content"); /* col name */
-	pq_sendint(&buf, 0, 4);		/* table oid */
-	pq_sendint(&buf, 0, 2);		/* attnum */
-	pq_sendint(&buf, BYTEAOID, 4);	/* type oid */
-	pq_sendint(&buf, -1, 2);	/* typlen */
-	pq_sendint(&buf, 0, 4);		/* typmod */
-	pq_sendint(&buf, 0, 2);		/* format code */
+	pq_sendint32(&buf, 0);		/* table oid */
+	pq_sendint16(&buf, 0);		/* attnum */
+	pq_sendint32(&buf, BYTEAOID);	/* type oid */
+	pq_sendint16(&buf, -1);		/* typlen */
+	pq_sendint32(&buf, 0);		/* typmod */
+	pq_sendint16(&buf, 0);		/* format code */
 	pq_endmessage(&buf);
 
 	/* Send a DataRow message */
 	pq_beginmessage(&buf, 'D');
-	pq_sendint(&buf, 2, 2);		/* # of columns */
+	pq_sendint16(&buf, 2);		/* # of columns */
 	len = strlen(histfname);
-	pq_sendint(&buf, len, 4);	/* col1 len */
+	pq_sendint32(&buf, len);	/* col1 len */
 	pq_sendbytes(&buf, histfname, len);
 
 	fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
@@ -489,7 +489,7 @@ SendTimeLineHistory(TimeLineHistoryCmd *cmd)
 				(errcode_for_file_access(),
 				 errmsg("could not seek to beginning of file \"%s\": %m", path)));
 
-	pq_sendint(&buf, histfilelen, 4);	/* col2 len */
+	pq_sendint32(&buf, histfilelen);	/* col2 len */
 
 	bytesleft = histfilelen;
 	while (bytesleft > 0)
@@ -646,7 +646,7 @@ StartReplication(StartReplicationCmd *cmd)
 		/* Send a CopyBothResponse message, and start streaming */
 		pq_beginmessage(&buf, 'W');
 		pq_sendbyte(&buf, 0);
-		pq_sendint(&buf, 0, 2);
+		pq_sendint16(&buf, 0);
 		pq_endmessage(&buf);
 		pq_flush();
 
@@ -1065,7 +1065,7 @@ StartLogicalReplication(StartReplicationCmd *cmd)
 	/* Send a CopyBothResponse message, and start streaming */
 	pq_beginmessage(&buf, 'W');
 	pq_sendbyte(&buf, 0);
-	pq_sendint(&buf, 0, 2);
+	pq_sendint16(&buf, 0);
 	pq_endmessage(&buf);
 	pq_flush();
 
diff --git a/src/backend/tcop/fastpath.c b/src/backend/tcop/fastpath.c
index 8101ae74e0b..a434f7f857f 100644
--- a/src/backend/tcop/fastpath.c
+++ b/src/backend/tcop/fastpath.c
@@ -143,7 +143,7 @@ SendFunctionResult(Datum retval, bool isnull, Oid rettype, int16 format)
 	if (isnull)
 	{
 		if (newstyle)
-			pq_sendint(&buf, -1, 4);
+			pq_sendint32(&buf, -1);
 	}
 	else
 	{
@@ -169,7 +169,7 @@ SendFunctionResult(Datum retval, bool isnull, Oid rettype, int16 format)
 
 			getTypeBinaryOutputInfo(rettype, &typsend, &typisvarlena);
 			outputbytes = OidSendFunctionCall(typsend, retval);
-			pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
+			pq_sendint32(&buf, VARSIZE(outputbytes) - VARHDRSZ);
 			pq_sendbytes(&buf, VARDATA(outputbytes),
 						 VARSIZE(outputbytes) - VARHDRSZ);
 			pfree(outputbytes);
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 36bb8c24fb4..92442a80349 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -2377,13 +2377,13 @@ exec_describe_statement_message(const char *stmt_name)
 	 */
 	pq_beginmessage_reuse(&row_description_buf, 't'); /* parameter description
 													   * message type */
-	pq_sendint(&row_description_buf, psrc->num_params, 2);
+	pq_sendint16(&row_description_buf, psrc->num_params);
 
 	for (i = 0; i < psrc->num_params; i++)
 	{
 		Oid			ptype = psrc->param_types[i];
 
-		pq_sendint(&row_description_buf, (int) ptype, 4);
+		pq_sendint32(&row_description_buf, (int) ptype);
 	}
 	pq_endmessage_reuse(&row_description_buf);
 
@@ -3821,8 +3821,8 @@ PostgresMain(int argc, char *argv[],
 		StringInfoData buf;
 
 		pq_beginmessage(&buf, 'K');
-		pq_sendint(&buf, (int32) MyProcPid, sizeof(int32));
-		pq_sendint(&buf, (int32) MyCancelKey, sizeof(int32));
+		pq_sendint32(&buf, (int32) MyProcPid);
+		pq_sendint32(&buf, (int32) MyCancelKey);
 		pq_endmessage(&buf);
 		/* Need not flush since ReadyForQuery will do it. */
 	}
diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c
index ca04b13e825..b4c31ef65c2 100644
--- a/src/backend/utils/adt/arrayfuncs.c
+++ b/src/backend/utils/adt/arrayfuncs.c
@@ -1590,13 +1590,13 @@ array_send(PG_FUNCTION_ARGS)
 	pq_begintypsend(&buf);
 
 	/* Send the array header information */
-	pq_sendint(&buf, ndim, 4);
-	pq_sendint(&buf, AARR_HASNULL(v) ? 1 : 0, 4);
-	pq_sendint(&buf, element_type, sizeof(Oid));
+	pq_sendint32(&buf, ndim);
+	pq_sendint32(&buf, AARR_HASNULL(v) ? 1 : 0);
+	pq_sendint32(&buf, element_type);
 	for (i = 0; i < ndim; i++)
 	{
-		pq_sendint(&buf, dim[i], 4);
-		pq_sendint(&buf, lb[i], 4);
+		pq_sendint32(&buf, dim[i]);
+		pq_sendint32(&buf, lb[i]);
 	}
 
 	/* Send the array elements using the element's own sendproc */
@@ -1614,14 +1614,14 @@ array_send(PG_FUNCTION_ARGS)
 		if (isnull)
 		{
 			/* -1 length means a NULL */
-			pq_sendint(&buf, -1, 4);
+			pq_sendint32(&buf, -1);
 		}
 		else
 		{
 			bytea	   *outputbytes;
 
 			outputbytes = SendFunctionCall(&my_extra->proc, itemvalue);
-			pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
+			pq_sendint32(&buf, VARSIZE(outputbytes) - VARHDRSZ);
 			pq_sendbytes(&buf, VARDATA(outputbytes),
 						 VARSIZE(outputbytes) - VARHDRSZ);
 			pfree(outputbytes);
diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c
index 0992bb3fdd0..04e737d0808 100644
--- a/src/backend/utils/adt/date.c
+++ b/src/backend/utils/adt/date.c
@@ -239,7 +239,7 @@ date_send(PG_FUNCTION_ARGS)
 	StringInfoData buf;
 
 	pq_begintypsend(&buf);
-	pq_sendint(&buf, date, sizeof(date));
+	pq_sendint32(&buf, date);
 	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
 }
 
@@ -2049,7 +2049,7 @@ timetz_send(PG_FUNCTION_ARGS)
 
 	pq_begintypsend(&buf);
 	pq_sendint64(&buf, time->time);
-	pq_sendint(&buf, time->zone, sizeof(time->zone));
+	pq_sendint32(&buf, time->zone);
 	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
 }
 
diff --git a/src/backend/utils/adt/geo_ops.c b/src/backend/utils/adt/geo_ops.c
index 0348855b11c..e13389a6cc7 100644
--- a/src/backend/utils/adt/geo_ops.c
+++ b/src/backend/utils/adt/geo_ops.c
@@ -1433,7 +1433,7 @@ path_send(PG_FUNCTION_ARGS)
 
 	pq_begintypsend(&buf);
 	pq_sendbyte(&buf, path->closed ? 1 : 0);
-	pq_sendint(&buf, path->npts, sizeof(int32));
+	pq_sendint32(&buf, path->npts);
 	for (i = 0; i < path->npts; i++)
 	{
 		pq_sendfloat8(&buf, path->p[i].x);
@@ -3514,7 +3514,7 @@ poly_send(PG_FUNCTION_ARGS)
 	int32		i;
 
 	pq_begintypsend(&buf);
-	pq_sendint(&buf, poly->npts, sizeof(int32));
+	pq_sendint32(&buf, poly->npts);
 	for (i = 0; i < poly->npts; i++)
 	{
 		pq_sendfloat8(&buf, poly->p[i].x);
diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c
index 96ef25b900e..4cd8960b3fc 100644
--- a/src/backend/utils/adt/int.c
+++ b/src/backend/utils/adt/int.c
@@ -99,7 +99,7 @@ int2send(PG_FUNCTION_ARGS)
 	StringInfoData buf;
 
 	pq_begintypsend(&buf);
-	pq_sendint(&buf, arg1, sizeof(int16));
+	pq_sendint16(&buf, arg1);
 	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
 }
 
@@ -304,7 +304,7 @@ int4send(PG_FUNCTION_ARGS)
 	StringInfoData buf;
 
 	pq_begintypsend(&buf);
-	pq_sendint(&buf, arg1, sizeof(int32));
+	pq_sendint32(&buf, arg1);
 	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
 }
 
diff --git a/src/backend/utils/adt/jsonb.c b/src/backend/utils/adt/jsonb.c
index 95db8955389..771c05120bb 100644
--- a/src/backend/utils/adt/jsonb.c
+++ b/src/backend/utils/adt/jsonb.c
@@ -154,7 +154,7 @@ jsonb_send(PG_FUNCTION_ARGS)
 	(void) JsonbToCString(jtext, &jb->root, VARSIZE(jb));
 
 	pq_begintypsend(&buf);
-	pq_sendint(&buf, version, 1);
+	pq_sendint8(&buf, version);
 	pq_sendtext(&buf, jtext->data, jtext->len);
 	pfree(jtext->data);
 	pfree(jtext);
diff --git a/src/backend/utils/adt/nabstime.c b/src/backend/utils/adt/nabstime.c
index 2c5948052d3..2bca39a90cc 100644
--- a/src/backend/utils/adt/nabstime.c
+++ b/src/backend/utils/adt/nabstime.c
@@ -315,7 +315,7 @@ abstimesend(PG_FUNCTION_ARGS)
 	StringInfoData buf;
 
 	pq_begintypsend(&buf);
-	pq_sendint(&buf, time, sizeof(time));
+	pq_sendint32(&buf, time);
 	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
 }
 
@@ -674,7 +674,7 @@ reltimesend(PG_FUNCTION_ARGS)
 	StringInfoData buf;
 
 	pq_begintypsend(&buf);
-	pq_sendint(&buf, time, sizeof(time));
+	pq_sendint32(&buf, time);
 	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
 }
 
@@ -794,9 +794,9 @@ tintervalsend(PG_FUNCTION_ARGS)
 	StringInfoData buf;
 
 	pq_begintypsend(&buf);
-	pq_sendint(&buf, tinterval->status, sizeof(tinterval->status));
-	pq_sendint(&buf, tinterval->data[0], sizeof(tinterval->data[0]));
-	pq_sendint(&buf, tinterval->data[1], sizeof(tinterval->data[1]));
+	pq_sendint32(&buf, tinterval->status);
+	pq_sendint32(&buf, tinterval->data[0]);
+	pq_sendint32(&buf, tinterval->data[1]);
 	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
 }
 
diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c
index 48d95e90501..2cd14f34012 100644
--- a/src/backend/utils/adt/numeric.c
+++ b/src/backend/utils/adt/numeric.c
@@ -876,12 +876,12 @@ numeric_send(PG_FUNCTION_ARGS)
 
 	pq_begintypsend(&buf);
 
-	pq_sendint(&buf, x.ndigits, sizeof(int16));
-	pq_sendint(&buf, x.weight, sizeof(int16));
-	pq_sendint(&buf, x.sign, sizeof(int16));
-	pq_sendint(&buf, x.dscale, sizeof(int16));
+	pq_sendint16(&buf, x.ndigits);
+	pq_sendint16(&buf, x.weight);
+	pq_sendint16(&buf, x.sign);
+	pq_sendint16(&buf, x.dscale);
 	for (i = 0; i < x.ndigits; i++)
-		pq_sendint(&buf, x.digits[i], sizeof(NumericDigit));
+		pq_sendint16(&buf, x.digits[i]);
 
 	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
 }
@@ -3693,7 +3693,7 @@ numeric_avg_serialize(PG_FUNCTION_ARGS)
 	pq_sendbytes(&buf, VARDATA_ANY(sumX), VARSIZE_ANY_EXHDR(sumX));
 
 	/* maxScale */
-	pq_sendint(&buf, state->maxScale, 4);
+	pq_sendint32(&buf, state->maxScale);
 
 	/* maxScaleCount */
 	pq_sendint64(&buf, state->maxScaleCount);
@@ -3815,7 +3815,7 @@ numeric_serialize(PG_FUNCTION_ARGS)
 	pq_sendbytes(&buf, VARDATA_ANY(sumX2), VARSIZE_ANY_EXHDR(sumX2));
 
 	/* maxScale */
-	pq_sendint(&buf, state->maxScale, 4);
+	pq_sendint32(&buf, state->maxScale);
 
 	/* maxScaleCount */
 	pq_sendint64(&buf, state->maxScaleCount);
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 7baaa1dd4ee..87e87fe54d5 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -154,7 +154,7 @@ oidsend(PG_FUNCTION_ARGS)
 	StringInfoData buf;
 
 	pq_begintypsend(&buf);
-	pq_sendint(&buf, arg1, sizeof(Oid));
+	pq_sendint32(&buf, arg1);
 	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
 }
 
diff --git a/src/backend/utils/adt/rangetypes.c b/src/backend/utils/adt/rangetypes.c
index d0aa33c010b..e79f0dbfca6 100644
--- a/src/backend/utils/adt/rangetypes.c
+++ b/src/backend/utils/adt/rangetypes.c
@@ -272,7 +272,7 @@ range_send(PG_FUNCTION_ARGS)
 		uint32		bound_len = VARSIZE(bound) - VARHDRSZ;
 		char	   *bound_data = VARDATA(bound);
 
-		pq_sendint(buf, bound_len, 4);
+		pq_sendint32(buf, bound_len);
 		pq_sendbytes(buf, bound_data, bound_len);
 	}
 
@@ -283,7 +283,7 @@ range_send(PG_FUNCTION_ARGS)
 		uint32		bound_len = VARSIZE(bound) - VARHDRSZ;
 		char	   *bound_data = VARDATA(bound);
 
-		pq_sendint(buf, bound_len, 4);
+		pq_sendint32(buf, bound_len);
 		pq_sendbytes(buf, bound_data, bound_len);
 	}
 
diff --git a/src/backend/utils/adt/rowtypes.c b/src/backend/utils/adt/rowtypes.c
index 98fe00ff394..9b32db5d0ae 100644
--- a/src/backend/utils/adt/rowtypes.c
+++ b/src/backend/utils/adt/rowtypes.c
@@ -718,7 +718,7 @@ record_send(PG_FUNCTION_ARGS)
 		if (!TupleDescAttr(tupdesc, i)->attisdropped)
 			validcols++;
 	}
-	pq_sendint(&buf, validcols, 4);
+	pq_sendint32(&buf, validcols);
 
 	for (i = 0; i < ncolumns; i++)
 	{
@@ -732,12 +732,12 @@ record_send(PG_FUNCTION_ARGS)
 		if (att->attisdropped)
 			continue;
 
-		pq_sendint(&buf, column_type, sizeof(Oid));
+		pq_sendint32(&buf, column_type);
 
 		if (nulls[i])
 		{
 			/* emit -1 data length to signify a NULL */
-			pq_sendint(&buf, -1, 4);
+			pq_sendint32(&buf, -1);
 			continue;
 		}
 
@@ -756,7 +756,7 @@ record_send(PG_FUNCTION_ARGS)
 
 		attr = values[i];
 		outputbytes = SendFunctionCall(&column_info->proc, attr);
-		pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
+		pq_sendint32(&buf, VARSIZE(outputbytes) - VARHDRSZ);
 		pq_sendbytes(&buf, VARDATA(outputbytes),
 					 VARSIZE(outputbytes) - VARHDRSZ);
 	}
diff --git a/src/backend/utils/adt/tid.c b/src/backend/utils/adt/tid.c
index 083f7d60a7b..854097dd583 100644
--- a/src/backend/utils/adt/tid.c
+++ b/src/backend/utils/adt/tid.c
@@ -149,10 +149,8 @@ tidsend(PG_FUNCTION_ARGS)
 	StringInfoData buf;
 
 	pq_begintypsend(&buf);
-	pq_sendint(&buf, ItemPointerGetBlockNumberNoCheck(itemPtr),
-			   sizeof(BlockNumber));
-	pq_sendint(&buf, ItemPointerGetOffsetNumberNoCheck(itemPtr),
-			   sizeof(OffsetNumber));
+	pq_sendint32(&buf, ItemPointerGetBlockNumberNoCheck(itemPtr));
+	pq_sendint16(&buf, ItemPointerGetOffsetNumberNoCheck(itemPtr));
 	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
 }
 
diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c
index b11d452fc8a..5797aaad34c 100644
--- a/src/backend/utils/adt/timestamp.c
+++ b/src/backend/utils/adt/timestamp.c
@@ -1009,8 +1009,8 @@ interval_send(PG_FUNCTION_ARGS)
 
 	pq_begintypsend(&buf);
 	pq_sendint64(&buf, interval->time);
-	pq_sendint(&buf, interval->day, sizeof(interval->day));
-	pq_sendint(&buf, interval->month, sizeof(interval->month));
+	pq_sendint32(&buf, interval->day);
+	pq_sendint32(&buf, interval->month);
 	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
 }
 
diff --git a/src/backend/utils/adt/tsquery.c b/src/backend/utils/adt/tsquery.c
index fdb041971e5..5cdfe4d7322 100644
--- a/src/backend/utils/adt/tsquery.c
+++ b/src/backend/utils/adt/tsquery.c
@@ -952,23 +952,22 @@ tsquerysend(PG_FUNCTION_ARGS)
 
 	pq_begintypsend(&buf);
 
-	pq_sendint(&buf, query->size, sizeof(uint32));
+	pq_sendint32(&buf, query->size);
 	for (i = 0; i < query->size; i++)
 	{
-		pq_sendint(&buf, item->type, sizeof(item->type));
+		pq_sendint8(&buf, item->type);
 
 		switch (item->type)
 		{
 			case QI_VAL:
-				pq_sendint(&buf, item->qoperand.weight, sizeof(uint8));
-				pq_sendint(&buf, item->qoperand.prefix, sizeof(uint8));
+				pq_sendint8(&buf, item->qoperand.weight);
+				pq_sendint8(&buf, item->qoperand.prefix);
 				pq_sendstring(&buf, GETOPERAND(query) + item->qoperand.distance);
 				break;
 			case QI_OPR:
-				pq_sendint(&buf, item->qoperator.oper, sizeof(item->qoperator.oper));
+				pq_sendint8(&buf, item->qoperator.oper);
 				if (item->qoperator.oper == OP_PHRASE)
-					pq_sendint(&buf, item->qoperator.distance,
-							   sizeof(item->qoperator.distance));
+					pq_sendint16(&buf, item->qoperator.distance);
 				break;
 			default:
 				elog(ERROR, "unrecognized tsquery node type: %d", item->type);
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index 6f66c1f58ce..b0a9217d1e3 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -410,7 +410,7 @@ tsvectorsend(PG_FUNCTION_ARGS)
 
 	pq_begintypsend(&buf);
 
-	pq_sendint(&buf, vec->size, sizeof(int32));
+	pq_sendint32(&buf, vec->size);
 	for (i = 0; i < vec->size; i++)
 	{
 		uint16		npos;
@@ -423,14 +423,14 @@ tsvectorsend(PG_FUNCTION_ARGS)
 		pq_sendbyte(&buf, '\0');
 
 		npos = POSDATALEN(vec, weptr);
-		pq_sendint(&buf, npos, sizeof(uint16));
+		pq_sendint16(&buf, npos);
 
 		if (npos > 0)
 		{
 			WordEntryPos *wepptr = POSDATAPTR(vec, weptr);
 
 			for (j = 0; j < npos; j++)
-				pq_sendint(&buf, wepptr[j], sizeof(WordEntryPos));
+				pq_sendint16(&buf, wepptr[j]);
 		}
 		weptr++;
 	}
diff --git a/src/backend/utils/adt/txid.c b/src/backend/utils/adt/txid.c
index 1e38ca2aa5e..9d312edf04f 100644
--- a/src/backend/utils/adt/txid.c
+++ b/src/backend/utils/adt/txid.c
@@ -640,7 +640,7 @@ txid_snapshot_send(PG_FUNCTION_ARGS)
 	uint32		i;
 
 	pq_begintypsend(&buf);
-	pq_sendint(&buf, snap->nxip, 4);
+	pq_sendint32(&buf, snap->nxip);
 	pq_sendint64(&buf, snap->xmin);
 	pq_sendint64(&buf, snap->xmax);
 	for (i = 0; i < snap->nxip; i++)
diff --git a/src/backend/utils/adt/varbit.c b/src/backend/utils/adt/varbit.c
index 0cf1c6f6d60..478fab9bfce 100644
--- a/src/backend/utils/adt/varbit.c
+++ b/src/backend/utils/adt/varbit.c
@@ -665,7 +665,7 @@ varbit_send(PG_FUNCTION_ARGS)
 	StringInfoData buf;
 
 	pq_begintypsend(&buf);
-	pq_sendint(&buf, VARBITLEN(s), sizeof(int32));
+	pq_sendint32(&buf, VARBITLEN(s));
 	pq_sendbytes(&buf, (char *) VARBITS(s), VARBITBYTES(s));
 	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
 }
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 2051709fdef..67c32ac6193 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -68,7 +68,7 @@ xidsend(PG_FUNCTION_ARGS)
 	StringInfoData buf;
 
 	pq_begintypsend(&buf);
-	pq_sendint(&buf, arg1, sizeof(arg1));
+	pq_sendint32(&buf, arg1);
 	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
 }
 
@@ -196,7 +196,7 @@ cidsend(PG_FUNCTION_ARGS)
 	StringInfoData buf;
 
 	pq_begintypsend(&buf);
-	pq_sendint(&buf, arg1, sizeof(arg1));
+	pq_sendint32(&buf, arg1);
 	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
 }
 
-- 
2.14.1.536.g6867272d5b.dirty

-- 
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