是否可以在每个记录标签上使用PG序列?

dgo*_*o.a 5 sql postgresql concurrency database-design sequence

PostgreSQL 9.2+是否提供了任何功能,可以生成一个命名空间为特定值的序列?例如:

 .. | user_id | seq_id | body | ...
 ----------------------------------
  - |    4    |   1    |  "abc...."
  - |    4    |   2    |  "def...."
  - |    5    |   1    |  "ghi...."
  - |    5    |   2    |  "xyz...."
  - |    5    |   3    |  "123...."
Run Code Online (Sandbox Code Playgroud)

这对于为用户生成自定义URL非常有用:

domain.me/username_4/posts/1    
domain.me/username_4/posts/2

domain.me/username_5/posts/1
domain.me/username_5/posts/2
domain.me/username_5/posts/3
Run Code Online (Sandbox Code Playgroud)

我没有在PG文档中找到任何内容(关于序列和序列函数)来执行此操作.INSERT语句中的子查询或自定义PG功能是唯一的其他选项吗?

Fed*_*oli -2

是的:

CREATE TABLE your_table
(
    column type DEFAULT NEXTVAL(sequence_name),
    ...
);
Run Code Online (Sandbox Code Playgroud)

更多详细信息请参见: http://www.postgresql.org/docs/9.2/static/ddl-default.html