Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oo3Aj-0007pq-Nu for pgsql-general@arkaria.postgresql.org; Thu, 27 Oct 2022 13:43:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oo3Ai-0003OF-Ci for pgsql-general@arkaria.postgresql.org; Thu, 27 Oct 2022 13:43:48 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oo3Ai-0003O5-1o for pgsql-general@lists.postgresql.org; Thu, 27 Oct 2022 13:43:48 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oo3Af-0002DQ-Rp for pgsql-general@lists.postgresql.org; Thu, 27 Oct 2022 13:43:47 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 29RDhirI646783; Thu, 27 Oct 2022 09:43:44 -0400 From: Tom Lane To: Mark Mizzi cc: pgsql-general@lists.postgresql.org Subject: Re: EXPLAIN ANALYZE does not return accurate execution times In-reply-to: References: Comments: In-reply-to Mark Mizzi message dated "Thu, 27 Oct 2022 15:28:14 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <646781.1666878224.1@sss.pgh.pa.us> Date: Thu, 27 Oct 2022 09:43:44 -0400 Message-ID: <646782.1666878224@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Mark Mizzi 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