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;
returncur;
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