<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Anusha Renangi</title>
    <description>The latest articles on DEV Community by Anusha Renangi (@anusha_renangi_05a4e41d35).</description>
    <link>https://dev.to/anusha_renangi_05a4e41d35</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2788588%2F6f537281-3e1a-4dda-9f41-6e3d844fe871.jpeg</url>
      <title>DEV Community: Anusha Renangi</title>
      <link>https://dev.to/anusha_renangi_05a4e41d35</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anusha_renangi_05a4e41d35"/>
    <language>en</language>
    <item>
      <title>Networking Essentials for Software Engineers – Part 5: How Email Actually Works</title>
      <dc:creator>Anusha Renangi</dc:creator>
      <pubDate>Sun, 15 Nov 2026 17:43:02 +0000</pubDate>
      <link>https://dev.to/anusha_renangi_05a4e41d35/networking-essentials-for-software-engineers-part-5-how-email-actually-works-1g8o</link>
      <guid>https://dev.to/anusha_renangi_05a4e41d35/networking-essentials-for-software-engineers-part-5-how-email-actually-works-1g8o</guid>
      <description>&lt;p&gt;We use emails almost every day, whether it's for work or communicating with someone.&lt;/p&gt;

&lt;p&gt;Suppose you're using Gmail and want to send an email to your friend. You enter their email address, write your message, and click &lt;strong&gt;Send&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That's all we do from our side.&lt;/p&gt;

&lt;p&gt;But have you ever wondered how Gmail knows where to send that email, especially if your friend uses Yahoo or another email provider?&lt;/p&gt;

&lt;p&gt;Just like HTTP, email also uses networking protocols to communicate between servers.&lt;/p&gt;

&lt;p&gt;Let's see how an email travels from our application to the receiver's inbox and understand the different protocols involved.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Does an Email Actually Travel?
&lt;/h2&gt;

&lt;p&gt;When we send an email, it doesn't go directly from our computer to the receiver's computer. It goes through &lt;strong&gt;email servers&lt;/strong&gt;, which handle sending, receiving, and storing emails.&lt;/p&gt;

&lt;p&gt;But who manages these email servers?&lt;/p&gt;

&lt;p&gt;Some companies maintain their own mail servers, while others use email providers like &lt;strong&gt;Google, Microsoft, or Zoho&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example, suppose a company owns the domain &lt;code&gt;example.com&lt;/code&gt;. It can set up its own mail servers to handle emails for addresses like &lt;code&gt;employee@example.com&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Or it can use Google Workspace. Employees can still use &lt;code&gt;employee@example.com&lt;/code&gt;, but Google takes care of sending and receiving their emails.&lt;/p&gt;

&lt;p&gt;So, having our own email domain doesn't mean we need to run our own mail servers.&lt;/p&gt;

&lt;p&gt;Now, let's see how an email travels when we send it from &lt;strong&gt;Gmail to Yahoo Mail&lt;/strong&gt;. We'll also look at what changes when both people use Gmail or when we use a third-party email application like Thunderbird.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sending an Email from Gmail to Yahoo Mail
&lt;/h3&gt;

&lt;p&gt;Suppose you're using Gmail and want to send an email to your friend who uses Yahoo Mail.&lt;/p&gt;

&lt;p&gt;You open Gmail in your browser, write your message, enter your friend's email address, and click &lt;strong&gt;Send&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's see what happens next.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Your email reaches Gmail's servers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Since we're using Gmail in a browser, our email first reaches Google's servers through &lt;strong&gt;HTTPS&lt;/strong&gt;, the same protocol we learned about in our previous article.&lt;/p&gt;

&lt;p&gt;Google receives the email and checks where it needs to be sent.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Gmail finds Yahoo's mail server&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose your friend's email address is &lt;code&gt;friend@yahoo.com&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Gmail looks at the domain &lt;code&gt;yahoo.com&lt;/code&gt; and needs to find which server receives emails for that domain.&lt;/p&gt;

&lt;p&gt;To find it, Gmail uses &lt;strong&gt;DNS&lt;/strong&gt; and looks for a special record called an &lt;strong&gt;MX (Mail Exchange) record&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;An MX record tells us which mail servers handle incoming emails for a particular domain.&lt;/p&gt;

&lt;p&gt;Once Gmail finds the mail server's name, it can find its IP address and connect to it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Gmail sends the email to Yahoo&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now Gmail needs to transfer the email to Yahoo's mail server.&lt;/p&gt;

&lt;p&gt;For this, it uses &lt;strong&gt;SMTP (Simple Mail Transfer Protocol)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;SMTP is the protocol mail servers commonly use to send emails to each other.&lt;/p&gt;

&lt;p&gt;Gmail connects to Yahoo's mail server, sends the message, and receives a response telling it whether the email was accepted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Yahoo receives and stores the email&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once Yahoo accepts the email, it processes the message and delivers it to your friend's mailbox.&lt;/p&gt;

&lt;p&gt;Now, how does your friend read it?&lt;/p&gt;

&lt;p&gt;If they open Yahoo Mail in their browser, the browser uses &lt;strong&gt;HTTPS&lt;/strong&gt; to communicate with Yahoo's servers and display the email.&lt;/p&gt;

&lt;p&gt;But if they're using a third-party email application like Thunderbird, it can use &lt;strong&gt;IMAP or POP3&lt;/strong&gt; to access emails from Yahoo's servers.&lt;/p&gt;

&lt;p&gt;We'll understand how IMAP and POP3 work shortly.&lt;/p&gt;

&lt;h3&gt;
  
  
  What If Both of You Use Gmail?
&lt;/h3&gt;

&lt;p&gt;Now suppose you and your friend both use Gmail.&lt;/p&gt;

&lt;p&gt;When you click Send, your browser sends the email to Google's servers using &lt;strong&gt;HTTPS&lt;/strong&gt;, just like in our previous example.&lt;/p&gt;

&lt;p&gt;But this time, Google doesn't need to find another email provider's mail server.&lt;/p&gt;

&lt;p&gt;Since your friend also uses Gmail, Google can handle the delivery within its own mail system and store the email in your friend's mailbox.&lt;/p&gt;

&lt;p&gt;Google may use SMTP or other methods to communicate between its internal servers, but it doesn't need to establish an SMTP connection with another email provider.&lt;/p&gt;

&lt;h3&gt;
  
  
  What If We Use a Third-Party Email Application?
&lt;/h3&gt;

&lt;p&gt;So far, we've seen how emails work when we use Gmail directly in our browser.&lt;/p&gt;

&lt;p&gt;But Gmail isn't the only application we can use to access our emails.&lt;/p&gt;

&lt;p&gt;Suppose you have a Gmail account, but instead of opening &lt;code&gt;gmail.com&lt;/code&gt;, you want to send and read your emails using an application like &lt;strong&gt;Thunderbird&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You can connect your Gmail account to Thunderbird and use it to manage your emails.&lt;/p&gt;

&lt;p&gt;Now, when you send an email from Thunderbird, it can use &lt;strong&gt;SMTP&lt;/strong&gt; to send the message to Gmail's outgoing mail server. Gmail then takes care of delivering it to the receiver.&lt;/p&gt;

&lt;p&gt;Similarly, when you want to read emails, Thunderbird can use &lt;strong&gt;IMAP or POP3&lt;/strong&gt; to access the messages stored on Gmail's servers.&lt;/p&gt;

&lt;p&gt;So, the main difference is how the application communicates with Gmail:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Gmail in your browser:&lt;/strong&gt; Uses HTTPS to communicate with Google's web service.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Thunderbird/Third Party Applications:&lt;/strong&gt; Can use SMTP to send emails and IMAP or POP3 to receive them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's understand how &lt;strong&gt;SMTP, IMAP, and POP3&lt;/strong&gt; work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Do We Use Different Protocols to Send and Receive Emails?
&lt;/h2&gt;

&lt;p&gt;You might be wondering why we need SMTP to send emails and IMAP or POP3 to receive them. Why can't we use the same protocol for both?&lt;/p&gt;

&lt;p&gt;Let's take a simple example.&lt;/p&gt;

&lt;p&gt;Suppose we have a long wooden stick and a rope.&lt;/p&gt;

&lt;p&gt;We can use the wooden stick to &lt;strong&gt;push&lt;/strong&gt; an object away from us, but we can't easily use it to pull the object back.&lt;/p&gt;

&lt;p&gt;Similarly, we can use a rope to &lt;strong&gt;pull&lt;/strong&gt; an object towards us, but we can't use it to push the object away.&lt;/p&gt;

&lt;p&gt;Email protocols work in a similar way.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SMTP works like pushing:&lt;/strong&gt; When we send an email, SMTP transfers it from our email application to the outgoing mail server, or from one mail server to another.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IMAP and POP3 work like pulling:&lt;/strong&gt; When we want to read emails, these protocols allow our email application to access messages from the mail server.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's see how each of these protocols works.&lt;/p&gt;

&lt;h2&gt;
  
  
  POP3 vs IMAP
&lt;/h2&gt;

&lt;p&gt;We've seen that POP3 and IMAP are used to access emails from a mail server. But they work differently.&lt;/p&gt;

&lt;p&gt;Let's understand POP3 first.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. POP3 (Post Office Protocol Version 3)
&lt;/h3&gt;

&lt;p&gt;Suppose you have a Gmail account and use it on two devices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Laptop:&lt;/strong&gt; Thunderbird, connected to Gmail using POP3.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Phone:&lt;/strong&gt; Gmail opened in your browser.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now suppose your friend sends you an email.&lt;/p&gt;

&lt;p&gt;The email reaches Gmail's server and appears in your inbox. When you open Thunderbird on your laptop, it downloads the email from Gmail.&lt;/p&gt;

&lt;p&gt;But what happens to Gmail's copy after downloading?&lt;/p&gt;

&lt;p&gt;If you open &lt;strong&gt;Gmail → Settings → See all settings → Forwarding and POP/IMAP&lt;/strong&gt;, you'll find several options.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Enable POP for all mail&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose you already have 1,000 emails in Gmail.&lt;/p&gt;

&lt;p&gt;If you enable this option, Thunderbird can download your existing emails as well as new emails that arrive later.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Enable POP for mail that arrives from now on&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose you already have 1,000 emails, but you only want Thunderbird to download new emails.&lt;/p&gt;

&lt;p&gt;You can select this option. Gmail makes emails arriving from that point onward available for POP download, instead of including your older emails.&lt;/p&gt;

&lt;p&gt;Gmail also lets you decide what happens to its copy when Thunderbird downloads an email.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Keep Gmail's copy in the Inbox&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose Thunderbird downloads an email and you read it on your laptop.&lt;/p&gt;

&lt;p&gt;When you open Gmail on your phone, the email will still be in your inbox. It might even appear as unread because POP3 doesn't synchronize the read status.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Mark Gmail's copy as read&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now suppose Thunderbird downloads an email, but you haven't opened it yet.&lt;/p&gt;

&lt;p&gt;If you've selected this option, Gmail marks its copy as read as soon as it's downloaded.&lt;/p&gt;

&lt;p&gt;So, when you open Gmail on your phone, the email already appears as read, even though you haven't actually read it in Thunderbird.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Archive Gmail's copy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose Thunderbird downloads an email.&lt;/p&gt;

&lt;p&gt;Gmail removes its copy from the inbox but keeps it under &lt;strong&gt;All Mail&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When you open Gmail on your phone, you won't find that email in your inbox, but you can still find it under All Mail.&lt;/p&gt;

&lt;p&gt;The downloaded copy remains available in Thunderbird.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Delete Gmail's copy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose Thunderbird downloads an email.&lt;/p&gt;

&lt;p&gt;Gmail moves its copy to &lt;strong&gt;Trash&lt;/strong&gt;, while the downloaded email can still remain on your laptop.&lt;/p&gt;

&lt;p&gt;When you open Gmail on your phone, the email will no longer appear in your inbox. You can find Gmail's copy in Trash until it's permanently deleted.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Happens When We Use POP3 on Multiple Devices?
&lt;/h3&gt;

&lt;p&gt;POP3 doesn't synchronize changes between devices.&lt;/p&gt;

&lt;p&gt;For example, if you read an email on your laptop, it might still appear as unread on your desktop. Similarly, if you move an email to another folder or delete it on one device, those changes won't automatically appear on the other device.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. IMAP (Internet Message Access Protocol)
&lt;/h3&gt;

&lt;p&gt;Now suppose we connect Thunderbird to Gmail using IMAP instead of POP3.&lt;/p&gt;

&lt;p&gt;This time, Thunderbird can keep our emails and changes synchronized with Gmail's server.&lt;/p&gt;

&lt;p&gt;Let's see what happens.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Reading emails&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose you read an email in Thunderbird on your laptop.&lt;/p&gt;

&lt;p&gt;When you open Gmail on your phone, the same email appears as read.&lt;/p&gt;

&lt;p&gt;If you mark it as unread again, that change also appears on your phone.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Deleting emails&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose you delete an email in Thunderbird.&lt;/p&gt;

&lt;p&gt;With IMAP, the deletion is communicated to Gmail's server. Depending on your settings, Gmail may move it to Trash, archive it, or delete it permanently.&lt;/p&gt;

&lt;p&gt;When you open Gmail on your phone, you'll see the updated result.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Moving emails between folders&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose you move an email into a folder called &lt;em&gt;Work&lt;/em&gt; in Thunderbird.&lt;/p&gt;

&lt;p&gt;If that folder is synchronized through IMAP, Gmail updates the corresponding folder or label.&lt;/p&gt;

&lt;p&gt;You can then find the email in the same location when you open Gmail on your phone.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Saving drafts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose you start writing an email in Thunderbird but don't finish it.&lt;/p&gt;

&lt;p&gt;If Thunderbird saves it to Gmail's Drafts folder, you can open Gmail on your phone and continue writing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Using multiple devices&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose you use Gmail on your laptop, desktop, and phone.&lt;/p&gt;

&lt;p&gt;With IMAP, all three devices can access the same mailbox. When you read, move, or delete an email on one device, those changes can appear on the others.&lt;/p&gt;

&lt;h3&gt;
  
  
  Which One Should We Use?
&lt;/h3&gt;

&lt;p&gt;If we mainly want to download emails and keep them on one device, POP3 can be useful.&lt;/p&gt;

&lt;p&gt;But if we use the same email account on multiple devices and want everything to stay updated, IMAP is generally the better choice.&lt;/p&gt;

&lt;p&gt;The main difference is that &lt;strong&gt;POP3 downloads emails, while IMAP allows us to access emails and keep changes synchronized with the server.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding a Domain's Email Servers
&lt;/h2&gt;

&lt;p&gt;We can also check which mail servers handle emails for a particular domain using a simple command.&lt;/p&gt;

&lt;p&gt;If you're using Linux, open your terminal and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dig MX gmail.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can replace &lt;code&gt;gmail.com&lt;/code&gt; with any domain, such as &lt;code&gt;amazon.com&lt;/code&gt; or your company's domain, to see which mail servers are responsible for receiving its emails.&lt;/p&gt;

</description>
      <category>softwaredevelopment</category>
      <category>networking</category>
      <category>smtp</category>
    </item>
    <item>
      <title>Networking Essentials for Software Engineers – Part 4: Everything You Need to Know About HTTP</title>
      <dc:creator>Anusha Renangi</dc:creator>
      <pubDate>Sat, 14 Nov 2026 18:25:20 +0000</pubDate>
      <link>https://dev.to/anusha_renangi_05a4e41d35/networking-essentials-for-software-engineers-part-4-everything-you-need-to-know-about-http-fnk</link>
      <guid>https://dev.to/anusha_renangi_05a4e41d35/networking-essentials-for-software-engineers-part-4-everything-you-need-to-know-about-http-fnk</guid>
      <description>&lt;p&gt;When we build an application, we often write code like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"https://api.example.com/users"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We know this makes a request to our backend server. But how does the server understand what our application is requesting?&lt;/p&gt;

&lt;p&gt;Both our application and the server need to follow the same set of rules to communicate. Otherwise, they wouldn't understand each other's requests and responses.&lt;/p&gt;

&lt;p&gt;These rules are defined by &lt;strong&gt;HTTP (Hypertext Transfer Protocol)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Since we're using HTTP while building our application and communicating with the server, it comes under the &lt;strong&gt;Application Layer&lt;/strong&gt; of the networking model, which we learned about earlier.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTTP Request and Response
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwdakqcgsropbdhmy59k7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwdakqcgsropbdhmy59k7.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our earlier example, the browser sends an &lt;strong&gt;HTTP request&lt;/strong&gt; to our backend server, asking for the users' data.&lt;/p&gt;

&lt;p&gt;The server receives the request, processes it, and sends back an &lt;strong&gt;HTTP response&lt;/strong&gt; containing the result.&lt;/p&gt;

&lt;p&gt;This communication is called the &lt;strong&gt;Request-Response cycle&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So, there are two parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;HTTP Request:&lt;/strong&gt; The information our application sends to the server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HTTP Response:&lt;/strong&gt; The information the server sends back to our application.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, let's understand what an HTTP request contains.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTTP Request
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdveqc43dabvezpa5xu46.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdveqc43dabvezpa5xu46.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  1. HTTP Methods
&lt;/h3&gt;

&lt;p&gt;When we build an application , we often make API requests to fetch, create, update, or delete data.&lt;/p&gt;

&lt;p&gt;We use different &lt;strong&gt;HTTP methods&lt;/strong&gt; depending on what we want the server to do.&lt;/p&gt;

&lt;p&gt;Let's take an application that manages users as an example.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;GET:&lt;/strong&gt; Suppose we want to display all the users in our application. We use GET to fetch the list of users from the backend.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;POST:&lt;/strong&gt; Suppose someone fills out a registration form and clicks Submit. We use POST to send their details to the backend to create a new user.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;PUT:&lt;/strong&gt; Suppose we want to replace a user's existing profile details with new details. We use PUT to send the complete updated profile.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;PATCH:&lt;/strong&gt; Suppose we only want to change a user's city without updating the other details. We use PATCH to send just that change.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;DELETE:&lt;/strong&gt; Suppose a user wants to delete their account. We use DELETE to request that the backend delete it.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, in JavaScript, we can specify the HTTP method like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/api/users"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"POST"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are the HTTP methods we commonly use while building applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Request URL
&lt;/h3&gt;

&lt;p&gt;When we make an HTTP request, we need to tell the server which resource we want to access.&lt;/p&gt;

&lt;p&gt;For example, while building an application in JavaScript, we might write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"https://api.example.com/users/123"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"GET"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we're making a &lt;strong&gt;GET request&lt;/strong&gt; to an API that returns the details of the user with ID &lt;code&gt;123&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's look at the URL:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;https://&lt;/code&gt;&lt;/strong&gt; — The protocol used to communicate securely with the server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;api.example.com&lt;/code&gt;&lt;/strong&gt; — The domain name of the server we're contacting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;/users/123&lt;/code&gt;&lt;/strong&gt; — The API path that identifies the user whose ID is &lt;code&gt;123&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sometimes, we also pass additional information through &lt;strong&gt;query parameters&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example, suppose we want to fetch only active users from Hyderabad:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"https://api.example.com/users?city=Hyderabad&amp;amp;status=active"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"GET"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;city&lt;/code&gt; and &lt;code&gt;status&lt;/code&gt; are query parameters. The backend can use them to filter the results.&lt;/p&gt;

&lt;p&gt;So, the URL tells our application where to send the request and which resource we're asking for.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Request Headers
&lt;/h3&gt;

&lt;p&gt;Sometimes, along with our request, we need to send some additional information to the backend. For example, what type of data we're sending or information the backend needs to check whether we're logged in.&lt;/p&gt;

&lt;p&gt;We can send these details through &lt;strong&gt;HTTP headers&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"https://api.example.com/users"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"POST"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"Content-Type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"application/json"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"Authorization"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Bearer your_access_token"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Anusha"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Hyderabad"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you look at the &lt;code&gt;headers&lt;/code&gt; section, we've added two things.&lt;/p&gt;

&lt;p&gt;First, &lt;code&gt;Content-Type&lt;/code&gt; is set to &lt;code&gt;application/json&lt;/code&gt; because we're sending the user's details as JSON. This helps the backend understand how to read that data. If we're uploading a file, we might use a different format, such as &lt;code&gt;multipart/form-data&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, what about &lt;code&gt;Authorization&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Think about what happens when we log in to an application. After logging in, we might open our profile, view our orders, or access other pages. We don't enter our username and password every time, right?&lt;/p&gt;

&lt;p&gt;That's because, after a successful login, the backend can give us an &lt;strong&gt;access token&lt;/strong&gt;. Our application sends that token in the &lt;code&gt;Authorization&lt;/code&gt; header with subsequent requests, allowing the backend to verify that we're already authenticated.&lt;/p&gt;

&lt;p&gt;This is just one way of handling login. Some applications use cookies and sessions instead, where the browser automatically sends a cookie containing a session ID through the &lt;code&gt;Cookie&lt;/code&gt; header.&lt;/p&gt;

&lt;p&gt;Apart from these, there are many other HTTP headers. Some are added by us when needed, while browsers handle others automatically.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Request Body
&lt;/h3&gt;

&lt;p&gt;Suppose we're building an application where users can register.&lt;/p&gt;

&lt;p&gt;When someone fills out the registration form and clicks Submit, we need to send their details, such as their name, email, and city, to the backend.&lt;/p&gt;

&lt;p&gt;We send this information through the &lt;strong&gt;HTTP request body&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"https://api.example.com/users"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"POST"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"Content-Type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"application/json"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Anusha"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"anusha@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Hyderabad"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we're making a POST request to create a new user.&lt;/p&gt;

&lt;p&gt;If you look at the &lt;code&gt;body&lt;/code&gt; section, we're sending the user's details as JSON. The backend receives this data and can use it to create the user.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A request body isn't always required.&lt;/strong&gt; For example, in our earlier GET request to fetch user &lt;code&gt;123&lt;/code&gt;, we didn't send a body. We simply included the user ID in the URL, which was enough for the backend to know whose details we wanted.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTTP Response
&lt;/h2&gt;

&lt;p&gt;Now that we've seen what an HTTP request contains, let's look at what happens when the backend sends a response.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5u2nqvxc3l5m39j2ah78.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5u2nqvxc3l5m39j2ah78.png" alt=" " width="800" height="267"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Suppose we make a GET request to fetch the details of user &lt;code&gt;123&lt;/code&gt;. The backend receives the request, looks for that user, and sends back a response.&lt;/p&gt;

&lt;p&gt;An &lt;strong&gt;HTTP response&lt;/strong&gt; mainly contains three things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Status Code:&lt;/strong&gt; Tells us whether the request was successful or something went wrong.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Response Headers:&lt;/strong&gt; Provide additional information about the response, such as the format of the data being returned.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Response Body:&lt;/strong&gt; Contains the actual data returned by the backend.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  1. HTTP Status Codes
&lt;/h3&gt;

&lt;p&gt;If the backend finds user &lt;code&gt;123&lt;/code&gt;, it might return &lt;code&gt;200 OK&lt;/code&gt;. But if the user doesn't exist, it might return &lt;code&gt;404 Not Found&lt;/code&gt; instead.&lt;/p&gt;

&lt;p&gt;These numbers are called &lt;strong&gt;HTTP status codes&lt;/strong&gt;, and they help us understand what happened to our request.&lt;/p&gt;

&lt;p&gt;Here are some common status codes we come across while building applications:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Status Code&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;200 OK&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;The request was successful.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;201 Created&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Something new, like a user, was created successfully.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;400 Bad Request&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;There's something wrong with the request we sent.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;401 Unauthorized&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;We need to log in or provide valid authentication.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;403 Forbidden&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;We're not allowed to access what we requested.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;404 Not Found&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;The resource we're looking for doesn't exist or couldn't be found.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;500 Internal Server Error&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Something went wrong on the backend while processing our request.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;You might have noticed that some status codes start with &lt;code&gt;2&lt;/code&gt;, others with &lt;code&gt;4&lt;/code&gt; or &lt;code&gt;5&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;HTTP status codes are divided into five groups:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;1xx:&lt;/strong&gt; Informational responses&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;2xx:&lt;/strong&gt; Successful requests&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;3xx:&lt;/strong&gt; Redirections&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;4xx:&lt;/strong&gt; Client errors&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;5xx:&lt;/strong&gt; Server errors&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, when we see a &lt;code&gt;2xx&lt;/code&gt; status code, our request was successful. If we see &lt;code&gt;4xx&lt;/code&gt;, there's usually an issue with the request or access permissions. And &lt;code&gt;5xx&lt;/code&gt; means something went wrong on the server's side.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Response Headers
&lt;/h3&gt;

&lt;p&gt;Just like HTTP requests, responses also contain headers.&lt;/p&gt;

&lt;p&gt;Along with the result, the backend might send headers like these:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="nc"&gt;Content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;Content-Type&lt;/code&gt; is &lt;code&gt;application/json&lt;/code&gt; because the backend is returning the user's details in JSON format. If it were sending an HTML page, the content type would be &lt;code&gt;text/html&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Another header is &lt;code&gt;Content-Length&lt;/code&gt;, which tells us the size of the response body in bytes when the server includes it.&lt;/p&gt;

&lt;p&gt;There's also a header called &lt;code&gt;Set-Cookie&lt;/code&gt;. Suppose we log in to an application. The backend might send a session ID through this header, and the browser stores it as a cookie. When we make another request, the browser sends that cookie back, so the backend can recognize us without asking us to log in again.&lt;/p&gt;

&lt;p&gt;These are some common response headers. The backend can send other headers depending on what it's returning.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Response Body
&lt;/h3&gt;

&lt;p&gt;If the backend finds user &lt;code&gt;123&lt;/code&gt;, the &lt;strong&gt;response body&lt;/strong&gt; might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Anusha"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"email"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"anusha@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"city"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Hyderabad"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the response body contains the actual user details returned by the backend.&lt;/p&gt;

&lt;p&gt;The response body doesn't always contain JSON. Depending on what we're requesting, the backend might return HTML, plain text, images, or other types of data.&lt;/p&gt;

&lt;p&gt;Also, &lt;strong&gt;not every HTTP response has a body&lt;/strong&gt;. For example, suppose we send a DELETE request to remove user &lt;code&gt;123&lt;/code&gt;. Once the backend successfully deletes the user, it might return &lt;code&gt;204 No Content&lt;/code&gt;, which means the request was successful, but there's no data in the response body.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTTP is Stateless
&lt;/h2&gt;

&lt;p&gt;Suppose we log in to an application and then open our profile page.&lt;/p&gt;

&lt;p&gt;The login request was successful, but when we make another request to view our profile, HTTP doesn't automatically remember that we logged in earlier.&lt;/p&gt;

&lt;p&gt;That's because &lt;strong&gt;HTTP is a stateless protocol&lt;/strong&gt;. Each request is treated independently, and the server doesn't automatically remember what happened in previous requests.&lt;/p&gt;

&lt;p&gt;But when we use applications, we don't have to log in every time we open a new page, right?&lt;/p&gt;

&lt;p&gt;Applications handle this using things like &lt;strong&gt;cookies, sessions, or access tokens&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example, after logging in, the backend might create a session and send its ID to our browser as a cookie. When we make another request, the browser sends that cookie back, allowing the backend to identify us.&lt;/p&gt;

&lt;p&gt;So, even though HTTP itself is stateless, applications can still remember logged-in users by using these mechanisms.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTTP vs HTTPS
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fv4ktk826f8zd8qj30zmv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fv4ktk826f8zd8qj30zmv.png" alt=" " width="800" height="356"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You might have noticed that some website URLs start with &lt;code&gt;http://&lt;/code&gt;, while most websites today use &lt;code&gt;https://&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With &lt;strong&gt;HTTP&lt;/strong&gt;, the information we send travels without encryption, meaning someone who intercepts it might be able to read it.&lt;/p&gt;

&lt;p&gt;Remember our school days? Sometimes, when we wanted to talk to our friends without others understanding, we'd make up our own secret language. We'd add extra letters between words or change them in a way only we understood.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HTTPS works on a similar idea, but uses much stronger protection.&lt;/strong&gt; It encrypts the information exchanged between our browser and the server, making it unreadable to anyone intercepting it.&lt;/p&gt;

&lt;p&gt;HTTPS also helps protect the information from being changed along the way and verifies that we're communicating with the intended website.&lt;/p&gt;

&lt;p&gt;We'll learn how this protection actually works later in our networking series.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTTP Versions
&lt;/h2&gt;

&lt;p&gt;HTTP has been around for many years, and it has gone through several versions to make communication between browsers and servers faster and more efficient.&lt;/p&gt;

&lt;p&gt;Let's look at how it evolved.&lt;/p&gt;

