I have a function which inserts 1 month worth data From main tables into History tables. As postgres does not have stored procedure I have declared the insert and delete logic as a function. I want to know if there is a way to return the counts from the inserts and deletes as an output of the function. Following is the code.
CREATE FUNCTION public.insertDelete(integer)
RETURNS void
LANGUAGE plpgsql
AS $BODY$
DECLARE
saveTime timestamp;
BEGIN
saveTime := now();
INSERT INTO public.hist_One
(select * from public.main_One
WHERE udate < (saveTime - ($1::text || ' months')::interval));
INSERT INTO public.hist_Two
(select * from public.main_Two
WHERE udate < (saveTime - ($1::text || ' months')::interval));
INSERT INTO public.hist_Three
(select * from public.main_Three
WHERE udate < (saveTime - ($1::text || ' months')::interval));
delete from public.main_One
where udate < (saveTime - ($1::text || ' months')::interval);
delete from public.main_Two
where udate < (saveTime - ($1::text || ' months')::interval);
delete from public.main_Three
where udate < (saveTime - ($1::text || ' months')::interval);
END;
$BODY$;
If I try to return Query then the code looks something like this
CREATE FUNCTION public.insertDelete(integer)
RETURNS Table(Hist_One int, Main_one int)
LANGUAGE plpgsql
AS $BODY$
DECLARE
saveTime timestamp;
BEGIN
saveTime := now();
INSERT INTO public.hist_One
(select * from public.main_One
WHERE udate < (saveTime - ($1::text || ' months')::interval));
RETURN QUERY select count(*) from public.main_One
WHERE udate < (saveTime - ($1::text || ' months')::interval)
The problem though is I want to return for all the tables and with RETURN QUERY
I can do it only for one table. so How can I return a table that will list the inserts into the Hist tables and the deletes from Main tables ?