PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
testlibpq2.c
Go to the documentation of this file.
1 /*
2  * src/test/examples/testlibpq2.c
3  *
4  *
5  * testlibpq2.c
6  * Test of the asynchronous notification interface
7  *
8  * Start this program, then from psql in another window do
9  * NOTIFY TBL2;
10  * Repeat four times to get this program to exit.
11  *
12  * Or, if you want to get fancy, try this:
13  * populate a database with the following commands
14  * (provided in src/test/examples/testlibpq2.sql):
15  *
16  * CREATE TABLE TBL1 (i int4);
17  *
18  * CREATE TABLE TBL2 (i int4);
19  *
20  * CREATE RULE r1 AS ON INSERT TO TBL1 DO
21  * (INSERT INTO TBL2 VALUES (new.i); NOTIFY TBL2);
22  *
23  * and do this four times:
24  *
25  * INSERT INTO TBL1 VALUES (10);
26  */
27 
28 #ifdef WIN32
29 #include <windows.h>
30 #endif
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #ifdef HAVE_SYS_SELECT_H
38 #include <sys/select.h>
39 #endif
40 
41 #include "libpq-fe.h"
42 
43 static void
45 {
46  PQfinish(conn);
47  exit(1);
48 }
49 
50 int
51 main(int argc, char **argv)
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:5934
PGnotify * PQnotifies(PGconn *conn)
Definition: fe-exec.c:2193
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3491
#define select(n, r, w, e, timeout)
Definition: win32.h:374
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
PGconn * conn
Definition: streamutil.c:42
int be_pid
Definition: libpq-fe.h:164
int main(int argc, char **argv)
Definition: testlibpq2.c:51
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:229
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:5881
void PQfreemem(void *ptr)
Definition: fe-exec.c:3200
int PQsocket(const PGconn *conn)
Definition: fe-connect.c:5952
PGconn * PQconnectdb(const char *conninfo)
Definition: fe-connect.c:522