12

Is there a way to add/alter table column encoding on the original table without creating a new table and select all content from old table into new table ?

4 Answers 4

10

Update: as pointed out by @gelin: since Oct 2020, altering column encodings in place is now supported: ALTER TABLE tablename ALTER COLUMN columnname ENCODE newencode. More info here.

No, this is not supported.

From the documentation, the options that you have:

  • apply a compression type, or encoding, to the columns in a table manually when you create the table
  • use the COPY command to analyze and apply compression automatically (on an empty table)
  • specify the encoding for a column when it is added to a table using the ALTER TABLE command

From the same documentation,

You cannot change the compression encoding for a column after the table is created.

2
  • 5
    Now (from Oct 2020) it's allowed as ALTER TABLE tablename ALTER COLUMN columnname ENCODE newencode. The announcement: aws.amazon.com/ru/about-aws/whats-new/2020/10/…
    – gelin
    Jan 13, 2021 at 11:34
  • Amazingly great news on this announcement. So painful to backfill a TB scale table with years of daily loads, now should be much easier vs. workarounds.
    – Merlin
    Apr 13, 2021 at 18:08
8

Ketan is correct. AWS does provide a utility, https://github.com/awslabs/amazon-redshift-utils/tree/master/src/ColumnEncodingUtility, that can take care of it for you though.

1
  • 1
    This utility essentially does what original question described. It creates a new table, then does "insert into <new_table> select * from <old_table>", then renames the tables.
    – David Ha
    Dec 19, 2018 at 18:45
4

Yes - this is now a supported option as of 20th Oct 2020, see AWS docs :

ALTER TABLE table_name 
{
| ALTER COLUMN column_name ENCODE new_encode_type 

https://aws.amazon.com/about-aws/whats-new/2020/10/amazon-redshift-supports-modifying-column-comprression-encodings-to-optimize-storage-utilization-query-performance/

https://docs.aws.amazon.com/redshift/latest/dg/r_ALTER_TABLE.html

2

You don' really have to create a parallel table with the new encoding. You could alter the existing table by adding a new column with the desired compression, update the new column with the values from the old column and finally, drop the old column.

1
  • I found that this method is very simple to implement, also setting the new column to the old column is pretty fast. Wonder what the caveat is though.
    – David Ha
    Dec 19, 2018 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.