그래서 현재 테이블 정의를 빌드하기 위해 postgres (9.1) 카탈로그를 읽는 SQL을 만들고 있습니다. 그러나 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;