proposal: add new dblink function, dblink_wait_for_query() - Mailing list pgsql-hackers

From Merlin Moncure
Subject proposal: add new dblink function, dblink_wait_for_query()
Date
Msg-id CAHyXU0w92SrpQtKca9dq=YEod44XbKWCoQuANN15C0svPLYhzw@mail.gmail.com
Whole thread
Responses Re: proposal: add new dblink function, dblink_wait_for_query()
List pgsql-hackers
dblink has some nice capabilities for executing background queries, but it is missing some functionality; there is no ability to open connections asynchronously, and any polling of outstanding queries has do be done in SQL with a dblink_is_busy() loop.  The polling issue is more severe especially if large amounts of result data are pulled over the connection since only one PQconsumeInput can execute at any one time.  This idea attempts to mitigate that issue.  dblink may be somewhat baroque, but it remains to often be the best way to do db to db querying and the only way to reliably way to issue background work in cloud SQL environments. 

The basic idea here is to implement a new SQL API routine:
dblink_wait_for_query(_timeout INTERVAL) -> TEXT[]  

The idea is that the function would wait untl an asynchronous query (sent via dblink_send_query) becomes 'unbusy', or ready to return a result without waiting unless timeout happens first. The returned list is an array of names that are ready for dblink_get_result without blocking.

The implementation would be to:
1. create a WaitEventSet in dblink_init,
2. Add the libpq socket to the set inside of dblink_send_query()
3. Remove the libpqsocket (if present) during dblink_get_result() 
4. in dblink_wait_for_query, issue WaitEventSetWait() converting and passing the SQL supplied timeout.  Open receiving evented traffic, it would resove the connection via hash table lookup from the socket and run an is_busy check on it. All connections returning as not busy would be added to the result set and passed back to the SQL layer.  If none did, the timeout would be locally adjusted, and WaitEventSetWait would be re-issued

I'm looking for feedback on the approach, especially regarding the appropriateness of using WaitEventSetWait in this context.  This idea aims to significantly optimize any scenario where dblink issues a large number of queries or queries that return large amounts of data.


merlin


 

pgsql-hackers by date:

Previous
From: Ilia Evdokimov
Date:
Subject: Re: Apply extended statistics to join clause during parameterized path costing
Next
From: Bharath Rupireddy
Date:
Subject: Re: pg_xmin_horizon: a system view of everything pinning the xmin horizon