Add %r substitution for psql prompts to show recovery status

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Add %r substitution for psql prompts to show recovery status

Ian Barwick-3
Hi

Attached patch adds an "%r" substitution for psql prompts to show
recovery status. Specifically it displays an "&" (ampersand) if
the server is in recovery, otherwise nothing, e.g.:

     postgres=&# SELECT foo;

Why is this useful? Because I find myself messing about with replication
clusters a lot, and it would be nice to have an at-a-glance confirmation
whether I'm connected to a standby or not.

Why an ampersand? Because it's not used for any other prompts, and
it can be used as a mnemonic: "and" -> "st'and'by" (clutching at
straws a bit there I admit, but best I could come up with).

Note this substitution sends a "pg_is_in_recovery()" query to the server
each time it's encountered; unless there's something I'm overlooking I
think that's the only reliable way to determine current recovery status.
A possible alternative would be only to check the status each time a new
database connection is made, but that wouldn't catch the case where the
server has been promoted.

Will submit to next commitfest.


Regards

Ian Barwick

--
  Ian Barwick                   http://www.2ndQuadrant.com/
  PostgreSQL Development, 24x7 Support, Training & Services

psql-recovery-status-prompt.patch (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Add %r substitution for psql prompts to show recovery status

Robert Haas
On Wed, Dec 6, 2017 at 9:19 PM, Ian Barwick <[hidden email]> wrote:
> Note this substitution sends a "pg_is_in_recovery()" query to the server
> each time it's encountered; unless there's something I'm overlooking I
> think that's the only reliable way to determine current recovery status.

That seems kinda painful.

And what happens in an aborted transaction?

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

Reply | Threaded
Open this post in threaded view
|

Re: Add %r substitution for psql prompts to show recovery status

David G Johnston
In reply to this post by Ian Barwick-3
On Wed, Dec 6, 2017 at 7:19 PM, Ian Barwick <[hidden email]> wrote:
A possible alternative would be only to check the status each time a new
database connection is made, but that wouldn't catch the case where the
server has been promoted.

​Can we cache a false pg_is_in_recovery response and return that instead of querying the server?

David J.
Reply | Threaded
Open this post in threaded view
|

Re: Add %r substitution for psql prompts to show recovery status

Tatsuo Ishii-3
In reply to this post by Robert Haas
> On Wed, Dec 6, 2017 at 9:19 PM, Ian Barwick <[hidden email]> wrote:
>> Note this substitution sends a "pg_is_in_recovery()" query to the server
>> each time it's encountered; unless there's something I'm overlooking I
>> think that's the only reliable way to determine current recovery status.
>
> That seems kinda painful.
>
> And what happens in an aborted transaction?

Yeah. I think we need some from help backend for this. For example, a
parameter status message can be used here.  If PostgreSQL moves to the
recovery state or vice versa, PostgreSQL sends the parameter status
message to frontend.

Best regards,
--
Tatsuo Ishii
SRA OSS, Inc. Japan
English: http://www.sraoss.co.jp/index_en.php
Japanese:http://www.sraoss.co.jp

Reply | Threaded
Open this post in threaded view
|

Re: Add %r substitution for psql prompts to show recovery status

Alexander Korotkov-3
On Fri, Dec 8, 2017 at 3:54 AM, Tatsuo Ishii <[hidden email]> wrote:
> On Wed, Dec 6, 2017 at 9:19 PM, Ian Barwick <[hidden email]> wrote:
>> Note this substitution sends a "pg_is_in_recovery()" query to the server
>> each time it's encountered; unless there's something I'm overlooking I
>> think that's the only reliable way to determine current recovery status.
>
> That seems kinda painful.
>
> And what happens in an aborted transaction?

Yeah. I think we need some from help backend for this. For example, a
parameter status message can be used here.  If PostgreSQL moves to the
recovery state or vice versa, PostgreSQL sends the parameter status
message to frontend.

If we would use parameter status messages, then can we handle compatibility correctly?  So that new psql can work with old backend without errors (and without recovery status display, for sure), and old psql can work with new backend without errors.

Because seeing recovery status in psql prompt is very neat.  But execution of extra query each time doesn't seem like reasonable price for it...

------
Alexander Korotkov
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company 
Reply | Threaded
Open this post in threaded view
|

Re: Add %r substitution for psql prompts to show recovery status

Tatsuo Ishii-3
>> Yeah. I think we need some from help backend for this. For example, a
>> parameter status message can be used here.  If PostgreSQL moves to the
>> recovery state or vice versa, PostgreSQL sends the parameter status
>> message to frontend.
>>
>
> If we would use parameter status messages, then can we handle compatibility
> correctly?  So that new psql can work with old backend without errors (and
> without recovery status display, for sure),

I suppose so.

> and old psql can work with new
> backend without errors.

It should work because any frontend program should ignore parameter
status messages they do not know, according to the docs.

> Because seeing recovery status in psql prompt is very neat.  But execution
> of extra query each time doesn't seem like reasonable price for it...

I think so.

Best regards,
--
Tatsuo Ishii
SRA OSS, Inc. Japan
English: http://www.sraoss.co.jp/index_en.php
Japanese:http://www.sraoss.co.jp

Previous Thread Next Thread