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
相关推荐
传统上,为了获取下一个自增长ID,开发人员可能会选择查询表中的最大ID值,然后在此基础上加1。例如: ```sql SELECT MAX(id) + 1 AS NextID FROM TableXXX; ``` 这种方法简单直观,但在多用户环境中容易出现问题...
这个触发器会在每次向表 `T1` 插入新记录时自动为 `ID` 字段赋值。 #### MySQL MySQL 中自增字段的创建非常简单,只需要在字段定义中加入 `AUTO_INCREMENT` 关键词即可: ```sql CREATE TABLE T1 ( id INT NOT ...
在这个例子中,ID字段就是通过`SERIAL`关键字创建的序列,每次插入新记录时,PostgreSQL会自动为ID字段分配下一个可用的序列值。 插入记录时,我们不需要显式提供ID字段的值,PostgreSQL会自动处理。例如: ```sql...
在PostgreSQL中,表分区是一种优化数据库性能的技术,它允许将大表的数据分割成更小、更易管理的部分,每个部分称为一个分区。这有助于提高查询速度,减少维护成本,并优化存储空间。以下是对创建表分区的详细说明:...
在本文中,我们将深入探讨PostgreSQL,一个强大的开源对象关系型数据库系统。通过学习这篇"Postgresql学习笔记",你可以快速掌握其基本概念和常用操作,从而顺利入门。 首先,让我们从连接数据库开始。要使用`psql`...
使用`@Id`定义主键,如`@GeneratedValue(strategy=GenerationType.IDENTITY)`自动增长。 4. 映射文件:编写`hbm.xml`映射文件,将实体类与数据库表进行关联,或者使用注解方式直接在实体类上定义映射信息。 5. ...
综上所述,Hibernate提供了丰富的主键生成策略,涵盖了从简单的自动增长到复杂的分布式唯一ID生成。开发者应根据具体的应用需求和数据库特性,选择最合适的主键生成策略,以确保系统的稳定性和性能。无论是追求高...
1. 自动递增ID:优点在于插入速度较快,易于排序和分页;缺点是无法保证跨数据库或服务器的唯一性,且可能暴露数据增长情况。 2. UUID:优点是全局唯一,适合分布式环境;缺点是占用存储空间稍大,插入速度较慢,且...
例如,在大量并发插入的情况下,自动增长主键可能会成为性能瓶颈,因为每次插入都需要获取下一个主键值,可能导致锁竞争。此时,预生成一批主键或者使用UUID可能是更好的选择。 此外,对于分布式系统,全局唯一性的...
这个方法在处理数据库记录时非常有用,特别是当数据库表中有自动增长的主键时,我们可以用它来获取新插入记录的唯一标识。 ### 方法说明 `PDO::lastInsertId`的语法如下: ```php string PDO::lastInsertId ([ ...
为实现商品ID的自动增长,创建一个序列(book_seq),并在插入数据时使用这个序列。 3. 管理购物车:用户可以在购物车中修改商品数量或删除商品。购物车功能可能涉及到多个表,例如在订单详情表(ordersItem)中...
本文将详细介绍如何通过命令行工具(cmd)在PostgreSQL中进行基本的操作,包括创建用户、创建数据库、连接数据库、创建表、插入数据以及查询数据。 #### 二、创建用户 在PostgreSQL中,用户管理是十分重要的一步。...
此表包含四个字段:`id`为自动增长的主键,`username`和`email`用于存储用户名称和电子邮件地址,`created_at`为记录创建时间的时间戳字段。 - **插入数据** ```sql INSERT INTO users (username, email) ...
- `extrataskid`:额外的任务ID,可能是主键(PriKey)并自动递增(Auto_increment),表明这是任务表的主键,每次插入新任务时会自动增长。 - `taskname`:任务名称,Char(20),非空,表示每个任务都有一个独特的...
部署和实施简单,支持Catelet开发,类似数据库存储过程,用于跨分片复杂SQL的人工智能编码实现,支持NIO与AIO两种网络通信机制,支持MySQL存储过程调用,以插件方式支持SQL拦截和改写,支持自增长主键,支持Oracle的...
例如,创建新表、插入数据、更新现有数据以及删除不再需要的数据。 2. **数据库设计**:在MyKTV项目中,数据库设计是至关重要的。它涉及到对实体(如用户、歌曲、房间)的识别,以及它们之间的关系。常见的设计模式...
### PostGIS入门——GIS数据库的学习与应用 #### 一、PostGIS概述 **PostGIS**是在**PostgreSQL**对象-关系...随着技术的发展和应用需求的增长,**PostGIS**将继续发挥重要作用,并为用户提供更加丰富的功能和服务。
- **identity**: 使用数据库自身的自动增长机制,适用于 MySQL, SQL Server, DB2 等数据库。 - **sequence**: 使用序列生成主键,适用于 Oracle, PostgreSQL 等数据库。 - **native**: 根据不同的数据库自动选择合适...
- **IDENTITY/AUTO_INCREMENT**:在某些数据库系统中(如SQL Server和MySQL),可以为列设置自动增长属性,每次插入新记录时,该列的值会自动递增。 - **INDEX**:创建索引以加快查询速度,如`CREATE INDEX idx_...