<?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: Payyavula Balanagaraju</title>
    <description>The latest articles on DEV Community by Payyavula Balanagaraju (@payyavula_balanagaraju_6d).</description>
    <link>https://dev.to/payyavula_balanagaraju_6d</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%2F4221214%2F649d4460-7797-42c7-afad-1a1bf884d48d.png</url>
      <title>DEV Community: Payyavula Balanagaraju</title>
      <link>https://dev.to/payyavula_balanagaraju_6d</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/payyavula_balanagaraju_6d"/>
    <language>en</language>
    <item>
      <title>I patched the Postgres planner, found 8 bugs in my own patch, then proved Tom Lane's fix with one commit</title>
      <dc:creator>Payyavula Balanagaraju</dc:creator>
      <pubDate>Tue, 17 Nov 2026 19:47:35 +0000</pubDate>
      <link>https://dev.to/payyavula_balanagaraju_6d/i-patched-the-postgres-planner-found-8-bugs-in-my-own-patch-then-proved-tom-lanes-fix-with-one-3716</link>
      <guid>https://dev.to/payyavula_balanagaraju_6d/i-patched-the-postgres-planner-found-8-bugs-in-my-own-patch-then-proved-tom-lanes-fix-with-one-3716</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Who this is for:&lt;/strong&gt; anyone who read my first Postgres article and wanted the rest of the story, or anyone who builds views on views.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The short version:&lt;/strong&gt; I wrote a patch for the Postgres 15.8 planner. It took one report from 194,910 ms to 114 ms. I then found 8 bugs in my own patch. Then I found that Tom Lane had committed a better fix, so I tested that one against the same data and did not submit mine.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  1. The order is 16 years old, and it was a choice
&lt;/h2&gt;

&lt;p&gt;In my &lt;a href="https://depthfirst.hashnode.dev/changing-one-number-from-32-to-34-made-a-postgres-query-6-000-faster-here-s-exactly-why" rel="noopener noreferrer"&gt;first article&lt;/a&gt; I showed that the planner splits a big join into groups of &lt;code&gt;join_collapse_limit&lt;/code&gt; tables (default 8). Only after that does it remove the LEFT JOINs a query does not need.&lt;/p&gt;

&lt;p&gt;That order dates from Postgres 9.0 (2010). Tom Lane put join removal right after the grouping step on purpose. Removal needs facts the planner only builds during that step. Moving it earlier, he wrote, would need "a massive rethinking of the order of operations in the planner". He also judged that removal helps only a minority of queries.&lt;/p&gt;

&lt;p&gt;For most queries this is fine. Views on views are the exception.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. What it did to one report
&lt;/h2&gt;

&lt;p&gt;A reporting view at work expanded to about 435 table references. The query needed about 11 of them. But the groups were drawn while all 435 were still counted. Two tables that needed each other, so the planner could look one up through an index from the other, landed in different groups. That fast plan was no longer possible.&lt;/p&gt;

&lt;p&gt;I am not the first to hit this. People have asked about slow nested views since at least 2010 (&lt;a href="https://www.postgresql.org/message-id/201011241937.08622%40Mail-Followup-To" rel="noopener noreferrer"&gt;pgsql-performance, "problem with from_collapse_limit and joined views"&lt;/a&gt;). Tom Lane answered a similar question again in &lt;a href="https://postgresql.org/message-id/20264.1486185381%40sss.pgh.pa.us" rel="noopener noreferrer"&gt;2017&lt;/a&gt;. The usual advice is to raise the limit, and that works. It is the workaround I shipped.&lt;/p&gt;

&lt;p&gt;I wanted to know if the cause could be removed instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. My patch: remove first, then group
&lt;/h2&gt;

&lt;p&gt;I wrote a pass for 15.8 that removes the unneeded LEFT JOINs from the query tree before the split. The groups are then drawn on the survivors only.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;code&gt;join_collapse_limit&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;Before&lt;/th&gt;
&lt;th&gt;With my patch&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;194,910 ms&lt;/td&gt;
&lt;td&gt;114 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8 (default)&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;only about 1.5x faster&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;At 8 the gain was small. My reading: about 11 tables survive, and 11 is more than 8, so the survivors still get split. At 9 the groups happened to fall the right way. That is the same "holes" behaviour from the first article.&lt;/p&gt;

&lt;p&gt;The speed was the easy part.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. The 8 bugs in my own patch
&lt;/h2&gt;

&lt;p&gt;Correct results were the real work. Here is what I found, in plain words.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;#&lt;/th&gt;
&lt;th&gt;What my patch did wrong&lt;/th&gt;
&lt;th&gt;What happened&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;Treated &lt;code&gt;t.a = t.b&lt;/code&gt; as proof that &lt;code&gt;t&lt;/code&gt; matches one row&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Wrong rows.&lt;/strong&gt; A test returned 2 rows instead of 6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Left a dead entry behind when it removed a join&lt;/td&gt;
&lt;td&gt;Planner error: "so where are the outer joins?"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Opened an index without taking a lock&lt;/td&gt;
&lt;td&gt;Assertion failure on debug builds&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Missed a table used through a join alias&lt;/td&gt;
&lt;td&gt;"no relation entry for relid N"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Missed a table needed by a placeholder expression with no column of it&lt;/td&gt;
&lt;td&gt;Same error&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;Removed the target table of a &lt;code&gt;MERGE&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Broken &lt;code&gt;MERGE&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;Ignored references from &lt;code&gt;LATERAL&lt;/code&gt; subqueries&lt;/td&gt;
&lt;td&gt;Same error, in system views&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;Registered the new setting without a flag that &lt;code&gt;EXPLAIN&lt;/code&gt; needs&lt;/td&gt;
&lt;td&gt;One regression test failed&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Most of these sat in checks I had rewritten myself. I wrote my own "is this table used?" walker and my own "is this row unique?" check. Postgres already has both, tested over many years.&lt;/p&gt;

&lt;p&gt;The lesson: a table can be "used" in ways a simple walker misses. Join aliases, placeholder expressions, references from subqueries and the target of a &lt;code&gt;MERGE&lt;/code&gt; are four of them. Postgres's own bookkeeping already knows all four.&lt;/p&gt;

&lt;p&gt;Bug 1 is the dangerous kind. The others fail loudly. That one returned wrong rows, and nothing complained.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Tom Lane's commit
&lt;/h2&gt;

&lt;p&gt;Then I read commit &lt;code&gt;2ebf25e7d7&lt;/code&gt;, committed on 28 August 2026. It uses the same core idea: replace the LEFT JOIN node by its left input.&lt;/p&gt;

&lt;p&gt;The purpose is different. He was fixing &lt;a href="https://www.postgresql.org/message-id/19560-54cd7ede78d5e355%40postgresql.org" rel="noopener noreferrer"&gt;bug #19560&lt;/a&gt;, where a query silently lost a &lt;code&gt;WHERE&lt;/code&gt; condition and returned wrong results. The faster grouping is a side effect.&lt;/p&gt;

&lt;p&gt;The approach is also different:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Let Postgres decide what is removable, using its own &lt;code&gt;join_is_removable&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Edit the jointree to cut those joins out.&lt;/li&gt;
&lt;li&gt;Throw away the derived data and restart planning, so the grouping runs on the survivors.&lt;/li&gt;
&lt;li&gt;Repeat until nothing more can be removed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It was back-patched to 16. It shipped in 16.16, 17.12 and 18.7 on 12 November 2026. It never went into 15.&lt;/p&gt;

&lt;p&gt;So the part where my patch failed, the safety proof, is the part he reuses. The price is the restart. My version avoided that cost, but it paid with the bugs above. I did not submit mine.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. The proof: one commit, nothing else changed
&lt;/h2&gt;

&lt;p&gt;"18.7 is fast" is not proof. 18.7 holds other changes too. So I ran an A/B on a copy of the real data, with default limits (&lt;code&gt;join_collapse_limit&lt;/code&gt; = &lt;code&gt;from_collapse_limit&lt;/code&gt; = 8):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Build&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Official 18.6&lt;/td&gt;
&lt;td&gt;about 2 minutes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;My own build of 18.6, plain&lt;/td&gt;
&lt;td&gt;over 2 minutes (153 s, 358 s, 678 s)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;My own build of 18.6 with &lt;strong&gt;only&lt;/strong&gt; Tom's commit&lt;/td&gt;
&lt;td&gt;2.1 s on the first run, then 0.3 s and 0.2 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Official 18.7&lt;/td&gt;
&lt;td&gt;about 40 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Both own builds came from the same source tree. I built it plain, applied the 18.6 back-port of the commit to the same tree, and rebuilt. Then I ran both servers on the same copy of the data.&lt;/p&gt;

