pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Mark Mizzi <mizzimark2001@gmail.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: EXPLAIN ANALYZE does not return accurate execution times
Date: Thu, 27 Oct 2022 09:43:44 -0400
Message-ID: <646782.1666878224@sss.pgh.pa.us> (raw)
In-Reply-To: <CANQK6i8FeAXhTa9F6p8JKoVRHene5da7vLjrA3+P=A2XR6J2pQ@mail.gmail.com>
References: <CANQK6i8FeAXhTa9F6p8JKoVRHene5da7vLjrA3+P=A2XR6J2pQ@mail.gmail.com>

Mark Mizzi <mizzimark2001@gmail.com> writes:
> When I run

> EXPLAIN ANALYZE SELECT * FROM unary;

> I get the following result:

>  Seq Scan on unary  (cost=0.00..1637.01 rows=100001 width=18) (actual
> time=0.009..6.667 rows=100001 loops=1)
>  Planning Time: 0.105 ms
>  Execution Time: 8.565 ms

> On the other hand, the following command

> time sudo -u postgres psql -c "SELECT * FROM unary" -o /dev/null

> returns after 17s with:
> sudo -u postgres psql -c "SELECT * FROM unary" -o /dev/null  0.01s user
> 0.01s system 0% cpu 16.912 total

The main thing actual execution does that EXPLAIN does not is
format the data and send it off to the client.  There are a
number of possible bottlenecks involved there -- TOAST fetching,
data formatting, network traffic, or client processing.  Watching
this example in "top", I see psql consuming near 100% CPU, meaning
that the problem is with psql's code to make a nicely-aligned
ASCII table out of the result.  This isn't too surprising: that
code was never meant to operate on resultsets that are too large
for human consumption.  You could use a different formatting rule,
or switch to COPY.

As an example, using

psql -c '\pset format unaligned' -c "SELECT * FROM unary" -o /dev/null

this example drops from ~16s to ~1.7s on my machine.

			regards, tom lane





view thread (5+ messages)  latest in thread

Message-ID: <646782.1666878224@sss.pgh.pa.us>
Permalink:  ../646782.1666878224@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/646782.1666878224@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, mizzimark2001@gmail.com
  Subject: Re: EXPLAIN ANALYZE does not return accurate execution times
  In-Reply-To: <646782.1666878224@sss.pgh.pa.us>
  References: <CANQK6i8FeAXhTa9F6p8JKoVRHene5da7vLjrA3+P=A2XR6J2pQ@mail.gmail.com> <646782.1666878224@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox