Postgresql equivalent of Python's timeit?

Peter J. Holzer hjp-python at hjp.at
Sun Sep 17 12:34:17 EDT 2023


On 2023-09-17 11:01:43 +0200, Albert-Jan Roskam via Python-list wrote:
>    On Sep 15, 2023 19:45, "Peter J. Holzer via Python-list"
>    <python-list at python.org> wrote:
> 
>      On 2023-09-15 17:42:06 +0200, Albert-Jan Roskam via Python-list wrote:
>      >    This is more related to Postgresql than to Python, I hope this is
>      ok.
>      >    I want to measure Postgres queries N times, much like Python timeit
>      >    (https://docs.python.org/3/library/timeit.html). I know about
>      EXPLAIN
>      >    ANALYZE and psql \timing, but there's quite a bit of variation in
>      the
>      >    times. Is there a timeit-like function in Postgresql?
> 
>      Why not simply call it n times from Python?
> 
>      (But be aware that calling the same query n times in a row is likely to
>      be
>      unrealistically fast because most of the data will already be in
>      memory.)
> 
>    =====
>    Thanks, I'll give this a shot. Hopefully the caching is not an issue if I
>    don't re-use the same database connection.

There is some per-session caching, but the bulk of it is shared between
sessions or even in the operating system. And you wouldn't want to get
rid of these caches either (which you could do by rebooting or - a bit
faster - restarting postgres and dropping the caches
(/proc/sys/vm/drop_caches on Linux), because that would make the
benchmark unrealistically slow (unless you want to establish some
worst-case baseline). During normal operations some data will be cached,
but probably not all of it and it will change depending on workload and
possibly other factors.

I think Avi's advice to wait for a few minutes between repetitions is
good. Of course that means that you can't just time the whole thing but
have to time each query separately and then compute the average. (On the
bright side that also gives you the opportunity to compute standard
deviation, min, max, quantiles, etc.)

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp at hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://mail.python.org/pipermail/python-list/attachments/20230917/26558ed8/attachment.sig>


More information about the Python-list mailing list