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 1sbo7G-001ZEA-5I for pgsql-hackers@arkaria.postgresql.org; Wed, 07 Aug 2024 21:22:42 +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 1sbo7C-00AGe2-TC for pgsql-hackers@arkaria.postgresql.org; Wed, 07 Aug 2024 21:22:38 +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 1sbo7C-00AGds-EB for pgsql-hackers@lists.postgresql.org; Wed, 07 Aug 2024 21:22:38 +0000 Received: from mail-qv1-xf2f.google.com ([2607:f8b0:4864:20::f2f]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sbo79-003bSi-T7 for pgsql-hackers@postgresql.org; Wed, 07 Aug 2024 21:22:37 +0000 Received: by mail-qv1-xf2f.google.com with SMTP id 6a1803df08f44-6b79fc76d03so2038756d6.1 for ; Wed, 07 Aug 2024 14:22:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723065755; x=1723670555; darn=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=juh0t20xqA0dx+9vaUi7+9R0IK6cGTIi40xnWIpwcUo=; b=fTkN0+U5iSLQv8y/lL9OKVy8w4PyThTV9fi/lJ6h3eJlm/BvU+UarU5E7FyMrWWd3G Uk8zVW8Uc39+0HD3LlQOQZayQ1u+OMhpqNHFZYSWCUBFPY+wOR0KgC2LFoSOxM3IxRTL h+2YM0PhhrSUJ/diJjEiUmlbs6Rh1NWR2Tzq7QvppwgOfcIRiNYI49XGqIaK+DNG1FSN qkKN/6oYWXeSKPCrfUwEZblrzgC/DAU1wg/Dc5Ti6D82m5Ur1CQXrYwF45KyYz47vvTZ t5R5OWM3oA6JzMRFuulZAHcz9VcZYe+JM2zgGy2rokbNieVHNAZ0fwHnrl7T1R67Ex8q 3Kyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723065755; x=1723670555; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=juh0t20xqA0dx+9vaUi7+9R0IK6cGTIi40xnWIpwcUo=; b=GnH5d3QvpsYHuN/RU22USahgmfk1QmKOg5n1bhEfI0UOj5WngLTWh2FIsNwFQ9fcGS Gn32jpY1ZFeBbWYjHOVtOiCFWFOI+K8z/9fH/4Vk1ZNBpFZ+bphXvh4Yfok3MFRra6Rt vZ+L7+ZhACwIWHO02+v3Zo4Q3wTbZYtCW4yb3jZZ/BiCVZBur6LDT/N2wDaNKxdO1qcJ oodSVl2RtfDso6ZlFrIrouPJG8LLXLyOHnW2XHqFYWpWbQ53T7JjJoKVe33UbnVHm64Y /OVqXhBHyFmFXKtyykp4hrQytbP4ccfdXhi1QJ9WL1ZmxCPhLS72oXCx+eLB6gxM6apv UYEg== X-Forwarded-Encrypted: i=1; AJvYcCVq4K46/8/9yq66AmhHzAA/CtJdpQjj27VGpnLrLxTb0pHNTMspToPqUgXoy4R0WiR85w83C782F9jAKk9i9EUwnnsW4W8NLQUx9saC X-Gm-Message-State: AOJu0YzOtqrNqCXJPphVfwF8ZkkuIQoXNNugkUzrYuoKiKHFbL/opEoa QMzNDYdxeu71m1P13G+kF4HFxXxMmSzYNOPoizYHQaCe7XKn4Zoz X-Google-Smtp-Source: AGHT+IFy+Spg0W6zZHIFFUYSIhiAwv1r/FxFqyGtLJtlcYXHsuWqkw4fE67jO0rHZgl5Tx7b97qh3A== X-Received: by 2002:a05:6214:5d8c:b0:6b5:e377:38a with SMTP id 6a1803df08f44-6bb98417fb7mr209969556d6.39.1723065754786; Wed, 07 Aug 2024 14:22:34 -0700 (PDT) Received: from smtpclient.apple ([15.248.7.181]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6bb9c83bcb0sm59176756d6.103.2024.08.07.14.22.33 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 07 Aug 2024 14:22:33 -0700 (PDT) From: Sami Imseih Message-Id: Content-Type: multipart/mixed; boundary="Apple-Mail=_D0E35155-2E87-4DE0-8625-806371F447C6" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3774.600.62\)) Subject: Re: Restart pg_usleep when interrupted Date: Wed, 7 Aug 2024 16:22:22 -0500 In-Reply-To: Cc: Bertrand Drouvot , Tom Lane , Robert Haas , "pgsql-hackers@postgresql.org" To: Nathan Bossart References: <33287B8A-3CA0-494F-87D3-C9D85C6468FE@gmail.com> <19E276C9-2C2B-435A-B275-8FA22222AEB8@gmail.com> <50E1D6AD-3E51-4E45-95A7-7B20DC037DCB@gmail.com> X-Mailer: Apple Mail (2.3774.600.62) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_D0E35155-2E87-4DE0-8625-806371F447C6 Content-Disposition: attachment; filename=v7-0001-vaccum_delay-with-absolute-time-nanosleep.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="v7-0001-vaccum_delay-with-absolute-time-nanosleep.patch" Content-Transfer-Encoding: quoted-printable =46rom=2083ef3f489e39f031d70f779582cdc276d0d415d1=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20Sami=20Imseih=20=0ADate:=20= Wed,=207=20Aug=202024=2010:09:00=20-0500=0ASubject:=20[PATCH=20v7=201/1]=20= vaccum_delay=20with=20absolute=20time=20nanosleep=0A=0A---=0A=20= src/backend/commands/vacuum.c=20=20=20=20=20=20=20=20|=2057=20= +++++++++++++++++++++++++++-=0A=20src/include/portability/instr_time.h=20= |=2010=20+++++=0A=202=20files=20changed,=2066=20insertions(+),=201=20= deletion(-)=0A=0Adiff=20--git=20a/src/backend/commands/vacuum.c=20= b/src/backend/commands/vacuum.c=0Aindex=2048f8eab202..4edffd3bc5=20= 100644=0A---=20a/src/backend/commands/vacuum.c=0A+++=20= b/src/backend/commands/vacuum.c=0A@@=20-116,6=20+116,61=20@@=20static=20= bool=20vacuum_rel(Oid=20relid,=20RangeVar=20*relation,=20VacuumParams=20= *params,=0A=20static=20double=20compute_parallel_delay(void);=0A=20= static=20VacOptValue=20get_vacoptval_from_boolean(DefElem=20*def);=0A=20= static=20bool=20vac_tid_reaped(ItemPointer=20itemptr,=20void=20*state);=0A= +static=20void=20vacuum_sleep(double=20msec);=0A+=0A+/*=0A+=20*=20Delay=20= vacuum=20for=20a=20specified=20time.=0A+=20*=0A+=20*=20Unlike=20= pg_usleep,=20This=20function=20continues=20the=20delay=20in=20case=20of=20= an=0A+=20*=20interrupt.=0A+=20*/=0A+static=20void=0A+vacuum_sleep(double=20= msec)=0A+{=0A+=09long=09=09microsec=20=3D=20msec=20*=201000;=0A+=0A+=09= if=20(microsec=20>=200)=0A+=09{=0A+#ifndef=20WIN32=0A+=09=09/*=0A+=09=09=20= *=20We=20allow=20nanosleep=20to=20handle=20interrupts=20and=20retry=20= with=20the=0A+=09=09=20*=20remaining=20time.=20However,=20frequent=20= interruptions=20and=20restarts=20of=20the=0A+=09=09=20*=20nanosleep=20= calls=20can=20substantially=20lead=20to=20drift=20in=20the=20time=20when=0A= +=09=09=20*=20the=20sleep=20finally=20completes.=20To=20deal=20with=20= this,=20we=20break=20out=20of=20the=0A+=09=09=20*=20loop=20whenever=20= the=20current=20time=20is=20past=20the=20expected=20end=20time=20of=20= the=0A+=09=09=20*=20sleep.=0A+=09=09=20*/=0A+=09=09struct=20timespec=20= delay;=0A+=09=09struct=20timespec=20remain;=0A+=09=09instr_time=09= end_time;=0A+=0A+=09=09INSTR_TIME_SET_CURRENT(end_time);=0A+=09=09= INSTR_TIME_ADD_MICROSEC(end_time,=20microsec);=0A+=0A+=09=09delay.tv_sec=20= =3D=20microsec=20/=201000000L;=0A+=09=09delay.tv_nsec=20=3D=20(microsec=20= %=201000000L)=20*=201000;=0A+=0A+=09=09while=20(nanosleep(&delay,=20= &remain)=20=3D=3D=20-1=20&&=20errno=20=3D=3D=20EINTR)=0A+=09=09{=0A+=09=09= =09instr_time=09current_time;=0A+=0A+=09=09=09= INSTR_TIME_SET_CURRENT(current_time);=0A+=0A+=09=09=09if=20= (INSTR_TIME_IS_GREATER(current_time,=20end_time))=0A+=09=09=09=09break;=0A= +=0A+=09=09=09delay=20=3D=20remain;=0A+=09=09}=0A+#else=0A+=09=09/*=0A+=09= =09=20*=20Win32=20uses=20SleepEx=20to=20perform=20the=20delay.=20It=20is=20= non-interruptable,=0A+=09=09=20*=20therefore=20no=20special=20handling=20= is=20required=20to=20restart=20the=20sleep=20with=0A+=09=09=20*=20= remaining=20time.=0A+=09=09=20*/=0A+=09=09SleepEx((microsec=20<=20500=20= ?=201=20:=20(microsec=20+=20500)=20/=201000),=20FALSE);=0A+#endif=0A+=09= }=0A+}=0A=20=0A=20/*=0A=20=20*=20GUC=20check=20function=20to=20ensure=20= GUC=20value=20specified=20is=20within=20the=20allowable=0A@@=20-2384,7=20= +2439,7=20@@=20vacuum_delay_point(void)=0A=20=09=09=09msec=20=3D=20= vacuum_cost_delay=20*=204;=0A=20=0A=20=09=09= pgstat_report_wait_start(WAIT_EVENT_VACUUM_DELAY);=0A-=09=09= pg_usleep(msec=20*=201000);=0A+=09=09vacuum_sleep(msec);=0A=20=09=09= pgstat_report_wait_end();=0A=20=0A=20=09=09/*=0Adiff=20--git=20= a/src/include/portability/instr_time.h=20= b/src/include/portability/instr_time.h=0Aindex=20e66ecf34cd..6e4b0f1b17=20= 100644=0A---=20a/src/include/portability/instr_time.h=0A+++=20= b/src/include/portability/instr_time.h=0A@@=20-36,6=20+36,10=20@@=0A=20=20= *=0A=20=20*=20INSTR_TIME_GET_NANOSEC(t)=09=09convert=20t=20to=20int64=20= (in=20nanoseconds)=0A=20=20*=0A+=20*=20INSTR_TIME_ADD_MICROSEC(x,t)=09=09= add=20t=20(in=20microseconds)=20to=20x=0A+=20*=0A+=20*=20= INSTR_TIME_IS_GREATER(x,y)=09=09is=20x=20greater=20than=20y?=0A+=20*=0A=20= =20*=20Note=20that=20INSTR_TIME_SUBTRACT=20and=20INSTR_TIME_ACCUM_DIFF=20= convert=0A=20=20*=20absolute=20times=20to=20intervals.=20=20The=20= INSTR_TIME_GET_xxx=20operations=20are=0A=20=20*=20only=20useful=20on=20= intervals.=0A@@=20-194,4=20+198,10=20@@=20GetTimerFrequency(void)=0A=20= #define=20INSTR_TIME_GET_MICROSEC(t)=20\=0A=20=09= (INSTR_TIME_GET_NANOSEC(t)=20/=20NS_PER_US)=0A=20=0A+#define=20= INSTR_TIME_ADD_MICROSEC(x,t)=20\=0A+=09((x).ticks=20+=3D=20(t)=20*=20= NS_PER_US)=0A+=0A+#define=20INSTR_TIME_IS_GREATER(x,y)=20\=0A+=09= ((x).ticks=20>=20(y).ticks)=0A+=0A=20#endif=09=09=09=09=09=09=09/*=20= INSTR_TIME_H=20*/=0A--=20=0A2.39.3=20(Apple=20Git-146)=0A=0A= --Apple-Mail=_D0E35155-2E87-4DE0-8625-806371F447C6 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii > > On Wed, Aug 07, 2024 at 06:00:53AM +0000, Bertrand Drouvot wrote: >> + SleepEx((microsec < 500 ? 1 : (microsec + 500) / 1000), FALSE); > > I think this deserves a comment. > Done >> +#define INSTR_TIME_ADD_MICROSEC(x,t) \ >> + ((x).ticks += t * NS_PER_US) > > I'd add parentheses around "t" to ensure any expressions given as "t" are > evaluated first. > Done > Also, do we need to worry about overflow here? It looks like the rest of > instr_time.h is oblivious about overflow, so maybe this is better discussed > in a separate thread... > I agree, this needs to be handled in a different thread. Please see v7. Regards, Sami --Apple-Mail=_D0E35155-2E87-4DE0-8625-806371F447C6--