pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Limit transaction lifetime
4+ messages / 2 participants
[nested] [flat]

* Limit transaction lifetime
@ 2020-03-06 14:24 Andrei Zhidenkov <andrei.zhidenkov@n26.com>
  2020-03-06 15:09 ` Re: Limit transaction lifetime Fabio Ugo Venchiarutti <f.venchiarutti@ocado.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Andrei Zhidenkov @ 2020-03-06 14:24 UTC (permalink / raw)
  To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Is there a way to limit a transaction lifetime in PostgreSQL? I could use
`idle_in_transaction_session_timeout` parameter but it applies only to IDLE
transactions. However, I want to rollback any transaction that executes
more than specified period of time.

--

With best regards, Andrei Zhidenkov.

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Limit transaction lifetime
  2020-03-06 14:24 Limit transaction lifetime Andrei Zhidenkov <andrei.zhidenkov@n26.com>
@ 2020-03-06 15:09 ` Fabio Ugo Venchiarutti <f.venchiarutti@ocado.com>
  2020-03-06 16:14   ` Re: Limit transaction lifetime Andrei Zhidenkov <andrei.zhidenkov@n26.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Fabio Ugo Venchiarutti @ 2020-03-06 15:09 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org


On 06/03/2020 14:24, Andrei Zhidenkov wrote:
> Is there a way to limit a transaction lifetime in PostgreSQL? I could 
> use `idle_in_transaction_session_timeout` parameter but it applies only 
> to IDLE transactions. However, I want to rollback any transaction that 
> executes more than specified period of time.
> 
> --
> 
> With best regards, Andrei Zhidenkov.

If it's a single command you're trying to limit `SET statement_timeout 
TO <whatever>` should do the trick.


If you want it based on the session's cumulative statement time, off the 
top of my head I can't think of anything in vanilla PG without using 
executor hooks (that requires some coding).


If the queries that worry you are long-lived, you might be able to get 
by with a scheduled process checking against pg_stat_activity (eg: 
age(query_start)) and adding the current query's run-time to some 
per-session total, but it's an highly inaccurate process.




-- 
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] 4+ messages in thread

* Re: Limit transaction lifetime
  2020-03-06 14:24 Limit transaction lifetime Andrei Zhidenkov <andrei.zhidenkov@n26.com>
  2020-03-06 15:09 ` Re: Limit transaction lifetime Fabio Ugo Venchiarutti <f.venchiarutti@ocado.com>
@ 2020-03-06 16:14   ` Andrei Zhidenkov <andrei.zhidenkov@n26.com>
  2020-03-06 16:18     ` Re: Limit transaction lifetime Fabio Ugo Venchiarutti <f.venchiarutti@ocado.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Andrei Zhidenkov @ 2020-03-06 16:14 UTC (permalink / raw)
  To: f.venchiarutti@ocado.com; +Cc: pgsql-general@lists.postgresql.org


> If it's a single command you're trying to limit `SET statement_timeout TO <whatever>` should do the trick.

This will set only statement timeout but won’t work for long transactions that contain a lot of short statements.

> If you want it based on the session's cumulative statement time, off the top of my head I can't think of anything in vanilla PG without using executor hooks (that requires some coding).

Yes, that’s exactly I want to do.

> If the queries that worry you are long-lived, you might be able to get by with a scheduled process checking against pg_stat_activity (eg: age(query_start)) and adding the current query's run-time to some per-session total, but it's an highly inaccurate process.

I think in my case I should check `xact_start`, because not every query initiates a new transaction.






^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Limit transaction lifetime
  2020-03-06 14:24 Limit transaction lifetime Andrei Zhidenkov <andrei.zhidenkov@n26.com>
  2020-03-06 15:09 ` Re: Limit transaction lifetime Fabio Ugo Venchiarutti <f.venchiarutti@ocado.com>
  2020-03-06 16:14   ` Re: Limit transaction lifetime Andrei Zhidenkov <andrei.zhidenkov@n26.com>
@ 2020-03-06 16:18     ` Fabio Ugo Venchiarutti <f.venchiarutti@ocado.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Fabio Ugo Venchiarutti @ 2020-03-06 16:18 UTC (permalink / raw)
  To: Andrei Zhidenkov <andrei.zhidenkov@n26.com>; +Cc: pgsql-general@lists.postgresql.org

On 06/03/2020 16:14, Andrei Zhidenkov wrote:
> 
>> If it's a single command you're trying to limit `SET statement_timeout TO <whatever>` should do the trick.
> 
> This will set only statement timeout but won’t work for long transactions that contain a lot of short statements.
> 
>> If you want it based on the session's cumulative statement time, off the top of my head I can't think of anything in vanilla PG without using executor hooks (that requires some coding).
> 
> Yes, that’s exactly I want to do.
> 
>> If the queries that worry you are long-lived, you might be able to get by with a scheduled process checking against pg_stat_activity (eg: age(query_start)) and adding the current query's run-time to some per-session total, but it's an highly inaccurate process.
> 
> I think in my case I should check `xact_start`, because not every query initiates a new transaction.
That way you'd also be counting in time a given transaction spent 
idling, which IME is one of the biggest source of concurrency headaches 
if it's holding locks while doing nothing.
If your use case is unaffected by that, that'd be good news for you.





-- 
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] 4+ messages in thread


end of thread, other threads:[~2020-03-06 16:18 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-03-06 14:24 Limit transaction lifetime Andrei Zhidenkov <andrei.zhidenkov@n26.com>
2020-03-06 15:09 ` Fabio Ugo Venchiarutti <f.venchiarutti@ocado.com>
2020-03-06 16:14   ` Andrei Zhidenkov <andrei.zhidenkov@n26.com>
2020-03-06 16:18     ` Fabio Ugo Venchiarutti <f.venchiarutti@ocado.com>

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