I have a procedure like that;
CREATE OR REPLACE PROCEDURE insert_into_tableAv2(inout rows_affected INT) LANGUAGE 'plpgsql' AS $$ BEGIN
INSERT INTO insert_into_tableAv2 (serial, aaa, ooo, UPDATED)
SELECT id, aaa, ooo, UPDATED
FROM tableA
WHERE NOT EXISTS (
SELECT 1
FROM tableAv2
WHERE tableA.id = tableAv2.serial AND tableA.aaa = tableAv2.aaa
)
LIMIT 100;
GET DIAGNOSTICS rows_affected = ROW_COUNT;
RETURN;
END; $$;
TableA has 9000 records in my DEV environment. Basically, it’s a pretty small table.
When I call it like that it makes RDS CPU %100, and cannot insert anything and timed-out as well.
DO $$
DECLARE
result int := 0; -- Initialize with a starting value
BEGIN
LOOP
result := 0;
-- Call the procedure and update the result
CALL insert_into_tableAv2(result);
-- Exit the loop if the result is 0
EXIT WHEN result = 0;
END LOOP;
END $$;
The reason I wrote it like a procedure is that I want to run similar queries in bigger tables and call it many times. So does anyone know why the procedure call is not working ?
3