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.

References PQfinish().

Referenced by main().

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

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 51 of file testlibpq2.c.

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

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, "Connection to database failed: %s",
76  PQerrorMessage(conn));
77  exit_nicely(conn);
78  }
79 
80  /* Set always-secure search path, so malicous users can't take control. */
81  res = PQexec(conn,
82  "SELECT pg_catalog.set_config('search_path', '', false)");
83  if (PQresultStatus(res) != PGRES_COMMAND_OK)
84  {
85  fprintf(stderr, "SET failed: %s", PQerrorMessage(conn));
86  PQclear(res);
87  exit_nicely(conn);
88  }
89 
90  /*
91  * Should PQclear PGresult whenever it is no longer needed to avoid memory
92  * leaks
93  */
94  PQclear(res);
95 
96  /*
97  * Issue LISTEN command to enable notifications from the rule's NOTIFY.
98  */
99  res = PQexec(conn, "LISTEN TBL2");
100  if (PQresultStatus(res) != PGRES_COMMAND_OK)
101  {
102  fprintf(stderr, "LISTEN command failed: %s", PQerrorMessage(conn));
103  PQclear(res);
104  exit_nicely(conn);
105  }
106  PQclear(res);
107 
108  /* Quit after four notifies are received. */
109  nnotifies = 0;
110  while (nnotifies < 4)
111  {
112  /*
113  * Sleep until something happens on the connection. We use select(2)
114  * to wait for input, but you could also use poll() or similar
115  * facilities.
116  */
117  int sock;
118  fd_set input_mask;
119 
120  sock = PQsocket(conn);
121 
122  if (sock < 0)
123  break; /* shouldn't happen */
124 
125  FD_ZERO(&input_mask);
126  FD_SET(sock, &input_mask);
127 
128  if (select(sock + 1, &input_mask, NULL, NULL, NULL) < 0)
129  {
130  fprintf(stderr, "select() failed: %s\n", strerror(errno));
131  exit_nicely(conn);
132  }
133 
134  /* Now check for input */
135  PQconsumeInput(conn);
136  while ((notify = PQnotifies(conn)) != NULL)
137  {
138  fprintf(stderr,
139  "ASYNC NOTIFY of '%s' received from backend PID %d\n",
140  notify->relname, notify->be_pid);
141  PQfreemem(notify);
142  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:6116
PGnotify * PQnotifies(PGconn *conn)
Definition: fe-exec.c:2244
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3638
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
PGconn * conn
Definition: streamutil.c:55
int be_pid
Definition: libpq-fe.h:164
#define select(n, r, w, e, timeout)
Definition: win32_port.h:447
char * relname
Definition: libpq-fe.h:163
int PQconsumeInput(PGconn *conn)
Definition: fe-exec.c:1682
void PQclear(PGresult *res)
Definition: fe-exec.c:671
static void exit_nicely(PGconn *conn)
Definition: testlibpq2.c:44
const char * strerror(int errnum)
Definition: strerror.c:19
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6063
void PQfreemem(void *ptr)
Definition: fe-exec.c:3251
int PQsocket(const PGconn *conn)
Definition: fe-connect.c:6134
PGconn * PQconnectdb(const char *conninfo)
Definition: fe-connect.c:592