{"id":981,"date":"2024-03-30T19:12:23","date_gmt":"2024-03-30T10:12:23","guid":{"rendered":"https:\/\/osmanthus.work\/?p=981"},"modified":"2024-04-03T07:58:38","modified_gmt":"2024-04-02T22:58:38","slug":"how-to-debug-postgresql-remotely-using-visual-studio-code-in-linux-2-2","status":"publish","type":"post","link":"https:\/\/osmanthus.work\/?p=981","title":{"rendered":"How to do linux debugging TimescaleDB remotely using Visual Studio Code"},"content":{"rendered":"\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">1. Prerequisites<\/h2>\n\n\n\n<p>Version of TimescaleDB: TimescaleDB2.13<\/p>\n\n\n\n<p>Version of PostgreSQL: PostgreSQL16<\/p>\n\n\n\n<p>Target Machine: Rocky Linux 8 &amp;  4core cpu<\/p>\n\n\n\n<p>Host Machine: Windows10<\/p>\n\n\n\n<p>See [5] for TimescaleDB version and PostgreSQL version compatibility.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Preparation in target machine<\/h2>\n\n\n\n<p>Hereinafter we assume your username is postgres. Perform the follwing steps in target machine according to [1]<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.1 Installation of PostgreSQL<\/h2>\n\n\n\n<p>(1) Download sources of PostgreSQL from Github repository and change directory<br>mkdir ts-postgres<br>cd ts-postgres<br>git clone https:\/\/github.com\/postgres\/postgres<br>cd .\/postgres<\/p>\n\n\n\n<p>(2) Install optional softwares as your environment<br>sudo dnf install readline-devel zlib-devel systemd-devel openssl-devel<\/p>\n\n\n\n<p>(3) Configure. In the following, &#8220;&#8211;with-systemd&#8221; is for register postgresql service<br>.\/configure &#8211;enable-debug &#8211;with-systemd &#8211;prefix=\/usr\/local\/pgsql\/16 &#8211;with-openssl<\/p>\n\n\n\n<p>(4) Remove a flag which means code optimization from &#8220;src\/Makefile.global&#8221;<br>before:<br>CFLAGS = -Wall (\u2026) -fexcess-precision=standard -g -O2<br>CXXFLAGS = -Wall (\u2026) -g -O2<br><\/p>\n\n\n\n<p>after:<br>CFLAGS = -Wall (\u2026) -fexcess-precision=standard -g<br>CXXFLAGS = -Wall (\u2026) -g<\/p>\n\n\n\n<p>(5) Execute build and install<br>make<br>sudo make install<\/p>\n\n\n\n<p>(6) (Optional) Set search path for shared libraries for PostgreSQL<\/p>\n\n\n\n<p>sudo \/sbin\/ldconfig \/usr\/local\/pgsql\/lib<\/p>\n\n\n\n<p>(7) To set Path for PostgreSQL, append ~\/.bach_profile or \/etc\/profile the following lines<br>export PATH=\/usr\/local\/pgsql\/16\/bin:$PATH<\/p>\n\n\n\n<p>(8) Create data directory. Hereinafter we call this direcroty &lt;datadir&gt;<\/p>\n\n\n\n<p>mkdir &lt;datadir&gt;<\/p>\n\n\n\n<p>(9) Initialize database<\/p>\n\n\n\n<p>initdb -U postgres -D &lt;datadir><\/p>\n\n\n\n<p>(10) Create service unit file &#8220;\/etc\/systemd\/system\/postgresql-16.service&#8221; as follow<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[Unit]\nDescription=PostgreSQL16 database server\nDocumentation=man:postgres(1)\n\n[Service]\nType=notify\nUser=postgres\nExecStart=\/usr\/local\/pgsql\/bin\/postgres -D &lt;datadir>\nExecReload=\/bin\/kill -HUP $MAINPID\nKillMode=mixed\nKillSignal=SIGINT\nTimeoutSec=0\n\n[Install]\nWantedBy=multi-user.target<\/pre>\n\n\n\n<p>(11) (Optional) Configure the system automatically boot postgresql-16 services at boot<\/p>\n\n\n\n<p>sudo systemctl enable postgresql-16<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.2 Installation of TimescaleDB<\/h2>\n\n\n\n<p>(1) Download sources of TimescaleDB from Github repository and change directory<br>cd ~<br>git clone https:\/\/github.com\/timescale\/timescaledb<br>cd timescaledb<br>git checkout 2.14.0<\/p>\n\n\n\n<p>(2) Install optional softwares as your environment<br>sudo dnf install cmake<\/p>\n\n\n\n<p>(3) Build and Install<br>.\/bootstrap<br>cd build<br>make -j4<br>sudo make install<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.3 Create hypertable<\/h2>\n\n\n\n<p>(1) Start postgresql-16 service<br>sudo systemctl start postgresql-16<\/p>\n\n\n\n<p>(2) Connect to PostgreSQL<br>psql -U postgres<\/p>\n\n\n\n<p>(3) Create database and extension<br>create database example;<br>\\c example<br>create extension if not exists timescaledb;<\/p>\n\n\n\n<p><br>(4) Create sample hypertable and insert sample data into the table in psql terminal<br>create table conditions (<br>time timestamptz not null,<br>location text not null,<br>device text not null,<br>temperature double precision null,<br>humidity double precision null<br>);<br>select create_hypertable(&#8216;conditions&#8217;, by_range(&#8216;time&#8217;));<br>insert into conditions(time, location, device, temperature, humidity) values (now(), &#8216;office&#8217;, &#8216;device1&#8217;, 70.0, 50.0);<\/p>\n\n\n\n<p>(5) Show the pid of PostgreSQL server in psql terminal<br>create view pid as select pg_backend_pid();<br>table pid;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Preparation in host machine<\/h2>\n\n\n\n<p>(1) According to [3] etc., install Visual Studio Code and set up for remote development using extensions, Remote-SSH, C\/C++, etc. <\/p>\n\n\n\n<p>(2) Select [Run]&gt;[Add Configuration] and add the following configuration to &#8220;launch.json&#8221;<\/p>\n\n\n\n<p>{<br>    &#8220;name&#8221;: &#8220;(gdb) Attach&#8221;,<br>    &#8220;type&#8221;: &#8220;cppdbg&#8221;,<br>    &#8220;request&#8221;: &#8220;attach&#8221;,<br>    &#8220;program&#8221;: &#8220;\/usr\/local\/pgsql\/16\/bin\/postgres&#8221;,<br>    &#8220;processId&#8221;: &#8220;${command:pickProcess}&#8221;,<br>    &#8220;MIMode&#8221;: &#8220;gdb&#8221;,<br>    &#8220;setupCommands&#8221;: [<br>        {<br>             &#8220;description&#8221;: &#8220;Enable pretty-printing for gdb&#8221;,<br>             &#8220;text&#8221;: &#8220;-enable-pretty-printing&#8221;,<br>             &#8220;ignoreFailures&#8221;: true<br>        }<br>    ]<br>}<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Debugging<\/h2>\n\n\n\n<p>(1) In host machine, select [Run]&gt;[Start Debugging] and select pid of 2.(14)<\/p>\n\n\n\n<p>(2) In host machine, view [Breakpoints] and set a breakpoint on any function, for example, timescaledb_planner().<\/p>\n\n\n\n<p>(3) In target machine, start sample query, for example, &#8220;select * from conditions where device = &#8216;device1&#8217; &#8220;. If you can see PostgreSQL server stopping at timescaledb_planner() in planner.c, debugging success..<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. References<\/h2>\n\n\n\n<p>[1] PostgreSQL official document, Chapter\u00a017.\u00a0Installation from Source Code<br><a href=\"https:\/\/www.postgresql.org\/docs\/16\/installation.html\">https:\/\/www.postgresql.org\/docs\/16\/installation.html<\/a><\/p>\n\n\n\n<p>[2] Remote Development using SSH<br><a href=\"https:\/\/code.visualstudio.com\/docs\/remote\/ssh\">https:\/\/code.visualstudio.com\/docs\/remote\/ssh<\/a><\/p>\n\n\n\n<p>[3] Configuring C\/C++ debugging<br><a href=\"https:\/\/code.visualstudio.com\/docs\/cpp\/launch-json-reference\">https:\/\/code.visualstudio.com\/docs\/cpp\/launch-json-reference<\/a><\/p>\n\n\n\n<p>[4] TimescaleDB document, Upgrade PostgreSQL<br><a href=\"https:\/\/docs.timescale.com\/self-hosted\/latest\/upgrades\/upgrade-pg\/\">https:\/\/docs.timescale.com\/self-hosted\/latest\/upgrades\/upgrade-pg\/<\/a><\/p>\n\n\n\n<p>[5] Install self-hosted TimescaleDB from source<br><a href=\"https:\/\/docs.timescale.com\/self-hosted\/latest\/install\/installation-source\/\">https:\/\/docs.timescale.com\/self-hosted\/latest\/install\/installation-source\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-981","post","type-post","status-publish","format-standard","hentry","category-postgresql","entry"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/osmanthus.work\/index.php?rest_route=\/wp\/v2\/posts\/981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/osmanthus.work\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osmanthus.work\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osmanthus.work\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/osmanthus.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=981"}],"version-history":[{"count":25,"href":"https:\/\/osmanthus.work\/index.php?rest_route=\/wp\/v2\/posts\/981\/revisions"}],"predecessor-version":[{"id":1017,"href":"https:\/\/osmanthus.work\/index.php?rest_route=\/wp\/v2\/posts\/981\/revisions\/1017"}],"wp:attachment":[{"href":"https:\/\/osmanthus.work\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osmanthus.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osmanthus.work\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}