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

相关文章

MySQL 分区与分库分表策略应用小结

《MySQL分区与分库分表策略应用小结》在大数据量、复杂查询和高并发的应用场景下,单一数据库往往难以满足性能和扩展性的要求,本文将详细介绍这两种策略的基本概念、实现方法及优缺点,并通过实际案例展示如... 目录mysql 分区与分库分表策略1. 数据库水平拆分的背景2. MySQL 分区策略2.1 分区概念

MySQL高级查询之JOIN、子查询、窗口函数实际案例

《MySQL高级查询之JOIN、子查询、窗口函数实际案例》:本文主要介绍MySQL高级查询之JOIN、子查询、窗口函数实际案例的相关资料,JOIN用于多表关联查询,子查询用于数据筛选和过滤,窗口函... 目录前言1. JOIN(连接查询)1.1 内连接(INNER JOIN)1.2 左连接(LEFT JOI

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2

MySQL中动态生成SQL语句去掉所有字段的空格的操作方法

《MySQL中动态生成SQL语句去掉所有字段的空格的操作方法》在数据库管理过程中,我们常常会遇到需要对表中字段进行清洗和整理的情况,本文将详细介绍如何在MySQL中动态生成SQL语句来去掉所有字段的空... 目录在mysql中动态生成SQL语句去掉所有字段的空格准备工作原理分析动态生成SQL语句在MySQL

MySQL中FIND_IN_SET函数与INSTR函数用法解析

《MySQL中FIND_IN_SET函数与INSTR函数用法解析》:本文主要介绍MySQL中FIND_IN_SET函数与INSTR函数用法解析,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一... 目录一、功能定义与语法1、FIND_IN_SET函数2、INSTR函数二、本质区别对比三、实际场景案例分

MySQL中的交叉连接、自然连接和内连接查询详解

《MySQL中的交叉连接、自然连接和内连接查询详解》:本文主要介绍MySQL中的交叉连接、自然连接和内连接查询,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、引入二、交php叉连接(cross join)三、自然连接(naturalandroid join)四

Mysql如何将数据按照年月分组的统计

《Mysql如何将数据按照年月分组的统计》:本文主要介绍Mysql如何将数据按照年月分组的统计方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql将数据按照年月分组的统计要的效果方案总结Mysql将数据按照年月分组的统计要的效果方案① 使用 DA

Mysql表如何按照日期字段的年月分区

《Mysql表如何按照日期字段的年月分区》:本文主要介绍Mysql表如何按照日期字段的年月分区的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、创键表时直接设置分区二、已有表分区1、分区的前置条件2、分区操作三、验证四、注意总结一、创键表时直接设置分区

mysql的基础语句和外键查询及其语句详解(推荐)

《mysql的基础语句和外键查询及其语句详解(推荐)》:本文主要介绍mysql的基础语句和外键查询及其语句详解(推荐),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录一、mysql 基础语句1. 数据库操作 创建数据库2. 表操作 创建表3. CRUD 操作二、外键

MySQL更新某个字段拼接固定字符串的实现

《MySQL更新某个字段拼接固定字符串的实现》在MySQL中,我们经常需要对数据库中的某个字段进行更新操作,本文就来介绍一下MySQL更新某个字段拼接固定字符串的实现,感兴趣的可以了解一下... 目录1. 查看字段当前值2. 更新字段拼接固定字符串3. 验证更新结果mysql更新某个字段拼接固定字符串 -