PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
testlibpq2.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/types.h>
#include "libpq-fe.h"
Include dependency graph for testlibpq2.c:

Go to the source code of this file.

Functions

static void exit_nicely (PGconn *conn)
 
int main (int argc, char **argv)
 

Function Documentation

static void exit_nicely ( PGconn conn)
static

Definition at line 44 of file testlibpq2.c.

References PQfinish().

Referenced by main().

45 {
46  PQfinish(conn);
47  exit(1);
48 }
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3516
int main ( int  argc,
char **  argv 
)

Definition at line 51 of file testlibpq2.c.

References pgNotify::be_pid, conn, CONNECTION_OK, exit_nicely(), NULL, PGRES_COMMAND_OK, PQclear(), PQconnectdb(), PQconsumeInput(), PQerrorMessage(), PQexec(), PQfinish(), PQfreemem(), PQnotifies(), PQresultStatus(), PQsocket(), PQstatus(), pgNotify::relname, select, and strerror().

52 {
53  const char *conninfo;
54  PGconn *conn;
55  PGresult *res;
56  PGnotify *notify;
57  int nnotifies;
58 
59  /*
60  * If the user supplies a parameter on the command line, use it as the
61  * conninfo string; otherwise default to setting dbname=postgres and using
62  * environment variables or defaults for all other connection parameters.
63  */
64  if (argc > 1)
65  conninfo = argv[1];
66  else
67  conninfo = "dbname = postgres";
68 
69  /* Make a connection to the database */
70  conn = PQconnectdb(conninfo);
71 
72  /* Check to see that the backend connection was successfully made */
73  if (PQstatus(conn) != CONNECTION_OK)
74  {
75  fprintf(stderr, "Connection to database failed: %s",
76  PQerrorMessage(conn));
77  exit_nicely(conn);
78  }
79 
80  /*
81  * Issue LISTEN command to enable notifications from the rule's NOTIFY.
82  */
83  res = PQexec(conn, "LISTEN TBL2");
84  if (PQresultStatus(res) != PGRES_COMMAND_OK)
85  {
86  fprintf(stderr, "LISTEN command failed: %s", PQerrorMessage(conn));
87  PQclear(res);
88  exit_nicely(conn);
89  }
90 
91  /*
92  * should PQclear PGresult whenever it is no longer needed to avoid memory
93  * leaks
94  */
95  PQclear(res);
96 
97  /* Quit after four notifies are received. */
98  nnotifies = 0;
99  while (nnotifies < 4)
100  {
101  /*
102  * Sleep until something happens on the connection. We use select(2)
103  * to wait for input, but you could also use poll() or similar
104  * facilities.
105  */
106  int sock;
107  fd_set input_mask;
108 
109  sock = PQsocket(conn);
110 
111  if (sock < 0)
112  break; /* shouldn't happen */
113 
114  FD_ZERO(&input_mask);
115  FD_SET(sock, &input_mask);
116 
117  if (select(sock + 1, &input_mask, NULL, NULL, NULL) < 0)
118  {
119  fprintf(stderr, "select() failed: %s\n", strerror(errno));
120  exit_nicely(conn);
121  }
122 
123  /* Now check for input */
124  PQconsumeInput(conn);
125  while ((notify = PQnotifies(conn)) != NULL)
126  {
127  fprintf(stderr,
128  "ASYNC NOTIFY of '%s' received from backend PID %d\n",
129  notify->relname, notify->be_pid);
130  PQfreemem(notify);
131  nnotifies++;
132  }
133  }
134 
135  fprintf(stderr, "Done.\n");
136 
137  /* close the connection to the database and cleanup */
138  PQfinish(conn);
139 
140  return 0;
141 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5959
PGnotify * PQnotifies(PGconn *conn)
Definition: fe-exec.c:2193
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3516
#define select(n, r, w, e, timeout)
Definition: win32.h:384
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
PGconn * conn
Definition: streamutil.c:42
int be_pid
Definition: libpq-fe.h:164
char * relname
Definition: libpq-fe.h:163
int PQconsumeInput(PGconn *conn)
Definition: fe-exec.c:1631
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define NULL
Definition: c.h:226
static void exit_nicely(PGconn *conn)
Definition: testlibpq2.c:44
const char * strerror(int errnum)
Definition: strerror.c:19
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:5906
void PQfreemem(void *ptr)
Definition: fe-exec.c:3200
int PQsocket(const PGconn *conn)
Definition: fe-connect.c:5977
PGconn * PQconnectdb(const char *conninfo)
Definition: fe-connect.c:526