Thread: libpq, blocking/nonblocking mechanism
Hi, While I'm trying to figure out the point of PQsetnonblocking() call, confused so much on blocking/nonblocking mechanism. Sync. Connect » Async. Query Async. Connect » Sync. Query Both of above flows work without any PQsetnonblocking() call. Therefore sync/async connection and sync/async query execution are not dependent to each other. (Right?) If so, what's the point of PQsetnonblocking() function? When do we require it and when should we use it? As I saw from src/interfaces/libpq/fe-exec.c, PQsetnonblocking() only assigns True or False to conn->nonblocking variable. When I searched for "->nonblocking" under libpq directory, I found that it's used while creating an empty PGconn and while closing PGconn. This didn't help me too. I'd be so appreciated for a comprehensive "specialized for dummies" answer. Regards. P.S. Documentation pointers will be accepted with pleasure too.
From the docs: PQsetnonblocking Sets the nonblocking status of the connection. int PQsetnonblocking(PGconn *conn, int arg); Sets the state of the connection to nonblocking if arg is 1, or blocking if arg is 0. Returns 0 if OK, -1 if error. In the nonblocking state, calls to PQsendQuery, PQputline, PQputnbytes, and PQendcopy will not block but instead return an error if they need to be called again. Note that PQexec does not honor nonblocking mode; if it is called, it will act in blocking fashion anyway. A call to PQsetnonblocking only affects PQsendQuery, etc. These commands will work without calling PQsetnonblocking, but they will block. Our application uses a combination of PQexec and PQsendQuery. I'm looking at the documentation in: file:/usr/share/doc/postgresql-7.4.6/html/libpq-async.html on my machine. On Saturday 28 May 2005 04:34 pm, Volkan YAZICI saith: > Hi, > > While I'm trying to figure out the point of PQsetnonblocking() call, > confused so much on blocking/nonblocking mechanism. > > Sync. Connect » Async. Query > Async. Connect » Sync. Query > > Both of above flows work without any PQsetnonblocking() call. > Therefore sync/async connection and sync/async query execution are not > dependent to each other. (Right?) If so, what's the point of > PQsetnonblocking() function? When do we require it and when should we > use it? > > As I saw from src/interfaces/libpq/fe-exec.c, PQsetnonblocking() only > assigns True or False to conn->nonblocking variable. When I searched > for "->nonblocking" under libpq directory, I found that it's used > while creating an empty PGconn and while closing PGconn. This didn't > help me too. > > I'd be so appreciated for a comprehensive "specialized for dummies" answer. > Regards. > > P.S. Documentation pointers will be accepted with pleasure too. > > ---------------------------(end of broadcast)--------------------------- > TIP 5: Have you checked our extensive FAQ? > > http://www.postgresql.org/docs/faq
Hi, On 5/31/05, Terry Lee Tucker <terry@esc1.com> wrote: [snipped] > I'm looking at the documentation in: > file:/usr/share/doc/postgresql-7.4.6/html/libpq-async.html > on my machine. Thanks so much, but I'm able to read manuals too. I'm trying to don't carry my questions here until I finish a satisfying postgresql.org/docs plus google.com search. Namely, I read that lines nearly I hundred times. I advice you to looking at my question twice. Regards.
On Wednesday 01 June 2005 02:18 pm, Volkan YAZICI saith: > Hi, > > On 5/31/05, Terry Lee Tucker <terry@esc1.com> wrote: > [snipped] > > > I'm looking at the documentation in: > > file:/usr/share/doc/postgresql-7.4.6/html/libpq-async.html > > on my machine. > > Thanks so much, but I'm able to read manuals too. From your original message: "P.S. Documentation pointers will be accepted with pleasure too." > I'm trying to don't > carry my questions here until I finish a satisfying > postgresql.org/docs plus google.com search. Namely, I read that lines > nearly I hundred times. > > I advice you to looking at my question twice. PQsetnonblocking () causes PQsendQuery to send an sql string to the backend and it returns immediately if you have set nonblocking to true. If you have not set nonblocking to true, then PQsendQuery behaves just like PQexec; it blocks, i.e., it waits for the query, update, delete, or whatever to complete before it returns program control back to your program. We set nonblocking to True in our application and when the user queries a large table that could return thousands of records, we use PQsendQuery instead of PQexec; however, if you do that, then you have to use PQconsumeInput and PQisBusy in conjunction with a select statement to monitor the backend connection socket so you can process the results when the query finishes. Another reason for using asynchronous queries is that you can provide a mechanism for the user to cancel the operation. Our application is X-Windows based written in C. All my examples are C. If you want to see any of it, you can let me know. > > Regards. > > ---------------------------(end of broadcast)--------------------------- > TIP 9: the planner will ignore your desire to choose an index scan if your > joining column's datatypes do not match
Hi, On 6/1/05, Terry Lee Tucker <terry@esc1.com> wrote: > PQsetnonblocking() causes PQsendQuery to send an sql string to the backend > and it returns immediately if you have set nonblocking to true. If you have > not set nonblocking to true, then PQsendQuery behaves just like PQexec; it > blocks, i.e., it waits for the query, update, delete, or whatever to complete > before it returns program control back to your program. We set nonblocking to > True in our application and when the user queries a large table that could > return thousands of records, we use PQsendQuery instead of PQexec; however, > if you do that, then you have to use PQconsumeInput and PQisBusy in > conjunction with a select statement to monitor the backend connection socket > so you can process the results when the query finishes. Another reason for > using asynchronous queries is that you can provide a mechanism for the user > to cancel the operation. Thanks so much for the explanation. > Our application is X-Windows based written in C. > All my examples are C. If you want to see any of it, you can let me know. I'd be so appreciated to see them. Regards.