0

I want to print all tables that I got from the query. However my code displays only the name of the function.

# create or replace function remove_scene()
# RETURNS void AS $$
# DECLARE
# row record;
# BEGIN
# FOR row IN
# select table_name from information_schema.tables WHERE table_schema = 'public' AND table_name ~ 'measurement[0-9]'
# LOOP
# RAISE NOTICE '** %', quote_ident(row.table_name);
# END LOOP;
# END;
# $$ LANGUAGE plpgsql;
CREATE FUNCTION
 # select remove_scene();
 remove_scene
--------------

(1 row)

#

1 Answer 1

0

The reason you are not seeing anything is that your function doesn't return anything. Text that is "written" using RAISE NOTICE will only be displayed in the client if a) the client supports that (which psql does) and b) if your session is configured to actually return them. In order for your raise to be seen in psql you need to use:

set client_min_messages = NOTICE;

Your current setting is probably WARNING and therefor a NOTICE is not displayed.

However: doing this in a loop with RAISE NOTICE is very inefficient. It would be much better to change your function into a set returning function:

create or replace function remove_scene()
  RETURNS table(table_name text) 
AS $$
   select table_name 
   from information_schema.tables 
   WHERE table_schema = 'public' 
     AND table_name ~ 'measurement[0-9]'
$$ 
LANGUAGE sql;

and then use it like this:

select *
from remove_scene();

Another option would be to put that select statement into a view.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.