Postgres/Postgres Internal

VSCode의 Remote - SSH를 활용한 postgres 코드 디버깅

moxie2ks 2025. 2. 15. 18:01
728x90
반응형

개요

지난번에 리서치했던 MacOS의 VSCode 환경에서 PostgreSQL 디버깅에 이어서, Windows OS의 VSCode에서 SSH를 통해 MacOS의 PostgreSQL 소스코드를 접속 후, lldb를 사용하여 PostgreSQL을 디버깅하는 환경을 만들어 보자. 이 작업을 통해, 소스코드가 없는 환경에서도 구동 중인 Postgres서버와 소스코드가 존재하는 노드의 접속정보만 있다면, OS에 상관없이 소스코드를 디버깅해볼 수 있다.

사전작업

Remote(MacOS)에 테스트를 위한 아래의 사전 작업을 진행한다.

[Remote] 사전작업1 - 테이블 생성

create table my_table (key numeric(2), name varchar(14), age numeric(3));

[Remote] 사전작업2 - 테이블에 데이터 추가

insert into my_table values (01, 'Kisoon', 21);
insert into my_table values (02, 'Umar', 17);

[Local & Remote] VSCode extension 설치 - Code Runner

[Local & Remote] VSCode extension 설치 - Remote - SSH

[Local] 좌하단 "원격 창 열기" 버튼

[Local] Connect to Host…

[Local] Add New SSH Host…

[Local] Remote 접속을 위한 ssh 커맨드 입력

[Local] 비밀번호 입력(2회 입력 예상)

[Local] 접속 후 Open Folder -> 빌드한 소스코드 디렉토리 열기

[Local] 터미널 창을 열어서 backend pid 확인

select pg_backend_pid();

[Local] Debug 탭에서 "create a launch.json file" -> launch.json file을 생성

[Local] 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": "<path to postgres>/postgres",
            "MIMode": "lldb", // 또는 "gdb",
            "targetArchitecture": "arm64" // 생략 가능
        },
   ]
}
  • targetArchitecture 이 arm64로 설정된 것은 M1 mac을 위한 설정이며, 생략 가능하다.
  • program 은 설치된 설치된 폴더안의 bin/ 폴더 안에 있는 postgres binary를 지정해야 한다.

[Local] F5 or Run and Debug

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

아래 화면이 나타나면, Remote 서버에서 DevToolsSecurity -enable을 수행한다.

pid를 입력하여 (lldb) Attach DB를 실행

테스트

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

select * from my_table;

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

아래 버튼들을 활용하여 디버깅한다.

참고문헌

Debugging PostgreSQL on a remote machine with Visual Studio Code: A Step-by-Step Guide - Highgo Software Inc.

728x90
반응형