Reputation: 2084
I want to generate big data sample (almost 1 million records) for studying tuplesort.c's polyphase merge in postgresql, and I hope the schema as follows:
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 do it? thanks a lot!
Upvotes: 67
Views: 61331
Reputation: 125284
To insert one million rows into 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 id
is a serial
it is not necessary to include it.
left ( string text, n integer ) → text
left('abcde', 2)
→ abmd5 ( text ) → text
md5('abc')
→ 900150983cd24fb0d6963f7d28e17f72generate_series ( start integer, stop integer [, step integer ] ) → setof integer
Generates a series of values from start to stop, with a step size of step. step defaults to 1.
Refs
Upvotes: 141