diff -Naur pgsql.virg/src/backend/utils/adt/encode.c pgsql.dev/src/backend/utils/adt/encode.c
--- pgsql.virg/src/backend/utils/adt/encode.c	Mon Sep 10 13:31:04 2001
+++ pgsql.dev/src/backend/utils/adt/encode.c	Mon Sep 10 13:38:27 2001
@@ -19,8 +19,8 @@
 
 struct pg_encoding
 {
-	unsigned		(*encode_len) (unsigned dlen);
-	unsigned		(*decode_len) (unsigned dlen);
+	unsigned		(*encode_len) (const uint8 *data, unsigned dlen);
+	unsigned		(*decode_len) (const uint8 *data, unsigned dlen);
 	unsigned		(*encode) (const uint8 *data, unsigned dlen, uint8 *res);
 	unsigned		(*decode) (const uint8 *data, unsigned dlen, uint8 *res);
 };
@@ -50,7 +50,7 @@
 	if (enc == NULL)
 		elog(ERROR, "No such encoding");
 
-	resultlen = enc->encode_len(datalen);
+	resultlen = enc->encode_len(VARDATA(data), datalen);
 	result = palloc(VARHDRSZ + resultlen);
 
 	res = enc->encode(VARDATA(data), datalen, VARDATA(result));
@@ -81,7 +81,7 @@
 	if (enc == NULL)
 		elog(ERROR, "No such encoding");
 
-	resultlen = enc->decode_len(datalen);
+	resultlen = enc->decode_len(VARDATA(data), datalen);
 	result = palloc(VARHDRSZ + resultlen);
 
 	res = enc->decode(VARDATA(data), datalen, VARDATA(result));
@@ -169,13 +169,13 @@
 }
 
 static unsigned
-hex_enc_len(unsigned srclen)
+hex_enc_len(const uint8 * src, unsigned srclen)
 {
 	return srclen << 1;
 }
 
 static unsigned
-hex_dec_len(unsigned srclen)
+hex_dec_len(const uint8 * src, unsigned srclen)
 {
 	return srclen >> 1;
 }
@@ -308,19 +308,189 @@
 
 
 static unsigned
-b64_enc_len(unsigned srclen)
+b64_enc_len(const uint8 * src, unsigned srclen)
 {
 	/* 3 bytes will be converted to 4, linefeed after 76 chars */
 	return (srclen + 2) * 4 / 3 + srclen / (76 * 3 / 4);
 }
 
 static unsigned
-b64_dec_len(unsigned srclen)
+b64_dec_len(const uint8 * src, unsigned srclen)
 {
 	return (srclen * 3) >> 2;
 }
 
 /*
+ * Escape
+ * Minimally escape bytea to text.
+ * De-escape text to bytea.
+ * 
+ * Only two characters are escaped:
+ * \0 (null) and \\ (backslash)
+ * 
+ * De-escapes \\ and any \### octal
+ */
+
+#define VAL(CH)			((CH) - '0')
+#define DIG(VAL)		((VAL) + '0')
+
+static unsigned
+esc_encode(const uint8 *src, unsigned srclen, uint8 *dst)
+{
+	const uint8	   *end = src + srclen;
+	uint8			*rp = dst;
+	int				val;
+	int				len = 0;
+
+	while (src < end)
+	{
+		if (*src == '\0')
+		{
+			val = *src;
+			rp[0] = '\\';
+			rp[1] = '0';
+			rp[2] = '0';
+			rp[3] = '0';
+			rp += 4;
+			len += 4;
+		}
+		else if (*src == '\\')
+		{
+			val = *src;
+			rp[0] = '\\';
+			rp[1] = '\\';
+			rp += 2;
+			len += 2;
+		}
+		else
+		{
+			*rp++ = *src;
+			len++;
+		}
+
+		src++;
+	}
+	*rp = '\0';
+
+	return len;
+}
+
+static unsigned
+esc_decode(const uint8 *src, unsigned srclen, uint8 *dst)
+{
+	const uint8		*end = src + srclen;
+	uint8			*rp = dst;
+	int				val;
+	int				len = 0;
+
+	while (src < end)
+	{
+		if (src[0] != '\\')
+		{
+			*rp++ = *src++;
+		}
+		else if	( (src[0] == '\\') &&
+					(src[1] >= '0' && src[1] <= '3') &&
+					(src[2] >= '0' && src[2] <= '7') &&
+					(src[3] >= '0' && src[3] <= '7') )
+		{
+			val = VAL(src[1]);
+			val <<= 3;
+			val += VAL(src[2]);
+			val <<= 3;
+			*rp++ = val + VAL(src[3]);
+			src += 4;
+		}
+		else if ( (src[0] == '\\') &&
+					(src[1] == '\\') )
+		{
+			*rp++ = '\\';
+			src += 2;
+		}
+		else
+		{
+			/*
+			 * One backslash, not followed by ### valid octal.
+			 * Should never get here, since esc_dec_len does same check.
+			 */
+			elog(ERROR, "decode: Bad input string for type bytea");
+		}
+
+		len++;
+	}
+	return len;
+}
+
+static unsigned
+esc_enc_len(const uint8 *src, unsigned srclen)
+{
+	const uint8		*end = src + srclen;
+	int				len = 0;
+
+	while (src < end)
+	{
+		if (*src == '\0')
+			len += 4;
+		else if (*src == '\\')
+			len += 2;
+		else
+			len++;
+
+		src++;
+	}
+
+	/*
+	 * Allow for null terminator
+	 */
+	len++;
+
+	return len;
+}
+
+static unsigned
+esc_dec_len(const uint8 *src, unsigned srclen)
+{
+	const uint8		*end = src + srclen;
+	int				len = 0;
+
+	while (src < end)
+	{
+		if (src[0] != '\\')
+		{
+			src++;
+		}
+		else if	( (src[0] == '\\') &&
+					(src[1] >= '0' && src[1] <= '3') &&
+					(src[2] >= '0' && src[2] <= '7') &&
+					(src[3] >= '0' && src[3] <= '7') )
+		{
+			/*
+			 * backslash + valid octal
+			 */
+			src += 4;
+		}
+		else if ( (src[0] == '\\') &&
+					(src[1] == '\\') )
+		{
+			/*
+			 * two backslashes = backslash
+			 */
+			src += 2;
+		}
+		else
+		{
+			/*
+			 * one backslash, not followed by ### valid octal
+			 */
+			elog(ERROR, "decode: Bad input string for type bytea");
+		}
+
+		len++;
+	}
+	return len;
+}
+
+/*
  * Common
  */
 
