Thread: notify problem

notify problem

From
Alessandro GARDICH
Date:
Hi to all

I have problem with NOTIFY/LISTEN ...
I'm  writing a C++ application with libpqxx, I thought was a problem of
the lib but I try also with a C program using libpq with the same
result, so maybe there is something else of wrong.

Using the example 2 in the libpq chapter on "Postgres Programmer's
Guide" as template i write :

!---------->
#include <unistd.h>
#include <libpq-fe.h>

int main() {

   PGconn *conn;
   PGresult *res;
   PGnotify *notify;

   int i;

   conn = PQconnectdb ("host=localhost user=test dbname=test");

   if (PQstatus(conn) == CONNECTION_BAD) {
      fprintf(stderr,"failed connecting.\n");
      PQfinish(conn);
      exit(1);
   }

   res = PQexec(conn, "LISTEN alert");
   if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
      fprintf(stderr,"failed listening.\n");
      PQclear(res);
      PQfinish(conn);
      exit(1);
   }

   PQclear(res);

   for(i = 0; i < 80 ; ++i) {
      sleep(1);
      PQconsumeInput(conn);
      while ((notify = PQnotifies(conn)) != NULL) {
         fprintf(stderr,"\nGOT NOTIFY!!!\n");
         free(notify);
      }
      fprintf(stderr,".");
   }

   PQfinish(conn);

   return 0;
}
<----------!


On a console with psql I issue some notify with
NOTIFY alert; COMMIT;

but nothing are catched !!!

if in another console with psql i do a "LISTEN alert;" all work
correctly.

I'm using Linux, RedHat 9, so PostgreSQL 7.3.2.
user and database are created with :
createuser -h localhost -U postgres -D -A -P test
createdb -O test -h localhost -U postgres test

and nothing alse, the database are empty.

what's wrong ???

Thanks.


--
Alessandro GARDICH <gremlin@gremlin.it>
gremlin.it

Re: notify problem

From
Tom Lane
Date:
Alessandro GARDICH <gremlin@gremlin.it> writes:
>    res = PQexec(conn, "LISTEN alert");

>    for(i = 0; i < 80 ; ++i) {
>       sleep(1);
>       PQconsumeInput(conn);
>       while ((notify = PQnotifies(conn)) != NULL) {
>          fprintf(stderr,"\nGOT NOTIFY!!!\n");
>          free(notify);
>       }
>       fprintf(stderr,".");
>    }

> On a console with psql I issue some notify with
> NOTIFY alert; COMMIT;
> but nothing are catched !!!

Hm.  Should I guess from the above that you've set autocommit off in
postgresql.conf?  If so, your problem is you didn't commit the LISTEN.
Notifications are not sent to clients that are in open transactions.

            regards, tom lane