Comfortably check BackendPID with psql
Hi, hackers!
This is a so tiny patch but I think it is very useful for hackers and DBAs.
When we debug with psql, we frequently use "SELECT pg_backend_pid();".
This can change the input of the 24 characters to the only 4 characters!
Image.
----------------------
naoya=# \bid
Backend Process ID
pid
------
1716
(1 row)
-----------------------
How do you like it?
Regards,
Naoya
---
Naoya Anzai
Engineering Department
NEC Solution Inovetors, Ltd.
E-Mail: nao-anzai@xc.jp.nec.com
---
Attachments:
psql_show_backend_pid.patchapplication/octet-stream; name=psql_show_backend_pid.patchDownload
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 38253fa..76b7276 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -204,6 +204,14 @@ exec_command(const char *cmd,
success = do_pset("format", "unaligned", &pset.popt, pset.quiet);
}
+ /*
+ * \bid -- show backend pid
+ */
+ if (strcmp(cmd,"bid") == 0)
+ {
+ success = showBackendPid();
+ }
+
/* \C -- override table title (formerly change HTML caption) */
else if (strcmp(cmd, "C") == 0)
{
diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index db56809..7a845d8 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -737,6 +737,36 @@ listAllDbs(const char *pattern, bool verbose)
return true;
}
+/*
+ * \bid
+ *
+ * Show Backend PID
+ */
+bool
+showBackendPid()
+{
+ PQExpBufferData buf;
+ PGresult *res;
+ printQueryOpt myopt = pset.popt;
+
+ initPQExpBuffer (&buf);
+ printfPQExpBuffer(&buf, "SELECT pg_backend_pid as pid from pg_backend_pid()");
+
+ res = PSQLexec(buf.data);
+ termPQExpBuffer(&buf);
+ if(!res)
+ return false;
+
+ myopt.nullPrint = NULL;
+ myopt.title = _("Backend Process ID");
+ myopt.translate_header = true;
+
+ printQuery(res, &myopt, pset.queryFout, pset.logfile);
+
+ PQclear(res);
+ return true;
+
+}
/*
* List Tables' Grant/Revoke Permissions
diff --git a/src/bin/psql/describe.h b/src/bin/psql/describe.h
index 822e71a..7c77bd5 100644
--- a/src/bin/psql/describe.h
+++ b/src/bin/psql/describe.h
@@ -57,6 +57,9 @@ extern bool listTSTemplates(const char *pattern, bool verbose);
/* \l */
extern bool listAllDbs(const char *pattern, bool verbose);
+/* \bid */
+extern bool showBackendPid(void);
+
/* \dt, \di, \ds, \dS, etc. */
extern bool listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem);
On 6/11/15 11:41 AM, Naoya Anzai wrote:
This can change the input of the 24 characters to the only 4 characters!
Image.
----------------------
naoya=# \bid
Backend Process ID
pid
------
1716
(1 row)
-----------------------How do you like it?
Seems easier to set this in .psqlrc:
\set bid 'select pg_backend_pid();'
and then:
=# :bid
pg_backend_pid
----------------
84430
(1 row)
No patches or concerns for psql version necessary.
.m
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
Hi,
On 2015-06-11 09:41:17 +0000, Naoya Anzai wrote:
This is a so tiny patch but I think it is very useful for hackers and DBAs.
When we debug with psql, we frequently use "SELECT pg_backend_pid();".
This can change the input of the 24 characters to the only 4 characters!
Not a big fan of that abbreviation itself. What I'd wondered about
instead - and actually had patched into my psql at some point - is
adding an appropriate escape to psql's PROMPT. I think that'd serve your
purpose as well?
Regards,
Andres
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
Hi, Andres, Marko
Seems easier to set this in .psqlrc:
oops! I've never noticed..
Thank you for your comment.
Regards,
Naoya
---
Naoya Anzai
Engineering Department
NEC Solution Inovetors, Ltd.
E-Mail: nao-anzai@xc.jp.nec.com
---
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
On 6/11/15 4:55 AM, Andres Freund wrote:
Hi,
On 2015-06-11 09:41:17 +0000, Naoya Anzai wrote:
This is a so tiny patch but I think it is very useful for hackers and DBAs.
When we debug with psql, we frequently use "SELECT pg_backend_pid();".
This can change the input of the 24 characters to the only 4 characters!Not a big fan of that abbreviation itself. What I'd wondered about
instead - and actually had patched into my psql at some point - is
adding an appropriate escape to psql's PROMPT. I think that'd serve your
purpose as well?
+3.14159; that would be hugely helpful when using gdb.
--
Jim Nasby, Data Architect, Blue Treble Consulting, Austin TX
Data in Trouble? Get it in Treble! http://BlueTreble.com
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
On Thu, Jun 11, 2015 at 04:05:13PM -0500, Jim Nasby wrote:
On 6/11/15 4:55 AM, Andres Freund wrote:
On 2015-06-11 09:41:17 +0000, Naoya Anzai wrote:
This is a so tiny patch but I think it is very useful for hackers and DBAs.
When we debug with psql, we frequently use "SELECT pg_backend_pid();".
This can change the input of the 24 characters to the only 4 characters!Not a big fan of that abbreviation itself. What I'd wondered about
instead - and actually had patched into my psql at some point - is
adding an appropriate escape to psql's PROMPT. I think that'd serve your
purpose as well?+3.14159; that would be hugely helpful when using gdb.
You can get that today. In ~/.psqlrc:
SELECT pg_catalog.pg_backend_pid() AS backend_pid \gset
\set PROMPT1 '%m %:backend_pid: %/%R%# '
It doesn't update after \connect, but the overlap between my use of \connect
and my use of debuggers is tiny.
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
Not a big fan of that abbreviation itself. What I'd wondered about
instead - and actually had patched into my psql at some point - is
adding an appropriate escape to psql's PROMPT. I think that'd serve your
purpose as well?+3.14159; that would be hugely helpful when using gdb.
You can get that today. In ~/.psqlrc:
SELECT pg_catalog.pg_backend_pid() AS backend_pid \gset
\set PROMPT1 '%m %:backend_pid: %/%R%# 'It doesn't update after \connect, but the overlap between my use of \connect
and my use of debuggers is tiny.
Thank you all!
My hack is going to be much smoother.
Regards,
Naoya
---
Naoya Anzai
Engineering Department
NEC Solution Inovetors, Ltd.
E-Mail: nao-anzai@xc.jp.nec.com
---
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
On 12/06/2015 06:56, Noah Misch wrote:
On Thu, Jun 11, 2015 at 04:05:13PM -0500, Jim Nasby wrote:
On 6/11/15 4:55 AM, Andres Freund wrote:
On 2015-06-11 09:41:17 +0000, Naoya Anzai wrote:
This is a so tiny patch but I think it is very useful for hackers and DBAs.
When we debug with psql, we frequently use "SELECT pg_backend_pid();".
This can change the input of the 24 characters to the only 4 characters!Not a big fan of that abbreviation itself. What I'd wondered about
instead - and actually had patched into my psql at some point - is
adding an appropriate escape to psql's PROMPT. I think that'd serve your
purpose as well?+3.14159; that would be hugely helpful when using gdb.
+1
You can get that today. In ~/.psqlrc:
SELECT pg_catalog.pg_backend_pid() AS backend_pid \gset
\set PROMPT1 '%m %:backend_pid: %/%R%# 'It doesn't update after \connect, but the overlap between my use of \connect
and my use of debuggers is tiny.
Tiny for me too, but I sometimes had the need.
I can't really see any good reason not to add a %p escape to psql's
PROMPT, so I'm attaching a simple patch to implement it. Unless someone
objects, I'll add it to the next commitfest.
--
Julien Rouhaud
http://dalibo.com - http://dalibo.org
Attachments:
psql_pid-v1.patchtext/x-patch; name=psql_pid-v1.patchDownload
*** a/doc/src/sgml/ref/psql-ref.sgml
--- b/doc/src/sgml/ref/psql-ref.sgml
***************
*** 3365,3370 **** testdb=> <userinput>INSERT INTO my_table VALUES (:'content');</userinput>
--- 3365,3377 ----
</varlistentry>
<varlistentry>
+ <term><literal>%p</literal></term>
+ <listitem>
+ <para>The PID of the backend.</para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
<term><literal>%></literal></term>
<listitem><para>The port number at which the database server is listening.</para></listitem>
</varlistentry>
*** a/src/bin/psql/prompt.c
--- b/src/bin/psql/prompt.c
***************
*** 34,39 ****
--- 34,40 ----
* %M - database server "hostname.domainname", "[local]" for AF_UNIX
* sockets, "[local:/dir/name]" if not default
* %m - like %M, but hostname only (before first dot), or always "[local]"
+ * %p - backend PID
* %> - database server port number
* %n - database user name
* %/ - current database
***************
*** 161,166 **** get_prompt(promptStatus_t status)
--- 162,172 ----
if (pset.db)
strlcpy(buf, session_username(), sizeof(buf));
break;
+ /* backend PID */
+ case 'p':
+ if (pset.db)
+ snprintf(buf, sizeof(buf), "%d", PQbackendPID(pset.db));
+ break;
case '0':
case '1':
On 2015-07-05 14:11:38 +0200, Julien Rouhaud wrote:
Tiny for me too, but I sometimes had the need.
I can't really see any good reason not to add a %p escape to psql's
PROMPT, so I'm attaching a simple patch to implement it. Unless someone
objects, I'll add it to the next commitfest.
Pushed the patch. I only made a minor belt-and-suspenders type of
change, namely to check whether PQbackendPID() returns 0 and not print
that and replaced PID by pid in the docs and comments.
Thanks for the patch!
Greetings,
Andres Freund
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
Le 07/07/2015 13:41, Andres Freund a écrit :
On 2015-07-05 14:11:38 +0200, Julien Rouhaud wrote:
Tiny for me too, but I sometimes had the need.
I can't really see any good reason not to add a %p escape to psql's
PROMPT, so I'm attaching a simple patch to implement it. Unless someone
objects, I'll add it to the next commitfest.Pushed the patch. I only made a minor belt-and-suspenders type of
change, namely to check whether PQbackendPID() returns 0 and not print
that and replaced PID by pid in the docs and comments.Thanks for the patch!
Thanks!
Greetings,
Andres Freund
--
Julien Rouhaud
http://dalibo.com - http://dalibo.org
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
Andres Freund <andres@anarazel.de> writes:
Pushed the patch. I only made a minor belt-and-suspenders type of
change, namely to check whether PQbackendPID() returns 0 and not print
that and replaced PID by pid in the docs and comments.
I would s/pid/process ID/ in the docs. "PID" is not a particularly
user-friendly term, and it's even less so if you fail to upper-case it.
regards, tom lane
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
On 2015-07-07 10:17:39 -0400, Tom Lane wrote:
I would s/pid/process ID/ in the docs. "PID" is not a particularly
user-friendly term, and it's even less so if you fail to upper-case it.
We have both pid and PID in a bunch of places in the docs, and pid in
the ones that seem more likely to be noticed (e.g. system column docs in
catalogs.sgml). And the targeted audience of PROMPT and especially %p
seems to be likely to know what a pid is.
I don't mind replacing it with process ID though. Done.
Andres
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers