Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 33e4c27

Browse files
author
Ekaterina Sokolova
committed
Fix plan processing and update meson.build file.
1 parent 5578a38 commit 33e4c27

File tree

3 files changed

+85
-55
lines changed

3 files changed

+85
-55
lines changed

meson.build

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ pg_query_state = shared_module('pg_query_state',
2020
)
2121
contrib_targets += pg_query_state
2222

23-
extversion = '1.1'
24-
output_name = 'pg_query_state--' + extversion + '.sql'
23+
extversion = '1.2'
2524

2625
configure_file(
2726
input: 'init.sql',
28-
output: output_name,
27+
output: 'pg_query_state--' + extversion + '.sql',
2928
copy: true,
3029
install: true,
3130
install_dir: contrib_data_args['install_dir'],
@@ -34,6 +33,7 @@ configure_file(
3433
install_data(
3534
'pg_query_state.control',
3635
'pg_query_state--1.0--1.1.sql',
36+
'pg_query_state--1.1--1.2.sql',
3737
kwargs: contrib_data_args,
3838
)
3939

@@ -50,4 +50,10 @@ tests += {
5050
'--load-extension=pg_query_state',
5151
],
5252
},
53+
'tap': {
54+
'tests': [
55+
't/001_bad_progress_bar.pl',
56+
],
57+
'test_kwargs': {'timeout': 3000},
58+
},
5359
}

pg_query_state.c

Lines changed: 76 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,78 +1249,102 @@ DetachPeer(void)
12491249
}
12501250

