From c13580f8e36583fa63230ab5fbbbdc1f61324aba Mon Sep 17 00:00:00 2001 From: Ashutosh Bapat Date: Tue, 23 Jun 2026 09:19:06 +0530 Subject: [PATCH v20260724 5/7] PID of the backend process backing the BackgroundPsql session Many tests which use background psql sessions also fetch the pid of the backend process as a separate step. They end up maintaing a separate variable for the pid and pass it to the subroutines along with the BackgroundPsql object when needed. Further the PID can not be fetched if the backend is blocked in a query or an injection point or if the backend has died. This can be avoided by proactively fetching the PID of the backend process when the BackgroundPsql is started and storing it in the BackgroundPsql object. The PID can then be fetched from the BackgroundPsql object when needed. The pid is reset when the BackgroundPsql session is finished. Many of the tests which use BackgroundPsql explicitly call {run}->finish to finish the session when the backend is gone. They will have stale PID in the BackgroundPsql object. The commit introduces a finish method in BackgroundPsql which will finish the session and reset the PID. Note to the reviewer: This commit adds code to set the PID proactively and also the finish method. These will be used in a future buffer resize test. But I have not modified the existing tests to use the new capabilities. If we find this change useful, we can modify the existing tests to use the new capabilities before committing this change. Author: Ashutosh Bapat --- .../perl/PostgreSQL/Test/BackgroundPsql.pm | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/test/perl/PostgreSQL/Test/BackgroundPsql.pm b/src/test/perl/PostgreSQL/Test/BackgroundPsql.pm index d7797225451..699334320d9 100644 --- a/src/test/perl/PostgreSQL/Test/BackgroundPsql.pm +++ b/src/test/perl/PostgreSQL/Test/BackgroundPsql.pm @@ -173,6 +173,14 @@ sub wait_connect $self->{stderr} = ''; die "psql startup timed out" if $self->{timeout}->is_expired; + + # Many tests which use background psql sessions also fetch the pid of the + # backend process, so we capture it here. The Callers that need the pid + # after a blocking query or after the backend has died can read it from + # $self->{backend_pid}. + my $pid = $self->query('SELECT pg_backend_pid()', verbose => 0); + chomp $pid; + $self->{backend_pid} = $pid; } =pod @@ -190,6 +198,25 @@ sub quit $self->{stdin} .= "\\q\n"; + return $self->finish; +} + +=pod + +=item $session->finish + +Reap the underlying IPC::Run handle without sending \q. Intended for +sessions whose psql process has already exited (e.g. after the server +terminated the backend or the client connection was killed). + +=cut + +sub finish +{ + my ($self) = @_; + + $self->{backend_pid} = undef; + return $self->{run}->finish; } @@ -212,7 +239,7 @@ sub reconnect_and_clear { $self->{stdin} .= "\\q\n"; } - $self->{run}->finish; + $self->finish; # restart $self->{run}->run(); -- 2.34.1