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 1mI7sY-0003g7-P7 for pgsql-committers@arkaria.postgresql.org; Mon, 23 Aug 2021 11:12:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mI7sX-0000Q9-Ex for pgsql-committers@arkaria.postgresql.org; Mon, 23 Aug 2021 11:12:33 +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 1mI7sX-0000Q1-8B for pgsql-committers@lists.postgresql.org; Mon, 23 Aug 2021 11:12:33 +0000 Received: from smtp.outgoing.loopia.se ([93.188.3.37]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mI7sU-0001b2-LN for pgsql-committers@lists.postgresql.org; Mon, 23 Aug 2021 11:12:32 +0000 Received: from s807.loopia.se (localhost [127.0.0.1]) by s807.loopia.se (Postfix) with ESMTP id AEB802E9BD6C for ; Mon, 23 Aug 2021 13:12:29 +0200 (CEST) Received: from s645.loopia.se (unknown [172.22.191.5]) by s807.loopia.se (Postfix) with ESMTP id 9EFE92E282A7; Mon, 23 Aug 2021 13:12:29 +0200 (CEST) Received: from s474.loopia.se (unknown [172.22.191.6]) by s645.loopia.se (Postfix) with ESMTP id 8F6A2157A0B0; Mon, 23 Aug 2021 13:12:29 +0200 (CEST) X-Virus-Scanned: amavisd-new at amavis.loopia.se X-Spam-Flag: NO X-Spam-Score: -1 X-Spam-Level: X-Spam-Status: No, score=-1 tagged_above=-999 required=6.2 tests=[ALL_TRUSTED=-1] autolearn=disabled Received: from s899.loopia.se ([172.22.191.6]) by s474.loopia.se (s474.loopia.se [172.22.190.14]) (amavisd-new, port 10024) with LMTP id HprJMkWjqi_V; Mon, 23 Aug 2021 13:12:29 +0200 (CEST) X-Loopia-Auth: user X-Loopia-User: daniel@yesql.se X-Loopia-Originating-IP: 89.255.232.193 Received: from [192.168.72.144] (customer-89-255-232-193.stosn.net [89.255.232.193]) (Authenticated sender: daniel@yesql.se) by s899.loopia.se (Postfix) with ESMTPSA id 1F5AB2C8BA45; Mon, 23 Aug 2021 13:12:29 +0200 (CEST) From: Daniel Gustafsson Message-Id: Content-Type: multipart/mixed; boundary="Apple-Mail=_C9271181-2EC9-4545-A52E-E3304D11D401" Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.7\)) Subject: Re: pgsql: psql: Add test for query canceling Date: Mon, 23 Aug 2021 13:12:28 +0200 In-Reply-To: <1983764.1629499242@sss.pgh.pa.us> Cc: Fabien COELHO , Peter Eisentraut , pgsql-committers@lists.postgresql.org To: Tom Lane References: <5F16EBAA-30D3-4C0D-840C-C2FF2739E370@yesql.se> <1957503.1629485278@sss.pgh.pa.us> <1983764.1629499242@sss.pgh.pa.us> X-Mailer: Apple Mail (2.3608.120.23.2.7) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_C9271181-2EC9-4545-A52E-E3304D11D401 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On 21 Aug 2021, at 00:40, Tom Lane wrote: >=20 > Daniel Gustafsson writes: >> On 20 Aug 2021, at 20:47, Tom Lane wrote: >>> I think you should drop the overly-cute bit with a SIGALRM handler, >>> and instead have a loop-with-delay around an attempt to read the >>> psql.pid file, after launching the psql run without an immediate >>> wait for termination. >=20 >> This could perhaps be done with a PostgresNode::interactive_psql >> session? >=20 > Yeah, we do have that infrastructure available in the > 010_tab_completion.pl test. In order to satisfy my own curiosity I decided to take a stab at this = which turned into the attached. I wasn=E2=80=99t able to reliably know that = the pg_sleep query had started except letting the timer expire, but that may very = well be the rust on my Perl skills showing. Not sure if this moves the needle on this in either direction, but I = figured I should report back progress either way. -- Daniel Gustafsson https://vmware.com/ --Apple-Mail=_C9271181-2EC9-4545-A52E-E3304D11D401 Content-Disposition: attachment; filename=psql_query_cancel_test.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="psql_query_cancel_test.patch" Content-Transfer-Encoding: quoted-printable =46rom=2098753c29da00612ccbefd6d47c23adde33cc91fd=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20Daniel=20Gustafsson=20=0A= Date:=20Mon,=2023=20Aug=202021=2013:04:32=20+0200=0ASubject:=20[PATCH]=20= Refactor=20psql=20query=20cancellation=20test=20to=20use=20= interactive_psql=0A=0AUtilize=20the=20interactive_psql=20client=20to=20= inject=20query=20cancellation.=0A=0ADiscussion:=20= https://www.postgresql.org/message-id/flat/E1mH14Q-0002gh-HS%40gemulon.pos= tgresql.org=0A---=0A=20src/bin/psql/t/020_cancel.pl=20|=2066=20= ++++++++++++++++++++----------------=0A=201=20file=20changed,=2036=20= insertions(+),=2030=20deletions(-)=0A=0Adiff=20--git=20= a/src/bin/psql/t/020_cancel.pl=20b/src/bin/psql/t/020_cancel.pl=0Aindex=20= 7713fff8e4..ed654869d4=20100644=0A---=20a/src/bin/psql/t/020_cancel.pl=0A= +++=20b/src/bin/psql/t/020_cancel.pl=0A@@=20-6,42=20+6,48=20@@=20use=20= warnings;=0A=20=0A=20use=20PostgresNode;=0A=20use=20TestLib;=0A-use=20= Test::More=20tests=20=3D>=202;=0A+use=20IPC::Run=20qw(pump=20finish=20= timer);=0A+use=20Test::More=20tests=20=3D>=204;=0A=20=0A-my=20$tempdir=20= =3D=20TestLib::tempdir;=0A+plan=20skip_all=20=3D>=20"Testsuite=20= requires=20a=20Unix=20shell"=20if=20($windows_os);=0A=20=0A=20my=20$node=20= =3D=20PostgresNode->new('main');=0A=20$node->init;=0A=20$node->start;=0A=20= =0A=20#=20Test=20query=20canceling=20by=20sending=20SIGINT=20to=20a=20= running=20psql=0A-#=0A+my=20$in;=0A+my=20$out;=0A+my=20$timer=20=3D=20= timer(5);=0A+my=20$h=20=3D=20$node->interactive_psql('postgres',=20\$in,=20= \$out,=20$timer);=0A+=0A=20#=20There=20is,=20as=20of=20this=20writing,=20= no=20documented=20way=20to=20get=20the=20PID=20of=0A=20#=20the=20process=20= from=20IPC::Run.=20=20As=20a=20workaround,=20we=20have=20psql=20print=20= its=0A-#=20own=20PID=20(which=20is=20the=20parent=20of=20the=20shell=20= launched=20by=20psql)=20to=20a=0A-#=20file.=0A-SKIP:=20{=0A-=09skip=20= "cancel=20test=20requires=20a=20Unix=20shell",=202=20if=20$windows_os;=0A= -=0A-=09local=20%ENV=20=3D=20$node->_get_env();=0A-=0A-=09my=20($stdin,=20= $stdout,=20$stderr);=0A-=0A-=09#=20Test=20whether=20shell=20supports=20= $PPID.=20=20It's=20part=20of=20POSIX,=20but=20some=0A-=09#=20= pre-/non-POSIX=20shells=20don't=20support=20it=20(e.g.,=20NetBSD,=20= Solaris).=0A-=09$stdin=20=3D=20"\\!=20echo=20\$PPID";=0A-=09= IPC::Run::run(['psql',=20'-X',=20'-v',=20'ON_ERROR_STOP=3D1'],=20'<',=20= \$stdin,=20'>',=20\$stdout,=20'2>',=20\$stderr);=0A-=09$stdout=20=3D~=20= /^\d+$/=20or=20skip=20"shell=20apparently=20does=20not=20support=20= \$PPID",=202;=0A-=0A-=09local=20$SIG{ALRM}=20=3D=20sub=20{=0A-=09=09my=20= $psql_pid=20=3D=20TestLib::slurp_file("$tempdir/psql.pid");=0A-=09=09= kill=20'INT',=20$psql_pid;=0A-=09};=0A-=09alarm=201;=0A-=0A-=09$stdin=20= =3D=20"\\!=20echo=20\$PPID=20>$tempdir/psql.pid\nselect=20pg_sleep(3);";=0A= -=09my=20$result=20=3D=20IPC::Run::run(['psql',=20'-X',=20'-v',=20= 'ON_ERROR_STOP=3D1'],=20'<',=20\$stdin,=20'>',=20\$stdout,=20'2>',=20= \$stderr);=0A-=0A-=09ok(!$result,=20'query=20failed=20as=20expected');=0A= -=09like($stderr,=20qr/canceling=20statement=20due=20to=20user=20= request/,=20'query=20was=20canceled');=0A-}=0A+#=20own=20PID=20(which=20= is=20the=20parent=20of=20the=20shell=20launched=20by=20psql)=0A+$out=20=3D= =20'';=0A+$timer->start(5);=0A+$in=20=3D=20"\\!=20echo=20= \"\$PPID\"\"x\"\n";=0A+pump=20$h=20until=20($out=20=3D~=20/[^\D]+x/=20||=20= $timer->is_expired);=0A+ok(!$timer->is_expired,=20"PPID=20timer=20= expired");=0A+=0A+#=20Grab=20PID=20from=20the=20output=20and=20clear=20= the=20output=20buffer.=20If=20no=20PID=20was=20found=0A+#=20then=20skip=20= the=20rest=20of=20the=20tests=20as=20the=20platform=20doesn't=20support=20= PPID.=0A+(my=20$psql_pid=20=3D=20$out)=20=3D~=20s/[^\d]//g;=0A+$out=20=3D=20= '';=0A+scalar($psql_pid=20>=200)=20or=20skip=20"shell=20apparently=20= does=20not=20support=20\$PPID",=203;=0A+=0A+#=20Put=20the=20interactive=20= client=20to=20sleep.=20Pumping=20on=20the=20output=20is=20tricky=20so=20= for=0A+#=20now=20allow=20the=20timer=20to=20expire=20and=20assume=20we=20= are=20at=20sleep=20by=20then.=0A+$timer->start(2);=0A+$in=20.=3D=20= "SELECT=20pg_sleep(15);\n";=0A+pump=20$h=20until=20$timer->is_expired;=0A= +=0A+#=20Signal=20the=20client=20and=20await=20the=20expected=20output=0A= +$timer->start(10);=0A+my=20$killed=20=3D=20kill=20'INT',=20$psql_pid;=0A= +ok($killed=20eq=20'1',=20"psql=20was=20signaled");=0A+pump=20$h=20until=20= ($out=20=3D~=20/request/=20||=20$timer->is_expired);=0A+like($out,=20= qr/canceling=20statement=20due=20to=20user=20request/,=20'query=20was=20= canceled');=0A+ok(!$timer->is_expired,=20"Sleep=20query=20timer=20= expired");=0A+=0A+$node->stop;=0A--=20=0A2.24.3=20(Apple=20Git-128)=0A=0A= --Apple-Mail=_C9271181-2EC9-4545-A52E-E3304D11D401--