Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

How to create a Function to truncate all Tables created by Particular User

In this post, I am sharing a PostgreSQL user define a function to truncate all Tables which are created by particular X User.
Sometimes, Database administrator or Database Developer creates a user only for test purpose, and this TEST user created an N number of test rows or tables which are required drop or truncate.
Using this function, you can truncate all tables of a database with the help of Owner and Schema input parameters.
CREATE OR REPLACE FUNCTION fn_TruncateAllTable
(
 InputUserName CHARACTER VARYING 
 ,InputSchemaName CHARACTER VARYING 
) RETURNS void AS $$
DECLARE
 statements CURSOR FOR
 SELECT tablename FROM pg_tables
 WHERE tableowner = InputUserName AND schemaname = InputSchemaName;
BEGIN
    FOR stmt IN statements LOOP
 EXECUTE 'TRUNCATE TABLE ' || quote_ident(stmt.tablename) || ' CASCADE;'; 
    END LOOP;
END;
$$ LANGUAGE plpgsql;

Comments

Popular posts from this blog

How to find the server is whether standby (slave) or primary(master) in Postgresql replication ?

7 Steps to configure BDR replication in postgresql

How to Get Table Size, Database Size, Indexes Size, schema Size, Tablespace Size, column Size in PostgreSQL Database

Ora2PG - Oracle/MySQL to Postgres DB migration Version 20.0

PostgreSQL Introduction