Add tests for libpq query cancellation APIs
[pgsql.git] / src / test / examples / testlibpq2.c
blob05ce8c3f138c40a391d6ab1bcd08a1628f3e03aa
1 /*
2 * src/test/examples/testlibpq2.c
5 * testlibpq2.c
6 * Test of the asynchronous notification interface
8 * Start this program, then from psql in another window do
9 * NOTIFY TBL2;
10 * Repeat four times to get this program to exit.
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):
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);
23 * Start this program, then from psql do this four times:
25 * INSERT INTO TESTLIBPQ2.TBL1 VALUES (10);
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>
39 #include "libpq-fe.h"
41 static void
42 exit_nicely(PGconn *conn)
44 PQfinish(conn);
45 exit(1);
48 int
49 main(int argc, char **argv)
51 const char *conninfo;
52 PGconn *conn;
53 PGresult *res;
54 PGnotify *notify;
55 int nnotifies;
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.
62 if (argc > 1)
63 conninfo = argv[1];
64 else
65 conninfo = "dbname = postgres";
67 /* Make a connection to the database */
68 conn = PQconnectdb(conninfo);
70 /* Check to see that the backend connection was successfully made */
71 if (PQstatus(conn) != CONNECTION_OK)
73 fprintf(stderr, "%s", PQerrorMessage(conn));
74 exit_nicely(conn);
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)");
80 if (PQresultStatus(res) != PGRES_TUPLES_OK)
82 fprintf(stderr, "SET failed: %s", PQerrorMessage(conn));
83 PQclear(res);
84 exit_nicely(conn);
88 * Should PQclear PGresult whenever it is no longer needed to avoid memory
89 * leaks
91 PQclear(res);
94 * Issue LISTEN command to enable notifications from the rule's NOTIFY.
96 res = PQexec(conn, "LISTEN TBL2");
97 if (PQresultStatus(res) != PGRES_COMMAND_OK)
99 fprintf(stderr, "LISTEN command failed: %s", PQerrorMessage(conn));
100 PQclear(res);
101 exit_nicely(conn);
103 PQclear(res);
105 /* Quit after four notifies are received. */
106 nnotifies = 0;
107 while (nnotifies < 4)
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.
114 int sock;
115 fd_set input_mask;
117 sock = PQsocket(conn);
119 if (sock < 0)
120 break; /* shouldn't happen */
122 FD_ZERO(&input_mask);
123 FD_SET(sock, &input_mask);
125 if (select(sock + 1, &input_mask, NULL, NULL, NULL) < 0)
127 fprintf(stderr, "select() failed: %s\n", strerror(errno));
128 exit_nicely(conn);
131 /* Now check for input */
132 PQconsumeInput(conn);
133 while ((notify = PQnotifies(conn)) != NULL)
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++;
140 PQconsumeInput(conn);
144 fprintf(stderr, "Done.\n");
146 /* close the connection to the database and cleanup */
147 PQfinish(conn);
149 return 0;