Home > OS >  A function to generate up to N virtual unique records
A function to generate up to N virtual unique records

Time:11-25

In my database app stress-testing, I need to be able to execute a function that produces a huge number of unique records.

Is it possible to implement a function in PostgreSql that would take a parameter - number of records to be generated, and then produce that many records of some virtual structure that I could later tweak?

CREATE FUNCTION generate_records(N)

/*
    generate N unique records of some random structure;
*/

I want to avoid creating actual tables with many millions of records for automated stress-testing, because I am not testing performance of the database, I am testing data-processing performance of a client that consumes a lot of data.

CodePudding user response:

You can use generate_series() for this.

To create a specific structure, just select the columns you want:

select g.id, 
       (random() * 99)   1)::int as status_code
       gen_random_uuid() as some_uid,
       date '2000-01-01'   (random() * 8000):int as due_date 
from generate_series(1,1e6) as g(id);
  • Related