PostgreSQL实用查询SQL

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介:

统计了postgresql的系统表关联的常用SQL

select * from pg_database;

postgres=# select * from pg_database;

datname datdba encoding datcollate datctype datistemplate datallowconn datconnlimit datlastsysoid datfrozenxid datminmxid dattablespace datacl
postgres 10 6 Chinese (Simplified)_People's Republic of China.936 Chinese (Simplified)_People's Republic of China.936 f t -1 12937 549 1 1663
template1 10 6 Chinese (Simplified)_People's Republic of China.936 Chinese (Simplified)_People's Republic of China.936 t t -1 12937 549 1 1663 {=c/postgres,postgres=CTc/postgres}
template0 10 6 Chinese (Simplified)_People's Republic of China.936 Chinese (Simplified)_People's Republic of China.936 t f -1 12937 549 1 1663 {=c/postgres,postgres=CTc/postgres}

(3 行记录)

--查看表空间

postgres=# select * from pg_tablespace;

spcname spcowner spcacl spcoptions
pg_default 10
pg_global 10

(2 行记录)

--查看语言

postgres=# select * from pg_language;

lanname lanowner lanispl lanpltrusted lanplcallfoid laninline lanvalidator lanacl
internal 10 f f 0 0 2246
c 10 f f 0 0 2247
sql 10 f t 0 0 2248
plpgsql 10 t t 12925 12926 12927

(4 行记录)

--查看角色用户

select * from pg_user;

select * from pg_shadow;

select * from pg_roles;

--查看会话进程

select * from pg_stat_activity;

--查看表

SELECT * FROM pg_tables where schemaname = 'public';

--查看表字段

select * from information_schema.columns where table_schema = 'public' and table_name = 'pf_vip_org';

--查看视图

select * from pg_views where schemaname = 'public';

select * from information_schema.views where table_schema = 'public';

--查看触发器

select * from information_schema.triggers;

--查看序列

select * from information_schema.sequences where sequence_schema = 'public';

--查看约束

select * from pg_constraint where contype = 'p'

--u unique,p primary,f foreign,c check,t trigger,x exclusion

select a.relname as table_name,b.conname as constraint_name,b.contype as constraint_type from pg_class a,pg_constraint b where a.oid = b.conrelid and a.relname = 'cc';

--查看索引

select * from pg_index ;

--查看表上存在哪些索引以及大小

select relname,n.amname as index_type from pg_class m,pg_am n where m.relam = n.oid and m.oid in (

select b.indexrelid from pg_class a,pg_index b where a.oid = b.indrelid and a.relname = 'cc');

SELECT c.relname,c2.relname, c2.relpages*8 as size_kb

FROM pg_class c, pg_class c2, pg_index i

WHERE c.relname = 'cc' AND

c.oid = i.indrelid AND

c2.oid = i.indexrelid

ORDER BY c2.relname;

--查看索引定义

select b.indexrelid from pg_class a,pg_index b where a.oid = b.indrelid and a.relname = 'cc';

select pg_get_indexdef(b.indexrelid);

--查看过程函数定义

select oid,* from pg_proc where proname = 'insert_platform_action_exist'; --oid = 24610

select * from pg_get_functiondef(24610);

--查看表大小(不含索引等信息)

select pg_relation_size('cc'); --368640 byte

select pg_size_pretty(pg_relation_size('cc')) --360 kB

--查看DB大小

select pg_size_pretty(pg_database_size('smiletao')); --12M

--查看服务器DB运行状态

[postgres@eyar ~]$ pg_ctl status -D $PGDATA

pg_ctl: server is running (PID: 2373)

/home/postgres/bin/postgres "-D" "/database/pgdata"

--查看每个DB的使用情况(读,写,缓存,更新,事务等)

select * from pg_stat_database

--查看索引的使用情况

select * from pg_stat_user_indexes;

--查看表所对应的数据文件路径与大小

SELECT pg_relation_filepath(oid), relpages FROM pg_class WHERE relname = 'empsalary';

--查看索引与相关字段及大小

