pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
statement_timeout vs DECLARE CURSOR
5+ messages / 2 participants
[nested] [flat]

* statement_timeout vs DECLARE CURSOR
@ 2021-09-27 17:42  Christophe Pettus <xof@thebuild.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Christophe Pettus @ 2021-09-27 17:42 UTC (permalink / raw)
  To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Hi,

We've encountered some unexpected behavior with statement_timeout not cancelling a query in DECLARE CURSOR, but only if the DECLARE CURSOR is outside of a transaction:

xof=# select version();
                                                      version                                                      
-------------------------------------------------------------------------------------------------------------------
 PostgreSQL 13.4 on x86_64-apple-darwin19.6.0, compiled by Apple clang version 12.0.0 (clang-1200.0.32.29), 64-bit
(1 row)

xof=# set statement_timeout = '1s';
SET
xof=# \timing
Timing is on.
xof=# select * from (with test as (select pg_sleep(10), current_timestamp as cur_time) select 1 from test ) as slp;
ERROR:  canceling statement due to statement timeout
Time: 1000.506 ms (00:01.001)
xof=# declare x no scroll cursor with hold for select * from (with test as (select pg_sleep(10), current_timestamp as cur_time) select 1 from test ) as slp;
DECLARE CURSOR
Time: 10001.929 ms (00:10.002)
xof=# 

but:

xof=# set statement_timeout = '1s';
SET
xof=# \timing
Timing is on.
xof=# begin;
BEGIN
Time: 0.161 ms
xof=*# declare x no scroll cursor with hold for select * from (with test as (select pg_sleep(10), current_timestamp as cur_time) select 1 from test ) as slp;
DECLARE CURSOR
Time: 0.949 ms
xof=*# fetch all from x;
ERROR:  canceling statement due to statement timeout
Time: 1000.520 ms (00:01.001)
xof=!# abort;
ROLLBACK
Time: 0.205 ms
xof=# 






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

* Re: statement_timeout vs DECLARE CURSOR
@ 2021-09-27 18:10  Christophe Pettus <xof@thebuild.com>
  parent: Christophe Pettus <xof@thebuild.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Christophe Pettus @ 2021-09-27 18:10 UTC (permalink / raw)
  To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>



> On Sep 27, 2021, at 10:42, Christophe Pettus <xof@thebuild.com> wrote:
> We've encountered some unexpected behavior with statement_timeout not cancelling a query in DECLARE CURSOR, but only if the DECLARE CURSOR is outside of a transaction:

A bit more poking revealed the reason: The ON HOLD cursor's query is executed at commit time (which is, logically, not interruptible), but that's all wrapped in the single statement outside of a transaction.




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

* Re: statement_timeout vs DECLARE CURSOR
@ 2021-09-27 19:40  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Christophe Pettus <xof@thebuild.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Tom Lane @ 2021-09-27 19:40 UTC (permalink / raw)
  To: Christophe Pettus <xof@thebuild.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Christophe Pettus <xof@thebuild.com> writes:
>> On Sep 27, 2021, at 10:42, Christophe Pettus <xof@thebuild.com> wrote:
>> We've encountered some unexpected behavior with statement_timeout not cancelling a query in DECLARE CURSOR, but only if the DECLARE CURSOR is outside of a transaction:

> A bit more poking revealed the reason: The ON HOLD cursor's query is executed at commit time (which is, logically, not interruptible), but that's all wrapped in the single statement outside of a transaction.

Hmm ... seems like a bit of a UX failure.  I wonder why we don't persist
such cursors before we get into the uninterruptible part of COMMIT.

			regards, tom lane





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

* Re: statement_timeout vs DECLARE CURSOR
@ 2021-09-28 19:57  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 5+ messages in thread

From: Tom Lane @ 2021-09-28 19:57 UTC (permalink / raw)
  To: Christophe Pettus <xof@thebuild.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

I wrote:
> Christophe Pettus <xof@thebuild.com> writes:
>> A bit more poking revealed the reason: The ON HOLD cursor's query is executed at commit time (which is, logically, not interruptible), but that's all wrapped in the single statement outside of a transaction.

> Hmm ... seems like a bit of a UX failure.  I wonder why we don't persist
> such cursors before we get into the uninterruptible part of COMMIT.

Oh, I see the issue.  It's not that that part of COMMIT isn't
interruptible; you can control-C out of it just fine.  The problem
is that finish_xact_command() disarms the statement timeout before
starting CommitTransactionCommand at all.

We could imagine pushing the responsibility for that down into
xact.c, allowing it to happen after CommitTransaction has finished
running user-defined code.  But it seems like a bit of a mess
because there are so many other code paths there.  Not sure how
to avoid future bugs-of-omission.

			regards, tom lane





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

* Re: statement_timeout vs DECLARE CURSOR
@ 2021-09-28 20:15  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 5+ messages in thread

From: Tom Lane @ 2021-09-28 20:15 UTC (permalink / raw)
  To: Christophe Pettus <xof@thebuild.com>; +Cc: pgsql-hackers@lists.postgresql.org

[ redirect to -hackers ]

I wrote:
>> Christophe Pettus <xof@thebuild.com> writes:
>>> A bit more poking revealed the reason: The ON HOLD cursor's query is executed at commit time (which is, logically, not interruptible), but that's all wrapped in the single statement outside of a transaction.

>> Hmm ... seems like a bit of a UX failure.  I wonder why we don't persist
>> such cursors before we get into the uninterruptible part of COMMIT.

> Oh, I see the issue.  It's not that that part of COMMIT isn't
> interruptible; you can control-C out of it just fine.  The problem
> is that finish_xact_command() disarms the statement timeout before
> starting CommitTransactionCommand at all.

> We could imagine pushing the responsibility for that down into
> xact.c, allowing it to happen after CommitTransaction has finished
> running user-defined code.  But it seems like a bit of a mess
> because there are so many other code paths there.  Not sure how
> to avoid future bugs-of-omission.

Actually ... maybe it needn't be any harder than the attached?

This makes it possible for a statement timeout interrupt to occur
anytime during CommitTransactionCommand, but I think
CommitTransactionCommand had better be able to protect itself
against that anyway, for a couple of reasons:

1. It's not significantly different from a query-cancel interrupt,
which surely could arrive during that window.

2. COMMIT-within-procedures already exposes us to statement timeout
during COMMIT.

			regards, tom lane

Attachments:

include-commit-processing-in-statement-timeout.patch (text/x-diff, 589B) download diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index 0775abe35d..9ec96f2453 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -2713,9 +2713,6 @@ start_xact_command(void) static void finish_xact_command(void) { - /* cancel active statement timeout after each command */ - disable_statement_timeout(); - if (xact_started) { CommitTransactionCommand(); @@ -2733,6 +2730,9 @@ finish_xact_command(void) xact_started = false; } + + /* cancel active statement timeout after each command */ + disable_statement_timeout(); }
^ permalink raw reply [nested|flat] 5+ messages in thread


end of thread, other threads:[~2021-09-28 20:15 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-09-27 17:42 statement_timeout vs DECLARE CURSOR Christophe Pettus <xof@thebuild.com>
2021-09-27 18:10 ` Christophe Pettus <xof@thebuild.com>
2021-09-27 19:40   ` Tom Lane <tgl@sss.pgh.pa.us>
2021-09-28 19:57     ` Tom Lane <tgl@sss.pgh.pa.us>
2021-09-28 20:15       ` Tom Lane <tgl@sss.pgh.pa.us>

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