diff options
author | Bruce Momjian <bruce@momjian.us> | 2004-06-02 21:29:29 +0000 |
---|---|---|
committer | Bruce Momjian <bruce@momjian.us> | 2004-06-02 21:29:29 +0000 |
commit | e8d9d68ca4866c775b5d1170581ac9e1ade3abd8 (patch) | |
tree | 1ca2cd413be5a75faa23bba21916d2bdbcf90678 /src/backend/utils/adt/misc.c | |
parent | de2c66539eaabdcee94ae4f243ec6402058b1f4c (diff) | |
download | postgresql-e8d9d68ca4866c775b5d1170581ac9e1ade3abd8.tar.gz postgresql-e8d9d68ca4866c775b5d1170581ac9e1ade3abd8.zip |
Per previous discussions, here are two functions to send INT and TERM
(cancel and terminate) signals to other backends. They permit only INT
and TERM, and permits sending only to postgresql backends.
Magnus Hagander
Diffstat (limited to 'src/backend/utils/adt/misc.c')
-rw-r--r-- | src/backend/utils/adt/misc.c | 48 |
1 files changed, 47 insertions, 1 deletions
diff --git a/src/backend/utils/adt/misc.c b/src/backend/utils/adt/misc.c index 673b175bbc0..8ab6953cb02 100644 --- a/src/backend/utils/adt/misc.c +++ b/src/backend/utils/adt/misc.c @@ -8,16 +8,18 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/misc.c,v 1.33 2004/05/21 05:08:02 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/misc.c,v 1.34 2004/06/02 21:29:29 momjian Exp $ * *------------------------------------------------------------------------- */ #include "postgres.h" #include <sys/file.h> +#include <signal.h> #include "commands/dbcommands.h" #include "miscadmin.h" +#include "storage/sinval.h" #include "utils/builtins.h" @@ -57,3 +59,47 @@ current_database(PG_FUNCTION_ARGS) namestrcpy(db, get_database_name(MyDatabaseId)); PG_RETURN_NAME(db); } + + +/* + * Functions to terminate a backend or cancel a query running on + * a different backend. + */ + +static int pg_signal_backend(int pid, int sig) +{ + if (!superuser()) + ereport(ERROR, + (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), + (errmsg("only superuser can signal other backends")))); + + if (!IsBackendPid(pid)) + { + /* This is just a warning so a loop-through-resultset will not abort + * if one backend terminated on it's own during the run */ + ereport(WARNING, + (errmsg("pid %i is not a postgresql backend",pid))); + return 0; + } + + if (kill(pid, sig)) + { + /* Again, just a warning to allow loops */ + ereport(WARNING, + (errmsg("failed to send signal to backend %i: %m",pid))); + return 0; + } + return 1; +} + +Datum +pg_terminate_backend(PG_FUNCTION_ARGS) +{ + PG_RETURN_INT32(pg_signal_backend(PG_GETARG_INT32(0),SIGTERM)); +} + +Datum +pg_cancel_backend(PG_FUNCTION_ARGS) +{ + PG_RETURN_INT32(pg_signal_backend(PG_GETARG_INT32(0),SIGINT)); +} |