Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e8d9d68

Browse files
committed
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
1 parent de2c665 commit e8d9d68

File tree

6 files changed

+95
-7
lines changed

6 files changed

+95
-7
lines changed

src/backend/storage/ipc/sinval.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/storage/ipc/sinval.c,v 1.63 2004/05/23 03:50:45 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/storage/ipc/sinval.c,v 1.64 2004/06/02 21:29:28 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -463,6 +463,40 @@ TransactionIdIsInProgress(TransactionId xid)
463463
return result;
464464
}
465465

466+
/*
467+
* IsBackendPid -- is a given pid a running backend
468+
*/
469+
bool
470+
IsBackendPid(int pid)
471+
{
472+
bool result = false;
473+
SISeg *segP = shmInvalBuffer;
474+
ProcState *stateP = segP->procState;
475+
int index;
476+
477+
LWLockAcquire(SInvalLock, LW_SHARED);
478+
479+
for (index = 0; index < segP->lastBackend; index++)
480+
{
481+
SHMEM_OFFSET pOffset = stateP[index].procStruct;
482+
483+
if (pOffset != INVALID_OFFSET)
484+
{
485+
PGPROC *proc = (PGPROC *) MAKE_PTR(pOffset);
486+
487+
if (proc->pid == pid)
488+
{
489+
result = true;
490+
break;
491+
}
492+
}
493+
}
494+
495+
LWLockRelease(SInvalLock);
496+
497+
return result;
498+
}
499+
466500
/*
467501
* GetOldestXmin -- returns oldest transaction that was running
468502
* when any current transaction was started.

src/backend/utils/adt/misc.c

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/misc.c,v 1.33 2004/05/21 05:08:02 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/misc.c,v 1.34 2004/06/02 21:29:29 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
1515
#include "postgres.h"
1616

1717
#include <sys/file.h>
18+
#include <signal.h>
1819

1920
#include "commands/dbcommands.h"
2021
#include "miscadmin.h"
22+
#include "storage/sinval.h"
2123
#include "utils/builtins.h"
2224

2325

@@ -57,3 +59,47 @@ current_database(PG_FUNCTION_ARGS)
5759
namestrcpy(db, get_database_name(MyDatabaseId));
5860
PG_RETURN_NAME(db);
5961
}
62+
63+
64+
/*
65+
* Functions to terminate a backend or cancel a query running on
66+
* a different backend.
67+
*/
68+
69+
static int pg_signal_backend(int pid, int sig)
70+
{
71+
if (!superuser())
72+
ereport(ERROR,
73+
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
74+
(errmsg("only superuser can signal other backends"))));
75+
76+
if (!IsBackendPid(pid))
77+
{
78+
/* This is just a warning so a loop-through-resultset will not abort
79+
* if one backend terminated on it's own during the run */
80+
ereport(WARNING,
81+
(errmsg("pid %i is not a postgresql backend",pid)));
82+
return 0;
83+
}
84+
85+
if (kill(pid, sig))
86+
{
87+
/* Again, just a warning to allow loops */
88+
ereport(WARNING,
89+
(errmsg("failed to send signal to backend %i: %m",pid)));
90+
return 0;
91+
}
92+
return 1;
93+
}
94+
95+
Datum
96+
pg_terminate_backend(PG_FUNCTION_ARGS)
97+
{
98+
PG_RETURN_INT32(pg_signal_backend(PG_GETARG_INT32(0),SIGTERM));
99+
}
100+
101+
Datum
102+
pg_cancel_backend(PG_FUNCTION_ARGS)
103+
{
104+
PG_RETURN_INT32(pg_signal_backend(PG_GETARG_INT32(0),SIGINT));
105+
}

src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.232 2004/05/26 18:37:33 momjian Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.233 2004/06/02 21:29:29 momjian Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 200405262
56+
#define CATALOG_VERSION_NO 200406021
5757

5858
#endif

src/include/catalog/pg_proc.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.333 2004/05/26 18:37:33 momjian Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.334 2004/06/02 21:29:29 momjian Exp $
1111
*
1212
* NOTES
1313
* The script catalog/genbki.sh reads this file and generates .bki
@@ -2808,6 +2808,11 @@ DESCR("Statistics: Blocks fetched for database");
28082808
DATA(insert OID = 1945 ( pg_stat_get_db_blocks_hit PGNSP PGUID 12 f f t f s 1 20 "26" _null_ pg_stat_get_db_blocks_hit - _null_ ));
28092809
DESCR("Statistics: Blocks found in cache for database");
28102810

2811+
DATA(insert OID = 2171 ( pg_terminate_backend PGNSP PGUID 12 f f t f s 1 23 "23" _null_ pg_terminate_backend - _null_ ));
2812+
DESCR("Terminate a backend process");
2813+
DATA(insert OID = 2172 ( pg_cancel_backend PGNSP PGUID 12 f f t f s 1 23 "23" _null_ pg_cancel_backend - _null_ ));
2814+
DESCR("Cancel running query on a backend process");
2815+
28112816
DATA(insert OID = 1946 ( encode PGNSP PGUID 12 f f t f i 2 25 "17 25" _null_ binary_encode - _null_ ));
28122817
DESCR("Convert bytea value into some ascii-only text string");
28132818
DATA(insert OID = 1947 ( decode PGNSP PGUID 12 f f t f i 2 17 "25 25" _null_ binary_decode - _null_ ));

src/include/storage/sinval.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/storage/sinval.h,v 1.34 2004/05/23 03:50:45 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/storage/sinval.h,v 1.35 2004/06/02 21:29:29 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -97,6 +97,7 @@ extern void ReceiveSharedInvalidMessages(
9797

9898
extern bool DatabaseHasActiveBackends(Oid databaseId, bool ignoreMyself);
9999
extern bool TransactionIdIsInProgress(TransactionId xid);
100+
extern bool IsBackendPid(int pid);
100101
extern TransactionId GetOldestXmin(bool allDbs);
101102
extern int CountActiveBackends(void);
102103
extern int CountEmptyBackendSlots(void);

src/include/utils/builtins.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.240 2004/05/26 18:35:46 momjian Exp $
10+
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.241 2004/06/02 21:29:29 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -354,6 +354,8 @@ extern Datum float84ge(PG_FUNCTION_ARGS);
354354
extern Datum nullvalue(PG_FUNCTION_ARGS);
355355
extern Datum nonnullvalue(PG_FUNCTION_ARGS);
356356
extern Datum current_database(PG_FUNCTION_ARGS);
357+
extern Datum pg_terminate_backend(PG_FUNCTION_ARGS);
358+
extern Datum pg_cancel_backend(PG_FUNCTION_ARGS);
357359

358360
/* not_in.c */
359361
extern Datum int4notin(PG_FUNCTION_ARGS);

0 commit comments

Comments
 (0)