sqlpostgresql

How to stop a running query in PostqreSQL?


First I run this query to see the running queries:

select * from pg_stat_activity;

then I run this query to stop them:

SELECT pg_cancel_backend(pid);

but, when I run the pg_stat_activity again, it still shows all the queries!

why it didn't kill the queries?


Solution

  • A number of possible explanations:

    In general it's safe to use pg_terminate_backend as a "bigger hammer". SIGTERM as sent by pg_terminate_backend() will often, but not always, cause a backend that can't respond to a cancel to exit.

    Do not kill -9 (SIGKILL) a PostgreSQL backend (postgres process). It will cause the whole PostgreSQL server to emergency-restart to protect shared memory safety.