pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedtcp keep alive don't work when the backend is busy
6+ messages / 5 participants
[nested] [flat]
* tcp keep alive don't work when the backend is busy
@ 2019-12-10 12:53 Олег Самойлов <splarv@ya.ru>
2019-12-10 15:06 ` Re: tcp keep alive don't work when the backend is busy Tom Lane <tgl@sss.pgh.pa.us>
2019-12-10 15:36 ` Re: tcp keep alive don't work when the backend is busy Justin <zzzzz.graf@gmail.com>
0 siblings, 2 replies; 6+ messages in thread
From: Олег Самойлов @ 2019-12-10 12:53 UTC (permalink / raw)
To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
According to the documentation
https://www.postgresql.org/docs/12/runtime-config-connection.html
A backend must check connection to the client by tcp_keepalive messages. (Config option tcp_keepalives_idle).
But this is don't work if the backend is busy.
Simple example:
psql localhost
set tcp_keepalives_idle=1;
do $$begin loop perform pg_sleep(1);end loop;end;$$;
In other terminal kill -9 the psql on the first terminal.
select * from pg_stat_activity where state='active';
And we will see that the backend is still active and busy.
The more realistic example. In the real code one of the loops, due to bug with asynchronous communication, come to the infinite loop. And occupy a backend and locks for a two week after the client was killed, before we detected this.
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: tcp keep alive don't work when the backend is busy
2019-12-10 12:53 tcp keep alive don't work when the backend is busy Олег Самойлов <splarv@ya.ru>
@ 2019-12-10 15:06 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-12-10 15:17 ` Re: tcp keep alive don't work when the backend is busy Fabio Ugo Venchiarutti <f.venchiarutti@ocado.com>
1 sibling, 1 reply; 6+ messages in thread
From: Tom Lane @ 2019-12-10 15:06 UTC (permalink / raw)
To: Олег Самойлов <splarv@ya.ru>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
=?utf-8?B?0J7Qu9C10LMg0KHQsNC80L7QudC70L7Qsg==?= <splarv@ya.ru> writes:
> According to the documentation
> https://www.postgresql.org/docs/12/runtime-config-connection.html
> A backend must check connection to the client by tcp_keepalive messages. (Config option tcp_keepalives_idle).
> But this is don't work if the backend is busy.
You're reading something into the documentation that isn't there.
The TCP keepalive mechanism is something that the OS does, independently
of backend processing. The backend isn't going to notice loss of client
connection until it tries to read or write on the connection.
If it were free to improve this, we might do so. But it would be
very much not free.
regards, tom lane
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: tcp keep alive don't work when the backend is busy
2019-12-10 12:53 tcp keep alive don't work when the backend is busy Олег Самойлов <splarv@ya.ru>
2019-12-10 15:06 ` Re: tcp keep alive don't work when the backend is busy Tom Lane <tgl@sss.pgh.pa.us>
@ 2019-12-10 15:17 ` Fabio Ugo Venchiarutti <f.venchiarutti@ocado.com>
2019-12-11 04:35 ` Re: tcp keep alive don't work when the backend is busy Thomas Munro <thomas.munro@gmail.com>
0 siblings, 1 reply; 6+ messages in thread
From: Fabio Ugo Venchiarutti @ 2019-12-10 15:17 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; Олег Самойлов <splarv@ya.ru>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On 10/12/2019 15:06, Tom Lane wrote:
> =?utf-8?B?0J7Qu9C10LMg0KHQsNC80L7QudC70L7Qsg==?= <splarv@ya.ru> writes:
>> According to the documentation
>> https://www.postgresql.org/docs/12/runtime-config-connection.html
>> A backend must check connection to the client by tcp_keepalive messages. (Config option tcp_keepalives_idle).
>
>> But this is don't work if the backend is busy.
>
> You're reading something into the documentation that isn't there.
>
> The TCP keepalive mechanism is something that the OS does, independently
> of backend processing. The backend isn't going to notice loss of client
> connection until it tries to read or write on the connection.
>
> If it were free to improve this, we might do so. But it would be
> very much not free.
>
> regards, tom lane
>
>
At what points does the backend bite the bullet to test the state of
that file descriptor?
I'd expect select() and poll() to return immediately when keepalive
probes timeout, so idling clients are covered (and that's the main use
case); does any other code path go out if its way to ensure that there's
still a client without actually needing to read()/write()/send()/recv()?
(obviously at the cost you mentioned)
--
Regards
Fabio Ugo Venchiarutti
OSPCFC Network Engineering Dpt.
Ocado Technology
--
Notice:
This email is confidential and may contain copyright material of
members of the Ocado Group. Opinions and views expressed in this message
may not necessarily reflect the opinions and views of the members of the
Ocado Group.
If you are not the intended recipient, please notify us
immediately and delete all copies of this message. Please note that it is
your responsibility to scan this message for viruses.
References to the
"Ocado Group" are to Ocado Group plc (registered in England and Wales with
number 7098618) and its subsidiary undertakings (as that expression is
defined in the Companies Act 2006) from time to time. The registered office
of Ocado Group plc is Buildings One & Two, Trident Place, Mosquito Way,
Hatfield, Hertfordshire, AL10 9UL.
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: tcp keep alive don't work when the backend is busy
2019-12-10 12:53 tcp keep alive don't work when the backend is busy Олег Самойлов <splarv@ya.ru>
2019-12-10 15:06 ` Re: tcp keep alive don't work when the backend is busy Tom Lane <tgl@sss.pgh.pa.us>
2019-12-10 15:17 ` Re: tcp keep alive don't work when the backend is busy Fabio Ugo Venchiarutti <f.venchiarutti@ocado.com>
@ 2019-12-11 04:35 ` Thomas Munro <thomas.munro@gmail.com>
0 siblings, 0 replies; 6+ messages in thread
From: Thomas Munro @ 2019-12-11 04:35 UTC (permalink / raw)
To: f.venchiarutti@ocado.com; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Олег Самойлов <splarv@ya.ru>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Wed, Dec 11, 2019 at 4:17 AM Fabio Ugo Venchiarutti
<f.venchiarutti@ocado.com> wrote:
> On 10/12/2019 15:06, Tom Lane wrote:
> > =?utf-8?B?0J7Qu9C10LMg0KHQsNC80L7QudC70L7Qsg==?= <splarv@ya.ru> writes:
> >> According to the documentation
> >> https://www.postgresql.org/docs/12/runtime-config-connection.html
> >> A backend must check connection to the client by tcp_keepalive messages. (Config option tcp_keepalives_idle).
> >
> >> But this is don't work if the backend is busy.
> >
> > You're reading something into the documentation that isn't there.
> >
> > The TCP keepalive mechanism is something that the OS does, independently
> > of backend processing. The backend isn't going to notice loss of client
> > connection until it tries to read or write on the connection.
> >
> > If it were free to improve this, we might do so. But it would be
> > very much not free.
>
> At what points does the backend bite the bullet to test the state of
> that file descriptor?
>
> I'd expect select() and poll() to return immediately when keepalive
> probes timeout, so idling clients are covered (and that's the main use
> case); does any other code path go out if its way to ensure that there's
> still a client without actually needing to read()/write()/send()/recv()?
> (obviously at the cost you mentioned)
It has been proposed that busy backends should (optionally)
periodically try to do a MSG_PEEK so they can learn about a client
that has gone away some time before they eventually try to write:
https://www.postgresql.org/message-id/flat/77def86b27e41f0efcba411460e929ae%40postgrespro.ru
More work is needed to move that forward, though.
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: tcp keep alive don't work when the backend is busy
2019-12-10 12:53 tcp keep alive don't work when the backend is busy Олег Самойлов <splarv@ya.ru>
@ 2019-12-10 15:36 ` Justin <zzzzz.graf@gmail.com>
2019-12-11 13:25 ` Re: tcp keep alive don't work when the backend is busy Олег Самойлов <splarv@ya.ru>
1 sibling, 1 reply; 6+ messages in thread
From: Justin @ 2019-12-10 15:36 UTC (permalink / raw)
To: Олег Самойлов <splarv@ya.ru>; pgsql-general
Hi Oner
It appears that you looking for a way to detect and kill of idle
connections or process that are running for a long time Correct??
If that is the case use statement_timeout setting and then use Pg_Agent and
this script to kill off idle connections
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = 'Database_Name'
AND pid <> pg_backend_pid()
AND state in ('idle', 'idle in transaction', 'idle in transaction (aborted)'
, 'disabled')
AND state_change < current_timestamp - INTERVAL '15' MINUTE;
Statement_Timeout can be set per session/connection
On Tue, Dec 10, 2019 at 7:53 AM Олег Самойлов <splarv@ya.ru> wrote:
> According to the documentation
> https://www.postgresql.org/docs/12/runtime-config-connection.html
> A backend must check connection to the client by tcp_keepalive messages.
> (Config option tcp_keepalives_idle).
>
> But this is don't work if the backend is busy.
>
> Simple example:
>
> psql localhost
>
> set tcp_keepalives_idle=1;
> do $$begin loop perform pg_sleep(1);end loop;end;$$;
>
> In other terminal kill -9 the psql on the first terminal.
>
> select * from pg_stat_activity where state='active';
> And we will see that the backend is still active and busy.
>
> The more realistic example. In the real code one of the loops, due to bug
> with asynchronous communication, come to the infinite loop. And occupy a
> backend and locks for a two week after the client was killed, before we
> detected this.
>
>
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: tcp keep alive don't work when the backend is busy
2019-12-10 12:53 tcp keep alive don't work when the backend is busy Олег Самойлов <splarv@ya.ru>
2019-12-10 15:36 ` Re: tcp keep alive don't work when the backend is busy Justin <zzzzz.graf@gmail.com>
@ 2019-12-11 13:25 ` Олег Самойлов <splarv@ya.ru>
0 siblings, 0 replies; 6+ messages in thread
From: Олег Самойлов @ 2019-12-11 13:25 UTC (permalink / raw)
To: Justin <zzzzz.graf@gmail.com>; +Cc: pgsql-general
> 10 дек. 2019 г., в 18:36, Justin <zzzzz.graf@gmail.com> написал(а):
>
> Hi Oner
>
> It appears that you looking for a way to detect and kill of idle connections or process that are running for a long time Correct??
Nope, not idle. Only to stop an active connection if the client is already died.
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2019-12-11 13:25 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-12-10 12:53 tcp keep alive don't work when the backend is busy Олег Самойлов <splarv@ya.ru>
2019-12-10 15:06 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-12-10 15:17 ` Fabio Ugo Venchiarutti <f.venchiarutti@ocado.com>
2019-12-11 04:35 ` Thomas Munro <thomas.munro@gmail.com>
2019-12-10 15:36 ` Justin <zzzzz.graf@gmail.com>
2019-12-11 13:25 ` Олег Самойлов <splarv@ya.ru>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox