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 1sPylx-007Eib-R7 for pgsql-general@arkaria.postgresql.org; Sat, 06 Jul 2024 06:19:49 +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 1sPylu-000rEX-6D for pgsql-general@arkaria.postgresql.org; Sat, 06 Jul 2024 06:19:46 +0000 Received: from makus.postgresql.org ([72.32.157.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 1sPylt-000rDU-MY for pgsql-general@lists.postgresql.org; Sat, 06 Jul 2024 06:19:46 +0000 Received: from mail-yw1-x1130.google.com ([2607:f8b0:4864:20::1130]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sPylr-000eVu-JN for pgsql-general@lists.postgresql.org; Sat, 06 Jul 2024 06:19:45 +0000 Received: by mail-yw1-x1130.google.com with SMTP id 00721157ae682-64f4c11d2c9so18701087b3.2 for ; Fri, 05 Jul 2024 23:19:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720246782; x=1720851582; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=p21HDbRF1BmqwPp3tOeptGhLwPzVKTu0l7xwVlpVAlA=; b=lTtOJLobRf5v925C+0tdAFRWqrK1Hxki/ConY/QuhRpSpqiOOQdMOPx5zMTZlLXQGt iFQrZ5n/5YX5rv3tFrtPo5gDaKEQkqhLPQ9Wg/ADKEdxw0qE8ZmgmWDARcxAlGY+jUXq 1Sb2+WLCnEUIqGwZXffTqqnu2IDtSYkYNpTLGlLfsnycfQJ5V8Q92yx5vD4us68u5/u9 hkQ8m3ZE+PTBP/l47B2vi7EqzRqpKU/3ybhctjXJ7AlgQptHZePlLnvXDikKSL6hJc39 5gpnlzqKS4tBdwyoLxt9FN85ueKIe/jKiKqKoCO9z1lG1uHeg1kQ5yzfKBtzqSsmjAxb 0ysA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720246782; x=1720851582; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=p21HDbRF1BmqwPp3tOeptGhLwPzVKTu0l7xwVlpVAlA=; b=lEJrfkgxiLX69fht1vTOBMMqXsKqgM8JBPB6w1kWv+aAES/HGmW1P8LHd5uZgkUJYF vcmenmpoQcbq+gNv6sgr0RVAvts8v6Mua2FsgjWx2CQ1bg0ZIzEUlrfV8gf+0mdixz1y Oxs1e1ewzWMqU6OnbCir38VPmYrPjk/9iDsaBN7vmi1urHJ1nGXwBXWleFion+BHcy/A Auwd1lLQ3S+oe9NWxTcU/IxuCTHeiXfgWTk6TJSM6iVjxxekK2dPrpf0aqqBm/6ob23r BmEzXAhdhJYzYtCm9eA2UVO5NuLwNOuMdHQB8pH1RTjJrR9VJAhOJuCOKPANVlqEGNi3 p1xw== X-Forwarded-Encrypted: i=1; AJvYcCWXYXN/jeztW3hTfFEjALb9QuEAgzrgT2HPJ6M5kdwIGP5WdIEr8gpI+DeFf2loH58bYU7Lq2kmi77Ld+Frbd+STlF3DJfm+M2hlk6jWDJcGcGJ X-Gm-Message-State: AOJu0YwLxBprM7FcMsLSFrMS0dcXR4639N5U9u00iA20D8st3W/MvfEl aX9pFwllxIFiLrXxFXYKOA7OmhWAQ43ERbMLurdsSFVTQYbyJocx1y59Avl/0T7grqmd6qh8Egz 09WkM+aGe+eGInpL9SWRBuIzWpAw= X-Google-Smtp-Source: AGHT+IFcMp9mOSltfnHap9MxTJccLVtb4jRXglgdSV5WwWd5DhxMRhzFVytvEmzarNhzZzGfNPJK8VWUqtGShFIm3hc= X-Received: by 2002:a05:690c:4513:b0:650:8d5c:9615 with SMTP id 00721157ae682-652d5ef8fb7mr77084317b3.23.1720246782228; Fri, 05 Jul 2024 23:19:42 -0700 (PDT) MIME-Version: 1.0 References: <7b98efa8-9a3e-4012-8dd9-d980b5e40609@cloud.gatewaynet.com> <4d1943be-b6f5-4b9b-92fb-9bd7d9ba07e5@cloud.gatewaynet.com> In-Reply-To: <4d1943be-b6f5-4b9b-92fb-9bd7d9ba07e5@cloud.gatewaynet.com> From: Pavel Stehule Date: Sat, 6 Jul 2024 08:19:06 +0200 Message-ID: Subject: Re: Can a long running procedure detect when smart shutdown is pending? To: Achilleas Mantzios Cc: dwhite@seawardmoon.com, pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000006181d2061c8e2af9" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000006181d2061c8e2af9 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable so 6. 7. 2024 v 8:06 odes=C3=ADlatel Achilleas Mantzios < a.mantzios@cloud.gatewaynet.com> napsal: > =CE=A3=CF=84=CE=B9=CF=82 6/7/24 08:39, =CE=BF/=CE=B7 Pavel Stehule =CE=AD= =CE=B3=CF=81=CE=B1=CF=88=CE=B5: > > > > so 6. 7. 2024 v 1:36 odes=C3=ADlatel Dennis White > napsal: > >> Thanks for responding. >> This will be a stored procedure written in plpgsql that's kicked off by >> pg_cron. >> I wasn't sure a normal smart shutdown would stop it. >> > > shutdown try to cancel any query. The plpgsql routine should be canceled > without problems. > > fast mode will have this effect. smart mode not, from my testing. The > running queries do not get interrupted in smart shutdown. > ok but you can set timeout there. It doesn't help? You can create some aux postgresql connection with specific app name, and then you can monitor if this process still live from pg_stat_activity one process connect set application_name to 'xxxx'; second process if not exists(select * from pg_stat_activity where appname =3D 'xxxx') then exit end if; > > > >> >> Thanks >> >> On Fri, Jul 5, 2024 at 4:57=E2=80=AFPM Achilleas Mantzios < >> a.mantzios@cloud.gatewaynet.com> wrote: >> >>> =CE=A3=CF=84=CE=B9=CF=82 5/7/24 21:12, =CE=BF/=CE=B7 Dennis White =CE= =AD=CE=B3=CF=81=CE=B1=CF=88=CE=B5: >>> >>> My project's DB has a mutli-step stored procedure using Transaction >>> Control that may take 30 minutes or more to complete. >>> I am curious if there is a way to make it more smart shutdown friendly >>> so it can stop between steps? >>> >>> We are using both PG 14 and PG 16 on Rhel 8. >>> Pardon me if it's obvious but is there a function to call or a table >>> that could be checked after a commit within the procedure to determine = a >>> shutdown is pending? >>> >>> Maybe somehow checking the log for a message like : >>> >>> received smart shutdown request >>> >>> Or use tail_n_mail against the log and then implement some logic using >>> NOTIFY , ideally you want your procedure to be interrupted rather than = do >>> polling. >>> >>> >>> Thanks, >>> Dennis >>> >>> -- >>> Achilleas Mantzios >>> IT DEV - HEAD >>> IT DEPT >>> Dynacom Tankers Mgmt (as agents only) >>> >>> -- > Achilleas Mantzios > IT DEV - HEAD > IT DEPT > Dynacom Tankers Mgmt (as agents only) > > --0000000000006181d2061c8e2af9 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
so 6. 7. 2024 v=C2=A08:06 odes=C3=ADl= atel Achilleas Mantzios <a.mantzios@cloud.gatewaynet.com> napsal:
=20 =20 =20
=CE=A3=CF=84=CE=B9=CF=82 6/7/24 08:39, =CE=BF/=CE=B7 Pavel Stehule =CE=AD=CE=B3=CF=81=CE=B1=CF=88=CE=B5:
=20


so 6. 7. 2024 v=C2=A01:36 odes=C3=ADlatel Dennis White <dwhite@seawardmoon.com> napsal:
Thanks for responding.
This will be a stored procedure written in plpgsql that's kicked off by pg_cron.
I wasn't sure a normal smart shutdown would stop it.=

shutdown try to cancel any query. The plpgsql routine should be canceled without problems.
fast mode will have this effect. smart mode not, from my testing. The running queries do not get interrupted in smart shutdown.
=

ok

but you can se= t timeout there. It doesn't help?

You can crea= te some aux postgresql connection with specific app name, and then you can = monitor if this process still live from pg_stat_activity

one process

connect
set applicati= on_name to 'xxxx';

second process
if not exists(select * from pg_stat_activity where appname =3D 'xxxx&= #39;) then
=C2=A0 exit
end if;


=C2=A0

=C2=A0

Thanks

On Fri, Jul 5, 2024 at 4:57=E2=80=AFPM Achilleas Mantzios <a.mantzios@cloud.gatewayne= t.com> wrote:
=CE=A3=CF=84=CE=B9=CF=82 5/7/24 21:12, =CE=BF/=CE=B7= Dennis White =CE=AD=CE=B3=CF=81=CE=B1=CF=88=CE=B5:
My project's DB has a mutli-step stored procedure using Transaction Control that may take 30 minutes or more to complete.
I am curious if there is a way to make it more smart shutdown friendly so it can stop between steps?

We are using both PG 14 and PG 16 on Rhel 8.
Pardon me if it's obvious but is there a function to call or a table that could be checked after a commit within the procedure to determine a shutdown is pending?

Maybe somehow checking the log for a message like :

received smart shutdown request

Or use tail_n_mail against the log and then implement some logic using NOTIFY , ideally you want your procedure to be interrupted rather than do polling.


Thanks,
Dennis
--=20
Achilleas Mantzios
 IT DEV - HEAD
 IT DEPT
 Dynacom Tankers Mgmt (as agents only)
--=20
Achilleas Mantzios
 IT DEV - HEAD
 IT DEPT
 Dynacom Tankers Mgmt (as agents only)
--0000000000006181d2061c8e2af9--