PG运维篇--常用管理命令

Posted 进击的CJR

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PG运维篇--常用管理命令相关的知识,希望对你有一定的参考价值。

查看系统信息

版本查看

查看当前数据库实例的版本信息

postgres=# select version();
version
----------------------------------------------------------------------------------------------------------
PostgreSQL 12.10 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
(1 row)


时间相关

查看数据库的启动时间

postgres=# select pg_postmaster_start_time();
pg_postmaster_start_time
-------------------------------
2022-05-21 22:28:29.200415+08
(1 row)


查看最后load配置文件的时间

postgres=#  select pg_conf_load_time();
pg_conf_load_time
-------------------------------
2022-05-21 22:28:29.173032+08
(1 row)


使用pg_ctl reload后会改变配置的装载时间

[postgres@VM-16-10-centos ~]$ pg_ctl reload -D /opt/pgdata
server signaled
postgres=#  select pg_conf_load_time();
pg_conf_load_time
-------------------------------
2022-07-19 09:15:22.721516+08
(1 row)


显示当前数据库时区

postgres=#  show timezone;
TimeZone
---------------
Asia/Shanghai
(1 row)

注意,数据库的时区有时并不是当前操作系统的时区,此时在数据库中看到的时间就与在操作系统中看到的不一致


库相关

查看当前实例中有哪些数据库

postgres=# \\l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
db01 | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
mydb | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
mydb3 | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
osdba2 | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
osdba3 | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
osdba4 | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
(9 rows)


查询当前连接的数据库名称

postgres=> select current_catalog, current_database();
current_catalog | current_database
-----------------+------------------
postgres | postgres
(1 row)

注意,使用current_catalog与current_database()都显示当前连接的数据库名称,两者的功能完全相同,只不过catalog是SQL标准中的用语。


用户相关

查看当前用户

postgres=# select user;
user
----------
postgres
(1 row)

postgres=# select current_user;
current_user
--------------
postgres
(1 row)


注意,通常情况下“session_user”与“user”是相同的。但当用命令“SET ROLE”改变用户的角色时,这两者就不同了

postgres=# set role cjr
postgres-# ;
SET
postgres=> select session_user;
session_user
--------------
postgres
(1 row)

postgres=> select current_user;
current_user
--------------
cjr
(1 row)

session_user始终是原始用户,而user是当前的角色用户




连接信息相关

查询当前session所在客户端的IP地址及端口

select inet_client_addr(),inet_client_port();
inet_client_addr | inet_client_port
------------------+------------------
|
(1 row)


查询当前数据库服务器的IP地址及端口

select inet_server_addr(),inet_server_port();


查询当前session的后台服务进程的PID

postgres=# select pg_backend_pid();
pg_backend_pid
----------------
28859
(1 row)


参数配置相关

查看当前参数配置情况

show shared_buffers;
select current_setting(shared_buffers);


修改当前session的参数配置

set maintenance_work_mem to 128MB;

SELECT set_config(maintenance_work_mem, 128MB, false);
set_config
------------
128MB
(1 row)


wal日志相关

查看当前正在写的WAL文件

select pg_xlogfile_name(pg_current_xlog_location());


查看当前WAL文件的buffer中还有多少字节的数据没有写入磁盘中

select pg_xlog_location_diff(pg_current_xlog_insert_location(), pg_current_xlog_location());


库状态相关

查看数据库实例是否正在做基础备份

select pg_is_in_backup(), pg_backup_start_time() ;
pg_is_in_backup | pg_backup_start_time
-----------------+----------------------
f |
(1 row)


查看当前数据库实例处于Hot Standby状态还是正常数据库状态

select pg_is_in_recovery();
pg_is_in_recovery
-------------------
f
(1 row)


数据库对象size查询

查看数据库的大小

select pg_database_size(osdba), pg_size_pretty(pg_database_size(osdba));


查看表的大小

select pg_size_pretty(pg_relation_size(ipdb2)) ;
select pg_size_pretty(pg_total_relation_size(ipdb2)) ;

上例中,pg_relation_size()仅计算表的大小,不包括索引的大小,而pg_total_relation_size()则会把表上索引的大小也计算进来。


查看表上所有索引的大小

elect pg_size_pretty(pg_indexes_size(ipdb2));

pg_indexes_size()函数的参数名是一个表对应的OID(输入表名会自动转换成表的OID),而不是索引的名称。


查看表空间的大小

select pg_size_pretty(pg_tablespace_size(pg_global));
select pg_size_pretty(pg_tablespace_size(pg_default));


查看表对应的数据文件

select pg_relation_filepath(test01);


系统维护常用命令


配置文件生效

修改配置文件“postgresql.conf”后,要想让修改生效,有以下两种方法

方法一:在操作系统下使用如下命令:

pg_ctl reload

方法二:在psql中使用如下命令:

select pg_reload_conf();

注意,如果是需要重启数据库服务才能使修改生效的配置项,使用上面的方法无效。使用上面的方法能使修改生效的配置项都是不需要重启数据库服务就能使修改生效的配置项。


SQL执行相关

取消正在长时间执行的SQL命令的方法有以下两种。

·pg_cancel_backend(pid):取消一个正在执行的SQL命令。

·pg_terminate_backend(pid):终止一个后台服务进程,同时释放此后台服务进程的资源。

这两个函数的区别是,pg_cancel_backend()函数实际上是给正在执行的SQL任务置一个取消标志,正在执行的任务在合适的时候检测到此标志后会主动退出;但如果该任务没有主动检测到此标志就无法正常退出,此时就需要使用pg_terminate_backend命令来中止SQL命令的执行。

 通常先查询pg_stat_activity以找出长时间运行的SQL命令

