I am trying to write a function to get the list of objects in schema from Redshift. I have created a dblink from RDS PostgreSQL to Redshift. The query is working just fine when invoked individually, but not working when written inside a function with arguments. I want to pass multiple arguments (schema names), hence I used VARIADIC arguments. The function looks like below -
CREATE FUNCTION f_fetch_tables(VARIADIC list text[])
RETURNS VOID
AS $$
DECLARE
begin_time TIMESTAMP;
expire_time TIMESTAMP;
BEGIN
/* To fetch the list of all objects from Redshift */
EXECUTE 'drop table if exists tmp_rs_obj_list;
create table tmp_rs_obj_list as
SELECT * FROM dblink(''rs_link'',$REDSHIFT$ select * from (select schemaname,
tablename from pg_tables UNION select schemaname, viewname from pg_views) where schemaname
not in (array_to_string($1,'','')) $REDSHIFT$) AS t1 (schema_nm varchar(30), obj_nm varchar(100))' using list;
END;
$$
LANGUAGE plpgsql
;
The function compiles fine and is created successfully, but I am not able to figure out a way to call it -
Used these calls so far, without any luck -
select f_fetch_tables('{public,pg_catalog}')
ERROR: there is no parameter $1 Where: Error occurred on dblink connection named "unnamed": could not execute query.
select * from f_fetch_tables(VARIADIC '{public,pg_catalog}')
ERROR: there is no parameter $1 Where: Error occurred on dblink connection named "unnamed": could not execute query.
Any suggestions would be really helpful.
Thank you, Kamlesh