Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ut1tp-00ElWl-9R for pgsql-hackers@arkaria.postgresql.org; Mon, 01 Sep 2025 10:36:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1ut1to-005eyn-KU for pgsql-hackers@arkaria.postgresql.org; Mon, 01 Sep 2025 10:36:33 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ut1to-005eyf-8q for pgsql-hackers@lists.postgresql.org; Mon, 01 Sep 2025 10:36:32 +0000 Received: from mail-ot1-x334.google.com ([2607:f8b0:4864:20::334]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1ut1tm-002p74-18 for pgsql-hackers@postgresql.org; Mon, 01 Sep 2025 10:36:31 +0000 Received: by mail-ot1-x334.google.com with SMTP id 46e09a7af769-74543fea20dso4082056a34.0 for ; Mon, 01 Sep 2025 03:36:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756722990; x=1757327790; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=59N2C719xNHlp4tySfCtYVngFQoNvkhRidKDhW9loXY=; b=Moadm21++I2HgOivXrZU1WHU1yq9DmL0xzmFwHmek/PXRUpY+G1wdfBZgABH8IxfOi c8yO0chAV2By1aS8q6GbiGlkCnKZsBkUE1feoB/uESGjOgeSxkeGAAmKdhunjSO2/lUk 7DjGYwgjAk0N43Rw52FdhfnPu/sZlmn8yXMg6E/T1H6it5Qrf7nws2RfEkr9r3jknnCS +W96DiNYWcA8V5s8lbXpDvv6DxEaE/TM2i5+0FmqL0YPqhNXzm61a0/CKxiENPHPxq41 z6gGIF6ffnoTCEaACMqmF3OEuaLhld5O8jZ00NKjj0yRFS8BqhqgWyGsNg/pjstySpbU XKGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756722990; x=1757327790; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=59N2C719xNHlp4tySfCtYVngFQoNvkhRidKDhW9loXY=; b=gXyzLJQhzGKtKAOWldFot4vIMoH1zac0OAo8YrYsfuzsZbGKZHJzxKLMu+m/VhI9vZ PRoRTK2nPtkJuWjy+4LA/hMmk5u1jc5poFMlBSKBtgA4uO8OcLRK108F0IaIIWIyRka7 T/AOmwSD3d/r4dkuwbkxF3AGT++fVEP6bh/QVM3/NceBW59AvKG576GA/hsm87KGNyJ6 soJdxjEg7UgtsS/TowLg7tNosGyKWyjU1OiaN4+S99bQpDp/CfEx1vbqfMr/bgm2cKZl ilZ15nMVK4ZsT8Qowy+28zL3YXE7i0K4uWEJe3j2WG++9Mn1PgyhGB5J001zOTcYBkLB 2mNA== X-Forwarded-Encrypted: i=1; AJvYcCWgzr/LKozr0wEdX0Rg/jHHDxoDX9JndM1F+lZwpIBqHVdIT6eZ7VxunuVB1XCcCtYJanEpA0Vgv1HA9NL/@postgresql.org X-Gm-Message-State: AOJu0Yy8iBvGbpLs0TllSEc7zEQL6jzMgUcVml8qoaqccxsAtHvNNSK/ rxDIzJUSOIl1fz5GBpGuQFQG06cg3c0cKI8tHqmaFR9wvRZreOUcQw+z X-Gm-Gg: ASbGncuNYVt77wg/J3iMl0xdqU/EfGIKXO0MXFstWt5I0BPTLUAJQUwvdn9YRjavZm5 X33Km2PbBfiOMlKJSqbcswiic4Dw1aR2hA3EQPodlIiMy+uWv9lEkaiJfoJ+bLIROX1Ez7c9QcL ypDtqu2l0cuytWHvBvTifDWoXPS8aNwV0reIlKA60ojQd+3SqlRolwVE6QPlic7Z5F2RrDZYrDR CzPVmdIXJFkNMPomIM7QSICnTr47yWjTKyne+lRZ33COw9iK/yfbgSQpYfVaAMSmNwSkffIpqOu RogoEf/Hc7veo1npReiwAzc2vzATZCyZWOUhbuPHej9KHAEq57QeqMY/Wm2I0wSsHz7GbrSVf00 tNQNJT/f+lTupk1oH4T/pdMb9odCP3Na/ga+QSFSLXbvMLosOEyb7ZXBybEgjpiOYLZ3V4Q== X-Google-Smtp-Source: AGHT+IH9UV9EtYln+qao6lbXLLuV3N1mqUFgXv6SDBhwktJD2WnD2L69lzwuT+DZoohi9ltlKxqM4A== X-Received: by 2002:a05:6830:92a:b0:73e:9ee1:3d63 with SMTP id 46e09a7af769-74569db7a87mr3302892a34.9.1756722990287; Mon, 01 Sep 2025 03:36:30 -0700 (PDT) Received: from [10.255.184.220] (vip-148-139-1-229.cust.service-now.com. [148.139.1.229]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-61e3209f7cfsm655733eaf.18.2025.09.01.03.36.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 01 Sep 2025 03:36:29 -0700 (PDT) Message-ID: Date: Mon, 1 Sep 2025 12:36:24 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Reduce timing overhead of EXPLAIN ANALYZE using rdtsc? To: Lukas Fittl , Andres Freund Cc: Pavel Stehule , Tomas Vondra , vignesh C , Michael Paquier , Ibrar Ahmed , Maciek Sakrejda , pgsql-hackers References: <24aa958f-0463-03d4-ce54-20b277c954c6@gmail.com> <20230121041439.zraxavau2wqf2ys3@awork3.anarazel.de> <20230123174158.rhmrbmjp6ycak64r@awork3.anarazel.de> <20230207181218.47irz3of34qsww5l@awork3.anarazel.de> <989acbcf-e950-7911-c2e2-fe7aa7d15108@gmail.com> <198ef658-a5b7-9862-2017-faf85d59e3a8@gmail.com> <20240601185200.riqzbabh5azna2fr@alap3.anarazel.de> Content-Language: en-US From: David Geier In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi Lukas! On 01.03.2025 08:45, Lukas Fittl wrote: > On Sun, Jun 2, 2024 at 1:08 AM Andres Freund wrote: > >> At some point this patch switched from rdtsc to rdtscp, which imo largely >> negates the point of it. What lead to that? > > > From what I can gather, it appears this was an oversight when David first > reapplied the work on the instr_time changes that were committed. Yes, that was by accident. > > I've come back to this and rebased this, as well as: Thanks for moving this forward. > - Added support for VMs running under KVM/VMware Hypervisors > > On that last item, this does indeed make a difference on VMs, contrary to > the code comment in earlier versions (and I've not seen any odd behaviors > again, FWIW): How can we be sure we've actually covered all hypervisors? How much coverage do we have in the build farm? Are we good if passes on all build animals? > > On a c5.xlarge (Skylake-SP or Cascade Lake) on AWS, with the same test as > done initially in this thread: > > SELECT COUNT(*) FROM lotsarows; > Time: 974.423 ms > > EXPLAIN (ANALYZE, TIMING OFF) SELECT COUNT(*) FROM lotsarows; > Time: 1336.196 ms (00:01.336) > > Without patch: > EXPLAIN (ANALYZE) SELECT COUNT(*) FROM lotsarows; > Time: 2165.069 ms (00:02.165) > > Per loop time including overhead: 22.15 ns > > With patch: > EXPLAIN (ANALYZE, TIMING ON) SELECT COUNT(*) FROM lotsarows; > Time: 1654.289 ms (00:01.654) > > Per loop time including overhead: 9.81 ns > > I'm registering this again in the current commitfest to help reviews. > > Open questions I have: > - Could we rely on checking whether the TSC timesource is invariant (via > CPUID), instead of relying on Linux choosing it as a clocksource? Why do you want to do that? Are you concerned that Linux might pick a different clock source even though invariant TSC is available? We could code our own check but looking at the Linux kernel code, this is a bit more involved if we want to do it completely right. They check e.g. if the TSC is also synchronized across different CPUs, which is not the case if they're on different chassis (see unsynchronized_tsc() -> apic_is_clustered_box()). I think it's safer to start with relying on the kernel. Some research suggests that the TSC is the preferred clock source if available. > - For the Hypervisor CPUID checks I had to rely on __cpuidex which is only > available on newer GCC versions ( > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95973), how do we best check > for its presence? (compiler version, or rather configure check?) -- note > this is also the reason the patch fails the clang compiler warning check in > CI, despite clang having support in recent versions ( > https://reviews.llvm.org/D121653) What about instead using #if !__has_builtin(_cpuidex) ... #endif to define the built-in ourselves as a function in case it doesn't exist? -- David Geier