&lt;h3&gt;
  
  
  HTTP/1.0
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8vfqvcfzx684f2lmjryx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8vfqvcfzx684f2lmjryx.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;HTTP/1.0 uses &lt;strong&gt;TCP&lt;/strong&gt; to communicate between the browser and the server.&lt;/p&gt;

&lt;p&gt;In this version, a new TCP connection was typically created for each request.&lt;/p&gt;

&lt;p&gt;Imagine opening a website that contains an HTML page, 10 images, and a few other files. The browser would usually need to open a separate connection for each request.&lt;/p&gt;

&lt;p&gt;Creating connections repeatedly added extra time.&lt;/p&gt;

&lt;h3&gt;
  
  
  HTTP/1.1
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb7bvu2wepsfhwbu95g5o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb7bvu2wepsfhwbu95g5o.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;HTTP/1.1 also uses &lt;strong&gt;TCP&lt;/strong&gt;, but it improved how connections were handled.&lt;/p&gt;

&lt;p&gt;Instead of opening a new TCP connection for every request, the browser could &lt;strong&gt;reuse the same connection for multiple requests&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This reduced the time spent creating new connections.&lt;/p&gt;

&lt;h3&gt;
  
  
  HTTP/2
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fneux4kf3txq9dvm4ytb4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fneux4kf3txq9dvm4ytb4.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;HTTP/2 also uses &lt;strong&gt;TCP&lt;/strong&gt;, but it introduced an improvement called &lt;strong&gt;multiplexing&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Suppose we open a website that contains HTML, CSS, JavaScript, and images.&lt;/p&gt;

&lt;p&gt;With HTTP/1.1, if the browser wants to download multiple files at the same time, it generally needs to open multiple TCP connections. Otherwise, it has to wait for one response to finish before receiving the next on the same connection.&lt;/p&gt;

&lt;p&gt;HTTP/2 improved this by allowing &lt;strong&gt;multiple requests and responses to travel at the same time over a single TCP connection&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is called &lt;strong&gt;multiplexing&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But there was still a problem.&lt;/p&gt;

&lt;p&gt;Suppose we're downloading three images at the same time, and a network packet containing part of Image B gets lost.&lt;/p&gt;

&lt;p&gt;Since TCP delivers data in order, it has to recover the missing packet before passing along the data that arrived after it. This can also delay Images A and C, even if their packets have already arrived.&lt;/p&gt;

&lt;p&gt;This problem is called &lt;strong&gt;TCP Head-of-Line (HOL) Blocking&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So, even though HTTP/2 can handle multiple requests over one connection, a lost packet can still delay the other requests sharing that connection.&lt;/p&gt;

&lt;h3&gt;
  
  
  HTTP/3
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzxge7yp99osocwq4m60i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzxge7yp99osocwq4m60i.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;HTTP/3 was introduced to address this problem.&lt;/p&gt;

&lt;p&gt;Instead of TCP, HTTP/3 uses a newer transport protocol called &lt;strong&gt;QUIC&lt;/strong&gt;, which works over UDP.&lt;/p&gt;

&lt;p&gt;Just like HTTP/2, it allows multiple requests and responses to travel at the same time over a single connection.&lt;/p&gt;

&lt;p&gt;But let's take the same example of downloading three images.&lt;/p&gt;

&lt;p&gt;Suppose a network packet containing part of Image B gets lost.&lt;/p&gt;

&lt;p&gt;With HTTP/3, Images A and C can continue receiving their data without waiting for the missing packet from Image B to be recovered.&lt;/p&gt;

&lt;p&gt;This is possible because QUIC handles these streams independently, so a lost packet in one stream doesn't have to block the others.&lt;/p&gt;

&lt;p&gt;QUIC can also handle network changes, such as switching from Wi-Fi to mobile data, without always needing to start a new connection.&lt;/p&gt;

&lt;p&gt;The HTTP methods, status codes, headers, and request-response concepts we've learned remain largely the same across these versions. What changes is how HTTP sends and receives information over the network.&lt;/p&gt;

&lt;p&gt;We'll learn more about &lt;strong&gt;TCP, UDP, QUIC, and Head-of-Line Blocking&lt;/strong&gt; when we cover transport-layer protocols later in our networking series.&lt;/p&gt;

&lt;h2&gt;
  
  
  Viewing HTTP Requests and Responses in the Browser
&lt;/h2&gt;

&lt;p&gt;We can actually see the HTTP requests and responses we've discussed using our browser.&lt;/p&gt;

&lt;p&gt;Let's try it in Google Chrome.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open any website, right-click anywhere on the page, and select &lt;strong&gt;Inspect&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Go to the &lt;strong&gt;Network&lt;/strong&gt; tab and refresh the page.&lt;/li&gt;
&lt;li&gt;Click on any request to see its URL, method, status code, headers, and response.&lt;/li&gt;
&lt;li&gt;To check the HTTP version, right-click the &lt;strong&gt;Name&lt;/strong&gt; column heading and enable &lt;strong&gt;Protocol&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You'll see the HTTP version used for each request:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;http/1.1&lt;/code&gt;&lt;/strong&gt; — HTTP/1.1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;h2&lt;/code&gt;&lt;/strong&gt; — HTTP/2&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;h3&lt;/code&gt;&lt;/strong&gt; — HTTP/3&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You might even notice different HTTP versions being used on the same webpage, depending on which servers the browser is communicating with.&lt;/p&gt;

&lt;p&gt;If you're just starting to learn networking, I hope this article helped you understand HTTP a little better.&lt;/p&gt;

&lt;p&gt;Some of these concepts might already be familiar if you're building applications every day, but topics like HTTP versions and how they've evolved might be new to you.&lt;/p&gt;

&lt;p&gt;Thanks for reading! See you in the next part of the Networking Essentials series.&lt;/p&gt;

</description>
      <category>softwareengineering</category>
      <category>networking</category>
      <category>webdev</category>
      <category>http</category>
    </item>
    <item>
      <title>Networking Essentials for Software Engineers – Part 3: What Happens When You Type a Website Name?</title>
      <dc:creator>Anusha Renangi</dc:creator>
      <pubDate>Wed, 11 Nov 2026 18:10:06 +0000</pubDate>
      <link>https://dev.to/anusha_renangi_05a4e41d35/networking-essentials-for-software-engineers-part-3-what-happens-when-you-type-a-website-name-8pf</link>
      <guid>https://dev.to/anusha_renangi_05a4e41d35/networking-essentials-for-software-engineers-part-3-what-happens-when-you-type-a-website-name-8pf</guid>
      <description>&lt;p&gt;Imagine we open our browser and type &lt;code&gt;google.com&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We want to visit Google's website. But how does our browser know which server to contact?&lt;/p&gt;

&lt;p&gt;Computers use &lt;strong&gt;IP addresses&lt;/strong&gt; to communicate across networks. So, our browser needs to find an IP address associated with &lt;code&gt;google.com&lt;/code&gt; before it can connect to the website.&lt;/p&gt;

&lt;p&gt;We could use IP addresses directly, but imagine having to remember the IP addresses of Google, YouTube, Amazon, and every other website we visit.&lt;/p&gt;

&lt;p&gt;And those IP addresses might change over time.&lt;/p&gt;

&lt;p&gt;Remembering website names is much easier than remembering a bunch of numbers, right?&lt;/p&gt;

&lt;p&gt;That's why we have &lt;strong&gt;DNS (Domain Name System)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;DNS helps us find the IP addresses associated with domain names such as &lt;code&gt;google.com&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But how does DNS know which IP address belongs to a particular domain?&lt;/p&gt;

&lt;p&gt;To understand that, we first need to know how domain names are organized and where their IP address information is stored.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Domain Names
&lt;/h2&gt;

&lt;p&gt;Let's take &lt;code&gt;mail.google.com&lt;/code&gt; as an example.&lt;/p&gt;

&lt;p&gt;If we look closely, it has three parts separated by dots (&lt;code&gt;.&lt;/code&gt;).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;mail&lt;/code&gt; — Subdomain:&lt;/strong&gt; Google uses this to identify a particular service, such as Gmail.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;google&lt;/code&gt; — Second-Level Domain:&lt;/strong&gt; This is the main name registered by Google under &lt;code&gt;.com&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;.com&lt;/code&gt; — Top-Level Domain (TLD):&lt;/strong&gt; This is the last part of the domain name. Top-level domains help organize domain names into different categories or groups.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;.com&lt;/code&gt;&lt;/strong&gt; — Originally used for commercial businesses, but now used by all kinds of websites.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;.org&lt;/code&gt;&lt;/strong&gt; — Originally intended for organizations and commonly used by nonprofits.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;.in&lt;/code&gt;&lt;/strong&gt; — Represents India.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, let's see how DNS finds the IP address associated with a domain name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsjxkc8yfz65kkfhbogh4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsjxkc8yfz65kkfhbogh4.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Browser Requests the IP Address:&lt;/strong&gt; When we type &lt;code&gt;google.com&lt;/code&gt;, Chrome needs its IP address to connect to the website. So, it starts a DNS lookup, usually by asking the operating system to resolve the domain.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Checking the Cache:&lt;/strong&gt; Before contacting an external DNS server, the browser or operating system checks whether the IP address is already cached. If a valid answer is available, it can use that directly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Contacting the DNS Resolver:&lt;/strong&gt; If the IP address isn't available locally, our computer sends a request to a DNS resolver, often provided by our Internet Service Provider (ISP). If the resolver already has the answer cached, it returns it immediately.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Root DNS Server:&lt;/strong&gt; If the resolver doesn't have the answer, it contacts a Root DNS Server. The root server directs it to the DNS servers responsible for &lt;code&gt;.com&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Top-Level Domain (TLD) DNS Server:&lt;/strong&gt; The resolver contacts a &lt;code&gt;.com&lt;/code&gt; DNS server, which tells it which authoritative DNS servers are responsible for &lt;code&gt;google.com&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Authoritative DNS Server:&lt;/strong&gt; The resolver contacts the authoritative DNS server for &lt;code&gt;google.com&lt;/code&gt;. This server holds DNS records for the domain, including its IP address information, and returns the requested record.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Returning the IP Address:&lt;/strong&gt; The resolver returns the IP address to our computer, and Chrome can use it to connect to the website.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Who Manages DNS Servers?
&lt;/h2&gt;

&lt;p&gt;We saw how different DNS servers help us find a website's IP address.&lt;/p&gt;

&lt;p&gt;But who manages all these servers?&lt;/p&gt;

&lt;p&gt;Let's start with the Root DNS Servers.&lt;/p&gt;

&lt;p&gt;There are &lt;strong&gt;13 root DNS server identities&lt;/strong&gt;, named from &lt;code&gt;A&lt;/code&gt; to &lt;code&gt;M&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Does that mean there are only 13 physical servers in the world?&lt;/p&gt;

&lt;p&gt;No. Each one has multiple servers spread across different locations. These 13 root server identities are operated by &lt;strong&gt;12 different organizations&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You might have heard of &lt;strong&gt;ICANN (Internet Corporation for Assigned Names and Numbers)&lt;/strong&gt;. It helps coordinate the domain name system.&lt;/p&gt;

&lt;p&gt;Now, let's say we're building our own website and want to call it &lt;code&gt;agriboon.com&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can't just choose that name and start using it, right? We first need to register it.&lt;/p&gt;

&lt;p&gt;We can do that through companies like &lt;strong&gt;GoDaddy&lt;/strong&gt;, which are called domain registrars.&lt;/p&gt;

&lt;p&gt;Once we've registered &lt;code&gt;agriboon.com&lt;/code&gt;, we need to connect it to the server where our website is hosted.&lt;/p&gt;

&lt;p&gt;Suppose our server has the IP address &lt;code&gt;203.0.113.10&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In GoDaddy's DNS management section, we can add an &lt;strong&gt;A record&lt;/strong&gt; like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;agriboon.com → 203.0.113.10&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This information is made available through the authoritative DNS servers responsible for our domain.&lt;/p&gt;

&lt;p&gt;We can also use another DNS provider, such as Cloudflare or AWS Route 53, if we prefer.&lt;/p&gt;

&lt;p&gt;Now, when someone enters &lt;code&gt;agriboon.com&lt;/code&gt; in their browser, the DNS lookup process we discussed earlier can find the IP address we've configured.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's Try DNS Ourselves
&lt;/h2&gt;

&lt;p&gt;Want to find the IP address of a website yourself?&lt;/p&gt;

&lt;p&gt;If you're using Linux or macOS, open your terminal and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dig google.com +short
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command displays the IP addresses associated with &lt;code&gt;google.com&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Try it with other websites by replacing &lt;code&gt;google.com&lt;/code&gt; with any domain name.&lt;/p&gt;

&lt;h2&gt;
  
  
  Can Incognito Mode Hide Our DNS Requests?
&lt;/h2&gt;

&lt;p&gt;Suppose we open Chrome in Incognito Mode and visit &lt;code&gt;google.com&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We might think that since we're using Incognito Mode, no one can see which websites we're visiting.&lt;/p&gt;

&lt;p&gt;But Chrome still needs to find the IP address of &lt;code&gt;google.com&lt;/code&gt;, right?&lt;/p&gt;

&lt;p&gt;So, the DNS lookup happens as usual, and our Internet Service Provider (ISP) may still be able to see which websites we're visiting.&lt;/p&gt;

&lt;p&gt;Incognito Mode simply prevents Chrome from saving our browsing history after we close the incognito session. It doesn't hide our activity from the ISP.&lt;/p&gt;

&lt;p&gt;I hope you now have a better understanding of how DNS helps our browser find a website's IP address and connect to it when we enter a domain name.&lt;/p&gt;

</description>
      <category>networking</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Networking Essentials for Software Engineers – Part 2: How Data Travels Across Networks</title>
      <dc:creator>Anusha Renangi</dc:creator>
      <pubDate>Thu, 05 Nov 2026 04:11:48 +0000</pubDate>
      <link>https://dev.to/anusha_renangi_05a4e41d35/networking-essentials-for-software-engineers-part-2-how-data-travels-across-networks-26d1</link>
      <guid>https://dev.to/anusha_renangi_05a4e41d35/networking-essentials-for-software-engineers-part-2-how-data-travels-across-networks-26d1</guid>
      <description>&lt;p&gt;In the previous article, we discussed many fundamental concepts of networking.&lt;/p&gt;

&lt;p&gt;Now, imagine you're on a WhatsApp video call with your friend.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxbuf43oir6s735z0h4x3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxbuf43oir6s735z0h4x3.png" alt=" " width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You're in India, connected to your home Wi-Fi, while your friend is in the US, connected to their home Wi-Fi.&lt;/p&gt;

&lt;p&gt;As you speak, your voice and video travel from your phone, through your Wi-Fi router, ISP, and several other networks, before reaching your friend's phone.&lt;/p&gt;

&lt;p&gt;All this happens almost instantly!&lt;/p&gt;

&lt;p&gt;But what exactly happens to our data during this journey? How does our phone prepare it, how do routers forward it, and how does our friend's phone understand it?&lt;/p&gt;

&lt;p&gt;We'll follow the journey in three phases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Our phone → Our home router:&lt;/strong&gt; How our phone prepares and sends the data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Our router → Our friend's router:&lt;/strong&gt; How the data travels across different networks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Our friend's router → Our friend's phone:&lt;/strong&gt; How the data reaches our friend's phone and gets processed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Along the way, we'll understand how the &lt;strong&gt;OSI and TCP/IP models&lt;/strong&gt; help us organize these networking responsibilities.&lt;/p&gt;

&lt;h2&gt;
  
  
  Phase 1: From Our Phone to Our Home Router
&lt;/h2&gt;

&lt;p&gt;Let's start with our phone in India.&lt;/p&gt;

&lt;p&gt;Before our voice and video can travel to our friend in the US, our phone needs to prepare the data and send it to our home Wi-Fi router.&lt;/p&gt;

&lt;p&gt;We'll follow what happens inside our phone, step by step.&lt;/p&gt;

&lt;h2&gt;
  
  
  OSI Model
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;OSI (Open Systems Interconnection) model&lt;/strong&gt; was developed by the International Organization for Standardization (ISO).&lt;/p&gt;

&lt;p&gt;It divides network communication into &lt;strong&gt;7 layers&lt;/strong&gt;, each with its own responsibility.&lt;/p&gt;

&lt;p&gt;Here's how the seven layers are arranged:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Foa800o4e1vx3phlhnaem.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Foa800o4e1vx3phlhnaem.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Application Layer (Layer 7)
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fodapjwlxlwnx97g449lf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fodapjwlxlwnx97g449lf.png" alt=" " width="800" height="409"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, we've opened WhatsApp and started a video call with our friend.&lt;/p&gt;

&lt;p&gt;WhatsApp needs a way to send and receive information over the network, right?&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Application Layer&lt;/strong&gt; handles how applications exchange information using network protocols.&lt;/p&gt;

&lt;p&gt;For example, when we open a website:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;DNS&lt;/strong&gt;: When we enter a website name like google.com, DNS helps find its IP address so our browser knows which server to connect to.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HTTP or HTTPS&lt;/strong&gt; defines how requests are formed and how responses are received.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Similarly, WhatsApp uses communication protocols to initiate calls and exchange information with other devices.&lt;/p&gt;

&lt;p&gt;We'll learn more about HTTP, HTTPS, and DNS later.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But where does all this happen?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This mainly happens in the application software and the networking libraries it uses.&lt;/p&gt;

&lt;p&gt;For example, when we write code to make an API request, we might use JavaScript's &lt;code&gt;fetch()&lt;/code&gt; function. The browser then handles creating the HTTP request using its networking code.&lt;/p&gt;

&lt;p&gt;Similarly, WhatsApp's developers write code that uses networking libraries and protocols to communicate with other devices.&lt;/p&gt;

&lt;p&gt;So, the Application Layer isn't a separate physical component. It represents the networking responsibilities handled by applications and the software they use.&lt;/p&gt;

&lt;p&gt;Now that the application has prepared the information, let's see how the &lt;strong&gt;Presentation Layer&lt;/strong&gt; handles its representation.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Presentation Layer (Layer 6)
&lt;/h3&gt;

&lt;p&gt;Our voice and video contain a lot of data. Sending everything in its original form would require transferring a large amount of data, which could make our video call slow.&lt;/p&gt;

&lt;p&gt;So, we compress the data to reduce its size.&lt;/p&gt;

&lt;p&gt;We also don't want anyone else listening to our conversation, right? So, we encrypt the data to keep it private.&lt;/p&gt;

&lt;p&gt;And our friend's phone needs to understand the audio and video format so it can play them correctly.&lt;/p&gt;

&lt;p&gt;These are the three main responsibilities of the &lt;strong&gt;Presentation Layer&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Compression:&lt;/strong&gt; Reducing the size of data so less data needs to be transferred. For example, video codecs like H.264 compress video by reducing repeated information between frames.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Encoding:&lt;/strong&gt; Converting data into a specific format. For example, H.264 encodes video so the receiving phone can decode and display it. Similarly, when a website sends text, character encodings like UTF-8 help the browser interpret it correctly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Encryption:&lt;/strong&gt; Protecting data so others cannot easily understand it. We can see this in two examples:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;WhatsApp video call:&lt;/strong&gt; WhatsApp uses end-to-end encryption so only the call participants can access the contents of their conversation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Opening an HTTPS website:&lt;/strong&gt; TLS (Transport Layer Security) encrypts the communication between our browser and the website's server. SSL was its older predecessor and is no longer considered secure.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;But where does all this happen?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These tasks are handled by software running on our devices, sometimes with help from hardware.&lt;/p&gt;

&lt;p&gt;For example, video codecs handle encoding and compression on our phones, while browsers use TLS libraries to protect HTTPS connections.&lt;/p&gt;

&lt;p&gt;So, there isn't necessarily a separate Presentation Layer program running on our devices. These are responsibilities grouped under this layer in the OSI model.&lt;/p&gt;

&lt;p&gt;Now that our data is prepared, let's see how the communication between devices is managed in the &lt;strong&gt;Session Layer&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Session Layer (Layer 5)
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhz3uj7jj4qclxbq5taij.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhz3uj7jj4qclxbq5taij.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But the call shouldn't end after sending just one piece of audio or video, right? We need to keep the communication going until one of us ends the call.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Session Layer&lt;/strong&gt; represents how communication sessions between applications are established, maintained, and ended.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Starting a session:&lt;/strong&gt; When we start a WhatsApp call, the app sets up the communication needed for the call.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maintaining a session:&lt;/strong&gt; As we continue talking, the app keeps track of the ongoing call.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ending a session:&lt;/strong&gt; When we hang up, the app closes the call session.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can see something similar when we open a website. After logging in, the website may remember that we're logged in using a session cookie, so we don't have to enter our password on every page.&lt;/p&gt;

&lt;p&gt;But remember, a website's login session isn't exactly the same as an OSI Session Layer session. It's an application-level way of maintaining user state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But where does this happen?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In real applications like WhatsApp, the code responsible for call setup, tracking, and termination handles these tasks. For websites, application code and session-management libraries may handle user sessions.&lt;/p&gt;

&lt;p&gt;So, just like the Presentation Layer, we don't necessarily have a separate Session Layer program running on our devices.&lt;/p&gt;

&lt;p&gt;Now, we have an ongoing call session, but we still need to move the actual audio and video data between the devices.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Do We Have Both OSI and TCP/IP Models?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fit0b5qziiayfs4iq0r16.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fit0b5qziiayfs4iq0r16.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Until now, we've covered three layers of the OSI model: &lt;strong&gt;Application, Presentation, and Session&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you noticed, most of the work we discussed happens in the application code or the libraries it uses.&lt;/p&gt;

&lt;p&gt;For example, WhatsApp manages the call, video codecs handle encoding and compression, and encryption libraries protect the communication.&lt;/p&gt;

&lt;p&gt;Since these responsibilities are often handled together in applications, the &lt;strong&gt;TCP/IP model&lt;/strong&gt; groups all three into a single &lt;strong&gt;Application Layer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But why do we have two different models?&lt;/p&gt;

&lt;p&gt;Back in the 1970s, researchers were developing &lt;strong&gt;TCP/IP&lt;/strong&gt; to connect different networks. By 1983, TCP/IP was already being used on ARPANET.&lt;/p&gt;

&lt;p&gt;Around the same time, different companies had their own networking systems, and getting them to communicate wasn't easy.&lt;/p&gt;

&lt;p&gt;So, the &lt;strong&gt;International Organization for Standardization (ISO)&lt;/strong&gt; developed the OSI model to provide a common framework for networking standards. It divided communication into seven layers, making each responsibility clearer.&lt;/p&gt;

&lt;p&gt;Over time, TCP/IP became widely adopted because its protocols were already working in real networks, while the OSI protocol suite didn't achieve the same adoption.&lt;/p&gt;

&lt;p&gt;But we still use the OSI model to understand networking and troubleshoot problems because its seven layers help us identify where something might be going wrong.&lt;/p&gt;

&lt;p&gt;So, we have &lt;strong&gt;7 layers in OSI&lt;/strong&gt;, while the TCP/IP model is commonly shown with &lt;strong&gt;4 layers&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We'll see how the remaining layers relate to each other as we continue.&lt;/p&gt;

&lt;p&gt;Now, let's move to the &lt;strong&gt;Transport Layer&lt;/strong&gt;, which is present in both models.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Transport Layer (Layer 4)
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1fzaib2bf2tqgqi5ywla.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1fzaib2bf2tqgqi5ywla.png" alt=" " width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our video call is going on, and our phone has audio and video data to send to our friend.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Transport Layer&lt;/strong&gt; handles communication between applications running on different devices.&lt;/p&gt;

&lt;h3&gt;
  
  
  TCP and UDP
&lt;/h3&gt;

&lt;p&gt;During our video call, if a small piece of audio is lost, waiting for it again might interrupt our conversation.&lt;/p&gt;

&lt;p&gt;But if we're transferring an important file, we don't want any part of it missing.&lt;/p&gt;

&lt;p&gt;So, the Transport Layer mainly uses two protocols:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;TCP (Transmission Control Protocol):&lt;/strong&gt; When we need all our data to arrive correctly and in order, TCP takes care of missing data by sending it again.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;UDP (User Datagram Protocol):&lt;/strong&gt; When receiving data quickly is more important than recovering every missing piece, UDP lets us send data without waiting for those confirmations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;UDP is commonly used for real-time communication like video calls, while TCP is useful for communication that needs reliable delivery.&lt;/p&gt;

&lt;p&gt;We'll learn more about both protocols later.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Happens at the Transport Layer?
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. Segmentation
&lt;/h4&gt;

&lt;p&gt;During our video call, our phone keeps producing audio and video data.&lt;/p&gt;

&lt;p&gt;Instead of handling everything as one large piece, the data is handled in smaller pieces.&lt;/p&gt;

&lt;p&gt;In TCP, these pieces are called &lt;strong&gt;segments&lt;/strong&gt;, while UDP sends individual messages called &lt;strong&gt;datagrams&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We'll understand why dividing data into smaller pieces is useful in a moment.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Adding Port Numbers
&lt;/h4&gt;

&lt;p&gt;Our phone might have WhatsApp, Chrome, and other applications running at the same time.&lt;/p&gt;

&lt;p&gt;When data arrives, how does our phone know which application should receive it?&lt;/p&gt;

&lt;p&gt;The Transport Layer adds &lt;strong&gt;source and destination port numbers&lt;/strong&gt; to help identify the correct application.&lt;/p&gt;

&lt;p&gt;For example, HTTPS commonly uses destination port &lt;code&gt;443&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But what if we open Google and ChatGPT in two different Chrome tabs? Both use HTTPS and commonly connect to port &lt;code&gt;443&lt;/code&gt;, right?&lt;/p&gt;

&lt;p&gt;Along with port numbers, the source and destination IP addresses help distinguish different TCP connections.&lt;/p&gt;

&lt;p&gt;A TCP connection is identified using four values:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(Source IP, Source Port, Destination IP, Destination Port)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So, even if multiple HTTPS connections use destination port &lt;code&gt;443&lt;/code&gt;, the operating system can identify them correctly.&lt;/p&gt;

&lt;p&gt;Note: &lt;strong&gt;IP addresses are handled by the Network Layer&lt;/strong&gt;, which we'll discuss next. The Transport Layer adds the port numbers.&lt;/p&gt;

&lt;h4&gt;
  
  
  3. Sequence Control
&lt;/h4&gt;

&lt;p&gt;Suppose we send three pieces of data in this order:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;1 → 2 → 3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But they arrive in a different order:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;2 → 1 → 3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If the application needs the data in the correct order, we must keep track of which piece comes first.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sequence control&lt;/strong&gt; helps arrange the received data in the correct order when ordered delivery is required.&lt;/p&gt;

&lt;h4&gt;
  
  
  4. Flow Control
&lt;/h4&gt;

&lt;p&gt;Suppose our phone sends data faster than our friend's phone can receive it.&lt;/p&gt;

&lt;p&gt;If we keep sending too much data, the receiving device's buffer might fill up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flow control&lt;/strong&gt; helps adjust how much data the sender can send based on what the receiver can handle.&lt;/p&gt;

&lt;h4&gt;
  
  
  5. Error Control
&lt;/h4&gt;

&lt;p&gt;Suppose we send:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;1 → 2 → 3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But only &lt;code&gt;1 → 3&lt;/code&gt; arrives.&lt;/p&gt;

&lt;p&gt;The second piece might have been lost somewhere along the way. Sometimes, data might also get corrupted during transmission.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Error control&lt;/strong&gt; helps detect these problems and, when reliable delivery is needed, recover the missing data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Do We Break Data into Smaller Pieces?
&lt;/h3&gt;

&lt;p&gt;There are a few practical reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Packet size limits:&lt;/strong&gt; Networks have limits on how much data can fit into a single packet. We can't send an unlimited amount of data at once.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Continuous communication:&lt;/strong&gt; During our video call, smaller pieces can be sent as audio and video are produced, without waiting for a large amount of data to accumulate.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Easier retransmission:&lt;/strong&gt; If a piece of data is lost, a reliable protocol can resend the missing data instead of the entire large message.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sharing the network:&lt;/strong&gt; Smaller packets make it easier for different applications, like WhatsApp and YouTube, to share the same network link. Network devices can send packets from different applications without waiting for an entire large transfer to finish.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The last point is also related to &lt;strong&gt;packet switching&lt;/strong&gt;, which we'll understand when discussing how data travels through networks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Where Does All This Happen?
&lt;/h3&gt;

&lt;p&gt;Most TCP and UDP processing happens inside our phone's &lt;strong&gt;operating system&lt;/strong&gt;, such as Android or iOS.&lt;/p&gt;

&lt;p&gt;Applications use networking APIs to send data, while the operating system usually handles these protocols.&lt;/p&gt;

&lt;p&gt;So, when we write application code, we don't normally have to manage TCP sequence numbers, acknowledgements, or retransmissions ourselves.&lt;/p&gt;

&lt;p&gt;The Transport Layer is present in both the &lt;strong&gt;OSI and TCP/IP models&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;After handling the outgoing data, it passes it to the &lt;strong&gt;Network Layer&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Network Layer (Layer 3)
&lt;/h3&gt;

&lt;p&gt;Now, the Transport Layer has handled our data and added the port numbers.&lt;/p&gt;

&lt;p&gt;But our friend is connected to a different network in the US. How does the data reach their device?&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Network Layer&lt;/strong&gt; takes care of getting data from the source device toward the destination device across different networks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5sf9d79etzmd6myefhp9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5sf9d79etzmd6myefhp9.png" alt=" " width="799" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It mainly handles two things:&lt;/p&gt;

&lt;h4&gt;
  
  
  1. IP Addressing
&lt;/h4&gt;

&lt;p&gt;We already learned about IP addresses in Part 1.&lt;/p&gt;

&lt;p&gt;Just like port numbers help identify the correct application, &lt;strong&gt;IP addresses help identify the source and destination devices&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The Network Layer adds an IP header containing the source and destination IP addresses.&lt;/p&gt;

&lt;p&gt;The data, along with this header, is called an &lt;strong&gt;IP packet&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Routing
&lt;/h4&gt;

&lt;p&gt;Our friend's phone isn't directly connected to our phone.&lt;/p&gt;

&lt;p&gt;The packet may need to pass through several routers before reaching the destination.&lt;/p&gt;

&lt;p&gt;Each router checks the destination IP address and uses its routing table to decide where to forward the packet next.&lt;/p&gt;

&lt;p&gt;This process is called &lt;strong&gt;routing&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Where Does This Happen?
&lt;/h4&gt;

&lt;p&gt;On our phone, the &lt;strong&gt;operating system (Android or iOS)&lt;/strong&gt; creates the IP packet by adding the source and destination IP addresses.&lt;/p&gt;

&lt;p&gt;Now, depending on how we're connected to the Internet:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Wi-Fi:&lt;/strong&gt; The packet goes through our Wi-Fi router.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mobile Data (4G/5G):&lt;/strong&gt; The packet travels through a nearby mobile tower into our mobile operator's network, such as Jio or Airtel.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From there, routers check the destination IP address and forward the packet toward its destination.&lt;/p&gt;

&lt;p&gt;So, the Network Layer works both inside our phone's operating system and in routers across the network.&lt;/p&gt;

&lt;p&gt;The Network Layer in OSI is called the &lt;strong&gt;Internet Layer&lt;/strong&gt; in the TCP/IP model.&lt;/p&gt;

&lt;p&gt;Now, our packet has the IP addresses needed to travel across networks. But how does it actually move from our phone to the next device?&lt;/p&gt;

&lt;p&gt;We'll see that in the &lt;strong&gt;Data Link Layer&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Data Link Layer (Layer 2)
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fw0lhnvfydlo6gmd8rtm5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fw0lhnvfydlo6gmd8rtm5.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our phone has prepared the IP packet, and now it needs to send it to our home Wi-Fi router.&lt;/p&gt;

&lt;p&gt;We already know the router's IP address. But how does our phone actually identify the router on the local network?&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;MAC (Media Access Control) address&lt;/strong&gt; is a unique identifier assigned to a network interface, such as the Wi-Fi interface on our phone or router, to identify it on a local network.&lt;/p&gt;

&lt;p&gt;So, our phone needs to find the router's MAC address before sending the data.&lt;/p&gt;

&lt;h4&gt;
  
  
  How Does Our Phone Find the MAC Address?
&lt;/h4&gt;

&lt;p&gt;Suppose our Wi-Fi router's IP address is &lt;code&gt;192.168.1.1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Our phone knows this IP address, but it doesn't know the corresponding MAC address.&lt;/p&gt;

&lt;p&gt;For IPv4, it uses something called &lt;strong&gt;ARP (Address Resolution Protocol)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Our phone sends a broadcast message to the local network, essentially asking:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;"Hey, who has the IP address 192.168.1.1? Tell me your MAC address."&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Since it's a broadcast, all devices on that local network can receive the request.&lt;/p&gt;

&lt;p&gt;The router recognizes its IP address and replies with its MAC address.&lt;/p&gt;

&lt;p&gt;Our phone now knows the router's MAC address and saves it in its ARP cache, so it doesn't need to ask again for every packet.&lt;/p&gt;

&lt;h4&gt;
  
  
  Framing
&lt;/h4&gt;

&lt;p&gt;Now our phone knows where to send the packet on the local network.&lt;/p&gt;

&lt;p&gt;The Data Link Layer takes the IP packet and wraps it inside a &lt;strong&gt;frame&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For our Wi-Fi connection, the frame includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Source MAC:&lt;/strong&gt; Our phone's Wi-Fi MAC address.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Receiver MAC:&lt;/strong&gt; Our Wi-Fi router's MAC address.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IP packet:&lt;/strong&gt; Contains the source and destination IP addresses added by the Network Layer.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Error Detection
&lt;/h4&gt;

&lt;p&gt;The Data Link Layer also adds error-detection information to the frame.&lt;/p&gt;

&lt;p&gt;This helps detect whether bits have been corrupted during transmission.&lt;/p&gt;

&lt;h4&gt;
  
  
  Where Does This Happen?
&lt;/h4&gt;

&lt;p&gt;On our phone, the &lt;strong&gt;operating system&lt;/strong&gt; handles tasks such as finding the router's MAC address using ARP.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Wi-Fi driver and Wi-Fi hardware&lt;/strong&gt; work together to prepare the Wi-Fi frame for transmission.&lt;/p&gt;

&lt;p&gt;If we use mobile data instead, our phone's cellular modem handles communication with the mobile network using cellular protocols rather than Wi-Fi.&lt;/p&gt;

&lt;p&gt;In the TCP/IP model, the Data Link Layer is grouped with the Physical Layer under the &lt;strong&gt;Network Access Layer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now our phone has prepared the frame with the required information. The &lt;strong&gt;Physical Layer&lt;/strong&gt; takes care of transmitting it as signals over the connection.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Physical Layer (Layer 1)
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftg2egkeqebzcpq84qq0k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftg2egkeqebzcpq84qq0k.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our phone has prepared the Wi-Fi frame with the required MAC addresses and IP packet.&lt;/p&gt;

&lt;p&gt;But everything we've discussed so far is still information inside our phone.&lt;/p&gt;

&lt;p&gt;How does this data actually travel from our phone to our Wi-Fi router?&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Physical Layer&lt;/strong&gt; handles the actual transmission of data through a physical medium, such as wireless signals, electrical signals, or light.&lt;/p&gt;

&lt;h4&gt;
  
  
  How Does Our Phone Send the Data?
&lt;/h4&gt;

&lt;p&gt;Our Wi-Fi frame contains digital data, represented as bits (&lt;code&gt;0&lt;/code&gt;s and &lt;code&gt;1&lt;/code&gt;s).&lt;/p&gt;

&lt;p&gt;But our router cannot receive these bits directly from our phone's memory.&lt;/p&gt;

&lt;p&gt;They need to be transmitted as signals.&lt;/p&gt;

&lt;p&gt;Since we're using Wi-Fi, our phone's Wi-Fi hardware converts the digital information into radio signals and transmits them through the air.&lt;/p&gt;

&lt;p&gt;Our router's Wi-Fi hardware receives these signals and recovers the transmitted bits.&lt;/p&gt;

&lt;h4&gt;
  
  
  What Happens When We Use a Cable?
&lt;/h4&gt;

&lt;p&gt;Not every network connection uses Wi-Fi.&lt;/p&gt;

&lt;p&gt;For example, when we connect a computer to a router using an Ethernet cable, the data travels through electrical signals.&lt;/p&gt;

&lt;p&gt;Similarly, Internet service providers often use fiber-optic cables to carry data over long distances.&lt;/p&gt;

&lt;p&gt;In fiber-optic connections, data is transmitted using light signals.&lt;/p&gt;

&lt;p&gt;So, the physical medium changes depending on the connection:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Wi-Fi:&lt;/strong&gt; Radio signals through the air.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ethernet cables:&lt;/strong&gt; Electrical signals through copper wires.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fiber-optic cables:&lt;/strong&gt; Light signals through optical fibers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Physical Layer defines how bits are represented and transmitted over these different media.&lt;/p&gt;

&lt;h4&gt;
  
  
  Where Does This Happen?
&lt;/h4&gt;

&lt;p&gt;On our phone, the &lt;strong&gt;Wi-Fi hardware&lt;/strong&gt;, including its radio circuitry and antenna, handles the wireless transmission.&lt;/p&gt;

&lt;p&gt;When using mobile data, the phone's &lt;strong&gt;cellular modem and antenna&lt;/strong&gt; communicate with the mobile network using radio signals.&lt;/p&gt;

&lt;p&gt;For wired connections, network interface hardware converts digital data into electrical or optical signals, depending on the technology.&lt;/p&gt;

&lt;p&gt;So, unlike the upper layers that mainly involve software, the Physical Layer is closely connected to the actual networking hardware.&lt;/p&gt;

&lt;p&gt;In the TCP/IP model, the Physical Layer is grouped with the Data Link Layer under the &lt;strong&gt;Network Access Layer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Our data has now left the phone and started its journey through the network.&lt;/p&gt;

&lt;h2&gt;
  
  
  Phase 2: From Our Router to Our Friend's Router
&lt;/h2&gt;

&lt;p&gt;Our data has reached our home Wi-Fi router in India.&lt;/p&gt;

&lt;p&gt;Now, let's see how it travels to our friend's home router in the US.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Physical Layer (Layer 1):&lt;/strong&gt; Our router receives the Wi-Fi signals from our phone and converts them back into bits.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data Link Layer (Layer 2):&lt;/strong&gt; The router processes the Wi-Fi frame and extracts the IP packet.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Network Layer (Layer 3):&lt;/strong&gt; The router checks the destination IP address and uses its routing table to decide the next hop toward our friend's network.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data Link Layer (Layer 2):&lt;/strong&gt; The router prepares the IP packet for the next connection, using the link-layer protocol supported by that connection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Physical Layer (Layer 1):&lt;/strong&gt; The router transmits the data toward our Internet Service Provider (ISP), such as Jio or Airtel, through its outgoing connection.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When the packet reaches the ISP's router, the same &lt;strong&gt;Layer 1 → Layer 2 → Layer 3 → Layer 2 → Layer 1&lt;/strong&gt; process repeats.&lt;/p&gt;

&lt;p&gt;This continues as the packet passes through different routers and networks.&lt;/p&gt;

&lt;p&gt;Since our friend is in the US, the data may also travel through &lt;strong&gt;submarine fiber-optic cables&lt;/strong&gt; connecting networks across oceans. These cables transmit data using light signals, as we learned in the Physical Layer.&lt;/p&gt;

&lt;p&gt;Eventually, the packet reaches our friend's ISP in the US and is forwarded toward their home router.&lt;/p&gt;

&lt;p&gt;Throughout this journey, routers mainly use &lt;strong&gt;Layers 1, 2, and 3&lt;/strong&gt; to forward packets. They don't need to process all seven layers like our phone does.&lt;/p&gt;

&lt;p&gt;Now that our data has reached our friend's home router, let's see how it reaches their phone in &lt;strong&gt;Phase 3&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Phase 3: From Our Friend's Router to Their Phone
&lt;/h2&gt;

&lt;p&gt;Our data has finally reached our friend's home Wi-Fi router in the US.&lt;/p&gt;

&lt;p&gt;Now, the router needs to send it to our friend's phone.&lt;/p&gt;

&lt;p&gt;Just like our phone used the router's MAC address in Phase 1, our friend's router uses their phone's MAC address to prepare a Wi-Fi frame.&lt;/p&gt;

&lt;p&gt;The router then transmits it using Wi-Fi signals.&lt;/p&gt;

&lt;p&gt;But our friend's home might have multiple phones and laptops connected to the same Wi-Fi network.&lt;/p&gt;

&lt;p&gt;Since Wi-Fi uses radio signals, other nearby devices may also detect the transmission.&lt;/p&gt;

&lt;p&gt;So how do they know which device should accept the data?&lt;/p&gt;

&lt;p&gt;The Wi-Fi frame contains the &lt;strong&gt;receiver's MAC address&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Each device's Wi-Fi hardware checks this address. If the frame is meant for another device, it normally ignores it without passing the data to the operating system.&lt;/p&gt;

&lt;p&gt;Our friend's phone sees that the receiver MAC address matches its own and accepts the frame.&lt;/p&gt;

&lt;p&gt;This is called &lt;strong&gt;unicast&lt;/strong&gt;, where data is sent to one specific device rather than broadcast to everyone.&lt;/p&gt;

&lt;p&gt;Now, let's see how our friend's phone processes the data through the seven layers, in reverse order.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Physical Layer (Layer 1):&lt;/strong&gt; The phone's Wi-Fi hardware receives the radio signals and converts them back into bits (&lt;code&gt;0&lt;/code&gt;s and &lt;code&gt;1&lt;/code&gt;s).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data Link Layer (Layer 2):&lt;/strong&gt; The Wi-Fi hardware checks the receiver MAC address, verifies that the frame isn't corrupted, and extracts the IP packet.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Network Layer (Layer 3):&lt;/strong&gt; The phone checks whether the destination IP address belongs to it. If it does, the IP packet is processed and its contents are passed to the appropriate transport protocol.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Transport Layer (Layer 4):&lt;/strong&gt; The operating system checks the destination port and other connection information to find the correct application socket. If TCP is being used, it also handles ordering and retransmission of missing data. With UDP, it delivers the received datagram without those guarantees.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Session Layer (Layer 5):&lt;/strong&gt; WhatsApp identifies the incoming audio and video as part of our ongoing video call.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Presentation Layer (Layer 6):&lt;/strong&gt; WhatsApp decrypts the data and decodes the compressed audio and video so the phone can play them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Application Layer (Layer 7):&lt;/strong&gt; WhatsApp plays our voice through the speaker and displays our video on the screen.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Our voice and video have now reached our friend's phone in the US, after travelling through multiple networks.&lt;/p&gt;

&lt;p&gt;When our friend speaks, the same journey happens in the opposite direction, allowing us to have a real-time conversation.&lt;/p&gt;

&lt;p&gt;The next time you're on a video call, think about how your voice and video travel through these layers and across different networks before reaching the other person. All of this happens in just milliseconds!&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>networking</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Understanding Binary Lifting: Finding Ancestors Efficiently in Trees</title>
      <dc:creator>Anusha Renangi</dc:creator>
      <pubDate>Mon, 02 Nov 2026 02:09:15 +0000</pubDate>
      <link>https://dev.to/anusha_renangi_05a4e41d35/understanding-binary-lifting-finding-ancestors-efficiently-in-trees-1ph4</link>
      <guid>https://dev.to/anusha_renangi_05a4e41d35/understanding-binary-lifting-finding-ancestors-efficiently-in-trees-1ph4</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;When working with trees, we sometimes need to find the ancestors of a particular node.&lt;/p&gt;

&lt;p&gt;For example, in an organization, every employee might have a manager, and that manager might report to another manager.&lt;/p&gt;

&lt;p&gt;Now, if we want to find an employee's manager, we can directly look at their parent in the hierarchy.&lt;/p&gt;

&lt;p&gt;But what if we want to find the manager who is 10 levels above that employee?&lt;/p&gt;

&lt;p&gt;We could keep moving from one parent to another until we reach the required ancestor.&lt;/p&gt;

&lt;p&gt;This works, but if the tree is large and we need to perform thousands of such queries, repeatedly traversing the tree can become expensive.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Binary Lifting&lt;/strong&gt; is a technique that helps us find ancestors efficiently by precomputing information that allows us to jump multiple levels at once.&lt;/p&gt;

&lt;p&gt;Instead of moving one level at a time, we can jump 1, 2, 4, 8, 16, and more levels using powers of two.&lt;/p&gt;

&lt;h2&gt;
  
  
  Normal Parent Traversal vs. Binary Lifting
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fm67gdehxyaud2lsghnbu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fm67gdehxyaud2lsghnbu.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's take a tree where each node has a parent, except the root node.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        A
        |
        B
        |
        C
        |
        D
        |
        E
        |
        F
        |
        G
        |
        H
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;strong&gt;A is the root node&lt;/strong&gt;, and each node is connected to its immediate parent.&lt;/p&gt;

&lt;p&gt;Now, we want to find the &lt;strong&gt;6th ancestor of node H&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Normal Parent Traversal
&lt;/h3&gt;

&lt;p&gt;We can start from H and move to its parent one level at a time.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;H → G → F → E → D → C → B&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We need &lt;strong&gt;6 jumps&lt;/strong&gt; to reach node B.&lt;/p&gt;

&lt;p&gt;If we want to find the Kth ancestor of a node, this approach can take &lt;strong&gt;O(K)&lt;/strong&gt; time.&lt;/p&gt;

&lt;p&gt;For a small tree, this is fine. But if we have thousands of nodes and need to answer many ancestor queries, traversing one level at a time becomes inefficient.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using Binary Lifting
&lt;/h3&gt;

&lt;p&gt;Instead of moving to the immediate parent every time, what if we already knew the ancestor at certain distances?&lt;/p&gt;

&lt;p&gt;For each node, we can precompute its ancestors at distances:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;1, 2, 4, 8, 16, 32, ...&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For example, from node H:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1 level above → G&lt;/li&gt;
&lt;li&gt;2 levels above → F&lt;/li&gt;
&lt;li&gt;4 levels above → D&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, to find the 6th ancestor, we don't need six individual jumps.&lt;/p&gt;

&lt;p&gt;We can split 6 into powers of two:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6 = 4 + 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Starting from H:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;H → D (4 levels) → B (2 levels)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We reached the same ancestor using just &lt;strong&gt;2 jumps instead of 6&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is the idea behind Binary Lifting.&lt;/p&gt;

&lt;p&gt;By storing ancestors at distances that are powers of two, we can find the Kth ancestor in &lt;strong&gt;O(log K)&lt;/strong&gt; time after preprocessing.&lt;/p&gt;

&lt;h3&gt;
  
  
  How Are These Values Calculated?
&lt;/h3&gt;

&lt;p&gt;We already know that Binary Lifting stores ancestors at distances of &lt;strong&gt;1, 2, 4, 8, 16, ...&lt;/strong&gt; levels.&lt;/p&gt;

&lt;p&gt;But how do we calculate these ancestors without moving through every parent again?&lt;/p&gt;

&lt;p&gt;We can use the ancestor information we've already calculated.&lt;/p&gt;

&lt;p&gt;Let's take node H from our tree.&lt;/p&gt;

&lt;p&gt;We know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The 1st ancestor of H is G.&lt;/li&gt;
&lt;li&gt;The 2nd ancestor of H is F.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, we want to find the &lt;strong&gt;4th ancestor of H&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Instead of moving four levels from H, we can split the journey into two jumps of 2 levels.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;H → F → D&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We already know that F is the 2nd ancestor of H, and D is the 2nd ancestor of F.&lt;/p&gt;

&lt;p&gt;So, &lt;strong&gt;the 4th ancestor of H is the 2nd ancestor of its 2nd ancestor.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We can use the same approach for larger jumps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;2 levels = 1 + 1&lt;/li&gt;
&lt;li&gt;4 levels = 2 + 2&lt;/li&gt;
&lt;li&gt;8 levels = 4 + 4&lt;/li&gt;
&lt;li&gt;16 levels = 8 + 8&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Deriving the Formula
&lt;/h3&gt;

&lt;p&gt;We represent the ancestor table using &lt;code&gt;up[node][j]&lt;/code&gt;, where &lt;code&gt;j&lt;/code&gt; represents the power of two.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;up[H][0]&lt;/code&gt; → 2⁰ = 1 level&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;up[H][1]&lt;/code&gt; → 2¹ = 2 levels&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;up[H][2]&lt;/code&gt; → 2² = 4 levels&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To find an ancestor (2^j) levels above a node, we divide the jump into two equal parts.&lt;/p&gt;

&lt;p&gt;Mathematically, we want to establish:&lt;code&gt;2^j = 2^(j-1) + 2^(j-1)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fp5iiugqcmx4r3u7rgkk4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fp5iiugqcmx4r3u7rgkk4.png" alt=" " width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This means we can find an ancestor (2^j) levels above a node by making two jumps of (2^{j-1}) levels.&lt;/p&gt;

&lt;p&gt;The first jump takes us to:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;up[node][j-1]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;From that ancestor, we make another jump of the same size:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;up[up[node][j-1]][j-1]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So, the Binary Lifting formula becomes:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;up[node][j] = up[up[node][j-1]][j-1]&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For example, to calculate the 4th ancestor of H:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;H&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;H&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# 2nd ancestor of H = F
# 2nd ancestor of F = D
&lt;/span&gt;
&lt;span class="c1"&gt;# Therefore, 4th ancestor of H = D
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the immediate parent, we directly store:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;up[node][0] = parent[node]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If an ancestor doesn't exist, we store a special value such as &lt;code&gt;-1&lt;/code&gt; and avoid using it as a valid node index.&lt;/p&gt;

&lt;p&gt;For a tree with N nodes, we generally precompute ancestors up to &lt;code&gt;2^⌊log₂ N⌋&lt;/code&gt; levels.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvwelelfdi912vsclu19v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvwelelfdi912vsclu19v.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Time and Space Complexity
&lt;/h3&gt;

&lt;p&gt;For a tree with &lt;strong&gt;N nodes&lt;/strong&gt;, we don't need to store every ancestor of each node.&lt;/p&gt;

&lt;p&gt;We only store ancestors at distances of &lt;strong&gt;1, 2, 4, 8, 16, ...&lt;/strong&gt; until we reach the maximum possible depth of the tree.&lt;/p&gt;

&lt;p&gt;For example, if our tree has 16 nodes, the jump distances we need to consider are:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;1, 2, 4, 8, 16&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That's just 5 entries per node, instead of storing up to 15 individual ancestors.&lt;/p&gt;

&lt;p&gt;But how many entries do we need for a tree with N nodes?&lt;/p&gt;

&lt;p&gt;Since our jump distances are powers of two, the maximum required power satisfies:&lt;/p&gt;

&lt;p&gt;For a tree with N nodes, we generally precompute ancestors up to &lt;code&gt;2^⌊log₂ N⌋&lt;/code&gt; levels.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;2^j ≤ N&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Taking logarithm base 2 on both sides:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;j ≤ log₂ N&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Since we start from &lt;code&gt;j = 0&lt;/code&gt;, the maximum number of entries per node is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;⌊log₂ N⌋ + 1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Therefore, &lt;strong&gt;each node stores O(log N) ancestor entries&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now, we have N nodes, and for each node, we calculate at most O(log N) entries.&lt;/p&gt;

&lt;p&gt;So, the total number of entries is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;N × O(log N) = O(N log N)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Each entry is calculated in constant time using our previously derived formula.&lt;/p&gt;

&lt;p&gt;Therefore:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity: O(N log N)&lt;/strong&gt; — We calculate at most O(log N) ancestor entries for each of the N nodes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity: O(N log N)&lt;/strong&gt; — We store those entries in the binary lifting table.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Finding the Kth Ancestor
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flcho1kow19hyjy83kikv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flcho1kow19hyjy83kikv.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our previous example, we found the 6th ancestor of node H by splitting 6 into two jumps:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;6 = 4 + 2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But how do we decide which jumps to make when K is any number?&lt;/p&gt;

&lt;p&gt;We can represent K as a sum of powers of two.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;5 = 4 + 1&lt;/li&gt;
&lt;li&gt;6 = 4 + 2&lt;/li&gt;
&lt;li&gt;7 = 4 + 2 + 1&lt;/li&gt;
&lt;li&gt;13 = 8 + 4 + 1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every positive integer can be represented as a sum of distinct powers of two. This is also how numbers are represented in binary.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using Binary Representation
&lt;/h3&gt;

&lt;p&gt;Let's take K = 6.&lt;/p&gt;

&lt;p&gt;Its binary representation is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;6 = (110)₂ = 4 + 2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Each binary digit represents a power of two:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Power of 2:    2²  2¹  2⁰
Jump size:      4   2   1
Binary digit:   1   1   0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A binary digit of &lt;code&gt;1&lt;/code&gt; means we need that jump, while &lt;code&gt;0&lt;/code&gt; means we can skip it.&lt;/p&gt;

&lt;p&gt;For K = 6, we need jumps of 4 and 2 levels.&lt;/p&gt;

&lt;p&gt;Starting from node H:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;H → D → B&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We reached the 6th ancestor using just two jumps.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation
&lt;/h3&gt;

&lt;p&gt;We can use bitwise operations to identify which jumps are required.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;kth_ancestor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;bit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;bit&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

        &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="n"&gt;bit&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;k &amp;amp; 1&lt;/code&gt;&lt;/strong&gt; checks whether the current rightmost binary digit is 1. If it is, we make the corresponding jump.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;k &amp;gt;&amp;gt;= 1&lt;/code&gt;&lt;/strong&gt; shifts K one bit to the right, allowing us to check the next binary digit.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;bit += 1&lt;/code&gt;&lt;/strong&gt; moves to the next power of two in our ancestor table.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;up&lt;/code&gt; table must contain the required jump columns. If K is beyond the supported range or the ancestor doesn't exist, we return &lt;code&gt;-1&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Time Complexity
&lt;/h3&gt;

&lt;p&gt;For every iteration, we remove one binary digit from K by shifting it right.&lt;/p&gt;

&lt;p&gt;A positive integer K has &lt;code&gt;⌊log₂ K⌋ + 1&lt;/code&gt; binary digits.&lt;/p&gt;

&lt;p&gt;Since each iteration performs at most one table lookup, finding the Kth ancestor takes:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;O(log K)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The binary lifting table is already precomputed, so we don't need to traverse the intermediate nodes individually.&lt;/p&gt;

&lt;h3&gt;
  
  
  Binary Lifting – Python Implementation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BinaryLifting&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LOG&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bit_length&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LOG&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;]&lt;/span&gt;

        &lt;span class="c1"&gt;# Store immediate parents
&lt;/span&gt;        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

        &lt;span class="c1"&gt;# Precompute ancestors at powers of two
&lt;/span&gt;        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LOG&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
                &lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;kth_ancestor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;k must be non-negative&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;

        &lt;span class="n"&gt;bit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;bit&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

            &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
            &lt;span class="n"&gt;bit&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Time and Space Complexity:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Preprocessing:&lt;/strong&gt; O(N log N)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Kth Ancestor Query:&lt;/strong&gt; O(log K)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space:&lt;/strong&gt; O(N log N)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Lowest Common Ancestor (LCA)
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffrnb8v47ud9v3j626h0g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffrnb8v47ud9v3j626h0g.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's understand another use of Binary Lifting, which is finding the &lt;strong&gt;Lowest Common Ancestor (LCA)&lt;/strong&gt; of two nodes in a tree.&lt;/p&gt;

&lt;p&gt;In a tree, two nodes can have multiple common ancestors.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Lowest Common Ancestor (LCA)&lt;/strong&gt; is the deepest common ancestor of two nodes, meaning the common ancestor closest to both nodes.&lt;/p&gt;

&lt;p&gt;Let's take an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            A
           / \
          B   C
         / \   \
        D   E   F
       / \     / \
      G   H   I   J
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, A is the root node.&lt;/p&gt;

&lt;p&gt;If we want to find the LCA of &lt;strong&gt;G and H&lt;/strong&gt;, both nodes share D, B, and A as their ancestors.&lt;/p&gt;

&lt;p&gt;Among them, D is the closest common ancestor.&lt;/p&gt;

&lt;p&gt;So, &lt;strong&gt;LCA(G, H) = D&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Similarly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;LCA(G, E) = B&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LCA(I, J) = F&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LCA(G, J) = A&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Finding LCA Without Binary Lifting
&lt;/h4&gt;

&lt;p&gt;One way to find the LCA is to move upward through the tree.&lt;/p&gt;

&lt;p&gt;For example, to find the LCA of G and E:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;G's ancestors are D → B → A.&lt;/li&gt;
&lt;li&gt;E's ancestors are B → A.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The closest common ancestor is B.&lt;/p&gt;

&lt;p&gt;We can find it by storing the ancestors of one node and checking the ancestors of the other.&lt;/p&gt;

&lt;p&gt;This approach can take &lt;strong&gt;O(N)&lt;/strong&gt; time in the worst case for a tree with N nodes.&lt;/p&gt;

&lt;p&gt;If we have many LCA queries on a large tree, repeatedly traversing the ancestors becomes expensive.&lt;/p&gt;

&lt;p&gt;We already have a Binary Lifting table that allows us to jump multiple levels at once. We can use the same table to find the LCA more efficiently.&lt;/p&gt;

&lt;h3&gt;
  
  
  Finding LCA Using Binary Lifting
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fe4mduq7f9lnv7xb2hhdf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fe4mduq7f9lnv7xb2hhdf.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We already know how Binary Lifting helps us jump multiple levels in a tree using powers of two.&lt;/p&gt;

