drop database command is cancelled ^C
Let's try again, but with the correct statement ;)

When you cancel a query in psql, what it does internally is to send a pg_cancel_backend() to the server. This returns directly, but the statement might still be running on the server.

When a query is run, there are a few times it checks if it safely can cancel the statement, and in that case do so. This applies to a drop database statement as well.

There is a short time between the database files are removed from disk and the statement is committed where things can get interesting. It is not possible to cancel the query during that phase, but if there is a server crash then there is a risk that the database still exists in the meta tables, but the files are removed from disk.

Update, clarification of when it can be cancelled

When the server receives a cancel request, it marks a flag that the statement should be cancelled, and the statement checks that flag once in a while during execution. To explain what happens when we cancel the drop database we need to understand what it does:


Statement is received from the server.
Implicit transaction is started.
Lock is taken on the database.
Checks are made to make sure that the database is not being used.
The database is removed from the meta tables.
A checkpoint is written in the WAL.
The database files are removed.
The transaction is committed.


At any point up until #6 the statement can be cancelled (the final check of the flag is done at #6)