PostgreSQL Source Code  git master
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 SCHEMA TESTLIBPQ2;
17  * SET search_path = TESTLIBPQ2;
18  * CREATE TABLE TBL1 (i int4);
19  * CREATE TABLE TBL2 (i int4);
20  * CREATE RULE r1 AS ON INSERT TO TBL1 DO
21  * (INSERT INTO TBL2 VALUES (new.i); NOTIFY TBL2);
22  *
23  * Start this program, then from psql do this four times:
24  *
25  * INSERT INTO TESTLIBPQ2.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/select.h>
36 #include <sys/time.h>
37 #include <sys/types.h>
38 
39 #include "libpq-fe.h"
40 
41 static void
43 {
44  PQfinish(conn);
45  exit(1);
46 }
47 
48 int
49 main(int argc, char **argv)
50 {
51  const char *conninfo;
52  PGconn *conn;
53  PGresult *res;
54  PGnotify *notify;
55  int nnotifies;
56 
57  /*
58  * If the user supplies a parameter on the command line, use it as the
59  * conninfo string; otherwise default to setting dbname=postgres and using
60  * environment variables or defaults for all other connection parameters.
61  */
62  if (argc > 1)
63  conninfo = argv[1];
64  else
65  conninfo = "dbname = postgres";
66 
67  /* Make a connection to the database */
68  conn = PQconnectdb(conninfo);
69 
70  /* Check to see that the backend connection was successfully made */
71  if (PQstatus(conn) != CONNECTION_OK)
72  {
73  fprintf(stderr, "%s", PQerrorMessage(conn));
75  }
76 
77  /* Set always-secure search path, so malicious users can't take control. */
78  res = PQexec(conn,
79  "SELECT pg_catalog.set_config('search_path', '', false)");
81  {
82  fprintf(stderr, "SET failed: %s", PQerrorMessage(conn));
83  PQclear(res);
85  }
86 
87  /*
88  * Should PQclear PGresult whenever it is no longer needed to avoid memory
89  * leaks
90  */
91  PQclear(res);
92 
93  /*
94  * Issue LISTEN command to enable notifications from the rule's NOTIFY.
95  */
96  res = PQexec(conn, "LISTEN TBL2");
98  {
99  fprintf(stderr, "LISTEN command failed: %s", PQerrorMessage(conn));
100  PQclear(res);
101  exit_nicely(conn);
102  }
103  PQclear(res);
104 
105  /* Quit after four notifies are received. */
106  nnotifies = 0;
107  while (nnotifies < 4)
108  {
109  /*
110  * Sleep until something happens on the connection. We use select(2)
111  * to wait for input, but you could also use poll() or similar
112  * facilities.
113  */
114  int sock;
115  fd_set input_mask;
116 
117  sock = PQsocket(conn);
118 
119  if (sock < 0)
120  break; /* shouldn't happen */
121 
122  FD_ZERO(&input_mask);
123  FD_SET(sock, &input_mask);
124 
125  if (select(sock + 1, &input_mask, NULL, NULL, NULL) < 0)
126  {
127  fprintf(stderr, "select() failed: %s\n", strerror(errno));
128  exit_nicely(conn);
129  }
130 
131  /* Now check for input */
133  while ((notify = PQnotifies(conn)) != NULL)
134  {
135  fprintf(stderr,
136  "ASYNC NOTIFY of '%s' received from backend PID %d\n",
137  notify->relname, notify->be_pid);
138  PQfreemem(notify);
139  nnotifies++;
141  }
142  }
143 
144  fprintf(stderr, "Done.\n");
145 
146  /* close the connection to the database and cleanup */
147  PQfinish(conn);
148 
149  return 0;
150 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7212
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:7149
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4893
PGconn * PQconnectdb(const char *conninfo)
Definition: fe-connect.c:745
int PQsocket(const PGconn *conn)
Definition: fe-connect.c:7238
void PQfreemem(void *ptr)
Definition: fe-exec.c:4032
PGnotify * PQnotifies(PGconn *conn)
Definition: fe-exec.c:2667
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3411
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2262
int PQconsumeInput(PGconn *conn)
Definition: fe-exec.c:1984
@ CONNECTION_OK
Definition: libpq-fe.h:81
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:120
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:123
exit(1)
#define strerror
Definition: port.h:251
#define fprintf
Definition: port.h:242
PGconn * conn
Definition: streamutil.c:55
int be_pid
Definition: libpq-fe.h:219
char * relname
Definition: libpq-fe.h:218
static void exit_nicely(PGconn *conn)
Definition: testlibpq2.c:42
int main(int argc, char **argv)
Definition: testlibpq2.c:49
#define select(n, r, w, e, timeout)
Definition: win32_port.h:513