Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xRYdt-000000014uo-22vW for pgsql-bugs@arkaria.postgresql.org; Thu, 12 Nov 2026 17:31:21 +0000 Received: from localhost ([127.0.0.1]:47716 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xRYds-0000000450q-0q1M for pgsql-bugs@arkaria.postgresql.org; Thu, 12 Nov 2026 17:31:20 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]:44186) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xR9eI-0000000Bfws-3LSU for pgsql-bugs@lists.postgresql.org; Wed, 11 Nov 2026 14:50:07 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xR9eH-00000000OBb-36kI for pgsql-bugs@lists.postgresql.org; Wed, 11 Nov 2026 14:50:06 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Message-ID:Date:Reply-To:Cc:From:To:Subject: Content-Transfer-Encoding:MIME-Version:Content-Type:Sender:Content-ID: Content-Description:In-Reply-To:References; bh=fgkL+ep8+J1aDsRDnCbz6BHInZDbsR2TnMEo84wd99Y=; b=Yb5/H7MWVh7798Gxajr2bf/U7r YK0Bm0AMR6gxphCENZjUTJKZSY4gSEItUIM74+KY7OIQaeB6BPc0KNdgiu4a9stN0Bq6nKWWHBOVj JORFtIHW4lB5glSUT316lwnhmCP3hDnWh0vhQQOlyr1lBLxXFWCxl8vVSuBvaL1hxS+TTfb4u0l0s 7YviizqfX6ndjYs4Rc2UrPWb2IqIW7FCEiVvzS/n0luPFUjMHQ3hFKATblyWOlckqKYMV+kh8Lw84 z3F+fI9pVTZcp6TIEbiHDgH7k53Pk7NWOKNtXbLzKtiwNxW4NoUChTWp/x6jyiIlLNktgyd06JKic hXW9y6Ew==; Received: from wrigleys.postgresql.org ([2a02:16a8:dc51::60]) by mahout.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xR9eG-000000014M9-3inA for pgsql-bugs@lists.postgresql.org; Wed, 11 Nov 2026 14:50:05 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xR9eF-00000002lOE-3jIu for pgsql-bugs@lists.postgresql.org; Wed, 11 Nov 2026 14:50:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19886: pg_terminate_backend timeout expires early after latch wakeups To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: imchifan8@gmail.com Reply-To: imchifan8@gmail.com, pgsql-bugs@lists.postgresql.org Date: Wed, 11 Nov 2026 14:49:13 +0000 Message-ID: <19886-16f4759c592d0c00@postgresql.org> X-Auto-Response-Suppress: All Auto-Submitted: auto-generated List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk The following bug has been logged on the website: Bug reference: 19886 Logged by: Noah Email address: imchifan8@gmail.com PostgreSQL version: 18.6 Operating system: Linux/amd64 Description: =20 pg_terminate_backend(pid, timeout) can return false and warn that the full timeout elapsed after only a small fraction of that duration. This occurs when the target remains alive after SIGTERM and unrelated events repeatedly wake the waiting backend's latch. The timeout should measure elapsed wall-clock time. Early latch wakeups should not consume portions of the timeout that were not actually waited. How to repeat: This requires a Linux PostgreSQL instance with dblink available and a role permitted to create the extension, signal backends, and execute COPY TO PROGRAM. Run the following as `psql -X -f reproduce.sql`: ``` \set ON_ERROR_STOP on SELECT EXISTS ( SELECT 1 FROM pg_extension WHERE extname =3D 'dblink' ) AS dblink_preexisting \gset CREATE EXTENSION IF NOT EXISTS dblink; CREATE TEMP TABLE timeout_result ( control_result boolean, control_elapsed_ms numeric, signaled_result boolean, signaled_elapsed_ms numeric, reproduced boolean ); DO $test$ DECLARE control_pid integer; test_pid integer; control_started timestamptz; test_started timestamptz; control_elapsed numeric; test_elapsed numeric; control_result boolean; test_result boolean; BEGIN /* A stopped backend cannot process SIGTERM, so this must time out. */ PERFORM dblink_connect( 'control', 'dbname=3D' || current_database() || ' application_name=3Dterminate_control'); PERFORM pg_stat_clear_snapshot(); SELECT pid INTO STRICT control_pid FROM pg_stat_activity WHERE application_name =3D 'terminate_control'; PERFORM dblink_send_query( 'control', $q$COPY (SELECT pg_backend_pid()) TO PROGRAM 'read pid; kill -STOP "$pid"'$q$); PERFORM pg_sleep(0.25); control_started :=3D clock_timestamp(); control_result :=3D pg_terminate_backend(control_pid, 1000); control_elapsed :=3D 1000 * extract(epoch FROM clock_timestamp() - control_started); EXECUTE format( $q$COPY (SELECT %s) TO PROGRAM 'read pid; kill -CONT "$pid"'$q$, control_pid); PERFORM pg_sleep(0.20); BEGIN PERFORM dblink_disconnect('control'); EXCEPTION WHEN OTHERS THEN NULL; END; /* Repeated SIGUSR1 delivery wakes this backend's latch. */ PERFORM dblink_connect( 'target', 'dbname=3D' || current_database() || ' application_name=3Dterminate_target'); PERFORM pg_stat_clear_snapshot(); SELECT pid INTO STRICT test_pid FROM pg_stat_activity WHERE application_name =3D 'terminate_target'; PERFORM dblink_send_query( 'target', $q$COPY (SELECT pg_backend_pid()) TO PROGRAM 'read pid; kill -STOP "$pid"'$q$); PERFORM pg_sleep(0.25); EXECUTE $q$COPY (SELECT pg_backend_pid()) TO PROGRAM 'read pid; nohup sh -c ''for i in $(seq 1 80); do kill -USR1 "$1"; sleep 0.005; done'' sh "$pid" >/dev/null 2>&1 &'$q$; test_started :=3D clock_timestamp(); test_result :=3D pg_terminate_backend(test_pid, 3000); test_elapsed :=3D 1000 * extract(epoch FROM clock_timestamp() - test_started); EXECUTE format( $q$COPY (SELECT %s) TO PROGRAM 'read pid; kill -CONT "$pid"'$q$, test_pid); PERFORM pg_sleep(0.60); BEGIN PERFORM dblink_disconnect('target'); EXCEPTION WHEN OTHERS THEN NULL; END; INSERT INTO timeout_result VALUES (control_result, control_elapsed, test_result, test_elapsed, control_result IS FALSE AND control_elapsed >=3D 800 AND test_result IS FALSE AND test_elapsed < 1500); EXCEPTION WHEN OTHERS THEN IF control_pid IS NOT NULL THEN BEGIN EXECUTE format( $q$COPY (SELECT %s) TO PROGRAM 'read pid; kill -CONT "$pid"'$q$, control_pid); EXCEPTION WHEN OTHERS THEN NULL; END; END IF; IF test_pid IS NOT NULL THEN BEGIN EXECUTE format( $q$COPY (SELECT %s) TO PROGRAM 'read pid; kill -CONT "$pid"'$q$, test_pid); EXCEPTION WHEN OTHERS THEN NULL; END; END IF; RAISE; END $test$; SELECT control_result, round(control_elapsed_ms, 3) AS control_elapsed_ms, signaled_result, round(signaled_elapsed_ms, 3) AS signaled_elapsed_ms, reproduced FROM timeout_result; \if :dblink_preexisting \else DROP EXTENSION dblink; \endif ``` A representative recorded run produced: ``` CREATE EXTENSION CREATE TABLE DO f|1001.698|f|179.005|t DROP EXTENSION WARNING: backend with PID 85229 did not terminate within 1000 milliseconds WARNING: backend with PID 85232 did not terminate within 3000 milliseconds ``` The 1000 ms control lasted 1001.698 ms, whereas repeated SIGUSR1 wakeups made the nominal 3000 ms wait return false after 179.005 ms. Another run measured 1001.860 ms for the control and 171.359 ms for the signaled call. The issue was reproduced on PostgreSQL 18.6 (Debian 18.6-1.pgdg13+2) on Linux/amd64. pg_wait_until_termination() subtracts the requested 100 ms wait interval after every WaitLatch return, even when the latch wakes before that interval has elapsed. The termination signal is still sent; callers can work around the premature synchronous return by polling for backend termination or retrying without unrelated latch traffic. This occurs when pg_terminate_backend waits with a positive timeout while unrelated signals repeatedly wake the caller's latch.