@@ -330,6 +500,7 @@
 } enclist[] = {
 	{"hex", { hex_enc_len, hex_dec_len, hex_encode, hex_decode }},
 	{"base64", { b64_enc_len, b64_dec_len, b64_encode, b64_decode }},
+	{"escape", { esc_enc_len, esc_dec_len, esc_encode, esc_decode }},
 	{NULL, { NULL, NULL, NULL, NULL } }
 };
 
diff -Naur pgsql.virg/src/backend/utils/adt/like.c pgsql.dev/src/backend/utils/adt/like.c
--- pgsql.virg/src/backend/utils/adt/like.c	Mon Sep 10 13:31:04 2001
+++ pgsql.dev/src/backend/utils/adt/like.c	Mon Sep 10 14:00:53 2001
@@ -34,6 +34,8 @@
 		  unsigned char *p, int plen);
 static int MatchTextIC(unsigned char *t, int tlen,
 			unsigned char *p, int plen);
+static int MatchBytea(unsigned char *t, int tlen,
+		  unsigned char *p, int plen);
 
 
 #ifdef MULTIBYTE
@@ -120,6 +122,9 @@
 #define CopyAdvChar(dst, src, srclen) (*(dst)++ = *(src)++, (srclen)--)
 #endif
 
+#define BYTEA_CHAREQ(p1, p2) (*(p1) == *(p2))
+#define BYTEA_NextChar(p, plen) ((p)++, (plen)--)
+#define BYTEA_CopyAdvChar(dst, src, srclen) (*(dst)++ = *(src)++, (srclen)--)
 
 /*
  *	interface routines called by the function manager
@@ -209,6 +214,48 @@
 	PG_RETURN_BOOL(result);
 }
 
+Datum
+bytealike(PG_FUNCTION_ARGS)
+{
+	bytea	   *str = PG_GETARG_BYTEA_P(0);
+	bytea	   *pat = PG_GETARG_BYTEA_P(1);
+	bool		result;
+	unsigned char *s,
+			   *p;
+	int			slen,
+				plen;
+
+	s = VARDATA(str);
+	slen = (VARSIZE(str) - VARHDRSZ);
+	p = VARDATA(pat);
+	plen = (VARSIZE(pat) - VARHDRSZ);
+
+	result = (MatchBytea(s, slen, p, plen) == LIKE_TRUE);
+
+	PG_RETURN_BOOL(result);
+}
+
+Datum
+byteanlike(PG_FUNCTION_ARGS)
+{
+	bytea	   *str = PG_GETARG_BYTEA_P(0);
+	bytea	   *pat = PG_GETARG_BYTEA_P(1);
+	bool		result;
+	unsigned char *s,
+			   *p;
+	int			slen,
+				plen;
+
+	s = VARDATA(str);
+	slen = (VARSIZE(str) - VARHDRSZ);
+	p = VARDATA(pat);
+	plen = (VARSIZE(pat) - VARHDRSZ);
+
+	result = (MatchBytea(s, slen, p, plen) != LIKE_TRUE);
+
+	PG_RETURN_BOOL(result);
+}
+
 /*
  * Case-insensitive versions
  */
