0

I have created the following function to truncate bunch of tables starting with "irm_gtresult". There are no syntax errors in my function, but the function doesn't truncate the tables when I run it. What could be wrong here?

My Postgres db version is 8.4.

create or replace function br()
RETURNS void
LANGUAGE plpgsql
AS
$$
DECLARE
row text;
BEGIN
 FOR row IN
select table_name from information_schema.tables where table_name ILIKE 'irm_gtresult%'
LOOP
EXECUTE 'TRUNCATE TABLE ' || row;
END LOOP;
END;
$$;

Call:

select br();
7
  • 1
    You are querying all the tables that start with irm_gtresult, not irm. Is that intentional? Commented Jun 16, 2015 at 13:51
  • I pasted the code it is not a sceenshot, I judge upon size of the directory where the database resides. Yes, I want intentionally truncate those table that start with 'irm_gtresult'. In fact the standalone select table_name from information_schema.tables where table_name ILIKE 'irm_gtresult%' returns the list of tables exactly that I want to truncate. Commented Jun 16, 2015 at 14:04
  • You need to provide more information. Your version of Postgres? How exactly do you run it? Connected to the right database? Do you get any messages? If 'irm_gtresult%' is the pattern you want, please fix the text of your question. Commented Jun 16, 2015 at 14:05
  • Use row. table_name in your TRUNCATE statement, not just row. Commented Jun 16, 2015 at 14:09
  • 1
    @FrankHeikens: row isn't actually a row in his code. That's just a very unfortunate variable name. Commented Jun 16, 2015 at 14:11

2 Answers 2

1

Your code is valid. I tested and it works for me in Postgres 9.4.
Using the outdated and unsupported version 8.4 (like you added) may be the problem. The version is just too old, consider upgrading to a current version.

However, I have a couple of suggestions:

  • Don't use key word row as variable name.
  • You don't need to loop, you can TRUNCATE all tables in a single command. Faster, shorter.
  • You may need to add CASCADE if there are dependencies. Be aware of the effect.

CREATE OR REPLACE FUNCTION br()
  RETURNS void AS
$func$
BEGIN
   EXECUTE (
      SELECT 'TRUNCATE TABLE '
          || string_agg(format('%I.%I', schemaname, tablename), ',')
          || ' CASCADE'
      FROM   pg_tables t
      WHERE  tablename ILIKE 'irm_gtresult%'
      AND    schemaname = 'public'
      -- AND tableowner = 'postgres'  -- optionaly restrict to one user
      );
END
$func$  LANGUAGE plpgsql;

Call:

SELECT br();

I am using the view pg_tables from the system catalog. You can as well use information_schema.tables like you did. Note the subtle differences:

Related answers with more explanation:

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

2 Comments

Another potential reason: d88 is not using auto commit and simply not committing
Guys, thank you very much for your help! Adding 'CASCADE' into the statement solved the issue.And sorry for my annoying style of writing the post, next time I will bear in mind the rules;)
0

To truncate in postgres you just have to use the TRUNC() function.

Example:

SELECT TRUNC(price, 0) AS truncated_price
FROM product;

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.