&lt;p&gt;The plan shows why. Without the commit, the child table is read by a scan over the whole account:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bitmap Heap Scan on enrollment      (all ~51k rows of the account)
  Recheck Cond: (&amp;lt;account filter&amp;gt; AND NOT is_deleted)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the commit, the same table is read through an index, one lookup per course:&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 on enrollment
  Index Cond: (course_id = course.id)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Index Cond: (course_id = course.id)&lt;/code&gt; only exists when &lt;code&gt;course&lt;/code&gt; and &lt;code&gt;enrollment&lt;/code&gt; sit in the same group. With the commit, my two tables finally landed together. No setting was needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Two more numbers
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;15.8 never gets the fix.&lt;/strong&gt; On 15.8 the shipped workaround is still &lt;code&gt;SET LOCAL join_collapse_limit = 64&lt;/code&gt;, with &lt;code&gt;from_collapse_limit&lt;/code&gt; left at 8. Raising &lt;code&gt;from_collapse_limit&lt;/code&gt; too made this query worse. On the 15.8 copy this gave about 24 ms. The setting stays scoped to one transaction.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What does the restart cost?&lt;/strong&gt; A reader asked this under my first LinkedIn post. Part of the answer, from official images, same data, fresh statistics:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Version&lt;/th&gt;
&lt;th&gt;Planning time&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;18.7&lt;/td&gt;
&lt;td&gt;0.5 to 0.8 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;18.6&lt;/td&gt;
&lt;td&gt;0.9 to 2.3 s&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;On these views, 18.7 planned faster, not slower. That is not the full answer. Richard Guo measured the bad case on the mailing list. A chain of 64 removals that each depend on the last took 20.9 ms instead of 1.98 ms, because every link costs one full pass. Views on views can have that shape. I have not yet counted how many restart passes my views need. That is next.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Caveats
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;My own builds are slower and noisier than the official images, and plain 18.6 got slower on each run. Compare the ratio and the plan, not the milliseconds.&lt;/li&gt;
&lt;li&gt;The own builds show a collation version warning, because the locale files came from the official image. It applies equally to both builds.&lt;/li&gt;
&lt;li&gt;The data is a copy of real data. No names appear here, and the tables are renamed.&lt;/li&gt;
&lt;li&gt;An early run of mine compared 15.8 with 18.7 and gave the wrong cause. The fresh statistics differed, and the collapse limit made no difference on 15.8. I dropped that comparison and ran the 18.6 A/B instead.&lt;/li&gt;
&lt;li&gt;This is one query on one data set. It shows what the commit does here. It does not claim every nested view gets faster.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I'd tell you
&lt;/h2&gt;

&lt;p&gt;If you see a fast fix, find the cause. If you have a patch, look for a better one before you submit. Mine was right about the idea and wrong in eight places, and a Postgres committer had already done the same job with the checks the planner trusts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Planner folks: beyond the regression suite, how do you test a change like this for wrong results?&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Sources&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Commit &lt;code&gt;2ebf25e7d7&lt;/code&gt;: &lt;a href="https://github.com/postgres/postgres/commit/2ebf25e7d70a8fce31ace78d723fa9271ab8af72" rel="noopener noreferrer"&gt;https://github.com/postgres/postgres/commit/2ebf25e7d70a8fce31ace78d723fa9271ab8af72&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Bug #19560 report: &lt;a href="https://www.postgresql.org/message-id/19560-54cd7ede78d5e355%40postgresql.org" rel="noopener noreferrer"&gt;https://www.postgresql.org/message-id/19560-54cd7ede78d5e355%40postgresql.org&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Discussion thread: &lt;a href="https://www.postgresql.org/message-id/flat/1186816.1784573544%40sss.pgh.pa.us" rel="noopener noreferrer"&gt;https://www.postgresql.org/message-id/flat/1186816.1784573544%40sss.pgh.pa.us&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;2010 thread, "problem with from_collapse_limit and joined views": &lt;a href="https://www.postgresql.org/message-id/201011241937.08622%40Mail-Followup-To" rel="noopener noreferrer"&gt;https://www.postgresql.org/message-id/201011241937.08622%40Mail-Followup-To&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Tom Lane, 2017 reply: &lt;a href="https://postgresql.org/message-id/20264.1486185381%40sss.pgh.pa.us" rel="noopener noreferrer"&gt;https://postgresql.org/message-id/20264.1486185381%40sss.pgh.pa.us&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;2010 thread on join removal: &lt;a href="https://www.postgresql.org/message-id/flat/8212.1269701421%40sss.pgh.pa.us" rel="noopener noreferrer"&gt;https://www.postgresql.org/message-id/flat/8212.1269701421%40sss.pgh.pa.us&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;PostgreSQL 18.7 release notes: &lt;a href="https://www.postgresql.org/docs/release/18.7/" rel="noopener noreferrer"&gt;https://www.postgresql.org/docs/release/18.7/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;First article: &lt;a href="https://depthfirst.hashnode.dev/changing-one-number-from-32-to-34-made-a-postgres-query-6-000-faster-here-s-exactly-why" rel="noopener noreferrer"&gt;https://depthfirst.hashnode.dev/changing-one-number-from-32-to-34-made-a-postgres-query-6-000-faster-here-s-exactly-why&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>performance</category>
      <category>opensource</category>
    </item>
    <item>
      <title>P vs NP for developers who never learned it, and what the AI 3-SAT news really means</title>
      <dc:creator>Payyavula Balanagaraju</dc:creator>
      <pubDate>Mon, 16 Nov 2026 09:12:15 +0000</pubDate>
      <link>https://dev.to/payyavula_balanagaraju_6d/p-vs-np-for-developers-who-never-learned-it-and-what-the-ai-3-sat-news-really-means-a77</link>
      <guid>https://dev.to/payyavula_balanagaraju_6d/p-vs-np-for-developers-who-never-learned-it-and-what-the-ai-3-sat-news-really-means-a77</guid>
      <description>&lt;h2&gt;
  
  
  Why I wrote this
&lt;/h2&gt;

&lt;p&gt;I watched a video about AI and "NP problems" and understood almost none of it. I've been a backend developer for years and had never heard of P vs NP. So I went back to zero and learned it one small step at a time. The deepest parts (how one problem is turned into another) still lost me, so this covers only what I actually understood. It's written for anyone starting where I started.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: How fast is "fast"?
&lt;/h2&gt;

