From 157c17195b085907c19d4c935cc28076a2aca56c Mon Sep 17 00:00:00 2001 From: Bharath Rupireddy Date: Mon, 14 Jun 2021 07:00:09 -0700 Subject: [PATCH v2] Remove pg_wait_for_backend_termination function It was unable to wait on a backend that had already left the procarray. Users tolerant of that limitation can poll pg_stat_activity. Other users can employ the "timeout" argument of pg_terminate_backend function. Per suggestion from Justin Pryzby, reworded the docs, code comments and release notes relating to the pg_terminate_backend function. --- doc/src/sgml/func.sgml | 19 +-------- doc/src/sgml/release-14.sgml | 8 +--- src/backend/catalog/system_functions.sql | 5 --- src/backend/storage/ipc/signalfuncs.c | 50 ++++-------------------- src/include/catalog/pg_proc.dat | 6 +-- 5 files changed, 10 insertions(+), 78 deletions(-) diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index fbc80c1403..764c3ad307 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -24998,27 +24998,10 @@ SELECT collation for ('foo' COLLATE "de_DE"); milliseconds) and greater than zero, the function waits until the process is actually terminated or until the given time has passed. If the process is terminated, the function - returns true. On timeout a warning is emitted and + returns true. On timeout, a warning is emitted and false is returned. - - - - - pg_wait_for_backend_termination - - pg_wait_for_backend_termination ( pid integer, timeout bigint DEFAULT 5000 ) - boolean - - - Waits for the backend process with the specified Process ID to - terminate. If the process terminates before - the timeout (in milliseconds) - expires, true is returned. On timeout, a warning - is emitted and false is returned. - - diff --git a/doc/src/sgml/release-14.sgml b/doc/src/sgml/release-14.sgml index 058ba7cd4e..89bf78500a 100644 --- a/doc/src/sgml/release-14.sgml +++ b/doc/src/sgml/release-14.sgml @@ -611,13 +611,7 @@ Author: Magnus Hagander --> - Add function pg_wait_for_backend_termination() - that waits for session exit (Bharath Rupireddy) - - - - Also add a similar optional wait parameter to pg_terminate_backend() diff --git a/src/backend/catalog/system_functions.sql b/src/backend/catalog/system_functions.sql index a4373b176c..a416e94d37 100644 --- a/src/backend/catalog/system_functions.sql +++ b/src/backend/catalog/system_functions.sql @@ -397,11 +397,6 @@ CREATE OR REPLACE FUNCTION RETURNS boolean STRICT VOLATILE LANGUAGE INTERNAL AS 'pg_terminate_backend' PARALLEL SAFE; -CREATE OR REPLACE FUNCTION - pg_wait_for_backend_termination(pid integer, timeout int8 DEFAULT 5000) - RETURNS boolean STRICT VOLATILE LANGUAGE INTERNAL AS 'pg_wait_for_backend_termination' - PARALLEL SAFE; - -- legacy definition for compatibility with 9.3 CREATE OR REPLACE FUNCTION json_populate_record(base anyelement, from_json json, use_json_as_text boolean DEFAULT false) diff --git a/src/backend/storage/ipc/signalfuncs.c b/src/backend/storage/ipc/signalfuncs.c index 837699481c..30c4516f8d 100644 --- a/src/backend/storage/ipc/signalfuncs.c +++ b/src/backend/storage/ipc/signalfuncs.c @@ -187,12 +187,12 @@ pg_wait_until_termination(int pid, int64 timeout) } /* - * Signal to terminate a backend process. This is allowed if you are a member - * of the role whose process is being terminated. If timeout input argument is - * 0 (which is default), then this function just signals the backend and - * doesn't wait. Otherwise it waits until given the timeout milliseconds or no - * process has the given PID and returns true. On timeout, a warning is emitted - * and false is returned. + * Send a signal to terminate a backend process. This is allowed if you are a + * member of the role whose process is being terminated. If the timeout input + * argument is 0, then this function just signals the backend and returns true. + * If timeout is nonzero, then it waits until no process has the given PID; if + * the process ends within the timeout, true is returned, and if the timeout is + * exceeded, a warning is emitted and false is returned. * * Note that only superusers can signal superuser-owned processes. */ @@ -201,7 +201,7 @@ pg_terminate_backend(PG_FUNCTION_ARGS) { int pid; int r; - int timeout; + int timeout; /* milliseconds */ pid = PG_GETARG_INT32(0); timeout = PG_GETARG_INT64(1); @@ -230,42 +230,6 @@ pg_terminate_backend(PG_FUNCTION_ARGS) PG_RETURN_BOOL(r == SIGNAL_BACKEND_SUCCESS); } -/* - * Wait for a backend process with the given PID to exit or until the given - * timeout milliseconds occurs. Returns true if the backend has exited. On - * timeout a warning is emitted and false is returned. - * - * We allow any user to call this function, consistent with any user being - * able to view the pid of the process in pg_stat_activity etc. - */ -Datum -pg_wait_for_backend_termination(PG_FUNCTION_ARGS) -{ - int pid; - int64 timeout; - PGPROC *proc = NULL; - - pid = PG_GETARG_INT32(0); - timeout = PG_GETARG_INT64(1); - - if (timeout <= 0) - ereport(ERROR, - (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), - errmsg("\"timeout\" must not be negative or zero"))); - - proc = BackendPidGetProc(pid); - - if (proc == NULL) - { - ereport(WARNING, - (errmsg("PID %d is not a PostgreSQL server process", pid))); - - PG_RETURN_BOOL(false); - } - - PG_RETURN_BOOL(pg_wait_until_termination(pid, timeout)); -} - /* * Signal to reload the database configuration * diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index acbcae4607..8acb39aa7b 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -6187,14 +6187,10 @@ proname => 'pg_cancel_backend', provolatile => 'v', prorettype => 'bool', proargtypes => 'int4', prosrc => 'pg_cancel_backend' }, { oid => '2096', - descr => 'terminate a backend process and if timeout is specified, wait for its exit or until timeout occurs', + descr => 'terminate a server process', proname => 'pg_terminate_backend', provolatile => 'v', prorettype => 'bool', proargtypes => 'int4 int8', proargnames => '{pid,timeout}', prosrc => 'pg_terminate_backend' }, -{ oid => '2137', descr => 'wait for a backend process exit or timeout occurs', - proname => 'pg_wait_for_backend_termination', provolatile => 'v', - prorettype => 'bool', proargtypes => 'int4 int8', - proargnames => '{pid,timeout}', prosrc => 'pg_wait_for_backend_termination' }, { oid => '2172', descr => 'prepare for taking an online backup', proname => 'pg_start_backup', provolatile => 'v', proparallel => 'r', prorettype => 'pg_lsn', proargtypes => 'text bool bool', -- 2.25.1