Subject: Can we set time exception?

From: Shaozhong SHI <[email-masked]>
Date: Thu, 13 Jul 2023 15:46:12 +0100

In a Do statement, it calls function to work?

Can we set a time exception?

If the function responds with result in a given time, we take the result.

If the response of the function exceeds the time set, skip it.

Regards,

David

---

From: "Voillequin, Jean-Marc" <[email-masked]>
Date: Thu, 13 Jul 2023 15:11:15 +0000

create or replace function shi() returns text as $$
declare
ts timestamp:=clock_timestamp();
begin
               perform pg_sleep(2);
               if extract ('epoch' from age(clock_timestamp(),ts))>2 then
                              return 'timeout';
               else
                              return 'ok';
               end if;
end;$$ language plpgsql;

From: Shaozhong SHI <[email-masked]>
Sent: Thursday, July 13, 2023 4:46 PM
To: pgsql-sql <[email-masked]>
Subject: Can we set time exception?

In a Do statement, it calls function to work? Can we set a time exception? If the function responds with result in a given time, we take the result. If the response of the function exceeds the time set, skip it. Regards, David 
ZjQcmQRYFpfptBannerStart
This email originated from outside of Moody's
Do not click links or open attachments unless you recognize the sender and know the content is safe.
ZjQcmQRYFpfptBannerEnd
In a Do statement, it calls function to work?

Regards,

David

----------------------------------------------------------------------
Moody's monitors email communications through its networks for regulatory compliance purposes and to protect its customers, employees and business and where allowed to do so by applicable law. The information contained in this e-mail message, and any attachment thereto, is confidential and may not be disclosed without our express permission. If you are not the intended recipient or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution or copying of this message, or any attachment thereto, in whole or in part, is strictly prohibited. If you have received this message in error, please immediately notify us by telephone, fax or e-mail and delete the message and all of its attachments. Every effort is made to keep our network free from viruses. You should, however, review this e-mail message, as well as any attachment thereto, for viruses. We take no responsibility and have no liability for any computer virus which may be transferred via this e-mail message.

---

From: "David G. Johnston" <[email-masked]>
Date: Thu, 13 Jul 2023 08:36:48 -0700

> If the response of the function exceeds the time set, skip it.
No.  Function execution happens on a single thread (i.e., synchronous) in a
stacked manner.  There is no way for the caller to get processing control
back other than the callee returning.

If your choice of procedural language can overcome the
synchronous execution limitation then sure.  SQL and pl/pgsql cannot.

David J.

---

From: Tom Lane <[email-masked]>
Date: Thu, 13 Jul 2023 11:52:32 -0400

> back other than the callee returning.

You could do something with statement_timeout, but the recovery would
have to be managed on the client (application) side.  IIRC we measure
that as the total time to finish a client query, not the time of any
one statement within a function or DO block.

			regards, tom lane