title: IN subquery's WHERE condition affects main query - Is this a feature or a bug?
body: <p>Assume the two tables:</p>
  
  <pre><code>Table A: A1, A2, A_Other
  Table B: B1, B2, B_Other
  </code></pre>
  
  <p><em>In the following examples,</em> <code>is something</code> <em>is a condition checked against a fixed value, e.g.</em> <code>= 'ABC'</code> <em>or</em> <code>&lt; 45</code>.</p>
  
  <p>I wrote a query like the following <strong>(1)</strong>:</p>
  
  <pre><code>Select * from A
  Where A1 IN (
      Select Distinct B1 from B
      Where B2 is something
      And A2 is something
  );
  </code></pre>
  
  <p>What I really meant to write was <strong>(2)</strong>:</p>
  
  <pre><code>Select * from A
  Where A1 IN (
      Select Distinct B1 from B
      Where B2 is something
  )
  And A2 is something;
  </code></pre>
  
  <p><strong>Strangely,</strong> both queries returned the same result. When looking at the <em>explain plan</em> of query <strong>1</strong>, it looked like when the subquery was executed, because the condition <code>A2 is something</code> was not applicable to the subquery, it was <strong>deferred</strong> for use as a filter on the main query results.</p>
  
  <p>I would normally expect query <strong>1</strong> to fail because the subquery <em>by itself</em> would fail:</p>
  
  <pre><code>Select Distinct B1 from B
  Where B2 is something
  And A2 is something; --- ERROR:  column "A2" does not exist
  </code></pre>
  
  <p>But I find this is not the case, and Postgres defers inapplicable subquery conditions to the main query.</p>
  
  <p>Is this standard behaviour or a Postgres anomaly? <strong>Where is this documented, and what is this feature called?</strong></p>
  
  <p>Also, I find that if I add a column <code>A2</code> in table <code>B</code>, only query <strong>2</strong> works as originally intended. In this case the reference <code>A2</code> in query <strong>2</strong> would still refer to <code>A.A2</code>, but the reference in query <strong>1</strong> would refer to the new column <code>B.A2</code> because it is now applicable directly in the subquery.</p>