如何确定是否将列定义为串行数据类型而不是基于目录的整数?


9

因此,我目前正在创建一些SQL来阅读postgres(9.1)目录以构建表定义。但是,我遇到了SERIAL / BIGSERIAL数据类型的问题。

例:

CREATE TABLE cruft.temp ( id BIGSERIAL PRIMARY KEY );
SELECT * FROM information_schema.columns WHERE table_schema='cruft' AND table_name='temp';
"db","cruft","temp","id",1,"nextval('cruft.temp_id_seq'::regclass)","NO","bigint",,,64,2,0,,,,,,,,,,,,,"db","pg_catalog","int8",,,,,"1","NO","NO",,,,,,,"NEVER",,"YES"

它提供了数据库名称(db),模式名称(cruft),表名称(temp),列名称(id),默认值(nextval(...))和数据类型(bigint和int8 .. NOT bigserial)。 ...我意识到我可以检查默认值是否为序列-但我不认为这是100%准确的,因为我可以手动创建一个序列并创建一个默认值是那个顺序。

有人对我如何做到这一点有建议吗?除了检查nextval(* _ seq)的默认值之外,没有其他功能吗?

针对TL; DR或不熟悉pg_catalog的新用户在此处添加的SQL解决方案进行了编辑:

with sequences as (
  select oid, relname as sequencename from pg_class where relkind = 'S'
) select
  sch.nspname as schemaname, tab.relname as tablename, col.attname as columnname, col.attnum as columnnumber, seqs.sequencename
from pg_attribute col
join pg_class tab on col.attrelid = tab.oid
join pg_namespace sch on tab.relnamespace = sch.oid
left join pg_attrdef def on tab.oid = def.adrelid and col.attnum = def.adnum
left join pg_depend deps on def.oid = deps.objid and deps.deptype = 'n'
left join sequences seqs on deps.refobjid = seqs.oid
where sch.nspname != 'information_schema' and sch.nspname not like 'pg_%' -- won't work if you have user schemas matching pg_
  and col.attnum > 0
  and seqs.sequencename is not null -- TO ONLY VIEW SERIAL/BIGSERIAL COLUMNS
order by sch.nspname, tab.relname, col.attnum;

1
稍后,提供与代码示例相关的答案:dba.stackexchange.com/questions/90555/…–
Erwin Brandstetter,

Answers:


8

SERIALBIGSERIAL是一种伪类型。正如您所注意到的,它们在内部实际上只是INTBIGINT

幕后发生的事情是PostgreSQL创建了一个序列并建立了对表的依赖。您可以在pg_class中搜索序列名称及其与表的关系。

pg_class:http : //www.postgresql.org/docs/9.2/static/catalog-pg-class.html

SQL小提琴:http://sqlfiddle.com/#!12 / dfcbd / 6

序列函数:http : //www.postgresql.org/docs/9.2/static/functions-sequence.html

此StackOverflow帖子可能会有所帮助:https : //stackoverflow.com/questions/1493262/list-all-sequences-in-a-postgres-db-8-1-with-sql

更新:您还可以使用pg_depend来找出与您感兴趣的表/列相关的序列:http : //www.postgresql.org/docs/9.2/static/catalog-pg-depend.html


10

让我补充一下efesar的回答,该文档指出以下内容:

数据类型smallserial,serial和bigserial不是真正的类型,而仅仅是创建唯一标识符列的符号方便(类似于某些其他数据库支持的AUTO_INCREMENT属性)。在当前的实现中,指定:

CREATE TABLE tablename (
    colname SERIAL
);

等效于指定:

CREATE SEQUENCE tablename_colname_seq;
CREATE TABLE tablename (
    colname integer NOT NULL DEFAULT nextval('tablename_colname_seq')
);
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;

这意味着

  • 列的数据类型为整数(bigint),并且
  • 它不是NULL并且
  • 其默认值来自相关列所拥有的序列

然后是一serial列。因此,按照您的建议(添加NOT NULL)检查目录中的这些因素就足以识别一serial列。

有关查找(大)序列号的实际查询,请参见Erwin Brandstetter 的出色答案

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.