&lt;p&gt;We can use the same idea to find the LCA of two nodes.&lt;/p&gt;

&lt;p&gt;There are two steps involved:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Bring both nodes to the same depth.&lt;/li&gt;
&lt;li&gt;Move both nodes upward until we find their lowest common ancestor.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Step 1: Bring Both Nodes to the Same Depth
&lt;/h4&gt;

&lt;p&gt;Let's use the same tree:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            A
           / \
          B   C
         / \   \
        D   E   F
       / \     / \
      G   H   I   J
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, we need to know the &lt;strong&gt;depth&lt;/strong&gt; of each node, which is the number of edges from the root to that node.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Depth of A = 0&lt;/li&gt;
&lt;li&gt;Depth of B = 1&lt;/li&gt;
&lt;li&gt;Depth of D = 2&lt;/li&gt;
&lt;li&gt;Depth of G = 3&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, let's find the &lt;strong&gt;LCA of G and E&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;G is at depth 3, while E is at depth 2.&lt;/p&gt;

&lt;p&gt;Since both nodes are at different depths, we first move the deeper node upward until both are at the same depth.&lt;/p&gt;

&lt;p&gt;The depth difference is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;3-2=1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Using our Binary Lifting table, we move G one level upward.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;G → D&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, D and E are both at depth 2.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 2: Move Both Nodes Upward
&lt;/h4&gt;

&lt;p&gt;Once both nodes are at the same depth, we need to find where their paths meet.&lt;/p&gt;

&lt;p&gt;Instead of moving one level at a time, we use our precomputed ancestor table to make larger jumps.&lt;/p&gt;

&lt;p&gt;We start with the largest available jump and move toward smaller jumps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If the ancestors of both nodes at a particular jump distance are different, we move both nodes to those ancestors.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If they're the same, we don't make that jump because it could move us to their common ancestor or above it.&lt;/p&gt;

&lt;p&gt;For our example, D and E have the same parent, B.&lt;/p&gt;

&lt;p&gt;So, we don't need to move them any further.&lt;/p&gt;

&lt;p&gt;Their immediate parent is the LCA.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LCA(G, E) = B&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Why Do We Check Larger Jumps First?
&lt;/h4&gt;

&lt;p&gt;Our goal is to bring both nodes as close as possible to their LCA without reaching or crossing it.&lt;/p&gt;

&lt;p&gt;For larger trees, moving through every parent would take more time.&lt;/p&gt;

&lt;p&gt;Binary Lifting allows us to skip multiple levels whenever the ancestors of both nodes are different.&lt;/p&gt;

&lt;p&gt;After checking all the powers of two, both nodes will be immediately below their LCA.&lt;/p&gt;

&lt;p&gt;We can then return the parent of either node.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One special case:&lt;/strong&gt; If both nodes become the same after adjusting their depths, that node itself is the LCA.&lt;/p&gt;

&lt;p&gt;Since we only check &lt;strong&gt;O(log N)&lt;/strong&gt; jump distances, finding the LCA takes &lt;strong&gt;O(log N)&lt;/strong&gt; time after preprocessing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Binary Lifting Implementation for LCA
&lt;/h3&gt;

&lt;p&gt;We can extend our existing Binary Lifting implementation to find the Lowest Common Ancestor of two nodes.&lt;/p&gt;

&lt;p&gt;Along with the ancestor table, we also need to store the depth of each node.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BinaryLiftingLCA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;edges&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LOG&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bit_length&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;depth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LOG&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;

        &lt;span class="c1"&gt;# Build adjacency list
&lt;/span&gt;        &lt;span class="n"&gt;graph&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[]&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;edges&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="c1"&gt;# Find parents and depths
&lt;/span&gt;        &lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
        &lt;span class="n"&gt;visited&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
        &lt;span class="n"&gt;visited&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
        &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;neighbor&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;visited&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;neighbor&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
                    &lt;span class="n"&gt;visited&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;neighbor&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
                    &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;neighbor&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;
                    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;depth&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;neighbor&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;depth&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
                    &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;neighbor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="c1"&gt;# Precompute ancestors at powers of two
&lt;/span&gt;        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LOG&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
                &lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;kth_ancestor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;k must be non-negative&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;depth&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;

        &lt;span class="n"&gt;bit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;bit&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

            &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
            &lt;span class="n"&gt;bit&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;lca&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# Step 1: Identify the deeper node
&lt;/span&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;depth&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;depth&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
            &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;

        &lt;span class="c1"&gt;# Step 2: Calculate the depth difference
&lt;/span&gt;        &lt;span class="n"&gt;depth_diff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;depth&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;depth&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

        &lt;span class="c1"&gt;# Step 3: Bring both nodes to the same depth
&lt;/span&gt;        &lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;kth_ancestor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;depth_diff&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;

        &lt;span class="c1"&gt;# Step 4: Move both nodes upward
&lt;/span&gt;        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LOG&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="nf"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
                    &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]):&lt;/span&gt;
                &lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
                &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

        &lt;span class="c1"&gt;# Step 5: Return their common parent
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;lca()&lt;/code&gt; method follows these steps:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Identify the deeper node&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We compare the depths of both nodes and make sure &lt;code&gt;u&lt;/code&gt; is the deeper node.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Calculate the depth difference&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;depth_diff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;depth&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;depth&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells us how many levels the deeper node needs to move upward.&lt;/p&gt;

&lt;p&gt;For example, if &lt;code&gt;u&lt;/code&gt; is at depth 5 and &lt;code&gt;v&lt;/code&gt; is at depth 2:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;depth_diff = 5 - 2 = 3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So, we need to move &lt;code&gt;u&lt;/code&gt; upward by 3 levels.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Bring both nodes to the same depth&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We use our previously implemented &lt;code&gt;kth_ancestor()&lt;/code&gt; method to move the deeper node upward by &lt;code&gt;depth_diff&lt;/code&gt; levels.&lt;/p&gt;

&lt;p&gt;If both nodes become the same, that node itself is the LCA.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Move both nodes upward&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We check their ancestors from the largest jump to the smallest. Whenever their ancestors are different, we move both nodes upward.&lt;/p&gt;

&lt;p&gt;After completing these jumps, both nodes are immediately below their LCA.&lt;/p&gt;

&lt;p&gt;We return the immediate parent of either node.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This code uses the &lt;code&gt;kth_ancestor()&lt;/code&gt; method from our previous implementation. We need to include that method in the class when running the code, but we're not repeating it here.&lt;/p&gt;

&lt;p&gt;The implementation assumes a valid, connected, undirected tree with nodes numbered from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;N-1&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Time and Space Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Preprocessing:&lt;/strong&gt; O(N log N) — Building the ancestor table.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LCA Query:&lt;/strong&gt; O(log N) — Finding the depth difference and checking the precomputed jump distances.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space:&lt;/strong&gt; O(N log N) — Storing the ancestor table and node depths.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The same precomputed table can be reused for multiple LCA queries.&lt;/p&gt;

&lt;h3&gt;
  
  
  Practice Problems
&lt;/h3&gt;

&lt;p&gt;Here are a few LeetCode problems to practice Binary Lifting:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Kth Ancestor of a Tree Node&lt;/strong&gt; — &lt;a href="https://leetcode.com/problems/kth-ancestor-of-a-tree-node/" rel="noopener noreferrer"&gt;LeetCode&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lowest Common Ancestor of a Binary Tree&lt;/strong&gt; — &lt;a href="https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/" rel="noopener noreferrer"&gt;LeetCode&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Number of Ways to Assign Edge Weights II&lt;/strong&gt; — &lt;a href="https://leetcode.com/problems/number-of-ways-to-assign-edge-weights-ii/description/" rel="noopener noreferrer"&gt;LeetCode&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>algorithms</category>
      <category>computerscience</category>
      <category>programming</category>
    </item>
    <item>
      <title>Networking Essentials for Software Engineers – Part 1: Understanding the Basics</title>
      <dc:creator>Anusha Renangi</dc:creator>
      <pubDate>Sun, 01 Nov 2026 13:11:05 +0000</pubDate>
      <link>https://dev.to/anusha_renangi_05a4e41d35/networking-essentials-for-software-engineers-part-1-understanding-the-basics-fkf</link>
      <guid>https://dev.to/anusha_renangi_05a4e41d35/networking-essentials-for-software-engineers-part-1-understanding-the-basics-fkf</guid>
      <description>&lt;p&gt;As software engineers, we work with APIs, databases, servers, and web applications almost every day.&lt;/p&gt;

&lt;p&gt;When we make an API request, our application communicates with a server to get a response. Similarly, when we connect to a database, it might be running on a completely different machine.&lt;/p&gt;

&lt;p&gt;All these communications happen through a network.&lt;/p&gt;

&lt;p&gt;We often come across terms like IP addresses, ports, HTTP, TCP, and routers etc... while developing applications. Understanding these concepts helps us understand how our applications communicate with other systems.&lt;/p&gt;

&lt;p&gt;I often find myself searching Google to understand networking concepts and how they actually work.&lt;/p&gt;

&lt;p&gt;I wanted to explain these concepts in a simple way, so that others learning networking don't have to spend as much time searching for explanations as I did.&lt;/p&gt;

&lt;p&gt;So, I decided to start a series called &lt;strong&gt;Networking Essentials for Software Engineers&lt;/strong&gt;, where I'll share what I've learned in a simple and easy-to-understand way.&lt;/p&gt;

&lt;p&gt;In this first part, we'll cover some common networking terms and concepts.&lt;/p&gt;

&lt;h2&gt;
  
  
  Internet
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1pwskxkphyn7lu6wewnc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1pwskxkphyn7lu6wewnc.png" alt=" " width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we make a video call to a friend through WhatsApp, we can see and talk to each other even when we're in different locations.&lt;/p&gt;

&lt;p&gt;Our phone might be connected to a Wi-Fi network, while our friend's phone is connected to a mobile network.&lt;/p&gt;

&lt;p&gt;Even though we're connected to different networks, we can still communicate through the Internet.&lt;/p&gt;

&lt;p&gt;So, &lt;strong&gt;the Internet is simply a global network of interconnected computer networks that allows devices across the world to communicate with each other.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  A Little History
&lt;/h3&gt;

&lt;p&gt;The development of the Internet can be traced back to &lt;strong&gt;ARPANET (Advanced Research Projects Agency Network)&lt;/strong&gt;, a network developed with funding from the U.S. Advanced Research Projects Agency (ARPA).&lt;/p&gt;

&lt;p&gt;ARPANET initially connected computers at different research institutions, allowing them to communicate and share resources.&lt;/p&gt;

&lt;p&gt;Later, more independent networks were developed, and there was a need for a common way for these networks to communicate.&lt;/p&gt;

&lt;p&gt;This led to the development and adoption of &lt;strong&gt;TCP/IP (Transmission Control Protocol / Internet Protocol)&lt;/strong&gt;, a set of communication protocols that allows different networks to exchange data.&lt;/p&gt;

&lt;p&gt;In 1983, ARPANET switched to TCP/IP.&lt;/p&gt;

&lt;p&gt;Over time, more networks became interconnected, gradually forming the global Internet we use today.&lt;/p&gt;

&lt;h2&gt;
  
  
  World Wide Web (WWW)
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fl65geg6ytq34cld99n8t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fl65geg6ytq34cld99n8t.png" alt=" " width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we browse the Internet, we often move from one webpage to another by clicking on links.&lt;/p&gt;

&lt;p&gt;For example, while reading an article on Wikipedia, we might click on a link to open another related article.&lt;/p&gt;

&lt;p&gt;These links are called &lt;strong&gt;hyperlinks&lt;/strong&gt;, and they connect webpages and other web resources.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;World Wide Web (WWW)&lt;/strong&gt; is a system of interconnected webpages and resources that we can access through the Internet using a web browser.&lt;/p&gt;

&lt;p&gt;The Internet provides the network connectivity needed to access these resources, while the World Wide Web allows us to navigate between them through hyperlinks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Client–Server Architecture
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6w6ah75hw45lvcau11pj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6w6ah75hw45lvcau11pj.png" alt=" " width="799" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we open &lt;code&gt;google.com&lt;/code&gt; in a browser, the browser sends a request to Google's server, which processes it and sends back a response.&lt;/p&gt;

&lt;p&gt;This is called &lt;strong&gt;Client–Server Architecture&lt;/strong&gt;, where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Client:&lt;/strong&gt; An application or device that requests data or services.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Server:&lt;/strong&gt; An application or system that receives and processes requests and returns responses.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this example, the browser acts as the client, and Google's server handles the request.&lt;/p&gt;

&lt;p&gt;We use this same architecture when a frontend application communicates with a backend API.&lt;/p&gt;

&lt;h2&gt;
  
  
  Protocols
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ft2ne6woylnu2e23cjzli.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ft2ne6woylnu2e23cjzli.png" alt=" " width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In places like colleges and offices, we follow certain rules to maintain proper communication and coordination.&lt;/p&gt;

&lt;p&gt;Similarly, when computers communicate over a network, they need to follow a set of rules to exchange data.&lt;/p&gt;

&lt;p&gt;These rules are called &lt;strong&gt;protocols&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Some commonly used networking protocols are &lt;strong&gt;TCP (Transmission Control Protocol), UDP (User Datagram Protocol), HTTP (Hypertext Transfer Protocol)&lt;/strong&gt;, etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Is Data Transferred Over a Network?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjza1z95au5y9vsnccr5u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjza1z95au5y9vsnccr5u.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we send an image or a file to someone through WhatsApp, the entire file is not necessarily transferred as a single piece of data.&lt;/p&gt;

&lt;p&gt;Instead, the data is usually divided into smaller units called &lt;strong&gt;packets&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;These packets travel through the network to reach the destination, where the data is put back together so the receiver can access the original file.&lt;/p&gt;

&lt;p&gt;But how does the network know where to send these packets?&lt;/p&gt;

&lt;h3&gt;
  
  
  IP Address
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvcqixva9ws5281efr7ca.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvcqixva9ws5281efr7ca.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An &lt;strong&gt;IP (Internet Protocol) address&lt;/strong&gt; is a logical address assigned to a device's network interface, allowing data to be sent to the correct destination.&lt;/p&gt;

&lt;p&gt;For example, when our laptop is connected to home Wi-Fi, it usually gets a private IP address from the router.&lt;/p&gt;

&lt;p&gt;But multiple devices connected to the same Wi-Fi network can access the Internet through a single public IP address.&lt;/p&gt;

&lt;p&gt;How is this possible?&lt;/p&gt;

&lt;h3&gt;
  
  
  Private and Public IP Addresses
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flgtrdwlimbkeb6s6fggb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flgtrdwlimbkeb6s6fggb.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our home router connects devices like laptops, mobiles, and TVs to the Internet.&lt;/p&gt;

&lt;p&gt;Each device has its own &lt;strong&gt;private IP address&lt;/strong&gt; within the local network, while the router typically uses a &lt;strong&gt;public IP address&lt;/strong&gt; for communication over the Internet.&lt;/p&gt;

&lt;p&gt;When our laptop sends a request to a website, the router usually translates the private IP address into its public IP address using &lt;strong&gt;NAT (Network Address Translation)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When the response comes back, the router uses its translation information to forward the data to the correct device.&lt;/p&gt;

&lt;h3&gt;
  
  
  Ports
&lt;/h3&gt;

&lt;p&gt;Our laptop might be running multiple applications at the same time, such as Chrome, WhatsApp, and a game.&lt;/p&gt;

&lt;p&gt;The IP address helps deliver data to the correct device, but how does the operating system know which application should receive it?&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;port&lt;/strong&gt; is a number used by transport protocols such as TCP and UDP to identify communication endpoints.&lt;/p&gt;

&lt;p&gt;Port numbers are 16-bit values, ranging from &lt;strong&gt;0 to 65535&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Some commonly used ports are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;80:&lt;/strong&gt; HTTP&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;443:&lt;/strong&gt; HTTPS&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;27017:&lt;/strong&gt; MongoDB&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;3306:&lt;/strong&gt; MySQL&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Port numbers are divided into three categories:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Well-known ports (0–1023):&lt;/strong&gt; Commonly used by standard services. On Linux, normal users generally cannot bind applications to these ports without additional permissions and may receive a &lt;code&gt;Permission denied&lt;/code&gt; error.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Registered ports (1024–49151):&lt;/strong&gt; Used by registered applications and services. We can also use available ports in this range for our applications.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dynamic or private ports (49152–65535):&lt;/strong&gt; Commonly used for temporary connections. For example, when a browser connects to a web server, the operating system can assign a temporary source port. The actual automatic port range depends on the operating system's configuration.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An IP address helps identify the destination device, while a port number helps identify the communication endpoint on that device.&lt;/p&gt;

&lt;h3&gt;
  
  
  Internet Speed
&lt;/h3&gt;

&lt;p&gt;Internet speed is commonly measured in &lt;strong&gt;bits per second (bps)&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;1 Kbps&lt;/strong&gt; = 1,000 bits per second&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;1 Mbps&lt;/strong&gt; = 1,000,000 bits per second&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;1 Gbps&lt;/strong&gt; = 1,000,000,000 bits per second&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How Does Communication Between Computers Happen?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk6udasyatsgl8hmocb8m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk6udasyatsgl8hmocb8m.png" alt=" " width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Computers can communicate with each other through two types of transmission media:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Guided (Wired) Communication&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In guided communication, data travels through a physical medium, such as a cable, connecting the devices.&lt;/p&gt;

&lt;p&gt;For example, two computers connected using an Ethernet cable can exchange data through that connection.&lt;/p&gt;

&lt;p&gt;Some commonly used wired media are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Twisted-pair cables&lt;/li&gt;
&lt;li&gt;Coaxial cables&lt;/li&gt;
&lt;li&gt;Optical fiber cables&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Unguided (Wireless) Communication&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In unguided communication, data travels through electromagnetic waves without requiring a physical cable between the devices.&lt;/p&gt;

&lt;p&gt;For example, when we connect our laptop to Wi-Fi or transfer files using Bluetooth, we're using wireless communication.&lt;/p&gt;

&lt;p&gt;Some commonly used wireless technologies are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Wi-Fi&lt;/li&gt;
&lt;li&gt;Bluetooth&lt;/li&gt;
&lt;li&gt;Mobile networks (3G, 4G, 5G)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Modem
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fs790hy1si18bf8hrspz5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fs790hy1si18bf8hrspz5.png" alt=" " width="800" height="267"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Computers work with digital data (0s and 1s). But communication mediums like traditional telephone lines carry analog signals.&lt;/p&gt;

&lt;p&gt;To transmit data through these lines, we need a device that converts digital data into signals suitable for transmission.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;modem (Modulator–Demodulator)&lt;/strong&gt; performs this conversion in two ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Modulation:&lt;/strong&gt; Converts digital data into signals suitable for transmission over the communication medium.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Demodulation:&lt;/strong&gt; Converts the received signals back into digital data that the computer can process.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, when our computer sends data through a traditional telephone line, the modem modulates the data into an analog signal. At the receiving end, another modem demodulates that signal to recover the digital data.&lt;/p&gt;

&lt;p&gt;Modern fiber and cable connections use different signaling technologies, but the purpose remains the same: enabling data transmission over the communication medium.&lt;/p&gt;

&lt;h3&gt;
  
  
  How Are We Connected to the Internet?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsadrw89norz44vv93ofg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsadrw89norz44vv93ofg.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At home, we connect our laptops and mobile phones to Wi-Fi to access the Internet.&lt;/p&gt;

&lt;p&gt;But who provides this Internet connection?&lt;/p&gt;

&lt;p&gt;Companies like &lt;strong&gt;Jio, Airtel, and BSNL&lt;/strong&gt; provide Internet connectivity. These companies are called &lt;strong&gt;Internet Service Providers (ISPs)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Our home router connects to the ISP's network, allowing our devices to access the Internet.&lt;/p&gt;

&lt;p&gt;But our ISP isn't directly connected to every server or website in the world. It communicates with other networks to exchange data.&lt;/p&gt;

&lt;p&gt;For example, our Internet connection might be provided by Jio, but the website we're accessing could be hosted on a network operated by another company.&lt;/p&gt;

&lt;p&gt;The data can travel through multiple interconnected networks before reaching the destination.&lt;/p&gt;

&lt;h3&gt;
  
  
  How Are Different Countries Connected?
&lt;/h3&gt;

&lt;p&gt;Networks across countries and continents are connected through communication infrastructure, including &lt;strong&gt;submarine optical fiber cables&lt;/strong&gt; laid under the oceans.&lt;/p&gt;

&lt;p&gt;Companies such as &lt;strong&gt;Tata Communications&lt;/strong&gt; operate international network infrastructure, including submarine cable systems.&lt;/p&gt;

&lt;p&gt;ISPs can connect directly with other networks or use larger network providers to reach networks in different regions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Types of ISPs
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsgo077n557hsmh177u5v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsgo077n557hsmh177u5v.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;ISPs are commonly grouped into three tiers based on how they connect to other networks.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Tier 1:&lt;/strong&gt; Large global networks that connect different countries and continents, often through submarine fiber cables. They exchange traffic with other Tier 1 networks without paying for Internet transit. Examples include &lt;strong&gt;Tata Communications and AT&amp;amp;T&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Tier 2:&lt;/strong&gt; These providers mainly operate at regional or national levels. They connect with other networks and may purchase connectivity from larger providers. Examples include &lt;strong&gt;Airtel, Jio, and BSNL&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Tier 3:&lt;/strong&gt; These are typically local Internet providers that purchase connectivity from larger ISPs and provide Internet access to homes and businesses. Examples include &lt;strong&gt;SITI Cable and Spectra&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Types of Networks
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fogaym0td94nn2smb5izo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fogaym0td94nn2smb5izo.png" alt=" " width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Networks can be classified based on the geographical area they cover.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. LAN (Local Area Network)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A LAN connects devices within a small area, such as a home, office, or college.&lt;/p&gt;

&lt;p&gt;For example, computers connected through the same Wi-Fi router at home are usually part of a LAN.&lt;/p&gt;

&lt;p&gt;LANs can use Ethernet cables or Wi-Fi to connect devices.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. MAN (Metropolitan Area Network)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A MAN connects networks across a city or metropolitan area.&lt;/p&gt;

&lt;p&gt;For example, &lt;strong&gt;cable TV networks&lt;/strong&gt; that connect different areas within a city can be considered a MAN.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. WAN (Wide Area Network)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A WAN connects networks over large geographical distances, such as between cities, countries, or continents.&lt;/p&gt;

&lt;p&gt;For example, &lt;strong&gt;cellular networks (3G, 4G, and 5G)&lt;/strong&gt; can operate as WANs, connecting mobile devices across large geographical areas through interconnected network infrastructure.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Internet is the largest example of a WAN&lt;/strong&gt;, connecting networks across the world.&lt;/p&gt;

&lt;h3&gt;
  
  
  Network Topologies
&lt;/h3&gt;

&lt;p&gt;When multiple computers are connected in a network, they can be arranged and connected in different ways.&lt;/p&gt;

&lt;p&gt;The arrangement of devices and their connections in a network is called &lt;strong&gt;Network Topology&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;There are five common types:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Bus Topology&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2zhje6sqbyhza3gvzttu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2zhje6sqbyhza3gvzttu.png" alt=" " width="520" height="495"&gt;&lt;/a&gt;&lt;br&gt;
All devices are connected to a single main cable called the backbone.&lt;/p&gt;

&lt;p&gt;Only one device can successfully transmit at a time on a traditional shared bus network. If the main cable breaks, communication across the network can be disrupted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Ring Topology&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fayx5ulhir11kkiti8p7f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fayx5ulhir11kkiti8p7f.png" alt=" " width="510" height="495"&gt;&lt;/a&gt;&lt;br&gt;
Devices are connected in a circular arrangement, with each device connected to two neighboring devices.&lt;/p&gt;

&lt;p&gt;Data travels through the ring to reach its destination. In a basic single-ring setup, a cable or device failure can interrupt communication.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Star Topology&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fz6dn21o3j8o2ym3i8vwt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fz6dn21o3j8o2ym3i8vwt.png" alt=" " width="516" height="495"&gt;&lt;/a&gt;&lt;br&gt;
All devices are connected to a central device, usually a switch.&lt;/p&gt;

&lt;p&gt;If one device's cable fails, the other devices can still communicate. However, if the central switch fails, communication between connected devices is disrupted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Tree Topology&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1fyqjab1j1jfh32jgttx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1fyqjab1j1jfh32jgttx.png" alt=" " width="745" height="494"&gt;&lt;/a&gt;&lt;br&gt;
Devices are connected in a hierarchical structure, often using multiple interconnected switches.&lt;/p&gt;

&lt;p&gt;It combines smaller star networks into a larger structure. A failure in an upstream connection can affect the devices connected below it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Mesh Topology&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4h2ioulcginx9kkp10lz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4h2ioulcginx9kkp10lz.png" alt=" " width="720" height="494"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Devices are connected through multiple paths, allowing data to take alternative routes.&lt;/p&gt;

&lt;p&gt;In a &lt;strong&gt;full mesh&lt;/strong&gt;, every device is directly connected to every other device. In a &lt;strong&gt;partial mesh&lt;/strong&gt;, only some devices have multiple connections.&lt;/p&gt;

&lt;p&gt;Mesh topology provides redundancy, but requires more connections and configuration.&lt;/p&gt;

&lt;p&gt;I hope this article helped you understand the basics of networking. I've tried my best to explain everything in a simple way.&lt;/p&gt;

&lt;p&gt;If I've made any mistakes or missed something, please feel free to correct me in the comments.&lt;/p&gt;

&lt;p&gt;In the next part, we'll explore the structure of network.&lt;/p&gt;

</description>
      <category>networking</category>
      <category>computerscience</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Database Indexing – Part 6: How Does PostgreSQL Decide Which Index to Use?</title>
      <dc:creator>Anusha Renangi</dc:creator>
      <pubDate>Thu, 29 Oct 2026 01:13:32 +0000</pubDate>
      <link>https://dev.to/anusha_renangi_05a4e41d35/database-indexing-part-6-how-does-postgresql-decide-which-index-to-use-43p7</link>
      <guid>https://dev.to/anusha_renangi_05a4e41d35/database-indexing-part-6-how-does-postgresql-decide-which-index-to-use-43p7</guid>
      <description>&lt;p&gt;In my previous article, we explored Sequential Scan, Index Scan, and Bitmap Index Scan in PostgreSQL.&lt;/p&gt;

&lt;p&gt;We also saw that PostgreSQL doesn't always use an index, even when one is available.&lt;/p&gt;

&lt;p&gt;But how does PostgreSQL decide which execution plan to use?&lt;/p&gt;

&lt;p&gt;It can use a single index, combine multiple indexes, or scan the entire table.&lt;/p&gt;

&lt;p&gt;In this article, we'll understand how PostgreSQL makes these decisions using practical examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Query Planner Decisions with an Example
&lt;/h2&gt;

&lt;p&gt;Let's create a table with two columns, &lt;code&gt;f1&lt;/code&gt; and &lt;code&gt;f2&lt;/code&gt;, and separate indexes on both.&lt;/p&gt;

&lt;p&gt;We'll use this table to understand when PostgreSQL chooses to use both indexes, only one index, or no index.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Creating the Table
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;planner_test&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="n"&gt;BIGSERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;f1&lt;/span&gt; &lt;span class="nb"&gt;INTEGER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;f2&lt;/span&gt; &lt;span class="nb"&gt;INTEGER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;record_name&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Inserting 1 Million Records
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;planner_test&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;record_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;999&lt;/span&gt;&lt;span class="p"&gt;)::&lt;/span&gt;&lt;span class="nb"&gt;INTEGER&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;999&lt;/span&gt;&lt;span class="p"&gt;)::&lt;/span&gt;&lt;span class="nb"&gt;INTEGER&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'Record_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="k"&gt;g&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;generate_series&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;f1&lt;/code&gt; and &lt;code&gt;f2&lt;/code&gt; contain randomly generated values from approximately 1 to 1,000.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Creating the Indexes
&lt;/h3&gt;

&lt;p&gt;Let's create separate indexes on both columns.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_planner_f1&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;planner_test&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_planner_f2&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;planner_test&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since &lt;code&gt;id&lt;/code&gt; is the primary key, PostgreSQL also automatically creates an index on it.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Updating Table Statistics
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt; &lt;span class="n"&gt;planner_test&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This updates PostgreSQL's statistics about the table and its data distribution, helping the query planner estimate the cost of different execution plans.&lt;/p&gt;

&lt;p&gt;Now, we'll run queries with different filtering conditions and observe how PostgreSQL decides which indexes to use.&lt;/p&gt;

&lt;h3&gt;
  
  
  Case 1: Using Both Indexes
&lt;/h3&gt;

