summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Dunstan2005-11-22 15:24:18 +0000
committerAndrew Dunstan2005-11-22 15:24:18 +0000
commit09985a3f0c5e7ebf97a6af709a1e45497cce87ba (patch)
tree9677a77f5646612cccb7aba153b956aeab0e1bc9
parent25b56a8c09debbd965a585045f14b71f3a0fb0de (diff)
DROP DATABASE IF EXISTS variant
-rw-r--r--doc/src/sgml/ref/drop_database.sgml12
-rw-r--r--src/backend/commands/dbcommands.c20
-rw-r--r--src/backend/nodes/copyfuncs.c1
-rw-r--r--src/backend/nodes/equalfuncs.c1
-rw-r--r--src/backend/parser/gram.y10
-rw-r--r--src/backend/tcop/utility.c2
-rw-r--r--src/include/commands/dbcommands.h2
-rw-r--r--src/include/nodes/parsenodes.h1
8 files changed, 43 insertions, 6 deletions
diff --git a/doc/src/sgml/ref/drop_database.sgml b/doc/src/sgml/ref/drop_database.sgml
index cd1297c2f4..7635bc17ee 100644
--- a/doc/src/sgml/ref/drop_database.sgml
+++ b/doc/src/sgml/ref/drop_database.sgml
@@ -20,7 +20,7 @@ PostgreSQL documentation
<refsynopsisdiv>
<synopsis>
-DROP DATABASE <replaceable class="PARAMETER">name</replaceable>
+DROP DATABASE [ IF EXISTS ] <replaceable class="PARAMETER">name</replaceable>
</synopsis>
</refsynopsisdiv>
@@ -46,6 +46,16 @@ DROP DATABASE <replaceable class="PARAMETER">name</replaceable>
<variablelist>
<varlistentry>
+ <term><literal>IF EXISTS</literal></term>
+ <listitem>
+ <para>
+ Do not throw an error if the database does not exist. A notice is issued
+ in this case.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
<term><replaceable class="PARAMETER">name</replaceable></term>
<listitem>
<para>
diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
index 469b3f7281..ba30a8db2c 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -551,7 +551,7 @@ createdb(const CreatedbStmt *stmt)
* DROP DATABASE
*/
void
-dropdb(const char *dbname)
+dropdb(const char *dbname, bool missing_ok)
{
Oid db_id;
bool db_istemplate;
@@ -585,9 +585,25 @@ dropdb(const char *dbname)
if (!get_db_info(dbname, &db_id, NULL, NULL,
&db_istemplate, NULL, NULL, NULL, NULL, NULL))
- ereport(ERROR,
+ {
+ if (! missing_ok)
+ {
+ ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_DATABASE),
errmsg("database \"%s\" does not exist", dbname)));
+ }
+ else
+ {
+
+ /* Close pg_database, release the lock, since we changed nothing */
+ heap_close(pgdbrel, ExclusiveLock);
+ ereport(NOTICE,
+ (errmsg("database \"%s\" does not exist, skipping",
+ dbname)));
+
+ return;
+ }
+ }
if (!pg_database_ownercheck(db_id, GetUserId()))
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE,
diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c
index a36491f4e7..460b516c1c 100644
--- a/src/backend/nodes/copyfuncs.c
+++ b/src/backend/nodes/copyfuncs.c
@@ -2261,6 +2261,7 @@ _copyDropdbStmt(DropdbStmt *from)
DropdbStmt *newnode = makeNode(DropdbStmt);
COPY_STRING_FIELD(dbname);
+ COPY_SCALAR_FIELD(missing_ok);
return newnode;
}
diff --git a/src/backend/nodes/equalfuncs.c b/src/backend/nodes/equalfuncs.c
index bdbd4e592a..561468b874 100644
--- a/src/backend/nodes/equalfuncs.c
+++ b/src/backend/nodes/equalfuncs.c
@@ -1188,6 +1188,7 @@ static bool
_equalDropdbStmt(DropdbStmt *a, DropdbStmt *b)
{
COMPARE_STRING_FIELD(dbname);
+ COMPARE_SCALAR_FIELD(missing_ok);
return true;
}
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index 6ba9b244a5..e1a970b10f 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -4698,7 +4698,7 @@ alterdb_opt_item:
/*****************************************************************************
*
- * DROP DATABASE
+ * DROP DATABASE [ IF EXISTS ]
*
* This is implicitly CASCADE, no need for drop behavior
*****************************************************************************/
@@ -4707,6 +4707,14 @@ DropdbStmt: DROP DATABASE database_name
{
DropdbStmt *n = makeNode(DropdbStmt);
n->dbname = $3;
+ n->missing_ok = FALSE;
+ $$ = (Node *)n;
+ }
+ | DROP DATABASE IF_P EXISTS database_name
+ {
+ DropdbStmt *n = makeNode(DropdbStmt);
+ n->dbname = $5;
+ n->missing_ok = TRUE;
$$ = (Node *)n;
}
;
diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c
index 1fdc1ee454..77523f5a31 100644
--- a/src/backend/tcop/utility.c
+++ b/src/backend/tcop/utility.c
@@ -840,7 +840,7 @@ ProcessUtility(Node *parsetree,
{
DropdbStmt *stmt = (DropdbStmt *) parsetree;
- dropdb(stmt->dbname);
+ dropdb(stmt->dbname, stmt->missing_ok);
}
break;
diff --git a/src/include/commands/dbcommands.h b/src/include/commands/dbcommands.h
index 8b1d4acc05..eb73939f5d 100644
--- a/src/include/commands/dbcommands.h
+++ b/src/include/commands/dbcommands.h
@@ -53,7 +53,7 @@ typedef struct xl_dbase_drop_rec
} xl_dbase_drop_rec;
extern void createdb(const CreatedbStmt *stmt);
-extern void dropdb(const char *dbname);
+extern void dropdb(const char *dbname, bool missing_ok);
extern void RenameDatabase(const char *oldname, const char *newname);
extern void AlterDatabase(AlterDatabaseStmt *stmt);
extern void AlterDatabaseSet(AlterDatabaseSetStmt *stmt);
diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h
index d023d872cb..93d7e6c9a8 100644
--- a/src/include/nodes/parsenodes.h
+++ b/src/include/nodes/parsenodes.h
@@ -1672,6 +1672,7 @@ typedef struct DropdbStmt
{
NodeTag type;
char *dbname; /* database to drop */
+ bool missing_ok; /* skip error if db is missing? */
} DropdbStmt;
/* ----------------------