@@ -395,6 +442,103 @@
 	PG_RETURN_TEXT_P(result);
 }
 
+/*
+ * like_escape_bytea() --- given a pattern and an ESCAPE string,
+ * convert the pattern to use Postgres' standard backslash escape convention.
+ */
+Datum
+like_escape_bytea(PG_FUNCTION_ARGS)
+{
+	bytea	   *pat = PG_GETARG_BYTEA_P(0);
+	bytea	   *esc = PG_GETARG_BYTEA_P(1);
+	bytea	   *result;
+	unsigned char *p,
+			   *e,
+			   *r;
+	int			plen,
+				elen;
+	bool		afterescape;
+
+	p = VARDATA(pat);
+	plen = (VARSIZE(pat) - VARHDRSZ);
+	e = VARDATA(esc);
+	elen = (VARSIZE(esc) - VARHDRSZ);
+
+	/*
+	 * Worst-case pattern growth is 2x --- unlikely, but it's hardly worth
+	 * trying to calculate the size more accurately than that.
+	 */
+	result = (text *) palloc(plen * 2 + VARHDRSZ);
+	r = VARDATA(result);
+
+	if (elen == 0)
+	{
+
+		/*
+		 * No escape character is wanted.  Double any backslashes in the
+		 * pattern to make them act like ordinary characters.
+		 */
+		while (plen > 0)
+		{
+			if (*p == '\\')
+				*r++ = '\\';
+			BYTEA_CopyAdvChar(r, p, plen);
+		}
+	}
+	else
+	{
+
+		/*
+		 * The specified escape must be only a single character.
+		 */
+		BYTEA_NextChar(e, elen);
+		if (elen != 0)
+			elog(ERROR, "ESCAPE string must be empty or one character");
+		e = VARDATA(esc);
+
+		/*
+		 * If specified escape is '\', just copy the pattern as-is.
+		 */
+		if (*e == '\\')
+		{
+			memcpy(result, pat, VARSIZE(pat));
+			PG_RETURN_BYTEA_P(result);
+		}
+
+		/*
+		 * Otherwise, convert occurrences of the specified escape
+		 * character to '\', and double occurrences of '\' --- unless they
+		 * immediately follow an escape character!
+		 */
+		afterescape = false;
+		while (plen > 0)
+		{
+			if (BYTEA_CHAREQ(p, e) && !afterescape)
+			{
+				*r++ = '\\';
+				BYTEA_NextChar(p, plen);
+				afterescape = true;
+			}
+			else if (*p == '\\')
+			{
+				*r++ = '\\';
+				if (!afterescape)
+					*r++ = '\\';
+				BYTEA_NextChar(p, plen);
+				afterescape = false;
+			}
+			else
+			{
+				BYTEA_CopyAdvChar(r, p, plen);
+				afterescape = false;
+			}
+		}
+	}
+
+	VARATT_SIZEP(result) = r - ((unsigned char *) result);
+
+	PG_RETURN_BYTEA_P(result);
+}
 
 /*
 **	Originally written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
@@ -614,3 +758,91 @@
 	 */
 	return LIKE_ABORT;
 }	/* MatchTextIC() */