&lt;p&gt;Let's execute a query with two filtering conditions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;record_name&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;planner_test&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;f1&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;f2&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution Plan:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5aotxfcjkzzwq1i16sp1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5aotxfcjkzzwq1i16sp1.png" alt=" " width="800" height="230"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PostgreSQL chose to use both indexes and combine their results using &lt;strong&gt;BitmapAnd&lt;/strong&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The index on &lt;code&gt;f1&lt;/code&gt; identified &lt;strong&gt;199,631&lt;/strong&gt; matching records.&lt;/li&gt;
&lt;li&gt;The index on &lt;code&gt;f2&lt;/code&gt; identified &lt;strong&gt;200,182&lt;/strong&gt; matching records.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;BitmapAnd&lt;/code&gt; combined both bitmaps, keeping only the row locations that matched both conditions.&lt;/li&gt;
&lt;li&gt;The Bitmap Heap Scan retrieved &lt;strong&gt;39,668&lt;/strong&gt; records from the table.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Each condition matched around 200,000 records individually, but only around 40,000 records satisfied both conditions.&lt;/p&gt;

&lt;p&gt;PostgreSQL estimated that combining both indexes would be cheaper than using one index and checking the other condition on the retrieved rows.&lt;/p&gt;

&lt;h3&gt;
  
  
  Case 2: Using Only One Index
&lt;/h3&gt;

&lt;p&gt;Now, let's execute a query where one condition matches very few records.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;record_name&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;planner_test&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;f1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;f2&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution Plan:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fiw0xq9som2jjsnbenjxr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fiw0xq9som2jjsnbenjxr.png" alt=" " width="800" height="167"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PostgreSQL chose to use only the index on &lt;code&gt;f1&lt;/code&gt;, followed by a Bitmap Heap Scan.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The Bitmap Index Scan on &lt;code&gt;idx_planner_f1&lt;/code&gt; identified &lt;strong&gt;487&lt;/strong&gt; records matching &lt;code&gt;f1 = 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The Bitmap Heap Scan fetched those records from the table.&lt;/li&gt;
&lt;li&gt;PostgreSQL checked the remaining condition, &lt;code&gt;f2 BETWEEN 1 AND 200&lt;/code&gt;, and removed &lt;strong&gt;399&lt;/strong&gt; records.&lt;/li&gt;
&lt;li&gt;Finally, &lt;strong&gt;88 records&lt;/strong&gt; were returned.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Since the first index identified only 487 records, PostgreSQL estimated that checking the second condition on those rows would be cheaper than scanning another index and combining both results using &lt;code&gt;BitmapAnd&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Case 3: Without Using Any Index
&lt;/h3&gt;

&lt;p&gt;Now, let's execute a query where both conditions match a large number of records.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;record_name&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;planner_test&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;f1&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="mi"&gt;900&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;f2&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="mi"&gt;900&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution Plan:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fs597fskkktg7dx8a6k9d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fs597fskkktg7dx8a6k9d.png" alt=" " width="799" height="132"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PostgreSQL chose a &lt;strong&gt;Sequential Scan&lt;/strong&gt;, even though separate indexes are available on both &lt;code&gt;f1&lt;/code&gt; and &lt;code&gt;f2&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;PostgreSQL scanned the entire &lt;code&gt;planner_test&lt;/code&gt; table.&lt;/li&gt;
&lt;li&gt;It checked both filtering conditions on each record.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;189,044 records&lt;/strong&gt; were removed because they didn't satisfy both conditions.&lt;/li&gt;
&lt;li&gt;Finally, &lt;strong&gt;810,956 records&lt;/strong&gt; were returned.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The query returned more than 80% of the table's records.&lt;/p&gt;

&lt;p&gt;Using indexes would require PostgreSQL to scan the indexes and fetch a large number of records from the heap.&lt;/p&gt;

&lt;p&gt;PostgreSQL estimated that scanning the table directly would be cheaper than using either index or combining both indexes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Importance of Table Statistics
&lt;/h2&gt;

&lt;p&gt;PostgreSQL uses table statistics to estimate how many records might match a query and choose an execution plan with the lowest estimated cost.&lt;/p&gt;

&lt;p&gt;After bulk INSERT, UPDATE, or DELETE operations, these statistics may become outdated. PostgreSQL might then choose a less efficient execution plan.&lt;/p&gt;

&lt;p&gt;We can run &lt;code&gt;VACUUM (ANALYZE)&lt;/code&gt; to clean up the table and update its statistics.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;VACUUM&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;VACUUM:&lt;/strong&gt; Cleans up dead tuples created by UPDATE and DELETE operations, making the space available for reuse.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ANALYZE:&lt;/strong&gt; Updates table statistics so PostgreSQL can make better decisions when choosing an execution plan.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PostgreSQL also performs these operations automatically, but they may not run immediately after bulk data changes.&lt;/p&gt;

&lt;p&gt;If we've only inserted new records, running &lt;code&gt;ANALYZE&lt;/code&gt; alone is generally sufficient to update the statistics.&lt;/p&gt;

&lt;p&gt;We can also give hints to PostgreSQL to influence which indexes it uses. &lt;/p&gt;

&lt;p&gt;We'll explore how these hints work in the next article.&lt;/p&gt;

</description>
      <category>database</category>
      <category>postgres</category>
      <category>performance</category>
    </item>
    <item>
      <title>Database Indexing – Part 5: Sequential Scan vs. Index Scan vs. Bitmap Index Scan in PostgreSQL</title>
      <dc:creator>Anusha Renangi</dc:creator>
      <pubDate>Sun, 25 Oct 2026 14:23:19 +0000</pubDate>
      <link>https://dev.to/anusha_renangi_05a4e41d35/database-indexing-part-5-sequential-scan-vs-index-scan-vs-bitmap-index-scan-in-postgresql-e3m</link>
      <guid>https://dev.to/anusha_renangi_05a4e41d35/database-indexing-part-5-sequential-scan-vs-index-scan-vs-bitmap-index-scan-in-postgresql-e3m</guid>
      <description>&lt;p&gt;In my previous articles, we explored different types of indexes in PostgreSQL.&lt;/p&gt;

&lt;p&gt;But creating an index doesn't mean PostgreSQL will always use it.&lt;/p&gt;

&lt;p&gt;Depending on the query and the number of matching records, PostgreSQL may choose a Sequential Scan, an Index Scan, or a Bitmap Index Scan.&lt;/p&gt;

&lt;p&gt;In this article, we'll understand how these scan methods work using practical examples and PostgreSQL execution plans.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Scan Methods with an Example
&lt;/h2&gt;

&lt;p&gt;Let's use a &lt;code&gt;students&lt;/code&gt; table to understand when PostgreSQL chooses a Sequential Scan, Index Scan, or Bitmap Index Scan.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Creating the Table
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="n"&gt;BIGSERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;grades&lt;/span&gt; &lt;span class="nb"&gt;INTEGER&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since &lt;code&gt;id&lt;/code&gt; is the primary key, PostgreSQL automatically creates an index on it.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Inserting Records
&lt;/h3&gt;

&lt;p&gt;Let's insert 1 million student records.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;grades&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="s1"&gt;'Student_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;g&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;generate_series&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;grades&lt;/code&gt; column contains values from 1 to 100.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Creating an Index on Grades
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_students_grades&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;grades&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we have two indexes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Primary key index:&lt;/strong&gt; On &lt;code&gt;id&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Separate index:&lt;/strong&gt; On &lt;code&gt;grades&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Updating Table Statistics
&lt;/h3&gt;

&lt;p&gt;Before running our experiments, let's understand why updating table statistics is important.&lt;/p&gt;

&lt;p&gt;PostgreSQL uses statistics to estimate how many records a query might return. Based on these estimates, it decides whether scanning the table or using an index would be more efficient.&lt;/p&gt;

&lt;p&gt;We can check the statistics using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;relname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;n_live_tup&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;n_mod_since_analyze&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;last_analyze&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;last_autoanalyze&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_stat_user_tables&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;relname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'students'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;n_live_tup&lt;/code&gt;: Estimated number of live records.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;n_mod_since_analyze&lt;/code&gt;: Estimated number of records modified since the last analysis.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;last_analyze&lt;/code&gt;: Last time statistics were updated manually.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;last_autoanalyze&lt;/code&gt;: Last time statistics were updated automatically.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Before ANALYZE:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwv8il51z1413q1jvpha4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwv8il51z1413q1jvpha4.png" alt=" " width="800" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see that &lt;code&gt;n_mod_since_analyze&lt;/code&gt; is &lt;strong&gt;1,000,000&lt;/strong&gt;, meaning PostgreSQL has recorded approximately 1 million modifications since the last analysis.&lt;/p&gt;

&lt;p&gt;Now, let's update the statistics manually.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;After ANALYZE:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6cd01zs0ykhzs4n3ckgi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6cd01zs0ykhzs4n3ckgi.png" alt=" " width="800" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After running &lt;code&gt;ANALYZE&lt;/code&gt;, we can observe that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;n_mod_since_analyze&lt;/code&gt; changed from &lt;strong&gt;1,000,000 to 0&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;last_analyze&lt;/code&gt; was updated with the execution time.&lt;/li&gt;
&lt;li&gt;PostgreSQL refreshed the statistics used for estimating query costs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PostgreSQL also performs this automatically through &lt;strong&gt;auto-analyze&lt;/strong&gt;. We ran it manually here to understand how the statistics change.&lt;/p&gt;

&lt;p&gt;Refer more &lt;a href="https://www.postgresql.org/docs/17/runtime-config-autovacuum.html?utm_source=chatgpt.com" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that our table and indexes are ready, let's run a few queries and observe which scan methods PostgreSQL chooses.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Index Scan
&lt;/h2&gt;

&lt;p&gt;Let's start with a query that retrieves a small number of records from our &lt;code&gt;students&lt;/code&gt; table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution Plan:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flbpqs44lio710ltwcwm3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flbpqs44lio710ltwcwm3.png" alt=" " width="798" height="163"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PostgreSQL chose an &lt;strong&gt;Index Scan&lt;/strong&gt; using &lt;code&gt;students_pkey&lt;/code&gt;, the primary key index on &lt;code&gt;id&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How Does an Index Scan Work?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;PostgreSQL searches the index to find entries where &lt;code&gt;id &amp;lt; 100&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Each matching index entry contains a reference to the corresponding row in the table (heap).&lt;/li&gt;
&lt;li&gt;PostgreSQL uses these references to fetch the rows from the heap and retrieve the &lt;code&gt;name&lt;/code&gt; values.&lt;/li&gt;
&lt;li&gt;This process continues until all matching records are retrieved.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Since we're selecting &lt;code&gt;name&lt;/code&gt;, which is not stored in the primary key index, PostgreSQL needs to visit the heap to retrieve it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Did PostgreSQL Choose an Index Scan?
&lt;/h3&gt;

&lt;p&gt;Our table contains 1 million records, but this query needs only 99.&lt;/p&gt;

&lt;p&gt;Instead of scanning the entire table, PostgreSQL uses the primary key index to locate the matching records.&lt;/p&gt;

&lt;p&gt;An &lt;strong&gt;Index Scan is generally useful when a query retrieves a small number of records&lt;/strong&gt;, especially when the required rows can be found through an index without scanning most of the table.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Sequential Scan
&lt;/h2&gt;

&lt;p&gt;Now, let's change the condition to retrieve most of the records from our &lt;code&gt;students&lt;/code&gt; table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution Plan:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhl1saebqv539yq8bxois.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhl1saebqv539yq8bxois.png" alt=" " width="800" height="171"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This time, PostgreSQL chose a &lt;strong&gt;Sequential Scan (Seq Scan)&lt;/strong&gt;, even though we already have an index on &lt;code&gt;id&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How Does a Sequential Scan Work?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;PostgreSQL reads the table pages sequentially.&lt;/li&gt;
&lt;li&gt;It checks each row against the condition &lt;code&gt;id &amp;gt; 100&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If the row matches, PostgreSQL retrieves its &lt;code&gt;name&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;This continues until the entire table has been scanned.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Why Didn't PostgreSQL Use the Index?
&lt;/h3&gt;

&lt;p&gt;Our table contains 1 million records, and this query returns almost all of them.&lt;/p&gt;

&lt;p&gt;If PostgreSQL used an Index Scan, it would need to find the matching entries in the index and then visit the corresponding heap rows to retrieve their names.&lt;/p&gt;

&lt;p&gt;Since almost every row matches, repeatedly accessing the table through index entries would involve unnecessary work.&lt;/p&gt;

&lt;p&gt;Instead, PostgreSQL chose to scan the table directly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A Sequential Scan can be more efficient than an Index Scan when a query needs to retrieve a large portion of the table.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Bitmap Index Scan
&lt;/h2&gt;

&lt;p&gt;Now, let's filter students whose grades are greater than 95.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;grades&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;95&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution Plan:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2opjtcrgcwb6le5tarhv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2opjtcrgcwb6le5tarhv.png" alt=" " width="800" height="207"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PostgreSQL used a &lt;strong&gt;Bitmap Index Scan&lt;/strong&gt; on &lt;code&gt;idx_students_grades&lt;/code&gt;, followed by a &lt;strong&gt;Bitmap Heap Scan&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How Does a Bitmap Index Scan Work?
&lt;/h3&gt;

&lt;p&gt;In a regular Index Scan, PostgreSQL uses the index to locate matching rows and fetches them from the heap.&lt;/p&gt;

&lt;p&gt;In a Bitmap Index Scan, PostgreSQL first collects the locations of all matching rows into a bitmap. The Bitmap Heap Scan then uses this information to fetch the required heap pages.&lt;/p&gt;

&lt;p&gt;Let's take a small example.&lt;/p&gt;

&lt;p&gt;Each index entry contains a &lt;strong&gt;TID (Tuple Identifier)&lt;/strong&gt;, which identifies a row using its heap page number and tuple offset.&lt;/p&gt;

&lt;p&gt;Suppose the index finds these matching locations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Page 10 → Tuples [2, 5, 8]
Page 20 → Tuples [3, 7]
Page 30 → Tuple [4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PostgreSQL stores these locations in a bitmap and processes the heap pages in page order.&lt;/p&gt;

&lt;p&gt;When Page 10 is fetched into memory, the entire page (usually 8 KB) is loaded, including all the tuples stored on it.&lt;/p&gt;

&lt;p&gt;However, PostgreSQL already knows that only tuples 2, 5, and 8 matched the index condition. So it directly accesses those tuples, checks their visibility, and retrieves the required values.&lt;/p&gt;

&lt;p&gt;This is called an &lt;strong&gt;exact bitmap&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;PostgreSQL can also combine multiple indexes using &lt;strong&gt;BitmapAnd&lt;/strong&gt; and &lt;strong&gt;BitmapOr&lt;/strong&gt; when queries have multiple filtering conditions.&lt;/p&gt;

&lt;p&gt;I've explained both methods with practical examples in my previous article: &lt;a href="https://dev.to/anusha_renangi_05a4e41d35/database-indexing-part-3-understanding-composite-indexes-in-postgresql-2g5"&gt;Database Indexing – Part 3: Understanding Composite Indexes in PostgreSQL&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Did PostgreSQL Choose a Bitmap Index Scan?
&lt;/h3&gt;

&lt;p&gt;Our table contains 1 million records, and this query returns 50,000 records.&lt;/p&gt;

&lt;p&gt;Using a regular Index Scan could involve repeatedly accessing heap pages for individual matching rows.&lt;/p&gt;

&lt;p&gt;A Sequential Scan would need to scan the entire table.&lt;/p&gt;

&lt;p&gt;A Bitmap Index Scan allows PostgreSQL to collect the matching row locations first and then access the required heap pages in page order, reducing unnecessary page accesses.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding Recheck Cond and Lossy Bitmap Pages
&lt;/h3&gt;

&lt;p&gt;If we look at our execution plan, we can see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Recheck Cond: (grades &amp;gt; 95)
Heap Blocks: exact=7352
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;exact=7352&lt;/code&gt; means PostgreSQL had the exact matching tuple locations for all 7,352 heap pages.&lt;/p&gt;

&lt;p&gt;But storing all these tuple locations requires memory.&lt;/p&gt;

&lt;p&gt;When there isn't enough memory, PostgreSQL can convert some exact bitmap entries into &lt;strong&gt;lossy entries&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Instead of remembering the matching tuple offsets:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Page 10 → Tuples [2, 5, 8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It remembers only the page:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Page 10 → May contain matching tuples
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When PostgreSQL fetches this page, it has to examine the candidate tuples and check the condition &lt;code&gt;grades &amp;gt; 95&lt;/code&gt; again, since the bitmap no longer contains the exact matching offsets.&lt;/p&gt;

&lt;p&gt;This is where &lt;strong&gt;&lt;code&gt;Recheck Cond&lt;/code&gt;&lt;/strong&gt; is used.&lt;/p&gt;

&lt;p&gt;Let's try this practically by reducing &lt;code&gt;work_mem&lt;/code&gt; to 64 KB.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="k"&gt;LOCAL&lt;/span&gt; &lt;span class="n"&gt;work_mem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'64kB'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;grades&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;95&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;ROLLBACK&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution Plan:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb6en8yijur8p2vmfd2cf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb6en8yijur8p2vmfd2cf.png" alt=" " width="799" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This time, PostgreSQL used a Parallel Bitmap Heap Scan.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Recheck Cond: (grades &amp;gt; 95)
Rows Removed by Index Recheck: 288507
Heap Blocks: exact=209 lossy=1660
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;exact=209&lt;/code&gt;:&lt;/strong&gt; Heap-block accesses with exact tuple-location information.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;lossy=1660&lt;/code&gt;:&lt;/strong&gt; Heap-block accesses where only page-level information was available.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Rows Removed by Index Recheck: 288507&lt;/code&gt;:&lt;/strong&gt; Candidate rows that PostgreSQL rejected after evaluating the condition again.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since this was a parallel scan, the heap-block counts shown are not necessarily the total number of distinct pages processed by all workers.&lt;/p&gt;

&lt;p&gt;With sufficient memory, PostgreSQL can remember the exact matching tuple locations. When memory is limited, it may retain only page locations and recheck the filtering condition while scanning the heap.&lt;/p&gt;

&lt;p&gt;PostgreSQL doesn't always use an index just because one is available. It chooses a scan method based on what it estimates will be most efficient for the query.&lt;/p&gt;

&lt;p&gt;In the next article, we'll explore how PostgreSQL's query optimizer makes these decisions.&lt;/p&gt;

&lt;p&gt;Thanks for reading! I hope this article helped you understand how different scan methods work in PostgreSQL.&lt;/p&gt;

</description>
      <category>database</category>
      <category>performance</category>
      <category>postgres</category>
      <category>sql</category>
    </item>
    <item>
      <title>Database Indexing – Part 4: Covering vs. Composite Indexes in PostgreSQL – Which One Should You Use?</title>
      <dc:creator>Anusha Renangi</dc:creator>
      <pubDate>Fri, 23 Oct 2026 15:29:47 +0000</pubDate>
      <link>https://dev.to/anusha_renangi_05a4e41d35/database-indexing-part-4-covering-vs-composite-indexes-in-postgresql-which-one-should-you-use-5786</link>
      <guid>https://dev.to/anusha_renangi_05a4e41d35/database-indexing-part-4-covering-vs-composite-indexes-in-postgresql-which-one-should-you-use-5786</guid>
      <description>&lt;p&gt;In my previous articles, we explored &lt;a href="https://dev.to/anusha_renangi_05a4e41d35/database-indexing-part-2-understanding-covering-indexes-in-postgresql-17ee"&gt;covering indexes&lt;/a&gt; and &lt;a href="https://dev.to/anusha_renangi_05a4e41d35/database-indexing-part-3-understanding-composite-indexes-in-postgresql-2g5"&gt;composite indexes&lt;/a&gt; separately.&lt;/p&gt;

&lt;p&gt;Both indexes can improve query performance, but how do they differ in terms of storage, INSERT, UPDATE, and SELECT performance?&lt;/p&gt;

&lt;p&gt;To understand this, I tested both index types in PostgreSQL using two different datasets: one with unique values in the first indexed column and another with duplicate values.&lt;/p&gt;

&lt;p&gt;In this article, we'll compare the results of these experiments and understand when to use covering and composite indexes in real-world applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Experiment 1: Covering vs. Composite Indexes with Unique Key Values
&lt;/h2&gt;

&lt;p&gt;To compare covering and composite indexes, we'll perform a few experiments in PostgreSQL using &lt;strong&gt;10 million records&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We'll use two separate tables with identical structures and data, so we can test both index types independently.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Creating the Tables
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;documents_covering&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;document_id&lt;/span&gt; &lt;span class="n"&gt;BIGSERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;document_key&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;document_version&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;documents_composite&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;document_id&lt;/span&gt; &lt;span class="n"&gt;BIGSERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;document_key&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;document_version&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Inserting 10 Million Records
&lt;/h3&gt;

&lt;p&gt;First, let's populate the covering-index table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;documents_covering&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="s1"&gt;'DOC_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;LPAD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;96&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="s1"&gt;'VERSION_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;LPAD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;192&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;generate_series&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10000000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, copy the same records into the composite-index table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;documents_composite&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;documents_covering&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Creating the Indexes
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Covering Index:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_documents_covering&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;documents_covering&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;INCLUDE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_version&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Composite Index:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_documents_composite&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;documents_composite&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, update the table statistics.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt; &lt;span class="n"&gt;documents_covering&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ANALYZE&lt;/span&gt; &lt;span class="n"&gt;documents_composite&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both tables now contain the same 10 million records, with different index configurations.&lt;/p&gt;

&lt;p&gt;We'll compare their &lt;strong&gt;storage usage, SELECT, INSERT, and UPDATE performance&lt;/strong&gt; to understand how these indexes behave.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Storage Comparison
&lt;/h2&gt;

&lt;p&gt;Let's start by comparing how much storage each index occupies after indexing 10 million records.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 1: Comparing Index Sizes
&lt;/h3&gt;

&lt;p&gt;We can use PostgreSQL's &lt;code&gt;pg_relation_size()&lt;/code&gt; function to check the storage occupied by each index.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="s1"&gt;'Covering Index'&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;index_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;pg_size_pretty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pg_relation_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'idx_documents_covering'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;index_size&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;pg_relation_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'idx_documents_covering'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;size_bytes&lt;/span&gt;
&lt;span class="k"&gt;UNION&lt;/span&gt; &lt;span class="k"&gt;ALL&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="s1"&gt;'Composite Index'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;pg_size_pretty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pg_relation_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'idx_documents_composite'&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
    &lt;span class="n"&gt;pg_relation_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'idx_documents_composite'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Results:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fua948xcwex88gywpyp6q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fua948xcwex88gywpyp6q.png" alt=" " width="798" height="161"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Both indexes occupied exactly the same storage space: &lt;strong&gt;3,625 MB (3,801,243,648 bytes)&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 2: Comparing Index Pages
&lt;/h3&gt;

&lt;p&gt;Next, let's check the number of pages occupied by each index using PostgreSQL's &lt;code&gt;pg_class&lt;/code&gt; system catalog.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;relname&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;index_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;relpages&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;total_pages&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_class&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;relname&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s1"&gt;'idx_documents_covering'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'idx_documents_composite'&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Results:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5ecalgmkllju3j10g1nl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5ecalgmkllju3j10g1nl.png" alt=" " width="798" height="161"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Are Their Sizes the Same?
&lt;/h3&gt;

&lt;p&gt;PostgreSQL uses a B-tree structure that works similarly to a &lt;strong&gt;B+ tree&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In PostgreSQL, B-tree indexes contain two main types of pages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Leaf pages:&lt;/strong&gt; Store the actual index entries containing indexed values and references to table rows.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Internal pages:&lt;/strong&gt; Store separator keys and pointers that help PostgreSQL navigate to the correct leaf pages.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can read more about this in the &lt;a href="https://www.postgresql.org/docs/16/btree-implementation.html#BTREE-STRUCTURE" rel="noopener noreferrer"&gt;PostgreSQL B-tree Implementation documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;PostgreSQL also mentions that typically &lt;strong&gt;more than 99% of B-tree pages are leaf pages&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Both covering and composite indexes store their indexed column values in leaf entries. The difference is in how PostgreSQL handles those columns in internal pages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Covering Index&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In a covering index, additional columns are included using &lt;code&gt;INCLUDE&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;These columns are stored in leaf entries for data retrieval, but they are not needed for searching or navigating the B-tree.&lt;/p&gt;

&lt;p&gt;So, PostgreSQL completely excludes included columns from internal navigation entries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Composite Index&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In a composite index, all columns are key columns and can participate in searching and sorting.&lt;/p&gt;

&lt;p&gt;However, PostgreSQL has an optimization called &lt;strong&gt;suffix truncation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Suffix truncation removes unnecessary trailing key columns from internal B-tree entries.&lt;/p&gt;

&lt;p&gt;For example, if the first indexed column is enough to distinguish two page ranges, PostgreSQL doesn't need to store the second column in the corresponding internal separator.&lt;/p&gt;

&lt;p&gt;You can read about this in the &lt;a href="https://www.postgresql.org/docs/16/indexes-index-only-scans.html" rel="noopener noreferrer"&gt;PostgreSQL documentation on suffix truncation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In our case, &lt;code&gt;document_key&lt;/code&gt; contains unique values, so the second column is generally unnecessary for distinguishing page ranges.&lt;/p&gt;

&lt;p&gt;Since both indexes store the same column values in their leaf entries, and their internal entries can also be similarly compact, their storage requirements can be very close.&lt;/p&gt;

&lt;p&gt;In our experiment, both indexes occupied exactly &lt;strong&gt;3,625 MB across 464,019 pages&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. SELECT Performance Comparison
&lt;/h2&gt;

&lt;p&gt;Next, let's compare how covering and composite indexes perform when retrieving records.&lt;/p&gt;

&lt;p&gt;We'll select 6,500 records using &lt;code&gt;document_key&lt;/code&gt; and retrieve both &lt;code&gt;document_key&lt;/code&gt; and &lt;code&gt;document_version&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We'll execute each query twice to compare the initial run and the warm-cache run.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 1: Covering Index
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;document_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;documents_covering&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;document_key&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt;
    &lt;span class="s1"&gt;'DOC_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;LPAD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'100000'&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;96&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;AND&lt;/span&gt;
    &lt;span class="s1"&gt;'DOC_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;LPAD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'106499'&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;96&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Initial Run:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb455by6vlp3z6zhysk8u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb455by6vlp3z6zhysk8u.png" alt=" " width="798" height="161"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Warm-Cache Run:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6yndtpufun6e8o3b0rlo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6yndtpufun6e8o3b0rlo.png" alt=" " width="798" height="161"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 2: Composite Index
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;document_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;documents_composite&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;document_key&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt;
    &lt;span class="s1"&gt;'DOC_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;LPAD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'100000'&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;96&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;AND&lt;/span&gt;
    &lt;span class="s1"&gt;'DOC_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;LPAD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'106499'&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;96&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Initial Run:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fy8fvafobj40uvjd7zo1b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fy8fvafobj40uvjd7zo1b.png" alt=" " width="798" height="161"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Warm-Cache Run:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F97eh1px8lj8106qlgm2c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F97eh1px8lj8106qlgm2c.png" alt=" " width="798" height="161"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Results Comparison
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Metric&lt;/th&gt;
&lt;th&gt;Covering Index&lt;/th&gt;
&lt;th&gt;Composite Index&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Initial execution time&lt;/td&gt;
&lt;td&gt;25.255 ms&lt;/td&gt;
&lt;td&gt;29.351 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Warm-cache execution time&lt;/td&gt;
&lt;td&gt;26.493 ms&lt;/td&gt;
&lt;td&gt;24.660 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Rows returned&lt;/td&gt;
&lt;td&gt;6,500&lt;/td&gt;
&lt;td&gt;6,500&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Initial shared-buffer reads&lt;/td&gt;
&lt;td&gt;301&lt;/td&gt;
&lt;td&gt;301&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Warm-cache shared-buffer hits&lt;/td&gt;
&lt;td&gt;301&lt;/td&gt;
&lt;td&gt;301&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Heap fetches&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Observations
&lt;/h3&gt;

&lt;p&gt;Both indexes used an &lt;strong&gt;Index Only Scan&lt;/strong&gt;, meaning PostgreSQL retrieved the required column values directly from the indexes.&lt;/p&gt;

&lt;p&gt;Both also reported &lt;strong&gt;0 heap fetches&lt;/strong&gt;, so PostgreSQL didn't need to visit the table rows to check their visibility.&lt;/p&gt;

&lt;p&gt;During the initial runs, both indexes reported 301 shared-buffer reads. During the warm-cache runs, those became 301 shared-buffer hits, as the required index pages were already available in PostgreSQL's shared buffers.&lt;/p&gt;

&lt;p&gt;The execution times were nearly identical, with small variations between runs.&lt;/p&gt;

&lt;p&gt;For this SELECT query, &lt;strong&gt;neither index showed a clear performance advantage&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. INSERT Performance Comparison
&lt;/h2&gt;

&lt;p&gt;Next, let's compare INSERT performance between covering and composite indexes.&lt;/p&gt;

&lt;p&gt;Both tables already contain 10 million records. We'll insert another &lt;strong&gt;100,000 records&lt;/strong&gt; into each table and measure the execution time using &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 1: Covering Index
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;documents_covering&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'DOC_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;LPAD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;96&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="s1"&gt;'VERSION_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;LPAD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;192&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;generate_series&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10000001&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10100000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution Result:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7s4behbq45gtxzhuzkzx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7s4behbq45gtxzhuzkzx.png" alt=" " width="798" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Execution time: &lt;strong&gt;4,814.892 ms&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 2: Composite Index
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;documents_composite&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'DOC_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;LPAD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;96&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="s1"&gt;'VERSION_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;LPAD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;192&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;generate_series&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10000001&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10100000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution Result:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fe9q18hijzo9hkirl1hnq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fe9q18hijzo9hkirl1hnq.png" alt=" " width="798" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Execution time: &lt;strong&gt;4,681.164 ms&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Results Comparison
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Index Type&lt;/th&gt;
&lt;th&gt;Records Inserted&lt;/th&gt;
&lt;th&gt;Execution Time&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Covering Index&lt;/td&gt;
&lt;td&gt;100,000&lt;/td&gt;
&lt;td&gt;4,814.892 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Composite Index&lt;/td&gt;
&lt;td&gt;100,000&lt;/td&gt;
&lt;td&gt;4,681.164 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Observations
&lt;/h3&gt;

&lt;p&gt;Both indexes showed similar INSERT performance, with the composite index completing around &lt;strong&gt;134 ms faster&lt;/strong&gt; in this run.&lt;/p&gt;

&lt;p&gt;When inserting records, PostgreSQL needs to write the new rows to the table and create the corresponding entries in its indexes.&lt;/p&gt;

&lt;p&gt;Both index types store the same column values in their leaf entries, so the amount of index maintenance can be similar.&lt;/p&gt;

&lt;p&gt;These tests were performed after deleting and vacuuming previously inserted records, so existing free space and index maintenance may also affect the results.&lt;/p&gt;

&lt;p&gt;We didn't observe a significant performance difference between covering and composite indexes in this INSERT test.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. UPDATE Performance Comparison
&lt;/h2&gt;

&lt;p&gt;Next, let's compare how covering and composite indexes handle updates.&lt;/p&gt;

&lt;p&gt;We'll update &lt;code&gt;document_version&lt;/code&gt; for &lt;strong&gt;100,000 existing records&lt;/strong&gt; in each table.&lt;/p&gt;

&lt;p&gt;In the covering index, &lt;code&gt;document_version&lt;/code&gt; is an included column, while in the composite index, it's a key column.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 1: Covering Index
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;documents_covering&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'UPDATED_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;LPAD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;190&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;document_id&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="mi"&gt;100000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution Result:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fru1xh4mz2wkpmn6z8m98.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fru1xh4mz2wkpmn6z8m98.png" alt=" " width="798" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Execution time: &lt;strong&gt;5,165.132 ms&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 2: Composite Index
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;documents_composite&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'UPDATED_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;LPAD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;190&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;document_id&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="mi"&gt;100000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution Result:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fs6jacf2maum5fvba38nk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fs6jacf2maum5fvba38nk.png" alt=" " width="798" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Execution time: &lt;strong&gt;5,691.076 ms&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Results Comparison
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Index Type&lt;/th&gt;
&lt;th&gt;Records Updated&lt;/th&gt;
&lt;th&gt;Execution Time&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Covering Index&lt;/td&gt;
&lt;td&gt;100,000&lt;/td&gt;
&lt;td&gt;5,165.132 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Composite Index&lt;/td&gt;
&lt;td&gt;100,000&lt;/td&gt;
&lt;td&gt;5,691.076 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Observations
&lt;/h3&gt;

&lt;p&gt;Both indexes showed relatively similar UPDATE performance, with the covering index completing around &lt;strong&gt;526 ms faster&lt;/strong&gt; in this test.&lt;/p&gt;

&lt;p&gt;When an indexed column is updated, PostgreSQL needs to maintain the corresponding index entries.&lt;/p&gt;

&lt;p&gt;This applies to both index types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Covering Index:&lt;/strong&gt; Updating an included column requires index maintenance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Composite Index:&lt;/strong&gt; Updating a key column also requires index maintenance.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Normally, PostgreSQL can sometimes perform a &lt;strong&gt;HOT (Heap-Only Tuple) update&lt;/strong&gt;, avoiding the need to create new index entries.&lt;/p&gt;

&lt;p&gt;However, updating a column referenced by a regular B-tree index prevents HOT updates, whether the column is a key or an included column. You can read about this in the &lt;a href="https://www.postgresql.org/docs/current/storage-hot.html" rel="noopener noreferrer"&gt;PostgreSQL HOT Updates documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In our experiment, both indexes required index maintenance, and neither showed a clear, consistent performance advantage based on this single test.&lt;/p&gt;

&lt;h2&gt;
  
  
  Experiment 2: Covering vs. Composite Indexes with Duplicate Key Values
&lt;/h2&gt;

&lt;p&gt;In our first experiment, &lt;code&gt;document_key&lt;/code&gt; contained unique values, and both indexes showed almost identical storage usage and similar performance.&lt;/p&gt;

&lt;p&gt;Now, let's see what happens when the first indexed column contains duplicate values.&lt;/p&gt;

&lt;p&gt;We'll create two new tables with &lt;strong&gt;1 million records&lt;/strong&gt;, where each &lt;code&gt;document_key&lt;/code&gt; repeats 10,000 times, while &lt;code&gt;document_version&lt;/code&gt; remains unique.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting Up the Experiment
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. Creating the Tables&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;documents_duplicate_covering&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;document_id&lt;/span&gt; &lt;span class="nb"&gt;BIGINT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;document_key&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;document_version&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;documents_duplicate_composite&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;document_id&lt;/span&gt; &lt;span class="nb"&gt;BIGINT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;document_key&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;document_version&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Inserting 1 Million Records&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, insert the records into the covering-index table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;documents_duplicate_covering&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'DOC_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;LPAD&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="k"&gt;g&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)::&lt;/span&gt;&lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;96&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="s1"&gt;'VERSION_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;LPAD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;192&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;generate_series&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copy the same data into the composite-index table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;documents_duplicate_composite&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;documents_duplicate_covering&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Creating the Indexes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Covering Index:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_duplicate_covering&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;documents_duplicate_covering&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;INCLUDE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_version&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Composite Index:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_duplicate_composite&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;documents_duplicate_composite&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both tables now contain identical data, but their indexes handle &lt;code&gt;document_version&lt;/code&gt; differently.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Storage Comparison
&lt;/h3&gt;

&lt;p&gt;Let's first compare the index sizes after creating both indexes on 1 million records.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;indexrelid&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regclass&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;index_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;pg_size_pretty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pg_relation_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;indexrelid&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;index_size&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;pg_relation_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;indexrelid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;size_bytes&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_index&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;indexrelid&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s1"&gt;'idx_duplicate_covering'&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regclass&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'idx_duplicate_composite'&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regclass&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Results:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F90a0mf7ji67ckz3x3lgl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F90a0mf7ji67ckz3x3lgl.png" alt=" " width="800" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Index Type&lt;/th&gt;
&lt;th&gt;Index Size&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Covering Index&lt;/td&gt;
&lt;td&gt;363 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Composite Index&lt;/td&gt;
&lt;td&gt;376 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This time, the composite index occupied approximately &lt;strong&gt;13 MB more storage&lt;/strong&gt; than the covering index.&lt;/p&gt;

&lt;p&gt;Unlike our first experiment, the leading indexed column now contains duplicate values.&lt;/p&gt;

&lt;p&gt;In a covering index, PostgreSQL stores included columns only in leaf entries.&lt;/p&gt;

&lt;p&gt;In a composite index, trailing key columns may also be needed in internal B-tree entries to distinguish page ranges when the leading column contains duplicate values.&lt;/p&gt;

&lt;p&gt;Both indexes still store the same column values in their leaf entries, but their internal navigation entries can differ.&lt;/p&gt;

&lt;p&gt;In our experiment, the composite index required slightly more storage.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. SELECT Performance Comparison
&lt;/h3&gt;

&lt;p&gt;Next, let's retrieve records using a &lt;code&gt;document_key&lt;/code&gt; that appears multiple times.&lt;/p&gt;

&lt;p&gt;Before running the queries, we updated the table statistics and visibility information.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;VACUUM&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;documents_duplicate_covering&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;VACUUM&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;documents_duplicate_composite&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Query 1: Covering Index&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;document_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;documents_duplicate_covering&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;document_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'DOC_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;LPAD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'50'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;96&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Results:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk655fnm1yvs9owm4xitv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk655fnm1yvs9owm4xitv.png" alt=" " width="798" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Query 2: Composite Index&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;document_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;documents_duplicate_composite&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;document_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'DOC_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;LPAD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'50'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;96&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Results:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1a10g45a5tm7lymjkozw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1a10g45a5tm7lymjkozw.png" alt=" " width="798" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Metric&lt;/th&gt;
&lt;th&gt;Covering Index&lt;/th&gt;
&lt;th&gt;Composite Index&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Records returned&lt;/td&gt;
&lt;td&gt;10,000&lt;/td&gt;
&lt;td&gt;10,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Execution time&lt;/td&gt;
&lt;td&gt;2.846 ms&lt;/td&gt;
&lt;td&gt;2.626 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Scan type&lt;/td&gt;
&lt;td&gt;Index Only Scan&lt;/td&gt;
&lt;td&gt;Index Only Scan&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Heap fetches&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Shared-buffer reads&lt;/td&gt;
&lt;td&gt;458&lt;/td&gt;
&lt;td&gt;459&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Observations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Both indexes used an &lt;strong&gt;Index Only Scan&lt;/strong&gt;, returning 10,000 records without fetching any rows from the table heap.&lt;/p&gt;

&lt;p&gt;The execution times were almost identical, and both accessed approximately the same number of index pages.&lt;/p&gt;

&lt;p&gt;Even with duplicate values in the first indexed column, we didn't observe a noticeable SELECT performance difference.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. INSERT Performance Comparison
&lt;/h3&gt;

&lt;p&gt;Now, let's insert another &lt;strong&gt;100,000 records&lt;/strong&gt; into each table while keeping duplicate values in &lt;code&gt;document_key&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Query 1: Covering Index&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;documents_duplicate_covering&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'DOC_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;LPAD&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="k"&gt;g&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)::&lt;/span&gt;&lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;96&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="s1"&gt;'VERSION_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;LPAD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;192&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;generate_series&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000001&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1100000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Results:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgvvy0l714x18k4uprc0u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgvvy0l714x18k4uprc0u.png" alt=" " width="799" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Query 2: Composite Index&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;documents_duplicate_composite&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'DOC_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;LPAD&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="k"&gt;g&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)::&lt;/span&gt;&lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;96&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="s1"&gt;'VERSION_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;LPAD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;192&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;generate_series&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000001&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1100000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Results:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fh2a5b46hv1r5uo0hffr3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fh2a5b46hv1r5uo0hffr3.png" alt=" " width="799" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Metric&lt;/th&gt;
&lt;th&gt;Covering Index&lt;/th&gt;
&lt;th&gt;Composite Index&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Records inserted&lt;/td&gt;
&lt;td&gt;100,000&lt;/td&gt;
&lt;td&gt;100,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Execution time&lt;/td&gt;
&lt;td&gt;2,892.511 ms&lt;/td&gt;
&lt;td&gt;5,710.005 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Observations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This time, the covering index completed the INSERT in approximately &lt;strong&gt;2.89 seconds&lt;/strong&gt;, while the composite index took &lt;strong&gt;5.71 seconds&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The composite-index test also performed more buffer reads and writes.&lt;/p&gt;

