Skip to content

Commit cfe1276

Browse files
committed
Use standard librart sqrt function in pg_stat_statements
The stddev calculation included a faster but unportable sqrt function. This is not worth the extra effort, and won't work everywhere. If the standard library function is good enough for the SQL function it should be good enough here too.
1 parent 3a20b0e commit cfe1276

File tree

1 file changed

+2
-19
lines changed

1 file changed

+2
-19
lines changed

contrib/pg_stat_statements/pg_stat_statements.c

+2-19
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
*/
5858
#include "postgres.h"
5959

60+
#include <math.h>
6061
#include <sys/stat.h>
6162
#include <unistd.h>
6263

@@ -326,7 +327,6 @@ static char *generate_normalized_query(pgssJumbleState *jstate, const char *quer
326327
int *query_len_p, int encoding);
327328
static void fill_in_constant_lengths(pgssJumbleState *jstate, const char *query);
328329
static int comp_location(const void *a, const void *b);
329-
static inline double sqrtd(const double x);
330330

331331

332332
/*
@@ -1583,7 +1583,7 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
15831583
*/
15841584
if (tmp.calls > 1)
15851585
values[i++] =
1586-
Float8GetDatum(sqrtd(tmp.sum_var_time / tmp.calls));
1586+
Float8GetDatum(sqrt(tmp.sum_var_time / tmp.calls));
15871587
else
15881588
values[i++] = Float8GetDatum(0.0);
15891589
}
@@ -2968,20 +2968,3 @@ comp_location(const void *a, const void *b)
29682968
else
29692969
return 0;
29702970
}
2971-
2972-
/*
2973-
* fast sqrt algorithm: reference from Fast inverse square root algorithms.
2974-
*/
2975-
static inline double
2976-
sqrtd(const double x)
2977-
{
2978-
double x_half = 0.5 * x;
2979-
long long int tmp = 0x5FE6EB50C7B537AAl - ( *(long long int*)&x >> 1);
2980-
double x_result = * (double*)&tmp;
2981-
2982-
x_result *= (1.5 - (x_half * x_result * x_result));
2983-
/* If retry this calculation, it becomes higher precision at sqrt */
2984-
x_result *= (1.5 - (x_half * x_result * x_result));
2985-
2986-
return x_result * x;
2987-
}

0 commit comments

Comments
 (0)