PostgreSQL Source Code git master
testlibpq2.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/select.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

◆ exit_nicely()

static void exit_nicely ( PGconn conn)
static

Definition at line 42 of file testlibpq2.c.

43{
45 exit(1);
46}
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4927
exit(1)
PGconn * conn
Definition: streamutil.c:53

References conn, exit(), and PQfinish().

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 49 of file testlibpq2.c.

50{
51 const char *conninfo;
52 PGconn *conn;
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 */
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);
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));
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}
#define fprintf(file, fmt, msg)
Definition: cubescan.l:21
PGconn * PQconnectdb(const char *conninfo)
Definition: fe-connect.c:753
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:7193
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7256
int PQsocket(const PGconn *conn)
Definition: fe-connect.c:7282
void PQfreemem(void *ptr)
Definition: fe-exec.c:4032
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3411
int PQconsumeInput(PGconn *conn)
Definition: fe-exec.c:1984
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2262
PGnotify * PQnotifies(PGconn *conn)
Definition: fe-exec.c:2667
@ CONNECTION_OK
Definition: libpq-fe.h:81
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:120
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:123
#define strerror
Definition: port.h:251
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
#define select(n, r, w, e, timeout)
Definition: win32_port.h:511

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