I need to run one stored function from another in PostgreSQL server, but I need to run inner function in 4 threads to reduce in execution time. I can do that by make 4 threaded calls from backend, but it will more complex than call inside PostgeSQL server. I'm using plpgsql language.
I'd like to make something like following
create or replace function abc(some_data integer)
returns integer
language plpgsql
as $$
declare
intermediate_data some_my_type;
is_done boolean;
result integer;
begin
intermediate_data := result_of_synchronous_call(some_data);
async perform asinchronous_call(intermediate_data.part1);
async perform asinchronous_call(intermediate_data.part2);
async perform asinchronous_call(intermediate_data.part3);
async perform asinchronous_call(intermediate_data.part4);
SELECT count(*) = 4 INTO is_done FROM results;
while not is_done loop
SELECT pg_sleep(10);
SELECT count(*) = 4 INTO is_done FROM results;
end loop;
SELECT sum(result_field) INTO result FROM results;
return result;
end;
$$
CodePudding user response:
There is no pretty way to do this. The best I can think of is to use the dblink extension and use dblink_send_query
to send a query. Completion status can be checked with dblink_is_busy
, and the results are ultimately collected with dblink_get_result
.