title: Postgres via Airflow not returning query result - frozen until timeout
body: <p>I'm using airflow 2.2.0 on docker and issuing several queries through an ETL pipeline. Everything is working fine until I reach a particular query which looks like this:</p>
  <pre><code>INSERT INTO final_table (
      col1,
      col2
  )
  SELECT
      col1,
      col2
  FROM some_temp_table
  LEFT JOIN some_other_tables
  </code></pre>
  <p>This query should take around 30 seconds to complete on the larger set of tables I've got. However, what happens is that the query is issued by Airflow to Postgres and then nothing gets returned for around 5 minutes until Airflow eventually crashes the pipeline due to not receiving any response in that particular task I believe.</p>
  <p>The error is the following:</p>
  <pre><code>psycopg2.OperationalError: server closed the connection unexpectedly
      This probably means the server terminated abnormally
      before or while processing the request.
      (...)
      self._execute_queries(connection=connection, cursor=cursor, query=query)
  psycopg2.InterfaceError: connection already closed
  </code></pre>
  <p>I've checked everything I could think of and can't detect anything weird. The <code>pg_stat_activity</code> table shows the query is always in active state and doesn't seem to be getting blocked by any other query.</p>
  <p>The logs of Postgres show that the query issued by Airflow is received by Postgres. Logs as follows:</p>
  <pre><code>[3244] LOG:  statement: BEGIN
  [3244] LOG:  statement: 
  
  INSERT INTO final_table (
      col1,
      col2
  )
  SELECT
      col1,
      col2
  FROM some_temp_table
  LEFT JOIN some_other_tables
  </code></pre>
  <p>But after that, no COMMIT command is issued by postgres (as it happens for all other transactions). The query just seems to hang on that state indefinitely.</p>
  <p>When I run this same exact query directly via the database client (DBeaver in this case), everything works fine and the query gets run and the table filled in 30 seconds. Also when I run the same ETL pipeline but with a smaller dataset, everything works well and the query gets run successfully via Airflow.</p>
  <p>I'm really confused as to what the problem may be and where to focus my attention on. If anyone has gone through something similar, any help would be highly appreciated.</p>