user3145945
user3145945

Reputation: 21

Need to truncate tables using plpgsql function

I have writen a pl/pgsql function to trucate tables. HIST_CLEANUP_TBL has 5 table_name in it. Out of 5, 1 table_name doesn't exist in the database.

Need is: It has to check all tables. If table does not exist's write the error to a error table and truncate the remaining table.

However, here when ever the unavailable table comes in the loop the function getting terminated.

How to captuture the error code and text in a variable and how to continue the loop though 1 table does not exist.

We are using postgresql 8.2

BEGIN  

    for recordvar in 
    SELECT 'truncate table  ' || TABLE_NAME  as SQRY  FROM HIST_CLEANUP_TBL
    loop
       QUERY1 := recordvar.SQRY;
       RAISE NOTICE 'QUERY: (%)', QUERY1;     

       BEGIN
       execute QUERY1;
       RAISE NOTICE 'QUERY EXECUTED SUCCESSFULLY';   
       EXCEPTION
       WHEN others
       THEN
       --GET STACKED DIAGNOSTICS 
      ERR_NUM := SQLSTATE;
      ERR_MESG := SQLERRM ;

       RAISE NOTICE 'ERROR WHILE EXECUTING THE QUERY : %',ERR_NUM;

      insert into Refresh_Error_tbl(sqlerr_code,sqlerr_desc,sql_desc, INSERT_DT,refresh_load_id) values(ERR_NUM,ERR_MESG,QUERY1,current_timestamp,'1');
       END;
      end loop; 
END;

Upvotes: 1

Views: 3329

Answers (1)

Tomasz Siorek
Tomasz Siorek

Reputation: 711

Cause of the error must be somewhere else. Following code is working properly:

CREATE TABLE hist_cleanup (
    table_name  text
);

CREATE TABLE table_a();
CREATE TABLE table_b();
--  table_c missing
CREATE TABLE table_d();
CREATE TABLE table_e();

INSERT INTO hist_cleanup
VALUES
    ('table_a')
,   ('table_b')
,   ('table_c')
,   ('table_d')
,   ('table_e');

CREATE TABLE refresh_error (
    sqlerr_code     text
,   sqlerr_desc     text
,   sql_desc        text
,   insert_dt       timestamp
,   refresh_load_id int
);

CREATE OR REPLACE FUNCTION cleanup()
RETURNS VOID AS $$
DECLARE
    _rec    record;
    _query  text;
BEGIN  

    FOR _rec IN (
        SELECT  table_name
        FROM    hist_cleanup
    ) LOOP
        _query := 'TRUNCATE TABLE ' || _rec.table_name;

        RAISE NOTICE 'QUERY: (%)', _query;     

        BEGIN
            EXECUTE _query;
            RAISE NOTICE 'QUERY EXECUTED SUCCESSFULLY';   
        EXCEPTION
            WHEN OTHERS
            THEN
                RAISE NOTICE 'ERROR WHILE EXECUTING THE QUERY: % %', SQLSTATE, SQLERRM;
                INSERT INTO refresh_error (
                    sqlerr_code
                ,   sqlerr_desc
                ,   sql_desc
                ,   insert_dt
                ,   refresh_load_id )
                VALUES (
                    SQLSTATE
                ,   SQLERRM
                ,   _query
                ,   CURRENT_TIMESTAMP
                ,   '1'
                );
        END;
    END LOOP;

    RETURN;

END $$ LANGUAGE plpgsql;

SELECT  cleanup();

The output:

NOTICE:  QUERY: (TRUNCATE TABLE table_a)
NOTICE:  QUERY EXECUTED SUCCESSFULLY
NOTICE:  QUERY: (TRUNCATE TABLE table_b)
NOTICE:  QUERY EXECUTED SUCCESSFULLY
NOTICE:  QUERY: (TRUNCATE TABLE table_c)
NOTICE:  ERROR WHILE EXECUTING THE QUERY: 42P01 relation "table_c" does not exist
NOTICE:  QUERY: (TRUNCATE TABLE table_d)
NOTICE:  QUERY EXECUTED SUCCESSFULLY
NOTICE:  QUERY: (TRUNCATE TABLE table_e)
NOTICE:  QUERY EXECUTED SUCCESSFULLY

Upvotes: 2

Related Questions