Skip to content

Commit

Permalink
support dbms_xplan.display_cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
Teingi committed Dec 17, 2024
1 parent 8e92f28 commit 8c526bf
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
Binary file removed .DS_Store
Binary file not shown.
19 changes: 19 additions & 0 deletions src/common/ob_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
28 changes: 27 additions & 1 deletion src/handler/gather/gather_plan_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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

0 comments on commit 8c526bf

Please sign in to comment.