Generating analytics issue

Hello team,

We are experiencing an issue with the generation of analytics on our server. Our DHIS2 version is 2.40.5. The problem is that generating the analytics tables takes a very long time. The size of our database without analytics is 50 GB, but it can grow up to 1 TB with the analytics.

The issue is that sometimes the generation works, but other times it fails. We have a task scheduler set to run this generation every day, but we had to temporarily stop it to avoid disrupting our users. Additionally, we have noticed that some tables, which should be automatically created during the generation process, are not being created.
Caused by: org.postgresql.util.PSQLException: ERROR: relation "analytics_completenesstarget" does not exist Position: 57
We are also encountering 504 errors, which always seem to be related to these analytics.

Thanks in advance for your help
Best