设为首页 收藏本站
查看: 606|回复: 0

[经验分享] postgresql的自增长id(获取最后插入id)

[复制链接]

尚未签到

发表于 2016-11-20 12:24:50 | 显示全部楼层 |阅读模式
FAQ: Using Sequences in PostgreSQL

Many of the questions asked in #postgresql revolve around using sequences in PostgreSQL. To avoid answering the same questions again and again, I thought it would be worthwhile to summarize the basic steps involving in using sequences in PostgreSQL.

What Is A Sequence?

A sequence is a special kind of database object designed for generating unique numeric identifiers. It is typically used to generate artificial primary keys. Sequences are similar, but not identical, to the AUTO_INCREMENT concept in MySQL.

How Do I Use A Sequence In A Table?

Sequences are most commonly used via the serial pseudotype. A serial is a special data type that encodes the following information:


  • it indicates that the values for the column will be generated by consulting the sequence
  • therefore, it creates a new sequence object, and sets the default value for the column to be the next value produced by the sequence
  • since a sequence always produces non-NULL values, it adds a NOT NULL constraint to the column
  • since the sequence that is produced is created "behind the scenes", PostgreSQL assumes that the sequence is only used to generate values for the table containing the serial column. Therefore, if this column is dropped, the sequence will be automatically removed.

For example, this command creates both a new table and a new sequence generator, and associates the sequence with the id column of the table:

test=# CREATE TABLE users (
test(#     id    SERIAL, -- assign each user a numeric ID
test(#     name  TEXT,
test(#     age   INT4
test(# );
NOTICE:  CREATE TABLE will create implicit sequence
"users_id_seq" for serial column "users.id"
CREATE TABLE

In this case, the sequence is automatically assigned the name users_id_seq. To avoid hard-coding the name of the sequence in SQL queries, we can use thepg_get_serial_sequence() function, as described below.
Note that using serial does not implicitly create an index on the column, or mark the column as a primary key. That can be easily done, however:

CREATE TABLE users (
-- make the "id" column a primary key; this also creates
-- a UNIQUE constraint and a b+-tree index on the column
id    SERIAL PRIMARY KEY,
name  TEXT,
age   INT4
);

How Do I Assign Sequence Values To Newly-Inserted Rows?

If you're using serial, the default value for the serial column will be the next value produced by the sequence. To specify that an INSERT should take the default value for a given column, either omit that column from the INSERT's column list, or specify theDEFAULT keyword as the column's value.
Usage example:

INSERT INTO users (name, age) VALUES ('Mozart', 20);

Or equivalently:

INSERT INTO users (name, age, id) VALUES ('Mozart', 20, DEFAULT);

How Do I Retrieve The Most Recent Value Generated By A Sequence?

You can use the currval() function, which returns the most recent value generated by a sequence for the current session. currval() takes a single parameter: the name of the sequence. We can use the function pg_get_serial_sequence() to find the name of the sequence associated with a given serial column:

SELECT currval(pg_get_serial_sequence('users', 'id'));

Note that if no values have been generated by the sequence yet in the current session,currval() will yield an error.

Isn't This Subject To A Race Condition?

That is, if one database client inserts a row into a table that includes a sequence-generated value, wouldn't it be possible for another insertion into the table to modify the sequence, causing a subsequent currval() by the first client to return the wrong results?
No: sequences were designed to elegantly avoid this problem. currval() returns the last value generated by the sequence for the current session: if concurrent database clients generate sequence values, the currval() seen by a given session does not change (until the session generates a new sequence value, for example).

Doesn't Invoking currval() Require Two Database Queries?

To use the currval() method shown above, we'd need two queries: one to insert into the table, and another to fetch the sequence value assigned to the new row. Since client-server roundtrips can be expensive, this is not ideal. One way around this is to send the INSERT and the SELECT as a single query string. For example, in PHP:

pg_exec("INSERT INTO users (name, age) VALUES ('Bach', 15);
SELECT currval(pg_get_serial_sequence('users', 'id'));")

This executes two queries, but does only a single roundtrip between the client and server, so the additional performance overhead of the second query should be negligible.
Alternatively, users of PostgreSQL 8.2 and later can take advantage of the INSERT ... RETURNING clause:

INSERT INTO users (name, age) VALUES ('Liszt', 10) RETURNING id;

which returns the value of the id column for the newly-inserted row.

What Is The Range Of Values Generated By A Sequence?

Sequences generate 64-bit signed integers. The serial pseudotype that we used above is a 32-bit signed integer: if you want to use the full 64-bit range of the underlying sequence, use the serial8 pseudotype instead.

Can There Be "Gaps" In The Values Generated By A Sequence?

Yes, there can. Sequences are intended for generating unique identifiers — not necessarily identifiers that are strictly sequential. If two concurrent database clients both attempt to get a value from a sequence (using nextval()), each client will get a different sequence value. If one of those clients subsequently aborts their transaction, the sequence value that was generated for that client will be unused, creating a gap in the sequence.
This can't easily be fixed without incurring a significant performance penalty. For more information, see Elein Mustein's "Gapless Sequences for Primary Keys" in the General Bits Newsletter.

What About Transactions?

Sequence operations are essentially non-transactional. nextval() increments the value of the sequence and is not rolled back if its transaction is later aborted; currval()returns the last value generated by the sequence for the current session, regardless of transaction boundaries.

What If I Want To Share One Sequence Between Two Tables?

The easiest way to do this is to create the sequence by hand, and then set the default clauses for the sequence-generated columns by hand, rather than using the serial type:

CREATE SEQUENCE common_fruit_id_seq;
CREATE TABLE apples (
id      INT4 DEFAULT nextval('common_fruit_id_seq') NOT NULL,
price   NUMERIC
);
CREATE TABLE oranges (
id      INT4 DEFAULT nextval('common_fruit_id_seq') NOT NULL,
weight  NUMERIC
);

nextval() is a function that produces a new sequence value.
Note that when using sequences in this manner, the sequence won't be automatically dropped when the table is dropped, and you won't be able to usepg_get_serial_sequence().

Where Can I Find More Information About Sequences?

Consult the PostgreSQL documentation:


  • Serial Types

  • Sequence Manipulation Functions (currval(), nextval(), etc.)
  • CREATE SEQUENCE
  • DROP SEQUENCE
  本文来源:http://www.neilconway.org/docs/sequences/

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-302939-1-1.html 上篇帖子: PostgreSQL 9.2.3 Documentation 18.1. Setting Parameters 下篇帖子: PostgreSQL出现You must install at least one postgres
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表