pow support for pgbench

Started by Raúl Marín Rodríguezover 8 years ago46 messageshackers
Jump to latest
#1Raúl Marín Rodríguez
rmrodriguez@carto.com

Hi,

I've written a small patch to add support for pow() in pgbench.

The main reason behind it is that I'm currently using a shell call to do it
which takes between 2-10 ms that can be a big percentage of the time taken
by the whole transaction. For example (shortened):

latency average = 11.718 ms
- statement latencies in milliseconds:
2.834 \setshell POWER2 awk 'BEGIN {p=2^ARGV[1]; print p }'
:ZOOM_CURRENT
8.846 SELECT
ST_AsBinary(ST_Simplify(ST_SnapToGrid("the_geom_webmercator",:SNAP),
:SIMPLIFY)) AS geom FROM

I've also updated the related docs and added some tests. Please let me know
if I'm missing anything.

Regards,
*Raúl Marín Rodríguez*
carto.com

Attachments:

pgbench_pow_v1.patchtext/x-patch; charset=US-ASCII; name=pgbench_pow_v1.patchDownload+27-2
#2Michael Paquier
michael@paquier.xyz
In reply to: Raúl Marín Rodríguez (#1)
Re: pow support for pgbench

On Fri, Oct 27, 2017 at 4:51 PM, Raúl Marín Rodríguez
<rmrodriguez@carto.com> wrote:

I've written a small patch to add support for pow() in pgbench.

Cool.

The main reason behind it is that I'm currently using a shell call to do it
which takes between 2-10 ms that can be a big percentage of the time taken
by the whole transaction. For example (shortened):

latency average = 11.718 ms
- statement latencies in milliseconds:
2.834 \setshell POWER2 awk 'BEGIN {p=2^ARGV[1]; print p }'
:ZOOM_CURRENT
8.846 SELECT
ST_AsBinary(ST_Simplify(ST_SnapToGrid("the_geom_webmercator",:SNAP),
:SIMPLIFY)) AS geom FROM

I've also updated the related docs and added some tests. Please let me know
if I'm missing anything.

Please add this patch to the upcoming commit fest if you would like to
get some feedback:
https://commitfest.postgresql.org/15/

I am adding as well Fabien in CC who worked in getting the internal
function infrastructure in the shape it is now (waaay better) with
commit 86c43f4.
--
Michael

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#3Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Michael Paquier (#2)
Re: pow support for pgbench

Michael Paquier wrote:

Please add this patch to the upcoming commit fest if you would like to
get some feedback:
https://commitfest.postgresql.org/15/

I am adding as well Fabien in CC who worked in getting the internal
function infrastructure in the shape it is now (waaay better) with
commit 86c43f4.

I think Ra�l would do well to review this patch by Fabien
/messages/by-id/alpine.DEB.2.20.1710201835390.15170@lancre
which adds a few functions and operators.

--
�lvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#4Michael Paquier
michael@paquier.xyz
In reply to: Alvaro Herrera (#3)
Re: pow support for pgbench

On Mon, Oct 30, 2017 at 11:07 AM, Alvaro Herrera
<alvherre@alvh.no-ip.org> wrote:

Michael Paquier wrote:

Please add this patch to the upcoming commit fest if you would like to
get some feedback:
https://commitfest.postgresql.org/15/

I am adding as well Fabien in CC who worked in getting the internal
function infrastructure in the shape it is now (waaay better) with
commit 86c43f4.

I think Raúl would do well to review this patch by Fabien
/messages/by-id/alpine.DEB.2.20.1710201835390.15170@lancre
which adds a few functions and operators.

Good idea. pow() is not added by Fabien's patch, but an operator for
pow() could be something to add as well.
--
Michael

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#5Raúl Marín Rodríguez
rmrodriguez@carto.com
In reply to: Michael Paquier (#4)
Re: pow support for pgbench

Hi,

both patches seem complementary. I've rebased my changes on top of that
patch
(v14) in https://git.io/vFtnT and everything seems to be working fine.

On Mon, Oct 30, 2017 at 12:36 PM, Michael Paquier <michael.paquier@gmail.com

wrote:

On Mon, Oct 30, 2017 at 11:07 AM, Alvaro Herrera
<alvherre@alvh.no-ip.org> wrote:

Michael Paquier wrote:

Please add this patch to the upcoming commit fest if you would like to
get some feedback:
https://commitfest.postgresql.org/15/

I am adding as well Fabien in CC who worked in getting the internal
function infrastructure in the shape it is now (waaay better) with
commit 86c43f4.

I think Raúl would do well to review this patch by Fabien
/messages/by-id/alpine.DEB.2.20.

1710201835390.15170@lancre

which adds a few functions and operators.

Good idea. pow() is not added by Fabien's patch, but an operator for
pow() could be something to add as well.
--
Michael

--

*Raúl Marín Rodríguez*carto.com

#6Michael Paquier
michael@paquier.xyz
In reply to: Raúl Marín Rodríguez (#5)
Re: pow support for pgbench

On Mon, Oct 30, 2017 at 11:56 AM, Raúl Marín Rodríguez
<rmrodriguez@carto.com> wrote:

both patches seem complementary. I've rebased my changes on top of that
patch
(v14) in https://git.io/vFtnT and everything seems to be working fine.

Attaching patches directly to a thread is a better practice as if
github goes away, any Postgres developers can still have an access to
any code you publish using the public archives on postgresql.org.
--
Michael

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#7Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Michael Paquier (#6)
Re: pow support for pgbench

Michael Paquier wrote:

Attaching patches directly to a thread is a better practice as if
github goes away, any Postgres developers can still have an access to
any code you publish using the public archives on postgresql.org.

Also, by posting to pgsql-hackers indicating intention to integrate to
Postgres you're implicitly making a statement about the license of your
contribution; see the second half of the last paragraph at
https://wiki.postgresql.org/wiki/Archives_Policy

--
�lvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#8Raúl Marín Rodríguez
rmrodriguez@carto.com
In reply to: Alvaro Herrera (#7)
Re: pow support for pgbench

Sorry about the patch. Attaching it now so it can be considered as
submitted.

--

*Raúl Marín Rodríguez*carto.com

Attachments:

pgbench_pow_v2_pgbench-more-ops-funcs-14.patchtext/x-patch; charset=US-ASCII; name=pgbench_pow_v2_pgbench-more-ops-funcs-14.patchDownload+26-1
#9Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Michael Paquier (#2)
Re: pow support for pgbench

Hello Michaël,

I'm fine with having pow in pgbench.

I am adding as well Fabien in CC who worked in getting the internal
function infrastructure in the shape it is now (waaay better) with
commit 86c43f4.

It might be even better if https://commitfest.postgresql.org/15/985/,
which has been around for over one year (!), get through some day...

--
Fabien.
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#10Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Raúl Marín Rodríguez (#8)
Re: pow support for pgbench

Hello Raúl,

Sorry about the patch. Attaching it now so it can be considered as
submitted.

There is a typo in the XML doc:

<literal>1024.0/<literal>

Please check that the documentation compiles.

I'm at odds with having the integer version rely on a double pow(), even
if it works. I think that there should be a specific integer version which
does use integer operations. From stack overflow, the following is
suggested:

int ipow(int base, int exp)
{
int result = 1;
while (exp)
{
if (exp & 1)
result *= base;
exp >>= 1;
base *= base;
}

return result;
}

The integer version should be when x & y are integers *AND* y >= 0.

if y is a negative integer, the double version should be used.

--
Fabien.
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#11Raúl Marín Rodríguez
rmrodriguez@carto.com
In reply to: Fabien COELHO (#10)
Re: pow support for pgbench

Hi Fabien,

Thanks for the review.
I've fixed the documentation and added an ipow function that handles both
positive and negative ints, having 0^0 == 1 and 0^(negative) == PG_INT64_MAX
since that's what my glibc math.h pow() is returning.

On Sat, Nov 4, 2017 at 12:34 PM, Fabien COELHO <coelho@cri.ensmp.fr> wrote:

Hello Raúl,

Sorry about the patch. Attaching it now so it can be considered as

submitted.

There is a typo in the XML doc:

<literal>1024.0/<literal>

Please check that the documentation compiles.

I'm at odds with having the integer version rely on a double pow(), even
if it works. I think that there should be a specific integer version which
does use integer operations. From stack overflow, the following is
suggested:

int ipow(int base, int exp)
{
int result = 1;
while (exp)
{
if (exp & 1)
result *= base;
exp >>= 1;
base *= base;
}

return result;
}

The integer version should be when x & y are integers *AND* y >= 0.

if y is a negative integer, the double version should be used.

--
Fabien.

--

*Raúl Marín Rodríguez*carto.com

Attachments:

pgbench_pow_v3_pgbench-more-ops-funcs-14.patchtext/x-patch; charset=US-ASCII; name=pgbench_pow_v3_pgbench-more-ops-funcs-14.patchDownload+75-1
#12Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Raúl Marín Rodríguez (#11)
Re: pow support for pgbench

Hello Raúl,

I've fixed the documentation and added an ipow function that handles both
positive and negative ints, having 0^0 == 1 and 0^(negative) == PG_INT64_MAX
since that's what my glibc math.h pow() is returning.

From the comment:

* For exp < 0 return 0 except when the base is 1 or -1

I think that it should do what POW does in psql, i.e.:

fabien=# SELECT POW(2, -2); # 0.25

that is if exp < 0 the double version should be used, it should
not return 0.

Basically the idea is that the pgbench client-side version should behave
the same as the SQL version.

--
Fabien.
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#13Raúl Marín Rodríguez
rmrodriguez@carto.com
In reply to: Fabien COELHO (#12)
Re: pow support for pgbench

Hi Fabien,

Sorry for the confusion, I wasn't aware that SQL pow changed types
depending on
the input value.

I've modified the function to match more closely the behaviour of SQL,
except
that 0^(negative) returns 'double inf'. Do you think there is any value in
raising an error instead?

On Mon, Nov 6, 2017 at 2:12 PM, Fabien COELHO <coelho@cri.ensmp.fr> wrote:

Hello Raúl,

I've fixed the documentation and added an ipow function that handles both

positive and negative ints, having 0^0 == 1 and 0^(negative) ==
PG_INT64_MAX
since that's what my glibc math.h pow() is returning.

From the comment:

* For exp < 0 return 0 except when the base is 1 or -1

I think that it should do what POW does in psql, i.e.:

fabien=# SELECT POW(2, -2); # 0.25

that is if exp < 0 the double version should be used, it should
not return 0.

Basically the idea is that the pgbench client-side version should behave
the same as the SQL version.

--
Fabien.

--

*Raúl Marín Rodríguez*carto.com

Attachments:

pgbench_pow_v4_pgbench-more-ops-funcs-14.patchtext/x-patch; charset=US-ASCII; name=pgbench_pow_v4_pgbench-more-ops-funcs-14.patchDownload+109-1
#14Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Raúl Marín Rodríguez (#13)
Re: pow support for pgbench

Hello,

Sorry for the confusion, I wasn't aware that SQL pow changed types
depending on the input value.

Indeed, this is quite strange...

fabien=# SELECT i, POW(2, i) FROM generate_series(-2, 2) AS i;
-2 | 0.25
-1 | 0.5
0 | 1
1 | 2
2 | 4

I've modified the function to match more closely the behaviour of SQL,
except that 0^(negative) returns 'double inf'. Do you think there is any
value in raising an error instead?

fabien=# SELECT POW(0,-1);
ERROR: zero raised to a negative power is undefined

Hmmmm... I'm fine with double inf, because exception in pgbench means the
end of the script, which is not desirable for benchmarking purposes.

I think that:

- you can simplify the ipow function by removing handling of y<0 case,
maybe add an assert to be sure to avoid it.

- you should add more symmetry and simplify the evaluation:

if (int & int)
{
i1, i2 = ...;
if (i2 >= 0)
setIntValue(retval, ipow(i1, i2));
else
// conversion is done by C, no need to coerce again
setDoubleValue(retval, pow(i1, i2));
}
else
{
d1, d2 = ...;
setDoubleValue(retval, pow(d1, d2));
}

Add a test case to show what happens on NULL arguments, hopefully the
result is NULL.

--
Fabien.

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#15Raúl Marín Rodríguez
rmrodriguez@carto.com
In reply to: Fabien COELHO (#14)
Re: pow support for pgbench

Hi,

Indeed, this is quite strange...

I don't want to go too deep into it, but you get stuff like this:

Select pow(2.0, -3)::text = pow(2, -3)::text;
?column?
----------
f
(1 row)

- you can simplify the ipow function by removing handling of y<0 case,

maybe add an assert to be sure to avoid it.

I agree, done.

- you should add more symmetry and simplify the evaluation:

Done too.

Add a test case to show what happens on NULL arguments, hopefully the

result is NULL.

Done and it does.

Thanks again for the review.

On Mon, Nov 6, 2017 at 4:14 PM, Fabien COELHO <coelho@cri.ensmp.fr> wrote:

Hello,

Sorry for the confusion, I wasn't aware that SQL pow changed types

depending on the input value.

Indeed, this is quite strange...

fabien=# SELECT i, POW(2, i) FROM generate_series(-2, 2) AS i;
-2 | 0.25
-1 | 0.5
0 | 1
1 | 2
2 | 4

I've modified the function to match more closely the behaviour of SQL,

except that 0^(negative) returns 'double inf'. Do you think there is any
value in raising an error instead?

fabien=# SELECT POW(0,-1);
ERROR: zero raised to a negative power is undefined

Hmmmm... I'm fine with double inf, because exception in pgbench means the
end of the script, which is not desirable for benchmarking purposes.

I think that:

- you can simplify the ipow function by removing handling of y<0 case,
maybe add an assert to be sure to avoid it.

- you should add more symmetry and simplify the evaluation:

if (int & int)
{
i1, i2 = ...;
if (i2 >= 0)
setIntValue(retval, ipow(i1, i2));
else
// conversion is done by C, no need to coerce again
setDoubleValue(retval, pow(i1, i2));
}
else
{
d1, d2 = ...;
setDoubleValue(retval, pow(d1, d2));
}

Add a test case to show what happens on NULL arguments, hopefully the
result is NULL.

--
Fabien.

--

*Raúl Marín Rodríguez *carto.com

Attachments:

pgbench_pow_v5_pgbench-more-ops-funcs-14.patchtext/x-patch; charset=US-ASCII; name=pgbench_pow_v5_pgbench-more-ops-funcs-14.patchDownload+89-1
#16Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Raúl Marín Rodríguez (#15)
Re: pow support for pgbench

I don't want to go too deep into it, but you get stuff like this:

Select pow(2.0, -3)::text = pow(2, -3)::text;

Sure. It does so with any overloaded operator or function:

fabien=# SELECT (2.0 + 3)::TEXT = (2 + 3)::TEXT; # f

Patch applies, make check ok in pgbench, doc gen ok.

ipow code is nice and simple.

I switched the patch to "Ready for Committer"

Let's now hope that a committer gets around to consider these patch some
day.

--
Fabien.

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#17Michael Paquier
michael@paquier.xyz
In reply to: Fabien COELHO (#16)
Re: [HACKERS] pow support for pgbench

On Tue, Nov 7, 2017 at 1:34 AM, Fabien COELHO <coelho@cri.ensmp.fr> wrote:

Let's now hope that a committer gets around to consider these patch some
day.

Which is not the case yet, so moved to CF 2018-01. Please note that
the patch proposed does not apply anymore, so its status is changed to
"waiting on author" for a rebase.
--
Michael

#18Raúl Marín Rodríguez
rmrodriguez@carto.com
In reply to: Michael Paquier (#17)
Re: [HACKERS] pow support for pgbench

Hi,

I've rebased the patch so it can be applied cleanly on top of current
master.

On Fri, Dec 1, 2017 at 3:55 AM, Michael Paquier <michael.paquier@gmail.com>
wrote:

On Tue, Nov 7, 2017 at 1:34 AM, Fabien COELHO <coelho@cri.ensmp.fr> wrote:

Let's now hope that a committer gets around to consider these patch some
day.

Which is not the case yet, so moved to CF 2018-01. Please note that
the patch proposed does not apply anymore, so its status is changed to
"waiting on author" for a rebase.
--
Michael

--

*Raúl Marín Rodríguez *carto.com

Attachments:

pgbench_pow_v6.patchtext/x-patch; charset=US-ASCII; name=pgbench_pow_v6.patchDownload+89-3
#19Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Raúl Marín Rodríguez (#18)
Re: [HACKERS] pow support for pgbench

Hello Raúl,

I've rebased the patch so it can be applied cleanly on top of current
master.

The idea is that it would be relative to the "more functions and
operators" patch, but I guess this is too much for people checking, so I'm
fine with having it with the current base.

Patch applies cleanly, make check ok.

Back to "ready".

--
Fabien.

#20Raúl Marín Rodríguez
rmrodriguez@carto.com
In reply to: Fabien COELHO (#19)
Re: [HACKERS] pow support for pgbench

Hi Fabien,

The idea is that it would be relative to the "more functions and operators"

patch, but I guess this is too much for people checking, so I'm fine with
having it with the current base.

I tried applying the last "more functions and operators" patch
(pgbench-more-ops-funcs-14.patch) but it also stopped applying cleanly so I
decided to go for master to avoid having too many issues. Let me know if
you rework that patch and I'll be happy to rebase mine on top.

--

*Raúl Marín Rodríguez *carto.com

#21Robert Haas
robertmhaas@gmail.com
In reply to: Raúl Marín Rodríguez (#18)
#22Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Robert Haas (#21)
#23Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Fabien COELHO (#22)
#24Raúl Marín Rodríguez
rmrodriguez@carto.com
In reply to: Robert Haas (#21)
#25Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Raúl Marín Rodríguez (#24)
#26Robert Haas
robertmhaas@gmail.com
In reply to: Fabien COELHO (#25)
#27Michael Paquier
michael@paquier.xyz
In reply to: Robert Haas (#26)
#28Tom Lane
tgl@sss.pgh.pa.us
In reply to: Michael Paquier (#27)
#29Michael Paquier
michael@paquier.xyz
In reply to: Tom Lane (#28)
#30Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Tom Lane (#28)
#31Raúl Marín Rodríguez
rmrodriguez@carto.com
In reply to: Fabien COELHO (#30)
#32Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Raúl Marín Rodríguez (#31)
#33Robert Haas
robertmhaas@gmail.com
In reply to: Raúl Marín Rodríguez (#31)
#34Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Robert Haas (#33)
#35Chapman Flack
chap@anastigmatix.net
In reply to: Raúl Marín Rodríguez (#15)
#36Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Raúl Marín Rodríguez (#24)
#37Raúl Marín Rodríguez
rmrodriguez@carto.com
In reply to: Fabien COELHO (#36)
#38Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Raúl Marín Rodríguez (#37)
#39Raúl Marín Rodríguez
rmrodriguez@carto.com
In reply to: Fabien COELHO (#38)
#40Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Raúl Marín Rodríguez (#39)
#41Robert Haas
robertmhaas@gmail.com
In reply to: Raúl Marín Rodríguez (#39)
#42Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Robert Haas (#41)
#43Raúl Marín Rodríguez
rmrodriguez@carto.com
In reply to: Fabien COELHO (#42)
#44Michael Paquier
michael@paquier.xyz
In reply to: Fabien COELHO (#42)
#45Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Michael Paquier (#44)
#46Robert Haas
robertmhaas@gmail.com
In reply to: Raúl Marín Rodríguez (#43)