<?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: Roman Frolov</title>
    <description>The latest articles on DEV Community by Roman Frolov (@pgwiz).</description>
    <link>https://dev.to/pgwiz</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2658231%2Fdd45564c-66c9-4535-a197-9175e781b482.png</url>
      <title>DEV Community: Roman Frolov</title>
      <link>https://dev.to/pgwiz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pgwiz"/>
    <language>en</language>
    <item>
      <title>Iotop + PostgreSQL</title>
      <dc:creator>Roman Frolov</dc:creator>
      <pubDate>Thu, 09 Jan 2025 15:24:53 +0000</pubDate>
      <link>https://dev.to/pgwiz/iotop-postgresql-2jpd</link>
      <guid>https://dev.to/pgwiz/iotop-postgresql-2jpd</guid>
      <description>&lt;p&gt;Sometimes it is useful to look at the current I/O load on a machine.&lt;br&gt;
For these purposes, you can use the iotop utility.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;installation [Centos 7]&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;yum -y install iotop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For instance, maybe you only want global disk usage data. The following command accomplishes this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo iotop -botqqqk --iter=60 &amp;gt;&amp;gt; /tmp/io.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, using a regular expression in Linux, we will obtain a certain set of data for the operating system user postgres, which we will then load into the database using a utility COPY&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;grep postgres: /tmp/io.txt | sed -r 's/^(\S+)\s+(\S+)\s+\S+\s+\S+\s+(\S+) K\/s\s+(\S+).+(postgres:.*)$/\1\t\2\t\3\t\4\t\5/' &amp;gt; res.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we will create a table in the database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE UNLOGGED TABLE iotop_log (
ts time NOT NULL,
pid int NOT NULL,
read_kb numeric NOT NULL,
write_kb numeric NOT NULL,
details text NOT NULL
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Load the data&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\copy iotop_log FROM 'res.txt' WITH (FORMAT text);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Prepare for querying:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;VACUUM ANALYZE iotop_log;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Execute the SQL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WITH base AS (
   SELECT *,
          CASE WHEN details LIKE 'postgres: autovacuum worker%' THEN 'autovacuum'
               WHEN details LIKE 'postgres: walwriter%' THEN 'walwriter'
               WHEN details LIKE 'postgres: background writer%' THEN 'bgwriter'
               WHEN details LIKE 'postgres: logger' THEN 'logger'
               WHEN details LIKE 'postgres: stats collector' THEN 'stats collector'
               WHEN details LIKE 'postgres: checkpointer' THEN 'checkpointer'
               ELSE 'other'
          END AS detail_type
     FROM iotop_log
), totals AS (
SELECT detail_type,
       SUM(read_kb) AS read_kb, SUM(write_kb) AS write_kb
  FROM base
 GROUP BY detail_type
)
SELECT *,
       trunc(100.0 * read_kb / SUM(read_kb) OVER (), 2) AS "read %total" ,
       trunc(100.0 * write_kb / SUM(write_kb) OVER (), 2) AS "write %total",
       trunc(read_kb / write_kb, 4) AS RW_ratio,
       pg_size_pretty(read_kb*1024.0) AS read_hr,
       pg_size_pretty(write_kb*1024.0) AS write_hr
  FROM totals
 ORDER BY write_kb DESC;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's look at some results&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; detail_type     |  read_kb   |  write_kb  | read %total | write %total | rw_ratio |   read_hr   | write_hr |
-----------------+------------+------------+-------------+--------------+----------+-------------+----------
 other           | 1363304.90 | 8835663.36 |      100.00 |        99.81 |   0.1542 | 1331 MB     | 8629 MB  |
 stats collector |       0.00 |    9207.64 |        0.00 |         0.10 |   0.0000 | 0.000 bytes | 9208 kB  |
 walwriter       |       0.00 |    5159.32 |        0.00 |         0.05 |   0.0000 | 0.000 bytes | 5159 kB  |
 logger          |       0.00 |    1426.85 |        0.00 |         0.01 |   0.0000 | 0.000 bytes | 1427 kB  |
 autovacuum      |       0.00 |     326.90 |        0.00 |         0.00 |   0.0000 | 0.000 bytes | 327 kB   |
 bgwriter        |       0.00 |      53.37 |        0.00 |         0.00 |   0.0000 | 0.000 bytes | 53 kB    |
(6 rows)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
