Is it normal for a function to acquire a database-level lock that prevents auto vacuuming and index creation on totally unrelated tables?
I've got a long-running stored procedure that reads from one table and writes SUM()
/AVG()
data to another table. During the execution of the script (which can take almost an hour on large systems), all auto vacuuming stops and attempts to do things like creating indexes (CREATE UNIQUE INDEX CONCURRENTLY
) on unrelated tables to block. As soon as the stored procedure completes, the blocked processes complete.
Thinking it was something within the stored procedure that was incorrectly locking some resource, I re-wrote it be literally just a sleep:
CREATE OR REPLACE FUNCTION summarize_day(p_agg_date date)
RETURNS int AS
$$
BEGIN
SELECT pg_sleep(120);
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
Even this stored do nothing procedure prevented auto vacuuming and other database operations.
Is there any way to code it to NOT lock the database for other operations while this long running procedure runs?