Postgres/Postgres Internal

MacOS의 VSCode 환경에서 PostgreSQL 디버깅

moxie2ks 2025. 2. 2. 22:28
728x90
반응형

개요

MacOS기반의 VSCode에서 lldb를 사용하여 PostgreSQL을 디버깅하는 환경을 만들고, VSCode 내에서 breakpoint를 만들어 라인별로 코드의 동작 흐름을 확인해 보자.

환경 설정

./configure 실행과 관련한 옵션만 수정하고, 업데이트 된 내용만 기술한다.

./configure 실행

cd postgres
./configure --prefix=$HOME/postgres/pg14 --enable-cassert \\
--enable-debug  CFLAGS="-ggdb -O0 -fno-omit-frame-pointer" CPPFLAGS="-g -O0"
  • prefix - folder where Postgres will be installed
  • enable-cassert - Enable C Assert statements
  • enable-debug - Enable Debug mode in build
  • CFLAGS - C compiler flags
    • O0 - disable optimisation
    • g - generate debug symbols
  • CPPFLAGS - C++ compiler flags I have given postgres/pg14 folder under my HOME directory as installation location for this Postgres build.

./configure 과정이 끝나면 아래와 같은 메세지를 확인할 수 있다.

config.status: linking src/include/port/darwin.h to src/include/pg_config_os.h config.status: linking src/makefiles/Makefile.darwin to src/Makefile.port

./configure 실행 이후, root/src 폴더에 ‘Makefile.global’ 파일이 생성되었을 것이다. 이때, optimizations들이 비활성화되고, 빌드과정에서 debug symbol이 활성화되었는지 확인해야 한다.

src/Makefile.global file을 열고 CFLAGS 와 CPPFLAGS 변수들을 찾아 -g와 -O0 flag가 있는지 확인한다.

설치

make && make install 커맨드를 실행하여 build 및 설치를 진행한다.

본 과정에서는 $HOME/postgres/pg14 폴더에 설치할 예정이다.

설치에 성공하면 설치 폴더에 들어가 아래 커맨드를 실행하여 build된 파일들이 debug symbol을 가지고 있는지 확인한다.

cd $HOME/postgres/pg14/bin
nm -pa ./postgres | grep OSO
  • nm 은 바이너리에서 symbol table name을 나열하는 데 사용된다.
  • PG14의 경우, nm 커맨드를 사용하면 747 개가 나타난다.

PGDATA 설정

Initialize a PGDATA folder for the database using initdb, I’m using $HOME/postgres/pgdata as my data directory,

initdb 사용을 위한 PGDATA 폴더를 초기화 한다. 본 장에서는 $HOME/postgres/pgdata 경로를 사용한다.

$HOME/postgres/pg14/bin/initdb -D $HOME/postgres/pgdata

실행 결과는 아래와 유사하게 나온다.

Success. You can now start the database server using:

/Users/me/postgres/pg14/bin/pg_ctl -D /Users/me/postgres/pgdata -l logfile start

Database 시작

아래 커맨드로 database를 start한다.

$HOME/postgres/pg14/bin/pg_ctl -D $HOME/postgres/pgdata -l logfile start

실행 결과는 아래와 같이 나온다.

waiting for server to start…. done

server started

DB 생성 및 simple query 수행

$HOME/postgres/pg14/bin/createdb sample
$HOME/postgres/pg14/bin/psql sample

아래 쿼리를 작성하여 기본 table을 생성한다.

create table hello(id int, message text);
insert into hello values(1, 'Hello world!');
select from hello;

VSCode에서 lldb 실행

Code Runner라는 VSCode extension 설치

Postgres소스코드를 VS code로 열고 launch.json file을 생성

launch.json 파일 수정

{
 *// Use IntelliSense to learn about possible attributes.
 // Hover to view descriptions of existing attributes.
 // For more information, visit: <https://go.microsoft.com/fwlink/?linkid=830387*>
 "version": "0.2.0",
 "configurations": [
     {
         "name": "(lldb) Attach DB",
         "type": "cppdbg",
         "request": "attach",
         "program": "${env:HOME}/postgres/pg14/bin/postgres",
         "MIMode": "lldb",
         "targetArchitecture": "arm64"
     },
 ]
}
  • targetArchitecture 이 arm64로 설정된 것은 M1 mac을 위한 설정이다.
  • program 은 설치된 설치된 폴더 안의 bin/폴더 안에 있는 postgres binary를 지정해야 한다.

PID 확인

실행되고 있는 sample db의 pid를 아래 쿼리로 확인한다.

select pg_backend_pid();

pg_backend_pid()는 현재 session의 postgres backend의 process id를 리턴하는 system 함수이다.

실행 결과는 아래와 유사하게 나온다.

pg_backend_pid

—————-

11340

(1 row)

VSCode로 디버깅

본 과정에서는 src/backend/tcop/postgres.c파일 안에 있는 exec_simple_query 함수에 breakpoint를 설정하였다.

F5 or Run and Debug -> (lldb) Attach DB를 실행하고 pg_backend_pid() 함수로부터 얻은 pid를 입력한다.

psql 터미널 창에 SQL query를 아래와 같이 수행해 본다.

select from hello;

아래 결과처럼, VSCode가 breakpoint에 멈추게 되고 여러 가지 정보들을 확인할 수 있다.

참조

Build and Debug Postgres using VS Code on a Mac

 

728x90
반응형