学习目标
学习检查数据库运行状态、锁信息、维护表、重建索引等数据库维护操作技术
课程作业
1、使用操作系统命令和数据库工具检查实例状态
root@modb:~# su - omm omm@modb:~$ ps -ef|grep gaussdb omm 1 0 0 10:01 ? 00:00:01 gaussdb omm 410 406 0 10:05 pts/0 00:00:00 grep gaussdb omm@modb:~$ ps -Tp 1 PID SPID TTY TIME CMD 1 1 ? 00:00:00 gaussdb 1 296 ? 00:00:00 alarm 1 297 ? 00:00:00 reaper 1 294 ? 00:00:00 syslogger 1 295 ? 00:00:00 jemalloc_bg_thd 1 298 ? 00:00:00 jemalloc_bg_thd 1 299 ? 00:00:00 jemalloc_bg_thd 1 287 ? 00:00:00 jemalloc_bg_thd 1 293 ? 00:00:00 gaussdb 1 343 ? 00:00:00 checkpointer 1 344 ? 00:00:00 Spbgwriter 1 345 ? 00:00:00 pagewriter 1 346 ? 00:00:00 pagewriter 1 350 ? 00:00:00 WALwriter 1 351 ? 00:00:00 WALwriteraux 1 352 ? 00:00:00 AVClauncher 1 347 ? 00:00:00 pagewriter 1 348 ? 00:00:00 pagewriter 1 349 ? 00:00:00 pagewriter 1 353 ? 00:00:00 Jobscheduler 1 354 ? 00:00:00 asyncundolaunch 1 355 ? 00:00:00 globalstats 1 356 ? 00:00:00 applylauncher 1 357 ? 00:00:00 statscollector 1 358 ? 00:00:00 percentworker 1 359 ? 00:00:00 ashworker 1 360 ? 00:00:00 TrackStmtWorker 1 361 ? 00:00:00 auditor 1 362 ? 00:00:00 2pccleaner 1 363 ? 00:00:00 faultmonitor 1 364 ? 00:00:00 WLMworker 1 365 ? 00:00:00 WLMmonitor 1 366 ? 00:00:00 WLMarbiter 1 367 ? 00:00:00 undorecycler omm@modb:~$ gs_ctl status -D /var/lib/opengauss/data [2022-12-14 10:07:34.345][438][][gs_ctl]: gs_ctl status,datadir is /var/lib/opengauss/data gs_ctl: server is running (PID: 1) /usr/local/opengauss/bin/gaussdb omm@modb:~$ gsql -r gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr ) Non-SSL connection (SSL connection is recommended when requiring high-security) Type "help" for help. omm=# show data_directory; data_directory ------------------------- /var/lib/opengauss/data (1 row)
复制
2、检查锁信息
–查询数据库中的锁信息
SELECT * FROM pg_locks;
–查询等待锁的线程状态信息
SELECT * FROM pg_thread_wait_status WHERE wait_status = ‘acquire lock’;
omm=# SELECT * FROM pg_locks; locktype | database | relation | page | tuple | bucket | virtualxid | transactionid | classid | objid | objsubid | virtualt ransaction | pid | sessionid | mode | granted | fastpath | locktag | global_sessionid ------------+----------+----------+------+-------+--------+------------+---------------+---------+-------+----------+--------- -----------+-----------------+-----------------+-----------------+---------+----------+-------------------+------------------ relation | 16384 | 12010 | | | | | | | | | 11/8 | 281471523519728 | 281471523519728 | AccessShareLock | t | t | 4000:2eea:0:0:0:0 | 0:0#0 virtualxid | | | | | | 11/8 | | | | | 11/8 | 281471523519728 | 281471523519728 | ExclusiveLock | t | t | b:8:0:0:0:7 | 0:0#0 virtualxid | | | | | | 8/13 | | | | | 8/13 | 281471693913328 | 281471693913328 | ExclusiveLock | t | t | 8:d:0:0:0:7 | 0:0#0 (3 rows)
复制
omm=#
omm=# omm=#
omm=# --查询等待锁的线程状态信息
SELECT * FROM pg_thread_wait_status WHERE wait_status = ‘acquire lock’;
node_name | db_name | thread_name | query_id | tid | sessionid | lwtid | psessionid | tlevel | smpid | wait_status | wait_eve
nt | locktag | lockmode | block_sessionid | global_sessionid
-----------±--------±------------±---------±----±----------±------±-----------±-------±------±------------±--------
—±--------±---------±----------------±-----------------
3、查看数据库版本、日志信息
–数据库版本
SELECT version();
–在gsql中
! ls /var/lib/opengauss/data/pg_log
\q
–数据库日志位置
cd /var/lib/opengauss/data/pg_log
ls /var/lib/opengauss/data/pg_log
4、检查应用连接数、查看现有最大连接数
–执行如下SQL语句查看连接数。
gsql -r
SELECT count(*) FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;
–查看现有最大连接数。
SHOW max_connections;
omm=# SELECT count(*) FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s; count ------- 21 (1 row) omm=# omm=# --查看现有最大连接数。 omm=# SHOW max_connections; max_connections ----------------- 200 (1 row)
复制
5、对表进行VACUUM、VACUUM FULL和ANALYZE操作
为了保证数据库的有效运行,数据库必须在插入/删除操作后,基于客户场景,定期做VACUUM FULL和ANALYZE,更新统计信息,以便获得更优的性能。
VACUUM和ANALYZE会导致I/O流量的大幅增加,这可能会影响其他活动会话的性能。
omm=# create table t2(id int,name varchar(10)); CREATE TABLE omm=# insert into t2 values(10,'aa'),(20,'bb'),(30,'cc'); INSERT 0 3 omm=# vacuum t2; VACUUM omm=# vacuum full t2; VACUUM omm=# analyze t2; ANALYZE omm=# analyze verbose t2; INFO: analyzing "public.t2"(gaussdb pid=1) INFO: ANALYZE INFO : "t2": scanned 1 of 1 pages, containing 3 live rows and 0 dead rows; 3 rows in sample, 3 estimated total rows(gaussdb pid=1) ANALYZE omm=# vacuum analyze t2; VACUUM
复制
维护建议:
定期对部分大表做VACUUM FULL,在性能下降后为全库做VACUUM FULL,目前暂定每月做一次VACUUM FULL。
定期对系统表做VACUUM FULL,主要是PG_ATTRIBUTE。
启用系统自动清理线程(AUTOVACUUM)自动执行VACUUM和ANALYZE,回收被标识为删除状态的记录空间,并更新表的统计数据。
6、建表、索引,进行重建索引操作
root@modb:~# su - omm omm@modb:~$ gsql -r gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr ) Non-SSL connection (SSL connection is recommended when requiring high-security) Type "help" for help. omm=# create table t1(id int,name varchar(10)); CREATE TABLE omm=# create index t1_id on t1(id); CREATE INDEX omm=# reindex table t1; REINDEX
复制
评论

