PG 常用维护性 SQL

文章目录

  • 查看哪些角色对表有增删改查权限
  • 查看哪些角色对函数有执行权限
  • 根据序列名获取表及列信息
  • 查看postgresql数据库用户系统权限、对象权限
  • 查看所有主键及其相关字段信息
  • 查看 排除主键索引之外的 其他所有唯一性约束与唯一索引
  • 给 data 用户授予 create publication 权限
  • 统计当前库中每张表数据条数
  • 查询所有外键对应的表与列
  • 查看表所属 schmea 及其oid
  • 查询表是否有索引, 触发器等信息
  • 通过 SQL 查询表结构及其字段注释信息
  • 查看表的注释

查看哪些角色对表有增删改查权限

SELECT grantor, grantee, table_schema, table_name,  string_agg(privilege_type,',') as privilege_type
FROM information_schema.role_table_grants
group by grantor, grantee, table_schema, table_name;

查看哪些角色对函数有执行权限

SELECT
    routine_catalog AS fct_db,
    routine_schema  AS fct_sch,
    routine_name    AS fct_nam,
    privilege_type  AS fct_priv,
    array_agg (grantee::text ORDER BY grantee::text) AS fct_rol
FROM
    information_schema.routine_privileges
WHERE
    routine_schema NOT IN ('information_schema','pg_catalog')
GROUP BY
    routine_catalog, routine_schema, routine_name, privilege_type
ORDER BY
    routine_catalog, routine_schema, routine_name, privilege_type
;

根据序列名获取表及列信息

select ts.nspname as object_schema,
        tbl.relname as table_name, 
       col.attname as column_name,
       s.relname   as sequence_name
from pg_class s
  join pg_namespace sn on sn.oid = s.relnamespace 
  join pg_depend d on d.refobjid = s.oid and d.refclassid='pg_class'::regclass 
  join pg_attrdef ad on ad.oid = d.objid and d.classid = 'pg_attrdef'::regclass
  join pg_attribute col on col.attrelid = ad.adrelid and col.attnum = ad.adnum
  join pg_class tbl on tbl.oid = ad.adrelid 
  join pg_namespace ts on ts.oid = tbl.relnamespace 
where s.relkind = 'S'
--  and s.relname = 'sequence_name'
  and d.deptype in ('a', 'n');

参考:https://www.modb.pro/db/181436

查看postgresql数据库用户系统权限、对象权限

参考连接

查看所有主键及其相关字段信息

  • 方法1
select kcu.table_schema,
       kcu.table_name,
       tco.constraint_name,
       string_agg(kcu.column_name,', ') as key_columns
from information_schema.table_constraints tco
join information_schema.key_column_usage kcu 
     on kcu.constraint_name = tco.constraint_name
     and kcu.constraint_schema = tco.constraint_schema
     and kcu.constraint_name = tco.constraint_name
where tco.constraint_type = 'PRIMARY KEY'
group by tco.constraint_name,
       kcu.table_schema,
       kcu.table_name
order by kcu.table_schema,
         kcu.table_name;

参考: https://dataedo.com/kb/query/postgresql/list-all-primary-keys-in-database

  • 方法2
SELECT conrelid::regclass AS table_name,
       conname AS primary_key, 
       pg_get_constraintdef(oid) 
FROM   pg_constraint 
WHERE  contype = 'p' 
AND    connamespace = 'public'::regnamespace   
ORDER  BY conrelid::regclass::text, contype DESC; 

参考:https://soft-builder.com/how-to-list-all-primary-keys-in-postgresql-database/#:~:text=The%20following%20script%20can%20get%20all%20primary%20keys%3A,connamespace%20%3D%20%27public%27%3A%3Aregnamespace%20ORDER%20BY%20conrelid%3A%3Aregclass%3A%3Atext%2C%20contype%20DESC%3B

查看 排除主键索引之外的 其他所有唯一性约束与唯一索引

-- 获取 排除主键索引之外的其他的所有唯一性索引
select * from pg_indexes where schemaname='public' and indexname not in 
(
	with tmp as
	(
	select kcu.table_schema, kcu.table_name, tco.constraint_name, string_agg(kcu.column_name,', ') as key_columns from information_schema.table_constraints tco join information_schema.key_column_usage kcu on kcu.constraint_name = tco.constraint_name and kcu.constraint_schema = tco.constraint_schema and kcu.constraint_name = tco.constraint_name where tco.constraint_type = 'PRIMARY KEY' group by tco.constraint_name, kcu.table_schema, kcu.table_name order by kcu.table_schema, kcu.table_name
	)
	select constraint_name from tmp where table_schema='public' group by constraint_name
) and indexdef ilike '%UNIQUE%';