+
+/*
+ * Same as above, but specifically for bytea (binary) datatype
+ */
+static int
+MatchBytea(unsigned char *t, int tlen, unsigned char *p, int plen)
+{
+	/* Fast path for match-everything pattern */
+	if ((plen == 1) && (*p == '%'))
+		return LIKE_TRUE;
+
+	while ((tlen > 0) && (plen > 0))
+	{
+		if (*p == '\\')
+		{
+			/* Next pattern char must match literally, whatever it is */
+			BYTEA_NextChar(p, plen);
+			if ((plen <= 0) || !BYTEA_CHAREQ(t, p))
+				return LIKE_FALSE;
+		}
+		else if (*p == '%')
+		{
+			/* %% is the same as % according to the SQL standard */
+			/* Advance past all %'s */
+			while ((plen > 0) && (*p == '%'))
+				BYTEA_NextChar(p, plen);
+			/* Trailing percent matches everything. */
+			if (plen <= 0)
+				return LIKE_TRUE;
+
+			/*
+			 * Otherwise, scan for a text position at which we can match
+			 * the rest of the pattern.
+			 */
+			while (tlen > 0)
+			{
+
+				/*
+				 * Optimization to prevent most recursion: don't recurse
+				 * unless first pattern char might match this text char.
+				 */
+				if (BYTEA_CHAREQ(t, p) || (*p == '\\') || (*p == '_'))
+				{
+					int			matched = MatchBytea(t, tlen, p, plen);
+
+					if (matched != LIKE_FALSE)
+						return matched; /* TRUE or ABORT */
+				}
+
+				BYTEA_NextChar(t, tlen);
+			}
+
+			/*
+			 * End of text with no match, so no point in trying later
+			 * places to start matching this pattern.
+			 */
+			return LIKE_ABORT;
+		}
+		else if ((*p != '_') && !BYTEA_CHAREQ(t, p))
+		{
+
+			/*
+			 * Not the single-character wildcard and no explicit match?
+			 * Then time to quit...
+			 */
+			return LIKE_FALSE;
+		}
+
+		BYTEA_NextChar(t, tlen);
+		BYTEA_NextChar(p, plen);
+	}
+
+	if (tlen > 0)
+		return LIKE_FALSE;		/* end of pattern, but not of text */
+
+	/* End of input string.  Do we have matching pattern remaining? */
+	while ((plen > 0) && (*p == '%'))	/* allow multiple %'s at end of
+										 * pattern */
+		BYTEA_NextChar(p, plen);
+	if (plen <= 0)
+		return LIKE_TRUE;
+
+	/*
+	 * End of text with no match, so no point in trying later places to
+	 * start matching this pattern.
+	 */
+	return LIKE_ABORT;
+}	/* MatchBytea() */
diff -Naur pgsql.virg/src/backend/utils/adt/oracle_compat.c pgsql.dev/src/backend/utils/adt/oracle_compat.c
--- pgsql.virg/src/backend/utils/adt/oracle_compat.c	Mon Sep 10 13:31:04 2001
+++ pgsql.dev/src/backend/utils/adt/oracle_compat.c	Mon Sep 10 13:38:27 2001
@@ -349,6 +349,78 @@
 	PG_RETURN_TEXT_P(ret);
 }
 
+/********************************************************************
+ *
+ * byteatrim
+ *
+ * Syntax:
+ *
+ *	 bytea byteatrim(byta string, bytea set)
+ *
+ * Purpose:
+ *
+ *	 Returns string with characters removed from the front and back
+ *	 up to the first character not in set.
+ *
+ * Cloned from btrim and modified as required.
+ ********************************************************************/
+
+Datum
+byteatrim(PG_FUNCTION_ARGS)
+{
+	bytea	   *string = PG_GETARG_BYTEA_P(0);
+	bytea	   *set = PG_GETARG_BYTEA_P(1);
+	bytea	   *ret;
+	char	   *ptr,
+			   *end,
+			   *ptr2,
+			   *end2;
+	int			m;
+
+	if ((m = VARSIZE(string) - VARHDRSZ) <= 0 ||
+		(VARSIZE(set) - VARHDRSZ) <= 0)
+		PG_RETURN_BYTEA_P(string);
+
+	ptr = VARDATA(string);
+	end = VARDATA(string) + VARSIZE(string) - VARHDRSZ - 1;
+	end2 = VARDATA(set) + VARSIZE(set) - VARHDRSZ - 1;
+
+	while (m > 0)
+	{
+		ptr2 = VARDATA(set);
+		while (ptr2 <= end2)
+		{
+			if (*ptr == *ptr2)
+				break;
+			++ptr2;
+		}
+		if (ptr2 > end2)
+			break;
+		ptr++;
+		m--;
+	}
+
+	while (m > 0)
+	{
+		ptr2 = VARDATA(set);
+		while (ptr2 <= end2)
+		{
+			if (*end == *ptr2)
+				break;
+			++ptr2;
+		}
+		if (ptr2 > end2)
+			break;
+		end--;
+		m--;
+	}
+
+	ret = (bytea *) palloc(VARHDRSZ + m);
+	VARATT_SIZEP(ret) = VARHDRSZ + m;
+	memcpy(VARDATA(ret), ptr, m);
+
+	PG_RETURN_BYTEA_P(ret);
+}
 
 /********************************************************************
  *
diff -Naur pgsql.virg/src/backend/utils/adt/selfuncs.c pgsql.dev/src/backend/utils/adt/selfuncs.c
--- pgsql.virg/src/backend/utils/adt/selfuncs.c	Mon Sep 10 13:31:04 2001
+++ pgsql.dev/src/backend/utils/adt/selfuncs.c	Mon Sep 10 13:38:27 2001
@@ -2992,6 +2992,11 @@
 													  datum1, datum2));
 			break;
 
+		case BYTEAOID:
+			result = DatumGetBool(DirectFunctionCall2(bytealt,
+													  datum1, datum2));
+			break;
+
 		default:
 			elog(ERROR, "string_lessthan: unexpected datatype %u", datatype);
 			result = false;
diff -Naur pgsql.virg/src/backend/utils/adt/varlena.c pgsql.dev/src/backend/utils/adt/varlena.c
--- pgsql.virg/src/backend/utils/adt/varlena.c	Mon Sep 10 13:31:04 2001
+++ pgsql.dev/src/backend/utils/adt/varlena.c	Mon Sep 10 13:38:27 2001
@@ -53,14 +53,28 @@
 
 	for (byte = 0, tp = inputText; *tp != '\0'; byte++)
 	{
-		if (*tp++ == '\\')
+		if (tp[0] != '\\')
 		{
-			if (*tp == '\\')
-				tp++;
-			else if (!isdigit((unsigned char) *tp++) ||
-					 !isdigit((unsigned char) *tp++) ||
-					 !isdigit((unsigned char) *tp++))
-				elog(ERROR, "Bad input string for type bytea");
+			tp++;
+		}
+		else if	( (tp[0] == '\\') &&
+					(tp[1] >= '0' && tp[1] <= '3') &&
+					(tp[2] >= '0' && tp[2] <= '7') &&
+					(tp[3] >= '0' && tp[3] <= '7') )
+		{
+			tp += 4;
+		}
+		else if ( (tp[0] == '\\') &&
+				(tp[1] == '\\') )
+		{
+			tp += 2;
+		}
+		else
+		{
+			/*
+			 * one backslash, not followed by 0 or ### valid octal
+			 */
+			elog(ERROR, "Bad input string for type bytea");
 		}
 	}
 