12511251
/*
1252-
* Count progress of query execution like ratio of
1253-
* number of received to planned rows in persent.
1254-
* Changes of this function can lead to more plausible results.
1252+
* Extract the number of actual rows and planned rows from
1253+
* the plan for one node in text format. Returns their ratio,
1254+
* or 1 if there are already more received than planned.
12551255
*/
12561256
static double
1257-
CountProgress(char *plan_text)
1257+
CountNodeProgress(char *node_text)
12581258
{
1259-
char *plan; /* Copy of plan_text */
1260-
char *node; /* Part of plan with information about single node */
12611259
char *rows; /* Pointer to rows */
12621260
char *actual_rows_str; /* Actual rows in string format */
12631261
char *plan_rows_str; /* Planned rows in string format */
12641262
int len; /* Length of rows in string format */
12651263
double actual_rows; /* Actual rows */
12661264
double plan_rows; /* Planned rows */
1265+
1266+
rows = (char *) (strstr(node_text, "\"Actual Rows\": ") /* pointer to "Actual Rows" */
1267+
+ strlen("\"Actual Rows\": ") * sizeof(char)); /* shift by number of actual rows */
1268+
len = strstr(rows, "\n") - rows;
1269+
if ((strstr(rows, ",") - rows) < len)
1270+
len = strstr(rows, ",") - rows;
1271+
actual_rows_str = palloc(sizeof(char) * (len + 1));
1272+
actual_rows_str[len] = 0;
1273+
strncpy(actual_rows_str, rows, len);
1274+
actual_rows = strtod(actual_rows_str, NULL);
1275+
pfree(actual_rows_str);
1276+
1277+
rows = strstr(node_text, "\"Plan Rows\": ");
1278+
rows = (char *) (rows + strlen("\"Plan Rows\": ") * sizeof(char));
1279+
len = strstr(rows, ",") - rows;
1280+
plan_rows_str = palloc(sizeof(char) * (len + 1));
1281+
plan_rows_str[len] = 0;
1282+
strncpy(plan_rows_str, rows, len);
1283+
plan_rows = strtod(plan_rows_str, NULL);
1284+
pfree(plan_rows_str);
1285+
1286+
if (plan_rows > actual_rows)
1287+
return actual_rows / plan_rows;
1288+
else
1289+
return 1;
1290+
}
1291+
1292+
/*
1293+
* Count progress of query execution like ratio of
1294+
* number of received to planned rows in persent.
1295+
* Changes of this function can lead to more plausible results.
1296+
*/
1297+
static double
1298+
CountProgress(char *plan_text)
1299+
{
1300+
char *plan; /* Copy of plan_text */
1301+
char *node; /* Part of plan with information about single node */
1302+
char *rows; /* Pointer to rows */
12671303
double progress = 0; /* Summary progress on nodes */
12681304
int node_amount = 0; /* Amount of plantree nodes using in counting progress */
12691305

12701306
plan = palloc(sizeof(char) * (strlen(plan_text) + 1));
12711307
strcpy(plan, plan_text);
1272-
node = strtok(plan, "["); /* Get information about upper node */
1308+
1309+
/*
1310+
* plan_text contains information about upper node in format:
1311+
* "Plan": {
1312+
* and in different format for other nodes:
1313+
* "Plans": [
1314+
*
1315+
* We will iterate over square brackets as over plan nodes.
1316+
*/
1317+
node = strtok(plan, "["); /* Get information about first (but not upper) node */
1318+
1319+
/* Iterating over nodes */
12731320
while (node != NULL)
12741321
{
1275-
if (strstr(node, "Seq Scan") == NULL)
1322+
/* Result and Modify Table nodes must be skipped */
1323+
if ((strstr(node, "Result") == NULL) && (strstr(node, "ModifyTable") == NULL))
12761324
{
1277-
if (strstr(node, "ModifyTable") == NULL)
1325+
/* Filter node */
1326+
if ((rows = strstr(node, "Rows Removed by Filter")) != NULL)
12781327
{
1279-
if (strstr(node, "Result") == NULL)
1280-
{
1281-
if ((rows = strstr(node, "Rows Removed by Filter")) != NULL)
1282-
{
1283-
node_amount++;
1284-
rows = (char *) (rows + strlen("Rows Removed by Filter\": ") * sizeof(char));
1285-
1286-
/*
1287-
* Filter node have 2 conditions:
1288-
* 1) Was not filtered (current progress = 0)
1289-
* 2) Was filtered (current progress = 1)
1290-
*/
1291-
if (rows[0] != '0')
1292-
progress += 1;
1293-
}
1294-
else if ((rows = strstr(node, "\"Actual Rows\": ")) != NULL)
1295-
{
1296-
node_amount++;
1297-
rows = (char *) (rows + strlen("\"Actual Rows\": ") * sizeof(char));
1298-
len = strstr(rows, "\n") - rows;
1299-
if ((strstr(rows, ",") - rows) < len)
1300-
len = strstr(rows, ",") - rows;
1301-
actual_rows_str = palloc(sizeof(char) * (len + 1));
1302-
actual_rows_str[len] = 0;
1303-
strncpy(actual_rows_str, rows, len);
1304-
actual_rows = strtod(actual_rows_str, NULL);
1305-
pfree(actual_rows_str);
1306-
1307-
rows = strstr(node, "\"Plan Rows\": ");
1308-
rows = (char *) (rows + strlen("\"Plan Rows\": ") * sizeof(char));
1309-
len = strstr(rows, ",") - rows;
1310-
plan_rows_str = palloc(sizeof(char) * (len + 1));
1311-
plan_rows_str[len] = 0;
1312-
strncpy(plan_rows_str, rows, len);
1313-
plan_rows = strtod(plan_rows_str, NULL);
1314-
pfree(plan_rows_str);
1315-
1316-
if (plan_rows > actual_rows)
1317-
progress += actual_rows / plan_rows;
1318-
else
1319-
progress += 1;
1320-
}
1321-
}
1328+
node_amount++;
1329+
rows = (char *) (rows + strlen("Rows Removed by Filter\": ") * sizeof(char));
1330+
1331+
/*
1332+
* Filter node have 2 conditions:
1333+
* 1) Was not filtered (current progress = 0)
1334+
* 2) Was filtered (current progress = 1)
1335+
*/
1336+
if (rows[0] != '0')
1337+
progress += 1;
1338+
}
1339+
/* Not Filter node */
1340+
else if (strstr(node, "\"Actual Rows\": ") != NULL)
1341+
{
1342+
node_amount++;
1343+
progress += CountNodeProgress(node);
13221344
}
13231345
}
1346+
1347+
/* Get next node */
13241348
node = strtok(NULL, "[");
13251349
}
13261350

File renamed without changes.

0 commit comments

Comments
 (0)