暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

openGauss每日一练第21天 | 学习心得体会

原创 zongzi 2022-12-14
327

学习目标
学习检查数据库运行状态、锁信息、维护表、重建索引等数据库维护操作技术

课程学习
1.检查openGauss状态
–操作系统命令查看实例状态

[omm@gaussdb ~]$ ps -ef|grep gaussdb
omm         9344       1  2 09:51 ?        00:00:16 /gaussdb/app/bin/gaussdb -D /gaussdb/data/db1
omm        10451    7543  0 10:01 pts/0    00:00:00 grep --color=auto gaussdb
[omm@gaussdb ~]$ ps -Tp 1
    PID    SPID TTY          TIME CMD
      1       1 ?        00:00:00 systemd

–使用数据库工具gs_ctl查看实例状态

[2022-12-14 10:03:36.833][10514][][gs_ctl]: gs_ctl status,datadir is /gaussdb/data/db1 
gs_ctl: server is running (PID: 9344)
/gaussdb/app/bin/gaussdb "-D" "/gaussdb/data/db1"

–检查参数

[omm@gaussdb ~]$ gsql -d postgres -p 26000
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:19 commit 0 last mr  )
NOTICE : The password has been expired, please change the password. 
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

openGauss=# show data_directory;
  data_directory   
-------------------
 /gaussdb/data/db1
(1 row)

2.检查锁信息
锁机制是数据库保证数据一致性的重要手段,检查相关信息可以检查数据库的事务和运行状况。
–查询数据库中的锁信息

openGauss=#  SELECT * FROM pg_locks;
  locktype  | database | relation | page | tuple | bucket | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction |       pid       |    sessionid    |     
 mode       | granted | fastpath |      locktag      | global_sessionid 
------------+----------+----------+------+-------+--------+------------+---------------+---------+-------+----------+--------------------+-----------------+-----------------+-----
------------+---------+----------+-------------------+------------------
 relation   |    15563 |    12010 |      |       |        |            |               |         |       |          | 8/72               | 140552634877696 | 140552634877696 | Acce
ssShareLock | t       | t        | 3ccb:2eea:0:0:0:0 | 0:0#0
 virtualxid |          |          |      |       |        | 8/72       |               |         |       |          | 8/72               | 140552634877696 | 140552634877696 | Excl
usiveLock   | t       | t        | 8:48:0:0:0:7      | 0:0#0
(2 rows)

–查询等待锁的线程状态信息

openGauss=#  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_event | locktag | lockmode | block_sessionid | global_se
ssionid 
-----------+---------+-------------+----------+-----+-----------+-------+------------+--------+-------+-------------+------------+---------+----------+-----------------+----------
--------
(0 rows)

openGauss=#  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_event | locktag | lockmode | block_sessionid | global_se
ssionid 
-----------+---------+-------------+----------+-----+-----------+-------+------------+--------+-------+-------------+------------+---------+----------+-----------------+----------
--------
(0 rows)

3.统计事件数据
SQL语句长时间运行会占用大量系统资源,用户可以通过查看事件发生的时间,占用内存大小来了解现在数据库运行状态。
–查询事件的时间,查询事件的线程启动时间、事务启动时间、SQL启动时间以及状态变更时间。

1670983604950.png
–查询当前服务器的会话计数信息

openGauss=#  SELECT count(*) FROM pg_stat_activity;
 count 
-------
     6
(1 row)

–查询系统级统计信息,查询当前使用内存最多的会话信息。

openGauss=#   SELECT * FROM pv_session_memory_detail() ORDER BY usedsize desc limit 10;
 sessid | threadid | contextname | level | parent | totalsize | freesize | usedsize 
--------+----------+-------------+-------+--------+-----------+----------+----------
(0 rows)

–查询表统计信息

openGauss=#  SELECT * FROM pg_statistic;
starelid      | 1247
starelkind    | c
staattnum     | 1
stainherit    | f
stanullfrac   | 0
stawidth      | 64
stadistinct   | -.998609
stakind1      | 1
stakind2      | 2
stakind3      | 3
stakind4      | 0
stakind5      | 0
staop1        | 93
staop2        | 660
staop3        | 660
staop4        | 0
staop5        | 0
stanumbers1   | {.00278164}
stanumbers2   | 
stanumbers3   | {.254631}
stanumbers4   | 
stanumbers5   | 
stavalues1    | {statement_history}
stavalues2    | {TdigestData,_box,_cid,_float4,_hll_trans_type,_int2vector_extend,_jsonb,_nvarchar2,_pg_foreign_tables,_regclass,_regtype,_timestamptz,_txid_snapshot,aclitem,anyra
nge,box,char,circle,column_privileges,date,exception,foreign_data_wrappers,global_candidate_status,global_locks,global_pagewriter_status,global_replication_stat,global_shared_memo
ry_detail,global_stat_database,global_stat_user_indexes,global_statement_complex_history_table,global_statio_sys_sequences,global_threadpool_status,gs_auditing,gs_client_global_ke
ys,gs_db_privileges,gs_global_config,gs_masking,gs_model_warehouse,gs_session_cpu_statistics,gs_shared_memory_detail,gs_total_memory_detail,gs_wlm_ec_operator_statistics,gs_wlm_pl
an_operator_info,gs_wlm_session_statistics,hll_hashval,int16,int8range,line,memory_node_detail,nvarchar2,os_runtime,pg_amproc,pg_available_extension_versions,pg_comm_send_stream,p
g_db_role_setting,pg_extension,pg_group,pg_job,pg_node_env,pg_os_threads,pg_publication_rel,pg_rewrite,pg_seclabels,pg_stat_activity,pg_stat_database_conflicts,pg_stat_user_tables
,pg_statio_all_tables,pg_statistic,pg_tde_info,pg_toast_12479,pg_toast_12554,pg_toast_14736,pg_toast_2619,pg_toast_5818,pg_total_user_resource_info,pg_type,pg_wlm_statistics,pgxc_
thread_wait_status,refcursor,regproc,role_routine_grants,sequences,shared_memory_detail,sql_implementation_info,stat_all_tables,stat_user_indexes,statement_complex_history,statio_
all_indexes,statio_user_tables,summary_stat_all_indexes,summary_stat_user_functions,summary_statement,summary_statio_sys_tables,summary_workload_sql_count,thread_wait_status,tinte
rval,tsrange,user_defined_types,varchar,wlm_user_resource_config,yes_or_no}
stavalues3    | 
stavalues4    | 
stavalues5    | 
stadndistinct | 0
staextinfo    |  

