0

I want to delete data from the Postgres table which are older than n days. For which I am using the below-stored procedure, I want to pass n days via parameter, but it's failing at runtime.

CREATE OR REPLACE PROCEDURE usp_deletelog(DaystoDelete integer)
language plpgsql    
as $$
DECLARE
  maxcurrenttime timestamp;
BEGIN
  maxcurrenttime := (SELECT MAX(log_time_to_timestamp) FROM public.interface_execution_log_VJ);
  RAISE NOTICE 'Value: %', maxcurrenttime;

  DELETE FROM public.interface_execution_log_VJ
  WHERE log_time_to_timestamp <= ( maxcurrenttime - '1 days'::interval);  -- works if hardcoded i.e. 1, how to pass the parameter instead i.e. DaystoDelete with single quotes ?

  COMMIT;
END;$$;

1 Answer 1

2

I prefer the function make_interval() for that:

maxcurrenttime - make_interval(days => daystodelete); 

Alternatively you can multiply a one day interval:

maxcurrenttime - interval '1 day' * daystodelete
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.