概述
本文档记录openGauss 3.0.0数据库每日一练第21天课程作业,学习检查数据库运行状态、锁信息、维护表、重建索引等数据库维护操作技术。
课程练习
使用操作系统命令和数据库工具检查实例状态
--操作系统命令查看实例状态
[root@ogauss2 ~]# ps -ef|grep gauss
root 557 30156 0 09:51 pts/0 00:00:00 grep --color=auto gauss
avahi 6216 1 0 12月12 ? 00:00:16 avahi-daemon: running [ogauss2.local]
omm 17322 1 0 12月12 ? 00:00:00 ssh-agent -a /home/omm/gaussdb_tmp/gauss_socket_tmp
omm 22633 1 13 12月12 ? 05:42:46 /opt/huawei/install/app/bin/gaussdb -D /opt/huawei/install/data/db1 -M primary
[root@ogauss2 ~]# ps -Tp 22633
PID SPID TTY TIME CMD
22633 22633 ? 00:00:01 gaussdb
22633 22634 ? 00:00:00 jemalloc_bg_thd
22633 22639 ? 00:00:00 gaussdb
22633 22640 ? 00:00:00 syslogger
22633 22641 ? 00:00:00 jemalloc_bg_thd
22633 22642 ? 00:00:22 alarm
22633 22643 ? 00:00:00 reaper
22633 22644 ? 00:00:00 jemalloc_bg_thd
22633 22646 ? 00:00:00 jemalloc_bg_thd
22633 24921 ? 00:00:19 startup
22633 24922 ? 00:00:07 checkpointer
22633 24923 ? 00:00:00 Spbgwriter
22633 24924 ? 00:04:59 pagerepair
22633 24925 ? 00:00:01 pagewriter
22633 24926 ? 00:00:01 pagewriter
22633 24927 ? 00:03:45 pagewriter
22633 24928 ? 00:00:01 pagewriter
22633 24929 ? 00:00:01 pagewriter
22633 24930 ? 00:00:00 statscollector
22633 24931 ? 00:00:14 WALwriteraux
22633 24932 ? 00:00:00 auditor
22633 24933 ? 00:00:20 heartbeat
22633 24934 ? 00:00:04 WALrecwriter
22633 24935 ? 00:32:27 WALreceiver
22633 25525 ? 01:14:58 pageredo
22633 25526 ? 01:14:58 pageredo
22633 25527 ? 01:14:54 pageredo
22633 25528 ? 01:14:57 pageredo
--使用数据库工具gs_ctl查看实例状态
[omm@ogauss1 ~]$ gs_ctl status -D /opt/huawei/install/data/db1
[2022-12-14 09:53:22.625][19500][][gs_ctl]: gs_ctl status,datadir is /opt/huawei/install/data/db1
gs_ctl: server is running (PID: 29053)
/opt/huawei/install/app/bin/gaussdb "-D" "/opt/huawei/install/data/db1" "-M" "primary"
[omm@ogauss1 ~]$ gs_om -t status --detail
[ Cluster State ]
cluster_state : Normal
redistributing : No
current_az : AZ_ALL
[ Datanode State ]
node node_ip port instance state
-----------------------------------------------------------------------------------------------
1 ogauss1 192.168.20.251 15400 6001 /opt/huawei/install/data/db1 P Primary Normal
2 ogauss2 192.168.20.252 15400 6002 /opt/huawei/install/data/db1 S Standby Normal
检查锁信息
--查询数据库中的锁信息
omm@local:/opt/huawei/tmp [postgres]=#\x
Expanded display is on.
omm@local:/opt/huawei/tmp [postgres]=# SELECT * FROM pg_locks;
-[ RECORD 1 ]------+------------------
locktype | relation
database | 15484
relation | 12010
page |
tuple |
bucket |
virtualxid |
transactionid |
classid |
objid |
objsubid |
virtualtransaction | 9/301193
pid | 140703053117184
sessionid | 140703053117184
mode | AccessShareLock
granted | t
fastpath | t
locktag | 3c7c:2eea:0:0:0:0
global_sessionid | 0:0#0
-[ RECORD 2 ]------+------------------
locktype | virtualxid
database |
relation |
page |
tuple |
bucket |
virtualxid | 9/301193
transactionid |
classid |
objid |
objsubid |
virtualtransaction | 9/301193
pid | 140703053117184
sessionid | 140703053117184
mode | ExclusiveLock
granted | t
fastpath | t
locktag | 9:49889:0:0:0:7
global_sessionid | 0:0#0
--查询等待锁的线程状态信息
omm@local:/opt/huawei/tmp [postgres]=# SELECT * FROM pg_thread_wait_status WHERE wait_status = 'acquire lock';
(No rows)
omm@local:/opt/huawei/tmp [postgres]=# SELECT * FROM pg_thread_wait_status WHERE wait_status = 'AccessShareLock';
(No rows)
omm@local:/opt/huawei/tmp [postgres]=# SELECT * FROM pg_thread_wait_status WHERE wait_status = 'ExclusiveLock';
(No rows)
查看数据库版本、日志信息
--版本
omm@local:/opt/huawei/tmp [postgres]=#SELECT version();
-[ RECORD 1 ]-------------------------------------------------------------------------------------------------------------------------------------------------
version | (openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:34 commit 0 last mr on x86_64-unknown-linux-gnu, compiled by g++ (GCC) 7.3.0, 64-bit
--在gsql中
omm@local:/opt/huawei/tmp [postgres]=#\! ls /opt/huawei/log/omm/pg_log/
dn_6001
----数据库日志位置
[omm@ogauss1 ~]$ ls /opt/huawei/log/omm/pg_log/
dn_6001
检查应用连接数、查看现有最大连接数
omm@local:/opt/huawei/tmp [postgres]=#SELECT count(*) FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;
count
-------
20
(1 row)
omm@local:/opt/huawei/tmp [postgres]=#SHOW max_connections;
max_connections
-----------------
5000
(1 row)
对表进行VACUUM、VACUUM FULL和ANALYZE操作
omm@local:/opt/huawei/tmp [postgres]=#CREATE TABLESPACE day21 RELATIVE LOCATION 'tablespace/day21';
CREATE TABLESPACE
omm@local:/opt/huawei/tmp [postgres]=#CREATE DATABASE musicdb21 WITH TABLESPACE = day21;
CREATE DATABASE
omm@local:/opt/huawei/tmp [postgres]=#CREATE USER user21 IDENTIFIED BY 'zs@123456';
CREATE ROLE
omm@local:/opt/huawei/tmp [postgres]=#ALTER USER user21 SYSADMIN;
ALTER ROLE
omm@local:/opt/huawei/tmp [postgres]=#CREATE TABLE tab_day21
openGauss-# ( c_customer_sk integer,
openGauss(# c_customer_id char(5),
openGauss(# c_first_name char(6),
openGauss(# c_last_name char(8)
openGauss(# ) ;^C
omm@local:/opt/huawei/tmp [postgres]=#
omm@local:/opt/huawei/tmp [postgres]=#
omm@local:/opt/huawei/tmp [postgres]=#\c musicdb21 user21
Password for user user21:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb21" as user "user21".
user21@local:/opt/huawei/tmp [musicdb21]=>CREATE TABLE tab_day21
musicdb21-> ( c_customer_sk integer,
musicdb21(> c_customer_id char(5),
musicdb21(> c_first_name char(6),
musicdb21(> c_last_name char(8)
musicdb21(> ) ;
CREATE TABLE
user21@local:/opt/huawei/tmp [musicdb21]=>INSERT INTO tab_day21 (c_customer_sk, c_customer_id, c_first_name,c_last_name) VALUES
musicdb21-> (6885, 1, 'Joes', 'Hunter'),
musicdb21-> (4321, 2, 'Lily','Carter'),
musicdb21-> (9527, 3, 'James', 'Cook'),
musicdb21-> (9500, 4, 'Lucy', 'Baker');
INSERT 0 4
user21@local:/opt/huawei/tmp [musicdb21]=>select count(*) from tab_day21;
count
-------
4
(1 row)
user21@local:/opt/huawei/tmp [musicdb21]=>
--使用VACUUM或VACUUM FULL命令,进行磁盘空间回收。
--使用VACUUM
user21@local:/opt/huawei/tmp [musicdb21]=>VACUUM tab_day21;
VACUUM
--使用VACUUM full
--需要向正在执行的表增加排他锁,且需要停止其他所有数据库操作。
user21@local:/opt/huawei/tmp [musicdb21]=>VACUUM FULL tab_day21;
VACUUM
--使用ANALYZE语句更新统计信息。
user21@local:/opt/huawei/tmp [musicdb21]=>ANALYZE tab_day21;
ANALYZE
--使用ANALYZE VERBOSE语句更新统计信息,并输出表的相关信息。
user21@local:/opt/huawei/tmp [musicdb21]=>ANALYZE VERBOSE tab_day21;
INFO: analyzing "public.tab_day21"(dn_6001_6002 pid=29053)
INFO: ANALYZE INFO : "tab_day21": scanned 1 of 1 pages, containing 4 live rows and 0 dead rows; 4 rows in sample, 4 estimated total rows(dn_6001_6002 pid=29053)
ANALYZE
--也可以同时执行VACUUM ANALYZE命令进行查询优化。
user21@local:/opt/huawei/tmp [musicdb21]=>VACUUM ANALYZE tab_day21;
VACUUM
建表、索引,进行重建索引操作
user21@local:/opt/huawei/tmp [musicdb21]=>CREATE INDEX IDX_DAY21_ ON tab_day21 (c_customer_id);
CREATE INDEX
user21@local:/opt/huawei/tmp [musicdb21]=>\di+
List of relations
Schema | Name | Type | Owner | Table | Size | Storage | Description
--------+------------+-------+--------+-----------+-------+---------+-------------
public | idx_day21_ | index | user21 | tab_day21 | 16 kB | |
(1 row)
user21@local:/opt/huawei/tmp [musicdb21]=>REINDEX table tab_day21;
REINDEX
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




