Skip to content

Commit 5b352d8

Browse files
committed
DROP DATABASE IF EXISTS variant
1 parent 179211a commit 5b352d8

File tree

8 files changed

+51
-14
lines changed

8 files changed

+51
-14
lines changed

doc/src/sgml/ref/drop_database.sgml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/drop_database.sgml,v 1.20 2005/06/21 04:02:31 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/drop_database.sgml,v 1.21 2005/11/22 15:24:17 adunstan Exp $
33
PostgreSQL documentation
44
-->
55

@@ -20,7 +20,7 @@ PostgreSQL documentation
2020

2121
<refsynopsisdiv>
2222
<synopsis>
23-
DROP DATABASE <replaceable class="PARAMETER">name</replaceable>
23+
DROP DATABASE [ IF EXISTS ] <replaceable class="PARAMETER">name</replaceable>
2424
</synopsis>
2525
</refsynopsisdiv>
2626

@@ -45,6 +45,16 @@ DROP DATABASE <replaceable class="PARAMETER">name</replaceable>
4545
<title>Parameters</title>
4646

4747
<variablelist>
48+
<varlistentry>
49+
<term><literal>IF EXISTS</literal></term>
50+
<listitem>
51+
<para>
52+
Do not throw an error if the database does not exist. A notice is issued
53+
in this case.
54+
</para>
55+
</listitem>
56+
</varlistentry>
57+
4858
<varlistentry>
4959
<term><replaceable class="PARAMETER">name</replaceable></term>
5060
<listitem>

src/backend/commands/dbcommands.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
*
1717
* IDENTIFICATION
18-
* $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.173 2005/10/15 02:49:15 momjian Exp $
18+
* $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.174 2005/11/22 15:24:17 adunstan Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -551,7 +551,7 @@ createdb(const CreatedbStmt *stmt)
551551
* DROP DATABASE
552552
*/
553553
void
554-
dropdb(const char *dbname)
554+
dropdb(const char *dbname, bool missing_ok)
555555
{
556556
Oid db_id;
557557
bool db_istemplate;
@@ -585,9 +585,25 @@ dropdb(const char *dbname)
585585

586586
if (!get_db_info(dbname, &db_id, NULL, NULL,
587587
&db_istemplate, NULL, NULL, NULL, NULL, NULL))
588-
ereport(ERROR,
588+
{
589+
if (! missing_ok)
590+
{
591+
ereport(ERROR,
589592
(errcode(ERRCODE_UNDEFINED_DATABASE),
590593
errmsg("database \"%s\" does not exist", dbname)));
594+
}
595+
else
596+
{
597+
598+
/* Close pg_database, release the lock, since we changed nothing */
599+
heap_close(pgdbrel, ExclusiveLock);
600+
ereport(NOTICE,
601+
(errmsg("database \"%s\" does not exist, skipping",
602+
dbname)));
603+
604+
return;
605+
}
606+
}
591607

592608
if (!pg_database_ownercheck(db_id, GetUserId()))
593609
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE,

src/backend/nodes/copyfuncs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Portions Copyright (c) 1994, Regents of the University of California
1616
*
1717
* IDENTIFICATION
18-
* $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.319 2005/11/21 12:49:31 alvherre Exp $
18+
* $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.320 2005/11/22 15:24:17 adunstan Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -2261,6 +2261,7 @@ _copyDropdbStmt(DropdbStmt *from)
22612261
DropdbStmt *newnode = makeNode(DropdbStmt);
22622262

22632263
COPY_STRING_FIELD(dbname);
2264+
COPY_SCALAR_FIELD(missing_ok);
22642265

22652266
return newnode;
22662267
}

src/backend/nodes/equalfuncs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Portions Copyright (c) 1994, Regents of the University of California
1919
*
2020
* IDENTIFICATION
21-
* $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.256 2005/11/21 12:49:31 alvherre Exp $
21+
* $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.257 2005/11/22 15:24:17 adunstan Exp $
2222
*
2323
*-------------------------------------------------------------------------
2424
*/
@@ -1188,6 +1188,7 @@ static bool
11881188
_equalDropdbStmt(DropdbStmt *a, DropdbStmt *b)
11891189
{
11901190
COMPARE_STRING_FIELD(dbname);
1191+
COMPARE_SCALAR_FIELD(missing_ok);
11911192

11921193
return true;
11931194
}

src/backend/parser/gram.y

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.514 2005/11/21 12:49:31 alvherre Exp $
14+
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.515 2005/11/22 15:24:17 adunstan Exp $
1515
*
1616
* HISTORY
1717
* AUTHOR DATE MAJOR EVENT
@@ -4698,7 +4698,7 @@ alterdb_opt_item:
46984698

46994699
/*****************************************************************************
47004700
*
4701-
* DROP DATABASE
4701+
* DROP DATABASE [ IF EXISTS ]
47024702
*
47034703
* This is implicitly CASCADE, no need for drop behavior
47044704
*****************************************************************************/
@@ -4707,6 +4707,14 @@ DropdbStmt: DROP DATABASE database_name
47074707
{
47084708
DropdbStmt *n = makeNode(DropdbStmt);
47094709
n->dbname = $3;
4710+
n->missing_ok = FALSE;
4711+
$$ = (Node *)n;
4712+
}
4713+
| DROP DATABASE IF_P EXISTS database_name
4714+
{
4715+
DropdbStmt *n = makeNode(DropdbStmt);
4716+
n->dbname = $5;
4717+
n->missing_ok = TRUE;
47104718
$$ = (Node *)n;
47114719
}
47124720
;

src/backend/tcop/utility.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/tcop/utility.c,v 1.247 2005/11/21 12:49:32 alvherre Exp $
13+
* $PostgreSQL: pgsql/src/backend/tcop/utility.c,v 1.248 2005/11/22 15:24:18 adunstan Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -840,7 +840,7 @@ ProcessUtility(Node *parsetree,
840840
{
841841
DropdbStmt *stmt = (DropdbStmt *) parsetree;
842842

843-
dropdb(stmt->dbname);
843+
dropdb(stmt->dbname, stmt->missing_ok);
844844
}
845845
break;
846846

src/include/commands/dbcommands.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/commands/dbcommands.h,v 1.42 2005/10/15 02:49:44 momjian Exp $
10+
* $PostgreSQL: pgsql/src/include/commands/dbcommands.h,v 1.43 2005/11/22 15:24:18 adunstan Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -53,7 +53,7 @@ typedef struct xl_dbase_drop_rec
5353
} xl_dbase_drop_rec;
5454

5555
extern void createdb(const CreatedbStmt *stmt);
56-
extern void dropdb(const char *dbname);
56+
extern void dropdb(const char *dbname, bool missing_ok);
5757
extern void RenameDatabase(const char *oldname, const char *newname);
5858
extern void AlterDatabase(AlterDatabaseStmt *stmt);
5959
extern void AlterDatabaseSet(AlterDatabaseSetStmt *stmt);

src/include/nodes/parsenodes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.294 2005/11/21 12:49:32 alvherre Exp $
10+
* $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.295 2005/11/22 15:24:18 adunstan Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -1672,6 +1672,7 @@ typedef struct DropdbStmt
16721672
{
16731673
NodeTag type;
16741674
char *dbname; /* database to drop */
1675+
bool missing_ok; /* skip error if db is missing? */
16751676
} DropdbStmt;
16761677

16771678
/* ----------------------

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