PQisBusy returns true but no more data is received.

Started by Pelle Johanssonabout 20 years ago2 messagesgeneral
Jump to latest
#1Pelle Johansson
morth@morth.org

Hello list,

I'm new here, but didn't see the problem in the archives.

Basically, I have an epoll loop that executes the following code when
I receive data from postgresql (greatly simplified).

int read_sql (PGconn *conn)
{
PGnotify *notice;
PGresult *res;

if (!PQconsumeInput (conn))
return -1;

while (1)
{
while ((notice = PQnotifies (conn)))
{
handle_notice (notice);
PQmemfree (notice);
}

if (PQisBusy (conn))
return 0;

res = PQgetResult (conn);
if (!res)
break;

while ((notice = PQnotifies (conn)))
{
handle_notice (notice);
PQmemfree (notice);
}

handle_result (res);
PQclear (res);
}

handle_query_done (conn);
}

(I've not tried to compile this sample code.)
The SQL query in question is a BEGIN followed by a SELECT (sent in
the same PQsendQuery()).

The problem is that after two iterations in the loop PQisBusy()
returns true, making me exit to the event loop, but no more data is
received, so the function will not be called again. If I disable the
call to PQisBusy() everything works as expected (PQgetResult() will
return NULL on the third iteration). If I move the call to PQisBusy()
outside the loop, everything also works good.

Is it safe to move the call to PQisBusy() outside the loop, or is it
possible that PQgetResult() will block on a long SELECT in that case?
(that would be very bad for me). Or have I misunderstood something
about these functions?
--
Pelle Johansson

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Pelle Johansson (#1)
Re: PQisBusy returns true but no more data is received.

Pelle Johansson <morth@morth.org> writes:

Basically, I have an epoll loop that executes the following code when
I receive data from postgresql (greatly simplified).
...
The problem is that after two iterations in the loop PQisBusy()
returns true, making me exit to the event loop, but no more data is
received, so the function will not be called again.

Shouldn't happen. Can you provide a complete test case? (Are you sure
the problem isn't just the SELECT taking a long time?)

Is it safe to move the call to PQisBusy() outside the loop,

Not if the idea is to not block in PQgetResult.

regards, tom lane