diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index cd0b4db07c..8e56faaf9f 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -1836,6 +1836,25 @@ repeat('Pg', 4) <returnvalue>PgPgPgPg</returnvalue>
        </para></entry>
       </row>
 
+      <row>
+       <entry role="func_table_entry">
+        <para role="func_signature">
+         <indexterm>
+          <primary>random_string</primary>
+         </indexterm>
+         <function>random_string</function> (
+         <parameter>nbytes</parameter> <type>integer</type> )
+         <returnvalue>bytea</returnvalue>
+       </para>
+       <para>
+        Returns a bytea of the specified size, containing random values.
+       </para>
+       <para>
+        <literal>random_string(3)</literal>
+        <returnvalue>\x78da35</returnvalue>
+       </para></entry>
+      </row>
+
       <row>
        <entry role="func_table_entry"><para role="func_signature">
         <indexterm>
diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c
index 36a9712b0e..1590546759 100644
--- a/src/backend/utils/adt/float.c
+++ b/src/backend/utils/adt/float.c
@@ -2785,6 +2785,35 @@ drandom_normal(PG_FUNCTION_ARGS)
 	PG_RETURN_FLOAT8(result);
 }
 
+/*
+ *		drandom_string		- returns a bytea filled with random content
+ *
+ */
+Datum
+drandom_string(PG_FUNCTION_ARGS)
+{
+	int32 nbytes = PG_GETARG_INT32(0);;
+	if (nbytes < 0)
+	{
+		ereport(ERROR,
+			(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+			 errmsg("string size must be non-negative")));
+	}
+	else
+	{
+		size_t result_size = VARHDRSZ + nbytes;
+		bytea *result = palloc(result_size);
+
+		if (nbytes > 0)
+		{
+			drandom_check_default_seed();
+			pg_strong_random(VARDATA(result), nbytes);
+		}
+		SET_VARSIZE(result, result_size);
+		PG_RETURN_BYTEA_P(result);
+	}
+}
+
 /*
  *		setseed		- set seed for the random number generator
  */
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index b923db6fda..42ddb2f827 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -3366,6 +3366,9 @@
   proname => 'random_normal', provolatile => 'v', proparallel => 'r',
   prorettype => 'float8', proargtypes => 'float8 float8',
   proargnames => '{mean,stddev}', prosrc => 'drandom_normal' },
+{ oid => '5152', descr => 'fill bytea with random values',
+  proname => 'random_string', provolatile => 'v', proparallel => 'r',
+  prorettype => 'bytea', proargtypes => 'int4', prosrc => 'drandom_string' },
 
 # OIDS 1600 - 1699
 
diff --git a/src/test/regress/expected/random.out b/src/test/regress/expected/random.out
index 784001480b..34e42a9f66 100644
--- a/src/test/regress/expected/random.out
+++ b/src/test/regress/expected/random.out
@@ -79,3 +79,21 @@ SELECT AVG(random) FROM random_tbl
 -----
 (0 rows)
 
+-- not allowed
+SELECT random_string(-1);
+ERROR:  string size must be non-negative
+-- zero length bytea
+SELECT random_string(0);
+ random_string 
+---------------
+ \x
+(1 row)
+
+-- should practically never happen
+SELECT bool_and(random_string(16) != random_string(16)) AS same
+  FROM generate_series(1,8);
+ same 
+------
+ t
+(1 row)
+
diff --git a/src/test/regress/sql/random.sql b/src/test/regress/sql/random.sql
index 4e0a91c3e4..7be48d83ae 100644
--- a/src/test/regress/sql/random.sql
+++ b/src/test/regress/sql/random.sql
@@ -66,4 +66,12 @@ SELECT random, count(random) FROM random_tbl
 SELECT AVG(random) FROM random_tbl
   HAVING AVG(random) NOT BETWEEN 400 AND 600;
 
+-- not allowed
+SELECT random_string(-1);
 
+-- zero length bytea
+SELECT random_string(0);
+
+-- should practically never happen
+SELECT bool_and(random_string(16) != random_string(16)) AS same
+  FROM generate_series(1,8);
