PostgreSQL - 表中的下一个序列值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18232714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:21:37  来源:igfitidea点击:

PostgreSQL - next serial value in a table

postgresql

提问by Adrian

I have a simple question, suppose we have a table:

我有一个简单的问题,假设我们有一张桌子:

 id   A   B
 1   Jon  Doe
 2   Foo  Bar

Is there a way to know, which is the next id's increment, in this case 3 ? Database is PostgreSQL!

有没有办法知道下一个 id 的增量,在这种情况下是 3 ?数据库是 PostgreSQL!

Tnx alot!

Tnx很多!

回答by IMSoP

If you want to claim an ID and return it, you can use nextval(), which advances the sequence without inserting any data.

如果您想声明一个 ID 并返回它,您可以使用nextval(),它在不插入任何数据的情况下推进序列。

Note that if this is a SERIALcolumn, you need to find the sequence's name based on the table and column name, as follows:

请注意,如果这是SERIAL列,则需要根据表和列名查找序列的名称,如下所示:

Select nextval(pg_get_serial_sequence('my_table', 'id')) as new_id;

There is no cast-iron guarantee that you'll see these IDs come back in order (the sequence generates them in order, but multiple sessions can claim an ID and not use it yet, or roll back an INSERTand the ID will not be reused) but there is a guarantee that they will be unique, which is normally the important thing.

无法保证您会看到这些 ID 按顺序返回(序列按顺序生成它们,但多个会话可以声明一个 ID 并且尚未使用它,或者回滚一个INSERTID 并且该 ID 不会被重用) 但有一个保证,它们将是唯一的,这通常是重要的事情。

If you do this often without actually using the ID, you will eventually use up all the possible values of a 32-bit integercolumn (i.e. reach the maximum representable integer), but if you use it only when there's a high chance you will actually be inserting a row with that ID it should be OK.

如果你经常这样做而不实际使用 ID,你最终会用完 32 位列的所有可能值integer(即达到最大可表示的整数),但如果你只在很有可能的情况下使用它,你实际上会插入具有该 ID 的行应该没问题。

回答by Joachim Isaksson

To get the currentvalue of a sequence without affecting it or needing a previous insert in the same session, you can use;

要获取序列的当前值而不影响它或需要在同一会话中进行先前插入,您可以使用;

SELECT last_value FROM tablename_fieldname_seq;

An SQLfiddle to test with.

一个用于测试的 SQLfiddle

Of course, getting the current value will not guarantee that the next value you'll get is actually last_value + 1if there are other simultaneous sessions doing inserts, since another session may have taken the serial value before you.

当然,获取当前值并不能保证您得到的下一个值实际上last_value + 1是其他同时进行插入的会话,因为另一个会话可能已经在您之前获取了串行值。

回答by Ivan Krechetov

SELECT currval('names_id_seq') + 1;

SELECT currval('names_id_seq') + 1;

See the docs

查看文档

However, of course, there's no guarantee that it's going to be yournext value. What if another client grabs it before you? You can though reserve one of the next values for yourself, selecting a nextvalfrom the sequence.

但是,当然,不能保证它会成为您的下一个价值。如果另一个客户在你之前抢到它怎么办?您可以为自己保留下一个值,nextval从序列中选择一个。