pg kernel learning, record it
Article directory
Installation package
(1) perl download
https://www.perl.org/get.html
(2) diff download
http://gnuwin32.sourceforge.net/packages/diffutils.htm
(3) Add perl and diff environment variables
(4) vs2022 community installation
https://visualstudio.microsoft.com/zh-hans/vs/
(5) Install C++ desktop development
(6) v142 component installation
First cut to a single component
and search for v142
because the pg12.5 source code I downloaded is required to be compiled with v142. Then, according to the pg source code requirements, search for the corresponding component and install it. Choose the corresponding generation tool to install
I download the MSVCv142-vS 2019 C++ x64/x86 generation tool (v14.29-16.11) here
(7) Download pg source code
https://www.postgresql.org/ftp/source/
I installed 12.5
and downloaded this installation package, unzip it
Note that there cannot be Chinese paths
Compile and install
(1) Open the VS terminal
2017 is called x64 native tools command prompt, 2022 is called x64 native tools command prompt, under the directory of start menu vs,
you need to install perl and diff commands before compiling
The following commands are all executed in this terminal
Open the prompt, if it is correct, it will display Copyright or something
(2) Switch to the source code directory
cd C:\install\postgresql-12.5\src\tools\msvc
(3) compile
perl build.pl DEBUG
After compiling, there are two warnings, which can be ignored.
(4) Regression testing
perl vcregress.pl check
(5) Compile and install
perl install.pl C:\install\postgresql-12.5
(6) Initialize the library
and open a new cmd window
First switch to the bin directory of the compiled installation
cd C:\install\postgresql-12.5\bin
initialize db
initdb.exe -D ..\data
start server
pg_ctl -D ^"^.^.^\data^" -l logfile start
enter psql
psql -d postgres
Add Postgresql project to VS
There is a pgsql.sln file in the postgresql source code directory, which can be opened by double-clicking.
Or go to vs and choose to open the project and select pgsql.sln
Debug source code
(1) Start the database
Enter the /bin directory under the compilation installation directory,
Use the following two commands to start the database. If it has been started, just execute psql -d postgres
pg_ctl start -l logfile -D ../data
psql -d postgres
(2) Start Visual Studio
VS choose to open the project and select pgsql.sln
(3) Add an additional process
Then click [Debug] -> [Additional Process] in the function bar, and select the first postgres.exe in the process list. Be sure to choose the first one here, and the next few processes attached to it cannot be debugged normally.
If you want to confirm whether this process is the main process of your own PostgreSQL, you can enter:
select * from pg_backend_pid();
(4) Set a breakpoint
If you don't know where to set the breakpoint, or you can't stop at the breakpoint you set, you can open the src/backend/tcop/postgres.c file
You can search for postgres.c in the Solution Explorer
The solution explorer on the right can be displayed directly by pressing the "ctrl + alt + l" shortcut key
Then ctrl+F5, search switch (firstchar)
Set a breakpoint here
(5) debugging
Enter the sql statement in the terminal
select * from pg_backend_pid();
VS will stop at the breakpoint, and there are three buttons for debugging, namely: statement by statement, process by process and jump out.
Statement by statement (F11) is to run the next C statement that is actually executed. If it is a function, it will enter the function; process by procedure (F10) is to run the line. If it is a function, it will directly execute and stop until the next line; Jump out (shift+F11) is to jump to the place where the outer layer calls the function after running the current function
After that, you can learn the source code of PostgreSQL step by step during debugging.