diff --git a/src/test/modules/test_tidstore/expected/test_tidstore.out b/src/test/modules/test_tidstore/expected/test_tidstore.out
index cbcacfd26e1..ad92041bf35 100644
--- a/src/test/modules/test_tidstore/expected/test_tidstore.out
+++ b/src/test/modules/test_tidstore/expected/test_tidstore.out
@@ -6,6 +6,11 @@ CREATE TEMP TABLE hideblocks(blockno bigint);
 -- The maximum number of heap tuples (MaxHeapTuplesPerPage) in 8kB block is 291.
 -- We use a higher number to test tidstore.
 \set maxoffset 512
+CREATE FUNCTION generate_random_offsets(noffs int) RETURNS int2[]
+BEGIN ATOMIC
+  SELECT array_agg(DISTINCT val ORDER BY val)
+  FROM (SELECT ((random() * :maxoffset)::int + 1) as val FROM generate_series(1, noffs));
+END;
 SELECT test_create(false);
  test_create 
 -------------
@@ -39,8 +44,7 @@ SELECT do_set_block_offsets(501, array[greatest((random() * :maxoffset)::int, 1)
                   501
 (1 row)
 
-SELECT do_set_block_offsets(502, array_agg(DISTINCT greatest((random() * :maxoffset)::int, 1))::int2[])
-  FROM generate_series(1, 3);
+SELECT do_set_block_offsets(502, generate_random_offsets(3));
  do_set_block_offsets 
 ----------------------
                   502
@@ -202,8 +206,7 @@ SELECT do_set_block_offsets(501, array[greatest((random() * :maxoffset)::int, 1)
                   501
 (1 row)
 
-SELECT do_set_block_offsets(502, array_agg(DISTINCT greatest((random() * :maxoffset)::int, 1))::int2[])
-  FROM generate_series(1, 3);
+SELECT do_set_block_offsets(502, generate_random_offsets(3));
  do_set_block_offsets 
 ----------------------
                   502
@@ -212,9 +215,8 @@ SELECT do_set_block_offsets(502, array_agg(DISTINCT greatest((random() * :maxoff
 -- Random TIDs test. The offset numbers are randomized and must be
 -- unique and ordered.
 INSERT INTO hideblocks (blockno)
-SELECT do_set_block_offsets(blkno, array_agg(DISTINCT greatest((random() * :maxoffset)::int, 1))::int2[])
-  FROM generate_series(1, 100) num_offsets,
-  generate_series(1000, 1100, 1) blkno
+SELECT do_set_block_offsets(blkno, generate_random_offsets(100))
+  FROM generate_series(1000, 1100, 1) blkno
 GROUP BY blkno;
 -- Check TIDs we've added to the store.
 SELECT check_set_block_offsets();
@@ -231,3 +233,4 @@ SELECT test_destroy();
 (1 row)
 
 DROP TABLE hideblocks;
+DROP FUNCTION generate_random_offsets(integer);
diff --git a/src/test/modules/test_tidstore/sql/test_tidstore.sql b/src/test/modules/test_tidstore/sql/test_tidstore.sql
index a29e4ec1c55..38f08d23501 100644
--- a/src/test/modules/test_tidstore/sql/test_tidstore.sql
+++ b/src/test/modules/test_tidstore/sql/test_tidstore.sql
@@ -9,6 +9,12 @@ CREATE TEMP TABLE hideblocks(blockno bigint);
 -- We use a higher number to test tidstore.
 \set maxoffset 512
 
+CREATE FUNCTION generate_random_offsets(noffs int) RETURNS int2[]
+BEGIN ATOMIC
+  SELECT array_agg(DISTINCT val ORDER BY val)
+  FROM (SELECT ((random() * :maxoffset)::int + 1) as val FROM generate_series(1, noffs));
+END;
+
 SELECT test_create(false);
 
 -- Test on empty tidstore.
@@ -25,8 +31,7 @@ SELECT do_set_block_offsets(blk, array_agg(off)::int2[])
 
 -- Test offsets embedded in the bitmap header.
 SELECT do_set_block_offsets(501, array[greatest((random() * :maxoffset)::int, 1)]::int2[]);
-SELECT do_set_block_offsets(502, array_agg(DISTINCT greatest((random() * :maxoffset)::int, 1))::int2[])
-  FROM generate_series(1, 3);
+SELECT do_set_block_offsets(502, generate_random_offsets(3));
 
 -- Add enough TIDs to cause the store to appear "full", compared
 -- to the allocated memory it started out with. This is easier
@@ -67,15 +72,13 @@ SELECT test_create(true);
 
 -- Test offsets embedded in the bitmap header.
 SELECT do_set_block_offsets(501, array[greatest((random() * :maxoffset)::int, 1)]::int2[]);
-SELECT do_set_block_offsets(502, array_agg(DISTINCT greatest((random() * :maxoffset)::int, 1))::int2[])
-  FROM generate_series(1, 3);
+SELECT do_set_block_offsets(502, generate_random_offsets(3));
 
 -- Random TIDs test. The offset numbers are randomized and must be
 -- unique and ordered.
 INSERT INTO hideblocks (blockno)
-SELECT do_set_block_offsets(blkno, array_agg(DISTINCT greatest((random() * :maxoffset)::int, 1))::int2[])
-  FROM generate_series(1, 100) num_offsets,
-  generate_series(1000, 1100, 1) blkno
+SELECT do_set_block_offsets(blkno, generate_random_offsets(100))
+  FROM generate_series(1000, 1100, 1) blkno
 GROUP BY blkno;
 
 -- Check TIDs we've added to the store.
@@ -84,3 +87,4 @@ SELECT check_set_block_offsets();
 -- cleanup
 SELECT test_destroy();
 DROP TABLE hideblocks;
+DROP FUNCTION generate_random_offsets(integer);