4.数据库版本、日志信息
–数据库版本

openGauss=# SELECT version();
                                                                       version                                                                        
------------------------------------------------------------------------------------------------------------------------------------------------------
 (openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:19 commit 0 last mr   on x86_64-unknown-linux-gnu, compiled by g++ (GCC) 7.3.0, 64-bit
(1 row)

–在gsql中

openGauss=# \! ls /gaussdb/log/omm/pg_log
dn_6001
openGauss=# \q
[omm@gaussdb pg_log]$ ls
dn_6001
[omm@gaussdb pg_log]$ cd dn_6001/
[omm@gaussdb dn_6001]$ ls
postgresql-2022-05-18_143415.log  postgresql-2022-11-25_101920.log  postgresql-2022-11-27_000000.log  postgresql-2022-12-14_095152.log
postgresql-2022-05-18_145814.log  postgresql-2022-11-26_000000.log  postgresql-2022-11-28_000000.log

5.检查应用连接数
如果应用程序与数据库的连接数超过最大值,则新的连接无法建立。建议每天检查连接数,及时释放空闲的连接或者增加最大连接数。
–执行如下SQL语句查看连接数。

openGauss=# SELECT count(*) FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;
 count 
-------
    18
(1 row)

–查看现有最大连接数。

openGauss=# SHOW max_connections;
 max_connections 
-----------------
 5000
(1 row)

6.例行维护表:VACUUM、VACUUM FULL和ANALYZE
为了保证数据库的有效运行,数据库必须在插入/删除操作后,基于客户场景,定期做VACUUM FULL和ANALYZE,更新统计信息,以便获得更优的性能。
VACUUM和ANALYZE会导致I/O流量的大幅增加,这可能会影响其他活动会话的性能。
–创建表、插入数据、查看数据
1670984374539.png
–使用VACUUM或VACUUM FULL命令,进行磁盘空间回收。

openGauss=# VACUUM customer_t;
VACUUM
openGauss=# VACUUM FULL customer_t;
VACUUM

–使用ANALYZE和ANALYZE VERBOSE语句更新统计信息。

openGauss=# ANALYZE customer_t;
ANALYZE
openGauss=# ANALYZE VERBOSE customer_t;
INFO:  analyzing "public.customer_t"(dn_6001 pid=9344)
INFO:  ANALYZE INFO : "customer_t": scanned 1 of 1 pages, containing 9 live rows and 0 dead rows; 9 rows in sample, 9 estimated total rows(dn_6001 pid=9344)
ANALYZE

–也可以同时执行VACUUM ANALYZE命令进行查询优化。

openGauss=# VACUUM ANALYZE customer_t;
VACUUM

维护建议:

定期对部分大表做VACUUM FULL,在性能下降后为全库做VACUUM FULL,目前暂定每月做一次VACUUM FULL。

定期对系统表做VACUUM FULL,主要是PG_ATTRIBUTE。

启用系统自动清理线程(AUTOVACUUM)自动执行VACUUM和ANALYZE,回收被标识为删除状态的记录空间,并更新表的统计数据。
7.重建索引
在重建索引前,用户可以通过临时增大maintenance_work_mem和psort_work_mem的取值来加快索引的重建。
–环境准备

openGauss=# CREATE TABLESPACE EXAMPLE RELATIVE LOCATION 'tablespace1/tablespace_1';
CREATE schema HR;
openGauss=# CREATE TABLESPACE;
CREATE SCHEMA
openGauss=#   CREATE TABLE HR.areaS(   area_ID NUMBER,   area_NAME VARCHAR2(25)  )tablespace EXAMPLE;
CREATE TABLE
openGauss=# \d HR.areaS
               Table "hr.areas"
  Column   |         Type          | Modifiers 
-----------+-----------------------+-----------
 area_id   | numeric               | 
 area_name | character varying(25) | 
Tablespace: "example"
openGauss=# CREATE INDEX areaS_idx ON HR.areaS (area_id);
CREATE INDEX

–使用REINDEX TABLE语句重建索引。

openGauss=# REINDEX  table HR.areaS;
REINDEX
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论