Psql Command to Attach Gdb to Backend
While hacking PostgreSQL it’s very useful to know pid of the backend you are
working with. You need to know pid of the process to attach debugger, profiler
etc. Luckily, .psqlrc provides us an elegant way to define the shortcuts for
psql. Using config line below one can find out backend pid just by typing :pid
.
1
|
|
1 2 3 4 5 |
|
In 9.6 it becomes possible to even include backend pid into psql prompt.
However, it’s possible to automate more complex actions in psql. I’ve configured
my psql to run gdb attached to current backend in new tab of iTerm2 just by
typing :gdb
.
The :gdb
command selects pid of current backend and puts it to the input of
pg_debug script.
1
|
|
pg_debug extracts pid from its input and then runs OSA script which runs gdb in the new tab of iTerm2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
This script works for Mac OS X and iTerm2, but the same approach should work for other platforms and terminal emulators.