After googleing a bit, it's plainly clear you cannot run vacuum from a function:
Postgres mailing - Vacuum behaviour in plpgsql function
You can't run VACUUM from inside a function because of memory-management limitations. In current sources there is an error check to prevent you from trying.
Stackoverflow - VACUUM cannot be executed from a function or multi-command string
Once the data is loaded, I would like to "vacuum analyze" the affected tables, both to recover the space from the deleted records and to accurately reflect the new contents. [...] When I run this, I get:
ERROR: VACUUM cannot be executed from a function or multi-command string
But I really need to "automatically" run vacuum whenever a certain function is run: this function updates a lot of records (if not all of them) so the number of dead rows increases quite fast and other query performance decrease a lot. The problem is the function is called from another program. Any best practice? Should I manually add the vacuum statement into a separeted piece of code after the function execution, someting like
sql = SELECT my_function()
<program.execute(sql)>
sql = VACUUM [FULL | ANALYZE] my_updated_table
<program.execute(sql)>
VACUUM FULL
, exclusively locking the table for the duration? If there's a large number of dead rows, can you instead useCLUSTER
?VACUUM FULL
exclusive lock. Yes, there's a large number of dead rows (last time 50% ~ 4GB). I've tried to manually run cluster (from the client PgAdmin III) but I get the errorERROR: there is no previously clustered index for table "my_table"