postgresql?常用SQL语句小结(post office 翻译)这样也行?

随心笔谈2年前发布 编辑
160 0
🌐 经济型:买域名、轻量云服务器、用途:游戏 网站等 《腾讯云》特点:特价机便宜 适合初学者用 点我优惠购买
🚀 拓展型:买域名、轻量云服务器、用途:游戏 网站等 《阿里云》特点:中档服务器便宜 域名备案事多 点我优惠购买
🛡️ 稳定型:买域名、轻量云服务器、用途:游戏 网站等 《西部数码》 特点:比上两家略贵但是稳定性超好事也少 点我优惠购买



目录1、查询链接数2、查看死锁状态3、删除死锁进程4、备份数据库5、还原数据库6、插入数据插入单条数据插入多条数据从一张表查询到的数据插入到另一张表7 、查询pg中单张表的大小(不包含索引)8、查询数据库中所有表的大小9、按顺序查看索引10 、查询数据库的大小11、查询被锁定的表12 、查询一个Schema下面的所有表的总大小(单位MB,包括索引和数据)14 、查询表大小按大小排序并分离data与index
SELECT sum(numbackends) FROM pg_stat_database;
select pid,
? ? ? ?usename,
? ? ? ?pg_blocking_pids(pid) as blocked_by,
? ? ? ?query as blocked_query
from pg_stat_activity
where cardinality(pg_blocking_pids(pid)) > 0;
SELECT pg_cancel_backend(__pid__);
SELECT pg_terminate_backend(__pid__);
# sql文件
pg_dump dangerousdb > db.sql
# tar文件
pg_dump -U postgres -F c dangerousdb > dangerousdb.tar
# gz文件
pg_dump -U postgres dangerousdb | gzip > dangerousdb.gz
# 已经存在数据库
pg_restore -U postgres -Ft -d dbcooper < dbcooper.tar
# 创建新数据库
pg_restore -U postgres -Ft -C -d dbcooper < dbcooper.tar
#? ?
INSERT INTO TABLE_1
(
column_1,
column_2,
column_3
)
values(
column_1,
column_2,
column_3
)
INSERT INTO TABLE_1
(
column_1,
column_2,
column_3
)
values(
column_1,
column_2,
column_3
),(
column_1,
column_2,
column_3
)…
INSERT INTO TABLE_1
(
column_1,
column_2,
column_3
)
SELECT
column_1,
column_2,
column_3
FROM
TABLE_2
where TABLE_2条件;
select
? ? pg_size_pretty(pg_relation_size(‘schema.table_name’));
select
? ? relname,
? ? pg_size_pretty(pg_relation_size(relid))
from
? ? pg_stat_user_tables
where
? ? schemaname=’public’
order by
? ? pg_relation_size(relid) desc;
select
? ? indexrelname,
? ? pg_size_pretty(pg_relation_size(relid))
from
? ? pg_stat_user_indexes
where
? ? schemaname=’public’
order by
? ? pg_relation_size(relid) desc;
select
? ? pg_database.datname,
? ? pg_size_pretty (pg_database_size(pg_database.datname)) as size
from
? ? pg_database;
select
? ? pg_class.relname as table,
? ? pg_database.datname as database,
? ? pid,
? ? mode,
? ? granted
from
? ? pg_locks,
? ? pg_class,
? ? pg_database
where
? ? pg_locks.relation=pg_class.oid
? ? and pg_locks.database=pg_database.oid;
select
? ? schemaname ,
? ? round(sum(pg_total_relation_size(schemaname || ‘.’ || tablename))/ 1024 / 1024) “Size_MB”
from
? ? pg_tables
where
? ? schemaname='<schemaname>’
group by
? ? 1;

13 、查询所有表的大小并排序(包含索引)

select
? ? table_schema || ‘.’ || table_name as table_full_name,
? ? pg_size_pretty(pg_total_relation_size(‘”‘ || table_schema || ‘”.”‘ || table_name || ‘”‘)) as size
from
? ? information_schema.tables
order by
? ? pg_total_relation_size(‘”‘ || table_schema || ‘”.”‘ || table_name || ‘”‘) desc
limit 20;
select
? ? table_name,
? ? pg_size_pretty(table_size) as table_size,
? ? pg_size_pretty(indexes_size) as indexes_size,
? ? pg_size_pretty(total_size) as total_size
from
? ? (
? ? select
? ? ? ? table_name,
? ? ? ? pg_table_size(table_name) as table_size,
? ? ? ? pg_indexes_size(table_name) as indexes_size,
? ? ? ? pg_total_relation_size(table_name) as total_size
? ? from
? ? ? ? (
? ? ? ? select
? ? ? ? ? ? (‘”‘ || table_schema || ‘”.”‘ || table_name || ‘”‘) as table_name
? ? ? ? from
? ? ? ? ? ? information_schema.tables) as all_tables
? ? order by
? ? ? ? total_size desc) as pretty_sizes;

或者

select
? ? table_name,
? ? pg_size_pretty(table_size) as table_size,
? ? pg_size_pretty(indexes_size) as indexes_size,
? ? pg_size_pretty(total_size) as total_size
from
? ? (
? ? select
? ? ? ? table_name,
? ? ? ? pg_table_size(table_name) as table_size,
? ? ? ? pg_indexes_size(table_name) as indexes_size,
? ? ? ? pg_total_relation_size(table_name) as total_size
? ? from
? ? ? ? (
? ? ? ? select
? ? ? ? ? ? (” || table_schema || ‘.’ || table_name || ”) as table_name
? ? ? ? from
? ? ? ? ? ? information_schema.tables) as all_tables
? ? order by
? ? ? ? total_size desc) as pretty_sizes;

到此这篇关于postgresql 常用SQL语句小结的文章就介绍到这了,更多相关postgresql 常用SQL内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

© 版权声明

相关文章