diff options
author | Amit Kapila <akapila@postgresql.org> | 2019-11-12 11:06:13 +0530 |
---|---|---|
committer | Amit Kapila <akapila@postgresql.org> | 2019-11-13 08:25:33 +0530 |
commit | 1379fd537f9fc7941c8acff8c879ce3636dbdb77 (patch) | |
tree | 7f74fd3cf0367c76ee511c14949c96e740a25916 /src/backend/commands/dbcommands.c | |
parent | 112caf9039f4c8fb286bb610461ced8253313e9f (diff) | |
download | postgresql-1379fd537f9fc7941c8acff8c879ce3636dbdb77.tar.gz postgresql-1379fd537f9fc7941c8acff8c879ce3636dbdb77.zip |
Introduce the 'force' option for the Drop Database command.
This new option terminates the other sessions connected to the target
database and then drop it. To terminate other sessions, the current user
must have desired permissions (same as pg_terminate_backend()). We don't
allow to terminate the sessions if prepared transactions, active logical
replication slots or subscriptions are present in the target database.
Author: Pavel Stehule with changes by me
Reviewed-by: Dilip Kumar, Vignesh C, Ibrar Ahmed, Anthony Nowocien,
Ryan Lambert and Amit Kapila
Discussion: https://postgr.es/m/CAP_rwwmLJJbn70vLOZFpxGw3XD7nLB_7+NKz46H5EOO2k5H7OQ@mail.gmail.com
Diffstat (limited to 'src/backend/commands/dbcommands.c')
-rw-r--r-- | src/backend/commands/dbcommands.c | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c index 6f28859f730..446813f0f0b 100644 --- a/src/backend/commands/dbcommands.c +++ b/src/backend/commands/dbcommands.c @@ -810,7 +810,7 @@ createdb_failure_callback(int code, Datum arg) * DROP DATABASE */ void -dropdb(const char *dbname, bool missing_ok) +dropdb(const char *dbname, bool missing_ok, bool force) { Oid db_id; bool db_istemplate; @@ -910,6 +910,14 @@ dropdb(const char *dbname, bool missing_ok) "There are %d subscriptions.", nsubscriptions, nsubscriptions))); + + /* + * Attempt to terminate all existing connections to the target database if + * the user has requested to do so. + */ + if (force) + TerminateOtherDBBackends(db_id); + /* * Check for other backends in the target database. (Because we hold the * database lock, no new ones can start after this.) @@ -1430,6 +1438,30 @@ movedb_failure_callback(int code, Datum arg) (void) rmtree(dstpath, true); } +/* + * Process options and call dropdb function. + */ +void +DropDatabase(ParseState *pstate, DropdbStmt *stmt) +{ + bool force = false; + ListCell *lc; + + foreach(lc, stmt->options) + { + DefElem *opt = (DefElem *) lfirst(lc); + + if (strcmp(opt->defname, "force") == 0) + force = true; + else + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("unrecognized DROP DATABASE option \"%s\"", opt->defname), + parser_errposition(pstate, opt->location))); + } + + dropdb(stmt->dbname, stmt->missing_ok, force); +} /* * ALTER DATABASE name ... |