Use JSON format for EXPLAIN

This commit is contained in:
Javier Goizueta 2016-05-18 13:09:55 +02:00
parent 3987e83b7a
commit e1aa0bc7ae

View File

@ -9,19 +9,15 @@ function FilterStatsApi(pgQueryRunner) {
module.exports = FilterStatsApi;
function getEstimatedRows(pgQueryRunner, username, query, callback) {
pgQueryRunner.run(username, "EXPLAIN "+query, function(err, result_rows) {
pgQueryRunner.run(username, "EXPLAIN (FORMAT JSON)"+query, function(err, result_rows) {
if (err){
callback(err);
return;
}
var rows;
var query_plan = result_rows[0]['QUERY PLAN'];
var match;
if ( query_plan ) {
match = query_plan.match(/rows=(\d+)/);
}
if ( match ) {
rows = +match[1];
if ( result_rows[0] && result_rows[0]['QUERY PLAN'] &&
result_rows[0]['QUERY PLAN'][0] && result_rows[0]['QUERY PLAN'][0].Plan ) {
rows = result_rows[0]['QUERY PLAN'][0].Plan['Plan Rows'];
}
return callback(null, rows);
});