我正在将数据库从postgres转换为mysql。
由于我找不到能自行解决问题的工具,因此我将使用autoincrement值将所有postgres序列转换为mysql中的autoincrement id。
因此,如何在Postgres DB(8.1版)中列出所有序列,以及有关使用该表的信息,下一个值等信息(通过SQL查询)?
请注意,我无法使用information_schema.sequences
8.4版本中的视图。
我正在将数据库从postgres转换为mysql。
由于我找不到能自行解决问题的工具,因此我将使用autoincrement值将所有postgres序列转换为mysql中的autoincrement id。
因此,如何在Postgres DB(8.1版)中列出所有序列,以及有关使用该表的信息,下一个值等信息(通过SQL查询)?
请注意,我无法使用information_schema.sequences
8.4版本中的视图。
Answers:
以下查询提供所有序列的名称。
SELECT c.relname FROM pg_class c WHERE c.relkind = 'S';
通常,序列称为${table}_id_seq
。简单的正则表达式模式匹配将为您提供表名。
要获取序列的最后一个值,请使用以下查询:
SELECT last_value FROM test_id_seq;
${table}_id_seq
提示是有用的
${table}_${column}_seq
自动创建的序列的方法
请注意,从PostgreSQL 8.4开始,您可以通过以下方式获取有关数据库中使用的序列的所有信息:
SELECT * FROM information_schema.sequences;
由于我使用的是更高版本的PostgreSQL(9.1),并且正在搜索相同的答案,因此,我为后代和将来的搜索者添加了此答案。
运行:psql -E
,然后\ds
有点痛苦后,我明白了。
实现此目的的最佳方法是列出所有表
select * from pg_tables where schemaname = '<schema_name>'
然后,对于每个表,列出所有具有属性的列
select * from information_schema.columns where table_name = '<table_name>'
然后,针对每一列,测试其是否具有序列
select pg_get_serial_sequence('<table_name>', '<column_name>')
然后,获取有关此序列的信息
select * from <sequence_name>
自动生成的序列(例如为SERIAL列创建的序列)与父表之间的关系由序列所有者属性建模。
您可以使用ALTER SEQUENCE命令的OWNED BY子句修改此关系
例如ALTER SEQUENCE foo_id由foo_schema.foo_table拥有
将其设置为链接到表foo_table
或ALTER SEQUENCE foo_id由NONE拥有
断开序列和任何表之间的连接
有关此关系的信息存储在pg_depend目录表中。
联接关系是pg_depend.objid-> pg_class.oid WHERE relkind ='S'之间的链接-将序列链接到联接记录,然后是pg_depend.refobjid-> pg_class.oid WHERE relkind ='r',该链接之间将记录加入拥有关系(表)
该查询返回数据库中的所有序列->表依赖性。where子句将其过滤为仅包括自动生成的关系,这将其限制为仅显示由SERIAL类型的列创建的序列。
WITH fq_objects AS (SELECT c.oid,n.nspname || '.' ||c.relname AS fqname ,
c.relkind, c.relname AS relation
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace ),
sequences AS (SELECT oid,fqname FROM fq_objects WHERE relkind = 'S'),
tables AS (SELECT oid, fqname FROM fq_objects WHERE relkind = 'r' )
SELECT
s.fqname AS sequence,
'->' as depends,
t.fqname AS table
FROM
pg_depend d JOIN sequences s ON s.oid = d.objid
JOIN tables t ON t.oid = d.refobjid
WHERE
d.deptype = 'a' ;
我知道这篇文章很老,但是我发现CMS的解决方案非常有用,因为我正在寻找一种将序列链接到表AND列的自动方法,并希望共享。使用pg_depend目录表是关键。我将所做的事情扩展到:
WITH fq_objects AS (SELECT c.oid,n.nspname || '.' ||c.relname AS fqname ,
c.relkind, c.relname AS relation
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace ),
sequences AS (SELECT oid,fqname FROM fq_objects WHERE relkind = 'S'),
tables AS (SELECT oid, fqname FROM fq_objects WHERE relkind = 'r' )
SELECT
s.fqname AS sequence,
'->' as depends,
t.fqname AS table,
a.attname AS column
FROM
pg_depend d JOIN sequences s ON s.oid = d.objid
JOIN tables t ON t.oid = d.refobjid
JOIN pg_attribute a ON a.attrelid = d.refobjid and a.attnum = d.refobjsubid
WHERE
d.deptype = 'a' ;
此版本将列添加到返回的字段列表中。有了表名和列名,对pg_set_serial_sequence的调用可以轻松确保正确设置数据库中的所有序列。例如:
CREATE OR REPLACE FUNCTION public.reset_sequence(tablename text, columnname text)
RETURNS void
LANGUAGE plpgsql
AS $function$
DECLARE
_sql VARCHAR := '';
BEGIN
_sql := $$SELECT setval( pg_get_serial_sequence('$$ || tablename || $$', '$$ || columnname || $$'), (SELECT COALESCE(MAX($$ || columnname || $$),1) FROM $$ || tablename || $$), true)$$;
EXECUTE _sql;
END;
$function$;
希望这对重置序列有帮助!
此语句列出与每个序列关联的表和列:
码:
SELECT t.relname as related_table,
a.attname as related_column,
s.relname as sequence_name
FROM pg_class s
JOIN pg_depend d ON d.objid = s.oid
JOIN pg_class t ON d.objid = s.oid AND d.refobjid = t.oid
JOIN pg_attribute a ON (d.refobjid, d.refobjsubid) = (a.attrelid, a.attnum)
JOIN pg_namespace n ON n.oid = s.relnamespace
WHERE s.relkind = 'S'
AND n.nspname = 'public'
更多请看这里链接答案
先前答案的改进:
select string_agg('select sequence_name, last_value from ' || relname, chr(13) || 'union' || chr(13) order by relname)
from pg_class where relkind ='S'
经过部分测试,但看起来大部分都完整。
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;
应归功于应归功的信贷...它是从已知\ d序列表中的\ d记录的SQL进行的反向工程。我敢肯定它也可以更清洁,但是嘿,性能不是问题。
通过解析DEFAULT子句按每个表的每个列获取序列。此方法提供有关链接到哪些列序列的信息,并且不使用某些序列可能不存在的依赖项。甚至pg_get_serial_sequence(sch.nspname||'.'||tbl.relname, col.attname)
函数对我来说也不是全部序列!
解:
SELECT
seq_sch.nspname AS sequence_schema
, seq.relname AS sequence_name
, seq_use."schema" AS used_in_schema
, seq_use."table" AS used_in_table
, seq_use."column" AS used_in_column
FROM pg_class seq
INNER JOIN pg_namespace seq_sch ON seq_sch.oid = seq.relnamespace
LEFT JOIN (
SELECT
sch.nspname AS "schema"
, tbl.relname AS "table"
, col.attname AS "column"
, regexp_split_to_array(
TRIM(LEADING 'nextval(''' FROM
TRIM(TRAILING '''::regclass)' FROM
pg_get_expr(def.adbin, tbl.oid, TRUE)
)
)
, '\.'
) AS column_sequence
FROM pg_class tbl --the table
INNER JOIN pg_namespace sch ON sch.oid = tbl.relnamespace
--schema
INNER JOIN pg_attribute col ON col.attrelid = tbl.oid
--columns
INNER JOIN pg_attrdef def ON (def.adrelid = tbl.oid AND def.adnum = col.attnum) --default values for columns
WHERE tbl.relkind = 'r' --regular relations (tables) only
AND col.attnum > 0 --regular columns only
AND def.adsrc LIKE 'nextval(%)' --sequences only
) seq_use ON (seq_use.column_sequence [1] = seq_sch.nspname AND seq_use.column_sequence [2] = seq.relname)
WHERE seq.relkind = 'S' --sequences only
ORDER BY sequence_schema, sequence_name;
请注意,1个序列可以在多个表中使用,因此可以在此处在多行中列出。
谢谢你的帮助。
这是pl / pgsql函数,用于更新数据库的每个序列。
---------------------------------------------------------------------------------------------------------
--- Nom : reset_sequence
--- Description : Générique - met à jour les séquences au max de l'identifiant
---------------------------------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION reset_sequence() RETURNS void AS
$BODY$
DECLARE _sql VARCHAR := '';
DECLARE result threecol%rowtype;
BEGIN
FOR result IN
WITH fq_objects AS (SELECT c.oid,n.nspname || '.' ||c.relname AS fqname ,c.relkind, c.relname AS relation FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace ),
sequences AS (SELECT oid,fqname FROM fq_objects WHERE relkind = 'S'),
tables AS (SELECT oid, fqname FROM fq_objects WHERE relkind = 'r' )
SELECT
s.fqname AS sequence,
t.fqname AS table,
a.attname AS column
FROM
pg_depend d JOIN sequences s ON s.oid = d.objid
JOIN tables t ON t.oid = d.refobjid
JOIN pg_attribute a ON a.attrelid = d.refobjid and a.attnum = d.refobjsubid
WHERE
d.deptype = 'a'
LOOP
EXECUTE 'SELECT setval('''||result.col1||''', COALESCE((SELECT MAX('||result.col3||')+1 FROM '||result.col2||'), 1), false);';
END LOOP;
END;$BODY$ LANGUAGE plpgsql;
SELECT * FROM reset_sequence();
此函数显示每个序列的last_value。
它输出一个2列的表格,上面写着序列名称及其最后生成的值。
drop function if exists public.show_sequence_stats();
CREATE OR REPLACE FUNCTION public.show_sequence_stats()
RETURNS TABLE(tablename text, last_value bigint)
LANGUAGE 'plpgsql'
COST 100
VOLATILE
ROWS 1000
AS $BODY$
declare r refcursor; rec record; dynamic_query varchar;
BEGIN
dynamic_query='select tablename,last_value from (';
open r for execute 'select nspname,relname from pg_class c join pg_namespace n on c.relnamespace=n.oid where relkind = ''S'' order by nspname';
fetch next from r into rec;
while found
loop
dynamic_query=dynamic_query || 'select '''|| rec.nspname || '.' || rec.relname ||''' "tablename",last_value from ' || rec.nspname || '.' || rec.relname || ' union all ';
fetch next from r into rec;
end loop;
close r;
dynamic_query=rtrim(dynamic_query,'union all') || ') x order by last_value desc;';
return query execute dynamic_query;
END;
$BODY$;
select * from show_sequence_stats();
假设exec()
在此帖子https://stackoverflow.com/a/46721603/653539中声明了函数,则可以使用单个查询来获取序列及其最后的值:
select s.sequence_schema, s.sequence_name,
(select * from exec('select last_value from ' || s.sequence_schema || '.' || s.sequence_name) as e(lv bigint)) last_value
from information_schema.sequences s
select sequence_name, (xpath('/row/last_value/text()', xml_count))[1]::text::int as last_value
from (
select sequence_schema,
sequence_name,
query_to_xml(format('select last_value from %I.%I', sequence_schema, sequence_name), false, true, '') as xml_count
from information_schema.sequences
where sequence_schema = 'public'
) new_table order by last_value desc;