当前位置: 首页 > article >正文

PostgreSQL 常用运维SQL整理

一、查询并杀会话

-- 查询会话

select pid,usename,client_addr,client_port,query_start,query,wait_event from pg_stat_activity;

-- 杀会话

select pg_terminate_backend('pid号');

-- 使用如下命令自动生成杀会话语句

select datid,datname,pid,usesysid,usename,application_name,client_addr,client_port,state,query,'select pg_terminate_backend('||pid||');' kill_pid from pg_stat_activity;

-- 杀状态是空闲会话

select pg_terminate_backend(pid) from pg_stat_activity where state='idle';

-- 查询进程杀会话

select pid,state from  pg_stat_activity;

-- 然后通过 kill -9 pid 杀会话

二、查看库-表-schema大小

1) 查看表空间大小

select pg_size_pretty(pg_tablespace_size('pg_default'));

2) 查看所有数据库大小

select pg_size_pretty(sum(pg_database_size(oid))) from pg_database;

3)查看每个数据库大小

select datname,pg_size_pretty(pg_database_size(oid)) from pg_database;

4) 查看指定数据库大小

select pg_size_pretty(pg_database_size('db_hr'));

5) 查看每个schema大小

select schemaname,pg_size_pretty(cast(sum(pg_relation_size(schemaname||'.'||tablename))as bigint)) from pg_tables t inner join pg_namespace d on t.schemaname=d.nspname group by schemaname;

6) 查看所有表大小并大小排序

select tableowner,schemaname,tablename,pg_table_size(schemaname||'.'||tablename) as table_size from pg_tables order by table_size desc;

7) 查看指定schema下所有表大小

select schemaname || '.' || tablename tname, pg_size_pretty(pg_total_relation_size('"' || schemaname || '"."' || tablename || '"')) from pg_tables where schemaname = '模式名' order by pg_total_relation_size('"' || schemaname || '"."' || tablename || '"')  desc ;

8) 查看单张表大小

select pg_size_pretty(pg_table_size('表名'));

select tableowner,schemaname,tablename,pg_size_pretty(pg_table_size(schemaname||'.'||tablename)) as table_size from pg_tables where tablename='表名';

9) 查看表分区大小

select pg_size_pretty(pg_partition_size('表名',' 分区名'));

三、查询清理事务槽

1) 查询流复制槽

select pg_wal_lsn_diff(pg_current_wal_lsn(),restart_lsn), * from pg_replication_slots;

2) 清理流复制槽

xxxdb=# \c xxx

You are now connected to database "xxx" as user "antdb".

xxx=# SELECT pg_drop_replication_slot('test_decoding');

xxx=# checkpoint ;    --- 要执行checkpoint

四、清理归档日志

可以使用pg_archivecleanup  /xxx/xxx目录  最旧文件

通过 pg_controldata $PGDATA 命令查询检查点以前xlog文件,可以清理:如:

 折叠源码

[antdb@opensource-db ~]$ pg_controldata -D /database/antdb

pg_controldata: fatal: could not open file "/database/antdb/global/pg_control" for reading: No such file or directory

[antdb@opensource-db ~]$ pg_controldata -D /database/antdb/data

pg_control version number:            1300

Catalog version number:               202007201

Database system identifier:           7265663343146682289

Database cluster state:               in production

pg_control last modified:             Fri 11 Aug 2023 01:18:33 PM CST

Latest checkpoint location:           1F/5D1CFFD8

Latest checkpoint's REDO location:    1F/5D1CFFA0

Latest checkpoint's REDO WAL file:    000000010000001F0000005D

Latest checkpoint's TimeLineID:       1

Latest checkpoint's PrevTimeLineID:   1

Latest checkpoint's full_page_writes: on

Latest checkpoint's NextXID:          0:6059616

Latest checkpoint's NextOID:          24577

Latest checkpoint's NextMultiXactId:  1

Latest checkpoint's NextMultiOffset:  0

Latest checkpoint's oldestXID:        628

Latest checkpoint's oldestXID's DB:   1

Latest checkpoint's oldestActiveXID:  6059616

Latest checkpoint's oldestMultiXid:   1

Latest checkpoint's oldestMulti's DB: 1

Latest checkpoint's oldestCommitTsXid:0

Latest checkpoint's newestCommitTsXid:0

Time of latest checkpoint:            Fri 11 Aug 2023 01:18:24 PM CST

Fake LSN counter for unlogged rels:   0/3E8

Minimum recovery ending location:     0/0

Min recovery ending loc's timeline:   0

Backup start location:                0/0

Backup end location:                  0/0

End-of-backup record required:        no

wal_level setting:                    logical

wal_log_hints setting:                on

max_connections setting:              15000

max_worker_processes setting:         250

max_wal_senders setting:              64

max_prepared_xacts setting:           15000

max_locks_per_xact setting:           256

track_commit_timestamp setting:       off

Maximum data alignment:               8

Database block size:                  8192

Blocks per segment of large relation: 524288

WAL block size:                       65536

Bytes per WAL segment:                16777216

Maximum length of identifiers:        64

Maximum columns in an index:          32

Maximum size of a TOAST chunk:        1996

Size of a large-object chunk:         2048

Date/time type storage:               64-bit integers

Float8 argument passing:              by value

Data page checksum version:           0

Mock authentication nonce:            6b5a172bd5b19f946299ec8858f522c45266783226874df6aec06a0b9840e561

然后可以使用 g_archivecleanup /database/antdb/data/pg_wal/ 000000010000001F0000005D 清理 之前的日志


http://www.kler.cn/a/443651.html

相关文章:

  • linux RCU调优
  • [源码解析] 模型并行分布式训练Megatron (2) --- 整体架构
  • 4-pandas常用操作
  • Python OCR 文字识别
  • 原点安全再次入选信通院 2024 大数据“星河”案例
  • linux安装字体(亲测)
  • 3.zabbix中文设置
  • 洛谷 B3836 [GESP202303 二级] 百鸡问题
  • day-21 内核链表以及栈
  • CSS系列(17)-- 工具与生态系统详解
  • 【实用技能】如何在 Unity3D 中将网页内容渲染为纹理
  • ChatGPT与领域特定语言的集成
  • [手机Linux] 六,ubuntu18.04私有网盘(NextCloud)安装
  • MFC扩展库BCGControlBar Pro v36.0 - 工具栏 对话框组件升级
  • SQL Server 中对网络数据库文件的支持说明
  • @pathvariable什么作用
  • Vue3+Vite 环境变量和模式配置详解
  • C语言编程1.26判断八进制数字字符
  • ISP代理提供商及其作用
  • 详解负载均衡
  • 远程连接:构建智能家居舒适生活
  • 案例:Spark/Hive中‘String=数值类型’丢失精度问题
  • 电子应用设计方案-61:智能沙发系统方案设计
  • Unity常用面试问题
  • CSS的样式计算过程
  • 本地虚拟机 docker 中安装体验 qwen2.5 大模型