Re: Should PQconsumeInput/PQisBusy be expensive to use? - Mailing list pgsql-general

From Alex Hunsaker
Subject Re: Should PQconsumeInput/PQisBusy be expensive to use?
Date
Msg-id AANLkTinZSBk4e22d4u750N2br9sCcSQWcVu5tFnHXh++@mail.gmail.com
Whole thread Raw
In response to Should PQconsumeInput/PQisBusy be expensive to use?  (Michael Clark <codingninja@gmail.com>)
List pgsql-general
On Wed, Oct 27, 2010 at 15:02, Michael Clark <codingninja@gmail.com> wrote:
> Hello everyone.
> Upon some investigation I found that not calling PQconsumeInput/PQisBusy
> produces results in line with PQexecParams (which PQexecParams seems to be
> doing under the hood).

> (please keep in mind this is just test code and rather simplistic...)
>     int send_result = PQsendQueryParams(self.db,
>                                         [sql UTF8String],
>                                         i,
>                                         NULL,
>                                         (const char *const *)vals,
>                                         (const int *)lens,
>                                         (const int *)formats,
>                                         kTextResultFormat);
>     int consume_result = 0;
>     int is_busy_result = 0;
>
>     while ( ((consume_result = PQconsumeInput(self.db)) == 1) &&
> ((is_busy_result = PQisBusy(self.db)) == 1) )
>         ;

You really want to select() or equivalent here...  This basically is a
busy loop using 100% cpu; neither PQconsumeInput or PQisBusy do any
kind of sleeping...

Something like:
fd_set read_mask;
int sock = PQsocket(st->con);
FD_ZERO(&read_mask);
FD_SET(sock, &read_mask);

while(1)
{
  struct timeval tv = {5, 0};
  select(sock+1, &read_mask, NULL, NULL, &tv);
  PQconsumeInput(self.db)
  if(!PQisBusy(self.db))
    break;
}

or something...

pgsql-general by date:

Previous
From: Daniel.Crespo@l-3com.com
Date:
Subject: Re: How to merge data from two separate databases into one (maybe using xlogs)?
Next
From: David Wilson
Date:
Subject: Re: Should PQconsumeInput/PQisBusy be expensive to use?