PG 常用维护性 SQL

2023-12-05 16:44
文章标签 sql 常用 pg database 维护性

本文主要是介绍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;

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

SELECTroutine_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
FROMinformation_schema.routine_privileges
WHEREroutine_schema NOT IN ('information_schema','pg_catalog')
GROUP BYroutine_catalog, routine_schema, routine_name, privilege_type
ORDER BYroutine_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 sjoin 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'::regclassjoin pg_attribute col on col.attrelid = ad.adrelid and col.attnum = ad.adnumjoin 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_nameand kcu.constraint_schema = tco.constraint_schemaand 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 cLEFT 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 cLEFT 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 dWHERE 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 tWHERE 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';

这篇关于PG 常用维护性 SQL的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/458341

相关文章

SQL server数据库如何下载和安装

《SQLserver数据库如何下载和安装》本文指导如何下载安装SQLServer2022评估版及SSMS工具,涵盖安装配置、连接字符串设置、C#连接数据库方法和安全注意事项,如混合验证、参数化查... 目录第一步:打开官网下载对应文件第二步:程序安装配置第三部:安装工具SQL Server Manageme

C#连接SQL server数据库命令的基本步骤

《C#连接SQLserver数据库命令的基本步骤》文章讲解了连接SQLServer数据库的步骤,包括引入命名空间、构建连接字符串、使用SqlConnection和SqlCommand执行SQL操作,... 目录建议配合使用:如何下载和安装SQL server数据库-CSDN博客1. 引入必要的命名空间2.

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

MySQL 多列 IN 查询之语法、性能与实战技巧(最新整理)

《MySQL多列IN查询之语法、性能与实战技巧(最新整理)》本文详解MySQL多列IN查询,对比传统OR写法,强调其简洁高效,适合批量匹配复合键,通过联合索引、分批次优化提升性能,兼容多种数据库... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

浅谈mysql的not exists走不走索引

《浅谈mysql的notexists走不走索引》在MySQL中,​NOTEXISTS子句是否使用索引取决于子查询中关联字段是否建立了合适的索引,下面就来介绍一下mysql的notexists走不走索... 在mysql中,​NOT EXISTS子句是否使用索引取决于子查询中关联字段是否建立了合适的索引。以下

Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式

《Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式》本文详细介绍如何使用Java通过JDBC连接MySQL数据库,包括下载驱动、配置Eclipse环境、检测数据库连接等关键步骤,... 目录一、下载驱动包二、放jar包三、检测数据库连接JavaJava 如何使用 JDBC 连接 mys

SQL中如何添加数据(常见方法及示例)

《SQL中如何添加数据(常见方法及示例)》SQL全称为StructuredQueryLanguage,是一种用于管理关系数据库的标准编程语言,下面给大家介绍SQL中如何添加数据,感兴趣的朋友一起看看吧... 目录在mysql中,有多种方法可以添加数据。以下是一些常见的方法及其示例。1. 使用INSERT I

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP