PostgreSQL Generate_Series() 插入未完成

PostgreSQL Generate_Series() Insert Not Completing

所以我试图跳过使用外部脚本生成模拟数据,而是在 PostgreSQL 中使用 generate_series()。如果我确实尝试更少的行,最多它会返回 "could not write block: temporary log file...not enough space on device".

代码:

CREATE TABLE posts(
    id INTEGER PRIMARY KEY,
    author VARCHAR(20),
    likes INTEGER,
    content VARCHAR(200),
    posted TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

INSERT INTO posts
SELECT DISTINCT id, author, likes, content, posted FROM 
    generate_series(1,10000) AS id, substr(md5(random()::text), 0, 20) AS 
    author, generate_series(1,10000) AS likes, md5(random()::text) AS 
    content, generate_series('2007-02-01'::timestamp, 
    '2018-04-01'::timestamp, '1 hour') AS posted;

我能想到的几种可能:

通过执行您在 from 子句中执行的操作,您将获得生成的所有集合的笛卡尔积。如果您只想生成 10000 行,那么下面就是您想要的。

INSERT INTO posts
SELECT id, substr(md5(random()::text), 0, 20) AS author, (random() * 100)::integer AS likes, 
    md5(random()::text) AS content, '2007-02-01'::timestamp + (id * '1 hour'::interval) AS posted 
FROM 
    generate_series(1,10000) AS id