[Schmitzm-commits] r2392 - trunk/schmitzm-db/src/main/java/de/schmitzm/postgres

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Tue Oct 8 22:07:53 CEST 2013


Author: alfonx
Date: 2013-10-08 22:07:53 +0200 (Tue, 08 Oct 2013)
New Revision: 2392

Modified:
   trunk/schmitzm-db/src/main/java/de/schmitzm/postgres/PGUtil.java
Log:
Erster Versuch PGUtil mit PG 9.3 kompatibel zu machen. Die relevanten Stellen finden ?\195?\188ber einen Aufruf von "select version()" selber raus, welche Version von PG verwendet wird.

Modified: trunk/schmitzm-db/src/main/java/de/schmitzm/postgres/PGUtil.java
===================================================================
--- trunk/schmitzm-db/src/main/java/de/schmitzm/postgres/PGUtil.java	2013-09-12 01:06:51 UTC (rev 2391)
+++ trunk/schmitzm-db/src/main/java/de/schmitzm/postgres/PGUtil.java	2013-10-08 20:07:53 UTC (rev 2392)
@@ -20,6 +20,34 @@
 public class PGUtil {
 	static final Logger log = Logger.getLogger(PGUtil.class);
 
+	public enum PgVersion {
+		_8_4, _9_0, _9_1, _9_2, _9_3, _9_4, _unknown
+	}
+
+	public static PgVersion getVersion(Connection c) throws SQLException {
+
+		ResultSet rs = c.createStatement().executeQuery("select version();");
+
+		rs.next();
+
+		String vString = rs.getString(0);
+
+		if (vString.matches("(?i)PostgeSQL 8\\.4\\."))
+			return PgVersion._8_4;
+		if (vString.matches("(?i)PostgeSQL 9\\.0\\."))
+			return PgVersion._9_0;
+		if (vString.matches("(?i)PostgeSQL 9\\.1\\."))
+			return PgVersion._9_1;
+		if (vString.matches("(?i)PostgeSQL 9\\.2\\."))
+			return PgVersion._9_2;
+		if (vString.matches("(?i)PostgeSQL 9\\.3\\."))
+			return PgVersion._9_3;
+		if (vString.matches("(?i)PostgeSQL 9\\.4\\."))
+			return PgVersion._9_4;
+
+		return PgVersion._unknown;
+	}
+
 	/**
 	 * PostgreSQL Privilegien die per Grant einer Tabelle zugeordnet werden können.<br/>
 	 * 
@@ -832,6 +860,19 @@
 		return getOpenSessionCount(c, dbName, userName, null);
 	}
 
+	private static String getColumnNameQueryInPgStatActivity(Connection c) throws SQLException {
+		if (getVersion(c).compareTo(PgVersion._9_3) > 0)
+			return "query";
+		return "current_query";
+	}
+	
+	private static String getColumnNameProcPidInPgStatActivity(Connection c) throws SQLException {
+		if (getVersion(c).compareTo(PgVersion._9_3) > 0)
+			return "pid";
+		return "procpid";
+	}
+	
+
 	/**
 	 * Liefert die Anzahl der offenen Sessions zur Datenbank.
 	 * 
@@ -853,7 +894,8 @@
 		if (userName != null)
 			stmtSql += " AND usename = ?";
 		if (queryLike != null) {
-			stmtSql += " AND current_query like '" + queryLike + "'";
+
+			stmtSql += " AND "+getColumnNameQueryInPgStatActivity(c)+" like '" + queryLike + "'";
 		}
 
 		PreparedStatement stmt = c.prepareStatement(stmtSql);
@@ -966,9 +1008,9 @@
 	 */
 	static public Long getBlockingPid(Connection c, Long blockedPid) throws SQLException {
 		final String sql = "SELECT kl.pid as blocking_pid FROM pg_catalog.pg_locks bl "
-				+ "JOIN pg_catalog.pg_stat_activity a on bl.pid = a.procpid "
+				+ "JOIN pg_catalog.pg_stat_activity a on bl.pid = a."+getColumnNameProcPidInPgStatActivity(c)+" "
 				+ "JOIN pg_catalog.pg_locks kl "
-				+ "JOIN pg_catalog.pg_stat_activity ka on kl.pid = ka.procpid on bl.transactionid = kl.transactionid and bl.pid != kl.pid "
+				+ "JOIN pg_catalog.pg_stat_activity ka on kl.pid = ka."+getColumnNameProcPidInPgStatActivity(c)+" on bl.transactionid = kl.transactionid and bl.pid != kl.pid "
 				+ "WHERE not bl.granted AND bl.pid = " + blockedPid;
 		ResultSet rs = c.createStatement().executeQuery(sql);
 		try {
@@ -1001,9 +1043,9 @@
 		String where = "WHERE TRUE ";
 
 		if (filter == 1)
-			where += "AND current_query != '<IDLE>' ";
+			where += "AND "+getColumnNameQueryInPgStatActivity(c)+" != '<IDLE>' ";
 		else if (filter == 2)
-			where += "AND current_query not like '<IDLE>%' ";
+			where += "AND "+getColumnNameQueryInPgStatActivity(c)+" not like '<IDLE>%' ";
 
 		if (datName != null)
 			where += " AND datname like '" + datName + "'";
@@ -1015,7 +1057,8 @@
 			where += " AND age (now(),query_start) >= interval '" + minSecondsRunning + "s' ";
 		}
 
-		final String sql = "SELECT current_query, age (now(),query_start) as qage, waiting, procpid, client_addr, usename, application_name from pg_stat_activity "
+		final String sql = "SELECT " + getColumnNameQueryInPgStatActivity(c)
+				+ ", age (now(),query_start) as qage, waiting, " + getColumnNameProcPidInPgStatActivity(c)+", client_addr, usename, application_name from pg_stat_activity "
 				+ where + " ORDER BY qage desc";
 		ResultSet rs = c.createStatement().executeQuery(sql);
 



More information about the Schmitzm-commits mailing list