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 1w8CUj-000InA-00 for pgsql-hackers@arkaria.postgresql.org; Thu, 02 Apr 2026 07:29:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w8CUg-004MEt-1p for pgsql-hackers@arkaria.postgresql.org; Thu, 02 Apr 2026 07:29:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w8CUg-004MEk-0Y for pgsql-hackers@lists.postgresql.org; Thu, 02 Apr 2026 07:29:34 +0000 Received: from mail-pj1-x102a.google.com ([2607:f8b0:4864:20::102a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w8CUd-000000009ph-2sRJ for pgsql-hackers@lists.postgresql.org; Thu, 02 Apr 2026 07:29:34 +0000 Received: by mail-pj1-x102a.google.com with SMTP id 98e67ed59e1d1-35d9923eec5so351335a91.2 for ; Thu, 02 Apr 2026 00:29:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775114970; x=1775719770; darn=lists.postgresql.org; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=PCqpr8jLoondKZY+ht3E1K15U8O8m0tMWcnXAzMYaFA=; b=HaTELBIJrhLoZceTwXo0M3RGpo/4mGIE1s36YlR8fUz9OAO4DM3CkEWivL/2S/OpxE xHIHsDp7kLOf/V3rNxSrb6BaKtT3o6o48evEYj5AtJdmmk1uxr174u91nCr5ZhwiYBb/ m1nZz8rXouoYToCIKZhJ6ls/IHKlExDFsQQVwCPEb/j64mCsOHAF6RjFD0x7e2ywKnKT K+QMgSpbM/17HvcJzAcLM3WqhUfqMIPz5rn492zDWUCZOn+nYlyRsu+baNXpYR+Br5Wk BiQT1oSwxGZ+zoiMVq1NpPix+JsdJJMMBrXqpYQMXjmw40PVGPpIsJNZGBGegxsAPsPN 61Vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775114970; x=1775719770; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=PCqpr8jLoondKZY+ht3E1K15U8O8m0tMWcnXAzMYaFA=; b=IumHuEjlPI79oj/KEpzkN7/4AEyJnAulcZWqs3K8kfhUeuGPahMN4TAzEdNbuZc8ez 7wHcBTP6xyOxG6UAr/tj6buWF9UM2rg0fn9MvSXLkZWjBT3Fz39MdXN9wesDFulKJyOG MA6cGNPmzFlsTvKy/vQxH3ykKs0CAlnRZlrgJCaOmtJooeA95/OaoulwT7HPFn9jZzE+ xwuppomy3qZ/LsrWafBeMXh4QeaD9grPf8RyGdD31XHwcGn5LG/FiWsKIWUKUONKqWrf TJclaHPxjsu51TbSG/D7X4xfqLQsmyZh5d9ZqxGq1ZwIIMTxI1RK/DtIfsBHZy0S/d+Z kmwQ== X-Gm-Message-State: AOJu0Yzwsl9eS5O6brMf+2OVsqUHx5jXP+VzGK+6KfLR5zK5wfHG22id 4JB1BLATC104IrbskVURkDoqEw+AlJ5XI6yNRq961/dTiMnbSRgMvr69 X-Gm-Gg: AeBDieuTTO0JBI7+eo8fkyjD4MELfc0Gp2xmS3FZA2aaPVEt4dXV1FXqVKqYVq/atpC dteIKwSFVicX6GTpTkuIVLAXcHhSiVl+H56GQZRedfiTWpkQdPbMsNeOiXS298tNW+VJuSzrFOT vTQ0ngfbq+XDSq2sTNlVskLNGOXOxlpwj6Vt5FGEHo/BOZxjdW+N5fXl8IeRNvp1cN3FbsfKFVi 3AKxXs7elDbKTJxireuuYCMkd/+d1KTywpyMr76I9xSL93N/BFQJvYiNTRuxUkaGW+wpGF9K1Hz pBuL+VEJ2QP1vKr2A5oKioWw/vYuywF3ax6YeIJepbrWBggwrG7619M+VSI+X61F/MbALMLKxON pF2g8IAt3uzumNyOOkg7S3iNJKfDA0hK0uoHsdOpZ8B0/kvWVyI1iVmKHgs+kt6F2DcNSSiWp0G kyNseOTR5gyToMMwEJB71nHoPNvwNS2yA= X-Received: by 2002:a17:90b:2689:b0:35b:9896:cbd0 with SMTP id 98e67ed59e1d1-35dc6f423d3mr6843270a91.25.1775114969545; Thu, 02 Apr 2026 00:29:29 -0700 (PDT) Received: from smtpclient.apple ([45.32.121.103]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-35ddb973ffasm600109a91.2.2026.04.02.00.29.26 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 02 Apr 2026 00:29:28 -0700 (PDT) From: Chao Li Message-Id: Content-Type: multipart/mixed; boundary="Apple-Mail=_B5AC8848-AD3E-4DF6-8C69-26708696F102" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.400.21\)) Subject: Re: pg_test_timing: fix unit typo and widen diff type Date: Thu, 2 Apr 2026 15:28:48 +0800 In-Reply-To: Cc: PostgreSQL Hackers , Tom Lane , Hannu Krosing To: Lukas Fittl References: X-Mailer: Apple Mail (2.3864.400.21) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_B5AC8848-AD3E-4DF6-8C69-26708696F102 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Apr 2, 2026, at 12:17, Lukas Fittl wrote: >=20 > Hi Chao, >=20 > On Wed, Apr 1, 2026 at 8:10=E2=80=AFPM Chao Li = wrote: >>=20 >> Hi, >>=20 >> This morning, as part of my usual routine, I synced the master branch = and read through the recent commits. While reading 82c0cb4e672, I = noticed a mistake in an error message. The relevant code is like: >> ``` >> diff =3D INSTR_TIME_GET_NANOSEC(diff_time); >>=20 >> fprintf(stderr, _("Time warp: %d ms\n"), diff); >> ``` >>=20 >> Here, =E2=80=9Cdiff" is in nanoseconds, but the error message prints = ms as the unit, which is incorrect. >=20 > Good catch! >=20 > It looks like the use of nanoseconds for "diff" got introduced last > year in 0b096e379e6f9bd49 (as you note later in the email, today's > commit didn't actually change that part), CCing Tom and Hannu as > authors of that earlier change. >=20 > That said, its a bit odd that we were using INSTR_TIME_GET_MICROSEC > there before that earlier commit, but called it "ms" (i.e. > milliseconds) in the error message. >=20 >>=20 >> To fix that, I think there are two possible options: >>=20 >> 1. Use INSTR_TIME_GET_MILLISEC to get =E2=80=9Cdiff" >> 2. Change =E2=80=9Cms" to =E2=80=9Cns" in the error message >>=20 >> After reading through the whole file, I think option 2 is the right = fix. While doing that, I also noticed another issue. >>=20 >> =E2=80=9Cdiff" is currently defined as int32. Although one might = think that is enough for a time delta, I believe it should be int64 for = two reasons: >>=20 >> * INSTR_TIME_GET_NANOSEC() explicitly returns int64: >> ``` >> #define INSTR_TIME_GET_NANOSEC(t) \ >> ((int64) (t).ticks) >> ``` >>=20 >> * The current code has a sanity check for backward clock drift: >> ``` >> /* Did time go backwards? */ >> if (unlikely(diff < 0)) >> { >> fprintf(stderr, _("Detected clock going backwards in = time.\n")); >> fprintf(stderr, _("Time warp: %d ms\n"), diff); >> exit(1); >> } >> ``` >> Clock jumping forward is also possible, and a forward jump of about = 2.14 seconds would overflow int32 when expressed in nanoseconds, making = the value appear negative. In that case, the code could report a = =E2=80=9Cbackwards=E2=80=9D clock jump when the actual jump was = forwards, which would be misleading. >=20 > I agree it doesn't seem sound to use an int32 for storing the result > of INSTR_TIME_GET_NANOSEC. It looks like we may also need to adjust > the call to pg_leftmost_one_pos32 though if we actually accept that > large a "diff" value, as in your patch. You are right. Changed to pg_leftmost_one_pos64 in v2. >=20 > Maybe we should error out if the diff is larger than an int32, noting > a positive time drift? I agree we should warn/fail upon clock forwards drift. But I doubt int32 = is too big (~2.14 seconds), I consider 1 second could be a too big = threshold. Let=E2=80=99s wait for more voices on this. >=20 > Independently of that, its worth noting we could easily emit the diff > in a larger unit (micro or milliseconds) for easier interpretation, by > just calling INSTR_TIME_GET_MICROSEC / INSTR_TIME_GET_MILLISEC on the > "diff_time" again. >=20 Given the error should rarely happen, I personally feel that might not = be super helpful. Also, if the drift is just beyond the threshold, = bumping to microsecond or millisecond might print just 0. PFA v2 - updated 0002 for pg_leftmost_one_pos64. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/ --Apple-Mail=_B5AC8848-AD3E-4DF6-8C69-26708696F102 Content-Disposition: attachment; filename=v2-0001-pg_test_timing-fix-unit-in-backward-clock-warning.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="v2-0001-pg_test_timing-fix-unit-in-backward-clock-warning.patch" Content-Transfer-Encoding: quoted-printable =46rom=205bb978b940a93c8f902aa4ea25ae39ef52eca3e3=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20"Chao=20Li=20(Evan)"=20=0A= Date:=20Thu,=202=20Apr=202026=2010:21:40=20+0800=0ASubject:=20[PATCH=20= v2=201/2]=20pg_test_timing:=20fix=20unit=20in=20backward-clock=20warning=0A= =0Apg_test_timing=20measures=20timing=20differences=20in=20nanoseconds=20= via=0AINSTR_TIME_GET_NANOSEC(),=20but=20the=20backward-clock=20warning=20= incorrectly=0Areported=20the=20value=20as=20milliseconds.=20Fix=20the=20= message=20to=20use=20"ns"=20so=0Athat=20it=20matches=20the=20actual=20= unit=20of=20diff.=0A=0AAuthor:=20Chao=20Li=20=0A= Reviewed-by:=20Lukas=20Fittl=20=0ADiscussion:=20= https://postgr.es/m/F780CEEB-A237-4302-9F55-60E9D8B6533D@gmail.com=0A---=0A= =20src/bin/pg_test_timing/pg_test_timing.c=20|=202=20+-=0A=201=20file=20= changed,=201=20insertion(+),=201=20deletion(-)=0A=0Adiff=20--git=20= a/src/bin/pg_test_timing/pg_test_timing.c=20= b/src/bin/pg_test_timing/pg_test_timing.c=0Aindex=20= aee41dbe3f9..cbdc5af09d9=20100644=0A---=20= a/src/bin/pg_test_timing/pg_test_timing.c=0A+++=20= b/src/bin/pg_test_timing/pg_test_timing.c=0A@@=20-191,7=20+191,7=20@@=20= test_timing(unsigned=20int=20duration)=0A=20=09=09if=20(unlikely(diff=20= <=200))=0A=20=09=09{=0A=20=09=09=09fprintf(stderr,=20_("Detected=20clock=20= going=20backwards=20in=20time.\n"));=0A-=09=09=09fprintf(stderr,=20= _("Time=20warp:=20%d=20ms\n"),=20diff);=0A+=09=09=09fprintf(stderr,=20= _("Time=20warp:=20%d=20ns\n"),=20diff);=0A=20=09=09=09exit(1);=0A=20=09=09= }=0A=20=0A--=20=0A2.50.1=20(Apple=20Git-155)=0A=0A= --Apple-Mail=_B5AC8848-AD3E-4DF6-8C69-26708696F102 Content-Disposition: attachment; filename=v2-0002-pg_test_timing-use-int64-for-largest-observed-tim.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="v2-0002-pg_test_timing-use-int64-for-largest-observed-tim.patch" Content-Transfer-Encoding: quoted-printable =46rom=20d17974f31f8b11e240ffadaf0f134b4e82b7e2de=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20"Chao=20Li=20(Evan)"=20=0A= Date:=20Thu,=202=20Apr=202026=2010:29:06=20+0800=0ASubject:=20[PATCH=20= v2=202/2]=20pg_test_timing:=20use=20int64=20for=20largest=20observed=20= time=0A=20delta=0A=0Apg_test_timing=20obtains=20timing=20deltas=20with=20= INSTR_TIME_GET_NANOSEC(),=0Awhich=20returns=20a=20nanosecond=20value=20= that=20should=20be=20stored=20in=20int64.=20Adjust=0Adiff=20and=20= largest_diff=20to=20use=20int64=20accordingly.=0A=0AAlso=20fix=20the=20= corresponding=20printf=20format=20specifiers=20so=20these=20values=20are=0A= printed=20correctly.=0A=0AAuthor:=20Chao=20Li=20=0A= Reviewed-by:=20Lukas=20Fittl=20=0ADiscussion:=20= https://postgr.es/m/F780CEEB-A237-4302-9F55-60E9D8B6533D@gmail.com=0A---=0A= =20src/bin/pg_test_timing/pg_test_timing.c=20|=2012=20++++++------=0A=20= 1=20file=20changed,=206=20insertions(+),=206=20deletions(-)=0A=0Adiff=20= --git=20a/src/bin/pg_test_timing/pg_test_timing.c=20= b/src/bin/pg_test_timing/pg_test_timing.c=0Aindex=20= cbdc5af09d9..323d98bbb61=20100644=0A---=20= a/src/bin/pg_test_timing/pg_test_timing.c=0A+++=20= b/src/bin/pg_test_timing/pg_test_timing.c=0A@@=20-25,7=20+25,7=20@@=20= static=20long=20long=20int=20histogram[32];=0A=20static=20long=20long=20= int=20direct_histogram[NUM_DIRECT];=0A=20=0A=20/*=20separately=20record=20= highest=20observed=20duration=20*/=0A-static=20int32=20largest_diff;=0A= +static=20int64=20largest_diff;=0A=20static=20long=20long=20int=20= largest_diff_count;=0A=20=0A=20=0A@@=20-176,8=20+176,8=20@@=20= test_timing(unsigned=20int=20duration)=0A=20=0A=20=09while=20= (INSTR_TIME_GT(end_time,=20cur))=0A=20=09{=0A-=09=09int32=09=09diff,=0A-=09= =09=09=09=09bits;=0A+=09=09int64=09=09diff;=0A+=09=09int32=09=09bits;=0A=20= =09=09instr_time=09diff_time;=0A=20=0A=20=09=09prev=20=3D=20cur;=0A@@=20= -191,13=20+191,13=20@@=20test_timing(unsigned=20int=20duration)=0A=20=09=09= if=20(unlikely(diff=20<=200))=0A=20=09=09{=0A=20=09=09=09fprintf(stderr,=20= _("Detected=20clock=20going=20backwards=20in=20time.\n"));=0A-=09=09=09= fprintf(stderr,=20_("Time=20warp:=20%d=20ns\n"),=20diff);=0A+=09=09=09= fprintf(stderr,=20_("Time=20warp:=20%lld=20ns\n"),=20diff);=0A=20=09=09=09= exit(1);=0A=20=09=09}=0A=20=0A=20=09=09/*=20What=20is=20the=20highest=20= bit=20in=20the=20time=20diff?=20*/=0A=20=09=09if=20(diff=20>=200)=0A-=09=09= =09bits=20=3D=20pg_leftmost_one_pos32(diff)=20+=201;=0A+=09=09=09bits=20= =3D=20pg_leftmost_one_pos64(diff)=20+=201;=0A=20=09=09else=0A=20=09=09=09= bits=20=3D=200;=0A=20=0A@@=20-319,7=20+319,7=20@@=20output(uint64=20= loop_count)=0A=20=09=09double=09=09prct=20=3D=20(double)=20= largest_diff_count=20*=20100=20/=20loop_count;=0A=20=0A=20=09=09= printf("...\n");=0A-=09=09printf("%*d=20=20=20%*.4f=20%*.4f=20%*lld\n",=0A= +=09=09printf("%*lld=20=20=20%*.4f=20%*.4f=20%*lld\n",=0A=20=09=09=09=20=20= =20len1,=20largest_diff,=0A=20=09=09=09=20=20=20len2,=20prct,=0A=20=09=09= =09=20=20=20len3,=20100.0,=0A--=20=0A2.50.1=20(Apple=20Git-155)=0A=0A= --Apple-Mail=_B5AC8848-AD3E-4DF6-8C69-26708696F102--