Skip to content

Commit 80e05a0

Browse files
author
Amit Kapila
committed
Add the support for '-f' option in dropdb utility.
Specifying '-f' will add the 'force' option to the DROP DATABASE command sent to the server. This will try to terminate all existing connections to the target database before dropping it. Author: Pavel Stehule Reviewed-by: Vignesh C and Amit Kapila Discussion: https://postgr.es/m/CAP_rwwmLJJbn70vLOZFpxGw3XD7nLB_7+NKz46H5EOO2k5H7OQ@mail.gmail.com
1 parent eecf963 commit 80e05a0

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

doc/src/sgml/ref/dropdb.sgml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ PostgreSQL documentation
8686
</listitem>
8787
</varlistentry>
8888

89+
<varlistentry>
90+
<term><option>-f</option></term>
91+
<term><option>--force</option></term>
92+
<listitem>
93+
<para>
94+
Attempt to terminate all existing connections to the target database
95+
before dropping it. See <xref linkend="sql-dropdatabase"/> for more
96+
information on this option.
97+
</para>
98+
</listitem>
99+
</varlistentry>
100+
89101
<varlistentry>
90102
<term><option>-V</option></term>
91103
<term><option>--version</option></term>

src/bin/scripts/dropdb.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ main(int argc, char *argv[])
3434
{"interactive", no_argument, NULL, 'i'},
3535
{"if-exists", no_argument, &if_exists, 1},
3636
{"maintenance-db", required_argument, NULL, 2},
37+
{"force", no_argument, NULL, 'f'},
3738
{NULL, 0, NULL, 0}
3839
};
3940

@@ -49,6 +50,7 @@ main(int argc, char *argv[])
4950
enum trivalue prompt_password = TRI_DEFAULT;
5051
bool echo = false;
5152
bool interactive = false;
53+
bool force = false;
5254

5355
PQExpBufferData sql;
5456

@@ -61,7 +63,7 @@ main(int argc, char *argv[])
6163

6264
handle_help_version_opts(argc, argv, "dropdb", help);
6365

64-
while ((c = getopt_long(argc, argv, "h:p:U:wWei", long_options, &optindex)) != -1)
66+
while ((c = getopt_long(argc, argv, "h:p:U:wWeif", long_options, &optindex)) != -1)
6567
{
6668
switch (c)
6769
{
@@ -86,6 +88,9 @@ main(int argc, char *argv[])
8688
case 'i':
8789
interactive = true;
8890
break;
91+
case 'f':
92+
force = true;
93+
break;
8994
case 0:
9095
/* this covers the long options */
9196
break;
@@ -123,8 +128,10 @@ main(int argc, char *argv[])
123128

124129
initPQExpBuffer(&sql);
125130

126-
appendPQExpBuffer(&sql, "DROP DATABASE %s%s;",
127-
(if_exists ? "IF EXISTS " : ""), fmtId(dbname));
131+
appendPQExpBuffer(&sql, "DROP DATABASE %s%s%s;",
132+
(if_exists ? "IF EXISTS " : ""),
133+
fmtId(dbname),
134+
force ? " WITH (FORCE)" : "");
128135

129136
/* Avoid trying to drop postgres db while we are connected to it. */
130137
if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
@@ -159,6 +166,7 @@ help(const char *progname)
159166
printf(_("\nOptions:\n"));
160167
printf(_(" -e, --echo show the commands being sent to the server\n"));
161168
printf(_(" -i, --interactive prompt before deleting anything\n"));
169+
printf(_(" -f, --force try to terminate other connections before dropping\n"));
162170
printf(_(" -V, --version output version information, then exit\n"));
163171
printf(_(" --if-exists don't report error if database doesn't exist\n"));
164172
printf(_(" -?, --help show this help, then exit\n"));

src/bin/scripts/t/050_dropdb.pl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use PostgresNode;
55
use TestLib;
6-
use Test::More tests => 11;
6+
use Test::More tests => 13;
77

88
program_help_ok('dropdb');
99
program_version_ok('dropdb');
@@ -19,5 +19,11 @@
1919
qr/statement: DROP DATABASE foobar1/,
2020
'SQL DROP DATABASE run');
2121

22+
$node->safe_psql('postgres', 'CREATE DATABASE foobar2');
23+
$node->issues_sql_like(
24+
[ 'dropdb', '--force', 'foobar2' ],
25+
qr/statement: DROP DATABASE foobar2 WITH \(FORCE\);/,
26+
'SQL DROP DATABASE (FORCE) run');
27+
2228
$node->command_fails([ 'dropdb', 'nonexistent' ],
2329
'fails with nonexistent database');

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy