<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rviswanathan on Medium]]></title>
        <description><![CDATA[Stories by Rviswanathan on Medium]]></description>
        <link>https://medium.com/@rviswanathan041101?source=rss-1433ca1aea07------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*LwtlJ5lAEgsvgZ7x</url>
            <title>Stories by Rviswanathan on Medium</title>
            <link>https://medium.com/@rviswanathan041101?source=rss-1433ca1aea07------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 07:45:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rviswanathan041101/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[postgresql.conf & pg_hba.conf]]></title>
            <link>https://medium.com/@rviswanathan041101/postgresql-conf-pg-hba-conf-36e383b127a8?source=rss-1433ca1aea07------2</link>
            <guid isPermaLink="false">https://medium.com/p/36e383b127a8</guid>
            <category><![CDATA[postgresql]]></category>
            <dc:creator><![CDATA[Rviswanathan]]></dc:creator>
            <pubDate>Thu, 08 Oct 2026 16:46:55 GMT</pubDate>
            <atom:updated>2026-10-08T16:46:55.120Z</atom:updated>
            <content:encoded><![CDATA[<p>Think of PostgreSQL like a <strong>building</strong>:</p><ul><li>postgresql.conf → <strong>How the building operates</strong></li><li>pg_hba.conf → <strong>Who is allowed to enter the building</strong></li></ul><h3>1. postgresql.conf</h3><p>This file controls the <strong>behavior and configuration of the PostgreSQL server</strong>.</p><p>For example:</p><pre>port = 5432<br>max_connections = 100<br>shared_buffers = 128MB<br>work_mem = 4MB<br>logging_collector = on</pre><pre>listen_addresses<br>port<br>max_connections<br>shared_buffers<br>work_mem<br>maintenance_work_mem<br>effective_cache_size<br>wal_buffers<br>checkpoint_timeout<br>max_wal_size<br>logging_collector<br>log_statement<br>log_min_duration_statement</pre><p>listen_addresseswhich network addresses PostgreSQL listens on.</p><p>shared_buffers If frequently accessed data is already in shared buffers, PostgreSQL can avoid reading it from disk again.</p><p>work_mem This is memory available for <strong>individual query operations</strong> such as Sorting, Hash joins, Hash aggregation, Some other query operations.</p><p>maintenance_work_mem This is memory used for <strong>maintenance operations</strong>.</p><p>For example:</p><pre>CREATE INDEX idx_employee_name<br>ON employee(name);</pre><p>PostgreSQL may use maintenance_work_mem while creating the index.</p><p>effective_cache_size A hint to the query planner about available cache</p><p>wal_buffersBefore PostgreSQL permanently writes certain changes to data files, it records them in WAL.</p><p>checkpoint_timeoutTime-based interval for triggering checkpoints</p><p>max_wal_sizeThis controls approximately how much WAL PostgreSQL can generate between checkpoints before PostgreSQL is likely to trigger a checkpoint.</p><p>logging_collector(on/off)This controls whether PostgreSQL collects server log output into log files.</p><p>log_statementThis controls <strong>which SQL statements PostgreSQL writes to the log</strong>.</p><p>Common values:</p><pre>none - Don&#39;t log SQL statements.<br>ddl - Log statements that modify database structure ( create , alter , drop)<br>mod - Log statements that modify data or structure (insert, update,..)<br>all - Log basically every SQL statement.</pre><h3>2. pg_hba.conf</h3><p>pg_hba.conf means:</p><p><strong>PostgreSQL Host-Based Authentication configuration</strong></p><p>This file controls <strong>who can connect to PostgreSQL, from where, to which database, and how they authenticate</strong>.</p><p>For example:</p><pre>host    mydb    myuser    192.168.1.0/24    scram-sha-256</pre><p>This basically says:</p><blockquote><em>Allow </em><em>myuser to connect to the </em><em>mydb database from the </em><em>192.168.1.x network using password authentication with SCRAM.</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=36e383b127a8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Master the Core Architecture of postgres.]]></title>
            <link>https://medium.com/@rviswanathan041101/master-the-core-architecture-of-postgres-91d9f64b3ec9?source=rss-1433ca1aea07------2</link>
            <guid isPermaLink="false">https://medium.com/p/91d9f64b3ec9</guid>
            <category><![CDATA[postgresql]]></category>
            <dc:creator><![CDATA[Rviswanathan]]></dc:creator>
            <pubDate>Tue, 06 Oct 2026 14:27:55 GMT</pubDate>
            <atom:updated>2026-10-06T14:27:55.745Z</atom:updated>
            <content:encoded><![CDATA[<p>Every clients(application) that connects gets its own separate backend process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/539/1*1hQAYiT-XPtIi9-bLT7EyQ.png" /></figure><p>When you start PostgreSQL, the OS launches a single parent process historically called the <strong>Postmaster</strong> (listed as postgres in process managers).</p><p><strong>Listens for Connections:</strong> It binds to TCP port 5432 (or a local Unix domain socket) and waits for connection handshakes</p><p>Its job is not to execute every SQL query.</p><p>Think of it as the <strong>parent/coordinator process</strong>.</p><p>It primarily:</p><ul><li>starts PostgreSQL</li><li>listens for incoming connections</li><li>creates backend processes</li><li>starts/manages background processes</li><li>handles server-wide coordination</li><li>performs certain server-management responsibilities</li></ul><h3><strong>Checkpointer</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wqfq1kj4Wm20jq7KMi9q3A.png" /></figure><p>Eventually those changes need to be persisted appropriately to disk.</p><p>The <strong>checkpointer</strong> coordinates checkpoint processing, helping ensure that dirty buffers are written to the data files and establishing recovery points.</p><p>The checkpointer writes dirty buffers to disk, and once the write is complete, those buffers are marked as clean.</p><p>Flushes dirty 8KB pages from shared_buffers to physical tables on disk.</p><h3>WAL Writer</h3><p><strong>WAL Writer</strong> is a background process in PostgreSQL that writes database changes to the <strong>WAL (Write-Ahead Log) files</strong> on disk.</p><p>In simple terms:</p><pre>Client<br>  ↓<br>UPDATE / INSERT / DELETE<br>  ↓<br>PostgreSQL<br>  ↓<br>Data changes in Shared Buffers<br>  ↓<br>WAL records are created<br>  ↓<br>WAL Writer<br>  ↓<br>WAL files are written to disk</pre><h3>Simple Example</h3><p>Suppose we have this table:</p><pre>Employee<br>---------<br>id | name<br>1  | Arun</pre><p>Now you run:</p><pre>UPDATE Employee<br>SET name = &#39;Kumar&#39;<br>WHERE id = 1;</pre><p>PostgreSQL does not necessarily update the table data on disk immediately.</p><p>First, the change happens in memory:</p><pre>Shared Buffer<br>-------------<br>1 | Kumar</pre><p>At the same time, PostgreSQL creates a <strong>WAL record</strong> that describes the change.</p><p>For example:</p><pre>WAL<br>---<br>Employee id=1<br>old = Arun<br>new = Kumar</pre><p>The <strong>WAL Writer</strong> writes this WAL information to the WAL files on disk.</p><h3>Why do we need WAL?</h3><p>Suppose this happens:</p><pre>UPDATE<br>   ↓<br>Change is in Shared Buffer<br>   ↓<br>Server suddenly crashes</pre><p>The data that was only in the Shared Buffer could be lost.</p><p>But if the WAL information has already been written to disk:</p><pre>WAL<br> ↓<br>PostgreSQL restarts<br> ↓<br>PostgreSQL reads the WAL<br> ↓<br>Missing changes can be recovered</pre><p>This is one of the main purposes of WAL: <strong>crash recovery</strong>.</p><h3>Important Point</h3><p><strong>WAL Writer does not write the actual table data to disk.</strong></p><p>Its main responsibility is:</p><pre>WAL records → WAL files → Disk</pre><p>The actual <strong>dirty data pages</strong> are written to disk mainly by the <strong>Background Writer and Checkpointer</strong>.</p><p>So, remember it like this:</p><pre>WAL Writer<br>    ↓<br>WAL → Disk</pre><pre>Background Writer / Checkpointer<br>    ↓<br>Dirty Data Pages → Disk</pre><p>This is the easiest way to understand the difference between the WAL Writer and the processes that write actual table data.</p><p>MVCC<br><a href="https://medium.com/@rviswanathan041101/mvcc-multiversion-concurrency-control-22105f2fe3c1">https://medium.com/@rviswanathan041101/mvcc-multiversion-concurrency-control-22105f2fe3c1</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/846/1*V7x49couVk87DkNcsLjSdg.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=91d9f64b3ec9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding PostgreSQL Shared Buffer and Dirty Buffers]]></title>
            <link>https://medium.com/@rviswanathan041101/understanding-postgresql-shared-buffer-and-dirty-buffers-3a3252ef47ce?source=rss-1433ca1aea07------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a3252ef47ce</guid>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[shared-buffers]]></category>
            <dc:creator><![CDATA[Rviswanathan]]></dc:creator>
            <pubDate>Wed, 23 Sep 2026 05:57:36 GMT</pubDate>
            <atom:updated>2026-09-23T05:57:36.708Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is a Shared Buffer?</h3><p>In PostgreSQL, the shared buffer is a dedicated area in the computer’s RAM. Its primary purpose is to temporarily store data retrieved from the database disk. Think of it as a high-speed cache for your database.</p><h3>Why Do We Need It?</h3><p>Accessing data directly from a computer’s physical disk is a relatively slow process. Frequently reading data from the disk can severely bottleneck a database’s performance.</p><p>Let’s imagine a table called ‘Employee’ stored on the disk:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/845/1*Ri2iL1eygkOW65cZFhbE7A.png" /></figure><p>If we read this data every time, it would be slow. PostgreSQL uses a “Shared Buffer” to speed things up:</p><p>PostgreSQL &lt;--&gt; Shared Buffer (RAM) &lt;--&gt; Database Disk</p><p><strong>A Simple Analogy:</strong></p><p>Instead of getting up and taking a book from a distant bookshelf every time you want to read a specific part, you keep the book on your desk for as long as you are frequently reading from it.</p><h3>The Role of 8KB Pages</h3><p>In PostgreSQL, all data is organized into “pages” of a fixed size, which is typically 8KB. When the database needs to read or write data, it does so one page at a time. The Shared Buffer’s size is also divided into these 8KB page blocks, and it can only store data in these page units.</p><h3>How Queries Use the Shared Buffer</h3><p>Let’s look at how the shared buffer is used during a typical query:</p><p>SELECT * FROM Employee WHERE id = 1;</p><p>Before going to the disk, PostgreSQL first checks if the required 8KB page is already present in the shared buffer. There are two main scenarios:</p><p><strong>Case 1: Buffer Hit (Already in RAM)</strong></p><p>This is the ideal scenario.</p><blockquote>Query -&gt; Shared Buffer -&gt; Found! -&gt; Return Data</blockquote><p><strong>Case 2: Buffer Miss (Not in RAM)</strong></p><p>This requires fetching the data from the disk.</p><blockquote>Query -&gt; Shared Buffer -&gt; Not Found -&gt; Disk -&gt; Read 8KB Page -&gt; Put Page into Shared Buffer -&gt; Return Data</blockquote><h3>Key Characteristics of the Shared Buffer</h3><p>It’s important to remember these two key properties:</p><ol><li><strong>8KB Page Storage:</strong> As mentioned, it exclusively stores data in 8KB pages.</li><li><strong>Single, Global Memory Pool:</strong> The shared buffer is not dedicated to a single user. It’s a single, global memory pool that is shared across <strong>all client connections</strong> and background processes. All clients (Client 1, Client 2, etc.) use this shared memory pool.</li></ol><h3>What is a Dirty Shared Buffer?</h3><p>A “dirty buffer” is a state where data in the RAM buffer is different from the data on the disk. This happens when data is updated.</p><p>Imagine we execute this update query:</p><p>UPDATE Employee SET name = &#39;Viswa&#39; WHERE id = 1;</p><ol><li>The data is on the disk (where the name for id 1 is ‘Arun’).</li><li>PostgreSQL will bring the 8KB page containing this data into the Shared Buffer (RAM).</li><li>The UPDATE is performed in the RAM.</li></ol><p>Now, we have a mismatch:</p><ul><li><strong>Disk:</strong> name = Arun</li><li><strong>Shared Buffer:</strong> name = Viswa</li></ul><p>This specific buffer is now a <strong>Dirty Buffer</strong>.</p><p>A <strong>Dirty Shared Buffer</strong> is defined as a page where data has been changed in the Shared Buffer but has not yet been pushed to the disk.</p><h3>Final Summary Point</h3><ul><li><strong>Shared Buffer:</strong> Database pages will be temporarily kept in RAM to improve read performance.</li><li><strong>Dirty Buffer:</strong> A modified page in RAM that has not yet been pushed to the disk.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a3252ef47ce" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Multi-threaded process vs Process-Based]]></title>
            <link>https://medium.com/@rviswanathan041101/multi-threaded-process-vs-process-based-ea242caf0ecc?source=rss-1433ca1aea07------2</link>
            <guid isPermaLink="false">https://medium.com/p/ea242caf0ecc</guid>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[sql-server]]></category>
            <dc:creator><![CDATA[Rviswanathan]]></dc:creator>
            <pubDate>Tue, 18 Aug 2026 12:18:21 GMT</pubDate>
            <atom:updated>2026-08-18T12:18:21.391Z</atom:updated>
            <content:encoded><![CDATA[<p>Imagine a database as a company handling requests from customers. The difference between <strong>Multi-threaded</strong> and <strong>Process-based</strong> comes down to how that company organizes its workers.</p><h3>1. Multi-threaded Architecture (MySQL / SQL Server)</h3><p><strong>The Analogy:</strong> <strong>One Large Open-Plan Office</strong></p><ul><li><strong>How it works:</strong> The database runs as <strong>one big main process</strong>. When 100 customers connect, the system assigns 100 workers (<em>threads</em>) inside that same room.</li><li><strong>The Advantage:</strong> Because everyone is in the same room, they share the same tools, whiteboards, and air conditioning (shared memory). It starts up very fast and uses less computer memory.</li><li><strong>The Risk:</strong> If one worker makes a catastrophic mistake (a bad bug or memory crash), <strong>the entire office crashes</strong>, affecting all 100 customers at once.</li></ul><h3>2. Process-Based Architecture (PostgreSQL)</h3><p><strong>The Analogy:</strong> <strong>A Business Center with Separate Private Offices</strong></p><ul><li><strong>How it works:</strong> PostgreSQL has a main manager. Every time a new customer connects, PostgreSQL creates a <strong>completely separate, dedicated private office</strong> (<em>a standalone process</em>) for that customer.</li><li><strong>The Advantage (Safety &amp; Isolation):</strong> If Customer #5’s room has a major crash, the walls protect everyone else. Customer #5 loses their connection, but the other 99 customers — and the main database itself — keep running without missing a beat.</li><li><strong>The Trade-off:</strong> Setting up a brand-new private room for every single connection requires a bit more memory and overhead than putting everyone in one room.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/1*JVMODxkkBhqKQiHyvp0HIg.png" /></figure><h3>1. Multi-Threaded Architecture (MySQL / SQL Server)</h3><h4>Technical Mechanism</h4><p>In MySQL (specifically the InnoDB storage engine), the database runs as a <strong>single Operating System Process</strong> (mysqld). When clients connect:</p><ul><li><strong>Threads:</strong> For every client connection, MySQL spawns or assigns a <strong>thread</strong> within that single OS process.</li><li><strong>Shared Memory Address Space:</strong> All threads share the exact same virtual memory space (Heap memory). They directly access global memory structures like the <strong>InnoDB Buffer Pool</strong>, <strong>Query Cache</strong>, and <strong>Lock Manager</strong>.</li><li><strong>Synchronization:</strong> Inter-thread communication is extremely fast because data exists in shared memory. However, to prevent data corruption when multiple threads write to the same memory address, the database relies heavily on synchronization primitives like <strong>Mutexes</strong> (Mutual Exclusion) and <strong>Spinlocks</strong>.</li></ul><pre>+-------------------------------------------------------------+<br>| OS Process (mysqld)                                         |<br>|                                                             |<br>|  [Shared Memory: InnoDB Buffer Pool, Log Buffers, Caches]   |<br>|                                                             |<br>|   +--------------+    +--------------+    +--------------+  |<br>|   |  Thread 1    |    |  Thread 2    |    |  Thread N    |  |<br>|   | (Conn 1)     |    | (Conn 2)     |    | (Conn N)     |  |<br>|   +--------------+    +--------------+    +--------------+  |<br>+-------------------------------------------------------------</pre><h4>Performance &amp; Reliability Characteristics</h4><ul><li><strong>Low Memory Overhead:</strong> Creating a thread requires very small stack allocation (~256 KB–1 MB per thread).</li><li><strong>Fast Context Switching:</strong> Switching CPU execution between threads within the same process carries low CPU cost because virtual memory mappings do not change.</li><li><strong>Single Point of Failure:</strong> If a thread executes a bad pointer operation, corrupts memory, or hits a segmentation fault, the OS terminates the <strong>entire process</strong>, causing a complete database outage for all connected clients.</li></ul><h3>2. Process-Based Architecture (PostgreSQL)</h3><h4>Technical Mechanism</h4><p>PostgreSQL uses a <strong>Process-per-Connection model</strong> coordinated by a central master process called <strong>postmaster</strong> (or postgres).</p><ul><li><strong>Independent OS Processes:</strong> When a client connects, postmaster calls the fork() system call to create a child process (a backend process).</li><li><strong>Isolated Address Spaces:</strong> Every backend process runs in its own isolated OS virtual address space. Process A cannot directly access or corrupt the private memory of Process B.</li><li><strong>Shared Memory Segment (IPC):</strong> Because processes have private memory, PostgreSQL allocates an explicit <strong>Shared Memory Segment</strong> (using mmap or System V shared memory) at startup. All backend processes map this shared segment into their virtual memory to access the <strong>Shared Buffers</strong> (data cache), <strong>WAL Buffers</strong>, and <strong>Lock Table</strong>.</li></ul><pre>+-------------------------------------------------------------------+<br>| OS Memory                                                         |<br>|                                                                   |<br>|  +-------------------------------------------------------------+  |<br>|  | Shared Memory Segment (Shared Buffers, WAL, Lock Table)    |  |<br>|  +-------------------------------------------------------------+  |<br>|         ^                               ^                         |<br>|         | (IPC)                         | (IPC)                   |<br>|  +--------------+                +--------------+                 |<br>|  | OS Process 1 |                | OS Process 2 |                 |<br>|  | (Backend 1)  |                | (Backend 2)  |                 |<br>|  +--------------+                +--------------+                 |<br>+-------------------------------------------------------------------+</pre><h4>Performance &amp; Reliability Characteristics</h4><ul><li><strong>Fault Isolation (Robustness):</strong> If a backend process encounters a fatal error or memory corruption, the OS terminates <strong>only that single process</strong>. The master process cleans up its locks, and other clients continue running uninterrupted.</li><li><strong>Higher Resource Overhead:</strong> Spawning an OS process via fork() requires cloning page tables and OS data structures, making connection creation heavier (~2 MB–10 MB per backend process).</li><li><strong>Connection Pooling Requirement:</strong> Because OS process switching and creation carry higher CPU costs, production PostgreSQL deployment relies on connection poolers (like <strong>PgBouncer</strong>) to reuse a fixed pool of OS processes.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*YSNBNCv_EWWvXHnhO1_vTA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ea242caf0ecc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[TOAST (The Oversized-Attribute Storage Technique)]]></title>
            <link>https://medium.com/@rviswanathan041101/toast-the-oversized-attribute-storage-technique-ef9bfec20b15?source=rss-1433ca1aea07------2</link>
            <guid isPermaLink="false">https://medium.com/p/ef9bfec20b15</guid>
            <category><![CDATA[toast]]></category>
            <category><![CDATA[postgresql-database]]></category>
            <dc:creator><![CDATA[Rviswanathan]]></dc:creator>
            <pubDate>Wed, 12 Aug 2026 08:56:14 GMT</pubDate>
            <atom:updated>2026-08-12T08:56:14.262Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>A Hard Rule </strong>— In postgres is that a single row (tuple) cannot cross a page boundary. It must fit entirely within a 8KB page.</p><p>Think — of storing 5MB paragraph, or a image blob. (Here TOAST comes to rescue).</p><p><strong>The 2 KB Trigger: When does TOAST Kick in?</strong> postgres want to pack as many rows into 8KB page as possible to keep disk I/O Efficiently. If a single row gets too fat, query performance drops.</p><p>To prevent this, postgres set a threshold: 2KB (Specifically, TOAST_TUPLE_THRESHOLD, which is 1/4th of a page)</p><p>If you insert or update a row and its total physical size exceeds roughly 2KB, postgres will automatically step in and compress or move the large column values out of main table.</p><h3>How TOAST Works</h3><p><strong>Step 1: Compression:</strong> Built in compression Algorithm (like pglz or lz4). If compressing bring the total row size back under 2KB, postgres keep the compressed data inside the main table page. This is called inline Storage.</p><p><strong>Step 2: Out-of-Line Moving (The Hidden table)</strong> If the data is too big after compression, postgres physically rips the large value out of the main row and moves it into a completely seperate, hidden side-table called TOAST table.</p><p>When it does this, it leaves behind a tiny 24-byte pointer (called a TOAST pointer) inside your table row.</p><p>pointer contains:</p><ul><li>OID (ID) for the hidden TOAST table</li><li>specific ID of the chunks belonging to that data.</li></ul><h3>How the Hidden TOAST Table Stores Data:</h3><p>Because TOAST table also bound by the 8KB page limit. it cannot store a 5MB file in one piece either. Instead, it breaks your massive value into small, uniform pieces called chunks (by default, 2KB per chunk)</p><p>If you store a 10 KB PDF file:</p><ol><li>postgres compresses it (lets say it shrinks to 6KB)</li><li>It breaks 6KB data into three 2KB chunks</li><li>It inserts those 3 chunks as 3 seperate rows into the hidden TOAST table.</li></ol><p>when we display It will reverse to glues them back together.</p><p>Toast pointer -&gt; Hidden TOAST table -&gt; fetch chunk and glues together -&gt; and Decompress the data.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ef9bfec20b15" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[8KB page layout(Postgres)]]></title>
            <link>https://medium.com/@rviswanathan041101/8kb-page-layout-postgres-e1e897d89be9?source=rss-1433ca1aea07------2</link>
            <guid isPermaLink="false">https://medium.com/p/e1e897d89be9</guid>
            <category><![CDATA[8kb]]></category>
            <category><![CDATA[postgres]]></category>
            <dc:creator><![CDATA[Rviswanathan]]></dc:creator>
            <pubDate>Wed, 12 Aug 2026 08:55:26 GMT</pubDate>
            <atom:updated>2026-10-18T13:31:22.753Z</atom:updated>
            <content:encoded><![CDATA[<p>PostgreSQL stores table and index data in fixed-size blocks called <strong>pages</strong>, which are <strong>8KB</strong> (8,192 bytes) by default.</p><h3>The Bird’s-Eye View: The Dual-Ended Page</h3><p>A PostgreSQL page grows from both ends toward the middle.</p><ul><li>Management overhead (headers and pointers) is written from the top down.</li><li>Actual data (tuples) is written from the bottom up.</li></ul><p>The empty space left in the middle is called Free Space. When this space runs out, the page is full.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/762/1*6Wok0KMJsYzAB574PyU7Ew.png" /></figure><h3>Inside the 8KB Page: Component Breakdown</h3><h3>1. Page Header (PageHeaderData)</h3><p>Occupies the first 24 bytes of the page. It contains critical metadata about the page itself.</p><ul><li>pd_lsn: The Log Sequence Number tracking the last Write-Ahead log (WAL) entry that modified this page (crucial for crash recovery).</li><li>pd_lower: A byte-offset pointer marking the start of unallocated free space.</li><li>pd_upper: The end of unallocated free space.</li><li>pd_special: Pointer space for specific index methods (partially crossed out in notes).</li></ul><h3>2. Line Pointers (ItemIdData)</h3><p>Directly following the header is an array of Line Pointers (also called ItemIDs).</p><ul><li>Each line pointer is exactly 4 bytes long.</li></ul><h3>3. Free Space</h3><h3>4. Tuples</h3><h3>Notes:</h3><p>If a table contains 100MB of data, your single table file on disk will contain exactly:</p><p>100MB / 8KB = 12,800 pages.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e1e897d89be9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Vacuum in Postgres]]></title>
            <link>https://medium.com/@rviswanathan041101/vacuum-in-postgres-d6710a59f37f?source=rss-1433ca1aea07------2</link>
            <guid isPermaLink="false">https://medium.com/p/d6710a59f37f</guid>
            <category><![CDATA[postgresql-database]]></category>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[vacuum]]></category>
            <dc:creator><![CDATA[Rviswanathan]]></dc:creator>
            <pubDate>Sun, 09 Aug 2026 06:07:59 GMT</pubDate>
            <atom:updated>2026-08-09T06:07:59.062Z</atom:updated>
            <content:encoded><![CDATA[<p>In Postgres, Vacuuming is a vital maintenance process that keeps your database running fast and it prevents from running out of disk spaces.</p><h3>Why Vacuum in Postgres? (MVCC)</h3><ul><li>DELETE: The row isn’t removed from the disk. Postgres just marks it as “invisible” to future transaction.</li><li>UPDATE: Postgres leaves the original row alone, marks it as invisible, and inserts a brand-new row with the updated row.</li></ul><p>These dead, invisible rows are called tuples.</p><h3>The Solution: What Vacuuming Does</h3><p>Vacuuming is the cleanup crew. It targets those dead tuples and perform a few crucial task.</p><blockquote>(If there are lots of dead tuples in your database. This accumulation is known as bloat)</blockquote><figure><img alt="Types of vacuum" src="https://cdn-images-1.medium.com/max/913/1*nYS-ADR5YiJLf2BwqJkcwg.png" /></figure><p>Fortunately, we do not have to run VACUUM command manually every hour. Postgres has a built-in background worker called Autovacuum.</p><h3>Analyzing Database Health with pgstattuple</h3><p>The pgstattuple extension in PostgreSQL is a built-in tool that provides granular, row-level statistics to help you analyze database health. It is primarily used to identify and measure table bloat, fragmentation, dead tuple, and free space to determine when maintenance like VACUUM or ReIndex is needed.</p><p>To check the disk size of the table:</p><pre>SELECT pg_size_pretty(pg_total_relation_size(&#39;bulk_data&#39;)) AS total_size;</pre><h3>Summary of the Lifecycle:</h3><ol><li>Before Vacuum: The space is occupied by invisible “dead tuples” that blocks new data.</li><li>After standard Vacuum: The dead tuple are cleared. The file size on disk is unchanged, but the empty space is open for fresh insert and update.</li><li>After Vacuum Full: Postgres Create a completely brand-new file, copies only the live rows into it packed tightly together, update the indexes, deletes the old bloated file and drops the disk usage instantly.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d6710a59f37f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[MVCC (Multiversion Concurrency Control)]]></title>
            <link>https://medium.com/@rviswanathan041101/mvcc-multiversion-concurrency-control-22105f2fe3c1?source=rss-1433ca1aea07------2</link>
            <guid isPermaLink="false">https://medium.com/p/22105f2fe3c1</guid>
            <category><![CDATA[mvcc]]></category>
            <category><![CDATA[postgresql-database]]></category>
            <category><![CDATA[postgres]]></category>
            <dc:creator><![CDATA[Rviswanathan]]></dc:creator>
            <pubDate>Sun, 09 Aug 2026 05:56:16 GMT</pubDate>
            <atom:updated>2026-08-09T05:56:16.793Z</atom:updated>
            <content:encoded><![CDATA[<p>When diving deep into database engineering and trying to master internal architecture, one of the most critical concepts to understand is how systems like PostgreSQL handle simultaneous operations. This is where MVCC, or Multiversion Concurrency Control, comes into play.</p><h3>What is MVCC?</h3><p>At its core, MVCC means the database keeps multiple versions of a row instead of locking it aggressively.</p><p>So, instead of the database saying:</p><blockquote>“Wait, someone is using this row.</blockquote><p>It elegantly handles the request by saying:</p><blockquote>“Fine, I’ll show you an older version of the row.</blockquote><p>That is the fundamental idea behind it.</p><h3>Why MVCC Exists</h3><p>To understand its value, let’s look at how a database behaves with and without it.</p><h3>Without MVCC:</h3><ul><li>Reads block writes.</li><li>Writes block reads.</li><li>The system slows down significantly under heavy concurrency.</li></ul><h3>With MVCC:</h3><ul><li>Reads don’t block writes.</li><li>Writes don’t block reads.</li><li>Delivers much better performance for real-world systems.</li></ul><h3>A Practical Example</h3><p>Let’s look at a simple row representing an account: id = 1, balance = 100.</p><p>The Scenario:</p><ul><li>Transaction A updates the balance to 200.</li><li>Transaction B reads the row at the exact same time.</li></ul><p>Without MVCC: Transaction B will either have to wait or get blocked entirely until Transaction A finishes.</p><p>With MVCC:</p><ul><li>Transaction B sees: balance = 100</li><li>Transaction A sees: balance = 200</li></ul><p>Both proceed without blocking each other!</p><blockquote><em>Important Note: You don’t see changes from future transactions. You only see what was committed before your snapshot</em></blockquote><h3>How it Works Internally</h3><p>How does PostgreSQL actually manage these versions? Each row has hidden metadata attached to it, including:</p><ul><li>created_tnx_id</li><li>deleted_tnx_id (or Expiry)</li></ul><p>When you update a row, the database does not overwrite it. Instead, it creates an entirely new version.</p><p>So internally, our earlier example becomes:</p><ul><li>Version 1 -&gt; balance = 100 (old)</li><li>Version 2 -&gt; balance = 200 (new)</li></ul><h3>The Key Takeaway</h3><p>Every transaction gets a snapshot of the database. This is what allows PostgreSQL to maintain consistency and high concurrency simultaneously.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=22105f2fe3c1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[HOT (Heap-Only Tuples)]]></title>
            <link>https://medium.com/@rviswanathan041101/hot-heap-only-tuples-49fd3875c9a9?source=rss-1433ca1aea07------2</link>
            <guid isPermaLink="false">https://medium.com/p/49fd3875c9a9</guid>
            <category><![CDATA[heap-only-tuple]]></category>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Rviswanathan]]></dc:creator>
            <pubDate>Wed, 29 Jul 2026 17:28:41 GMT</pubDate>
            <atom:updated>2026-08-02T09:31:35.325Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>HOT (Heap-Only Tuples)</strong> is easily one of the most brilliant performance optimizations inside PostgreSQL. It directly solves a massive side-effect of MVCC called <strong>Index Bloat</strong>.</p><p>To appreciate why HOT is so amazing, we first have to look at the painful problem it was invented to solve.</p><h3>The Problem: The “Index Amplification” Nightmare</h3><p>As you learned earlier, when you UPDATE a row in Postgres, it doesn&#39;t modify the data in place. Instead, it creates a brand-new tuple (the new version) and leaves the old one behind as a dead tuple.</p><p>Before HOT was introduced (in Postgres 8.3), every single UPDATE triggered a chain reaction:</p><ol><li>Postgres would insert the new row version — often on a completely different 8KB page.</li><li>Because the physical location (TID) of the row changed, <strong>every single index on that table had to be updated</strong> to add a new pointer to the new row version.</li></ol><p>Imagine a users table with 5 indexes (e.g., on email, created_at, status, etc.). If you updated just a single unindexed text field like bio, Postgres was forced to write <strong>5 new entries into your indexes</strong>, even though the indexed columns never changed! This caused massive write amplification, slowed down updates, and bloated the indexes with dead pointers that required heavy vacuuming.</p><h3>The Solution: How Heap-Only Tuples (HOT) Works</h3><p>The core philosophy of HOT is simple: <strong>If an update doesn’t touch any indexed columns, and there is enough free space on the exact same 8KB page, don’t touch the indexes at all.</strong></p><p>Here is the step-by-step breakdown of a HOT update inside an 8KB page:</p><h3>1. The Local Update</h3><p>When you update an unindexed column, Postgres checks the current 8KB page. If there is enough room in the middle <strong>Free Space</strong> area, it writes the new row version (Tuple V2) on the <strong>same page</strong> as the old version (Tuple V1).</p><h3>2. The Internal Page Link</h3><p>Instead of going out to the indexes to register Tuple V2, Postgres creates a secret internal link right inside the page header.</p><ul><li>It modifies the old Tuple V1 to point directly to Tuple V2.</li><li>It sets a special bit flag on Tuple V2 marking it as a <strong>Heap-Only Tuple</strong> (meaning no index points to it directly).</li></ul><h3>3. Index Redirection</h3><p>When a search query uses an index to look for this row, the index still points to the original <strong>Line Pointer 1</strong>.</p><ul><li>The query execution engine reads Line Pointer 1, which points to Tuple V1.</li><li>It sees the HOT flag on Tuple V1 saying: <em>“Hey, I’ve been updated! Follow the chain to Tuple V2.”</em></li><li>Postgres instantly jumps to Tuple V2 on the same page and reads the fresh data.</li></ul><p>The index remains completely untouched, completely unaware that an update even happened.</p><h3>The Hidden Bonus: Micro-Vacuuming (HOT Pruning)</h3><p>HOT updates offer a massive secondary benefit: <strong>Pruning</strong>.</p><p>Because the entire version chain (Tuple V1 $\rightarrow$ Tuple V2) lives inside a single 8KB page, Postgres doesn’t need to wait for the heavy global background VACUUM daemon to clean it up.</p><p>The next time a regular SELECT or INSERT query accesses that specific page, Postgres notices the dead Tuple V1 in the chain. If Tuple V1 is old enough that no active transactions need to see it anymore, Postgres will perform a lightning-fast <strong>&quot;mini-vacuum&quot;</strong> right then and there:</p><ol><li>It physically deletes the data bits of Tuple V1.</li><li>It changes <strong>Line Pointer 1</strong> to act as a direct redirector, pointing it straight to Tuple V2.</li><li>The space previously held by Tuple V1 is instantly reclaimed and added back to the page’s Free Space.</li></ol><p>This is called <strong>HOT Pruning</strong>, and it happens instantly in RAM, completely bypassing the need for disk-heavy index vacuuming.</p><h3>What Breaks HOT? (The Limitations)</h3><p>HOT is a passive optimization — it happens automatically, but only if two strict conditions are met:</p><ol><li><strong>No Indexed Columns are Modified:</strong> If your UPDATE statement changes even one column that is part of an index, HOT is completely disabled. Postgres must write new index entries.</li><li><strong>There Must Be Room on the Same Page:</strong> If the 8KB page is packed tight with no free space left in the middle, Postgres is forced to write the new tuple version to a different page, breaking the local HOT chain.</li></ol><h3>How to Optimize Your Tables for HOT</h3><p>If you have a table experiencing brutal write traffic (like a sessions table updating a last_active_at timestamp every few seconds), you can intentionally leave breathing room on your pages using the <strong>fillfactor</strong> setting.</p><p>By default, Postgres packs pages to 100% capacity. You can lower a table’s fillfactor to 80% or 90%:</p><p>SQL</p><pre>-- Tell Postgres to leave 20% of every page empty during initial inserts<br>ALTER TABLE bulk_data SET (fillfactor = 80);<br>-- Note: This only applies to new pages. You must run a VACUUM FULL <br>-- to rewrite the existing pages with the new fillfactor layout.<br>VACUUM FULL bulk_data;</pre><p>By giving up 10–20% of your disk space upfront, you ensure there is almost always room on the page for HOT updates, resulting in massive long-term performance wins.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=49fd3875c9a9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>