From 8c526bf53b68033bf74fc1e969a1fe86203940cd Mon Sep 17 00:00:00 2001 From: Teingi Date: Tue, 17 Dec 2024 14:47:33 +0800 Subject: [PATCH] support dbms_xplan.display_cursor --- .DS_Store | Bin 6148 -> 0 bytes src/common/ob_connector.py | 19 +++++++++++++++ src/handler/gather/gather_plan_monitor.py | 28 +++++++++++++++++++++- 3 files changed, 46 insertions(+), 1 deletion(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 5c3c6c2f9724dd4dd277ebc9126dd114ea190606..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKJ8r`;3?&nz2#_UXMqQyd5RCK$xj;WoiUJKh7}-7bTs>MJKSKhin>#dk1gIxb zd=m5q(-aYH&)uWQMnqO{L;12{YPN4au|-A{2*()*8IIlX_S*NKlkD38<37lToa9B` zFZs4XqXJZb3Qz$mKm}%3AWQ6OJ^RT#kP1+N|E_?29}3*CCXRvr>A>JE0I)&W4Rh}$ zfW-p9nm7g`0@I)ZgR0qLXwVTanO76Xz@Up}^PzdOW{0AFJI*hjE?NUQQUNM(t-vyt z8>|0M@L&4>YZ6yffC}7|0^02MyB1H%+B$ih)!G7ohFi`z+zfN4VDNGb^m2@amE)}^ bMP9Kv_G{u8=yb%L4&={(=|ZCdf33g|P&^fT diff --git a/src/common/ob_connector.py b/src/common/ob_connector.py index 02cc9d70..1d5f0974 100644 --- a/src/common/ob_connector.py +++ b/src/common/ob_connector.py @@ -151,6 +151,25 @@ def execute_sql_pretty(self, sql): cursor.close() return ret + def execute_display_cursor(self, business_sql): + if self.conn is None: + self._connect_db() + else: + self.conn.ping(reconnect=True) + cursor = self.conn.cursor() + try: + cursor.execute("SET TRANSACTION ISOLATION LEVEL READ COMMITTED") + cursor.execute(business_sql) + + cursor.execute("select dbms_xplan.display_cursor(0, 'all')") + plan_result = from_db_cursor(cursor) + cursor.close() + return plan_result + except Exception as e: + raise Exception("execute display cursor error: {0}".format(e)) + finally: + cursor.close() + def callproc(self, procname, args=()): if self.conn is None: self._connect_db() diff --git a/src/handler/gather/gather_plan_monitor.py b/src/handler/gather/gather_plan_monitor.py index bf3ada90..890d326c 100644 --- a/src/handler/gather/gather_plan_monitor.py +++ b/src/handler/gather/gather_plan_monitor.py @@ -54,6 +54,7 @@ def __init__(self, context, gather_pack_dir='./', is_scene=False): self.sql_audit_name = "gv$sql_audit" self.plan_explain_name = "gv$plan_cache_plan_explain" self.is_scene = is_scene + self.ob_version = "4.2.5.0" if self.context.get_variable("gather_timestamp", None): self.gather_timestamp = self.context.get_variable("gather_timestamp") else: @@ -165,6 +166,8 @@ def handle_plan_monitor_from_ob(cluster_name): # 输出plan cache的信息 self.stdio.verbose("[sql plan monitor report task] report plan cache") self.report_plan_cache(plan_explain_sql) + # dbms_xplan.display_cursor + self.report_display_cursor_obversion4(sql) # 输出表结构的信息 self.stdio.verbose("[sql plan monitor report task] report table schema") self.report_schema(user_sql, tenant_name) @@ -216,7 +219,7 @@ def handle_plan_monitor_from_ob(cluster_name): if getattr(sys, 'frozen', False): absPath = os.path.dirname(sys.executable) else: - absPath = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) + absPath = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) cs_resources_path = os.path.join(absPath, "resources") self.stdio.verbose("[cs resource path] : {0}".format(cs_resources_path)) target_resources_path = os.path.join(pack_dir_this_command, "resources") @@ -667,6 +670,7 @@ def tenant_mode_detected(self): if matched_version: version = matched_version.group(2) + self.ob_version = version major_version = int(version.split('.')[0]) self.sql_audit_name = "gv$ob_sql_audit" if major_version >= 4 else "gv$sql_audit" @@ -998,3 +1002,25 @@ def report_db_time_display_obversion4(self, sql_plan_monitor_db_time): self.stdio.exception("DB Time display> %s" % sql_plan_monitor_db_time) self.stdio.exception(repr(e)) pass + + def __is_select_statement(self, sql): + stripped_sql = sql.strip().upper() + return stripped_sql.startswith('SELECT') + + def report_display_cursor_obversion4(self, display_cursor_sql): + if not self.__is_select_statement(display_cursor_sql): + return + try: + if not StringUtils.compare_versions_lower(self.ob_version, "4.2.5.0"): + plan_result = self.db_connector.execute_display_cursor(display_cursor_sql) + self.stdio.verbose("execute SQL: %s", display_cursor_sql) + step = "obclient> SET TRANSACTION ISOLATION LEVEL READ COMMITTED;\n{0}\nselect dbms_xplan.display_cursor(0, 'all');".format(display_cursor_sql) + self.report_pre(step) + self.report_pre(plan_result) + self.stdio.verbose("display_cursor report complete") + else: + self.stdio.verbose("display_cursor report requires the OB version to be greater than 4.2.5.0 Your version: {0} does not meet this requirement.".format(self.ob_major_version)) + except Exception as e: + self.stdio.exception("display_cursor report> %s" % display_cursor_sql) + self.stdio.exception(repr(e)) + pass