&lt;p&gt;The difference was much larger than in our first experiment. However, these are single-run measurements, and cache conditions and disk activity can also affect the execution time.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. UPDATE Performance Comparison
&lt;/h3&gt;

&lt;p&gt;Finally, let's update &lt;code&gt;document_version&lt;/code&gt; for &lt;strong&gt;100,000 records&lt;/strong&gt; in both tables.&lt;/p&gt;

&lt;p&gt;Before the test, we ran:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;VACUUM&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;documents_duplicate_covering&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;VACUUM&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;documents_duplicate_composite&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Query 1: Covering Index&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;documents_duplicate_covering&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'UPDATED_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;LPAD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;190&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;document_id&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="mi"&gt;100000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Results:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fonu7z9d4mdoqh5u7vlgl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fonu7z9d4mdoqh5u7vlgl.png" alt=" " width="799" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Query 2: Composite Index&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;documents_duplicate_composite&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'UPDATED_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;LPAD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;190&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;document_id&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="mi"&gt;100000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Results:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fojj6paw6bp6n975ije21.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fojj6paw6bp6n975ije21.png" alt=" " width="799" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Metric&lt;/th&gt;
&lt;th&gt;Covering Index&lt;/th&gt;
&lt;th&gt;Composite Index&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Records updated&lt;/td&gt;
&lt;td&gt;100,000&lt;/td&gt;
&lt;td&gt;100,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Execution time&lt;/td&gt;
&lt;td&gt;3,288.368 ms&lt;/td&gt;
&lt;td&gt;4,598.804 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Scan type&lt;/td&gt;
&lt;td&gt;Sequential Scan&lt;/td&gt;
&lt;td&gt;Sequential Scan&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Observations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The covering index completed the UPDATE approximately &lt;strong&gt;1.31 seconds faster&lt;/strong&gt; than the composite index in this test.&lt;/p&gt;

&lt;p&gt;Both queries used a Sequential Scan because these new tables didn't have an index on &lt;code&gt;document_id&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Also, changing &lt;code&gt;document_version&lt;/code&gt; required index maintenance in both cases, whether it was an included column or a composite-index key column.&lt;/p&gt;

&lt;p&gt;The composite-index test recorded more buffer writes, which may have contributed to its longer execution time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Comparison of Both Experiments
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Experiment 1: Unique Values in the First Key Column
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;Covering Index&lt;/th&gt;
&lt;th&gt;Composite Index&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Storage (10 million records)&lt;/td&gt;
&lt;td&gt;3,625 MB&lt;/td&gt;
&lt;td&gt;3,625 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SELECT (6,500 records, warm cache)&lt;/td&gt;
&lt;td&gt;26.493 ms&lt;/td&gt;
&lt;td&gt;24.660 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;INSERT (100,000 records)&lt;/td&gt;
&lt;td&gt;4,814.892 ms&lt;/td&gt;
&lt;td&gt;4,681.164 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;UPDATE (100,000 records)&lt;/td&gt;
&lt;td&gt;5,165.132 ms&lt;/td&gt;
&lt;td&gt;5,691.076 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Experiment 2: Duplicate Values in the First Key Column
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;Covering Index&lt;/th&gt;
&lt;th&gt;Composite Index&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Storage (1 million records)&lt;/td&gt;
&lt;td&gt;363 MB&lt;/td&gt;
&lt;td&gt;376 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SELECT (10,000 records)&lt;/td&gt;
&lt;td&gt;2.846 ms&lt;/td&gt;
&lt;td&gt;2.626 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;INSERT (100,000 records)&lt;/td&gt;
&lt;td&gt;2,892.511 ms&lt;/td&gt;
&lt;td&gt;5,710.005 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;UPDATE (100,000 records)&lt;/td&gt;
&lt;td&gt;3,288.368 ms&lt;/td&gt;
&lt;td&gt;4,598.804 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In the first experiment, both indexes occupied the same storage space and showed similar SELECT, INSERT, and UPDATE performance.&lt;/p&gt;

&lt;p&gt;In the second experiment, where the first indexed column contained duplicate values, the composite index occupied more storage. The covering index also completed the INSERT and UPDATE operations faster in our tests.&lt;/p&gt;

&lt;p&gt;This shows why we should carefully decide which columns need to be index keys.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which One Should You Choose: Composite Index or Covering Index?
&lt;/h2&gt;

&lt;p&gt;In real-world applications, covering and composite indexes are not competing approaches. We can combine both in a single index.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Key columns:&lt;/strong&gt; Keep columns required for filtering, joining, or sorting as index keys.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Non-key columns:&lt;/strong&gt; Keep additional columns needed only for retrieving data in the &lt;code&gt;INCLUDE&lt;/code&gt; clause.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unnecessarily adding columns as index keys can waste storage, especially when the leading columns contain duplicate values.&lt;/p&gt;

&lt;p&gt;PostgreSQL may need those additional key columns in internal B-tree entries. But columns added using &lt;code&gt;INCLUDE&lt;/code&gt; are stored only in leaf entries, which can help reduce unnecessary storage.&lt;/p&gt;

&lt;p&gt;At the same time, we shouldn't add too many columns to &lt;code&gt;INCLUDE&lt;/code&gt;, because they still increase index size and require maintenance whenever their values change.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So, keep only the required columns as index keys, and use &lt;code&gt;INCLUDE&lt;/code&gt; for additional columns needed only for retrieval. Choose both carefully based on your actual query requirements.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading! I hope this article helped you better understand covering and composite indexes in PostgreSQL.&lt;/p&gt;

</description>
      <category>database</category>
      <category>performance</category>
      <category>postgres</category>
      <category>sql</category>
    </item>
    <item>
      <title>Database Indexing – Part 3: Understanding Composite Indexes in PostgreSQL</title>
      <dc:creator>Anusha Renangi</dc:creator>
      <pubDate>Wed, 21 Oct 2026 18:40:47 +0000</pubDate>
      <link>https://dev.to/anusha_renangi_05a4e41d35/database-indexing-part-3-understanding-composite-indexes-in-postgresql-2g5</link>
      <guid>https://dev.to/anusha_renangi_05a4e41d35/database-indexing-part-3-understanding-composite-indexes-in-postgresql-2g5</guid>
      <description>&lt;p&gt;In PostgreSQL, we often come across queries that filter records based on multiple conditions.&lt;/p&gt;

&lt;p&gt;Consider an &lt;code&gt;employees&lt;/code&gt; table containing information such as employee ID, name, department, and salary.&lt;/p&gt;

&lt;p&gt;Suppose we want to find employees working in the Engineering department who earn more than 50,000.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Engineering'&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;50000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This query looks simple, but what happens when the table contains millions of employee records?&lt;/p&gt;

&lt;p&gt;PostgreSQL needs to identify the rows that satisfy both conditions. As the amount of data grows, finding those records efficiently becomes more important.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How can we optimize queries that filter on multiple columns?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this article, we'll explore composite indexes in PostgreSQL and understand how they can help with such queries.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is a Composite Index?
&lt;/h2&gt;

&lt;p&gt;A composite index, also called a &lt;strong&gt;multicolumn index&lt;/strong&gt;, is an index created on two or more columns of a table.&lt;/p&gt;

&lt;p&gt;In PostgreSQL, we can create a composite index using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_employees_department_salary&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we're creating a single index on two columns: &lt;code&gt;department&lt;/code&gt; and &lt;code&gt;salary&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Unlike separate indexes on each column, a composite index stores the values together in a specific order.&lt;/p&gt;

&lt;p&gt;PostgreSQL organizes the index entries &lt;strong&gt;first by &lt;code&gt;department&lt;/code&gt;, and then by &lt;code&gt;salary&lt;/code&gt; within each department&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example, consider these employee records:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;employee_id&lt;/th&gt;
&lt;th&gt;department&lt;/th&gt;
&lt;th&gt;salary&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Engineering&lt;/td&gt;
&lt;td&gt;70000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;HR&lt;/td&gt;
&lt;td&gt;45000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Engineering&lt;/td&gt;
&lt;td&gt;50000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Sales&lt;/td&gt;
&lt;td&gt;60000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;HR&lt;/td&gt;
&lt;td&gt;55000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;Engineering&lt;/td&gt;
&lt;td&gt;90000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The composite index on &lt;code&gt;(department, salary)&lt;/code&gt; organizes its entries in this order:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;department&lt;/th&gt;
&lt;th&gt;salary&lt;/th&gt;
&lt;th&gt;Employee&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Engineering&lt;/td&gt;
&lt;td&gt;50000&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Engineering&lt;/td&gt;
&lt;td&gt;70000&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Engineering&lt;/td&gt;
&lt;td&gt;90000&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HR&lt;/td&gt;
&lt;td&gt;45000&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HR&lt;/td&gt;
&lt;td&gt;55000&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sales&lt;/td&gt;
&lt;td&gt;60000&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Notice that the employees are grouped by department. Within each department, their salaries are arranged in ascending order.&lt;/p&gt;

&lt;p&gt;This ordering is important because PostgreSQL can use it to narrow down the records it needs to examine.&lt;/p&gt;

&lt;p&gt;Also, &lt;strong&gt;column order matters&lt;/strong&gt;. An index on &lt;code&gt;(department, salary)&lt;/code&gt; is not organized in the same way as an index on &lt;code&gt;(salary, department)&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Index Behavior with an Example
&lt;/h2&gt;

&lt;p&gt;Let's use an &lt;code&gt;employees&lt;/code&gt; table to understand how PostgreSQL handles queries with multiple filtering conditions.&lt;/p&gt;

&lt;p&gt;First, we'll create the table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;employee_id&lt;/span&gt; &lt;span class="n"&gt;BIGSERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;employee_name&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;department&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="nb"&gt;INTEGER&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's insert 1 million employee records.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;employee_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="s1"&gt;'Employee_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ARRAY&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'Engineering'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'HR'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Sales'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Finance'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Marketing'&lt;/span&gt;&lt;span class="p"&gt;])[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;g&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)],&lt;/span&gt;
    &lt;span class="mi"&gt;30000&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;g&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;generate_series&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll compare three index configurations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Case 1:&lt;/strong&gt; Two separate indexes on &lt;code&gt;department&lt;/code&gt; and &lt;code&gt;salary&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Case 2:&lt;/strong&gt; A composite index on &lt;code&gt;(department, salary)&lt;/code&gt;, after removing the separate indexes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Case 3:&lt;/strong&gt; A composite index on &lt;code&gt;(department, salary)&lt;/code&gt; along with a separate index on &lt;code&gt;salary&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For each configuration, we'll execute the same four queries and observe the execution plans and performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Case 1: Creating Two Separate Indexes
&lt;/h2&gt;

&lt;p&gt;Let's start by creating two individual indexes on the &lt;code&gt;employees&lt;/code&gt; table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_employees_department&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_employees_salary&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's execute a few queries and see how PostgreSQL uses these indexes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 1: Filtering by Department
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;employee_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Engineering'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution Plan:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9hr4r1n550s1wve7xass.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9hr4r1n550s1wve7xass.png" alt=" " width="800" height="205"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PostgreSQL used a &lt;strong&gt;Bitmap Index Scan&lt;/strong&gt; on &lt;code&gt;idx_employees_department&lt;/code&gt;, followed by a &lt;strong&gt;Bitmap Heap Scan&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A Bitmap Index Scan first identifies the locations of matching records using the index. PostgreSQL then uses those locations to access the required heap pages through a Bitmap Heap Scan.&lt;/p&gt;

&lt;p&gt;Here, the query returned &lt;strong&gt;200,000 records&lt;/strong&gt; by accessing &lt;strong&gt;8,161 heap pages&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Execution Time: 123.040 ms&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 2: Filtering by Salary
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;employee_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;70000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution Plan:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqctjg8y0djbgj1szfupe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqctjg8y0djbgj1szfupe.png" alt=" " width="800" height="205"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This time, PostgreSQL used &lt;code&gt;idx_employees_salary&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The execution plan again contained a Bitmap Index Scan followed by a Bitmap Heap Scan.&lt;/p&gt;

&lt;p&gt;The query returned &lt;strong&gt;10,000 records&lt;/strong&gt;, accessing &lt;strong&gt;8,161 heap pages&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Execution Time: 130.689 ms&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 3: Filtering Using AND
&lt;/h3&gt;

&lt;p&gt;Now, let's filter employees using both &lt;code&gt;department&lt;/code&gt; and &lt;code&gt;salary&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;employee_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Engineering'&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;70000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution Plan:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6meva6d5c4srsirze5h4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6meva6d5c4srsirze5h4.png" alt=" " width="800" height="249"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PostgreSQL used two &lt;strong&gt;Bitmap Index Scans&lt;/strong&gt;, combined their results using &lt;strong&gt;BitmapAnd&lt;/strong&gt;, and then performed a &lt;strong&gt;Bitmap Heap Scan&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  How does BitmapAnd work?
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;PostgreSQL searches &lt;code&gt;idx_employees_department&lt;/code&gt; and identifies &lt;strong&gt;200,000 matching records&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;It searches &lt;code&gt;idx_employees_salary&lt;/code&gt; and identifies &lt;strong&gt;10,000 matching records&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;BitmapAnd&lt;/strong&gt; combines the two bitmaps, keeping only the row locations that satisfy both conditions.&lt;/li&gt;
&lt;li&gt;The Bitmap Heap Scan uses the resulting bitmap to fetch matching rows from the table.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example, suppose the two indexes identify these employee IDs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Department = Engineering → 1, 3, 5, 7
Salary = 70000          → 2, 3, 5, 8

BitmapAnd                 → 3, 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These IDs are just an example to illustrate the intersection. PostgreSQL's bitmaps represent heap tuple locations, not employee IDs directly.&lt;/p&gt;

&lt;p&gt;In our actual execution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Rows returned:&lt;/strong&gt; 10,000&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Heap pages accessed:&lt;/strong&gt; 8,161&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Execution time:&lt;/strong&gt; 117.610 ms&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notice that PostgreSQL scanned both indexes separately before combining their results. We will compare this behavior with a composite index in the next case.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 4: Filtering Using OR
&lt;/h3&gt;

&lt;p&gt;Now, let's find employees who belong to the Engineering department &lt;strong&gt;or&lt;/strong&gt; have a salary of 70,000.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;employee_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Engineering'&lt;/span&gt;
   &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;70000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution Plan:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7qqezp28k4cj7ns24fcz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7qqezp28k4cj7ns24fcz.png" alt=" " width="800" height="249"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PostgreSQL used two &lt;strong&gt;Bitmap Index Scans&lt;/strong&gt;, combined their results using &lt;strong&gt;BitmapOr&lt;/strong&gt;, and then performed a &lt;strong&gt;Bitmap Heap Scan&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  How does BitmapOr work?
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;PostgreSQL searches &lt;code&gt;idx_employees_department&lt;/code&gt; and identifies 200,000 matching records.&lt;/li&gt;
&lt;li&gt;It searches &lt;code&gt;idx_employees_salary&lt;/code&gt; and identifies 10,000 matching records.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;BitmapOr&lt;/strong&gt; combines the two bitmaps, keeping row locations that match either condition.&lt;/li&gt;
&lt;li&gt;The Bitmap Heap Scan visits the corresponding heap pages and retrieves the matching employee IDs.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Department = Engineering → 1, 3, 5, 7
Salary = 70000          → 2, 3, 5, 8

BitmapOr                  → 1, 2, 3, 5, 7, 8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PostgreSQL combines heap tuple locations rather than employee IDs directly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Actual Results:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rows returned: &lt;strong&gt;200,000&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Heap pages accessed: &lt;strong&gt;8,161&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Execution time: &lt;strong&gt;126.784 ms&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notice that the query returned 200,000 records, not 210,000. In our generated dataset, all employees earning 70,000 belong to the Engineering department, so those records already satisfy the first condition.&lt;/p&gt;

&lt;p&gt;PostgreSQL used both indexes and combined their results without returning duplicate rows.&lt;/p&gt;

&lt;h2&gt;
  
  
  Case 2: Creating a Composite Index
&lt;/h2&gt;

&lt;p&gt;Now, let's remove the two separate indexes and create a composite index on &lt;code&gt;department&lt;/code&gt; and &lt;code&gt;salary&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_employees_department&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_employees_salary&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create the composite index:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_employees_department_salary&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's run the same four queries and observe how PostgreSQL uses this index.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 1: Filtering by Department
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;employee_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Engineering'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution Plan:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftqaj1z255usxflwdgryc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftqaj1z255usxflwdgryc.png" alt=" " width="800" height="196"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PostgreSQL used a &lt;strong&gt;Bitmap Index Scan&lt;/strong&gt; on &lt;code&gt;idx_employees_department_salary&lt;/code&gt;, followed by a &lt;strong&gt;Bitmap Heap Scan&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here's how it works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;PostgreSQL searches the composite index using &lt;code&gt;department = 'Engineering'&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Since &lt;code&gt;department&lt;/code&gt; is the first column in the index, PostgreSQL can directly locate the matching index entries without requiring a condition on &lt;code&gt;salary&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The Bitmap Index Scan collects the matching heap tuple locations into a bitmap.&lt;/li&gt;
&lt;li&gt;The Bitmap Heap Scan visits the corresponding heap pages, checks tuple visibility, and retrieves the employee IDs. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Results:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rows returned: &lt;strong&gt;200,000&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Heap pages accessed: &lt;strong&gt;8,161&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Execution time: &lt;strong&gt;130.189 ms&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even though the index contains two columns, PostgreSQL was able to use it with a condition on the first column alone.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 2: Filtering by Salary
&lt;/h3&gt;