给 data 用户授予 create publication 权限

grant create on DATABASE ttp to ttpdata;

统计当前库中每张表数据条数

\o table_count.sql
select $$select '$$ || tablename ||  $$', count(*) from $$ || tablename from pg_tables where schemaname='public' order by tablename \gexec
\o

查询所有外键对应的表与列

SELECT conname "外键约束名", conrelid::regclass AS "表名", a1.attname AS "列名"  FROM pg_constraint c JOIN pg_stat_user_tables t ON t.relid = c.conrelid JOIN pg_attribute a1 ON a1.attnum = ANY(c.conkey) AND a1.attrelid = c.conrelid WHERE confrelid <> 0;
  • 授权序列访问权限
--授予当前 public 中所有序列访问权限
grant usage ,select , update on all sequences in schema public to test_user;

--授予未来 public 中所有序列访问权限
alter default privileges for user test_user in schema public grant select ,update,usage on SEQUENCES to test_user;
  • 授予 public 模式中所有表的 read 权限
--1. 授权已有表的只读权限给 用户
grant usage on schema public to test_user;
grant select on all tables in schema public to test_user;
 
--2. 授予未来新建的表的只读权限 给用户
alter default privileges 
[ for role xxdata ]-- 注意, 这里在多用户情况下, 是必须的, 否则会被当做这些 public 模式下的表是 postgres 创建的, 单用户模式下是可选的
in schema public grant select on tables to test_user;
 
--3. 回收 public 模式中所有表的 read 权限
revoke usage on schema public from test_user;
revoke select on all tables in schema public from test_user;
alter default privileges 
[ for role xxdata ] -- 注意, 这里在多用户情况下, 是必须的, 否则会被当做这些 public 模式下的表是 postgres 创建的, 单用户模式下是可选的
in schema public revoke select on tables from test_user;

查看表所属 schmea 及其oid

-- 假设查询的是 test 表
SELECT c.oid,
  n.nspname,
  c.relname
FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relname OPERATOR(pg_catalog.~) '^(test)$'
  AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 2, 3;

查询表是否有索引, 触发器等信息


SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, c.relhastriggers, c.relrowsecurity, c.relforcerowsecurity, false AS relhasoids, c.relispartition, pg_catalog.array_to_string(c.reloptions || array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')
, c.reltablespace, CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, c.relpersistence, c.relreplident, am.amname
FROM pg_catalog.pg_class c
 LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)
LEFT JOIN pg_catalog.pg_am am ON (c.relam = am.oid)
WHERE c.oid = (select oid from pg_class where relname OPERATOR(pg_catalog.~) '^(test)$');


通过 SQL 查询表结构及其字段注释信息

SELECT a.attname,
  pg_catalog.format_type(a.atttypid, a.atttypmod),
  (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),
  a.attnotnull,
  (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t
   WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) AS attcollation,
  a.attidentity,
  a.attstorage,
  CASE WHEN a.attstattarget=-1 THEN NULL ELSE a.attstattarget END AS attstattarget,
  pg_catalog.col_description(a.attrelid, a.attnum)
FROM pg_catalog.pg_attribute a
WHERE a.attrelid = (select oid from pg_class where relname OPERATOR(pg_catalog.~) '^(test)$')
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum;



--精简版
SELECT a.attname 字段名,
  pg_catalog.format_type(a.atttypid, a.atttypmod) 字段类型,
  a.attnotnull 字段是否非空,
  pg_catalog.col_description(a.attrelid, a.attnum) 字段注释
FROM pg_catalog.pg_attribute a
WHERE a.attrelid = (select oid from pg_class where relname OPERATOR(pg_catalog.~) '^(test)$')
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum;


查看表的注释

SELECT relname AS tabname,cast( obj_description ( relfilenode, 'pg_class' ) AS VARCHAR ) AS COMMENT  FROM pg_class c  WHERE	 relkind = 'r' AND relname ='test';

你可能感兴趣的:(postgresql)