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 1nC8H1-0003hu-2e for pgsql-general@arkaria.postgresql.org; Mon, 24 Jan 2022 22:57:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nC8Gz-0008WA-NM for pgsql-general@arkaria.postgresql.org; Mon, 24 Jan 2022 22:57:17 +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 1nC8Gz-0008UW-DD for pgsql-general@lists.postgresql.org; Mon, 24 Jan 2022 22:57:17 +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 1nC8Gv-0005LO-N5 for pgsql-general@lists.postgresql.org; Mon, 24 Jan 2022 22:57:15 +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 20OMv0fc2161864; Mon, 24 Jan 2022 17:57:00 -0500 From: Tom Lane To: Hannes Erven cc: pgsql-general@lists.postgresql.org Subject: Re: Big variance in execution times of simple queries In-reply-to: References: Comments: In-reply-to Hannes Erven message dated "Mon, 24 Jan 2022 23:07:02 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <2161862.1643065020.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Mon, 24 Jan 2022 17:57:00 -0500 Message-ID: <2161863.1643065020@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hannes Erven writes: > I'm looking at a "SELECT * FROM pg_stat_statements" output and am > puzzled by the huge differences between min/max_exec_time even for > simple queries. > The most extreme example is probably the statement used by the > application's connection health check: > SELECT 1 > min=0.001, mean=0.00386, max=36.812 Am I right in guessing that that's the first/only command issued in its connection? If so, there would be catalog cache population overhead involved here, which might explain some of the variance. (If some but not all instances are first commands, that'd definitely help explain the variance. PG backends are fairly heavyweight objects, and take a bit to come up to speed.) > Other statements with huge variances include: > SET application_name=$1 > min=0.002, mean=0.005, max=9.177 Could be same issue. > SELECT * FROM table WHERE id=$1  (where ID is the primary key column; > table has 0.5M rows and is frequently vacuum analyzed) > min=0.010, mean=0.260, max=12338.665 This, on the other hand, seems odd. Maybe the query sometimes gets blocked on a lock? Enabling log_lock_waits might help you check. > According to the system's monitoring, there is no pressure on any > resource (cpu/mem/io). It's 13.5-2pgdg20.04+1 on Ubuntu 20.4; the VM has > 12 cpus/16GB memory, ceph-based SSD storage (latency ~1.5ms), and runs > on max_connections=100 with usually 25-40 processes being connected. Hmm ... if you're sitting on a VM rather than directly on the iron, then there's a whole bunch of other potential reasons for irregular performance, most of which you probably can't see from inside the VM. regards, tom lane