无法通过存储过程访问Information_schema

时间:2019-09-09 11:13:45

标签: amazon-web-services amazon-redshift

我正在编写一个存储过程,该过程确实涉及在AWS Redshift中使用信息架构。

以下所有变量-

  1. 使用同一用户
  2. 使用相同的Redshift数据库(端点)

存储过程:

create or replace procedure dev.gp_information_schema_test
    (tablename varchar(64))
as $$
declare 
table_name varchar(64);
schema_name varchar(64);
counts int;
begin
table_name := split_part(tablename,'.',1);
schema_name:= split_part(tablename,'.',2);
raise info 'table_name - %,Schema_name - %',table_name,schema_name;
counts := (select count(*) from information_schema.tables where table_schema = schema_name);
raise info 'count is -%',counts;
end;
$$
language plpgsql

call dev.gp_information_schema_test('dev.abc');

结果:

Warnings:
table_name - dev,Schema_name - abc
count is -0

0 rows affected
call executed successfully

Execution time: 0.55s

但是,如果我在外部(即不通过存储过程)运行相同的查询,则:

select count(*) 
from information_schema.tables 
where table_schema = 'dev'

结果:

Result of query not from stored procedure

我已经阅读了AWS文档(Link)中存储过程的限制,但是没有提及对系统表的访问限制。

1 个答案:

答案 0 :(得分:0)

您无法使用:=从查询中设置变量的值。相反,您需要使用SELECT INTO variable表单。 https://docs.aws.amazon.com/redshift/latest/dg/c_PLpgSQL-structure.html

尝试此SP:

CREATE OR REPLACE PROCEDURE gp_information_schema_test(tablename VARCHAR(64)) AS
$$
DECLARE
    table_name  VARCHAR(64);
    schema_name VARCHAR(64);
    counts      INT;
BEGIN
    schema_name := split_part(tablename, '.', 1);
    table_name := split_part(tablename, '.', 2);
    RAISE INFO 'table_name - % , Schema_name - %',table_name,schema_name;
    counts := (SELECT count(*) FROM information_schema.tables WHERE table_schema = schema_name);
    RAISE INFO 'Tables in schema: %',counts;
END;
$$ LANGUAGE plpgsql;

致电:

CALL gp_information_schema_test('dev.abc');