Showing progress of a database function

From: "Tambet Matiisen" <t(dot)matiisen(at)aprote(dot)ee>
To: <pgsql-interfaces(at)postgresql(dot)org>
Subject: Showing progress of a database function
Date: 2005-03-01 13:55:41
Message-ID: A66A11DBF5525341AEF6B8DE39CDE770088046@black.aprote.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-interfaces

Hi everybody!

When designing database-centric applications it is always good idea to
push as much logic as possible to server side. The logic can be
implemented as check constraints, triggers and functions. The problem
is, that for lengthy function calls it will be impossible to see the
progress and to cancel the operation, unless your client library
supports asynchronous queries. Qt for example doesn't, like many others.

My idea was to use cursor as progress indicator for the function.
Simplified example:

create or replace function process_big_table() returns refcursor
language plpgsql as
declare
cur as refcursor;
begin;
open cur for
select process_row(id)
from big_table
where special_condition
order by special_order;
return cur;
end;

As you see, there is another function process_row(), which does the
actual processing. This function has to be called for certain rows in
big table, in certain order. In client code I do something like this:

begin;
select process_big_table(); -- returns the name of the cursor
loop
fetch next from <cursor name>;
<update progress counter>
<if cancel was pressed do rollback and break>
end loop
commit;

I expected it to call process_row() every time I fetch a row. But
actually it processes all of them on first fetch and all following rows
are returned in constant time. Is this a normal way of operation? Or
does it depend on query? Are the query results always precalculated at
the server side and the save from using cursors is just not to download
all rows to client?

If anyone has other ideas how to show progress of a database function,
then you are welcome to share. I have considered using notice
processing, but this requires access to libpq objects such as PGconn,
which can be tricky with Qt (altough possible, as Qt uses libpq
internally). And to be able to cancel requests I still need asynchronous
query API, which is missing in Qt.

Tambet

Responses

Browse pgsql-interfaces by date

  From Date Subject
Next Message Albert Cervera Areny 2005-03-02 07:22:51 Re: Showing progress of a database function
Previous Message b t 2005-02-28 09:53:06 How can I display the privileges of a certain user?