How to get query plan information from Postgres in JDBC

I want to display the cost numbers from the query plan that you receive when you β€œExplain” the request. Is there a way to get this data inside a Java ResultSet (or similar object)?

+4
source share
2 answers

Of course, just run it like a regular statement:

Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery("explain analyze select * from foo"); while (rs.next()) { System.out.println(rs.getString(1)); } 
+7
source

In addition to the question above, I would suggest that you use the ability to format EXPLAIN plans as XML in PostgreSQL 9.0 and later.

EXPLAIN (parse, format xml) SELECT ...

This will give you an explanation with which you can easily work in Java by manipulating it as XML.

+1
source

Source: https://habr.com/ru/post/1384317/


All Articles