@@ -72,15 +86,35 @@
 	rp = result->vl_dat;
 	while (*tp != '\0')
 	{
-		if (*tp != '\\' || *++tp == '\\')
+		if (tp[0] != '\\')
+		{
 			*rp++ = *tp++;
-		else
+		}
+		else if	( (tp[0] == '\\') &&
+					(tp[1] >= '0' && tp[1] <= '3') &&
+					(tp[2] >= '0' && tp[2] <= '7') &&
+					(tp[3] >= '0' && tp[3] <= '7') )
 		{
-			byte = VAL(*tp++);
+			byte = VAL(tp[1]);
 			byte <<= 3;
-			byte += VAL(*tp++);
+			byte += VAL(tp[2]);
 			byte <<= 3;
-			*rp++ = byte + VAL(*tp++);
+			*rp++ = byte + VAL(tp[3]);
+			tp += 4;
+		}
+		else if ( (tp[0] == '\\') &&
+				(tp[1] == '\\') )
+		{
+			*rp++ = '\\';
+			tp += 2;
+		}
+		else
+		{
+			/*
+			 * We should never get here. The first pass should
+			 * not allow it.
+			 */
+			elog(ERROR, "Bad input string for type bytea");
 		}
 	}
 
@@ -660,6 +694,147 @@
 	bytea	   *v = PG_GETARG_BYTEA_P(0);
 
 	PG_RETURN_INT32(VARSIZE(v) - VARHDRSZ);
+}
+
+/*
+ * byteacat -
+ *	  takes two bytea* and returns a bytea* that is the concatenation of
+ *	  the two.
+ *
+ * Cloned from textcat and modified as required.
+ */
+Datum
+byteacat(PG_FUNCTION_ARGS)
+{
+	bytea	   *t1 = PG_GETARG_BYTEA_P(0);
+	bytea	   *t2 = PG_GETARG_BYTEA_P(1);
+	int			len1,
+				len2,
+				len;
+	bytea	   *result;
+	char	   *ptr;
+
+	len1 = (VARSIZE(t1) - VARHDRSZ);
+	if (len1 < 0)
+		len1 = 0;
+
+	len2 = (VARSIZE(t2) - VARHDRSZ);
+	if (len2 < 0)
+		len2 = 0;
+
+	len = len1 + len2 + VARHDRSZ;
+	result = (bytea *) palloc(len);
+
+	/* Set size of result string... */
+	VARATT_SIZEP(result) = len;
+
+	/* Fill data field of result string... */
+	ptr = VARDATA(result);
+	if (len1 > 0)
+		memcpy(ptr, VARDATA(t1), len1);
+	if (len2 > 0)
+		memcpy(ptr + len1, VARDATA(t2), len2);
+
+	PG_RETURN_BYTEA_P(result);
+}
+
+/*
+ * bytea_substr()
+ * Return a substring starting at the specified position.
+ * Cloned from text_substr and modified as required.
+ *
+ * Input:
+ *	- string
+ *	- starting position (is one-based)
+ *	- string length
+ *
+ * If the starting position is zero or less, then return from the start of the string
+ * adjusting the length to be consistant with the "negative start" per SQL92.
+ * If the length is less than zero, return the remaining string.
+ *
+ */
+Datum
+bytea_substr(PG_FUNCTION_ARGS)
+{
+	bytea	   *string = PG_GETARG_BYTEA_P(0);
+	int32		m = PG_GETARG_INT32(1);
+	int32		n = PG_GETARG_INT32(2);
+	bytea	   *ret;
+	int			len;
+
+	len = VARSIZE(string) - VARHDRSZ;
+
+	/* starting position after the end of the string? */
+	if (m > len)
+	{
+		m = 1;
+		n = 0;
+	}
+
+	/*
+	 * starting position before the start of the string? then offset into
+	 * the string per SQL92 spec...
+	 */
+	else if (m < 1)
+	{
+		n += (m - 1);
+		m = 1;
+	}
+
+	/* m will now become a zero-based starting position */
+	m--;
+	if (((m + n) > len) || (n < 0))
+		n = (len - m);
+
+	ret = (bytea *) palloc(VARHDRSZ + n);
+	VARATT_SIZEP(ret) = VARHDRSZ + n;
+
+	memcpy(VARDATA(ret), VARDATA(string) + m, n);
+
+	PG_RETURN_BYTEA_P(ret);
+}
+
+/*
+ * byteapos -
+ *	  Return the position of the specified substring.
+ *	  Implements the SQL92 POSITION() function.
+ * Cloned from textpos and modified as required.
+ */
+Datum
+byteapos(PG_FUNCTION_ARGS)
+{
+	bytea	   *t1 = PG_GETARG_BYTEA_P(0);
+	bytea	   *t2 = PG_GETARG_BYTEA_P(1);
+	int			pos;
+	int			px,
+				p;
+	int			len1,
+				len2;
+	char		*p1,
+				*p2;
+
+	if (VARSIZE(t2) <= VARHDRSZ)
+		PG_RETURN_INT32(1);		/* result for empty pattern */
+
+	len1 = (VARSIZE(t1) - VARHDRSZ);
+	len2 = (VARSIZE(t2) - VARHDRSZ);
+
+	p1 = VARDATA(t1);
+	p2 = VARDATA(t2);
+
+	pos = 0;
+	px = (len1 - len2);
+	for (p = 0; p <= px; p++)
+	{
+		if ((*p2 == *p1) && (memcmp(p1, p2, len2) == 0))
+		{
+			pos = p + 1;
+			break;
+		};
+		p1++;
+	};
+
+	PG_RETURN_INT32(pos);
 }
 
 /*-------------------------------------------------------------
diff -Naur pgsql.virg/src/include/catalog/catversion.h pgsql.dev/src/include/catalog/catversion.h
--- pgsql.virg/src/include/catalog/catversion.h	Mon Sep 10 13:31:14 2001
+++ pgsql.dev/src/include/catalog/catversion.h	Mon Sep 10 13:38:27 2001
@@ -53,6 +53,6 @@
  */
 
 /*							yyyymmddN */
-#define CATALOG_VERSION_NO	200109081
+#define CATALOG_VERSION_NO	200109101
 
 #endif
diff -Naur pgsql.virg/src/include/catalog/pg_operator.h pgsql.dev/src/include/catalog/pg_operator.h
--- pgsql.virg/src/include/catalog/pg_operator.h	Mon Sep 10 13:31:14 2001
+++ pgsql.dev/src/include/catalog/pg_operator.h	Mon Sep 10 13:38:27 2001
@@ -811,11 +811,15 @@
 
 /* bytea operators */
 DATA(insert OID = 1955 ( "="	   PGUID 0 b t t 17 17	16 1955 1956 1957 1957 byteaeq eqsel eqjoinsel ));
