PG 常用维护性 SQL

文章目录

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

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;

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

sql 复制代码
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
;

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

sql 复制代码
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
sql 复制代码
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
sql 复制代码
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

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

sql 复制代码
-- 获取 排除主键索引之外的其他的所有唯一性索引
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 权限

sql 复制代码
grant create on DATABASE ttp to ttpdata;

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

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

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

sql 复制代码
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;
  • 授权序列访问权限
sql 复制代码
--授予当前 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 权限
sql 复制代码
--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

sql 复制代码
-- 假设查询的是 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;

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

sql 复制代码
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 查询表结构及其字段注释信息

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;

查看表的注释

sql 复制代码
SELECT relname AS tabname,cast( obj_description ( relfilenode, 'pg_class' ) AS VARCHAR ) AS COMMENT  FROM pg_class c  WHERE	 relkind = 'r' AND relname ='test';
相关推荐
阿乾之铭1 天前
通过Django 与 PostgreSQL 进行WEB开发详细流程
python·postgresql·django
TMDOG6661 天前
PostgreSQL 学习笔记:PostgreSQL 主从复制
笔记·学习·postgresql
大霸王龙1 天前
django+postgresql
数据库·后端·python·postgresql·django
喵手2 天前
PostgreSQL 增量备份:保护你的数据资产
数据库·postgresql
君败红颜3 天前
MySQL 和 PostgreSQL 的对比概述
数据库·mysql·postgresql
MMMMMMMMMMemory3 天前
服务器内存不够导致postgresql进程被kill的问题记录
运维·服务器·postgresql
PGCCC3 天前
【PGCCC】Postgresql 缓存池原理
spring·缓存·postgresql
DBA实战3 天前
PostgreSQL核心揭秘(二)-进程和内存架构
数据库·postgresql·架构
君败红颜3 天前
PostgreSQL 触发器的深入探讨
数据库·postgresql
高铭杰4 天前
Postgresql源码(137)执行器参数传递与使用
数据库·postgresql·参数·param·paramlistinfo