Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8730618

Browse files
committed
pg_ctl: Add idempotent option
This changes the behavior of the start and stop actions to exit successfully if the server was already started or stopped. This changes the default behavior of the start action: Before, if the server was already running, it would print a message and succeed. Now, that situation will result in an error. When running in idempotent mode, no message is printed and pg_ctl exits successfully. It was considered to just make the idempotent behavior the default and only option, but pg_upgrade needs the old behavior.
1 parent ba66752 commit 8730618

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

contrib/start-scripts/linux

+3-3
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,17 @@ case $1 in
8484
echo -n "Starting PostgreSQL: "
8585
test x"$OOM_SCORE_ADJ" != x && echo "$OOM_SCORE_ADJ" > /proc/self/oom_score_adj
8686
test x"$OOM_ADJ" != x && echo "$OOM_ADJ" > /proc/self/oom_adj
87-
su - $PGUSER -c "$DAEMON -D '$PGDATA' &" >>$PGLOG 2>&1
87+
su - $PGUSER -c "$DAEMON -I -D '$PGDATA' &" >>$PGLOG 2>&1
8888
echo "ok"
8989
;;
9090
stop)
9191
echo -n "Stopping PostgreSQL: "
92-
su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s -m fast"
92+
su - $PGUSER -c "$PGCTL stop -I -D '$PGDATA' -s -m fast"
9393
echo "ok"
9494
;;
9595
restart)
9696
echo -n "Restarting PostgreSQL: "
97-
su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s -m fast -w"
97+
su - $PGUSER -c "$PGCTL stop -I -D '$PGDATA' -s -m fast -w"
9898
test x"$OOM_SCORE_ADJ" != x && echo "$OOM_SCORE_ADJ" > /proc/self/oom_score_adj
9999
test x"$OOM_ADJ" != x && echo "$OOM_ADJ" > /proc/self/oom_adj
100100
su - $PGUSER -c "$DAEMON -D '$PGDATA' &" >>$PGLOG 2>&1

doc/src/sgml/ref/pg_ctl-ref.sgml

+21
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ PostgreSQL documentation
3939
<arg choice="opt"><option>-o</option> <replaceable>options</replaceable></arg>
4040
<arg choice="opt"><option>-p</option> <replaceable>path</replaceable></arg>
4141
<arg choice="opt"><option>-c</option></arg>
42+
<arg choice="opt"><option>-I</option></arg>
4243
</cmdsynopsis>
4344

4445
<cmdsynopsis>
@@ -55,6 +56,7 @@ PostgreSQL documentation
5556
<arg choice="plain"><option>i[mmediate]</option></arg>
5657
</group>
5758
</arg>
59+
<arg choice="opt"><option>-I</option></arg>
5860
</cmdsynopsis>
5961

6062
<cmdsynopsis>
@@ -270,6 +272,25 @@ PostgreSQL documentation
270272
</listitem>
271273
</varlistentry>
272274

275+
<varlistentry>
276+
<term><option>-I</option></term>
277+
<term><option>--idempotent</option></term>
278+
<listitem>
279+
<para>
280+
When used with the <literal>start</literal> or <literal>stop</literal>
281+
actions, return exit code 0 if the server is already running or
282+
stopped, respectively. Otherwise, an error is raised and a nonzero
283+
exit code is returned in these cases.
284+
</para>
285+
286+
<para>
287+
This option is useful for System-V-style init scripts, which require
288+
the <literal>start</literal> and <literal>stop</literal> actions to be
289+
idempotent.
290+
</para>
291+
</listitem>
292+
</varlistentry>
293+
273294
<varlistentry>
274295
<term><option>-l <replaceable class="parameter">filename</replaceable></option></term>
275296
<term><option>--log <replaceable class="parameter">filename</replaceable></option></term>

src/bin/pg_ctl/pg_ctl.c