-DATA(insert OID = 1956 ( "<>"	   PGUID 0 b t f 17 17	16 1956 1955  0 0 byteane neqsel neqjoinsel ));
-DATA(insert OID = 1957 ( "<"	   PGUID 0 b t f 17 17	16 1959 1960  0 0 bytealt scalarltsel scalarltjoinsel ));
-DATA(insert OID = 1958 ( "<="	   PGUID 0 b t f 17 17	16 1960 1959  0 0 byteale scalarltsel scalarltjoinsel ));
-DATA(insert OID = 1959 ( ">"	   PGUID 0 b t f 17 17	16 1957 1958  0 0 byteagt scalargtsel scalargtjoinsel ));
-DATA(insert OID = 1960 ( ">="	   PGUID 0 b t f 17 17	16 1958 1957  0 0 byteage scalargtsel scalargtjoinsel ));
+DATA(insert OID = 1956 ( "<>"	   PGUID 0 b t f 17 17	16 1956 1955 0    0 byteane neqsel neqjoinsel ));
+DATA(insert OID = 1957 ( "<"	   PGUID 0 b t f 17 17	16 1959 1960 0    0 bytealt scalarltsel scalarltjoinsel ));
+DATA(insert OID = 1958 ( "<="	   PGUID 0 b t f 17 17	16 1960 1959 0    0 byteale scalarltsel scalarltjoinsel ));
+DATA(insert OID = 1959 ( ">"	   PGUID 0 b t f 17 17	16 1957 1958 0    0 byteagt scalargtsel scalargtjoinsel ));
+DATA(insert OID = 1960 ( ">="	   PGUID 0 b t f 17 17	16 1958 1957 0    0 byteage scalargtsel scalargtjoinsel ));
+DATA(insert OID = 2016 (  "~~"	   PGUID 0 b t f 17 17  16 0    2017 0    0 bytealike likesel likejoinsel ));
+#define OID_BYTEA_LIKE_OP		2016
+DATA(insert OID = 2017 (  "!~~"	   PGUID 0 b t f 17 17  16 0    2016 0    0 byteanlike nlikesel nlikejoinsel ));
+DATA(insert OID = 2018 (  "||"	   PGUID 0 b t f 17 17  17 0    0	 0    0 byteacat - - ));
 
 /*
  * function prototypes
diff -Naur pgsql.virg/src/include/catalog/pg_proc.h pgsql.dev/src/include/catalog/pg_proc.h
--- pgsql.virg/src/include/catalog/pg_proc.h	Mon Sep 10 13:31:14 2001
+++ pgsql.dev/src/include/catalog/pg_proc.h	Mon Sep 10 13:40:09 2001
@@ -2740,6 +2740,28 @@
 DATA(insert OID = 1966 (  oidsmaller	   PGUID 12 f t t t 2 f 26 "26 26" 100 0 0 100	oidsmaller - ));
 DESCR("smaller of two");
 
+DATA(insert OID = 2005 (  bytealike		   PGUID 12 f t t t 2 f 16 "17 17" 100 0 0 100 bytealike - ));
+DESCR("matches LIKE expression");
+DATA(insert OID = 2006 (  byteanlike	   PGUID 12 f t t t 2 f 16 "17 17" 100 0 0 100 byteanlike - ));
+DESCR("does not match LIKE expression");
+DATA(insert OID = 2007 (  like			   PGUID 12 f t t t 2 f 16 "17 17" 100 0 0 100  bytealike - ));
+DESCR("matches LIKE expression");
+DATA(insert OID = 2008 (  notlike		   PGUID 12 f t t t 2 f 16 "17 17" 100 0 0 100  byteanlike - ));
+DESCR("does not match LIKE expression");
+DATA(insert OID = 2009 (  like_escape	   PGUID 12 f t t t 2 f 17 "17 17" 100 0 0 100 like_escape_bytea - ));
+DESCR("convert match pattern to use backslash escapes");
+DATA(insert OID = 2010 (  length		   PGUID 12 f t t t 1 f 23 "17" 100 0 0 100  byteaoctetlen - ));
+DESCR("octet length");
+DATA(insert OID = 2011 (  byteacat		   PGUID 12 f t t t 2 f 17 "17 17" 100 0 0 100	byteacat - ));
+DESCR("concatenate");
+DATA(insert OID = 2012 (  substring		   PGUID 12 f t t t 3 f 17 "17 23 23" 100 0 0 100  bytea_substr - ));
+DESCR("return portion of string");
+DATA(insert OID = 2013 (  substring		   PGUID 14 f t t t 2 f 17 "17 23" 100 0 0 100	"select substring($1, $2, -1)" - ));
+DESCR("return portion of string");
+DATA(insert OID = 2014 (  position		   PGUID 12 f t t t 2 f 23 "17 17" 100 0 0 100	byteapos - ));
+DESCR("return position of substring");
+DATA(insert OID = 2015 (  btrim			   PGUID 12 f t t t 2 f 17 "17 17" 100 0 0 100	byteatrim - ));
+DESCR("trim both ends of string");
  
 /*
  * prototypes for functions pg_proc.c
diff -Naur pgsql.virg/src/include/utils/builtins.h pgsql.dev/src/include/utils/builtins.h
--- pgsql.virg/src/include/utils/builtins.h	Mon Sep 10 13:31:16 2001
+++ pgsql.dev/src/include/utils/builtins.h	Mon Sep 10 13:38:27 2001
@@ -421,6 +421,9 @@
 extern Datum byteagt(PG_FUNCTION_ARGS);
 extern Datum byteage(PG_FUNCTION_ARGS);
 extern Datum byteacmp(PG_FUNCTION_ARGS);
+extern Datum byteacat(PG_FUNCTION_ARGS);
+extern Datum byteapos(PG_FUNCTION_ARGS);
+extern Datum bytea_substr(PG_FUNCTION_ARGS);
 
 /* version.c */
 extern Datum pgsql_version(PG_FUNCTION_ARGS);
