How to do linux debugging PostgreSQL remotely using Visual Studio Code(Using autoconf+make)

1. Prerequisites

Version of PostgreSQL: PostgreSQL13

Target Machine: CentOS7

Host Machine: Windows10

2. Preparation in target machine

Hereinafter we assume your username is postgres. Perform the follwing steps in target machine according to [1]

(1) Download sources of PostgreSQL from official website and change directory

tar -xzvf ./postgresql-13.0.tar.gz
cd ./postgresql-13.0

(2) Install optional softwares as your environment

sudo yum install readline-devel zlib-devel systemd-devel

(3) Configure. In the following, “–with-systemd” is for register postgresql service

./configure –enable-debug –with-systemd –prefix=/usr/local/pgsql

(4) Remove a flag which means code optimization from “src/”

CFLAGS = -Wall (…) -fexcess-precision=standard -g -O2
CXXFLAGS = -Wall (…) -g -O2
CFLAGS = -Wall (…) -fexcess-precision=standard -g
CXXFLAGS = -Wall (…) -g

(5) Execute build and install
sudo make install

(6) (Optional) Set search path for shared libraries for PostgreSQL

sudo /sbin/ldconfig /usr/local/pgsql/lib

(7) To set Path for PostgreSQL, append ~/.bach_profile or /etc/profile the following lines

export PATH

(8) Create data directory. Hereinafter we call this direcroty <datadir>

mkdir <datadir>

(9) Initialize database

initdb -D <datadir>

(10) Create service unit file “/etc/systemd/system/postgresql-13.service” as follow

Description=PostgreSQL13 database server

ExecStart=/usr/local/pgsql/bin/postgres -D <datadir>
ExecReload=/bin/kill -HUP $MAINPID


(11) (Optional) Configure the system automatically boot postgresql-13 services at boot

sudo systemctl enable postgresql-13

(12) Start postgresql-13 service

sudo systemctl start postgresql-13

(13) Connect to PostgreSQL

psql -U postgres

(14) Create sample table and insert sample data into the table in psql terminal

create table t1(id integer, name text);
insert into t1 values(1, ‘hoge1’);
insert into t1 values(2, ‘hoge2’);

(15) Show the pid of PostgreSQL server in psql terminal

select pg_backend_pid();

3. Preparation in host machine

(1) According to [3] etc., install Visual Studio Code and set up for remote development using extensions, Remote-SSH, C/C++, etc.

(2) Select [Run]>[Add Configuration] and add the following configuration to “launch.json”

“name”: “(gdb) Attach”,
“type”: “cppdbg”,
“request”: “attach”,
“program”: “/usr/local/pgsql/bin/postgres”,
“processId”: “${command:pickProcess}”,
“MIMode”: “gdb”,
“setupCommands”: [
“description”: “Enable pretty-printing for gdb”,
“text”: “-enable-pretty-printing”,
“ignoreFailures”: true

4. Debugging

(1) In host machine, select [Run]>[Start Debugging] and select pid of 2.(14)

(2) In host machine, view [Breakpoints] and set a breakpoint on any function, for example, planner.

(3) In target machine, start sample query, for example, “select * from t1 where id = 1”. If you can see PostgreSQL server stopping at planner() in planner.c, debugging success..

5. References

[1] PostgreSQL official document, Chapter 16. Installation from Source Code

[2] PostgreSQL source file browser

[3] Remote Development using SSH

[4] Configuring C/C++ debugging

Published by ktke109

I love open souce database management systems.