title: Workaround in Redshift for "ADD COLUMN IF NOT EXISTS"
body: <p>I'm trying to execute an S3 copy operation via Spark-Redshift and I'm looking to modify the Redshift table structure before running the copy command in order to add any missing columns (they should be all VARCHAR).</p>
  
  <p>What I'm able to do is send an SQL query before running the copy, so ideally I would have liked to ALTER TABLE ADD COLUMN IF NOT EXISTS column_name VARCHAR(256). Unfortunately, Redshift does not offer support for ADD COLUMN IF NOT EXISTS, so I'm currently looking for a workaround.</p>
  
  <p>I've tried to query the <code>pg_table_def</code> table to check for the existence of the column, and that works, but I'm not sure how to chain that with an ALTER TABLE statement. Here's the current state of my query, I'm open to any suggestions for accomplishing the above.</p>
  
  <pre><code>select 
      case when count(*) &lt; 1 then ALTER TABLE tbl { ADD COLUMN 'test_col' VARCHAR(256) }
      else 'ok'
      end
  from pg_table_def where schemaname = 'schema' and tablename = 'tbl' and pg_table_def.column = 'test_col'
  </code></pre>
  
  <p>Also, I've seen this question: <a href="https://stackoverflow.com/questions/42035068/redshift-add-column-if-not-exists">Redshift: add column if not exists</a>, however the accepted answer isn't mentioning how to actually achieve this.</p>