SELECT n.nspname AS schema_name,

    r.rolname as table_owner,

   bc.relname AS table_name,

   ic.relname AS index_name,

   a.attname  AS column_name,

   bc.relpages*8 as index_size_kb     

FROM pg_namespace n,

   pg_class bc,             -- base class

   pg_class ic,             -- index class

   pg_index i,

   pg_attribute a,           -- att in base

   pg_roles r

WHERE bc.relnamespace = n.oid

 and i.indrelid = bc.oid

 and i.indexrelid = ic.oid

 and bc.relowner = r.oid

 and i.indkey[0] = a.attnum

 and i.indnatts = 1

 and a.attrelid = bc.oid

 and n.nspname = 'public'

 and bc.relname = 'cc'

ORDER BY schema_name, table_name, index_name, attname;

--查看PG锁

select * from pg_locks;

备注:relpages*8 是实际所占磁盘大小

--查看表空间大小

select pg_tablespace_size('pg_default');

--查看序列与表的对应关系

WITH fq_objects AS (SELECT c.oid,c.relname AS fqname ,

                       c.relkind, c.relname AS relation

                FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace ),
 sequences AS (SELECT oid,fqname FROM fq_objects WHERE relkind = 'S'), 

 tables    AS (SELECT oid, fqname FROM fq_objects WHERE relkind = 'r' ) 

     SELECT

   s.fqname AS sequence,

   '->' as depends,

   t.fqname AS table

  FROM

   pg_depend d JOIN sequences s ON s.oid = d.objid 

             JOIN tables t ON t.oid = d.refobjid 

      WHERE

   d.deptype = 'a' and t.fqname = 'cc';
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
2天前
|
缓存 关系型数据库 数据库
关系型数据库高效查询和统计
【5月更文挑战第8天】关系型数据库高效查询和统计
21 7
|
21小时前
|
SQL 关系型数据库 MySQL
MYSQL根据查询结果删除sql 去除重复id 新增对比前一条与后一条数据 去重3种方法​ 窗口函数
MYSQL根据查询结果删除sql 去除重复id 新增对比前一条与后一条数据 去重3种方法​ 窗口函数
|
1天前
|
存储 SQL 关系型数据库
关系型数据库结构化数据存储查询方式
【5月更文挑战第10天】关系型数据库结构化数据存储查询方式
16 2
|
2天前
|
SQL Java 关系型数据库
Mybatis多表关联查询与动态SQL(下)
Mybatis多表关联查询与动态SQL
16 0
|
2天前
|
SQL Java 数据库连接
Mybatis多表关联查询与动态SQL(上)
Mybatis多表关联查询与动态SQL
9 0
|
2天前
|
存储 SQL 关系型数据库
关系型数据库强大的查询功能
【5月更文挑战第9天】关系型数据库强大的查询功能
8 3
|
2天前
|
SQL 分布式计算 DataWorks
实时数仓 Hologres产品使用合集之查询分区表的生命周期(即之前设置的'auto_partitioning.num_retention'值)的SQL语句,可以使用什么查询
实时数仓Hologres是阿里云推出的一款高性能、实时分析的数据库服务,专为大数据分析和复杂查询场景设计。使用Hologres,企业能够打破传统数据仓库的延迟瓶颈,实现数据到决策的无缝衔接,加速业务创新和响应速度。以下是Hologres产品的一些典型使用场景合集。
17 0
|
2天前
|
SQL 关系型数据库 数据管理
Microsoft SQL Server 是微软公司开发的一款关系型数据库管理系统
【5月更文挑战第14天】Microsoft SQL Server 是微软公司开发的一款关系型数据库管理系统
13 2
|
2天前
|
存储 Cloud Native 关系型数据库
PolarDB-X 是面向超高并发、海量存储和复杂查询场景设计的云原生分布式数据库系统
【5月更文挑战第14天】PolarDB-X 是面向超高并发、海量存储和复杂查询场景设计的云原生分布式数据库系统
30 2
|
2天前
|
SQL 前端开发
基于jeecgboot复杂sql查询的列表自定义列实现
基于jeecgboot复杂sql查询的列表自定义列实现
11 0