title: PostgreSQL query to detect overlapping time ranges
body: <p>I have a table in PostgreSQL 9.2 that looks like this (simplified):</p>
  
  <pre><code>CREATE TABLE my_features
  (
    id integer NOT NULL,
    feature_id integer NOT NULL,
    begin_time timestamp NOT NULL,
    end_time timestamp
  )
  </code></pre>
  
  <p>For each feature_id there may be multiple rows with time ranges specified by begin_time/end_time. They may overlap, but this is relatively rare. I'm looking for a fast way to find all feature_ids that have/don't have <em>any</em> overlaps.</p>
  
  <p>I tried to do this using window functions, like this:</p>
  
  <pre><code>SELECT feature_id, bool_or(end_time &gt; lead(begin_time) OVER ts_win) OVER ts_win AS overlaps_any
  FROM my_features
  WINDOW ts_win AS (PARTITION BY feature_id ORDER BY begin_time)
  </code></pre>
  
  <p>... but this doesn't work:</p>
  
  <pre><code>ERROR:  window function calls cannot be nested
  </code></pre>
  
  <p>The algorithm is simple: order the rows for a given feature_id by begin_time and check if any end_time > the next begin_time (if any). I suspect there must be an easy way to do this, perhaps with tsrange functions, but can't seem to find it just now.</p>