How can I generate a large sample of data for Postgresql using generate_series and random?

I want to create a large sample of data (almost 1 million records) to study the multiphase merge of tuplesort.c in postgresql, and I hope the schema looks like this:

CREATE TABLE Departments (code VARCHAR(4), UNIQUE (code));
CREATE TABLE Towns (
  id SERIAL UNIQUE NOT NULL,
  code VARCHAR(10) NOT NULL, -- not unique
  article TEXT,
  name TEXT NOT NULL, -- not unique
  department VARCHAR(4) NOT NULL REFERENCES Departments (code),
  UNIQUE (code, department)
);

how to use generate_series and random for this? Thank you so much!

+4
source share
1 answer

To insert a million rows in Towns

insert into towns (
    code, article, name, department
)
select
    left(md5(i::text), 10),
    md5(random()::text),
    md5(random()::text),
    left(md5(random()::text), 4)
from generate_series(1, 1000000) s(i)

Since it idis serial, it does not need to be included.

+13
source

All Articles