select pid,usename,query_start, query from pg_stat_activity;

 然后再使用pg_cancel_backend()取消该SQL命令,如果pg_cancel_backend()取消失败,再使用pg_terminate_backend()

select pg_cancel_backend(567);
select pid,usename,query_start, query from pg_stat_activity;
select pg_terminate_backend(567);

 

WAL日志维护

wal日志介绍

wal日志即write ahead log预写式日志,简称wal日志。wal日志可以说是PostgreSQL中十分重要的部分,相当于oracle中的redo日志。
当数据库中数据发生变更时:
(1)change发生时:先要将变更后内容计入wal buffer中,再将变更后的数据写入data buffer;
(2)commit发生时:wal buffer中数据刷新到磁盘;
(3)checkpoint发生时:将所有data buffer刷新的磁盘。

查看 online wal日志

postgres=# select * from pg_ls_waldir() order by modification asc;
name | size | modification
--------------------------+----------+------------------------
00000001000000000000000C | 16777216 | 2021-11-23 15:13:51+08
00000001000000000000000B | 16777216 | 2021-11-23 15:13:51+08
00000001000000000000000E | 16777216 | 2021-11-23 15:13:52+08
00000001000000000000000D | 16777216 | 2021-11-23 15:13:53+08
00000001000000000000000A | 16777216 | 2021-12-07 10:39:16+08
(5 rows)


postgres=# select pg_walfile_name(pg_current_wal_lsn());
pg_walfile_name
--------------------------
00000001000000000000000A
(1 row)


postgres=# select pg_current_wal_lsn();
pg_current_wal_lsn
--------------------
0/A0001C0
(1 row)


wal日志大小设置

max_wal_size
在自动 WAL检查点之间允许WAL 增长到的最大尺寸。这是一个软限制,在特殊的情况 下 WAL 尺寸可能会超过max_wal_size, 例如在重度负荷下、archive_command失败或者高的 wal_keep_segments设置。默认为 1 GB。增加这个参数可能导致崩溃恢复所需的时间。

PG运维篇--常用管理命令_sql


wal日志切换

postgres=# select pg_switch_wal();
pg_switch_wal
---------------
0/A0001D8
(1 row)

postgres=# select * from pg_ls_waldir() order by modification asc;
name | size | modification
--------------------------+----------+------------------------
00000001000000000000000C | 16777216 | 2021-11-23 15:13:51+08
00000001000000000000000E | 16777216 | 2021-11-23 15:13:52+08
00000001000000000000000D | 16777216 | 2021-11-23 15:13:53+08
00000001000000000000000A | 16777216 | 2021-12-08 11:23:21+08
00000001000000000000000B | 16777216 | 2021-12-08 11:23:21+08


查看wal日志内容

pg_waldump 00000001000000000000000B

两个commit之间为事务  XID表示事务编号

PG运维篇--常用管理命令_数据库_02


开启归档

show archive_command ;

cd $PGDATA
[postgres@VM-0-9-centos pgdata]$ cat postgresql.conf |grep archive_mode
#archive_mode = off # enables archiving; off, on, or always
[postgres@VM-0-9-centos pgdata]$ vi postgresql.conf

archive_mode = on
archive_command = cp %p /opt/arch/%f

配置完成之后,如下

PG运维篇--常用管理命令_数据库_03

PG运维篇--常用管理命令_数据库_04

再将库重启生效

pg_ctl -D /opt/pgdata restart &


测试归档开启之后,有没有将wal日志拷贝到归档目录之中


测试:

当前正在使用的wal日志为00000001000000000000000C,

将wal日志切换,切换后为00000001000000000000000D。

观察到归档目录中,已经将00000001000000000000000C拷贝到归档目录中

查看wal日志列表
postgres=# select * from pg_ls_waldir() order by modification asc;
name | size | modification
--------------------------+----------+------------------------
00000001000000000000000E | 16777216 | 2021-11-23 15:13:52+08
00000001000000000000000D | 16777216 | 2021-11-23 15:13:53+08
00000001000000000000000F | 16777216 | 2021-12-08 11:23:21+08
00000001000000000000000B | 16777216 | 2021-12-08 12:21:49+08
00000001000000000000000C | 16777216 | 2021-12-08 12:21:53+08
(5 rows)

进行切换
postgres=# select pg_switch_wal();
pg_switch_wal
---------------
0/C000078
(1 row)

再次查看wal日志列表
postgres=# select * from pg_ls_waldir() order by modification asc;
name | size | modification
--------------------------+----------+------------------------
00000001000000000000000E | 16777216 | 2021-11-23 15:13:52+08
00000001000000000000000F | 16777216 | 2021-12-08 11:23:21+08
00000001000000000000000B | 16777216 | 2021-12-08 12:21:49+08
00000001000000000000000C | 16777216 | 2021-12-08 12:22:08+08
00000001000000000000000D | 16777216 | 2021-12-08 12:22:14+08

(5 rows)

以上是关于PG运维篇--常用管理命令的主要内容,如果未能解决你的问题,请参考以下文章

PG运维篇--逻辑备份和物理备份实战

PG运维篇--服务配置参数

运维篇-------后台运行

Exchange Server 2016运维篇三:恢复删除的邮箱

运维篇-虚拟机docker宝塔安装及端口映射

RabbitMQ运维篇之部署与用户管理