Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite8d9d68

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 INTand TERM, and permits sending only to postgresql backends.Magnus Hagander
1 parentde2c665 commite8d9d68

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
returnresult;
464464
}
465465

466+
/*
467+
* IsBackendPid -- is a given pid a running backend
468+
*/
469+
bool
470+
IsBackendPid(intpid)
471+
{
472+
boolresult= false;
473+
SISeg*segP=shmInvalBuffer;
474+
ProcState*stateP=segP->procState;
475+
intindex;
476+
477+
LWLockAcquire(SInvalLock,LW_SHARED);
478+
479+
for (index=0;index<segP->lastBackend;index++)
480+
{
481+
SHMEM_OFFSETpOffset=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+
returnresult;
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+
staticintpg_signal_backend(intpid,intsig)
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+
return0;
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+
return0;
91+
}
92+
return1;
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-
#defineCATALOG_VERSION_NO200405262
56+
#defineCATALOG_VERSION_NO200406021
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(insertOID=1945 (pg_stat_get_db_blocks_hitPGNSPPGUID12fftfs120"26"_null_pg_stat_get_db_blocks_hit-_null_ ));
28092809
DESCR("Statistics: Blocks found in cache for database");
28102810

2811+
DATA(insertOID=2171 (pg_terminate_backendPGNSPPGUID12fftfs123"23"_null_pg_terminate_backend-_null_ ));
2812+
DESCR("Terminate a backend process");
2813+
DATA(insertOID=2172 (pg_cancel_backendPGNSPPGUID12fftfs123"23"_null_pg_cancel_backend-_null_ ));
2814+
DESCR("Cancel running query on a backend process");
2815+
28112816
DATA(insertOID=1946 (encodePGNSPPGUID12fftfi225"17 25"_null_binary_encode-_null_ ));
28122817
DESCR("Convert bytea value into some ascii-only text string");
28132818
DATA(insertOID=1947 (decodePGNSPPGUID12fftfi217"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
externboolDatabaseHasActiveBackends(OiddatabaseId,boolignoreMyself);
9999
externboolTransactionIdIsInProgress(TransactionIdxid);
100+
externboolIsBackendPid(intpid);
100101
externTransactionIdGetOldestXmin(boolallDbs);
101102
externintCountActiveBackends(void);
102103
externintCountEmptyBackendSlots(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
externDatumnullvalue(PG_FUNCTION_ARGS);
355355
externDatumnonnullvalue(PG_FUNCTION_ARGS);
356356
externDatumcurrent_database(PG_FUNCTION_ARGS);
357+
externDatumpg_terminate_backend(PG_FUNCTION_ARGS);
358+
externDatumpg_cancel_backend(PG_FUNCTION_ARGS);
357359

358360
/* not_in.c */
359361
externDatumint4notin(PG_FUNCTION_ARGS);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp