Lists: | pgsql-hackers |
---|
From: | Peter Da Silva <peter(dot)dasilva(at)flightaware(dot)com> |
---|---|
To: | PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org> |
Subject: | pl/tcl function to detect when a request has been canceled |
Date: | 2018-06-08 18:08:14 |
Message-ID: | 90F9A12C-2692-48C4-8910-6AA58C7E0137@flightaware.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-hackers |
We have occasional need to run very long-running pl/tcl scripts. If the request is cancelled (say, by the user hitting ^c in psql) the server-side script still runs to completion.
There is a C-level variable QueryCancelPending that can be used to monitor for this case, but it’s not visible at the pl/tcl scripting level. This is a simple new command that returns the current state of this variable to Tcl.
We are currently maintaining a fork of pl/tcl at https://github.com/flightaware/pltcl that has this mod, but it would be useful to get the functionality into mainline PostgreSQL.
Attachment | Content-Type | Size |
---|---|---|
cancel_pending.diff | application/octet-stream | 1.7 KB |
From: | Andres Freund <andres(at)anarazel(dot)de> |
---|---|
To: | Peter Da Silva <peter(dot)dasilva(at)flightaware(dot)com> |
Cc: | PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org> |
Subject: | Re: pl/tcl function to detect when a request has been canceled |
Date: | 2018-06-08 18:12:53 |
Message-ID: | 20180608181253.43sn7qw7zanv4vkb@alap3.anarazel.de |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-hackers |
Hi,
On 2018-06-08 18:08:14 +0000, Peter Da Silva wrote:
> We have occasional need to run very long-running pl/tcl scripts. If
> the request is cancelled (say, by the user hitting ^c in psql) the
> server-side script still runs to completion.
>
> There is a C-level variable QueryCancelPending that can be used to
> monitor for this case, but it’s not visible at the pl/tcl scripting
> level. This is a simple new command that returns the current state of
> this variable to Tcl.
>
> We are currently maintaining a fork of pl/tcl at
> https://github.com/flightaware/pltcl that has this mod, but it would
> be useful to get the functionality into mainline PostgreSQL.
I'm not terribly opposed to this, but I wonder if the much more
pragmatic solution is to just occasionally call a database function that
checks this? You could just run SELECT 1 occasionally :/
- Andres
From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | Andres Freund <andres(at)anarazel(dot)de> |
Cc: | Peter Da Silva <peter(dot)dasilva(at)flightaware(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org> |
Subject: | Re: pl/tcl function to detect when a request has been canceled |
Date: | 2018-06-08 18:41:41 |
Message-ID: | 518.1528483301@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-hackers |
Andres Freund <andres(at)anarazel(dot)de> writes:
> On 2018-06-08 18:08:14 +0000, Peter Da Silva wrote:
>> There is a C-level variable QueryCancelPending that can be used to
>> monitor for this case, but it’s not visible at the pl/tcl scripting
>> level. This is a simple new command that returns the current state of
>> this variable to Tcl.
> I'm not terribly opposed to this, but I wonder if the much more
> pragmatic solution is to just occasionally call a database function that
> checks this? You could just run SELECT 1 occasionally :/
That would be quite expensive for the purpose, surely.
My complaint about it is more like "if we're going to put this into
pltcl, why not also plperl and plpython?" It might be unfair to
ask this patch to cover all three, but it would be odd not to try to
maintain feature parity ... especially since pltcl is probably the
least-used of the three these days.
regards, tom lane
From: | Peter Da Silva <peter(dot)dasilva(at)flightaware(dot)com> |
---|---|
To: | Andres Freund <andres(at)anarazel(dot)de> |
Cc: | PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org> |
Subject: | Re: pl/tcl function to detect when a request has been canceled |
Date: | 2018-06-08 19:05:12 |
Message-ID: | C82A8B79-027A-4A6D-89FE-5859C240A409@flightaware.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-hackers |
On 6/8/18, 1:12 PM, "Andres Freund" <andres(at)anarazel(dot)de> wrote:
I'm not terribly opposed to this, but I wonder if the much more
pragmatic solution is to just occasionally call a database function that
checks this? You could just run SELECT 1 occasionally :/
That seems to work, and I suppose in most cases the overhead could be mitigated by only calling it every N times through a loop, but it's not as clean.
From: | Andres Freund <andres(at)anarazel(dot)de> |
---|---|
To: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
Cc: | Peter Da Silva <peter(dot)dasilva(at)flightaware(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org> |
Subject: | Re: pl/tcl function to detect when a request has been canceled |
Date: | 2018-06-08 19:08:35 |
Message-ID: | 20180608190835.6etsvpodkecwzryc@alap3.anarazel.de |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-hackers |
On 2018-06-08 14:41:41 -0400, Tom Lane wrote:
> Andres Freund <andres(at)anarazel(dot)de> writes:
> > On 2018-06-08 18:08:14 +0000, Peter Da Silva wrote:
> >> There is a C-level variable QueryCancelPending that can be used to
> >> monitor for this case, but it’s not visible at the pl/tcl scripting
> >> level. This is a simple new command that returns the current state of
> >> this variable to Tcl.
>
> > I'm not terribly opposed to this, but I wonder if the much more
> > pragmatic solution is to just occasionally call a database function that
> > checks this? You could just run SELECT 1 occasionally :/
>
> That would be quite expensive for the purpose, surely.
Sure, but it works today...
Either way, I'm not convinced that handling query cancels in isolation
is really the right thing. I think pretty much all forms of interrupt
would need to be processed, not just cancels. It's imo at least as
important to process session termination (including recovery conflicts),
and catchup interrupts. I think we largely require that the PLs handle
exceptions anyway, so just having a 'pg_process_interrupts()' function
that then is wrapped by the individual PLs would make sense imo.
Greetings,
Andres Freund
From: | Peter Da Silva <peter(dot)dasilva(at)flightaware(dot)com> |
---|---|
To: | Andres Freund <andres(at)anarazel(dot)de> |
Cc: | PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org> |
Subject: | Re: pl/tcl function to detect when a request has been canceled |
Date: | 2018-06-08 19:16:49 |
Message-ID: | DA8FEB43-77DB-4072-9176-0E44A1BB93ED@flightaware.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-hackers |
On 6/8/18, 1:12 PM, "Andres Freund" <andres(at)anarazel(dot)de> wrote:
I'm not terribly opposed to this, but I wonder if the much more
pragmatic solution is to just occasionally call a database function that
checks this? You could just run SELECT 1 occasionally :/
After further discussion with our team:
Would this work if the reason for it ignoring the cancel request is that it is already performing a long-running spi_exec with a large response?
From: | Andres Freund <andres(at)anarazel(dot)de> |
---|---|
To: | Peter Da Silva <peter(dot)dasilva(at)flightaware(dot)com> |
Cc: | PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org> |
Subject: | Re: pl/tcl function to detect when a request has been canceled |
Date: | 2018-06-08 19:21:54 |
Message-ID: | 20180608192154.shvmozcpstpnhlyf@alap3.anarazel.de |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-hackers |
On 2018-06-08 19:16:49 +0000, Peter Da Silva wrote:
> On 6/8/18, 1:12 PM, "Andres Freund" <andres(at)anarazel(dot)de> wrote:
> I'm not terribly opposed to this, but I wonder if the much more
> pragmatic solution is to just occasionally call a database function that
> checks this? You could just run SELECT 1 occasionally :/
>
> After further discussion with our team:
>
> Would this work if the reason for it ignoring the cancel request is
> that it is already performing a long-running spi_exec with a large
> response?
Not sure I quite understand what you mean. You're thinking of the case
where you're processing rows one-by-one with a cursor? Or that a single
spi call takes a long while to process the query?
Greetings,
Andres Freund
From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | Andres Freund <andres(at)anarazel(dot)de> |
Cc: | Peter Da Silva <peter(dot)dasilva(at)flightaware(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org> |
Subject: | Re: pl/tcl function to detect when a request has been canceled |
Date: | 2018-06-08 19:23:18 |
Message-ID: | 2633.1528485798@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-hackers |
Andres Freund <andres(at)anarazel(dot)de> writes:
> Either way, I'm not convinced that handling query cancels in isolation
> is really the right thing. I think pretty much all forms of interrupt
> would need to be processed, not just cancels.
+1
regards, tom lane
From: | Peter Da Silva <peter(dot)dasilva(at)flightaware(dot)com> |
---|---|
To: | Andres Freund <andres(at)anarazel(dot)de> |
Cc: | PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org> |
Subject: | Re: pl/tcl function to detect when a request has been canceled |
Date: | 2018-06-08 19:34:20 |
Message-ID: | 91E0AE11-E371-40DE-8FE7-CBE50932D2D1@flightaware.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-hackers |
On 6/8/18, 2:21 PM, "Andres Freund" <andres(at)anarazel(dot)de> wrote:
Not sure I quite understand what you mean. You're thinking of the case
where you're processing rows one-by-one with a cursor? Or that a single
spi call takes a long while to process the query?
The former, I believe. One example (lightly obfuscated):
spi_exec -array a "SELECT this,that from table where stuff..." {
if {$use_cancel_pending && [cancel_pending]} {
error "cancelled in ..."
}
do_something_with_array a
more_business_code_here
and_so_on...
}