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 1ox8BY-0006Lh-Lb for pgsql-admin@arkaria.postgresql.org; Mon, 21 Nov 2022 14:54:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ox8BX-0000jI-3n for pgsql-admin@arkaria.postgresql.org; Mon, 21 Nov 2022 14:54:11 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ox8BW-0000j8-Ph for pgsql-admin@lists.postgresql.org; Mon, 21 Nov 2022 14:54:10 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ox8BQ-0005sy-LP for pgsql-admin@lists.postgresql.org; Mon, 21 Nov 2022 14:54:10 +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 2ALEs1mQ3118193; Mon, 21 Nov 2022 09:54:01 -0500 From: Tom Lane To: Satalabaha Postgres cc: pgsql-admin@lists.postgresql.org Subject: Re: Finding query execution time using \timing and EXPLAIN ANALYZE.. In-reply-to: References: Comments: In-reply-to Satalabaha Postgres message dated "Mon, 21 Nov 2022 16:58:31 +0530" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3118191.1669042441.1@sss.pgh.pa.us> Date: Mon, 21 Nov 2022 09:54:01 -0500 Message-ID: <3118192.1669042441@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Satalabaha Postgres writes: > I was trying to get elapsed time for the sql using the \timing command in > psql as well as EXPLAIN ANALYZE.. I see both the commands reporting > different elapsed time. And this is after repeated execution of the same > query and not the first execution. In this case, which one would be the > right execution time for the query. They're presumably both accurate, but they aren't measuring the same thing. psql's \timing reports the end-to-end time from the client's perspective. EXPLAIN ANALYZE reports the time for two components of the server's processing. So \timing includes all of these: 1. Transmission of the query across the network to the server 2. Parsing and parse analysis of the query 3. Rewriting (a/k/a view expansion) of the query 4. Planning 5. Execution 6. Formatting of the query results as text 7. Transmission of the results across the network to the client. while EXPLAIN is only telling you about steps 4 and 5. Having said that, I'm a bit baffled by the size of the difference. This example doesn't look like any of the other steps should take very long: it's a short simple query, there doesn't seem to be any view involved, and the result is only one row. Unless maybe you're using tin-cans-and-string for the network connection? There are various effects whereby the first execution of a query can take longer than repeat executions, but you said you'd controlled for that. Also, how stable are these numbers if you repeat them several times? It might be mostly about noise and/or the effects of CPU power management. On my Linux box I find that I don't get very reproducible numbers for short queries unless I've done "sudo cpupower frequency-set --governor performance". regards, tom lane