@@ -434,7 +437,10 @@
 extern Datum textnlike(PG_FUNCTION_ARGS);
 extern Datum texticlike(PG_FUNCTION_ARGS);
 extern Datum texticnlike(PG_FUNCTION_ARGS);
+extern Datum bytealike(PG_FUNCTION_ARGS);
+extern Datum byteanlike(PG_FUNCTION_ARGS);
 extern Datum like_escape(PG_FUNCTION_ARGS);
+extern Datum like_escape_bytea(PG_FUNCTION_ARGS);
 
 /* oracle_compat.c */
 extern Datum lower(PG_FUNCTION_ARGS);
@@ -443,6 +449,7 @@
 extern Datum lpad(PG_FUNCTION_ARGS);
 extern Datum rpad(PG_FUNCTION_ARGS);
 extern Datum btrim(PG_FUNCTION_ARGS);
+extern Datum byteatrim(PG_FUNCTION_ARGS);
 extern Datum ltrim(PG_FUNCTION_ARGS);
 extern Datum rtrim(PG_FUNCTION_ARGS);
 extern Datum translate(PG_FUNCTION_ARGS);
diff -Naur pgsql.virg/src/interfaces/libpq/fe-exec.c pgsql.dev/src/interfaces/libpq/fe-exec.c
--- pgsql.virg/src/interfaces/libpq/fe-exec.c	Mon Sep 10 13:31:22 2001
+++ pgsql.dev/src/interfaces/libpq/fe-exec.c	Mon Sep 10 17:58:58 2001
@@ -111,7 +111,79 @@
 	return target - to;
 }
 
+/*
+ *		PQescapeBytea	- converts from binary string to the
+ *		minimal encoding necessary to include the string in an SQL
+ *		INSERT statement with a bytea type column as the target.
+ *
+ *		The following transformations are applied
+ *		'\0' == ASCII  0 == \\000
+ *		'\'' == ASCII 39 == \'
+ *		'\\' == ASCII 92 == \\\\
+ */
+unsigned char *
+PQescapeBytea(unsigned char *bintext, size_t binlen, size_t *bytealen)
+{
+	unsigned char	   *vp;
+	unsigned char	   *rp;
+	unsigned char	   *result;
+	size_t				i;
+	size_t				len;
 
+	/*
+	 * empty string has 1 char ('\0')
+	 */
+	len = 1;
+
+	vp = bintext;
+	for (i = binlen; i != 0; i--, vp++)
+	{
+		if (*vp == 0)
+			len += 5;
+		else if (*vp == 39)
+			len += 2;
+		else if (*vp == 92)
+			len += 4;
+		else
+			len++;
+	}
+
+	rp = result = (unsigned char *) malloc(len);
+	vp = bintext;
+	*bytealen = len;
+
+	for (i = binlen; i != 0; i--, vp++)
+	{
+		if (*vp == 0)
+		{
+			rp[0] = '\\';
+			rp[1] = '\\';
+			rp[2] = '0';
+			rp[3] = '0';
+			rp[4] = '0';
+			rp += 5;
+		}
+		else if (*vp == 39)
+		{
+			rp[0] = '\\';
+			rp[1] = '\'';
+			rp += 2;
+		}
+		else if (*vp == 92)
+		{
+			rp[0] = '\\';
+			rp[1] = '\\';
+			rp[2] = '\\';
+			rp[3] = '\\';
+			rp += 4;
+		}
+		else
+			*rp++ = *vp;
+	}
+	*rp = '\0';
+
+	return result;
+}
 
 /* ----------------
  * Space management for PGresult.
diff -Naur pgsql.virg/src/interfaces/libpq/libpq-fe.h pgsql.dev/src/interfaces/libpq/libpq-fe.h
--- pgsql.virg/src/interfaces/libpq/libpq-fe.h	Mon Sep 10 13:31:22 2001
+++ pgsql.dev/src/interfaces/libpq/libpq-fe.h	Mon Sep 10 17:58:15 2001
@@ -253,6 +253,7 @@
 
 	/* Quoting strings before inclusion in queries. */
 	extern size_t PQescapeString (char *to, const char *from, size_t length);
+	extern unsigned char *PQescapeBytea(unsigned char *bintext, size_t binlen, size_t *bytealen);
 
 	/* Simple synchronous query */
 	extern PGresult *PQexec(PGconn *conn, const char *query);
