0

As a follow up of this question Create FUNCTION in PostgreSQL from a Bash script I tried to extend it with my own parameters so that I can call this functions later in my shell script like that docker exec $CONTAINER_NAME psql -U dev -d $DB_DATABASE -v ON_ERROR_STOP=1 -c "select $DB_SCHEMA.truncate_tables('$DB_USERNAME','$DB_DATABASE');" .


 CREATE
OR REPLACE FUNCTION truncate_tables(dbUserName text, dbSchema text) RETURNS void LANGUAGE plpgsql AS $function$
  DECLARE
statements CURSOR FOR
SELECT tablename
FROM pg_tables
WHERE tableowner = dbUserName
  AND schemaname = dbSchema
  AND tablename not like 'flyway%';
BEGIN
FOR stmt IN statements LOOP
          EXECUTE 'TRUNCATE TABLE ' || quote_ident(stmt.tablename) || ';';
END LOOP;
END
  $function$;
 

So far my shell script runs but it doesn't truncate anything nor it fails.

When I hardcode values however it runs perfectly. I think I am missing smth from postgres syntax here.

SELECT tablename
FROM pg_tables
WHERE tableowner = 'dev'
  AND schemaname = 'mySchema'
  AND tablename not like 'flyway%';

UPD1:

Added schema name to a truncation, but still nothing happens (no failures either)

 create OR REPLACE FUNCTION truncate_tables(dbUserName IN varchar, dbSchema in varchar) 
 RETURNS void LANGUAGE plpgsql AS $function$
  DECLARE
statements CURSOR FOR
SELECT tablename FROM pg_tables
WHERE tableowner = dbUserName
  AND schemaname = dbSchema
  AND tablename not like 'flyway%';
begin
    raise notice 'Value: %', statements;
FOR stmt IN statements LOOP
          EXECUTE format('TRUNCATE %i.TABLE ' || quote_ident(stmt.tablename) || ';', dbSchema);
END LOOP;
END
  $function$;

UPD 2: Another attempt, but still data is not truncated :/

CREATE OR REPLACE FUNCTION truncate_tables(dbUserName IN varchar, dbSchema in varchar) RETURNS VOID AS $$
DECLARE
    v_parent_rec RECORD;
BEGIN
    FOR v_parent_rec IN SELECT tablename FROM pg_tables
        WHERE tableowner = dbUserName
        AND schemaname = dbSchema
        AND tablename not like 'flyway%' loop
        PERFORM  format('TRUNCATE %I.TABLE ' || quote_ident(v_parent_rec) || ';', dbSchema);
    END LOOP;
    RETURN;
END;
$$ LANGUAGE plpgsql;
6
  • Print the contents of the cursor to the console (raise notice / return / etc) before running execute to check that the syntax is correct (e.g. no problem with apostrophes) and the input parameters are being used (and not NULL) Commented Jun 13, 2022 at 16:39
  • 1
    The immediate problem I see is that you're checking tables in schema dbSchema, but are truncating without schema name. Commented Jun 13, 2022 at 16:44
  • @Jesusbrother added ...begin raise notice 'Value: %', statements; and got : Value: statements Commented Jun 13, 2022 at 16:45
  • Also, your code seems to use stmt variable. which is not defined. I guess your function "works" (as opposed to errors out) simply because you're using cursor, and the iteration on it, should be done differently. Commented Jun 13, 2022 at 16:51
  • @eijeze could you please elaborate on that? btw adding schema name to a truncation didn't work either Commented Jun 13, 2022 at 16:53

1 Answer 1

1

This should, I think, work:

CREATE OR REPLACE FUNCTION truncate_tables(dbUserName text, dbSchema text)
    RETURNS void LANGUAGE plpgsql AS
$function$
DECLARE
    v_sql TEXT;
BEGIN
    FOR v_sql IN SELECT format('TRUNCATE TABLE %I.%I', schemaname, tablename)
        FROM pg_tables
        WHERE tableowner = dbUserName AND schemaname = dbSchema AND tablename NOT like 'flyway%'
    LOOP
        raise notice 'Will try to do: %', v_sql;
        -- If notices look sane, uncomment next line:
        -- EXECUTE v_sql;
    END LOOP;
    END;
$function$;
Sign up to request clarification or add additional context in comments.

4 Comments

Still not working :/
What do you mean "not working"? What did it output?
There is empty output unfortunately and data is still in. I have attached another version. @eijeze
Turned out that I had a typo in my db name, now everything worked! Thanks!

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.