Skip to content

Commit

Permalink
20180617 add field backend type
Browse files Browse the repository at this point in the history
  • Loading branch information
dbacvetkov committed Jun 17, 2018
1 parent 85c3c53 commit 787f6b0
Show file tree
Hide file tree
Showing 24 changed files with 271 additions and 398 deletions.
Binary file modified screenshots/pash-viewer-screenshot-01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions src/org/ash/database/ASHDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,8 @@ public DefaultTableModel getASHRawData(double begin, double end, String detail)
"Wait Class",
"Wait Class id",
"UserID",
"Hostname"
"Hostname",
"Backend Type"
}, 0);

try {
Expand Down Expand Up @@ -721,7 +722,8 @@ public DefaultTableModel getASHRawData(double begin, double end, String detail)
ASH.getWaitClass(),
(long) ASH.getWaitClassId(),
ASH.getUserId(),
ASH.getHostname()
ASH.getHostname(),
ASH.getBackendType()
});
}
ActiveSessionHistoryCursor.close();
Expand Down
122 changes: 29 additions & 93 deletions src/org/ash/database/ASHDatabasePG10.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@

// dcvetkov import
import org.ash.util.Options;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

/**
* The Class ASHDatabasePG10.
Expand Down Expand Up @@ -90,15 +84,15 @@ public class ASHDatabasePG10 extends ASHDatabase {
* The query ash.
*/
private String queryASH = "SELECT current_timestamp, "
+ "datname, pid, usesysid, "
+ "coalesce(usename, backend_type, 'unknown') as usename, "
+ "application_name, "
+ "datname, pid, usesysid, usename, "
+ "application_name, backend_type, "
+ "coalesce(client_hostname, client_addr::text, 'localhost') as client_hostname, "
+ "wait_event_type, wait_event, backend_type, query "
+ "wait_event_type, wait_event, query "
+ "from pg_stat_activity "
+ "where state='active' and pid != pg_backend_pid() and query not like '%pash_snapshot()%'";

private String FILESEPARATOR = System.getProperty("file.separator");
private String fileSeparator = System.getProperty("file.separator");
private String planDir = Options.getInstance().getPlanDir();

