Вопрос

I'm trying to implement an ETL process in Amazon Redshift that needs to recreate certain tables automatically, and I need the script to drop those tables only when they are already present in the database.

Is there any table catalog where I can check for the presence of a table? Or a command similar to the DROP TABLE IF EXISTS in PSQL?

Это было полезно?

Решение

Update:

Now Redshift supports DROP TABLE with IF EXISTS clause. http://docs.aws.amazon.com/redshift/latest/dg/r_DROP_TABLE.html


Since Redshift didn't support DROP TABLE IF EXISTS, we were handling it by getting existing table names.

To get table names on Redshift, I'm using pg_table_def table. For example, I run the following sql to get all table names excluding system tables.

SELECT
  schemaname, tablename
FROM
  pg_table_def
WHERE
  schemaname <> 'pg_catalog'
  AND schemaname <> 'information_schema'
  AND schemaname !~ '^pg_toast'
GROUP BY
  schemaname,tablename;

Here is a sample result.

 schemaname | tablename 
------------+-----------
 my_schema  | access_log
 my_schema  | error_log
 my_schema  | vmstats_log
 public     | users
 public     | groups

See the following link for the details.

Другие советы

How about instead of dropping the table you do an insert overwrite?

INSERT OVERWRITE INTO target 
SELECT s.* FROM staging s LEFT JOIN target t
ON s.primaryKey = t.primaryKey AND s.distKey = t.distKey
WHERE t.primaryKey IS NULL;

Otherwise try to explain to me why your doing what your doing and I will update with more help. This is a case of where by redshift design there is usually a reason why you can't do what you want, but there is a "redshift" way.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top