<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>PostgreSQL on Ivan Luminaria</title><link>https://ivanluminaria.com/en/posts/postgresql/</link><description>Recent content in PostgreSQL on Ivan Luminaria</description><generator>Hugo</generator><language>en</language><lastBuildDate>Tue, 24 Mar 2026 08:03:00 +0100</lastBuildDate><atom:link href="https://ivanluminaria.com/en/posts/postgresql/index.xml" rel="self" type="application/rss+xml"/><item><title>VACUUM and autovacuum: why PostgreSQL needs someone to clean up</title><link>https://ivanluminaria.com/en/posts/postgresql/vacuum-autovacuum-postgresql/</link><pubDate>Tue, 24 Mar 2026 08:03:00 +0100</pubDate><guid>https://ivanluminaria.com/en/posts/postgresql/vacuum-autovacuum-postgresql/</guid><description>&lt;p&gt;A couple of years ago I was asked to look at a production PostgreSQL
instance that &amp;ldquo;slows down every week&amp;rdquo;. Always the same pattern: Monday
is fine, Friday is a disaster. Someone restarts the service over the
weekend and the cycle starts again.&lt;/p&gt;
&lt;p&gt;Database around 200 GB. Main tables occupying nearly three times their
actual data size. Queries falling into sequential scans where they
shouldn&amp;rsquo;t have been. Response times climbing day after day.&lt;/p&gt;</description></item><item><title>Roles and Users in PostgreSQL: Why Everything Is (Only) a ROLE</title><link>https://ivanluminaria.com/en/posts/postgresql/postgresql_roles_and_users/</link><pubDate>Tue, 10 Feb 2026 08:03:00 +0100</pubDate><guid>https://ivanluminaria.com/en/posts/postgresql/postgresql_roles_and_users/</guid><description>&lt;p&gt;The first time I seriously worked with PostgreSQL I was coming from
years of other databases. I looked for the &lt;code&gt;CREATE USER&lt;/code&gt; command. I found it.
Then I saw &lt;code&gt;CREATE ROLE&lt;/code&gt;. Then &lt;code&gt;ALTER USER&lt;/code&gt;. Then &lt;code&gt;ALTER ROLE&lt;/code&gt;.&lt;br&gt;
For a few minutes I thought: &amp;ldquo;Alright, someone here enjoys confusing
people.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;Actually, no. PostgreSQL is far more consistent than it appears.
It is just consistent in its own way.&lt;/p&gt;
&lt;h2 id="in-postgresql-there-are-no-users-there-are-roles" class="relative group"&gt;In PostgreSQL there are no users. There are roles. &lt;span class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100"&gt;&lt;a class="group-hover:text-primary-300 dark:group-hover:text-neutral-700" style="text-decoration-line: none !important;" href="#in-postgresql-there-are-no-users-there-are-roles" aria-label="Anchor"&gt;#&lt;/a&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p&gt;The key is this: &lt;strong&gt;in PostgreSQL everything is a ROLE&lt;/strong&gt;.&lt;/p&gt;</description></item><item><title>When a LIKE '%value%' Slows Everything Down: A Real PostgreSQL Optimization Case</title><link>https://ivanluminaria.com/en/posts/postgresql/like-optimization-postgresql/</link><pubDate>Tue, 06 Jan 2026 08:03:00 +0100</pubDate><guid>https://ivanluminaria.com/en/posts/postgresql/like-optimization-postgresql/</guid><description>&lt;p&gt;A few weeks ago, a client contacted me with a very common issue:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;ldquo;Search in the admin console is slow. Sometimes it takes several
seconds. We&amp;rsquo;ve already reduced the JOINs, but the problem hasn&amp;rsquo;t
disappeared.&amp;rdquo;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Environment: PostgreSQL on managed cloud.&lt;br&gt;
Main table: &lt;code&gt;payment_report&lt;/code&gt; (~6 million rows, 3 GB).&lt;br&gt;
Searched column: &lt;code&gt;reference_code&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Problematic query:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;reporting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;payment_report&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;JOIN&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;reporting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;payment_cart&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;ON&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cart_id&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;service_id&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1001&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AND&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reference_code&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;LIKE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;%ABC123%&amp;#39;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;ORDER&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;BY&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;LIMIT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;hr&gt;
&lt;h2 id="-first-observation-the-joins-were-not-the-problem" class="relative group"&gt;🧠 First observation: the JOINs were not the problem &lt;span class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100"&gt;&lt;a class="group-hover:text-primary-300 dark:group-hover:text-neutral-700" style="text-decoration-line: none !important;" href="#-first-observation-the-joins-were-not-the-problem" aria-label="Anchor"&gt;#&lt;/a&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p&gt;I compared:&lt;/p&gt;</description></item><item><title>EXPLAIN ANALYZE is not enough: how to actually read a PostgreSQL execution plan</title><link>https://ivanluminaria.com/en/posts/postgresql/explain-analyze-postgresql/</link><pubDate>Tue, 28 Oct 2025 08:03:00 +0100</pubDate><guid>https://ivanluminaria.com/en/posts/postgresql/explain-analyze-postgresql/</guid><description>&lt;p&gt;The other day a colleague sends me a screenshot on Teams. A query running on a 2-million-row table, 45 seconds execution time. He writes:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;ldquo;I ran EXPLAIN ANALYZE, but I can&amp;rsquo;t figure out what&amp;rsquo;s wrong. The plan looks fine.&amp;rdquo;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Spoiler: the plan was anything but fine. The optimizer had chosen a &lt;span class="glossary-tip" tabindex="0" data-glossary-desc="Nested Loop Join — the join strategy that scans the inner table for each row of the outer table, ideal for small datasets with an index." data-glossary-url="https://ivanluminaria.com/en/glossary/nested-loop/" data-glossary-more="Read more →"&gt;nested loop&lt;/span&gt;
 join where a &lt;span class="glossary-tip" tabindex="0" data-glossary-desc="Hash Join — a join strategy optimized for large data volumes, based on a hash table built in memory." data-glossary-url="https://ivanluminaria.com/en/glossary/hash-join/" data-glossary-more="Read more →"&gt;hash join&lt;/span&gt;
 was needed, and the reason was trivial — stale statistics. But to get there I had to read the plan line by line, and that&amp;rsquo;s when I realized that most DBAs I know use EXPLAIN ANALYZE as a binary oracle: if the time is high, the query is slow. End of analysis.&lt;/p&gt;</description></item></channel></rss>