diff --git a/src/backend/utils/adt/encode.c b/src/backend/utils/adt/encode.c
index b8d9ec7e00..8ae2bd58a3 100644
--- a/src/backend/utils/adt/encode.c
+++ b/src/backend/utils/adt/encode.c
@@ -20,7 +20,7 @@
 
 struct pg_encoding
 {
-	unsigned	(*encode_len) (const char *data, unsigned dlen);
+	Size		(*encode_len) (const char *data, unsigned dlen);
 	unsigned	(*decode_len) (const char *data, unsigned dlen);
 	unsigned	(*encode) (const char *data, unsigned dlen, char *res);
 	unsigned	(*decode) (const char *data, unsigned dlen, char *res);
@@ -40,8 +40,8 @@ binary_encode(PG_FUNCTION_ARGS)
 	text	   *result;
 	char	   *namebuf;
 	int			datalen,
-				resultlen,
 				res;
+	Size		resultlen;
 	const struct pg_encoding *enc;
 
 	datalen = VARSIZE_ANY_EXHDR(data);
@@ -60,7 +60,7 @@ binary_encode(PG_FUNCTION_ARGS)
 	res = enc->encode(VARDATA_ANY(data), datalen, VARDATA(result));
 
 	/* Make this FATAL 'cause we've trodden on memory ... */
-	if (res > resultlen)
+	if ((Size)res > resultlen)
 		elog(FATAL, "overflow - encode estimate too small");
 
 	SET_VARSIZE(result, VARHDRSZ + res);
@@ -76,8 +76,8 @@ binary_decode(PG_FUNCTION_ARGS)
 	bytea	   *result;
 	char	   *namebuf;
 	int			datalen,
-				resultlen,
 				res;
+	unsigned	resultlen;
 	const struct pg_encoding *enc;
 
 	datalen = VARSIZE_ANY_EXHDR(data);
@@ -184,10 +184,10 @@ hex_decode(const char *src, unsigned len, char *dst)
 	return p - dst;
 }
 
-static unsigned
+static Size
 hex_enc_len(const char *src, unsigned srclen)
 {
-	return srclen << 1;
+	return (Size)(srclen << 1);
 }
 
 static unsigned
@@ -331,11 +331,11 @@ pg_base64_decode(const char *src, unsigned len, char *dst)
 }
 
 
-static unsigned
+static Size
 pg_base64_enc_len(const char *src, unsigned srclen)
 {
 	/* 3 bytes will be converted to 4, linefeed after 76 chars */
-	return (srclen + 2) * 4 / 3 + srclen / (76 * 3 / 4);
+	return (Size)((srclen + 2) * 4 / 3 + srclen / (76 * 3 / 4));
 }
 
 static unsigned
@@ -448,11 +448,11 @@ esc_decode(const char *src, unsigned srclen, char *dst)
 	return len;
 }
 
-static unsigned
+static Size
 esc_enc_len(const char *src, unsigned srclen)
 {
 	const char *end = src + srclen;
-	int			len = 0;
+	Size		len = 0;
 
 	while (src < end)
 	{
diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c
index 907b5ab7b0..5fe7125629 100644
--- a/src/backend/utils/adt/varlena.c
+++ b/src/backend/utils/adt/varlena.c
@@ -388,7 +388,7 @@ byteaout(PG_FUNCTION_ARGS)
 	{
 		/* Print traditional escaped format */
 		char	   *vp;
-		int			len;
+		Size		len;
 		int			i;
 
 		len = 1;				/* empty string has 1 char */
@@ -3458,15 +3458,15 @@ byteaGetBit(PG_FUNCTION_ARGS)
 	int32		n = PG_GETARG_INT32(1);
 	int			byteNo,
 				bitNo;
-	int			len;
+	Size		len;
 	int			byte;
 
-	len = VARSIZE_ANY_EXHDR(v);
+	len = (Size)(VARSIZE_ANY_EXHDR(v));
 
-	if (n < 0 || n >= len * 8)
+	if (n < 0 || (Size)n >= len * 8)
 		ereport(ERROR,
 				(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
-				 errmsg("index %d out of valid range, 0..%d",
+				 errmsg("index %d out of valid range, 0..%ld",
 						n, len * 8 - 1)));
 
 	byteNo = n / 8;
@@ -3526,18 +3526,18 @@ byteaSetBit(PG_FUNCTION_ARGS)
 	bytea	   *res = PG_GETARG_BYTEA_P_COPY(0);
 	int32		n = PG_GETARG_INT32(1);
 	int32		newBit = PG_GETARG_INT32(2);
-	int			len;
+	Size		len;
 	int			oldByte,
 				newByte;
 	int			byteNo,
 				bitNo;
 
-	len = VARSIZE(res) - VARHDRSZ;
+	len = (Size)(VARSIZE(res) - VARHDRSZ);
 
-	if (n < 0 || n >= len * 8)
+	if (n < 0 || (Size)n >= len * 8)
 		ereport(ERROR,
 				(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
-				 errmsg("index %d out of valid range, 0..%d",
+				 errmsg("index %d out of valid range, 0..%ld",
 						n, len * 8 - 1)));
 
 	byteNo = n / 8;
