/* * startlots.c * * Build with libpq (eg, gcc startlots.c -o startlots -lpq). * * Usage: startlots N to start N backends. Control-C to exit. */ #include #include #include #include "libpq-fe.h" static void exit_nicely(PGconn *conn) { PQfinish(conn); exit(1); } static void start1conn() { char *pghost, *pgport, *pgoptions, *pgtty; char *dbName; PGconn *conn; PGresult *res; /* * begin, by setting the parameters for a backend connection if the * parameters are null, then the system will try to use reasonable * defaults by looking up environment variables or, failing that, * using hardwired constants */ pghost = NULL; /* host name of the backend server */ pgport = NULL; /* port of the backend server */ pgoptions = NULL; /* special options to start up the backend * server */ pgtty = NULL; /* debugging tty for the backend server */ dbName = "template1"; /* make a connection to the database */ conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName); /* check to see that the backend connection was successfully made */ if (PQstatus(conn) == CONNECTION_BAD) { fprintf(stderr, "Connection to database '%s' failed.\n", dbName); fprintf(stderr, "%s", PQerrorMessage(conn)); exit_nicely(conn); } /* run a transaction */ res = PQexec(conn, "select version()"); if (PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, "select version() command didn't return tuples properly\n"); PQclear(res); exit_nicely(conn); } PQclear(res); /* leave the connection open */ } int main(int argc, char**argv) { int nconn = atoi(argv[1]); while (nconn-- > 0) start1conn(); sleep(100000); return 0; }