<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <author>Abhishek Chanda()</author>
    <title>Home on A blog</title>
    <link>https://achanda.dev/</link>
    <description>Recent content in Home on A blog</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <lastBuildDate>Mon, 30 Jun 2025 23:06:23 -0500</lastBuildDate>
    <atom:link href="https://achanda.dev/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Running Postgres on Cloudflare Containers</title>
      <link>https://achanda.dev/posts/postgres-cloudflare-containers/</link>
      <pubDate>Mon, 30 Jun 2025 23:06:23 -0500</pubDate>
      <guid>https://achanda.dev/posts/postgres-cloudflare-containers/</guid>
      <description>&lt;p&gt;&lt;strong&gt;Disclaimer:&lt;/strong&gt; I work for Cloudflare. The views and opinions expressed in this post are my own and do not necessarily reflect the official policy or position of Cloudflare.&lt;/p&gt;&#xA;&lt;p&gt;Cloudflare recently launched &lt;a href=&#34;https://blog.cloudflare.com/containers-are-available-in-public-beta-for-simple-global-and-programmable/&#34;&gt;Cloudflare Containers&lt;/a&gt; allowing running containerized&#xA;applications with workers. To use this, an user would need to define an application in a dockerfile, define a worker, and bind the container to that worker. Naturally, I was curious if we can run Postgres on the new&#xA;container platform.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Postgres Protocol and a Side Quest</title>
      <link>https://achanda.dev/posts/postgres-protocol-side-quest/</link>
      <pubDate>Thu, 10 Apr 2025 22:24:49 -0500</pubDate>
      <guid>https://achanda.dev/posts/postgres-protocol-side-quest/</guid>
      <description>&lt;p&gt;I was watching Heikki Linnakangas&amp;rsquo;s &lt;a href=&#34;https://www.youtube.com/watch?v=FBPubrwGKhI&#34;&gt;talk&lt;/a&gt; on the Postgres protocol. I wanted to&#xA;follow along with and see the protocol in action for myself. I ran into a problem here: my ostgres server rejected all non secure&#xA;connections. And with TLS enabled, I could not get wireshark to decode postgres protocol messages because of encryption.&lt;/p&gt;&#xA;&lt;p&gt;Now, wireshark supports decoding TLS connections using a &lt;a href=&#34;https://wiki.wireshark.org/TLS#key-log-format&#34;&gt;key log file&lt;/a&gt;. Looking&#xA;through documentation I realized that there is no way for a client to  log the TLS keys using psql. So the natural next step&#xA;was to add that support and try to get that accepted upstream in postgres. Once I understood how this could be implemented, this&#xA;became my side quest.&lt;/p&gt;</description>
    </item>
    <item>
      <title>TIL: Looking at RocksDB Events</title>
      <link>https://achanda.dev/posts/til-rocksdb-events/</link>
      <pubDate>Sun, 22 Dec 2024 21:47:47 -0600</pubDate>
      <guid>https://achanda.dev/posts/til-rocksdb-events/</guid>
      <description>&lt;p&gt;RocksDB has an &lt;a href=&#34;https://github.com/facebook/rocksdb/wiki/EventListener&#34;&gt;&lt;code&gt;eventing system&lt;/code&gt;&lt;/a&gt; that clients can use to listen on&#xA;specific events within the DB. These events are written to the log file when RocksDB runs. The name of the log file is &lt;code&gt;LOG&lt;/code&gt;&#xA;by default and is located in the database directory.&lt;/p&gt;&#xA;&lt;p&gt;The whole log file is not in json format, so we need to grep to convert the output to json. Here is a bash one-liner that&#xA;continuously tails the log file and prints out the events.&lt;/p&gt;</description>
    </item>
    <item>
      <title>pg_power: initialization and basic setup</title>
      <link>https://achanda.dev/posts/pg_power_basic_setup/</link>
      <pubDate>Thu, 19 Dec 2024 20:46:06 -0600</pubDate>
      <guid>https://achanda.dev/posts/pg_power_basic_setup/</guid>
      <description>&lt;p&gt;I have been playing around with the &lt;a href=&#34;https://www.kernel.org/doc/html/next/power/powercap/powercap.html&#34;&gt;&lt;code&gt;powercap framework&lt;/code&gt;&lt;/a&gt;. I wrote&#xA;a postgres extension that shows the energy usage of a query. Postgres has a hook mechanism that allows an extension&#xA;to override the default executor. This implementation is very simple: the extension records the current energy reading when a query starts&#xA;and then calls the actual executor that runs the query. When the query finishes, a second hook records the current energy reading. The overall&#xA;energy usage of this query is the difference between the two values.&lt;/p&gt;</description>
    </item>
    <item>
      <title>TIL: Read sysfs without root access in Linux</title>
      <link>https://achanda.dev/posts/til-read-file-without-root/</link>
      <pubDate>Sun, 10 Nov 2024 14:48:48 -0600</pubDate>
      <guid>https://achanda.dev/posts/til-read-file-without-root/</guid>
      <description>&lt;p&gt;Typically an user needs root access to be able to read sysfs data. But such root access might not be possible&#xA;in all case; an example being Postgres which cannot be run as root. I recently learned that it is possible&#xA;to set an extra &lt;a href=&#34;https://man7.org/linux/man-pages/man7/capabilities.7.html&#34;&gt;capability&lt;/a&gt; to executables&#xA;such that it can read sysfs while not being root.&lt;/p&gt;&#xA;&lt;p&gt;I wrote a simple C program to read a specific sysfs entry&lt;/p&gt;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;&#xA;#include &amp;lt;stdlib.h&amp;gt;&#xA;#include &amp;lt;fcntl.h&amp;gt;&#xA;#include &amp;lt;unistd.h&amp;gt;&#xA;#include &amp;lt;errno.h&amp;gt;&#xA;#include &amp;lt;string.h&amp;gt;&#xA;&#xA;#define SYSFS_PATH &amp;#34;/sys/devices/virtual/powercap/intel-rapl/intel-rapl:0/energy_uj&amp;#34;&#xA;#define BUFFER_SIZE 32&#xA;&#xA;int main() {&#xA;    int fd;&#xA;    char buffer[BUFFER_SIZE];&#xA;    ssize_t bytes_read;&#xA;    unsigned long long energy_uj;&#xA;&#xA;    // Open the sysfs file&#xA;    fd = open(SYSFS_PATH, O_RDONLY);&#xA;    if (fd == -1) {&#xA;        fprintf(stderr, &amp;#34;Error opening file: %s\n&amp;#34;, strerror(errno));&#xA;        return 1;&#xA;    }&#xA;&#xA;    // Read the contents of the file&#xA;    bytes_read = read(fd, buffer, BUFFER_SIZE - 1);&#xA;    if (bytes_read == -1) {&#xA;        fprintf(stderr, &amp;#34;Error reading file: %s\n&amp;#34;, strerror(errno));&#xA;        close(fd);&#xA;        return 1;&#xA;    }&#xA;&#xA;    // Null-terminate the buffer&#xA;    buffer[bytes_read] = &amp;#39;\0&amp;#39;;&#xA;&#xA;    // Close the file descriptor&#xA;    close(fd);&#xA;&#xA;    // Convert the string to an unsigned long long&#xA;    energy_uj = strtoull(buffer, NULL, 10);&#xA;&#xA;    // Print the result&#xA;    printf(&amp;#34;Energy (µJ): %llu\n&amp;#34;, energy_uj);&#xA;&#xA;    return 0;&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;When I compile and run it as my regular non-root user, I get this&lt;/p&gt;</description>
    </item>
    <item>
      <title>Mapping Between Pgbouncer and Postgres Connection States</title>
      <link>https://achanda.dev/posts/mapping-between-pgbouncer-postgres-state/</link>
      <pubDate>Sun, 27 Oct 2024 11:21:48 -0500</pubDate>
      <guid>https://achanda.dev/posts/mapping-between-pgbouncer-postgres-state/</guid>
      <description>&lt;p&gt;Pgbouncer is a popular connection pooler for Postgres. Connections from the end user to pgbouncer&#xA;are called client connections from it&amp;rsquo;s point of view. Connections from pgbouncer to Postgres are&#xA;called server connections. In a typical deployment, end users&#xA;connect to pgbouncer, which maintains a pool of persistent server connections. The pooler assigns&#xA;a server connection to a client connection based on the given pooling mode (that is beyond the scope&#xA;of this discussion). Thus, from Postgres&amp;rsquo; point of view, the client is actually pgbouncer which gets&#xA;it&amp;rsquo;s own backend. Hence there is always a 1:1 correspondence between pgbouncer&amp;rsquo;s server connection and&#xA;it&amp;rsquo;s backend in postgres. A mental model for their relationship is useful for debugging pgbouncer&#xA;related issues. We will start with some definitions to establish that mapping.&lt;/p&gt;</description>
    </item>
    <item>
      <title>TIL: Getting the current capacity of a named pipe in Linux</title>
      <link>https://achanda.dev/posts/til-named-pipe/</link>
      <pubDate>Fri, 17 May 2024 21:43:58 -0500</pubDate>
      <guid>https://achanda.dev/posts/til-named-pipe/</guid>
      <description>&lt;p&gt;A named pipe (also known as FIFO) is a IPC channel, it allows one process to write and another to read. Like all things&#xA;in Linux, it is implemented as a file like entity using either &lt;a href=&#34;https://man7.org/linux/man-pages/man3/mkfifo.3.html&#34;&gt;&lt;code&gt;mkfifo&lt;/code&gt;&lt;/a&gt; or &lt;a href=&#34;https://man7.org/linux/man-pages/man2/pipe.2.html&#34;&gt;&lt;code&gt;pipe&lt;/code&gt;&lt;/a&gt; system calls.&lt;/p&gt;&#xA;&lt;p&gt;Pipes on Linux have a limited size, typically set to 65536 bytes on modern systems. I recently came across a situation where I needed to know the current remaining capacity of a given pipe without consuming data from it, while one process is writing to it and another is reading from it. I found that the &lt;code&gt;ioctl&lt;/code&gt; system call takes in a flag &lt;code&gt;FIONREAD&lt;/code&gt; that makes it read the count of unread bytes in a given pipe, identified by the file descriptor of either ends. The count is placed in an int buffer that &lt;code&gt;ioctl&lt;/code&gt; takes in.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Measuring energy usage of a function</title>
      <link>https://achanda.dev/posts/measuring-energy-usage-c/</link>
      <pubDate>Tue, 07 May 2024 16:43:04 -0500</pubDate>
      <guid>https://achanda.dev/posts/measuring-energy-usage-c/</guid>
      <description>&lt;p&gt;I recently needed a way to measure how much energy a piece of code consumes. This is easy to do in Linux using the perf utility assuming the underlying kernel has been built with appropriate flags. This looks something like this&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-sh&#34; data-lang=&#34;sh&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ sudo perf stat -a -e power/energy-pkg/ ls -la /usr&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;total &lt;span style=&#34;color:#3af&#34;&gt;116&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;drwxr-xr-x  &lt;span style=&#34;color:#3af&#34;&gt;14&lt;/span&gt; root root  &lt;span style=&#34;color:#3af&#34;&gt;4096&lt;/span&gt; Mar &lt;span style=&#34;color:#3af&#34;&gt;28&lt;/span&gt; 02:08 .&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;drwxr-xr-x  &lt;span style=&#34;color:#3af&#34;&gt;19&lt;/span&gt; root root  &lt;span style=&#34;color:#3af&#34;&gt;4096&lt;/span&gt; Apr &lt;span style=&#34;color:#3af&#34;&gt;16&lt;/span&gt; 16:02 ..&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;drwxr-xr-x   &lt;span style=&#34;color:#3af&#34;&gt;2&lt;/span&gt; root root &lt;span style=&#34;color:#3af&#34;&gt;36864&lt;/span&gt; May  &lt;span style=&#34;color:#3af&#34;&gt;7&lt;/span&gt; 15:05 bin&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;drwxr-xr-x   &lt;span style=&#34;color:#3af&#34;&gt;2&lt;/span&gt; root root  &lt;span style=&#34;color:#3af&#34;&gt;4096&lt;/span&gt; Apr &lt;span style=&#34;color:#3af&#34;&gt;18&lt;/span&gt;  &lt;span style=&#34;color:#3af&#34;&gt;2022&lt;/span&gt; games&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;drwxr-xr-x  &lt;span style=&#34;color:#3af&#34;&gt;37&lt;/span&gt; root root &lt;span style=&#34;color:#3af&#34;&gt;16384&lt;/span&gt; May  &lt;span style=&#34;color:#3af&#34;&gt;7&lt;/span&gt; 15:51 include&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;drwxr-xr-x  &lt;span style=&#34;color:#3af&#34;&gt;88&lt;/span&gt; root root  &lt;span style=&#34;color:#3af&#34;&gt;4096&lt;/span&gt; May  &lt;span style=&#34;color:#3af&#34;&gt;7&lt;/span&gt; 15:05 lib&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;drwxr-xr-x   &lt;span style=&#34;color:#3af&#34;&gt;2&lt;/span&gt; root root  &lt;span style=&#34;color:#3af&#34;&gt;4096&lt;/span&gt; Mar &lt;span style=&#34;color:#3af&#34;&gt;28&lt;/span&gt; 02:08 lib32&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;drwxr-xr-x   &lt;span style=&#34;color:#3af&#34;&gt;2&lt;/span&gt; root root  &lt;span style=&#34;color:#3af&#34;&gt;4096&lt;/span&gt; Apr &lt;span style=&#34;color:#3af&#34;&gt;19&lt;/span&gt; 06:14 lib64&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;drwxr-xr-x   &lt;span style=&#34;color:#3af&#34;&gt;9&lt;/span&gt; root root  &lt;span style=&#34;color:#3af&#34;&gt;4096&lt;/span&gt; Apr &lt;span style=&#34;color:#3af&#34;&gt;16&lt;/span&gt; 18:02 libexec&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;drwxr-xr-x   &lt;span style=&#34;color:#3af&#34;&gt;2&lt;/span&gt; root root  &lt;span style=&#34;color:#3af&#34;&gt;4096&lt;/span&gt; Mar &lt;span style=&#34;color:#3af&#34;&gt;28&lt;/span&gt; 02:08 libx32&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;drwxr-xr-x  &lt;span style=&#34;color:#3af&#34;&gt;10&lt;/span&gt; root root  &lt;span style=&#34;color:#3af&#34;&gt;4096&lt;/span&gt; Mar &lt;span style=&#34;color:#3af&#34;&gt;28&lt;/span&gt; 02:08 &lt;span style=&#34;color:#000&#34;&gt;local&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;drwxr-xr-x   &lt;span style=&#34;color:#3af&#34;&gt;2&lt;/span&gt; root root &lt;span style=&#34;color:#3af&#34;&gt;20480&lt;/span&gt; May  &lt;span style=&#34;color:#3af&#34;&gt;7&lt;/span&gt; 15:20 sbin&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;drwxr-xr-x &lt;span style=&#34;color:#3af&#34;&gt;119&lt;/span&gt; root root  &lt;span style=&#34;color:#3af&#34;&gt;4096&lt;/span&gt; May  &lt;span style=&#34;color:#3af&#34;&gt;7&lt;/span&gt; 15:05 share&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;drwxr-xr-x   &lt;span style=&#34;color:#3af&#34;&gt;7&lt;/span&gt; root root  &lt;span style=&#34;color:#3af&#34;&gt;4096&lt;/span&gt; Apr &lt;span style=&#34;color:#3af&#34;&gt;20&lt;/span&gt; 06:56 src&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt; Performance counter stats &lt;span style=&#34;color:#00f&#34;&gt;for&lt;/span&gt; &lt;span style=&#34;color:#5a2&#34;&gt;&amp;#39;system wide&amp;#39;&lt;/span&gt;:&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              0.03 Joules power/energy-pkg/&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       0.000777146 seconds &lt;span style=&#34;color:#000&#34;&gt;time&lt;/span&gt; elapsed&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This tells me that the &lt;code&gt;ls&lt;/code&gt; command consumed 0.03 Joules of energy and ran for 0.00078 seconds. I needed to get similar data for a C function that might be surrounded by code that I do not need to measure. Something like this:&lt;/p&gt;</description>
    </item>
  </channel>
</rss>