1 /*-------------------------------------------------------------------------
3 * pg_isready --- checks the status of the PostgreSQL server
5 * Copyright (c) 2013-2021, PostgreSQL Global Development Group
7 * src/bin/scripts/pg_isready.c
9 *-------------------------------------------------------------------------
12 #include "postgres_fe.h"
14 #include "common/logging.h"
15 #include "fe_utils/option_utils.h"
17 #define DEFAULT_CONNECT_TIMEOUT "3"
20 help(const char *progname
);
23 main(int argc
, char **argv
)
29 const char *pghost
= NULL
;
30 const char *pgport
= NULL
;
31 const char *pguser
= NULL
;
32 const char *pgdbname
= NULL
;
33 const char *connect_timeout
= DEFAULT_CONNECT_TIMEOUT
;
35 const char *pghost_str
= NULL
;
36 const char *pghostaddr_str
= NULL
;
37 const char *pgport_str
= NULL
;
39 #define PARAMS_ARRAY_SIZE 7
41 const char *keywords
[PARAMS_ARRAY_SIZE
];
42 const char *values
[PARAMS_ARRAY_SIZE
];
47 PQconninfoOption
*opts
= NULL
;
48 PQconninfoOption
*defs
= NULL
;
49 PQconninfoOption
*opt
;
50 PQconninfoOption
*def
;
54 * We accept user and database as options to avoid useless errors from
55 * connecting with invalid params
58 static struct option long_options
[] = {
59 {"dbname", required_argument
, NULL
, 'd'},
60 {"host", required_argument
, NULL
, 'h'},
61 {"port", required_argument
, NULL
, 'p'},
62 {"quiet", no_argument
, NULL
, 'q'},
63 {"timeout", required_argument
, NULL
, 't'},
64 {"username", required_argument
, NULL
, 'U'},
68 pg_logging_init(argv
[0]);
69 progname
= get_progname(argv
[0]);
70 set_pglocale_pgservice(argv
[0], PG_TEXTDOMAIN("pgscripts"));
71 handle_help_version_opts(argc
, argv
, progname
, help
);
73 while ((c
= getopt_long(argc
, argv
, "d:h:p:qt:U:", long_options
, NULL
)) != -1)
78 pgdbname
= pg_strdup(optarg
);
81 pghost
= pg_strdup(optarg
);
84 pgport
= pg_strdup(optarg
);
90 connect_timeout
= pg_strdup(optarg
);
93 pguser
= pg_strdup(optarg
);
96 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
99 * We need to make sure we don't return 1 here because someone
100 * checking the return code might infer unintended meaning
102 exit(PQPING_NO_ATTEMPT
);
108 pg_log_error("too many command-line arguments (first is \"%s\")",
110 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
113 * We need to make sure we don't return 1 here because someone
114 * checking the return code might infer unintended meaning
116 exit(PQPING_NO_ATTEMPT
);
119 keywords
[0] = "host";
121 keywords
[1] = "port";
123 keywords
[2] = "user";
125 keywords
[3] = "dbname";
126 values
[3] = pgdbname
;
127 keywords
[4] = "connect_timeout";
128 values
[4] = connect_timeout
;
129 keywords
[5] = "fallback_application_name";
130 values
[5] = progname
;
135 * Get the host and port so we can display them in our output
138 (strncmp(pgdbname
, "postgresql://", 13) == 0 ||
139 strncmp(pgdbname
, "postgres://", 11) == 0 ||
140 strchr(pgdbname
, '=') != NULL
))
142 opts
= PQconninfoParse(pgdbname
, &errmsg
);
145 pg_log_error("%s", errmsg
);
146 exit(PQPING_NO_ATTEMPT
);
150 defs
= PQconndefaults();
153 pg_log_error("could not fetch default options");
154 exit(PQPING_NO_ATTEMPT
);
157 for (opt
= opts
, def
= defs
; def
->keyword
; def
++)
159 if (strcmp(def
->keyword
, "host") == 0)
162 pghost_str
= opt
->val
;
166 pghost_str
= def
->val
;
168 pghost_str
= DEFAULT_PGSOCKET_DIR
;
170 else if (strcmp(def
->keyword
, "hostaddr") == 0)
173 pghostaddr_str
= opt
->val
;
175 pghostaddr_str
= def
->val
;
177 else if (strcmp(def
->keyword
, "port") == 0)
180 pgport_str
= opt
->val
;
184 pgport_str
= def
->val
;
191 rv
= PQpingParams(keywords
, values
, 1);
196 pghostaddr_str
!= NULL
? pghostaddr_str
: pghost_str
,
202 printf(_("accepting connections\n"));
205 printf(_("rejecting connections\n"));
207 case PQPING_NO_RESPONSE
:
208 printf(_("no response\n"));
210 case PQPING_NO_ATTEMPT
:
211 printf(_("no attempt\n"));
214 printf(_("unknown\n"));
222 help(const char *progname
)
224 printf(_("%s issues a connection check to a PostgreSQL database.\n\n"), progname
);
225 printf(_("Usage:\n"));
226 printf(_(" %s [OPTION]...\n"), progname
);
228 printf(_("\nOptions:\n"));
229 printf(_(" -d, --dbname=DBNAME database name\n"));
230 printf(_(" -q, --quiet run quietly\n"));
231 printf(_(" -V, --version output version information, then exit\n"));
232 printf(_(" -?, --help show this help, then exit\n"));
234 printf(_("\nConnection options:\n"));
235 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
236 printf(_(" -p, --port=PORT database server port\n"));
237 printf(_(" -t, --timeout=SECS seconds to wait when attempting connection, 0 disables (default: %s)\n"), DEFAULT_CONNECT_TIMEOUT
);
238 printf(_(" -U, --username=USERNAME user name to connect as\n"));
239 printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
240 printf(_("%s home page: <%s>\n"), PACKAGE_NAME
, PACKAGE_URL
);