Question

I want to know what happens when a drop database command is canceled using ^C.

Does it partially removes the data or completely?

Was it helpful?

Solution

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:

  1. Statement is received from the server.
  2. Implicit transaction is started.
  3. Lock is taken on the database.
  4. Checks are made to make sure that the database is not being used.
  5. The database is removed from the meta tables.
  6. A checkpoint is written in the WAL.
  7. The database files are removed.
  8. 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)

OTHER TIPS

I want to know what happens when a drop database command is canceled using ^C. Does it partially removes the data or completely?

That depends on why the command is hanging. DROP DATABASE does a lot of stuff to get the locks it needs and to ensure no one else is connected and the like. If stuck at a step north of line 898, nothing happens.

If you move past that though you could be in a world of fun. My assumption is that the DROP stuff happens in milliseconds.

The database files are removed with remove_dbtablespaces. That runs (almost) last so the database is already gone at that point. It's just cleaning up the file system.

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top