Skip to content

Commit

Permalink
20180110
Browse files Browse the repository at this point in the history
  • Loading branch information
dbacvetkov committed Jul 10, 2018
1 parent 6282c5b commit 1d1d219
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
apply plugin: 'application'
apply plugin: 'java'

version = '0.3.2'
version = '0.3.3'
sourceCompatibility = 1.7
targetCompatibility = 1.7

Expand Down
15 changes: 12 additions & 3 deletions src/org/ash/database/ASHDatabasePG10.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()";

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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");
Expand Down
8 changes: 6 additions & 2 deletions src/org/ash/database/ASHDatabasePG95.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()";

Expand Down Expand Up @@ -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");
Expand Down
8 changes: 6 additions & 2 deletions src/org/ash/database/ASHDatabasePG96.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()";

Expand Down Expand Up @@ -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");
Expand Down
64 changes: 62 additions & 2 deletions src/org/ash/database/DBUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down Expand Up @@ -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();
}
}
}



}

0 comments on commit 1d1d219

Please sign in to comment.