50{
51 const char *conninfo;
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 */
69
70 /* Check to see that the backend connection was successfully made */
72 {
75 }
76
77 /* Set always-secure search path, so malicious users can't take control. */
79 "SELECT pg_catalog.set_config('search_path', '', false)");
81 {
85 }
86
87 /*
88 * Should PQclear PGresult whenever it is no longer needed to avoid memory
89 * leaks
90 */
92
93 /*
94 * Issue LISTEN command to enable notifications from the rule's NOTIFY.
95 */
98 {
102 }
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
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 {
129 }
130
131 /* Now check for input */
134 {
136 "ASYNC NOTIFY of '%s' received from backend PID %d\n",
139 nnotifies++;
141 }
142 }
143
145
146 /* close the connection to the database and cleanup */
148
149 return 0;
150}
#define fprintf(file, fmt, msg)
PGconn * PQconnectdb(const char *conninfo)
ConnStatusType PQstatus(const PGconn *conn)
char * PQerrorMessage(const PGconn *conn)
int PQsocket(const PGconn *conn)
void PQfreemem(void *ptr)
int PQconsumeInput(PGconn *conn)
PGresult * PQexec(PGconn *conn, const char *query)
PGnotify * PQnotifies(PGconn *conn)
static void exit_nicely(PGconn *conn)
#define select(n, r, w, e, timeout)