title: Redshift + SQLAlchemy long query hangs
body: <p>I'm doing something among the lines of:</p>
  
  <pre><code>conn_string = "postgresql+pg8000://%s:%s@%s:%d/%s" % (db_user, db_pass, host, port, schema)
  conn = sqlalchemy.engine.create_engine(conn_string,execution_options={'autocommit':True},encoding='utf-8',isolation_level="AUTOCOMMIT") 
  rows = cur.execute(sql_query)
  </code></pre>
  
  <p>To run queries on a Redshift cluster. Lately, I've been doing maintenance tasks such as running <code>vacuum reindex</code> on large tables that get truncated and reloaded every day.</p>
  
  <p>The problem is that that command above takes around 7 minutes for a particular table (the table is huge, 60 million rows across 15 columns) and when I run it using the method above it just never finishes and hangs. I can see in the cluster dashboard in AWS that parts of the vacuum command are being run for about 5 minutes and then it just stops. No python errors, no errors on the cluster, no nothing.</p>
  
  <p>My guess is that the connection is lost during the command. So, how do I prove my theory? Anybody else with the issue? What do I change the connection string to keep it alive longer? </p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I change my connection this after the comments here:</p>
  
  <pre><code>conn = sqlalchemy.engine.create_engine(conn_string,
                                         execution_options={'autocommit': True},
                                         encoding='utf-8',
                                         connect_args={"keepalives": 1, "keepalives_idle": 60,
                                                               "keepalives_interval": 60},  
                                                          isolation_level="AUTOCOMMIT")
  </code></pre>
  
  <p>And it has been working for a while. However, it decided to start with the same behaviour for even larger tables in which the <code>vacuum reindex</code> actually takes around 45 minutes (at least that is my estimate, the command never finishes running in Python).</p>
  
  <p>How can I make this work regardless of the query runtime?</p>