&lt;p&gt;Everything here depends on one idea: how the work grows when the input grows. Take n = 1,000 items:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Growth&lt;/th&gt;
&lt;th&gt;Steps&lt;/th&gt;
&lt;th&gt;Feels like&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;n&lt;/td&gt;
&lt;td&gt;1,000&lt;/td&gt;
&lt;td&gt;instant&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;n²&lt;/td&gt;
&lt;td&gt;1,000,000&lt;/td&gt;
&lt;td&gt;instant&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;n³&lt;/td&gt;
&lt;td&gt;1,000,000,000&lt;/td&gt;
&lt;td&gt;about a second&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2ⁿ&lt;/td&gt;
&lt;td&gt;a number with 302 digits&lt;/td&gt;
&lt;td&gt;never finishes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The first three are &lt;strong&gt;polynomial&lt;/strong&gt;: n multiplied by itself a fixed number of times. The last is &lt;strong&gt;exponential&lt;/strong&gt;: every extra item doubles the work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: P, problems we can solve fast
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;P&lt;/strong&gt; is the group of yes/no problems a computer can solve in polynomial time. You use them every day:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sorting a list (every &lt;code&gt;ORDER BY&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Finding the shortest route on a map&lt;/li&gt;
&lt;li&gt;Comparing two files (&lt;code&gt;git diff&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Checking whether a number is prime (proven to be in P only in 2002)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;P is not a tool. It's a label that says "a fast, exact method exists for this."&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: NP, problems where a YES can be checked fast
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;NP&lt;/strong&gt; is the group of yes/no problems where, if the answer is YES, someone can hand you a short proof and you can check it fast.&lt;/p&gt;

&lt;p&gt;Example, &lt;strong&gt;Subset Sum&lt;/strong&gt;: does some subset of {10, 20, 30, 40, 50, 60} add up to 100?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Someone hands you &lt;strong&gt;{10, 30, 60}&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;You check: 10 + 30 + 60 = 100. Done in two additions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Checking is easy. &lt;strong&gt;Finding&lt;/strong&gt; that subset is the hard part: 6 numbers have 64 subsets, and 60 numbers have 2⁶⁰ subsets, about 36 years of work at a billion checks a second.&lt;/p&gt;

&lt;p&gt;Two common mix-ups:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;NP does not mean "Not Polynomial."&lt;/strong&gt; It means "Nondeterministic Polynomial": a machine that always guessed right would solve it fast.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NP is lopsided.&lt;/strong&gt; It promises a fast check for YES answers only. "Is there &lt;em&gt;no&lt;/em&gt; subset that adds up to 100?" has no known short proof.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 4: The open question
&lt;/h2&gt;

&lt;p&gt;Every problem in P is also in NP: if you can solve it fast, you can check it fast by just solving it. So P sits inside NP.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌──────────── NP: a YES can be checked fast ────────────┐
│   ┌──── P: can be solved fast ────┐                   │
│   │ sorting, shortest path, ...   │   Subset Sum,     │
│   └───────────────────────────────┘   Sudoku, ...     │
└───────────────────────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;P vs NP asks: is the outer box actually the same as the inner box?&lt;/strong&gt; If an answer is easy to check, is it always easy to find? Nobody knows. Most experts believe not, but nobody has proven it, for more than 50 years.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: The hardest problems in NP
&lt;/h2&gt;

&lt;p&gt;Some NP problems are special: &lt;strong&gt;NP-complete&lt;/strong&gt;. Every NP problem can be translated into any one of them, quickly. So a fast method for just one of them would give a fast method for all of NP, and P would equal NP.&lt;/p&gt;

&lt;p&gt;Familiar NP-complete problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;3-SAT&lt;/strong&gt;: can a list of "this OR that OR the other" rules all be true at once?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sudoku&lt;/strong&gt; (on large grids)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exam timetabling&lt;/strong&gt; with 3 or more slots and clashes&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Delivery routes&lt;/strong&gt;: can a van visit every shop in under X km?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Strange detail: the same problems in a slightly easier form are fast. Timetabling with &lt;strong&gt;2&lt;/strong&gt; slots is easy. With &lt;strong&gt;3&lt;/strong&gt;, it's NP-complete.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 6: The base of the exponent matters a lot
&lt;/h2&gt;

&lt;p&gt;Exponential doesn't always mean 2ⁿ. For 3-SAT, researchers had already got the best known method down to about &lt;strong&gt;1.3ⁿ&lt;/strong&gt;. That sounds like a small change. At a billion steps a second:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;n = 100&lt;/th&gt;
&lt;th&gt;n = 300&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2ⁿ&lt;/td&gt;
&lt;td&gt;about 40 trillion years&lt;/td&gt;
&lt;td&gt;hopeless&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.3ⁿ&lt;/td&gt;
&lt;td&gt;about 4 minutes&lt;/td&gt;
&lt;td&gt;about 10³⁴ steps, hopeless again&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So 1.3ⁿ is far better than 2ⁿ, and still exponential.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 7: The AI news
&lt;/h2&gt;

&lt;p&gt;In October 2026, OpenAI and Anthropic each said their AI had found, on 16 October, a method for 3-SAT that grows &lt;strong&gt;slower than cⁿ for every c &amp;gt; 1&lt;/strong&gt;. The technical word is &lt;em&gt;subexponential&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;If it holds, it breaks the &lt;strong&gt;Exponential Time Hypothesis&lt;/strong&gt; (Impagliazzo, Paturi and Zane, 1998/1999): the assumption that 3-SAT needs at least cⁿ time for some fixed c &amp;gt; 1. Many other results were built on that assumption, which is why theorists call it big news.&lt;/p&gt;

&lt;p&gt;The ladder looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2ⁿ  →  1.3ⁿ  →  subexponential (the new claim)  →  polynomial (= P)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The claim moves 3-SAT one rung up. P vs NP is about the top rung.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it does NOT mean
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;P vs NP is still unsolved.&lt;/strong&gt; Subexponential is still far from polynomial.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No practical speedup.&lt;/strong&gt; Ryan Williams, who leads the expert group checking it, told the New York Times there are no immediate practical applications.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No help for other NP problems.&lt;/strong&gt; Lance Fortnow noted it gives nothing new for Subset Sum or the Traveling Salesman. Translating one problem into another makes the input bigger, and for fine differences like this, that growth eats the gain.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Not fully verified yet.&lt;/strong&gt; Both labs say their proofs were machine-checked (in Lean), but as of early November no independent human review had finished.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What changed for me as a developer
&lt;/h2&gt;

&lt;p&gt;Honestly: for daily backend work, I don't need most of this. What I do need is Step 1, how work grows with input. That one idea now explains things I see every week.&lt;/p&gt;

&lt;p&gt;One example: choosing the order in which to join tables is a hard search problem. With 12 tables there are already 479,001,600 possible left-to-right orders (12!). That's why Postgres, by default, stops searching exhaustively at 12 tables (&lt;code&gt;geqo_threshold&lt;/code&gt;) and switches to a "good enough" genetic search.&lt;/p&gt;

&lt;p&gt;The bigger lesson for me: when a headline goes over my head, the basics are the shortcut, not the detour.&lt;/p&gt;

&lt;p&gt;What's something you learned surprisingly late in your career?&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;New York Times (via The Star), 9 Nov 2026: &lt;a href="https://www.thestar.com.my/tech/tech-news/2026/11/09/how-ai-just-blew-up-a-key-tenet-of-computer-science" rel="noopener noreferrer"&gt;How AI just blew up a key tenet of computer science&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;HackerNoon: &lt;a href="https://hackernoon.com/openai-and-anthropic-claim-ai-has-disproved-a-27-year-old-computer-science-hypothesis" rel="noopener noreferrer"&gt;OpenAI and Anthropic claim AI has disproved a 27-year-old computer science hypothesis&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Emergent Mind: &lt;a href="https://www.emergentmind.com/exponential-time-hypothesis" rel="noopener noreferrer"&gt;Exponential Time Hypothesis explainer&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;PostgreSQL docs: &lt;code&gt;geqo_threshold&lt;/code&gt; (default 12)&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>computerscience</category>
      <category>algorithms</category>
      <category>beginners</category>
      <category>ai</category>
    </item>
    <item>
      <title>Save the moment in UTC, save the place by name: a beginner's guide to dates and timezones</title>
      <dc:creator>Payyavula Balanagaraju</dc:creator>
      <pubDate>Sat, 07 Nov 2026 19:36:01 +0000</pubDate>
      <link>https://dev.to/payyavula_balanagaraju_6d/save-the-moment-in-utc-save-the-place-by-name-a-beginners-guide-to-dates-and-timezones-1a1g</link>
      <guid>https://dev.to/payyavula_balanagaraju_6d/save-the-moment-in-utc-save-the-place-by-name-a-beginners-guide-to-dates-and-timezones-1a1g</guid>
      <description>&lt;p&gt;If your app shows a date or a time to people in more than one place, this guide is for you. You don't need to&lt;br&gt;
know anything about timezones to read it. By the end you'll know the three rules most teams follow, why they&lt;br&gt;
work, and the one case where they are not enough.&lt;/p&gt;

&lt;p&gt;I learned all of this the hard way, so let me start there.&lt;/p&gt;
&lt;h2&gt;
  
  
  How I learned this
&lt;/h2&gt;

&lt;p&gt;When we started, we saved times the simple way: the local time someone typed, with no timezone attached.&lt;br&gt;
For example &lt;code&gt;2026-12-05 10:00&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It looked fine. Then it broke quietly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Courses closed an hour early, or an hour late.&lt;/li&gt;
&lt;li&gt;Early-bird prices ended at the wrong moment.&lt;/li&gt;
&lt;li&gt;A page showed a different time from the one that was saved.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The cause was the same every time. &lt;code&gt;2026-12-05 10:00&lt;/code&gt; is not a moment. It's 10:00 &lt;em&gt;somewhere&lt;/em&gt;. Without the&lt;br&gt;
"where", every server, browser and job read it in its own timezone, and they didn't agree.&lt;/p&gt;

&lt;p&gt;So I read the official sources, asked a lot of questions, and we changed how we store time. Here is what I&lt;br&gt;
wish someone had told me on day one.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 1: a moment and a wall clock are two different things
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;moment&lt;/strong&gt; is one point in time, the same everywhere on Earth. "When the rocket launched."&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;wall-clock time&lt;/strong&gt; is what a clock on the wall shows in one place. "10:00 in Vancouver."&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The same moment shows different wall-clock times in different places. 10:00 in Vancouver is 22:30 in&lt;br&gt;
Bengaluru on the same moment.&lt;/p&gt;

&lt;p&gt;Bugs happen when you save a wall-clock time and treat it like a moment.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 2: UTC is the reference clock
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;UTC&lt;/strong&gt; (Coordinated Universal Time) is the world's reference clock. It never changes for daylight saving.&lt;br&gt;
Every place on Earth is described as "UTC plus or minus some hours" at a given moment:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Place&lt;/th&gt;
&lt;th&gt;Offset today (Nov 2026)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Bengaluru&lt;/td&gt;
&lt;td&gt;UTC+5:30&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dublin&lt;/td&gt;
&lt;td&gt;UTC+0 in winter, UTC+1 in summer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Vancouver&lt;/td&gt;
&lt;td&gt;UTC−7, all year (new in 2026, see below)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you save every moment in UTC, every server, browser and background job reads the same value. That fixes the&lt;br&gt;
"which timezone did this mean?" problem at the root.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 3: save the place by name, never by offset
&lt;/h2&gt;

&lt;p&gt;An &lt;strong&gt;offset&lt;/strong&gt; like &lt;code&gt;−07:00&lt;/code&gt; is a number. A &lt;strong&gt;timezone name&lt;/strong&gt; like &lt;code&gt;America/Vancouver&lt;/code&gt; is a place with a&lt;br&gt;
history of rules.&lt;/p&gt;

&lt;p&gt;Offsets change. Dublin is &lt;code&gt;+00:00&lt;/code&gt; in January and &lt;code&gt;+01:00&lt;/code&gt; in July. Vancouver was &lt;code&gt;−08:00&lt;/code&gt; every winter until&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If you save &lt;code&gt;−08:00&lt;/code&gt;, you've saved a fact that was true for one date only.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you save &lt;code&gt;America/Vancouver&lt;/code&gt;, the software works out the right offset for any date: past, present or&lt;br&gt;
future, daylight saving included. You write no daylight-saving code at all.&lt;/p&gt;

&lt;p&gt;Names look like &lt;code&gt;Continent/City&lt;/code&gt;: &lt;code&gt;America/Vancouver&lt;/code&gt;, &lt;code&gt;America/Edmonton&lt;/code&gt;, &lt;code&gt;Europe/Dublin&lt;/code&gt;, &lt;code&gt;Asia/Kolkata&lt;/code&gt;.&lt;br&gt;
(Note: it's &lt;code&gt;Asia/Kolkata&lt;/code&gt; and &lt;code&gt;Europe/Dublin&lt;/code&gt;, not "India/Calcutta" or "Ireland/Dublin". Use the official&lt;br&gt;
list.)&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 4: who keeps these rules? The IANA timezone database
&lt;/h2&gt;

&lt;p&gt;Somebody has to record that "Vancouver stopped changing clocks in 2026". That somebody is the &lt;strong&gt;IANA Time Zone&lt;br&gt;
Database&lt;/strong&gt; (also called tzdata or the Olson database), a small group of volunteers who publish the rules for&lt;br&gt;
every place on Earth, going back decades.&lt;/p&gt;

&lt;p&gt;When a government changes its clocks, the database ships a new release. Operating systems, Java, Postgres,&lt;br&gt;
Node.js and libraries like moment-timezone copy that data into themselves.&lt;/p&gt;

&lt;p&gt;That last part matters: &lt;strong&gt;the timezone database is a dependency, like any library.&lt;/strong&gt; If you don't update it,&lt;br&gt;
your app uses old rules.&lt;/p&gt;

&lt;p&gt;And you probably have more than one copy:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the server's operating system,&lt;/li&gt;
&lt;li&gt;your runtime (Node.js, Java),&lt;/li&gt;
&lt;li&gt;your date library (moment-timezone bundles its own),&lt;/li&gt;
&lt;li&gt;your database (Postgres ships its own copy unless it was built to use the system's),&lt;/li&gt;
&lt;li&gt;every user's browser.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They can disagree for weeks after a change.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 5: what changed in Canada in 2026 (a real example)
&lt;/h2&gt;

&lt;p&gt;This year four parts of Canada stopped changing their clocks. The database needed four releases to keep up:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Release&lt;/th&gt;
&lt;th&gt;Date&lt;/th&gt;
&lt;th&gt;Change&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2026b&lt;/td&gt;
&lt;td&gt;22 Apr 2026&lt;/td&gt;
&lt;td&gt;British Columbia: UTC−7 all year&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026c&lt;/td&gt;
&lt;td&gt;8 Jul 2026&lt;/td&gt;
&lt;td&gt;Alberta: UTC−6 all year&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026d&lt;/td&gt;
&lt;td&gt;11 Sep 2026&lt;/td&gt;
&lt;td&gt;Northwest Territories: UTC−6 all year&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026e&lt;/td&gt;
&lt;td&gt;29 Sep 2026&lt;/td&gt;
&lt;td&gt;Manitoba: UTC−5 all year&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;British Columbia moved its clocks forward for the last time on 8 March 2026. On 1 November, when clocks would&lt;br&gt;
normally go back, they stayed put.&lt;/p&gt;
&lt;h3&gt;
  
  
  Why Vancouver now shows "MST"
&lt;/h3&gt;

&lt;p&gt;Short labels like PST and MST are not decoration. Each one names an offset:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Label&lt;/th&gt;
&lt;th&gt;Means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;PST&lt;/td&gt;
&lt;td&gt;Pacific Standard Time, UTC−8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PDT&lt;/td&gt;
&lt;td&gt;Pacific Daylight Time, UTC−7 (daylight saving)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MST&lt;/td&gt;
&lt;td&gt;Mountain Standard Time, UTC−7&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Vancouver is now at UTC−7 all year, with no daylight saving. PST would be an hour wrong. PDT would say daylight&lt;br&gt;
saving is on, and it no longer exists there. So the database labels it &lt;strong&gt;MST&lt;/strong&gt;, the long-standing name for&lt;br&gt;
UTC−7 standard time in North America. For the same reason, Edmonton (now UTC−6 all year) is labelled &lt;strong&gt;CST&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Two honest notes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The provinces use their own names: British Columbia calls it "Pacific Time" (PCT), Alberta calls it "Alberta
Time". The database's 2026b notes say a future release may change the label.&lt;/li&gt;
&lt;li&gt;Windows and browsers don't always show the database's label. Browsers use another project (Unicode CLDR)
for display names, so you may see "GMT-7" or a long name instead.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Our choice was to show what the database says. If the database changes the label later, our screens change&lt;br&gt;
with the next update, with no code. If you override labels by hand, you own that list forever. And never&lt;br&gt;
search-and-replace "MST" with something else: Arizona really is MST all year, and so is Denver in winter.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 6: what this looks like in code
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;In the database (Postgres):&lt;/strong&gt; use &lt;code&gt;timestamptz&lt;/code&gt; for moments, and keep the zone name next to it.&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;course&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;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;starts_at&lt;/span&gt;  &lt;span class="n"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;-- a moment; Postgres stores it as UTC&lt;/span&gt;
  &lt;span class="n"&gt;time_zone&lt;/span&gt;  &lt;span class="nb"&gt;text&lt;/span&gt;        &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;   &lt;span class="c1"&gt;-- IANA name, e.g. 'America/Vancouver'&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;-- Show it as the local wall-clock time of the course's place&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;starts_at&lt;/span&gt; &lt;span class="k"&gt;AT&lt;/span&gt; &lt;span class="nb"&gt;TIME&lt;/span&gt; &lt;span class="k"&gt;ZONE&lt;/span&gt; &lt;span class="n"&gt;time_zone&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;local_start&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;course&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Despite its name, &lt;code&gt;timestamptz&lt;/code&gt; doesn't store a timezone. It stores the moment in UTC and converts it for&lt;br&gt;
display using your session's &lt;code&gt;TimeZone&lt;/code&gt; setting. The plain &lt;code&gt;timestamp&lt;/code&gt; type (without time zone) is the&lt;br&gt;
"local time with no zone" trap from the start of this guide.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In JavaScript:&lt;/strong&gt; a &lt;code&gt;Date&lt;/code&gt; is already a moment (milliseconds since 1970 in UTC). Convert only when you display it.&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="nx"&gt;startsAt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2026-12-05T17:00:00Z&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// the moment, from the API, in UTC&lt;/span&gt;

&lt;span class="nx"&gt;startsAt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLocaleString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;en-CA&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;timeZone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;America/Vancouver&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// the place, by name&lt;/span&gt;
  &lt;span class="na"&gt;dateStyle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;medium&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;timeStyle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;short&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="c1"&gt;// 10:00 a.m. on 5 Dec 2026 in Vancouver (with up-to-date timezone data)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In background jobs:&lt;/strong&gt; schedule in UTC. "Close this course at 2026-12-05T17:00:00Z" fires at the right moment&lt;br&gt;
whether the course is in Vancouver, Dublin or Bengaluru. Our job queue only ever deals in UTC, and that&lt;br&gt;
removed a whole class of bugs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 7: the one case where UTC alone breaks: future events
&lt;/h2&gt;

&lt;p&gt;Here's the trap that almost nobody mentions in beginner guides.&lt;/p&gt;

&lt;p&gt;Say in February 2026 someone creates an event: &lt;strong&gt;10:00 on 5 December 2026, in Vancouver.&lt;/strong&gt; At that time the&lt;br&gt;
rules said Vancouver would be at UTC−8 in December. So the app saved &lt;code&gt;2026-12-05T18:00:00Z&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then British Columbia changed its law. Vancouver will be at UTC−7 in December instead. Now that saved moment,&lt;br&gt;
&lt;code&gt;18:00Z&lt;/code&gt;, shows as &lt;strong&gt;11:00&lt;/strong&gt; in Vancouver. The event is an hour off, and nobody touched it.&lt;/p&gt;

&lt;p&gt;The person meant "10:00 on the wall clock in Vancouver". We saved a moment computed with rules that later&lt;br&gt;
changed.&lt;/p&gt;

&lt;p&gt;What to do about it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;For things that already happened&lt;/strong&gt; (payments, logins, check-ins): UTC is exactly right. The past doesn't
change.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;For future events people plan by the wall clock&lt;/strong&gt; (a class at 10:00 local): also keep the local time and
the zone name the person chose, and recompute the UTC moment from them when the rules change. Or at least
check future events after every timezone-data update.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Jon Skeet wrote the classic piece on this: &lt;em&gt;Storing UTC is not a silver bullet&lt;/em&gt; (link at the end). Read it&lt;br&gt;
once you're comfortable with the basics.&lt;/p&gt;

&lt;h2&gt;
  
  
  A checklist to keep
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Save moments in UTC (&lt;code&gt;timestamptz&lt;/code&gt; in Postgres, ISO strings ending in &lt;code&gt;Z&lt;/code&gt; in APIs).&lt;/li&gt;
&lt;li&gt;Save the place as an IANA name (&lt;code&gt;America/Vancouver&lt;/code&gt;), never as an offset.&lt;/li&gt;
&lt;li&gt;Convert to local time only at the last step, when a person reads it.&lt;/li&gt;
&lt;li&gt;Schedule jobs in UTC.&lt;/li&gt;
&lt;li&gt;Treat the timezone database as a dependency: update it, and know every copy you run.&lt;/li&gt;
&lt;li&gt;For future wall-clock events, keep the local time + zone name too, and re-check after rule changes.&lt;/li&gt;
&lt;li&gt;Show the label the database gives you. Don't hand-edit labels.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What I'd tell a new developer
&lt;/h2&gt;

&lt;p&gt;UTC tells you &lt;em&gt;when&lt;/em&gt;. The zone name tells you &lt;em&gt;where&lt;/em&gt;. You need both, and the local time is something you work&lt;br&gt;
out at the end, not something you store.&lt;/p&gt;

&lt;p&gt;What's the one timezone rule you wish someone had told you earlier?&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Sources&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;IANA Time Zone Database: &lt;a href="https://www.iana.org/time-zones" rel="noopener noreferrer"&gt;https://www.iana.org/time-zones&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;2026b release notes (British Columbia, the "MST" label): &lt;a href="https://www.iana.org/time-zones/releases/2026b" rel="noopener noreferrer"&gt;https://www.iana.org/time-zones/releases/2026b&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;2026c release notes (Alberta, "CST"): &lt;a href="https://www.iana.org/time-zones/releases/2026c" rel="noopener noreferrer"&gt;https://www.iana.org/time-zones/releases/2026c&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;2026e release notes (Manitoba): &lt;a href="https://www.iana.org/time-zones/releases/2026e" rel="noopener noreferrer"&gt;https://www.iana.org/time-zones/releases/2026e&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Time in British Columbia (PCT name, dates): &lt;a href="https://en.wikipedia.org/wiki/Time_in_British_Columbia" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/Time_in_British_Columbia&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;moment-timezone issue #1163 (2026 Canadian changes): &lt;a href="https://github.com/moment/moment-timezone/issues/1163" rel="noopener noreferrer"&gt;https://github.com/moment/moment-timezone/issues/1163&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;PostgreSQL date/time types: &lt;a href="https://www.postgresql.org/docs/current/datatype-datetime.html" rel="noopener noreferrer"&gt;https://www.postgresql.org/docs/current/datatype-datetime.html&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Jon Skeet, &lt;em&gt;Storing UTC is not a silver bullet&lt;/em&gt; (2019): &lt;a href="https://codeblog.jonskeet.uk/2019/03/27/storing-utc-is-not-a-silver-bullet/" rel="noopener noreferrer"&gt;https://codeblog.jonskeet.uk/2019/03/27/storing-utc-is-not-a-silver-bullet/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>postgres</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Changing one number from 32 to 34 made a Postgres query 6,000 faster. Here's exactly why.</title>
      <dc:creator>Payyavula Balanagaraju</dc:creator>
      <pubDate>Sun, 01 Nov 2026 21:58:23 +0000</pubDate>
      <link>https://dev.to/payyavula_balanagaraju_6d/changing-one-number-from-32-to-34-made-a-postgres-query-6000x-faster-heres-exactly-why-4p79</link>
      <guid>https://dev.to/payyavula_balanagaraju_6d/changing-one-number-from-32-to-34-made-a-postgres-query-6000x-faster-heres-exactly-why-4p79</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Who this is for:&lt;/strong&gt; anyone who has seen a Postgres query that should be fast but isn't, and EXPLAIN shows a bad plan without saying why. You don't need to know planner internals. Everything is explained from zero, with the real code lines.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The short version:&lt;/strong&gt; in Postgres 15 and every version released so far, the planner splits your joins into groups &lt;em&gt;before&lt;/em&gt; it removes the joins it doesn't need. If the two tables that need an index lookup land in different groups, the fast plan can't be built at all. &lt;code&gt;join_collapse_limit&lt;/code&gt; sets the group size, and the "safe" values are not a range: they have holes. Postgres 16+ gets a change that helps, but doesn't remove the problem completely.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  1. The problem
&lt;/h2&gt;

&lt;p&gt;A report on the platform I work on started timing out. One query, run with a few filters, took &lt;strong&gt;137,779 ms&lt;/strong&gt;: more than two minutes.&lt;/p&gt;

&lt;p&gt;The query reads from a view. That view is built on other views, which are built on more views (we call them layered views). It looks like one simple &lt;code&gt;SELECT ... FROM report_view WHERE ...&lt;/code&gt;. Underneath, it is not simple at all.&lt;/p&gt;

&lt;p&gt;EXPLAIN ANALYZE showed the slow part clearly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Nested Loop  (rows=1) (actual time=1112..139689 rows=1005)
  Join Filter: (enrollment.course_id = course.id)
  Rows Removed by Join Filter: 8,944,739
  -&amp;gt;  Bitmap Heap Scan on course             (estimated rows=1, actual rows=181)
  -&amp;gt;  Hash Left Join ...                     (loops=181)
        -&amp;gt;  Hash (rows=895194) (actual time=538.801) loops=181     ← 538.8 ms × 181 = 97.5 s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things were wrong at once:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The plan shape.&lt;/strong&gt; Postgres built &lt;em&gt;both&lt;/em&gt; sides of the join completely, then compared them row by row (&lt;code&gt;Join Filter&lt;/code&gt;) and threw away 8.9 million rows. The fast way would have been: take each of the 181 courses and &lt;em&gt;look up&lt;/em&gt; its enrollments through an index. In a plan that looks like &lt;code&gt;Index Cond: (course_id = course.id)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The row estimate.&lt;/strong&gt; Postgres expected &lt;strong&gt;1&lt;/strong&gt; course and got &lt;strong&gt;181&lt;/strong&gt;. It planned an expensive inner step thinking it would run once. It ran 181 times.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Fixing the plan shape alone was enough: the inner step drops to ~0.06 ms, so running it 181 times stops mattering. The rest of this article is about the plan shape, because that's the part EXPLAIN can't explain.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. The first fix worked, and that was the problem
&lt;/h2&gt;

&lt;p&gt;The first suggestion I got (from an AI assistant) was to raise one planner setting, &lt;code&gt;join_collapse_limit&lt;/code&gt;. It worked. The report became fast.&lt;/p&gt;

&lt;p&gt;But I didn't know &lt;em&gt;why&lt;/em&gt; it worked, so I tested every value from 8 to 140, three times each, on a copy of real data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; 8 10 12 14 16 18 20 22 24 26 28 30 32 34 ............ 140
 ✗  ✗  ✓  ✗  ✓  ✓  ✓  ✓  ✓  ✗  ✗  ✓  ✗  ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Values&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;Slow&lt;/strong&gt; (bad plan)&lt;/td&gt;
&lt;td&gt;8, 10, 14, 26, 28, 32&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;Fast&lt;/strong&gt; (index lookup)&lt;/td&gt;
&lt;td&gt;12, 16–24, 30, 34 and up&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;At &lt;strong&gt;32&lt;/strong&gt; the query took &lt;strong&gt;140,611 ms&lt;/strong&gt;. At &lt;strong&gt;34&lt;/strong&gt; it took &lt;strong&gt;23.9 ms&lt;/strong&gt;. That's almost 6,000× from changing one number by two.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This is not a threshold. It's a range with holes in it.&lt;/strong&gt; If the setting were simply "bigger is better", 14 couldn't be slow when 12 is fast. Nobody could tell me why, so I went to the Postgres source code.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Background: what the planner does with your joins (two minutes)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  3.1 Views disappear
&lt;/h3&gt;

&lt;p&gt;A view is saved SQL. When you query a view, Postgres pastes the view's SQL into your query before planning. Views built on views get pasted recursively. Our one-line query became &lt;strong&gt;about 435 table references&lt;/strong&gt;: one inner view alone contributed 121, and it appeared three times.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.2 Postgres can throw away useless LEFT JOINs ("join removal")
&lt;/h3&gt;

&lt;p&gt;Most of those 435 tables aren't needed for this report. Postgres can remove a LEFT JOIN if &lt;strong&gt;all&lt;/strong&gt; of these are true (&lt;code&gt;join_is_removable&lt;/code&gt;, &lt;code&gt;src/backend/optimizer/plan/analyzejoins.c:159&lt;/code&gt; in 15.8):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It's a &lt;strong&gt;LEFT&lt;/strong&gt; join.&lt;/li&gt;
&lt;li&gt;The right side is &lt;strong&gt;one single table&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;That table can match &lt;strong&gt;at most one row&lt;/strong&gt;, proven by a primary key or unique index on the join columns.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No column&lt;/strong&gt; of that table is used anywhere above the join: SELECT, WHERE, ORDER BY, or another join's ON.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If all four hold, removing the table can't change the result, so Postgres drops it. In our query, removal cut ~435 references down to &lt;strong&gt;about 11&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Rule 4 is why the columns you select matter. With 2 columns selected, only &lt;strong&gt;3&lt;/strong&gt; tables survived and the query was fast at the default settings. With the report's &lt;strong&gt;27&lt;/strong&gt; columns, &lt;strong&gt;10–11&lt;/strong&gt; survived and it was slow. Each extra column can bring back a table that would otherwise be removed.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.3 Postgres limits how hard it searches
&lt;/h3&gt;

&lt;p&gt;To join N tables, the planner tries different orders. The number of possible orders grows like N!:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Tables&lt;/th&gt;
&lt;th&gt;Possible orders (≈ N!)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;24&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;40,320&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;td&gt;~40 million&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So Postgres protects itself with two settings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;join_collapse_limit&lt;/code&gt;&lt;/strong&gt; (default &lt;strong&gt;8&lt;/strong&gt;): tables written with explicit &lt;code&gt;JOIN&lt;/code&gt; syntax are grouped into lists of at most this many items. The planner searches join orders &lt;strong&gt;inside each list&lt;/strong&gt;, and treats each finished list as one fixed piece.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;geqo_threshold&lt;/code&gt;&lt;/strong&gt; (default &lt;strong&gt;12&lt;/strong&gt;): if a single list has 12 or more items, Postgres stops searching exhaustively and uses &lt;strong&gt;GEQO&lt;/strong&gt;, a genetic (randomised) optimiser. Plans can then change from run to run.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  4. The real cause: the order of two steps
&lt;/h2&gt;

&lt;p&gt;Here is the order inside the planner's main function, &lt;code&gt;query_planner()&lt;/code&gt;, in Postgres 15.8 (&lt;code&gt;src/backend/optimizer/plan/planmain.c&lt;/code&gt;):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Line&lt;/th&gt;
&lt;th&gt;Call&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;184&lt;/td&gt;
&lt;td&gt;&lt;code&gt;joinlist = deconstruct_jointree(root);&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Walks the whole join tree &lt;strong&gt;once&lt;/strong&gt; and does two jobs in that walk: (A) collects the facts the planner needs (which columns are used where, what each join condition looks like), and (B) &lt;strong&gt;builds the join list, applying &lt;code&gt;join_collapse_limit&lt;/code&gt;&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;221&lt;/td&gt;
&lt;td&gt;&lt;code&gt;joinlist = remove_useless_joins(root, joinlist);&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Then&lt;/strong&gt; removes the useless LEFT JOINs&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;The join list is built at line 184, counting all ~435 tables. The useless ones are removed at line 221, and the join list is never rebuilt.&lt;/strong&gt; That's the whole problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  4.1 How the join list is built
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;deconstruct_recurse&lt;/code&gt; visits the join tree bottom-up. At every &lt;code&gt;JOIN&lt;/code&gt; it has the list from the left side and the list from the right side, and applies one rule (&lt;code&gt;src/backend/optimizer/plan/initsplan.c:1037-1058&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;else&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;list_length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;leftjoinlist&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;list_length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rightjoinlist&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;join_collapse_limit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* OK to combine subproblems */&lt;/span&gt;
    &lt;span class="n"&gt;joinlist&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;list_concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;leftjoinlist&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rightjoinlist&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* can't combine, but needn't force join order above here */&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="n"&gt;joinlist&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;list_make2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;leftpart&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rightpart&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;In plain words:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;left items + right items ≤ limit&lt;/strong&gt; → merge them into one flat list (all orders will be tried across them);&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;otherwise&lt;/strong&gt; → don't merge; return exactly two items, the left side as a &lt;strong&gt;sub-list&lt;/strong&gt; and the right side.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A tiny example. &lt;code&gt;A JOIN B JOIN C&lt;/code&gt; with a limit of 2 (Postgres reads it as &lt;code&gt;(A JOIN B) JOIN C&lt;/code&gt;):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;JOIN node&lt;/th&gt;
&lt;th&gt;Left list&lt;/th&gt;
&lt;th&gt;Right list&lt;/th&gt;
&lt;th&gt;Total&lt;/th&gt;
&lt;th&gt;≤ 2?&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;A JOIN B&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[A]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[B]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[A, B]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;(…) JOIN C&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[A, B]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[C]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[[A, B], C]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The result &lt;code&gt;[[A, B], C]&lt;/code&gt; means: plan A+B together first, on their own; then join that finished piece with C. The order "A with C first" is never even considered.&lt;/p&gt;

&lt;h3&gt;
  
  
  4.2 Why a sub-list blocks the fast plan
&lt;/h3&gt;

&lt;p&gt;When the planner meets a sub-list, it plans it &lt;strong&gt;completely and alone&lt;/strong&gt; first (&lt;code&gt;make_rel_from_joinlist&lt;/code&gt;, &lt;code&gt;src/backend/optimizer/path/allpaths.c:3325&lt;/code&gt;). The level above gets one finished result and can't look inside it.&lt;/p&gt;

&lt;p&gt;The fast plan we wanted is "for each course, use an index to find its enrollments". Postgres calls this a &lt;em&gt;parameterized nested loop&lt;/em&gt;. It only works when both tables are in the &lt;strong&gt;same&lt;/strong&gt; list. If &lt;code&gt;enrollment&lt;/code&gt; sits inside a sub-list and &lt;code&gt;course&lt;/code&gt; sits outside it, that lookup can't be built. Postgres isn't choosing the slow plan because it thinks it's cheaper. The fast plan &lt;strong&gt;doesn't exist&lt;/strong&gt; as an option.&lt;/p&gt;

&lt;h3&gt;
  
  
  4.3 Removal doesn't fix the shape
&lt;/h3&gt;

&lt;p&gt;When &lt;code&gt;remove_useless_joins&lt;/code&gt; removes a table, it deletes that table's entry from the join list (&lt;code&gt;remove_rel_from_joinlist&lt;/code&gt;, &lt;code&gt;analyzejoins.c:462&lt;/code&gt;). A sub-list stays a sub-list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;sublist&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;remove_rel_from_joinlist&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;List&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;jlnode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;relid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nremoved&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="cm"&gt;/* Avoid including empty sub-lists in the result */&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sublist&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lappend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sublist&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the borders drawn while ~435 tables were counted survive, even though only ~11 tables remain. At the default limit of 8, &lt;code&gt;course&lt;/code&gt; and &lt;code&gt;enrollment&lt;/code&gt; ended up on different sides of a border.&lt;/p&gt;

&lt;p&gt;Postgres's own code admits the order is a compromise. This comment sits right above the removal call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
 * Remove any useless outer joins.  Ideally this would be done during
 * jointree preprocessing, but the necessary information isn't available
 * until we've built baserel data structures and classified qual clauses.
 */&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In other words: the facts needed to decide "this join is useless" are collected in the same walk that builds the join list. So by the time Postgres knows what's useless, the groups are already drawn.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Seeing it happen: I added logging inside the planner
&lt;/h2&gt;

&lt;p&gt;Reading the code told me &lt;em&gt;what&lt;/em&gt; could happen. To see &lt;em&gt;what did&lt;/em&gt; happen, I compiled PostgreSQL 15.8 from the official source and added log lines inside &lt;code&gt;deconstruct_recurse&lt;/code&gt; (every JOIN decision) and &lt;code&gt;make_rel_from_joinlist&lt;/code&gt; (every final list):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;elog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NOTICE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"JOIN|right=%s|L=%d|R=%d|sum=%d|jcl=%d|act=%s"&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;I loaded a schema-only copy of the database (tables, views, and all &lt;strong&gt;270&lt;/strong&gt; primary keys and unique constraints, &lt;strong&gt;no rows&lt;/strong&gt;) and ran the report query.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; the constraints matter. My first copy had no primary keys, so Postgres couldn't prove any join was useless, nothing was removed, and the trace showed &lt;strong&gt;766&lt;/strong&gt; join-list entries instead of &lt;strong&gt;45&lt;/strong&gt;. An experiment without unique constraints measures the wrong tree.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What came out, per run:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;limit = 8&lt;/th&gt;
&lt;th&gt;limit = 64&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;JOIN decisions evaluated&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;739&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;739&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;merges&lt;/td&gt;
&lt;td&gt;656&lt;/td&gt;
&lt;td&gt;730&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;refusals (cuts)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;83&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;9&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;separate join problems&lt;/td&gt;
&lt;td&gt;75&lt;/td&gt;
&lt;td&gt;45&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;the &lt;code&gt;enrollment&lt;/code&gt; table ends up in a group of&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;1&lt;/strong&gt; (alone)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;5&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;execution time (copy of real data)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;137,779 ms&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;91 ms&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Same 739 decisions either way. The limit turned 83 cuts into 9, and that's the whole difference.&lt;/p&gt;

&lt;p&gt;At limit 8, the top of the tree showed three cuts, each &lt;strong&gt;just&lt;/strong&gt; over the limit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  L +  R  =  sum   vs 8   -&amp;gt;  decision
  7 +  4  =  11    vs 8   -&amp;gt;  refuse    &amp;lt;-- cut
  8 +  2  =  10    vs 8   -&amp;gt;  refuse    &amp;lt;-- cut
  6 +  3  =   9    vs 8   -&amp;gt;  refuse    &amp;lt;-- cut
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At limit 64, the decisive JOIN merged two halves of the query, one of 34 table references and one of 30:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; 34 + 30  =  64    vs 64  -&amp;gt;  MERGE     &amp;lt;-- the merge that puts course and enrollment together
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;34 + 30 = 64, against a limit of exactly 64. It passes by a margin of zero.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  5.1 Why the safe values have holes
&lt;/h3&gt;

&lt;p&gt;Look at what each decision returns:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a &lt;strong&gt;merge&lt;/strong&gt; returns a list of length L + R (it gets longer);&lt;/li&gt;
&lt;li&gt;a &lt;strong&gt;refusal&lt;/strong&gt; returns a list of length &lt;strong&gt;2&lt;/strong&gt; (it gets shorter).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So a merge that succeeds low in the tree makes a list &lt;em&gt;longer&lt;/em&gt;, and that longer list can then fail a check higher up. A refusal low in the tree makes a list &lt;em&gt;shorter&lt;/em&gt;, which can then pass higher up. Change the limit and some decisions flip at the bottom, which flips other decisions further up. Whether &lt;code&gt;course&lt;/code&gt; and &lt;code&gt;enrollment&lt;/code&gt; end up together jumps around as you change the limit. That's the holes: 12 fast, 14 slow, 16 fast.&lt;/p&gt;




&lt;h2&gt;
  
  
  6. What we shipped, and two traps
&lt;/h2&gt;

&lt;h3&gt;
  
  
  6.1 The fix
&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;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;join_collapse_limit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- the report query (and its count query)&lt;/span&gt;
&lt;span class="k"&gt;COMMIT&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;137,779 ms → 91 ms.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  6.2 Trap 1: bigger is not safer
&lt;/h3&gt;

&lt;p&gt;My own first recommendation was &lt;strong&gt;100&lt;/strong&gt;. The trace showed that at 100 the tree collapses so far that the biggest single list reaches &lt;strong&gt;12&lt;/strong&gt; tables, exactly &lt;code&gt;geqo_threshold&lt;/code&gt;, so Postgres switches to the genetic optimiser and plans stop being deterministic. Confirmed on the copy of real data by changing &lt;code&gt;geqo_seed&lt;/code&gt; (if the plan changes when the seed changes, GEQO is running):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;jcl=48  : no GEQO     jcl=64  : no GEQO     jcl=72 : no GEQO
jcl=80  : no GEQO     jcl=100 : GEQO FIRES
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The usable window for this query was &lt;strong&gt;48–80&lt;/strong&gt;: fast plan, deterministic. 64 sits in the middle of it.&lt;/p&gt;

&lt;p&gt;Raising &lt;code&gt;from_collapse_limit&lt;/code&gt; as well (the sibling setting for comma-joins and subqueries) didn't help either. It made several values flip from fast to slow. It's a second axis with its own holes.&lt;/p&gt;

&lt;h3&gt;
  
  
  6.3 Trap 2: never set it globally
&lt;/h3&gt;

&lt;p&gt;A global 64 is dangerous for other queries. In a separate test with a 12-table join where nothing can be removed, &lt;code&gt;join_collapse_limit = 64&lt;/code&gt; made Postgres switch that query to GEQO, while the default of 8 didn't. So:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;set it with &lt;strong&gt;&lt;code&gt;SET LOCAL&lt;/code&gt;&lt;/strong&gt;, only in the transaction that runs this query;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;SET LOCAL&lt;/code&gt; outside a transaction &lt;strong&gt;does nothing&lt;/strong&gt;: Postgres prints a warning and keeps the old value. Make sure your code really opens a transaction;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;SET LOCAL&lt;/code&gt; also resets at COMMIT, so the setting doesn't leak to the next request on a pooled connection.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6.4 Fixes that don't need the setting at all
&lt;/h3&gt;

&lt;p&gt;The setting is a workaround. These fix the cause:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Select fewer columns in the hot query.&lt;/strong&gt; Fetching 25 IDs first and then the details for just those 25 ran in &lt;strong&gt;5.5 ms&lt;/strong&gt; at the default limit, with no planner setting at all.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Add the unique index that join removal needs.&lt;/strong&gt; No setting can remove a join Postgres can't prove is unique.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fewer view layers&lt;/strong&gt; in hot paths.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One more honest number: planning alone took about &lt;strong&gt;440 ms&lt;/strong&gt; for this query, because of view expansion. That's a separate cost, and the setting doesn't touch it.&lt;/p&gt;




&lt;h2&gt;
  
  
  7. The twist: Postgres changed this in August 2026
&lt;/h2&gt;

&lt;p&gt;On 28 August 2026, Tom Lane committed &lt;strong&gt;&lt;code&gt;2ebf25e7d7&lt;/code&gt;&lt;/strong&gt;, "Perform join removal by editing the query's jointree".&lt;/p&gt;

&lt;h3&gt;
  
  
  7.1 What it does
&lt;/h3&gt;

&lt;p&gt;Before (15.8 up to the latest released versions):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;build facts + join list (all tables counted);&lt;/li&gt;
&lt;li&gt;remove useless joins, &lt;strong&gt;patching&lt;/strong&gt; the planner's notes by hand;&lt;/li&gt;
&lt;li&gt;plan with the &lt;strong&gt;old&lt;/strong&gt; join list.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;build facts + join list;&lt;/li&gt;
&lt;li&gt;remove useless joins by &lt;strong&gt;cutting them out of the query tree itself&lt;/strong&gt; (the LEFT JOIN node is replaced by its left side, &lt;code&gt;analyzejoins.c:399&lt;/code&gt; in master: &lt;code&gt;return j-&amp;gt;larg;&lt;/code&gt;);&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;throw away all notes and the join list, and start again&lt;/strong&gt; (&lt;code&gt;goto restart&lt;/code&gt;, &lt;code&gt;planmain.c:286&lt;/code&gt; in master);&lt;/li&gt;
&lt;li&gt;repeat until nothing more can be removed, then plan.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The final join list is built &lt;strong&gt;only from the tables that survived&lt;/strong&gt;. In effect: remove first, then group.&lt;/p&gt;

&lt;h3&gt;
  
  
  7.2 Why he did it: a bug that returned wrong rows
&lt;/h3&gt;

&lt;p&gt;He didn't do it for speed. He did it because patching the notes by hand kept causing bugs. Bug #19560 is a good example (reported on 19beta2, a regression starting in 16; 15.x is not affected):&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;items&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;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;owner&lt;/span&gt; &lt;span class="nb"&gt;text&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;follows&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item_id&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;user_id&lt;/span&gt; &lt;span class="nb"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item_id&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;items&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'item1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'alice'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;viewer&lt;/span&gt; &lt;span class="k"&gt;AS&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;'bob'&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;id&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;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&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;items&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;follows&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;follows&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;item_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;follows&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'bob'&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;viewer&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;TRUE&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;viewer&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The only item belongs to alice and the viewer is bob, so the answer is &lt;strong&gt;0&lt;/strong&gt;. Postgres returned &lt;strong&gt;1&lt;/strong&gt;. The plan became just &lt;code&gt;Aggregate -&amp;gt; Seq Scan on items&lt;/code&gt;: the &lt;code&gt;WHERE&lt;/code&gt; condition &lt;strong&gt;vanished&lt;/strong&gt;. &lt;code&gt;follows&lt;/code&gt; was removable (a unique key, no columns used), and removing it changed the planner's internal notes so that a new filter should have been created. The filters had already been created earlier, and nothing re-checked them.&lt;/p&gt;

&lt;p&gt;Tom's commit message: the old code "implemented the removal by updating those structures in-place … nothing keeps it in step with the rest of the planner", and "We have seen many related bugs over the years too, so it's time to do something drastic."&lt;/p&gt;

&lt;h3&gt;
  
  
  7.3 Which versions have it (as of 1 November 2026)
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Version&lt;/th&gt;
&lt;th&gt;Has the fix?&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;15.x&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Never.&lt;/strong&gt; It was back-patched only to 16&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;16.15, 17.11, 18.6 (latest releases)&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Not yet&lt;/strong&gt;: the fix is on those branches, but in no release tag yet&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Next minor releases, 12 Nov 2026&lt;/td&gt;
&lt;td&gt;Expected (fixes on a stable branch normally ship in the next minor)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;19 (RC1; final release planned 19 Nov 2026)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Yes&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  7.4 What it does NOT fix
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;More survivors than the limit.&lt;/strong&gt; Our query keeps about 11 tables. 11 &amp;gt; 8, so even on the new code the survivors are split again. In a synthetic test, 6 survivors at limit 8 went from 213–270 ms to 33–36 ms; &lt;strong&gt;11 survivors at limit 8 kept the same bad plan.&lt;/strong&gt; On 16+ we'd still need the limit at 11 or more.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chains cost planning time.&lt;/strong&gt; Each restart repeats the planning setup. Independent useless joins are all removed in one pass, but a join that only becomes removable after another one is gone waits for the next pass. Views built on views create exactly those chains. Measured by Richard Guo in the discussion of the change:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Shape&lt;/th&gt;
&lt;th&gt;Before&lt;/th&gt;
&lt;th&gt;After&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;64 independent useless joins&lt;/td&gt;
&lt;td&gt;1.11 ms&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.57 ms&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;a chain of 64 dependent removals&lt;/td&gt;
&lt;td&gt;1.98 ms&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;20.9 ms&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;How long the chains are in real layered-view schemas, and what that costs, hasn't been measured publicly as far as I know.&lt;/p&gt;




&lt;h2&gt;
  
  
  8. Check your own query in 15 minutes
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Run &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt;.&lt;/strong&gt; Look for &lt;code&gt;Join Filter&lt;/code&gt; on a join you expected to use an index, with a big &lt;code&gt;Rows Removed by Join Filter&lt;/code&gt;. Compare with what you'd expect: &lt;code&gt;Index Cond: (child.parent_id = parent.id)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Count tables before and after removal.&lt;/strong&gt; If your query reads from views, count the tables the view expands into (or look at the plan). If far more tables exist in the views than appear in the plan, removal is happening, and the grouping was done on the bigger count.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Try a few limits, inside a transaction:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&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;join_collapse_limit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="k"&gt;ANALYZE&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;Test several values, not just one. Expect holes.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Check GEQO.&lt;/strong&gt; Run the plan with two different &lt;code&gt;geqo_seed&lt;/code&gt; values. If the plan changes, the genetic optimiser is in play at that limit.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ship it scoped.&lt;/strong&gt; &lt;code&gt;SET LOCAL&lt;/code&gt; inside the transaction of that one query, never globally. Re-test when the views change: our winning merge had a margin of zero.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fix the cause when you can:&lt;/strong&gt; fewer columns in the hot query, the missing unique index, fewer view layers.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  What I'd tell my team
&lt;/h2&gt;

&lt;p&gt;When a tool shows you &lt;em&gt;what&lt;/em&gt; is slow but not &lt;em&gt;why&lt;/em&gt;, the source code can. Postgres is open, and the comments in the planner are honest about its own compromises. A fix solves one query. Understanding why it works solves the next ten.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do you build views on top of views in Postgres? How many levels deep did you go before the planner started fighting you?&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;References (public)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Postgres commit &lt;code&gt;2ebf25e7d7&lt;/code&gt;: &lt;a href="https://github.com/postgres/postgres/commit/2ebf25e7d70a8fce31ace78d723fa9271ab8af72" rel="noopener noreferrer"&gt;https://github.com/postgres/postgres/commit/2ebf25e7d70a8fce31ace78d723fa9271ab8af72&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Bug #19560 report: &lt;a href="https://www.postgresql.org/message-id/19560-54cd7ede78d5e355%40postgresql.org" rel="noopener noreferrer"&gt;https://www.postgresql.org/message-id/19560-54cd7ede78d5e355%40postgresql.org&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Discussion thread: &lt;a href="https://www.postgresql.org/message-id/flat/1186816.1784573544%40sss.pgh.pa.us" rel="noopener noreferrer"&gt;https://www.postgresql.org/message-id/flat/1186816.1784573544%40sss.pgh.pa.us&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;PostgreSQL docs, explicit JOIN control and &lt;code&gt;join_collapse_limit&lt;/code&gt;: &lt;a href="https://www.postgresql.org/docs/current/explicit-joins.html" rel="noopener noreferrer"&gt;https://www.postgresql.org/docs/current/explicit-joins.html&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;PostgreSQL roadmap (release dates): &lt;a href="https://www.postgresql.org/developer/roadmap/" rel="noopener noreferrer"&gt;https://www.postgresql.org/developer/roadmap/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Source files: &lt;code&gt;src/backend/optimizer/plan/planmain.c&lt;/code&gt;, &lt;code&gt;initsplan.c&lt;/code&gt;, &lt;code&gt;analyzejoins.c&lt;/code&gt;, &lt;code&gt;src/backend/optimizer/path/allpaths.c&lt;/code&gt; (line numbers from REL_15_8 and master as of 2026-11)&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>postgres</category>
      <category>backend</category>
      <category>database</category>
    </item>
  </channel>
</rss>
