From: Noel <noel.faux@med.monash.edu.au>
To: ljb <ljb220@mindspring.com>
Cc: pgsql-php@postgresql.org
Subject: Re: How to cancel a query when user stops a page load
Date: Mon, 22 Dec 2003 10:24:33 +1100
Message-ID: <3FE62BB1.2050600@med.monash.edu.au> (raw)
In-Reply-To: <bs0bt1$2i35$1@news.hub.org>
References: <bs0bt1$2i35$1@news.hub.org>
It works great, Thanks heaps, so simple once you know how :).
Cheers
ljb wrote:
>This is in response to a message posted here asking how to have PHP cancel
>an in-progress PostgreSQL query when the user hits STOP on their web
>browser, or clicks to another page. Here is my solution. This is not a
>complete script, just fragments, but it should help.
>
>To the original poster: you can't just check pg_connection_busy() once,
>then call pg_get_result(), because pg_get_result will block waiting for the
>query to finish, and the user abort won't be seen. You must loop as shown
>below. [P.S. Do you really have a "select count(*) from test" which takes
>2 minutes? Wow. I had trouble coming up with a test case taking 20 seconds
>on a PII-350MHz.]
>
Counting through a table with >20mill rows, 20 times :), to show stats
which are dynamic.
>
>.........................................
>
># Make a global flag to indicate query in progress:
>$in_progress = 0;
>
># Register a function to be called at shutdown:
>function halted()
>{
> global $db, $in_progress;
> if ($in_progress) {
> pg_cancel_query($db);
> }
>}
>register_shutdown_function('halted');
>
># Connect to the database and check for errors (OMITTED):
>$db = pg_connect(...);
>
># Issue the query and set the flag telling the shutdown function to cancel it:
>if (!pg_send_query($db, $query)) { ... error handling omitted ... }
>$in_progress = 1;
>
># Now we loop waiting for the query to complete or the user to cancel.
># Display a message to the user telling how long it has been.
>flush();
>$base = time();
>while (pg_connection_busy($db)) {
> sleep(2);
> $delta = time() - $base;
> echo "<br>... $delta seconds\n";
> flush();
>}
># All done, and it took $delta seconds.
>
># Don't let the shutdown handler try to cancel the query:
>$in_progress = 0;
># Fetch the query result:
>$r = pg_get_result($db);
># Check $r for errors (omitted), display results (omitted).
>
>.........................................
>
>---------------------------(end of broadcast)---------------------------
>TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly
>
>
Thanks once again :)
--
Noel Faux
Department of Biochemistry and Molecular Biology
Monash University
Clayton 3168
Victoria
Australia
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-php@postgresql.org
Cc: noel.faux@med.monash.edu.au, ljb220@mindspring.com
Subject: Re: How to cancel a query when user stops a page load
In-Reply-To: <3FE62BB1.2050600@med.monash.edu.au>
References: <bs0bt1$2i35$1@news.hub.org> <3FE62BB1.2050600@med.monash.edu.au>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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