Re: [Patch] New psql prompt substitution %r (m = master, r = replica) - Mailing list pgsql-hackers

From Aleksander Alekseev
Subject Re: [Patch] New psql prompt substitution %r (m = master, r = replica)
Date
Msg-id 20160812131855.0619f9c9@e733
Whole thread Raw
In response to Re: [Patch] New psql prompt substitution %r (m = master, r = replica)  (Michael Paquier <michael.paquier@gmail.com>)
Responses Re: [Patch] New psql prompt substitution %r (m = master, r = replica)
List pgsql-hackers
Thank you everyone for your replies!

I did some research and apparently the is no need in any patch. As
David pointed out what I did could be easily done using \gset:

```
$ cat ~/.psqlrc 
select (case when pg_is_in_recovery() then 'r' else 'm' end) as mor
\gset

\set PROMPT1 '%p (%:mor:) =# '
```

Besides I figured out that replica promotion case could also be properly
handled without any patches. In case anyone is interested here is a
brief description of a solution.

~/.bash_profile:

```
export PATH="/home/eax/bin:$PATH"
alias psql='psql_wrapper'
```

~/bin/psql_wrapper:

```
#!/usr/bin/env python3

import subprocess
import sys

arg_string = ""
idx = 1
maxidx = len(sys.argv) - 1

while idx <= maxidx: arg_string += "'" + sys.argv[idx] + "' " idx += 1

cmd = """USER_ARGS=$'{}' psql {}""".format( arg_string.replace("'","\\'"), arg_string)

subprocess.call(cmd, shell = True)
```

~/.psqlrc:

```
\set PROMPT1 '%p <%`sh -c "psql $USER_ARGS -A -t -c $\'select case when pg_is_in_recovery() then \\\'replica\\\' else
\\\'master\\\'end\'"`> =# '
 
```

-- 
Best regards,
Aleksander Alekseev



pgsql-hackers by date:

Previous
From: Craig Ringer
Date:
Subject: Re: [PATCH] COPY vs \copy HINT
Next
From: maksim
Date:
Subject: Fwd: [BUG] Print timing statistics of trigger execution under turned off timing option of EXPLAIN ANALYZE