&lt;p&gt;Now, let's filter employees using only &lt;code&gt;salary&lt;/code&gt;, which is the second column in our composite index &lt;code&gt;(department, salary)&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;employee_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;70000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution Plan:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F39er6bqw60pmz20z15nl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F39er6bqw60pmz20z15nl.png" alt=" " width="800" height="172"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This time, PostgreSQL chose a &lt;strong&gt;Parallel Sequential Scan&lt;/strong&gt; instead of using the composite index.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Rows returned:&lt;/strong&gt; 10,000&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Heap pages read:&lt;/strong&gt; 8,161&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Workers launched:&lt;/strong&gt; 2&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Execution time:&lt;/strong&gt; 98.216 ms&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PostgreSQL scanned the table using three processes (the main process and two parallel workers), checking the salary condition against the rows.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why didn't PostgreSQL use the composite index?
&lt;/h4&gt;

&lt;p&gt;Our composite index is created on:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_employees_department_salary&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PostgreSQL organizes the index entries first by &lt;code&gt;department&lt;/code&gt;, then by &lt;code&gt;salary&lt;/code&gt; within each department.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Department&lt;/th&gt;
&lt;th&gt;Salary&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Engineering&lt;/td&gt;
&lt;td&gt;50000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Engineering&lt;/td&gt;
&lt;td&gt;70000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Engineering&lt;/td&gt;
&lt;td&gt;90000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Finance&lt;/td&gt;
&lt;td&gt;60000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Finance&lt;/td&gt;
&lt;td&gt;70000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HR&lt;/td&gt;
&lt;td&gt;45000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HR&lt;/td&gt;
&lt;td&gt;70000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sales&lt;/td&gt;
&lt;td&gt;55000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sales&lt;/td&gt;
&lt;td&gt;70000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Notice that employees earning 70,000 are spread across different departments in the index.&lt;/p&gt;

&lt;p&gt;When we filter by &lt;code&gt;department&lt;/code&gt;, PostgreSQL can directly locate the corresponding entries because it is the first indexed column.&lt;/p&gt;

&lt;p&gt;But when we filter only by &lt;code&gt;salary&lt;/code&gt;, the matching values are scattered across different departments. PostgreSQL cannot retrieve all matching entries through a single continuous range of the index.&lt;/p&gt;

&lt;p&gt;This behavior is commonly described by the &lt;strong&gt;leftmost prefix rule&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An index on &lt;code&gt;(department, salary)&lt;/code&gt; efficiently supports filtering by &lt;code&gt;department&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;It also supports filtering by both &lt;code&gt;department&lt;/code&gt; and &lt;code&gt;salary&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Filtering only by &lt;code&gt;salary&lt;/code&gt; is generally less efficient because the leading column is missing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PostgreSQL can sometimes use a composite index even without a condition on its first column, but whether that is worthwhile depends on the data and estimated query cost.&lt;/p&gt;

&lt;p&gt;In our execution, PostgreSQL chose a Parallel Sequential Scan rather than using the composite index.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 3: Filtering Using AND
&lt;/h3&gt;

&lt;p&gt;Now, let's filter employees using both &lt;code&gt;department&lt;/code&gt; and &lt;code&gt;salary&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;employee_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Engineering'&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;70000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution Plan:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftg5hsgdt0sio2us1791h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftg5hsgdt0sio2us1791h.png" alt=" " width="800" height="218"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PostgreSQL used a &lt;strong&gt;Bitmap Index Scan&lt;/strong&gt; on &lt;code&gt;idx_employees_department_salary&lt;/code&gt;, followed by a &lt;strong&gt;Bitmap Heap Scan&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Unlike Case 1, where PostgreSQL used two separate indexes and combined their results using &lt;code&gt;BitmapAnd&lt;/code&gt;, this time it used a single composite index.&lt;/p&gt;

&lt;p&gt;Here's how it works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;PostgreSQL searches the composite index for entries where &lt;code&gt;department = 'Engineering'&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Since salary values are ordered within each department, PostgreSQL can further narrow the search to entries where &lt;code&gt;salary = 70000&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The Bitmap Index Scan collects the matching heap tuple locations into a bitmap.&lt;/li&gt;
&lt;li&gt;The Bitmap Heap Scan visits the corresponding heap pages, checks tuple visibility, and retrieves the matching employee IDs.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Results:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rows returned: &lt;strong&gt;10,000&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Heap pages accessed: &lt;strong&gt;8,161&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Execution time: &lt;strong&gt;115.172 ms&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PostgreSQL was able to use both filtering conditions directly within a single composite index.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 4: Filtering Using OR
&lt;/h3&gt;

&lt;p&gt;Now, let's filter employees using either &lt;code&gt;department&lt;/code&gt; or &lt;code&gt;salary&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;employee_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Engineering'&lt;/span&gt;
   &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;70000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution Plan:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftya2qsznj8kqiy5eumlc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftya2qsznj8kqiy5eumlc.png" alt=" " width="800" height="187"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This time, PostgreSQL chose a &lt;strong&gt;Sequential Scan&lt;/strong&gt; instead of using the composite index.&lt;/p&gt;

&lt;p&gt;Here's why:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Our composite index is created on &lt;code&gt;(department, salary)&lt;/code&gt;, where &lt;code&gt;department&lt;/code&gt; is the first column.&lt;/li&gt;
&lt;li&gt;PostgreSQL can efficiently search using &lt;code&gt;department&lt;/code&gt;, but the &lt;code&gt;OR&lt;/code&gt; condition also requires finding employees with &lt;code&gt;salary = 70000&lt;/code&gt;, regardless of their department.&lt;/li&gt;
&lt;li&gt;Since &lt;code&gt;salary&lt;/code&gt; is the second column, its matching values are spread across different departments in the index.&lt;/li&gt;
&lt;li&gt;PostgreSQL estimated that scanning the table and checking both conditions would be cheaper than using the composite index.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Results:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rows returned: &lt;strong&gt;200,000&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Rows removed by filter: &lt;strong&gt;800,000&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Heap pages read: &lt;strong&gt;8,161&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Execution time: &lt;strong&gt;138.981 ms&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unlike Case 1, where PostgreSQL combined two separate indexes using &lt;code&gt;BitmapOr&lt;/code&gt;, here it performed a Sequential Scan because there was no separate index on &lt;code&gt;salary&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Case 3: Composite Index with a Separate Salary Index
&lt;/h2&gt;

&lt;p&gt;We have kept the composite index on &lt;code&gt;(department, salary)&lt;/code&gt; and created an additional index on &lt;code&gt;salary&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_employees_salary&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's run the same four queries again.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 1: Filtering by Department
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;employee_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Engineering'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution Plan:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fecjdtlndqga2c8dtcmaf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fecjdtlndqga2c8dtcmaf.png" alt=" " width="799" height="202"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PostgreSQL used a &lt;strong&gt;Bitmap Index Scan&lt;/strong&gt; on &lt;code&gt;idx_employees_department_salary&lt;/code&gt;, followed by a &lt;strong&gt;Bitmap Heap Scan&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here's how it works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;PostgreSQL searches the composite index using &lt;code&gt;department = 'Engineering'&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Since &lt;code&gt;department&lt;/code&gt; is the first column in the index, PostgreSQL can locate the matching entries without requiring a condition on &lt;code&gt;salary&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The Bitmap Index Scan collects the matching heap tuple locations.&lt;/li&gt;
&lt;li&gt;The Bitmap Heap Scan visits the corresponding heap pages, checks tuple visibility, and retrieves the employee IDs.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Results:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rows returned: &lt;strong&gt;200,000&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Heap pages accessed: &lt;strong&gt;8,161&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Execution time: &lt;strong&gt;127.859 ms&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PostgreSQL continued to use the composite index for this query. The separate &lt;code&gt;salary&lt;/code&gt; index was not needed because the query filtered only on &lt;code&gt;department&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 2: Filtering by Salary
&lt;/h3&gt;

&lt;p&gt;Now, let's filter employees using only &lt;code&gt;salary&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;employee_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;70000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution Plan:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmn49x1vb0jcfawjah9pm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmn49x1vb0jcfawjah9pm.png" alt=" " width="799" height="205"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PostgreSQL used a &lt;strong&gt;Bitmap Index Scan&lt;/strong&gt; on &lt;code&gt;idx_employees_salary&lt;/code&gt;, followed by a &lt;strong&gt;Bitmap Heap Scan&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here's how it works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;PostgreSQL searches the separate &lt;code&gt;salary&lt;/code&gt; index for entries where &lt;code&gt;salary = 70000&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The Bitmap Index Scan collects the matching heap tuple locations into a bitmap.&lt;/li&gt;
&lt;li&gt;The Bitmap Heap Scan visits the corresponding heap pages, checks tuple visibility, and retrieves the matching employee IDs.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Results:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rows returned: &lt;strong&gt;10,000&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Heap pages accessed: &lt;strong&gt;8,161&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Execution time: &lt;strong&gt;12.315 ms&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In Case 2, PostgreSQL chose a Parallel Sequential Scan because only the composite index &lt;code&gt;(department, salary)&lt;/code&gt; was available.&lt;/p&gt;

&lt;p&gt;Now that we have a separate index on &lt;code&gt;salary&lt;/code&gt;, PostgreSQL can directly search for matching salary values without depending on the leading &lt;code&gt;department&lt;/code&gt; column.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 3: Filtering Using AND
&lt;/h3&gt;

&lt;p&gt;Now, let's filter employees using both &lt;code&gt;department&lt;/code&gt; and &lt;code&gt;salary&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;employee_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Engineering'&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;70000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution Plan:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftthm4ixtbmw8y14af050.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftthm4ixtbmw8y14af050.png" alt=" " width="800" height="218"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PostgreSQL used a &lt;strong&gt;Bitmap Index Scan&lt;/strong&gt; on &lt;code&gt;idx_employees_department_salary&lt;/code&gt;, followed by a &lt;strong&gt;Bitmap Heap Scan&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Even though we have a separate index on &lt;code&gt;salary&lt;/code&gt;, PostgreSQL chose to use only the composite index.&lt;/p&gt;

&lt;p&gt;Here's how it works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;PostgreSQL searches the composite index for entries where &lt;code&gt;department = 'Engineering'&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Since salary values are ordered within each department, PostgreSQL can further narrow the search using &lt;code&gt;salary = 70000&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The Bitmap Index Scan collects the matching heap tuple locations into a bitmap.&lt;/li&gt;
&lt;li&gt;The Bitmap Heap Scan visits the corresponding heap pages, checks tuple visibility, and retrieves the matching employee IDs.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Results:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rows returned: &lt;strong&gt;10,000&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Heap pages accessed: &lt;strong&gt;8,161&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Execution time: &lt;strong&gt;94.164 ms&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unlike Case 1, where PostgreSQL combined two separate indexes using &lt;code&gt;BitmapAnd&lt;/code&gt;, here it used a single composite index to apply both filtering conditions.&lt;/p&gt;

&lt;p&gt;The separate salary index was not needed for this query.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 4: Filtering Using OR
&lt;/h3&gt;

&lt;p&gt;Now, let's filter employees using either &lt;code&gt;department&lt;/code&gt; or &lt;code&gt;salary&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;employee_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Engineering'&lt;/span&gt;
   &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;70000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution Plan:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Feqs3hzt759t707le8vng.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Feqs3hzt759t707le8vng.png" alt=" " width="800" height="239"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PostgreSQL used two &lt;strong&gt;Bitmap Index Scans&lt;/strong&gt;, combined their results using &lt;strong&gt;BitmapOr&lt;/strong&gt;, and then performed a &lt;strong&gt;Bitmap Heap Scan&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This time, PostgreSQL used both the composite index &lt;code&gt;idx_employees_department_salary&lt;/code&gt; and the separate index &lt;code&gt;idx_employees_salary&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here's how it works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;PostgreSQL searches the composite index using &lt;code&gt;department = 'Engineering'&lt;/code&gt; and identifies 200,000 matching records.&lt;/li&gt;
&lt;li&gt;It searches the separate salary index using &lt;code&gt;salary = 70000&lt;/code&gt; and identifies 10,000 matching records.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;BitmapOr&lt;/strong&gt; combines both bitmaps, keeping the heap tuple locations that match either condition.&lt;/li&gt;
&lt;li&gt;The Bitmap Heap Scan visits the corresponding heap pages, checks tuple visibility, and retrieves the matching employee IDs.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Results:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rows returned: &lt;strong&gt;200,000&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Heap pages accessed: &lt;strong&gt;8,161&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Execution time: &lt;strong&gt;63.308 ms&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In Case 2, PostgreSQL performed a Sequential Scan for the same &lt;code&gt;OR&lt;/code&gt; query because only the composite index was available.&lt;/p&gt;

&lt;p&gt;With the additional salary index, PostgreSQL was able to use &lt;code&gt;BitmapOr&lt;/code&gt; to combine both indexes and retrieve the matching records.&lt;/p&gt;

&lt;h2&gt;
  
  
  Comparing the Three Index Configurations
&lt;/h2&gt;

&lt;p&gt;We executed the same four queries using different index configurations.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Query&lt;/th&gt;
&lt;th&gt;Separate Indexes&lt;/th&gt;
&lt;th&gt;Composite Index&lt;/th&gt;
&lt;th&gt;Composite + Salary Index&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Department&lt;/td&gt;
&lt;td&gt;Bitmap Index Scan&lt;/td&gt;
&lt;td&gt;Bitmap Index Scan&lt;/td&gt;
&lt;td&gt;Bitmap Index Scan&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Salary&lt;/td&gt;
&lt;td&gt;Bitmap Index Scan&lt;/td&gt;
&lt;td&gt;Parallel Seq Scan&lt;/td&gt;
&lt;td&gt;Bitmap Index Scan&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Department AND Salary&lt;/td&gt;
&lt;td&gt;BitmapAnd&lt;/td&gt;
&lt;td&gt;Bitmap Index Scan&lt;/td&gt;
&lt;td&gt;Bitmap Index Scan&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Department OR Salary&lt;/td&gt;
&lt;td&gt;BitmapOr&lt;/td&gt;
&lt;td&gt;Seq Scan&lt;/td&gt;
&lt;td&gt;BitmapOr&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Composite indexes are useful when queries frequently filter or sort using multiple columns.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Column order matters:&lt;/strong&gt; The order of columns in a composite index determines how efficiently PostgreSQL can use it for searching and sorting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Leftmost prefix rule:&lt;/strong&gt; PostgreSQL generally uses composite indexes most efficiently when queries filter using the leading indexed columns. Filtering only by a non-leading column may be less efficient.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AND conditions:&lt;/strong&gt; A composite index can use multiple filtering conditions in a single index scan, avoiding the need to combine separate indexes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OR conditions:&lt;/strong&gt; PostgreSQL can combine multiple indexes using &lt;code&gt;BitmapOr&lt;/code&gt; when suitable indexes are available. Otherwise, it may choose a Sequential Scan.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Separate indexes:&lt;/strong&gt; A composite index may not replace the need for individual indexes, especially when non-leading columns are frequently filtered independently.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Composite indexes can improve query performance when columns are indexed in the right order. However, no single index works best for every query, so choose indexes based on your most frequently used filtering and sorting conditions.&lt;/p&gt;

</description>
      <category>database</category>
      <category>performance</category>
      <category>postgres</category>
      <category>sql</category>
    </item>
    <item>
      <title>Database Indexing – Part 2: Understanding Covering Indexes in PostgreSQL</title>
      <dc:creator>Anusha Renangi</dc:creator>
      <pubDate>Tue, 20 Oct 2026 15:34:37 +0000</pubDate>
      <link>https://dev.to/anusha_renangi_05a4e41d35/database-indexing-part-2-understanding-covering-indexes-in-postgresql-17ee</link>
      <guid>https://dev.to/anusha_renangi_05a4e41d35/database-indexing-part-2-understanding-covering-indexes-in-postgresql-17ee</guid>
      <description>&lt;p&gt;Recently, while working on a performance optimization task, I came across an interesting problem.&lt;/p&gt;

&lt;p&gt;I had a query where I needed to fetch document versions for around &lt;strong&gt;6,500 document IDs&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;documents&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;document_id&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;101&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;102&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;103&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...,&lt;/span&gt; &lt;span class="mi"&gt;6500&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This looks like a simple query, right?&lt;/p&gt;

&lt;p&gt;But the entire operation was taking around &lt;strong&gt;4 seconds&lt;/strong&gt;. When I checked where the time was being spent, I noticed that this single database query was taking almost &lt;strong&gt;85% of the total time&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That's around 3.4 seconds for just one database query!&lt;/p&gt;

&lt;p&gt;So, I started looking for ways to reduce the query execution time.&lt;/p&gt;

&lt;p&gt;My first thought was to create an index on both &lt;code&gt;document_id&lt;/code&gt; and &lt;code&gt;document_version&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_document_version&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;documents&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But I'm filtering only using &lt;code&gt;document_id&lt;/code&gt;. I just need &lt;code&gt;document_version&lt;/code&gt; in the result.&lt;/p&gt;

&lt;p&gt;Also, &lt;code&gt;document_version&lt;/code&gt; changes frequently, so I was concerned about the additional index maintenance during updates.&lt;/p&gt;

&lt;p&gt;I wanted to know whether both columns really needed to be part of the index key.&lt;/p&gt;

&lt;p&gt;PostgreSQL also has an &lt;code&gt;INCLUDE&lt;/code&gt; option, where we can store additional columns in an index without making them key columns.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_document_version&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;documents&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;INCLUDE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_version&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both indexes contain the columns required by my query. But how are they different internally? Does using &lt;code&gt;INCLUDE&lt;/code&gt; make any difference to query performance, index size, or updates?&lt;/p&gt;

&lt;p&gt;Let's look at how both indexes work.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Understanding Covering Indexes
&lt;/h2&gt;

&lt;p&gt;A covering index is an index that contains all the columns required by a query, so PostgreSQL may be able to return the result without fetching the actual row from the table.&lt;/p&gt;

&lt;p&gt;For example, consider this query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;documents&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;document_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we're using &lt;code&gt;document_id&lt;/code&gt; to search and &lt;code&gt;document_version&lt;/code&gt; to get the required value.&lt;/p&gt;

&lt;p&gt;If we have an index only on &lt;code&gt;document_id&lt;/code&gt;, PostgreSQL can use it to find the matching record. But it still needs to visit the heap to fetch &lt;code&gt;document_version&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;What if both values were already available in the index?&lt;/p&gt;

&lt;p&gt;We could create an index like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_document_version&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;documents&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;INCLUDE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_version&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we have two types of columns.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Key Column — &lt;code&gt;document_id&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Used to search and navigate through the B-tree.&lt;/li&gt;
&lt;li&gt;Determines the ordering of index entries.&lt;/li&gt;
&lt;li&gt;Can be used for filtering and sorting.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Non-Key Column — &lt;code&gt;document_version&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stored along with the key in the leaf pages.&lt;/li&gt;
&lt;li&gt;Doesn't participate in B-tree searching or ordering.&lt;/li&gt;
&lt;li&gt;Can be returned from the index without fetching that value from the heap.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How Are These Columns Stored?
&lt;/h3&gt;

&lt;p&gt;PostgreSQL's B-tree has three types of pages: root, internal, and leaf pages.&lt;/p&gt;

&lt;p&gt;In an index created using &lt;code&gt;INCLUDE&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Root and internal pages&lt;/strong&gt; contain navigation keys based on &lt;code&gt;document_id&lt;/code&gt; and pointers to lower-level pages.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Leaf pages&lt;/strong&gt; contain &lt;code&gt;document_id&lt;/code&gt;, the included &lt;code&gt;document_version&lt;/code&gt;, and a reference to the corresponding heap tuple.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, PostgreSQL searches the B-tree using &lt;code&gt;document_id&lt;/code&gt; and finds &lt;code&gt;document_version&lt;/code&gt; in the matching leaf entry.&lt;/p&gt;

&lt;p&gt;Since both required columns are available in the index, PostgreSQL may be able to return the result without fetching the actual row from the heap. This is called an &lt;strong&gt;Index Only Scan&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Testing Covering Indexes in PostgreSQL
&lt;/h2&gt;

&lt;p&gt;Let's create a &lt;code&gt;documents&lt;/code&gt; table with 10 million records and compare how PostgreSQL performs with a primary key index and an index using &lt;code&gt;INCLUDE&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Create the Table
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;documents&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;document_id&lt;/span&gt; &lt;span class="nb"&gt;BIGINT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;document_name&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;document_version&lt;/span&gt; &lt;span class="nb"&gt;INTEGER&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since &lt;code&gt;document_id&lt;/code&gt; is the primary key, PostgreSQL automatically creates a B-tree index named &lt;code&gt;documents_pkey&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Insert 10 Million Records
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;documents&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;document_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;document_version&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'Document_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)::&lt;/span&gt;&lt;span class="nb"&gt;INTEGER&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;generate_series&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10000000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, our table contains 10 million documents.&lt;/p&gt;

&lt;p&gt;Let's fetch document versions for 6,500 document IDs and compare both approaches.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 1: Using the Primary Key Index
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;documents&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;document_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;ANY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;ARRAY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;generate_series&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6500&lt;/span&gt;&lt;span class="p"&gt;)::&lt;/span&gt;&lt;span class="nb"&gt;BIGINT&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqp60r4s7d58hdvn8q1fi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqp60r4s7d58hdvn8q1fi.png" alt=" " width="799" height="252"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PostgreSQL used an &lt;strong&gt;Index Scan&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The primary key index contains &lt;code&gt;document_id&lt;/code&gt;, but not &lt;code&gt;document_version&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So PostgreSQL:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Searches the index for the matching document IDs.&lt;/li&gt;
&lt;li&gt;Gets the corresponding heap tuple references.&lt;/li&gt;
&lt;li&gt;Visits the heap to fetch &lt;code&gt;document_version&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The query returned 6,500 records and took &lt;strong&gt;11.028 ms&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 2: Creating an Index with INCLUDE
&lt;/h3&gt;

&lt;p&gt;Now, let's create an index that also contains &lt;code&gt;document_version&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_documents_covering&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;documents&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;INCLUDE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document_version&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;document_id&lt;/code&gt; is the key column, while &lt;code&gt;document_version&lt;/code&gt; is a non-key column stored in the leaf pages.&lt;/p&gt;

&lt;p&gt;Let's execute the same query again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;documents&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;document_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;ANY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;ARRAY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;generate_series&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6500&lt;/span&gt;&lt;span class="p"&gt;)::&lt;/span&gt;&lt;span class="nb"&gt;BIGINT&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fthsisutm0alz1omg8mx5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fthsisutm0alz1omg8mx5.png" alt=" " width="800" height="259"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This time, PostgreSQL used an &lt;strong&gt;Index Only Scan&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Since both &lt;code&gt;document_id&lt;/code&gt; and &lt;code&gt;document_version&lt;/code&gt; are available in the index, PostgreSQL was able to return the required values without visiting the heap in this execution.&lt;/p&gt;

&lt;p&gt;Notice:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Heap Fetches: 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The query took &lt;strong&gt;9.144 ms&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Comparing the Execution Times
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Metric&lt;/th&gt;
&lt;th&gt;Primary Key Index&lt;/th&gt;
&lt;th&gt;Index with INCLUDE&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Scan type&lt;/td&gt;
&lt;td&gt;Index Scan&lt;/td&gt;
&lt;td&gt;Index Only Scan&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Rows returned&lt;/td&gt;
&lt;td&gt;6,500&lt;/td&gt;
&lt;td&gt;6,500&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Execution time&lt;/td&gt;
&lt;td&gt;11.028 ms&lt;/td&gt;
&lt;td&gt;9.144 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In this experiment, the execution time decreased from &lt;strong&gt;11.028 ms to 9.144 ms&lt;/strong&gt;, approximately a &lt;strong&gt;17% improvement&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;With the primary key index, PostgreSQL had to visit the heap to fetch &lt;code&gt;document_version&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But with &lt;code&gt;INCLUDE&lt;/code&gt;, both required columns were available in the index, and PostgreSQL avoided those heap fetches.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Note on Caching
&lt;/h3&gt;

&lt;p&gt;I tested both approaches on my local Ubuntu machine. Before each test, I restarted PostgreSQL and cleared the Linux filesystem cache using the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl stop postgresql
&lt;span class="nb"&gt;sync
echo &lt;/span&gt;3 | &lt;span class="nb"&gt;sudo tee&lt;/span&gt; /proc/sys/vm/drop_caches
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl start postgresql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These commands are only intended for a dedicated local test environment, not a production or shared server.&lt;/p&gt;

&lt;p&gt;The covering index improved query performance in our experiment by avoiding heap fetches.&lt;/p&gt;

&lt;p&gt;However, since we can't completely eliminate caching effects, it's difficult to say exactly how much improvement we can expect.&lt;/p&gt;

&lt;p&gt;These results are from my local system, and the actual performance improvement may vary depending on the environment and workload.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Why Can an Index Only Scan Still Have Heap Fetches?
&lt;/h2&gt;

&lt;p&gt;In our previous query, PostgreSQL used an Index Only Scan with &lt;code&gt;Heap Fetches: 0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But an Index Only Scan doesn't always mean PostgreSQL will completely avoid accessing the heap.&lt;/p&gt;

&lt;p&gt;Let's update some records and check.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;documents&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;document_id&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="mi"&gt;6500&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's execute the same query again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document_version&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;documents&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;document_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;ANY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;ARRAY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;generate_series&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6500&lt;/span&gt;&lt;span class="p"&gt;)::&lt;/span&gt;&lt;span class="nb"&gt;BIGINT&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fv7s0qtm7sveayggcfh0m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fv7s0qtm7sveayggcfh0m.png" alt=" " width="800" height="242"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PostgreSQL still used an Index Only Scan, but this time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Heap Fetches: 13000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We updated 6,500 records, but PostgreSQL performed 13,000 heap fetches.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;h3&gt;
  
  
  How Does PostgreSQL Check Row Visibility?
&lt;/h3&gt;

&lt;p&gt;PostgreSQL uses &lt;strong&gt;MVCC (Multi-Version Concurrency Control)&lt;/strong&gt; to manage updates and transactions.&lt;/p&gt;

&lt;p&gt;When we update a row, PostgreSQL creates a new row version instead of overwriting the existing one.&lt;/p&gt;

&lt;p&gt;The index contains the values we need, but it doesn't have enough information to determine whether a row version is visible to the current transaction.&lt;/p&gt;

&lt;p&gt;For this, PostgreSQL maintains a &lt;strong&gt;Visibility Map&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The visibility map keeps track of which heap pages contain only row versions that are visible to all transactions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If a heap page is marked &lt;strong&gt;all-visible&lt;/strong&gt;, PostgreSQL can return the required values from the index without checking the heap.&lt;/li&gt;
&lt;li&gt;If a heap page isn't marked all-visible, PostgreSQL needs to visit the heap to check row visibility.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What Happened After the UPDATE?
&lt;/h3&gt;

&lt;p&gt;When we updated those 6,500 records, PostgreSQL created new row versions and cleared the all-visible flags for the affected heap pages.&lt;/p&gt;

&lt;p&gt;Since &lt;code&gt;document_version&lt;/code&gt; is included in our index, the update also required new index entries.&lt;/p&gt;

&lt;p&gt;During the next Index Only Scan, PostgreSQL had to check the heap for row visibility.&lt;/p&gt;

&lt;p&gt;That's why we got:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Heap Fetches: 13000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are heap tuple fetch attempts, not 13,000 different heap pages.&lt;/p&gt;

&lt;h3&gt;
  
  
  What About VACUUM?
&lt;/h3&gt;

&lt;p&gt;PostgreSQL provides &lt;code&gt;VACUUM&lt;/code&gt; to clean up dead tuples so their space can be reused. It also updates the visibility map when pages qualify as all-visible.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;VACUUM&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;documents&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can help reduce heap fetches during Index Only Scans, although it doesn't guarantee that they will become zero.&lt;/p&gt;

&lt;p&gt;We'll cover how &lt;code&gt;VACUUM&lt;/code&gt; works internally in a separate article.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Covering indexes can help reduce heap access when all the required columns are available in the index.&lt;/p&gt;

