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.96) (envelope-from ) id 1vRQWb-00Cqef-1W for pgsql-hackers@arkaria.postgresql.org; Fri, 05 Dec 2025 07:46:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vRQWZ-006y4k-2E for pgsql-hackers@arkaria.postgresql.org; Fri, 05 Dec 2025 07:46:44 +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.96) (envelope-from ) id 1vRQWZ-006y4c-1K for pgsql-hackers@lists.postgresql.org; Fri, 05 Dec 2025 07:46:43 +0000 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vRQWX-003F04-0p for pgsql-hackers@postgresql.org; Fri, 05 Dec 2025 07:46:42 +0000 Received: by mail-wr1-x444.google.com with SMTP id ffacd0b85a97d-42e29739ff1so765160f8f.1 for ; Thu, 04 Dec 2025 23:46:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764920800; x=1765525600; 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=1i7mrD+g/ooV2YvIYqtOvxwGsxB5xSBsKKLd+Wt624Q=; b=SYbsKjA6+6ckS5cJYj/vvQFDgM1G+6HvFoxJRV/1fuZ74rDVx8yGpNyjF09sPXuRe3 RrVS6xxKysQ9/HRgxwfcmuHcH9yHe03VM41Y0gg0slBpOa0jCp/H00bxpfmBZDH2FDWl mIQSZRXHIXBGHp5Em1aiRB/Z5JnRuI+Gou1f2CDFpE/HC93XrSm9iwKEAegWRuqRM/lS 9IirrFTBVGGCU9g7YlfBErsHLZ9LiO/gR2LDGNThpvyTMk8Zy63u6sLI5Dgrso/TgRRC HXWRrze4+znnF1aja+unQm/VhC3CVlPXbLXDtg4YO+nDDic1wBAeQguljUtHuDDQr5zp l80w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764920800; x=1765525600; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=1i7mrD+g/ooV2YvIYqtOvxwGsxB5xSBsKKLd+Wt624Q=; b=BzBM0hbUaQGNGu59Td6rjiU/6v6BQdf6mBHyvJ6ngoPBU9E6UeofHtzGcPoCGfDBqn RXS8pvAScPd51DtN9EIHvOPNSavqbRue+qzW466pKX2SfaGrgjUdVNCg1vNuw7FfRQnE bhxjk3GdeFy6/GoEm9BASoWR9n2125+2z8FFJ8pWMhUOdz/VZfMy9fx4qZ4xyL6d5daY It4i5jS+UX5idk/iWA45ovA7S2Geg8WsynnXwnwNEl4wh382/N/7oV1SeEMYkyHe6IQ7 6Dr9ERn9deg5f5LBr48Cxs5JwDWSSPvjSUopVI/0zZHkkb3BuKBQu6sJc8awlsKqgXtJ eSjw== X-Forwarded-Encrypted: i=1; AJvYcCXto/ZXIIm512tChXzWmGg1kzQdeROqfME0YUUA0uSMTGknRu5F/QBBhtbprgwIFNeTCDxxTVIHjsHsE2jc@postgresql.org X-Gm-Message-State: AOJu0YyhuI8t3kMu3hnOYHdfec9P8RZR8Zyy1wclOzZjLSaTCXQ8wEbS H7OUVg9EnqJ179qSvQO5WM3DIn7d3vD7NLv2oAnwaGX3EY/mQH83nrey X-Gm-Gg: ASbGncvIMX59dCwKmsdIFxjcQQW8/0JdFOfkHzp2q8u5Mp3fw7CrrGEp1ux86xvdUOG SJtkHv+CNpd9m1/B3lJ97w27s4m5j5RuXZjuj72tT2VTfs3ZAOMpYXW+PJX0LTqzrhZPt00Yhei vo+K5lnXL5Si/Gu7yW75ZKzw1KyTBbi+C4KE1w9G9ia+otU/mN74VEEI0HqobiLWi2ecciIZ+2c egh/zjDm2phzMieSeyTlfH9usMsJlRvOUV2r6t4MUrLtdeH6IscrpwHf2CsWV011z8Q2gMK5BKP FaFAOJ6BJret6Ga3jQofLAV+MRXTxzHv+/FJ9H4Xf5dDWYzEfhmpVX/x7FnEBikBsAO6i2BnxD/ IFTlhOR2Z7fMmo1cshY9w2PxgaWz+0g6FOkwiACoiHwyrkOO2l5eF7hc1I89Q6b2X4thlI/M33b cIc7p3PfhNOfeZa3f74G7bxQ== X-Google-Smtp-Source: AGHT+IG6kyke9NKFqpTFY2YygZm5QyDwc+ZCnyILDuEG9D5xX4rxfkXR1H2WHhffkJTyEjGKBwWoqw== X-Received: by 2002:a5d:51cc:0:b0:429:ca7f:8d6f with SMTP id ffacd0b85a97d-42f78772702mr5143261f8f.15.1764920800349; Thu, 04 Dec 2025 23:46:40 -0800 (PST) Received: from [192.168.2.32] ([165.225.27.7]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-42f7cbe90f0sm7263056f8f.9.2025.12.04.23.46.39 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 04 Dec 2025 23:46:39 -0800 (PST) Message-ID: <7717ba64-9283-49ec-aa3f-34c6de848b93@gmail.com> Date: Fri, 5 Dec 2025 08:46:38 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Reduce timing overhead of EXPLAIN ANALYZE using rdtsc? To: Hannu Krosing , Robert Haas Cc: Lukas Fittl , Andres Freund , Pavel Stehule , vignesh C , Michael Paquier , Ibrar Ahmed , Maciek Sakrejda , pgsql-hackers References: <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> <57845c37-85e8-4d0e-9456-76833e089c5d@gmail.com> Content-Language: en-US From: David Geier In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 04.12.2025 22:21, Hannu Krosing wrote: > I have not looked at this patch series yet, but when I played around > with using rdtsc (or actually some gcc/clang construct that compiled > to rctsc on c86 and into time register reads on Arm and risc-v) then > any extra step around it had noticeable overhead. I am not sure > putting some if or function call around rdtsc call is a good idea. We have that already. INSTR_TIME_SET_CURRENT_FAST() is currently implemented as: static inline instr_time pg_get_ticks_fast(void) { #if defined(__x86_64__) && defined(__linux__) if (has_rdtsc) { instr_time now; now.ticks = __rdtsc(); return now; } #endif return pg_clock_gettime(); } Based on Robert's suggestion I wanted to add a "fast_clock_source" enum GUC which can have the following values "auto", "rdtsc", "try_rdtsc" and "off". With that, at least no additional checks are needed and performance will remain as previously benchmarked in this thread. Beyond that, the condition will always evaluate to the same result, so there won't be branch mispredictions. Doing completely without any check is impossible, except if we were to JIT compile InstrStartNode() and InstrStopNode(). But that's a much bigger project. I'll still add unlikely() around the if (has_rdtsc). Any input regarding the proposed GUC is welcome. -- David Geier