Redshift でデータベースを跨いだ Late Binding View (LBV) へのアクセスで "ERROR: cross-database reference to xx found while analyzing an LBV on producer." が発生する。
事象
- データベースを作成する
$ psql -h redshift-cluster-poc-central.********.ap-northeast-1.redshift.amazonaws.com -p 5439 -d dev -U awsuser create database table_db; create database view_db; create database other_db;
- データベース "table_db" にテーブルを作成する
$ psql -h redshift-cluster-poc-central.********.ap-northeast-1.redshift.amazonaws.com -p 5439 -d table_db -U awsuser create table table_db.public.t1 (id int); insert into table_db.public.t1 values (1),(2),(3); select * from table_db.public.t1; id ---- 1 2 3 (3 rows)
- データベース "view_db" に Late Binding View (LBV) を作成する。
$ psql -h redshift-cluster-poc-central.********.ap-northeast-1.redshift.amazonaws.com -p 5439 -d view_db -U awsuser create view view_db.public.view_a as select * from table_db.public.t1 WITH NO SCHEMA BINDING; select * from view_db.public.view_a; id ---- 1 2 3 (3 rows)
- データベース "other_db" から "view_db.public.view_a" にアクセスする。
$ psql -h redshift-cluster-poc-central.********.ap-northeast-1.redshift.amazonaws.com -p 5439 -d other_db -U awsuser select * from view_db.public.view_a; ERROR: cross-database reference to table_db.public.t1 found while analyzing an LBV on producer.
- データベース "table_db" から "view_db.public.view_a" にアクセスする。
$ psql -h redshift-cluster-poc-central.********.ap-northeast-1.redshift.amazonaws.com -p 5439 -d table_db -U awsuser select * from view_db.public.view_a; ERROR: cross-database reference to table_db.public.t1 found while analyzing an LBV on producer.
原因
- LBV が存在するデータベース以外のデータベースに接続し、LBV が LBV と異なるデータベースのオブジェクトを参照しているため。
回避策
- LBV が存在するデータベースに接続してクエリを発行する。
補足
- データベースをまたがる通常 View は作成できない(LBV を作成しろと怒られる)。
$ psql -h redshift-cluster-poc-central.********.ap-northeast-1.redshift.amazonaws.com -p 5439 -d view_db -U awsuser create view view_db.public.view_b as select * from table_db.public.t1; ERROR: External tables are not supported in views HINT: Please use late binding view and add 'with no schema binding' at the query end.