Hi,

I'm sending small patch for textsend. It reduces unnecessary copies, and 
memory usage for duplication of varlena data. May you look?

Kind regards,
Radosław Smogura
diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c
index e111d26..f24bbcd 100644
--- a/src/backend/utils/adt/varlena.c
+++ b/src/backend/utils/adt/varlena.c
@@ -442,12 +442,20 @@ textrecv(PG_FUNCTION_ARGS)
 Datum
 textsend(PG_FUNCTION_ARGS)
 {
-	text	   *t = PG_GETARG_TEXT_PP(0);
-	StringInfoData buf;
-
-	pq_begintypsend(&buf);
-	pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
-	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
+        text       *t = PG_GETARG_TEXT_PP(0);
+        const char* textData = VARDATA_ANY(t);
+        const int   textSize = VARSIZE_ANY_EXHDR(t);
+        char* textConverted = pg_server_to_client(textData, textSize);
+        //Logic based on pq_sendtext
+        if (textConverted == textData) {
+		PG_RETURN_BYTEA_P(t);
+        }else {
+		StringInfoData buf;
+		pq_begintypsend(&buf);
+		appendBinaryStringInfo(&buf, textConverted, strlen(textConverted));
+		pfree(textConverted);
+		PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
+	}
 }
 
 
-- 
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