增加Postgresql数据库中的所有序列。

huangapple go评论59阅读模式
英文:

Increment all sequences in Postgresql DB

问题

我正在使用AWS DMS迁移一个PostgreSQL数据库。源数据库位于Aurora Postgres 10.xx上,目标数据库位于Aurora Postgres 12.xx上。

由于DMS不同步序列,所以我正在使用以下查询(在目标数据库上)来创建动态SQL查询,这些查询将通过与关联列的最大值+1000增加我的序列:

SELECT 
    'SELECT SETVAL(' ||
       quote_literal(quote_ident(sequence_namespace.nspname) || '.' || quote_ident(class_sequence.relname)) ||
       ', COALESCE(MAX(' ||quote_ident(pg_attribute.attname)|| '), 1)+ 1000 ) FROM ' || -- 缓冲1000
       quote_ident(table_namespace.nspname)|| '.'||quote_ident(class_table.relname)|| ';'
FROM pg_depend 
    INNER JOIN pg_class AS class_sequence
        ON class_sequence.oid = pg_depend.objid 
            AND class_sequence.relkind = 'S'
    INNER JOIN pg_class AS class_table
        ON class_table.oid = pg_depend.refobjid
    INNER JOIN pg_attribute 
        ON pg_attribute.attrelid = class_table.oid
            AND pg_depend.refobjsubid = pg_attribute.attnum
    INNER JOIN pg_namespace as table_namespace
        ON table_namespace.oid = class_table.relnamespace
    INNER JOIN pg_namespace AS sequence_namespace
        ON sequence_namespace.oid = class_sequence.relnamespace
    where sequence_namespace.nspname='public' -- 这是模式名称
ORDER BY sequence_namespace.nspname, class_sequence.relname;

这将返回总共15个SQL语句,这意味着在public模式中总共有15个序列。

现在,当我在同一个数据库上执行\ds public.*时,它给我返回25个序列。我无法弄清楚为什么SELECT查询无法选择所有25个序列。

有人可以帮忙吗?

谢谢!

英文:

I migrating a PostgreSQL DB using AWS DMS. The source DB is on Aurora Postgres 10.xx and the target DB is on Aurora Postgres 12.xx.

Since DMS does not sync sequences, so I am using the following query (on the target DB) to create Dynamic sql queries which will increment my sequences by max_value of the associated column + 1000:

SELECT 
    'SELECT SETVAL(' ||
       quote_literal(quote_ident(sequence_namespace.nspname) || '.' || quote_ident(class_sequence.relname)) ||
       ', COALESCE(MAX(' ||quote_ident(pg_attribute.attname)|| '), 1)+ 1000 ) FROM ' || -- Buffer of 1000
       quote_ident(table_namespace.nspname)|| '.'||quote_ident(class_table.relname)|| ';'
FROM pg_depend 
    INNER JOIN pg_class AS class_sequence
        ON class_sequence.oid = pg_depend.objid 
            AND class_sequence.relkind = 'S'
    INNER JOIN pg_class AS class_table
        ON class_table.oid = pg_depend.refobjid
    INNER JOIN pg_attribute 
        ON pg_attribute.attrelid = class_table.oid
            AND pg_depend.refobjsubid = pg_attribute.attnum
    INNER JOIN pg_namespace as table_namespace
        ON table_namespace.oid = class_table.relnamespace
    INNER JOIN pg_namespace AS sequence_namespace
        ON sequence_namespace.oid = class_sequence.relnamespace
    where sequence_namespace.nspname='public' -- This is the schema name
ORDER BY sequence_namespace.nspname, class_sequence.relname;

This returns a total of 15 SQL statements which means 15 sequences in total in the public schema.

Now when I do a \ds public.* on the same database, it gives me 25 sequences. I am not able to figure out why the SELECT query is not able to pick up all the 25 sequences.

Can someone please help here?

Thanks

答案1

得分: 1

只返回翻译好的部分:

你的查询仅返回由表列拥有的序列:

ALTER SEQUENCE seq OWNED BY tab.id;

其他序列似乎没有与表列的这种关联。

英文:

Your query only returns sequences that are owned by a table column:

ALTER SEQUENCE seq OWNED BY tab.id;

The other sequences seem not to have such a relationship to a table column.

huangapple
  • 本文由 发表于 2023年4月20日 03:32:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058199.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定