Problem
Sync requests were previously supported using net.collect_response (deprecated); which is bad for performance, since it does an infinite loop plus pg_sleep.
|
create or replace function net._await_response( |
|
request_id bigint |
|
) |
|
returns bool |
|
volatile |
|
parallel safe |
|
strict |
|
language plpgsql |
|
as $$ |
|
declare |
|
rec net._http_response; |
|
begin |
|
while rec is null loop |
|
select * |
|
into rec |
|
from net._http_response |
|
where id = request_id; |
|
|
|
if rec is null then |
|
-- Wait 50 ms before checking again |
|
perform pg_sleep(0.05); |
|
end if; |
|
end loop; |
|
|
|
return true; |
|
end; |
|
$$; |
Some simple use cases require sync requests (https://github.com/orgs/supabase/discussions/28771). While this is bad for performance, it can help some users get going.
Solution
Now that #139 is merged, we can implement sync requests efficiently by waiting on the socket file descriptor. No need for loops or querying the response table.
This would be implemented as a PROCEDURE, so users can only invoke it with CALL, which prevents combining it with other queries on the same statement. This way it'll be more clear that this is a special (and slow) operation.
Problem
Sync requests were previously supported using
net.collect_response(deprecated); which is bad for performance, since it does an infinite loop plus pg_sleep.pg_net/sql/pg_net.sql
Lines 50 to 76 in 28b26d8
Some simple use cases require sync requests (https://github.com/orgs/supabase/discussions/28771). While this is bad for performance, it can help some users get going.
Solution
Now that #139 is merged, we can implement sync requests efficiently by waiting on the socket file descriptor. No need for loops or querying the response table.
This would be implemented as a PROCEDURE, so users can only invoke it with CALL, which prevents combining it with other queries on the same statement. This way it'll be more clear that this is a special (and slow) operation.