Included is a example program appears in our docs (libpq.sgml). 
As you can see, the very last part of the program:

    PQfinish(conn);

    return 0;

never execute. Should we remove them?
--
Tatsuo Ishii


main()
{
    char       *pghost,
               *pgport,
               *pgoptions,
               *pgtty;
    char       *dbName;
    int         nFields;
    int         i,
                j;

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

    /*
     * begin, by setting the parameters for a backend connection if the
     * parameters are null, then the system will try to use reasonable
     * defaults by looking up environment variables or, failing that,
     * using hardwired constants
     */
    pghost = NULL;              /* host name of the backend server */
    pgport = NULL;              /* port of the backend server */
    pgoptions = NULL;           /* special options to start up the backend
                                 * server */
    pgtty = NULL;               /* debugging tty for the backend server */
    dbName = getenv("USER");    /* change this to the name of your test
                                 * database */

    /* make a connection to the database */
    conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);

    /*
     * check to see that the backend connection was successfully made
     */
    if (PQstatus(conn) == CONNECTION_BAD)
    {
        fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
        fprintf(stderr, "%s", PQerrorMessage(conn));
        exit_nicely(conn);
    }

    res = PQexec(conn, "LISTEN TBL2");
    if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "LISTEN command failed\n");
        PQclear(res);
        exit_nicely(conn);
    }

    /*
     * should PQclear PGresult whenever it is no longer needed to avoid
     * memory leaks
     */
    PQclear(res);

    while (1)
    {

        /*
         * wait a little bit between checks; waiting with select()
         * would be more efficient.
         */
        sleep(1);
        /* collect any asynchronous backend messages */
        PQconsumeInput(conn);
        /* check for asynchronous notify messages */
        while ((notify = PQnotifies(conn)) != NULL)
        {
            fprintf(stderr,
                 "ASYNC NOTIFY of '%s' from backend pid '%d' received\n",
                    notify-&gt;relname, notify-&gt;be_pid);
            free(notify);
        }
    }

    /* close the connection to the database and cleanup */
    PQfinish(conn);

    return 0;
}

Mon, 13 Aug 2001 13:49:15 +0900
Tatsuo Ishii <t-ishii@sra.co.jp>
example program bug?
On Mon, 13 Aug 2001, Tatsuo Ishii wrote:

> Included is a example program appears in our docs (libpq.sgml). 
> As you can see, the very last part of the program:
> 
>     PQfinish(conn);
> 
>     return 0;
> 
> never execute. Should we remove them?

Most compilers should be able to detect this and not generate
warnings. Compilers will default the return type of main() to int so
perhaps for the sake of form it should be left in there with a comment:

	/* we never get here */

By the same line of thinking, PQfinish(conn) may as well stay.

There are other parts of the code which need fixing though. The call to
sleep() will generate a prototyping error on many systems, since it is
defined in unistd.h which is not included from the program in the 7.2
version of the docs (on the Web site). Similar problem with getenv(),
which is defined in stdlib.h.

Gavin



Mon, 13 Aug 2001 18:49:50 +1000 (EST)
Gavin Sherry <swm@linuxworld.com.au>
Re: example program bug?
>> Included is a example program appears in our docs (libpq.sgml). 

If you're going to do any work on that example, how about transforming
it into something useful, ie, a more realistic example of using NOTIFY.
It should be using a select() to wait for input, not a sleep() loop.

As for the cleanup code, leave it in place, and make the sample program
break out of its loop when a specific NOTIFY name is received.

			regards, tom lane

Mon, 13 Aug 2001 09:47:09 -0400
Tom Lane <tgl@sss.pgh.pa.us>
Re: example program bug? 