How to Debug PostgreSQL

First, compile the source code use ./configure --prefix=XXXX --enable-debug && CFLAGS=-O0

then make and make install.

IF you want to RESTART PG

Detected if there is an instance of Postgre server. If there is, kill it. use
1
2
3
ps -e | grep postgre
// find id of the process
kill -9 $processid

to detect the pocess of postgresql and kill it.

then use:

1
2
3
4
gdb postgresqlHomeDIR/bin/postgres -quiet
// enter gdb then use
set args -D DATADIR
run

here DATADIR and postgresqlDIR are your own directory.

IF you don't want to RESTART PG

1
2
3
4
5
6
7
// find postgres pid
pid = `ps auxww | \
grep postgres: | \
grep -v -e 'grep postgres:' -e 'postgres: stats' -e 'postgres: writer' -e 'postgres: archiver' -e 'postgres: logger' -e 'postgres: autovacuum' | \
tee /dev/tty | \
awk '{print $2}'`
gdb pid

then use

1
./psql test

to test. Here test is  your database name.

IF you want to debug contrib

make && make install in contrib/yourdir

then go to gdb to file your shard lib to load symbol. Here XXX.so is your contrib share lib.

1
(gdb) file XXX.so

if not, when you use break somefunction, it will report:

No symbol table is loaded.  Use the “file” command.
Make breakpoint pending on future shared library load? (y or [n])