/**
* The k for sample_id after reconnect
Expand Down Expand Up @@ -183,12 +177,16 @@ private void loadAshDataToLocal() {

if (model.getConnectionPool() != null) {

String connDBName = getParameter("ASH.db");

conn = this.model.getConnectionPool().getConnection();

statement = conn.prepareStatement(this.queryASH);

// set ArraySize for current statement to improve performance
statement.setFetchSize(5000);
// set Timeout to 1 second
statement.setQueryTimeout(1);

resultSetAsh = statement.executeQuery();

Expand All @@ -206,9 +204,8 @@ private void loadAshDataToLocal() {
Long valueSampleIdTimeLongWait = (new Long(PGDateSampleTime.getTime()));

Long sessionId = resultSetAsh.getLong("pid");
String sessionType = resultSetAsh.getString("backend_type");
String backendType = resultSetAsh.getString("backend_type");

String ConnDBName = getParameter("ASH.db");
String databaseName = resultSetAsh.getString("datname");

Long userId = resultSetAsh.getLong("usesysid");
Expand All @@ -222,7 +219,7 @@ private void loadAshDataToLocal() {
query_text = "backup";
} else if (program.equals("walreceiver") || program.equals("walsender")) {
query_text = "wal";
} else if (sessionType.equals("walreceiver") || sessionType.equals("walsender")) {
} else if (backendType.equals("walreceiver") || backendType.equals("walsender")) {
query_text = "wal";
} else {
query_text = "empty";
Expand All @@ -241,7 +238,7 @@ private void loadAshDataToLocal() {
String event = resultSetAsh.getString("wait_event");
String waitClass = resultSetAsh.getString("wait_event_type");

if (waitClass == null) {
if ((waitClass == null) || (waitClass.equals(""))) {
waitClass = "CPU";
event = "CPU";
}
Expand Down Expand Up @@ -287,7 +284,7 @@ private void loadAshDataToLocal() {
.putNoReturn(new ActiveSessionHistory(
activeSessionHistoryIdWait,
valueSampleIdTimeLongWait,
sessionId, sessionType, userId, userName, sqlId, command_type,
sessionId, backendType, userId, userName, sqlId, command_type,
event, waitClass, waitClassId, program, hostname));
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -302,68 +299,11 @@ private void loadAshDataToLocal() {

// dcvetkov: explain plan
if (command_type.equals("SELECT") || command_type.equals("UPDATE") || command_type.equals("DELETE") || command_type.equals("INSERT")) {

String planFileName = Options.getInstance().getPlanDir() + FILESEPARATOR + sqlId + ".plan";
String textFileName = Options.getInstance().getPlanDir() + FILESEPARATOR + sqlId + ".sql";
File planFile = new File(planFileName);
// åñëè mtime ôàéëà ñòàðøå ÷àñà - çàïðàøèâàåì ïëàí çàíîâî
if (System.currentTimeMillis() - planFile.lastModified() > 3600000) {

String plan = "EXPLAIN PLAN FOR SQLID " + sqlId + " (" + command_type + "):\n"
+ "------------------------------------------------------------\n\n";

if (ConnDBName.equals(databaseName)) {

ResultSet rs1 = null;
PreparedStatement st1 = null;
try {
st1 = conn.prepareStatement("EXPLAIN " + query_text);
rs1 = st1.executeQuery();
} catch (Exception e) {
plan = plan + e.toString();
}

if (rs1 != null) {
while (rs1.next()) {
plan = plan + rs1.getString(1) + "\n";
}
rs1.close();
}

if (st1 != null) {
st1.close();
}
} else {
plan = plan + "You are connected to database " + ConnDBName + " while query " + sqlId + " executed in database " + databaseName;
plan = plan + ".\nSo sorry.";
}

if (ConnDBName.length() > 0) {
Writer writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(planFileName), "utf-8"));
writer.write(plan);
} catch (IOException ex) {
} finally {
try {
writer.close();
} catch (Exception ex) {/*ignore*/ }
}

writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(textFileName), "utf-8"));
writer.write(query_text);
} catch (IOException ex) {
} finally {
try {
writer.close();
} catch (Exception ex) {/*ignore*/
}
}
}
}
if (connDBName.length() > 0) {
DBUtils.explainPlan(sqlId, query_text, command_type, planDir, fileSeparator, connDBName, databaseName, conn);
}
}

}
if (conn != null) {
model.getConnectionPool().free(conn);
Expand Down Expand Up @@ -432,12 +372,8 @@ public void calculateSqlsSessionsData(double beginTime, double endTime,

// get rows from ActiveSessionHistory for samplId
EntityCursor<ActiveSessionHistory> ActiveSessionHistoryCursor;
ActiveSessionHistoryCursor = dao.doRangeQuery(
dao.activeSessionHistoryByAshId, ashSumMain
.getsampleId(), true, ashSumMain.getsampleId(),
true);
Iterator<ActiveSessionHistory> ActiveSessionHistoryIter = ActiveSessionHistoryCursor
.iterator();
ActiveSessionHistoryCursor = dao.doRangeQuery(dao.activeSessionHistoryByAshId, ashSumMain.getsampleId(), true, ashSumMain.getsampleId(), true);
Iterator<ActiveSessionHistory> ActiveSessionHistoryIter = ActiveSessionHistoryCursor.iterator();

while (ActiveSessionHistoryIter.hasNext()) {
ActiveSessionHistory ASH = ActiveSessionHistoryIter.next();
Expand All @@ -452,6 +388,7 @@ public void calculateSqlsSessionsData(double beginTime, double endTime,
String usernameSess = ASH.getUserName();
String programSess = ASH.getProgram();
programSess = programSess + "@" + ASH.getHostname();
String backendSess = ASH.getBackendType();
String waitClass = ASH.getWaitClass();
String eventName = ASH.getEvent();

Expand All @@ -460,14 +397,14 @@ public void calculateSqlsSessionsData(double beginTime, double endTime,
if (waitClass != null && waitClass.equalsIgnoreCase(eventFlag)) {
this.loadDataToTempSqlSession(tmpSqlsTemp,
tmpSessionsTemp, sqlId, waitClassId,
sessionId, sessionidS, 0.0, "",
sessionId, sessionidS, 0.0, backendSess,
useridL, usernameSess, programSess, true, eventName, 0);
}

} else {
this.loadDataToTempSqlSession(tmpSqlsTemp,
tmpSessionsTemp, sqlId, waitClassId,
sessionId, sessionidS, 0.0, "",
sessionId, sessionidS, 0.0, backendSess,
useridL, usernameSess, programSess, false, eventFlag, 0);
}
}
Expand Down Expand Up @@ -584,7 +521,7 @@ public String getSqlText(String sqlId) {
*/
private void loadDataToTempSqlSession(SqlsTemp tmpSqlsTemp,
SessionsTemp tmpSessionsTemp, String sqlId, double waitClassId, Long sessionId,
String sessionidS, Double sessionSerial, String sessioniSerialS,
String sessionidS, Double sessionSerial, String backendType,
Long useridL, String usernameSess, String programSess, boolean isDetail,
String eventDetail, double sqlPlanHashValue) {

Expand All @@ -605,8 +542,8 @@ private void loadDataToTempSqlSession(SqlsTemp tmpSqlsTemp,
/**
* Save data for session row
*/
tmpSessionsTemp.setSessionId(sessionidS, sessioniSerialS, programSess, "", usernameSess);
tmpSessionsTemp.setTimeOfGroupEvent(sessionidS + "_" + sessioniSerialS, waitClassId, count);
tmpSessionsTemp.setSessionId(sessionidS, backendType, programSess, "", usernameSess);
tmpSessionsTemp.setTimeOfGroupEvent(sessionidS, waitClassId, count);

/**
* Save event detail data for sql and sessions row
Expand All @@ -615,7 +552,7 @@ private void loadDataToTempSqlSession(SqlsTemp tmpSqlsTemp,
if (sqlId != null) {
tmpSqlsTemp.setTimeOfEventName(sqlId, waitClassId, eventDetail, count);
}
tmpSessionsTemp.setTimeOfEventName(sessionidS + "_" + sessioniSerialS, waitClassId, eventDetail, count);
tmpSessionsTemp.setTimeOfEventName(sessionidS, waitClassId, eventDetail, count);
}
}

Expand Down Expand Up @@ -697,9 +634,8 @@ private void loadHistAshDataToLocal() {
Long valueSampleIdTimeLongWait = (new Long(PGDateSampleTime.getTime()));

Long sessionId = resultSetAsh.getLong("pid");
String sessionType = resultSetAsh.getString("backend_type");
String backendType = resultSetAsh.getString("backend_type");

String ConnDBName = getParameter("ASH.db");
String databaseName = resultSetAsh.getString("datname");

Long userId = resultSetAsh.getLong("usesysid");
Expand All @@ -713,7 +649,7 @@ private void loadHistAshDataToLocal() {
query_text = "backup";
} else if (program.equals("walreceiver") || program.equals("walsender")) {
query_text = "wal";
} else if (sessionType.equals("walreceiver") || sessionType.equals("walsender")) {
} else if (backendType.equals("walreceiver") || backendType.equals("walsender")) {
query_text = "wal";
} else {
query_text = "empty";
Expand Down Expand Up @@ -778,7 +714,7 @@ private void loadHistAshDataToLocal() {
.putNoReturn(new ActiveSessionHistory(
activeSessionHistoryIdWait,
valueSampleIdTimeLongWait,
sessionId, sessionType, userId, userName, sqlId, command_type,
sessionId, backendType, userId, userName, sqlId, command_type,
event, waitClass, waitClassId, program, hostname));
} catch (Exception e) {
e.printStackTrace();
Expand Down
Loading

0 comments on commit 787f6b0

Please sign in to comment.