Select multiple ids from a PostgreSQL sequence

后端 未结 5 2137
广开言路
广开言路 2021-01-01 11:54

Is there a concise way to select the nextval for a PostgreSQL sequence multiple times in 1 query? This would be the only value being returned.

For example, I would

5条回答
  •  自闭症患者
    2021-01-01 12:16

    There is a great article about this exact problem: "getting multiple values from sequences".

    If performance is not an issue, for instance when using the sequence values dwarfs the time used to get them or n is small, then the SELECT nextval('seq') FROM generate_series(1,n) approach is the simplest and most appropriate.

    But when preparing data for bulk loads the last approach from the article of incrementing the sequence by n from within a lock is appropriate.

提交回复
热议问题