title: Does PostgreSQL short-circuit its BOOL_OR() evaluation?
body: <h3><code>EXISTS</code> is faster than <code>COUNT(*)</code> because it can be short-circuited</h3>
  
  <p>A lot of times, I like to check for existence of things in SQL. For instance, I do:</p>
  
  <pre><code>-- PostgreSQL syntax, SQL standard syntax:
  SELECT EXISTS (SELECT .. FROM some_table WHERE some_boolean_expression)
  
  -- Oracle syntax
  SELECT CASE 
    WHEN EXISTS (SELECT .. FROM some_table WHERE some_boolean_expression) THEN 1 
    ELSE 0 
  END
  FROM dual
  </code></pre>
  
  <p>In most databases, <code>EXISTS</code> is "short-circuited", i.e. the database can stop looking for rows in the table as soon as it has found one row. <a href="https://blog.jooq.org/2016/09/14/avoid-using-count-in-sql-when-you-could-use-exists" rel="noreferrer">This is usually much faster than comparing <code>COUNT(*) &gt;= 1</code> as can be seen in this blog post</a>.</p>
  
  <h3>Using <code>EXISTS</code> with <code>GROUP BY</code></h3>
  
  <p>Sometimes, I'd like to do this for each group in a <code>GROUP BY</code> query, i.e. I'd like to "aggregate" the existence value. There's no <code>EXISTS</code> aggregate function, but PostgreSQL luckily supports the <a href="https://www.postgresql.org/docs/current/static/functions-aggregate.html" rel="noreferrer"><code>BOOL_OR()</code></a> aggregate function, like in this statement:</p>
  
  <pre><code>SELECT something, bool_or (some_boolean_expression)
  FROM some_table
  GROUP BY something
  </code></pre>
  
  <p><a href="https://www.postgresql.org/docs/current/static/functions-aggregate.html" rel="noreferrer">The documentation mentions something about <code>COUNT(*)</code> being slow</a> because of the obvious sequential scan needed to calculate the count. But unfortunately, it doesn't say anything about <code>BOOL_OR()</code> being short-circuited. Is it the case? Does <code>BOOL_OR()</code> stop aggregating new values as soon as it encounters the first <code>TRUE</code> value?</p>