<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title></title>
    <link rel="self" type="application/atom+xml" href="https://jesipow.com/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://jesipow.com"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2024-12-21T00:00:00+00:00</updated>
    <id>https://jesipow.com/atom.xml</id>
    <entry xml:lang="en">
        <title>Reads causing writes in Postgres</title>
        <published>2024-12-21T00:00:00+00:00</published>
        <updated>2024-12-21T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Alex jesipow
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jesipow.com/blog/postgres-reads-cause-writes/"/>
        <id>https://jesipow.com/blog/postgres-reads-cause-writes/</id>
        
        <summary type="html">&lt;p&gt;It is good practice to regularly inspect the statements running in the hot path of your Postgres instance.
One way to do this is to examine the &lt;code&gt;pg_stat_statements&lt;&#x2F;code&gt; &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.postgresql.org&#x2F;docs&#x2F;current&#x2F;pgstatstatements.html&quot;&gt;view&lt;&#x2F;a&gt;,
which shows various statistics about the SQL statements executed by the Postgres server.&lt;&#x2F;p&gt;
&lt;p&gt;Among many other interesting columns, you will find the &lt;code&gt;shared_blks_dirtied&lt;&#x2F;code&gt; and &lt;code&gt;shared_blks_written&lt;&#x2F;code&gt; columns.
They keep track of how many blocks, Postgres&#x27; smallest unit of I&#x2F;O, in the shared buffer cache are modified by a statement and (ultimately) written to disk.&lt;&#x2F;p&gt;
&lt;p&gt;One observation you may make when inspecting the view is that &lt;code&gt;SELECT&lt;&#x2F;code&gt; queries show non-zero values in the &lt;code&gt;shared_blks_written&lt;&#x2F;code&gt; and &lt;code&gt;shared_blks_dirtied&lt;&#x2F;code&gt; columns.
This seems counterintuitive at first as it indicates that a simple &lt;code&gt;SELECT&lt;&#x2F;code&gt; query, which you would expect to only read data,
can in fact cause data to be written (keeping track of the statement statistics themselves is not what causes these writes).&lt;&#x2F;p&gt;
&lt;p&gt;This article discusses two mechanisms, setting hint bits for transaction state tracking and page pruning, that can cause writes by read-only statements.
Before we can go into detail about these mechanisms themselves, however, we first have to understand how Postgres organises table data and manages concurrency.&lt;&#x2F;p&gt;
</summary>
        
    </entry>
</feed>
