List all sequences in a Postgres db 8.1 with SQL

后端 未结 19 1737
旧时难觅i
旧时难觅i 2020-12-12 11:30

I\'m converting a db from postgres to mysql.

Since i cannot find a tool that does the trick itself, i\'m going to convert all postgres sequences to autoincrement id

19条回答
  •  借酒劲吻你
    2020-12-12 11:59

    Partially tested but looks mostly complete.

    select *
      from (select n.nspname,c.relname,
                   (select substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
                      from pg_catalog.pg_attrdef d
                     where d.adrelid=a.attrelid
                       and d.adnum=a.attnum
                       and a.atthasdef) as def
              from pg_class c, pg_attribute a, pg_namespace n
             where c.relkind='r'
               and c.oid=a.attrelid
               and n.oid=c.relnamespace
               and a.atthasdef
               and a.atttypid=20) x
     where x.def ~ '^nextval'
     order by nspname,relname;
    

    Credit where credit is due... it's partly reverse engineered from the SQL logged from a \d on a known table that had a sequence. I'm sure it could be cleaner too, but hey, performance wasn't a concern.

提交回复
热议问题