/*
 * pg_cxn.c
 */

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include "libpq-fe.h"

static void pg_connect(const char *conninfo);

int
main(int argc, char **argv)
{
	int		c;
	int		n = 1;
	int		optindex;
	int		i;
	const char *conninfo;

	while ((c = getopt_long(argc, argv, "n:", NULL, &optindex)) != -1)
	{
		switch (c)
		{
			case 'n':
				n = atoi(optarg);
				break;
			default:
				fprintf(stderr, "Usage: pg_cxn [-n count] connstr\n");
				exit(1);
		}
	}
	argv += optind;
	argc -= optind;

	if (argc > 0)
		conninfo = argv[0];
	else
		conninfo = "";

	for (i = 0; i < n; ++i)
		pg_connect(conninfo);

	while (1)
		sleep(3600);

	return 0;
}

static void
pg_connect(const char *conninfo)
{
	PGconn	   *conn;

	/* Make a connection to the database */
	conn = PQconnectdb(conninfo);

	/* Check to see that the backend connection was successfully made */
	if (PQstatus(conn) != CONNECTION_OK)
	{
		fprintf(stderr, "%s", PQerrorMessage(conn));
		exit(1);
	}
}
