From 1d1d219b37e38b2847b6d6cc46149af9b8bce8b0 Mon Sep 17 00:00:00 2001 From: dbacvetkov <39702213+dbacvetkov@users.noreply.github.com> Date: Tue, 10 Jul 2018 19:07:04 +0600 Subject: [PATCH] 20180110 --- build.gradle | 2 +- src/org/ash/database/ASHDatabasePG10.java | 15 ++++-- src/org/ash/database/ASHDatabasePG95.java | 8 ++- src/org/ash/database/ASHDatabasePG96.java | 8 ++- src/org/ash/database/DBUtils.java | 64 ++++++++++++++++++++++- 5 files changed, 87 insertions(+), 10 deletions(-) diff --git a/build.gradle b/build.gradle index 8b72373..2d5bab7 100644 --- a/build.gradle +++ b/build.gradle @@ -10,7 +10,7 @@ apply plugin: 'application' apply plugin: 'java' -version = '0.3.2' +version = '0.3.3' sourceCompatibility = 1.7 targetCompatibility = 1.7 diff --git a/src/org/ash/database/ASHDatabasePG10.java b/src/org/ash/database/ASHDatabasePG10.java index de2bcf9..a638cb4 100644 --- a/src/org/ash/database/ASHDatabasePG10.java +++ b/src/org/ash/database/ASHDatabasePG10.java @@ -88,7 +88,7 @@ public class ASHDatabasePG10 extends ASHDatabase { + "application_name, backend_type, " + "coalesce(client_hostname, client_addr::text, 'localhost') as client_hostname, " + "wait_event_type, wait_event, query, " - + "query_start, 1000 * EXTRACT(EPOCH FROM (clock_timestamp()-query_start)) as duration " + + "coalesce(query_start, xact_start, backend_start) as query_start, 1000 * EXTRACT(EPOCH FROM (clock_timestamp()-coalesce(query_start, xact_start, backend_start))) as duration " + "from pg_stat_activity " + "where state='active' and pid != pg_backend_pid()"; @@ -206,7 +206,11 @@ private void loadAshDataToLocal() { Long valueSampleIdTimeLongWait = (new Long(PGDateSampleTime.getTime())); java.sql.Timestamp queryStartTS = resultSetAsh.getTimestamp("query_start"); - Long queryStartLong = (new Long(queryStartTS.getTime())); + Long queryStartLong = 0L; + if (queryStartTS != null) { + queryStartLong = (new Long(queryStartTS.getTime())); + } + Double duration = resultSetAsh.getDouble("duration"); Long sessionId = resultSetAsh.getLong("pid"); @@ -311,6 +315,7 @@ private void loadAshDataToLocal() { if (command_type.equals("SELECT") || command_type.equals("UPDATE") || command_type.equals("DELETE") || command_type.equals("INSERT")) { if ((connDBName.length() > 0) && (explainFreq > 0)) { DBUtils.explainPlan(sqlId, query_text, command_type, planDir, fileSeparator, connDBName, databaseName, conn, explainFreq); + // DBUtils.FindQueryID(sqlId, query_text_norm, conn); } } @@ -644,7 +649,11 @@ private void loadHistAshDataToLocal() { Long valueSampleIdTimeLongWait = (new Long(PGDateSampleTime.getTime())); java.sql.Timestamp queryStartTS = resultSetAsh.getTimestamp("query_start"); - Long queryStartLong = (new Long(queryStartTS.getTime())); + Long queryStartLong = 0L; + if (queryStartTS != null) { + queryStartLong = (new Long(queryStartTS.getTime())); + } + Double duration = resultSetAsh.getDouble("duration"); Long sessionId = resultSetAsh.getLong("pid"); diff --git a/src/org/ash/database/ASHDatabasePG95.java b/src/org/ash/database/ASHDatabasePG95.java index 15fa10b..766a143 100644 --- a/src/org/ash/database/ASHDatabasePG95.java +++ b/src/org/ash/database/ASHDatabasePG95.java @@ -90,7 +90,7 @@ public class ASHDatabasePG95 extends ASHDatabase { + "application_name, " + "coalesce(client_hostname, client_addr::text, 'localhost') as client_hostname, " + "waiting::int as waiting, query, " - + "query_start, 1000 * EXTRACT(EPOCH FROM (clock_timestamp()-query_start)) as duration " + + "coalesce(query_start, xact_start, backend_start) as query_start, 1000 * EXTRACT(EPOCH FROM (clock_timestamp()-coalesce(query_start, xact_start, backend_start))) as duration " + "from pg_stat_activity " + "where state='active' and pid != pg_backend_pid()"; @@ -208,7 +208,11 @@ private void loadAshDataToLocal() { Long valueSampleIdTimeLongWait = (new Long(PGDateSampleTime.getTime())); java.sql.Timestamp queryStartTS = resultSetAsh.getTimestamp("query_start"); - Long queryStartLong = (new Long(queryStartTS.getTime())); + Long queryStartLong = 0L; + if (queryStartTS != null) { + queryStartLong = (new Long(queryStartTS.getTime())); + } + Double duration = resultSetAsh.getDouble("duration"); Long sessionId = resultSetAsh.getLong("pid"); diff --git a/src/org/ash/database/ASHDatabasePG96.java b/src/org/ash/database/ASHDatabasePG96.java index 79e177f..56300f7 100644 --- a/src/org/ash/database/ASHDatabasePG96.java +++ b/src/org/ash/database/ASHDatabasePG96.java @@ -90,7 +90,7 @@ public class ASHDatabasePG96 extends ASHDatabase { + "application_name, " + "coalesce(client_hostname, client_addr::text, 'localhost') as client_hostname, " + "wait_event_type, wait_event, query, " - + "query_start, 1000 * EXTRACT(EPOCH FROM (clock_timestamp()-query_start)) as duration " + + "coalesce(query_start, xact_start, backend_start) as query_start, 1000 * EXTRACT(EPOCH FROM (clock_timestamp()-coalesce(query_start, xact_start, backend_start))) as duration " + "from pg_stat_activity " + "where state='active' and pid != pg_backend_pid()"; @@ -208,7 +208,11 @@ private void loadAshDataToLocal() { Long valueSampleIdTimeLongWait = (new Long(PGDateSampleTime.getTime())); java.sql.Timestamp queryStartTS = resultSetAsh.getTimestamp("query_start"); - Long queryStartLong = (new Long(queryStartTS.getTime())); + Long queryStartLong = 0L; + if (queryStartTS != null) { + queryStartLong = (new Long(queryStartTS.getTime())); + } + Double duration = resultSetAsh.getDouble("duration"); Long sessionId = resultSetAsh.getLong("pid"); diff --git a/src/org/ash/database/DBUtils.java b/src/org/ash/database/DBUtils.java index 0f59bf4..bda952a 100644 --- a/src/org/ash/database/DBUtils.java +++ b/src/org/ash/database/DBUtils.java @@ -35,14 +35,30 @@ public static String GetSQLCommandType(String nsql) { for (int i = 0; i < array.length; i++) { if (array[i].equals("select")) { return "SELECT"; - } else if (array[i].equals("insert")) { - return "INSERT"; } else if (array[i].equals("update")) { return "UPDATE"; } else if (array[i].equals("insert")) { return "INSERT"; } else if (array[i].equals("delete")) { return "DELETE"; + } else if (array[i].equals("with")) { + int s = 0; + for (int j = i; j < array.length; j++) { + if (array[j].equals("(")) { s++; } + else if (array[j].equals(")")) { s--; } + else if (s == 0) { + if (array[j].equals("select")) { + return "SELECT"; + } else if (array[j].equals("update")) { + return "UPDATE"; + } else if (array[j].equals("insert")) { + return "INSERT"; + } else if (array[j].equals("delete")) { + return "DELETE"; + } + } + } + return "UNKNOWN"; } else if (array[i].equals("drop")) { return "DROP"; } else if (array[i].equals("truncate")) { @@ -390,4 +406,48 @@ public static void writeText(String sqlId, String query_text, String textFileNam } } + + public static void FindQueryID(String sqlId, String query_text_norm, Connection conn) { + + String queryid = ""; + + ResultSet rs1 = null; + PreparedStatement st1 = null; + try { + // pg_stat_statements не добавляет пробелы вокруг $1, т.е. там может быть "where id=$1" + st1 = conn.prepareStatement("select queryid from pg_stat_statements where query = '" + query_text_norm + "'"); + st1.setQueryTimeout(1); + rs1 = st1.executeQuery(); + } catch (Exception e) { + queryid = ""; + } + + if (rs1 != null) { + try { + while (rs1.next()) { + queryid = ""; + } + } catch (SQLException e) { + e.printStackTrace(); + } + + try { + rs1.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + if (st1 != null) { + try { + st1.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + + + } +