Re: proposal: psql: psql variable BACKEND_PID

From: "Daniel Verite" <daniel(at)manitou-mail(dot)org>
To: "Corey Huinker" <corey(dot)huinker(at)gmail(dot)com>
Cc: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: proposal: psql: psql variable BACKEND_PID
Date: 2023-02-06 11:16:13
Message-ID: b68bb584-ce58-4ffc-af5d-639a1d55091d@manitou-mail.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Corey Huinker wrote:

> Manually testing confirms that it works, at least for the connected state. I
> don't actually know how get psql to invoke DISCONNECT, so I killed the dev
> server and can confirm

Maybe something like this could be used, with no external action:

postgres=# \echo :BACKEND_PID
10805
postgres=# create user tester superuser;
CREATE ROLE
postgres=# \c postgres tester
You are now connected to database "postgres" as user "tester".
postgres=# alter user tester nosuperuser connection limit 0;
ALTER ROLE
postgres=# select pg_terminate_backend(pg_backend_pid());
FATAL: terminating connection due to administrator command
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.
The connection to the server was lost. Attempting reset: Failed.

!?> \echo :BACKEND_PID
:BACKEND_PID

> In the varlistentry, I suggest we add "This variable is unset when the
> connection is lost." after "but can be changed or unset.

Personally I'd much rather have BACKEND_PID set to 0 rather than being unset
when not connected. For one thing it allows safely using \if :BACKEND_PID.

Best regards,
--
Daniel Vérité
https://postgresql.verite.pro/
Twitter: @DanielVerite

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Marcos Pegoraro 2023-02-06 11:20:17 Understanding years part of Interval
Previous Message Alvaro Herrera 2023-02-06 10:49:17 Re: Pluggable toaster