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

45 {
46  PQfinish(conn);
47  exit(1);
48 }
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4257
exit(1)
PGconn * conn
Definition: streamutil.c:54

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

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 51 of file testlibpq2.c.

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, "%s", PQerrorMessage(conn));
77  }
78 
79  /* Set always-secure search path, so malicious users can't take control. */
80  res = PQexec(conn,
81  "SELECT pg_catalog.set_config('search_path', '', false)");
83  {
84  fprintf(stderr, "SET failed: %s", PQerrorMessage(conn));
85  PQclear(res);
87  }
88 
89  /*
90  * Should PQclear PGresult whenever it is no longer needed to avoid memory
91  * leaks
92  */
93  PQclear(res);
94 
95  /*
96  * Issue LISTEN command to enable notifications from the rule's NOTIFY.
97  */
98  res = PQexec(conn, "LISTEN TBL2");
100  {
101  fprintf(stderr, "LISTEN command failed: %s", PQerrorMessage(conn));
102  PQclear(res);
103  exit_nicely(conn);
104  }
105  PQclear(res);
106 
107  /* Quit after four notifies are received. */
108  nnotifies = 0;
109  while (nnotifies < 4)
110  {
111  /*
112  * Sleep until something happens on the connection. We use select(2)
113  * to wait for input, but you could also use poll() or similar
114  * facilities.
115  */
116  int sock;
117  fd_set input_mask;
118 
119  sock = PQsocket(conn);
120 
121  if (sock < 0)
122  break; /* shouldn't happen */
123 
124  FD_ZERO(&input_mask);
125  FD_SET(sock, &input_mask);
126 
127  if (select(sock + 1, &input_mask, NULL, NULL, NULL) < 0)
128  {
129  fprintf(stderr, "select() failed: %s\n", strerror(errno));
130  exit_nicely(conn);
131  }
132 
133  /* Now check for input */
135  while ((notify = PQnotifies(conn)) != NULL)
136  {
137  fprintf(stderr,
138  "ASYNC NOTIFY of '%s' received from backend PID %d\n",
139  notify->relname, notify->be_pid);
140  PQfreemem(notify);
141  nnotifies++;
143  }
144  }
145 
146  fprintf(stderr, "Done.\n");
147 
148  /* close the connection to the database and cleanup */
149  PQfinish(conn);
150 
151  return 0;
152 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6770
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6717
PGconn * PQconnectdb(const char *conninfo)
Definition: fe-connect.c:713
int PQsocket(const PGconn *conn)
Definition: fe-connect.c:6796
void PQfreemem(void *ptr)
Definition: fe-exec.c:3796
PGnotify * PQnotifies(PGconn *conn)
Definition: fe-exec.c:2516
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3178
void PQclear(PGresult *res)
Definition: fe-exec.c:694
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2193
int PQconsumeInput(PGconn *conn)
Definition: fe-exec.c:1904
@ CONNECTION_OK
Definition: libpq-fe.h:58
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:95
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:98
#define strerror
Definition: port.h:235
#define fprintf
Definition: port.h:226
int be_pid
Definition: libpq-fe.h:188
char * relname
Definition: libpq-fe.h:187
static void exit_nicely(PGconn *conn)
Definition: testlibpq2.c:44
#define select(n, r, w, e, timeout)
Definition: win32_port.h:474

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.