Search code examples
c#wpfpostgresqlautovacuum

Tuning autovacuum to run when database schema changes?


Whenever i upgrade my database schema from software(code) via multiple migrations, some database operations takes lot of time to complete. I understood pgstats that, something was messed up and query plan has got modified. The same operations will complete in 2-3 seconds after running ANALYZE manually.

Digging more into autovacuum and analyze made me understand what it does. But my requirement is to run ANALYZE after my db schema is updated.

My application is using Entity framework code first approach. The database schema updates when my software is launched and booted up. Even though my postgres autovacuum parameter is set to ON, it doesnt run when schema updates. So, I need to run ANALYZE after my software has booted up.

How can i run ANALYZE on the whole database from my code after the software has booted up and just before any clicks happen ? Its a C# WPF application.


Solution

  • You have to do it explicitly. Schema changes don't trigger an analyze, and even if it did, it would take a while to finish processing.

    But it should be simple to add an ANALYZE for each affected table as part of the upgrade procedure.