Re: Trouble with PQnotifies() - Mailing list pgsql-general

From seiliki@so-net.net.tw
Subject Re: Trouble with PQnotifies()
Date
Msg-id 20121214100751.4FA00F4883E@m5.so-net.net.tw
Whole thread Raw
In response to Trouble with PQnotifies()  (seiliki@so-net.net.tw)
Responses Re: Trouble with PQnotifies()  (Mark Morgan Lloyd <markMLl.pgsql-general@telemetry.co.uk>)
List pgsql-general
> The following listening worker thread behaves as expected if I insert/delete rows into/from table "t1" in psql
prompt.
>
> My trouble is when the SQL execution worker thread inserts/ deletes rows into/from table "t1", the listening worker
threadthen goes crazy: PQnotifies() always returns NULL which pushes the listening thread to grab all CPU power because
select()returns immediately in every iteration. The weird part is that select() says that there is something available
butPQnotifies() returns NULL. 
>
> ---------
> PGconn *c=/* Take one connection from connection pool */;
> PGresult *result=PQexec(c,"LISTEN NotifyMe");
> PQclear(result);
> fd_set InputMask;
> int sock=PQsocket(c);
> struct timeval TimeOut={1,200000};
> int SelectResult;
> PGnotify *notify;
> int terminated=0;
> while(!terminated){
>     FD_ZERO(&InputMask);
>     FD_SET((unsigned int)sock,&InputMask);
>     SelectResult=select(sock+1,&InputMask,NULL,NULL,&TimeOut);
>     if(SelectResult == SOCKET_ERROR){
>         puts("select() failed:");
>         break;
>     }
>     if(SelectResult == 0)
>         continue;
>     if(!FD_ISSET(sock,&InputMask))
>         continue;
>     PQconsumeInput(c);
>     while((notify=PQnotifies(c)) != NULL){ //here: unstable!
>         if(stricmp(notify->relname,"NotifyMe") == 0)
>             puts("Got notification");
>         PQfreemem(notify);
>     }
> }
> PQfinish(c);

Please ignore this question!

My connection pool implementation seems to have flaw. Somehow and somewhere the connection acquired by a thread is
robbedby other threads. The PGconn  sending "LISTEN NotifyMe" becomes different from the PGconn passed to PQsocket(),
PQconsumeInput(),and/or PQnotifies(). 

Please also pardon me for asking inappropriate questions like this one. As far as I can recall, every issue I
encounteredbefore always finally proved that PostgreSQL is flawless. 

Best Regards,
CN


pgsql-general by date:

Previous
From: "ac@hsk.hk"
Date:
Subject: PostgreSQL contrib 9.2.x
Next
From: Mark Morgan Lloyd
Date:
Subject: Re: Trouble with PQnotifies()