我可以运行以下行:
ALTER SEQUENCE seqName OWNED BY table.id;
如何获得由
OWNED BY
设置的序列的“所有者”(在本例中:table.id
)?
您可以使用以下查询:
select s.relname as seq, n.nspname as sch, t.relname as tab, a.attname as col
from pg_class s
join pg_depend d on d.objid=s.oid and d.classid='pg_class'::regclass and d.refclassid='pg_class'::regclass
join pg_class t on t.oid=d.refobjid
join pg_namespace n on n.oid=t.relnamespace
join pg_attribute a on a.attrelid=t.oid and a.attnum=d.refobjsubid
where s.relkind='S' and d.deptype='a'
它返回带有所有者信息的所有序列。只需在 WHERE 子句中过滤它们即可。
ALTER SEQUENCE seqName OWNED BY table.id;
您的
ALTER SEQUENCE
语句会在系统目录 pg_depend
中生成一个条目,其依赖项类型为 (deptype
) 'a' 且 refobjsubid
大于 0,指向属性编号 ( attnum
)在pg_attribute
。有了这些知识,您就可以设计一个简单的查询:
SELECT d.refobjid::regclass, a.attname
FROM pg_depend d
JOIN pg_attribute a ON a.attrelid = d.refobjid
AND a.attnum = d.refobjsubid
WHERE d.objid = 'public."seqName"'::regclass -- your sequence here
AND d.refobjsubid > 0
AND d.classid = 'pg_class'::regclass;
双引号 (
""
) 仅用于其他非法名称(大小写混合、保留字...)。
无需断言
refclassid
属于 regclass
类型,因为与 pg_attribute
的连接会自动执行此操作。pg_class
或 pg_namespace
。
架构名称仅用于消除歧义或者不在
search_path
中。regclass
会观察当前 search_path
以选择最佳匹配。如果该表不可见,您会收到一条错误消息。
此外,
regclass
类型会自动向用户显示为text
。 (如果没有,请转换为 text
。)架构名称会在必要时自动添加到前面,以便在会话中明确无误。
根据要求获取拥有特定序列的角色:
SELECT c.relname, u.usename
FROM pg_class c
JOIN pg_user u ON u.usesysid = c.relowner
WHERE c.oid = '"seqName"'::regclass; -- your sequence here
我能够使用以下 SQL 语句列出表和特定列的相应序列:
SELECT table_schema
, table_name
, column_name
, LTRIM(RTRIM(RTRIM(column_default, '::regclass)'),''''),'nextval(''') AS SEQUENCE_NAME
FROM information_schema.columns
WHERE column_default like '%nextval%';
SELECT c.relname,u.usename
FROM pg_class c, pg_user u
WHERE c.relowner = u.usesysid and c.relkind = 'S'
AND relnamespace IN (
SELECT oid
FROM pg_namespace
WHERE nspname NOT LIKE 'pg_%'
AND nspname != 'information_schema'
);
我使用该查询来获取所有查询。
更改 CTE 以过滤结果
WITH table_with_sequence as (
SELECT
d.refobjid::regclass::text tablename,
c.relname::text sequencename,
np.nspname::text schemaname,
a.attname::text attname,
u.usename::text
FROM
pg_depend d
INNER JOIN pg_class c ON c.oid = d.objid
AND c.relkind = 'S'
INNER JOIN pg_namespace np ON np.oid = c.relnamespace
AND (np.nspname NOT LIKE 'pg_%'
AND np.nspname != 'information_schema')
INNER JOIN pg_user u ON u.usesysid = c.relowner
INNER JOIN pg_attribute a ON a.attrelid = d.refobjid
AND a.attnum = d.refobjsubid
)
SELECT
'ALTER SEQUENCE '|| QUOTE_LITERAL(QUOTE_IDENT(schemaname) || '.' || QUOTE_IDENT(sequencename)) ||' OWNED BY ' || tablename || '.' || QUOTE_IDENT(attname)
FROM table_with_sequence
这是我获取所需所有信息的查询: 选择 t1.表架构, t1.TABLE_NAME, t1.COLUMN_NAME, t1.column_default, t2.schemaname AS SCHEMA, t2.sequencename 作为序列, t2.LAST_VALUE , t2.模式名称 从 information_schema.COLUMNS t1, pg_序列 t2 在哪里 t1.table_schema = t2.schemaname AND POSITION(t2.sequencename IN t1.column_default) >0 AND t1.column_default ILIKE'%nextval%' AND t2.LAST_VALUE 不为空 AND t2.schemaname 不在 ('tiger');