&lt;p&gt;But indexes also come with a cost.&lt;/p&gt;

&lt;p&gt;Even though &lt;code&gt;INCLUDE&lt;/code&gt; columns are not part of the index key, PostgreSQL still needs to store and maintain their values. If an included column is updated frequently, it can increase write overhead.&lt;/p&gt;

&lt;p&gt;So, we need to consider both read and write performance before creating additional indexes.&lt;/p&gt;

&lt;p&gt;In the next article, we'll look at &lt;strong&gt;composite indexes&lt;/strong&gt;, how column order affects index usage, and why the order of columns matters when filtering and sorting records.&lt;/p&gt;

&lt;p&gt;That's it for now!&lt;/p&gt;

&lt;p&gt;Thanks for reading! 😊&lt;/p&gt;

</description>
      <category>database</category>
      <category>performance</category>
      <category>postgres</category>
      <category>sql</category>
    </item>
    <item>
      <title>Database Indexing – Part 1: How Indexes Help Find Data Faster</title>
      <dc:creator>Anusha Renangi</dc:creator>
      <pubDate>Sat, 17 Oct 2026 15:10:09 +0000</pubDate>
      <link>https://dev.to/anusha_renangi_05a4e41d35/database-indexing-part-1-how-indexes-help-find-data-faster-2pk5</link>
      <guid>https://dev.to/anusha_renangi_05a4e41d35/database-indexing-part-1-how-indexes-help-find-data-faster-2pk5</guid>
      <description>&lt;p&gt;Recently, I was reading two books: &lt;em&gt;Good Vibes, Good Life&lt;/em&gt; and &lt;em&gt;The Mountain Is You&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsqtrxco4am6b8iolsacj.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsqtrxco4am6b8iolsacj.jpeg" alt=" " width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I usually forget things after some time 😅. So whenever I want to revisit something I've read, I go back to the book.&lt;/p&gt;

&lt;p&gt;But I noticed one difference between these two books.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Good Vibes, Good Life&lt;/strong&gt; has an index at the end of the book. Whenever I want to read about a particular topic, I can look at the index, find the page number, and directly go to that page.&lt;/p&gt;

&lt;p&gt;But &lt;strong&gt;The Mountain Is You&lt;/strong&gt; doesn't have that kind of index.&lt;/p&gt;

&lt;p&gt;So if I remember a topic but don't remember where I read it, I have to go through the chapters one by one until I find it.&lt;/p&gt;

&lt;p&gt;That takes more time, right?&lt;/p&gt;

&lt;p&gt;The same thing happens in databases.&lt;/p&gt;

&lt;p&gt;Imagine we have a table containing millions of records, and we want to find one particular record.&lt;/p&gt;

&lt;p&gt;Without an index, the database may need to scan the entire table, checking rows to find the data we're looking for.&lt;/p&gt;

&lt;p&gt;But if we have a suitable index, the database can use it to quickly locate the required record instead of checking every row.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This is why we use indexes in databases.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An index is a separate data structure that helps the database find records efficiently, just like the index in a book helps us find a topic without going through every page.&lt;/p&gt;

&lt;p&gt;Before moving forward, check out my previous article, &lt;a href="https://dev.to/anusha_renangi_05a4e41d35/database-storage-concepts-how-tables-are-stored-on-disk-387h"&gt;Database Storage Concepts – How Tables Are Stored on Disk&lt;/a&gt;, to understand how databases store data internally.&lt;/p&gt;

&lt;p&gt;Now, let's see how indexing actually works in PostgreSQL.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a Table with an Index and 10 Million Records
&lt;/h2&gt;

&lt;p&gt;First, let's create an &lt;code&gt;employees&lt;/code&gt; table with three columns: &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt;, and &lt;code&gt;salary&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Create the table&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="nb"&gt;INTEGER&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we defined &lt;code&gt;id&lt;/code&gt; as the &lt;strong&gt;PRIMARY KEY&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In PostgreSQL, whenever we define a primary key, it automatically creates a unique B-tree index on that column.&lt;/p&gt;

&lt;p&gt;In our case, PostgreSQL created an index named &lt;code&gt;employees_pkey&lt;/code&gt; on the &lt;code&gt;id&lt;/code&gt; column.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Insert 10 million records&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'Employee_'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="mi"&gt;30000&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;70000&lt;/span&gt;&lt;span class="p"&gt;)::&lt;/span&gt;&lt;span class="nb"&gt;INTEGER&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;generate_series&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10000000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;g&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We now have a table containing 10 million employee records.&lt;/p&gt;

&lt;p&gt;Let's understand how PostgreSQL uses indexes when we execute different queries.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Indexes Through Queries
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Query 1: Fetching a Record Using an Indexed Column
&lt;/h3&gt;

&lt;p&gt;Let's find the employee whose &lt;code&gt;id&lt;/code&gt; is &lt;code&gt;20&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before looking at the output, let's understand what &lt;code&gt;EXPLAIN&lt;/code&gt;, &lt;code&gt;ANALYZE&lt;/code&gt;, and &lt;code&gt;BUFFERS&lt;/code&gt; mean.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;EXPLAIN:&lt;/strong&gt; Shows how PostgreSQL plans to execute a query without actually running it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ANALYZE:&lt;/strong&gt; Executes the query and shows what actually happened, including the number of rows returned and execution time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;BUFFERS:&lt;/strong&gt; Shows how many data block accesses PostgreSQL made during planning and execution, including blocks already available in memory and blocks that had to be read into memory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By combining these options, we can understand how PostgreSQL executes our query and how it accesses data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output (First Execution):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftlvrbmvial0vcy5wn3o1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftlvrbmvial0vcy5wn3o1.png" alt=" " width="800" height="171"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's understand each part of the output.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Index Only Scan&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Index Only Scan using employees_pkey on employees
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;PostgreSQL used the &lt;code&gt;employees_pkey&lt;/code&gt; index, which was automatically created when we defined &lt;code&gt;id&lt;/code&gt; as the primary key.&lt;/li&gt;
&lt;li&gt;We're searching by &lt;code&gt;id&lt;/code&gt; and selecting only &lt;code&gt;id&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Since &lt;code&gt;id&lt;/code&gt; is already stored in the index, PostgreSQL can retrieve the required value directly from the index.&lt;/li&gt;
&lt;li&gt;It doesn't need to fetch the full employee record from the table, provided PostgreSQL can confirm that the row is visible without checking the heap.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is called an &lt;strong&gt;Index Only Scan&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Cost&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cost=0.43..4.45
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;0.43&lt;/code&gt;: Estimated startup cost before PostgreSQL can begin returning rows.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;4.45&lt;/code&gt;: Estimated total cost of executing this operation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These values are PostgreSQL's internal cost estimates, not actual execution times in milliseconds.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Rows and Width&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rows=1 width=4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;rows=1&lt;/code&gt;: PostgreSQL estimates that one row will be returned.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;width=4&lt;/code&gt;: The estimated average size of each returned row is 4 bytes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We're selecting only &lt;code&gt;id&lt;/code&gt;, which is an integer occupying 4 bytes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Actual Time, Rows, and Loops&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;actual time=0.058..0.059 rows=1 loops=1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;0.058&lt;/code&gt;: Time taken until the first row was returned.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;0.059&lt;/code&gt;: Time taken until this scan operation completed.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;rows=1&lt;/code&gt;: One row was actually returned.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;loops=1&lt;/code&gt;: This operation executed once.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The actual times are measured in milliseconds.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Index Condition&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Index Cond: (id = 20)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells us that PostgreSQL used the condition &lt;code&gt;id = 20&lt;/code&gt; to search the index.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Heap Fetches&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Heap Fetches: 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PostgreSQL stores actual table records in heap pages.&lt;/p&gt;

&lt;p&gt;An Index Only Scan can avoid visiting heap pages when all the required values are available in the index and PostgreSQL can confirm row visibility using its visibility map.&lt;/p&gt;

&lt;p&gt;Here, &lt;code&gt;Heap Fetches: 0&lt;/code&gt; means PostgreSQL didn't need to visit any heap pages to check row visibility.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Buffers&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Buffers: shared read=4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PostgreSQL stores frequently accessed data pages in memory, in an area called &lt;strong&gt;shared buffers&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;shared read=4&lt;/code&gt;: PostgreSQL had to read 4 blocks into its shared buffer cache.&lt;/li&gt;
&lt;li&gt;Each PostgreSQL block is typically 8 KB.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This doesn't necessarily mean 4 physical disk reads, because the operating system may already have those pages cached.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. Planning Time and Execution Time&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Planning:
  Buffers: shared hit=41 read=20
Planning Time: 0.616 ms
Execution Time: 0.097 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before executing a query, PostgreSQL first decides how to execute it.&lt;/p&gt;

&lt;p&gt;For example, it checks what indexes are available, estimates how many rows might match, and decides which execution plan would be more efficient.&lt;/p&gt;

&lt;p&gt;During this process, PostgreSQL may need to access database pages containing metadata and statistics.&lt;/p&gt;

&lt;p&gt;That's why we see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Planning:
  Buffers: shared hit=41 read=20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;shared hit=41&lt;/code&gt;: During planning, PostgreSQL made 41 buffer accesses that found pages already available in shared buffers.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;shared read=20&lt;/code&gt;: During planning, PostgreSQL made 20 block reads into shared buffers.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Planning Time: 0.616 ms&lt;/code&gt;: Time taken to prepare the execution plan.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Execution Time: 0.097 ms&lt;/code&gt;: Time taken to execute the plan and retrieve the result.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, PostgreSQL first decides &lt;strong&gt;how to fetch the data (planning)&lt;/strong&gt; and then actually &lt;strong&gt;fetches the data (execution)&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Happens When We Run the Same Query Again?
&lt;/h3&gt;

&lt;p&gt;I executed the exact same query one more time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output (Second Execution):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2kpx4fyu7vxzprn9l5l3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2kpx4fyu7vxzprn9l5l3.png" alt=" " width="800" height="149"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice the difference:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Metric&lt;/th&gt;
&lt;th&gt;First execution&lt;/th&gt;
&lt;th&gt;Second execution&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Execution shared reads&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Execution shared hits&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Planning time&lt;/td&gt;
&lt;td&gt;0.616 ms&lt;/td&gt;
&lt;td&gt;0.059 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Execution time&lt;/td&gt;
&lt;td&gt;0.097 ms&lt;/td&gt;
&lt;td&gt;0.027 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In the first execution, PostgreSQL had to read 4 blocks into shared buffers.&lt;/p&gt;

&lt;p&gt;But during the second execution, those blocks were already available in PostgreSQL's memory.&lt;/p&gt;

&lt;p&gt;That's why we see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Buffers: shared hit=4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A &lt;strong&gt;shared hit&lt;/strong&gt; means PostgreSQL found the required block in its shared buffer cache without needing to read it into that cache again.&lt;/p&gt;

&lt;p&gt;The second execution was faster in our experiment, although execution times can also vary for other reasons.&lt;/p&gt;

&lt;h3&gt;
  
  
  Testing Without PostgreSQL's Existing Cache
&lt;/h3&gt;

&lt;p&gt;If you want to observe how a query behaves when its pages are not already cached in PostgreSQL's shared buffers, you can restart PostgreSQL on your local test machine.&lt;/p&gt;

&lt;p&gt;On Ubuntu:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl restart postgresql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then execute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Restarting PostgreSQL clears its shared buffer cache, but it does &lt;strong&gt;not&lt;/strong&gt; clear the operating system's page cache. Some pages may also be loaded again before your query runs.&lt;/p&gt;

&lt;p&gt;So this is useful for observing PostgreSQL's buffer behavior, but it doesn't guarantee a completely cold disk-cache experiment.&lt;/p&gt;

&lt;p&gt;Only restart PostgreSQL for this purpose on a local test environment, not on a production or shared database.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 2: Fetching a Column That Is Not in the Index
&lt;/h3&gt;

&lt;p&gt;In the previous query, we selected &lt;code&gt;id&lt;/code&gt;, which was already available in the primary key index.&lt;/p&gt;

&lt;p&gt;Now, let's try fetching &lt;code&gt;name&lt;/code&gt; using the same index.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fo5brngykgfz5psmnwziq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fo5brngykgfz5psmnwziq.png" alt=" " width="797" height="135"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, PostgreSQL used an &lt;strong&gt;Index Scan&lt;/strong&gt; instead of an Index Only Scan.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Our &lt;code&gt;employees_pkey&lt;/code&gt; index contains &lt;code&gt;id&lt;/code&gt;, but not &lt;code&gt;name&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So PostgreSQL:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Searches the index to find &lt;code&gt;id = 500&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Gets the reference to the corresponding heap row.&lt;/li&gt;
&lt;li&gt;Visits the heap to fetch the &lt;code&gt;name&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is called an &lt;strong&gt;Index Scan&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Notice the estimated cost:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cost=0.43..8.45 rows=1 width=16
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The total estimated cost is &lt;code&gt;8.45&lt;/code&gt;, compared with &lt;code&gt;4.45&lt;/code&gt; in Query 1, because PostgreSQL also needs to fetch the row from the heap.&lt;/p&gt;

&lt;p&gt;The estimated &lt;code&gt;width&lt;/code&gt; is now &lt;code&gt;16&lt;/code&gt; bytes instead of &lt;code&gt;4&lt;/code&gt;, because we're selecting a text column rather than an integer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Buffers: shared hit=2 read=2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;During execution, PostgreSQL made 2 shared-buffer hits and 2 shared-buffer reads, covering the index search and heap access.&lt;/p&gt;

&lt;p&gt;The execution took &lt;code&gt;0.093 ms&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So the difference is simple: &lt;strong&gt;an Index Only Scan can return data from the index without visiting the heap when visibility permits, whereas this Index Scan uses the index to find the row and then visits the heap to retrieve &lt;code&gt;name&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 3: Searching Using a Column Without an Index
&lt;/h3&gt;

&lt;p&gt;Until now, we've been searching using &lt;code&gt;id&lt;/code&gt;, which has an index.&lt;/p&gt;

&lt;p&gt;But what happens if we search using a column that doesn't have an index?&lt;/p&gt;

&lt;p&gt;Let's try searching using &lt;code&gt;name&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Employee_500'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2pvhn8v13mx4ip5slvz7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2pvhn8v13mx4ip5slvz7.png" alt=" " width="800" height="213"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, PostgreSQL used a &lt;strong&gt;Parallel Sequential Scan&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's understand what happened.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Parallel Sequential Scan&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Parallel Seq Scan on employees
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since we haven't created an index on &lt;code&gt;name&lt;/code&gt;, PostgreSQL cannot use an index on that column to locate the required employee.&lt;/p&gt;

&lt;p&gt;Instead, it scans the table and checks the &lt;code&gt;name&lt;/code&gt; of each row against our condition.&lt;/p&gt;

&lt;p&gt;But rather than using a single process, PostgreSQL decided to scan the table in parallel.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Workers Planned and Launched&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Workers Planned: 2
Workers Launched: 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PostgreSQL planned to use 2 additional worker processes and successfully launched both.&lt;/p&gt;

&lt;p&gt;Along with the main process, 3 processes participated in scanning the table.&lt;/p&gt;

&lt;p&gt;Each process works on different portions of the table, allowing the scan to finish faster than a single-process scan might.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Filter and Rows Removed&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Filter: (name = 'Employee_500'::text)
Rows Removed by Filter: 3333333
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PostgreSQL checks each row against the condition &lt;code&gt;name = 'Employee_500'&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Rows that don't match are removed by the filter.&lt;/p&gt;

&lt;p&gt;Here, &lt;code&gt;Rows Removed by Filter: 3333333&lt;/code&gt; is the average number of rows rejected per execution of the parallel scan, rounded for display.&lt;/p&gt;

&lt;p&gt;Since &lt;code&gt;loops=3&lt;/code&gt;, approximately 10 million rows were checked across all three participants.&lt;/p&gt;

&lt;p&gt;Even though we wanted only one employee, PostgreSQL had to examine the entire table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Gather&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Gather  (cost=1000.00..125620.26 rows=1 width=4)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;Gather&lt;/code&gt; operation collects the rows produced by the parallel scan participants and passes them to the main query process.&lt;/p&gt;

&lt;p&gt;In our case, only one row matched the condition.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Buffer Usage&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Buffers: shared hit=1 read=72545
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;During execution, PostgreSQL made:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1 shared-buffer hit.&lt;/li&gt;
&lt;li&gt;72,545 shared-buffer reads.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With the usual 8 KB page size, those reads represent approximately &lt;strong&gt;567 MB of block-read traffic&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Notice that the &lt;code&gt;Gather&lt;/code&gt; and &lt;code&gt;Parallel Seq Scan&lt;/code&gt; show the same buffer totals. We shouldn't add them together, because the parent node's statistics include the work of its child node.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Execution Time&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Execution Time: 374.165 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The query took &lt;code&gt;374.165 ms&lt;/code&gt; to execute.&lt;/p&gt;

&lt;p&gt;Compare that with Query 2, where we searched using the indexed &lt;code&gt;id&lt;/code&gt; column and the execution took &lt;code&gt;0.093 ms&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The difference is significant in these particular runs.&lt;/p&gt;

&lt;p&gt;With the index, PostgreSQL could locate the row directly. Without an index on &lt;code&gt;name&lt;/code&gt;, it scanned all 10 million records to find one matching employee.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 4: Creating an Index on the Name Column and Comparing Query Performance
&lt;/h3&gt;

&lt;p&gt;In the previous query, PostgreSQL scanned all 10 million records because there was no index on &lt;code&gt;name&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, let's create an index on that column.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;employees_name_idx&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a B-tree index on &lt;code&gt;name&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's execute the query again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Employee_500'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ferhrje93yv7br3v4gz0g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ferhrje93yv7br3v4gz0g.png" alt=" " width="800" height="131"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This time, PostgreSQL used an &lt;strong&gt;Index Scan&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Index Scan&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Index Scan using employees_name_idx on employees
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PostgreSQL searches the new index for &lt;code&gt;Employee_500&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Since we're selecting &lt;code&gt;id&lt;/code&gt;, which isn't stored in this index, PostgreSQL then visits the heap to fetch it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Buffer Usage&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Buffers: shared hit=1 read=4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;During execution, PostgreSQL made just 5 shared-buffer accesses, compared with 72,546 in the previous query.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Execution Time&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Execution Time: 0.050 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's compare the results before and after creating the index.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Metric&lt;/th&gt;
&lt;th&gt;Without index&lt;/th&gt;
&lt;th&gt;With index&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Scan type&lt;/td&gt;
&lt;td&gt;Parallel Seq Scan&lt;/td&gt;
&lt;td&gt;Index Scan&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Shared hits&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Shared reads&lt;/td&gt;
&lt;td&gt;72,545&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Execution time&lt;/td&gt;
&lt;td&gt;374.165 ms&lt;/td&gt;
&lt;td&gt;0.050 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The execution time decreased from &lt;code&gt;374.165 ms&lt;/code&gt; to &lt;code&gt;0.050 ms&lt;/code&gt; — approximately &lt;strong&gt;7,483 times faster&lt;/strong&gt; in these two runs.&lt;/p&gt;

&lt;p&gt;Of course, caching and other execution conditions can also affect these timings, so we shouldn't expect exactly the same improvement every time.&lt;/p&gt;

&lt;p&gt;But the important difference is &lt;strong&gt;how PostgreSQL searches for the record&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Earlier, PostgreSQL had to scan the entire table to find &lt;code&gt;Employee_500&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;After creating the index, it searched the B-tree index for that particular name and used the matching entry to fetch the row from the heap.&lt;/p&gt;

&lt;p&gt;So even if the execution time varies due to caching, PostgreSQL no longer needs to scan all 10 million table records just to find that employee.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 5: Sorting Records Without an Index
&lt;/h3&gt;

&lt;p&gt;So far, we've seen how indexes help PostgreSQL search for records. But indexes can also help when sorting data.&lt;/p&gt;

&lt;p&gt;Let's find the top 100 employees with the highest salaries.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr59dpviy9woj2sxb0zvg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr59dpviy9woj2sxb0zvg.png" alt=" " width="799" height="284"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; When reading an &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; execution plan, it's usually easier to start from the &lt;strong&gt;bottom and move toward the top&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The lower-level operations produce data, which is then passed to their parent operations.&lt;/p&gt;

&lt;p&gt;In our query, the data flows through:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Parallel Seq Scan&lt;/strong&gt; – Reads the records from the table.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sort&lt;/strong&gt; – Finds the highest salaries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Gather Merge&lt;/strong&gt; – Combines the sorted results from the parallel workers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Limit&lt;/strong&gt; – Returns only the top 100 records.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is a useful way to understand execution plans, although some operations can run at the same time.&lt;/p&gt;

&lt;p&gt;Now, let's understand each operation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Parallel Sequential Scan&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Parallel Seq Scan on employees
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since there is no index on &lt;code&gt;salary&lt;/code&gt;, PostgreSQL scans the table to find the highest salaries.&lt;/p&gt;

&lt;p&gt;Even though we requested only 100 records, PostgreSQL still needs to examine all 10 million records to determine which employees have the highest salaries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Sort&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sort Key: salary DESC
Sort Method: top-N heapsort  Memory: 31kB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PostgreSQL needs to arrange the results by salary in descending order.&lt;/p&gt;

&lt;p&gt;But notice that it used &lt;strong&gt;top-N heapsort&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Since we requested only the top 100 records, PostgreSQL doesn't need to fully sort all 10 million records.&lt;/p&gt;

&lt;p&gt;Instead, each parallel scan participant keeps track of its best 100 candidates while processing the rows.&lt;/p&gt;

&lt;p&gt;This reduces the memory required for sorting, but the table still has to be scanned.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Gather Merge&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Gather Merge
Workers Planned: 2
Workers Launched: 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The parallel participants produce sorted results.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Gather Merge&lt;/code&gt; combines these results while maintaining the salary order.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Limit&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Limit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, PostgreSQL returns only the first 100 records from the sorted results.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Buffer Usage and Execution Time&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Buffers: shared hit=273 read=72385
Execution Time: 575.346 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;During execution, PostgreSQL made 273 shared-buffer hits and 72,385 shared-buffer reads.&lt;/p&gt;

&lt;p&gt;The query took &lt;strong&gt;575.346 ms&lt;/strong&gt; to execute.&lt;/p&gt;

&lt;p&gt;Even though we requested just 100 records, PostgreSQL had to examine all 10 million records to identify the highest salaries.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 6: Sorting Records After Creating an Index on Salary
&lt;/h3&gt;

&lt;p&gt;In the previous query, PostgreSQL scanned all 10 million records and performed sorting to find the top 100 highest salaries.&lt;/p&gt;

&lt;p&gt;Now, let's create an index on &lt;code&gt;salary&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;employees_salary_idx&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a B-tree index on the &lt;code&gt;salary&lt;/code&gt; column, where the index entries are maintained in sorted order.&lt;/p&gt;

&lt;p&gt;Let's run the same query again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0qt8m293jn4eyr911nz5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0qt8m293jn4eyr911nz5.png" alt=" " width="800" height="171"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice that PostgreSQL used an &lt;strong&gt;Index Scan Backward&lt;/strong&gt; instead of a Parallel Sequential Scan followed by sorting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Index Scan Backward&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Index Scan Backward using employees_salary_idx on employees
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A B-tree index keeps its entries in sorted order.&lt;/p&gt;

&lt;p&gt;Since our query uses &lt;code&gt;ORDER BY salary DESC&lt;/code&gt;, PostgreSQL can scan the index backward, starting from the highest salary values.&lt;/p&gt;

&lt;p&gt;It doesn't need to sort all 10 million records separately.&lt;/p&gt;

&lt;p&gt;And because we're also selecting &lt;code&gt;id&lt;/code&gt;, which isn't stored in the &lt;code&gt;salary&lt;/code&gt; index, PostgreSQL visits the heap to fetch it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Limit&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Limit (cost=0.43..5.17 rows=100 width=8)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is where the &lt;code&gt;LIMIT 100&lt;/code&gt; makes a big difference.&lt;/p&gt;

&lt;p&gt;PostgreSQL starts reading the index from the highest salary and stops after returning 100 records.&lt;/p&gt;

&lt;p&gt;Even though the index contains entries for 10 million records, PostgreSQL doesn't need to scan all of them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Buffer Usage&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Buffers: shared read=103
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PostgreSQL made only 103 shared-buffer reads during execution.&lt;/p&gt;

&lt;p&gt;Compare that with the previous query, which required 72,658 buffer accesses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Execution Time&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Execution Time: 1.123 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's compare both executions.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Metric&lt;/th&gt;
&lt;th&gt;Without salary index&lt;/th&gt;
&lt;th&gt;With salary index&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Execution plan&lt;/td&gt;
&lt;td&gt;Parallel Seq Scan + Sort&lt;/td&gt;
&lt;td&gt;Index Scan Backward&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Shared hits&lt;/td&gt;
&lt;td&gt;273&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Shared reads&lt;/td&gt;
&lt;td&gt;72,385&lt;/td&gt;
&lt;td&gt;103&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Execution time&lt;/td&gt;
&lt;td&gt;575.346 ms&lt;/td&gt;
&lt;td&gt;1.123 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The execution time decreased from &lt;code&gt;575.346 ms&lt;/code&gt; to &lt;code&gt;1.123 ms&lt;/code&gt;, approximately &lt;strong&gt;512 times faster&lt;/strong&gt; in these runs.&lt;/p&gt;

&lt;p&gt;Although caching can affect execution times, the important difference is that PostgreSQL no longer needs to scan the entire table and sort the results.&lt;/p&gt;

&lt;p&gt;It can simply scan the index backward, retrieve the highest salary records, and stop after finding 100 rows.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query 7: Why Doesn't PostgreSQL Always Use an Index?
&lt;/h3&gt;

&lt;p&gt;Until now, we've seen how indexes improved query performance.&lt;/p&gt;

&lt;p&gt;But does PostgreSQL always use an index whenever one is available?&lt;/p&gt;

&lt;p&gt;Let's try searching for employees whose salary is greater than &lt;code&gt;40000&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Remember, we already created an index on the &lt;code&gt;salary&lt;/code&gt; column.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;40000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flv8kmvl9z3oci8dg9jzm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flv8kmvl9z3oci8dg9jzm.png" alt=" " width="800" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Interesting! PostgreSQL chose a &lt;strong&gt;Sequential Scan&lt;/strong&gt;, even though an index exists on &lt;code&gt;salary&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Sequential Scan&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Seq Scan on employees
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our condition is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;40000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This matches a large number of employees.&lt;/p&gt;

&lt;p&gt;In our case, PostgreSQL found &lt;strong&gt;8,572,502 matching records out of 10 million&lt;/strong&gt;, which is approximately 85.7% of the table.&lt;/p&gt;

&lt;p&gt;If PostgreSQL used the &lt;code&gt;salary&lt;/code&gt; index, it would need to find all matching index entries and then visit the heap to fetch &lt;code&gt;id&lt;/code&gt;, since &lt;code&gt;id&lt;/code&gt; isn't stored in that index.&lt;/p&gt;

&lt;p&gt;For so many matching records, accessing the heap through the index could require more work than simply scanning the table.&lt;/p&gt;

&lt;p&gt;So PostgreSQL estimated that a Sequential Scan would be cheaper.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Rows Removed by Filter&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Filter: (salary &amp;gt; 40000)
Rows Removed by Filter: 1427498
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PostgreSQL scanned all 10 million records.&lt;/p&gt;

&lt;p&gt;Of those:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;8,572,502 records matched the condition.&lt;/li&gt;
&lt;li&gt;1,427,498 records were removed by the filter.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Buffer Usage&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Buffers: shared hit=16247 read=56299
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PostgreSQL accessed 72,546 shared-buffer blocks while scanning the table.&lt;/p&gt;

&lt;p&gt;Some pages were already cached, while others had to be read into shared buffers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Execution Time&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Execution Time: 1123.251 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The query took approximately &lt;strong&gt;1.12 seconds&lt;/strong&gt; to execute and return over 8.5 million rows to the query executor.&lt;/p&gt;

&lt;p&gt;Here, the important thing is that &lt;strong&gt;having an index doesn't mean PostgreSQL will always use it&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;PostgreSQL's query planner estimates the cost of different execution plans and chooses the one it expects to be cheaper.&lt;/p&gt;

&lt;p&gt;When a query returns a small number of records, an index can be very useful. But when most of the table matches, a sequential scan may be more efficient.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's Next?
&lt;/h2&gt;

&lt;p&gt;In this article, we focused on understanding indexing through practical queries and execution plans.&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;Part 2&lt;/strong&gt;, we'll explore different types of index scans, when PostgreSQL chooses each one, and how we can design indexes more efficiently.&lt;/p&gt;

&lt;p&gt;That's it for now! 😊&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>database</category>
      <category>indexing</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