+22-7
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ static char *pg_config = NULL;
8585
static char *pgdata_opt = NULL;
8686
static char *post_opts = NULL;
8787
static const char *progname;
88+
static bool idempotent = false;
8889
static char *log_file = NULL;
8990
static char *exec_path = NULL;
9091
static char *register_servicename = "PostgreSQL"; /* FIXME: + version ID? */
@@ -773,9 +774,15 @@ do_start(void)
773774
{
774775
old_pid = get_pgpid();
775776
if (old_pid != 0)
776-
write_stderr(_("%s: another server might be running; "
777-
"trying to start server anyway\n"),
778-
progname);
777+
{
778+
if (idempotent)
779+
exit(0);
780+
else
781+
{
782+
write_stderr(_("%s: another server might be running\n"), progname);
783+
exit(1);
784+
}
785+
}
779786
}
780787

781788
read_post_opts();
@@ -859,6 +866,8 @@ do_stop(void)
859866

860867
if (pid == 0) /* no pid file */
861868
{
869+
if (idempotent)
870+
exit(0);
862871
write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file);
863872
write_stderr(_("Is server running?\n"));
864873
exit(1);
@@ -1763,9 +1772,9 @@ do_help(void)
17631772
printf(_("%s is a utility to initialize, start, stop, or control a PostgreSQL server.\n\n"), progname);
17641773
printf(_("Usage:\n"));
17651774
printf(_(" %s init[db] [-D DATADIR] [-s] [-o \"OPTIONS\"]\n"), progname);
1766-
printf(_(" %s start [-w] [-t SECS] [-D DATADIR] [-s] [-l FILENAME] [-o \"OPTIONS\"]\n"), progname);
1767-
printf(_(" %s stop [-W] [-t SECS] [-D DATADIR] [-s] [-m SHUTDOWN-MODE]\n"), progname);
1768-
printf(_(" %s restart [-w] [-t SECS] [-D DATADIR] [-s] [-m SHUTDOWN-MODE]\n"
1775+
printf(_(" %s start [-w] [-t SECS] [-D DATADIR] [-s] [-I] [-l FILENAME] [-o \"OPTIONS\"]\n"), progname);
1776+
printf(_(" %s stop [-W] [-t SECS] [-D DATADIR] [-s] [-I] [-m SHUTDOWN-MODE]\n"), progname);
1777+
printf(_(" %s restart [-w] [-t SECS] [-D DATADIR] [-s] [-m SHUTDOWN-MODE]\n"
17691778
" [-o \"OPTIONS\"]\n"), progname);
17701779
printf(_(" %s reload [-D DATADIR] [-s]\n"), progname);
17711780
printf(_(" %s status [-D DATADIR]\n"), progname);
@@ -1798,6 +1807,8 @@ do_help(void)
17981807
printf(_(" -o OPTIONS command line options to pass to postgres\n"
17991808
" (PostgreSQL server executable) or initdb\n"));
18001809
printf(_(" -p PATH-TO-POSTGRES normally not necessary\n"));
1810+
printf(_("\nOptions for start or stop:\n"));
1811+
printf(_(" -I, --idempotent don't error if server already running or stopped\n"));
18011812
printf(_("\nOptions for stop, restart, or promote:\n"));
18021813
printf(_(" -m, --mode=MODE MODE can be \"smart\", \"fast\", or \"immediate\"\n"));
18031814

@@ -1980,6 +1991,7 @@ main(int argc, char **argv)
19801991
{"silent", no_argument, NULL, 's'},
19811992
{"timeout", required_argument, NULL, 't'},
19821993
{"core-files", no_argument, NULL, 'c'},
1994+
{"idempotent", no_argument, NULL, 'I'},
19831995
{NULL, 0, NULL, 0}
19841996
};
19851997

@@ -2045,7 +2057,7 @@ main(int argc, char **argv)
20452057
/* process command-line options */
20462058
while (optind < argc)
20472059
{
2048-
while ((c = getopt_long(argc, argv, "cD:l:m:N:o:p:P:sS:t:U:wW", long_options, &option_index)) != -1)
2060+
while ((c = getopt_long(argc, argv, "cD:Il:m:N:o:p:P:sS:t:U:wW", long_options, &option_index)) != -1)
20492061
{
20502062
switch (c)
20512063
{
@@ -2071,6 +2083,9 @@ main(int argc, char **argv)
20712083
pgdata_D);
20722084
break;
20732085
}
2086+
case 'I':
2087+
idempotent = true;
2088+
break;
20742089
case 'l':
20752090
log_file = pg_strdup(optarg);
20762091
break;

0 commit comments

Comments
 (0)