इसलिए मैं वर्तमान में टेबल परिभाषाओं के निर्माण के लिए पोस्टग्रेज (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), टेबल नाम (अस्थायी), कॉलम नाम (आईडी), डिफ़ॉल्ट मान (अगलावल (...)), और डेटा प्रकार (bigint और int8 .. नहीं bigserial) देता है। ... मुझे एहसास है कि मैं सिर्फ यह देखने के लिए जांच कर सकता हूं कि क्या डिफ़ॉल्ट मान एक अनुक्रम था - लेकिन मुझे विश्वास नहीं है कि यह 100% सही होगा क्योंकि मैं मैन्युअल रूप से एक अनुक्रम बना सकता हूं और एक गैर सीरियल कॉलम बना सकता हूं जहां डिफ़ॉल्ट मान था वह क्रम।
क्या किसी के पास यह सुझाव है कि मैं इसे कैसे पूरा कर सकता हूं? एक अगले (* _ seq) के लिए डिफ़ॉल्ट मान की जाँच के अलावा कुछ भी?
एसएल समाधान के लिए संपादित टीएल के मामले में यहां जोड़ा गया है; DR या नए उपयोगकर्ता pg_catalog से अपरिचित हैं:
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;